webslm 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -12
- package/dist/ai-sdk/index.d.ts +1 -1
- package/dist/ai-sdk/index.js +1 -1
- package/dist/cache/models.d.ts +6 -0
- package/dist/cache/models.js +11 -0
- package/dist/curated.d.ts +31 -1
- package/dist/curated.js +49 -5
- package/dist/engine.d.ts +33 -4
- package/dist/engine.js +451 -44
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/models.d.ts +23 -4
- package/dist/models.js +60 -5
- package/dist/native.d.ts +165 -0
- package/dist/native.js +243 -0
- package/dist/protocol.d.ts +12 -2
- package/dist/recommended-sampling.d.ts +54 -0
- package/dist/recommended-sampling.js +73 -0
- package/dist/simple.d.ts +20 -6
- package/dist/simple.js +33 -7
- package/dist/types.d.ts +81 -15
- package/native-manifest.json +47 -0
- package/package.json +2 -1
- package/wasm/relaxed/webslm_wasm.d.ts +26 -4
- package/wasm/relaxed/webslm_wasm.js +70 -17
- package/wasm/relaxed/webslm_wasm_bg.wasm +0 -0
- package/wasm/relaxed/webslm_wasm_bg.wasm.d.ts +5 -3
- package/wasm/simd/webslm_wasm.d.ts +26 -4
- package/wasm/simd/webslm_wasm.js +70 -17
- package/wasm/simd/webslm_wasm_bg.wasm +0 -0
- package/wasm/simd/webslm_wasm_bg.wasm.d.ts +5 -3
- package/wasm/threads/webslm_wasm.d.ts +26 -4
- package/wasm/threads/webslm_wasm.js +75 -22
- package/wasm/threads/webslm_wasm_bg.wasm +0 -0
- package/wasm/threads/webslm_wasm_bg.wasm.d.ts +5 -3
package/dist/protocol.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* `read` messages. `abort` and `readResult` get no answer.
|
|
7
7
|
*/
|
|
8
8
|
import type { SerializedError } from "./errors.js";
|
|
9
|
-
import type { Activations, AdapterMode, FileData, LoadProgress, LoraInfo, ModelInfo } from "./types.js";
|
|
9
|
+
import type { Activations, AdapterMode, Backend, FileData, LoadProgress, LoraInfo, ModelInfo } from "./types.js";
|
|
10
10
|
import type { WasmFlavour } from "./wasm-flavour.js";
|
|
11
11
|
/** A model source in a form that can be posted to a worker. */
|
|
12
12
|
export type WireSource = {
|
|
@@ -25,13 +25,23 @@ export type WireSource = {
|
|
|
25
25
|
};
|
|
26
26
|
export interface WireLoadOptions {
|
|
27
27
|
readonly quant: string;
|
|
28
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* The caller asked for `quant: "auto"` (and resolved it to `quant` for the
|
|
30
|
+
* WebAssembly engine): the native engine decides again, against the
|
|
31
|
+
* machine's memory and GPU.
|
|
32
|
+
*/
|
|
33
|
+
readonly autoQuant?: boolean;
|
|
34
|
+
/** `"auto"`, `"cpu"`, `"gpu"` or `"webgpu"`; the engine reports what it used. */
|
|
29
35
|
readonly device?: string;
|
|
36
|
+
/** `"auto"` (default), `"native"` or `"wasm"`. */
|
|
37
|
+
readonly backend?: Backend;
|
|
30
38
|
readonly contextLength?: number;
|
|
31
39
|
/** Scale of the adapter of a `load` request. */
|
|
32
40
|
readonly adapterScale?: number;
|
|
33
41
|
readonly adapterMode?: AdapterMode;
|
|
34
42
|
readonly cacheDir?: string;
|
|
43
|
+
/** `false` turns the converted-weight cache off. */
|
|
44
|
+
readonly weightCache?: boolean;
|
|
35
45
|
readonly deterministic?: boolean;
|
|
36
46
|
readonly activations?: Activations;
|
|
37
47
|
readonly wasmFlavour?: WasmFlavour;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sampling settings that model cards recommend: the table of the Rust
|
|
3
|
+
* engine (`crates/webslm-core/src/recommended_sampling.json`, used by the
|
|
4
|
+
* `webslm` command-line tool and server), copied here so that the package
|
|
5
|
+
* needs no file of the repository; `test/sampling.test.ts` checks that both
|
|
6
|
+
* stay identical. See `recommendedSampling` for how a checkpoint gets a set.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Sampling settings a model card recommends. Fields left out keep the
|
|
10
|
+
* engine's defaults (no top-k, top-p 1, min-p 0, no penalty).
|
|
11
|
+
*/
|
|
12
|
+
export interface SamplingDefaults {
|
|
13
|
+
readonly temperature: number;
|
|
14
|
+
readonly topK?: number;
|
|
15
|
+
readonly topP?: number;
|
|
16
|
+
readonly minP?: number;
|
|
17
|
+
readonly repetitionPenalty?: number;
|
|
18
|
+
readonly presencePenalty?: number;
|
|
19
|
+
}
|
|
20
|
+
/** The settings of one model family. */
|
|
21
|
+
export interface SamplingSet {
|
|
22
|
+
/** Whether the family reasons when `enable_thinking` is not set (Qwen3, LFM3). */
|
|
23
|
+
readonly thinksByDefault?: boolean;
|
|
24
|
+
/** When the model answers directly. */
|
|
25
|
+
readonly answer: SamplingDefaults;
|
|
26
|
+
/** When it reasons first (`answer` when absent). */
|
|
27
|
+
readonly thinking?: SamplingDefaults;
|
|
28
|
+
}
|
|
29
|
+
/** The table: sets by name, and which repositories and `model_type`s use them. */
|
|
30
|
+
export interface SamplingTable {
|
|
31
|
+
readonly sets: Readonly<Record<string, SamplingSet>>;
|
|
32
|
+
readonly repos: Readonly<Record<string, string>>;
|
|
33
|
+
readonly modelTypes: Readonly<Record<string, string>>;
|
|
34
|
+
}
|
|
35
|
+
export declare const RECOMMENDED_SAMPLING: SamplingTable;
|
|
36
|
+
/** What identifies a checkpoint for {@link samplingSet}. */
|
|
37
|
+
export interface CheckpointTraits {
|
|
38
|
+
/** Hub repository id (a `@revision` suffix is ignored). */
|
|
39
|
+
readonly repo?: string | undefined;
|
|
40
|
+
/** `model_type` of its `config.json`. */
|
|
41
|
+
readonly modelType?: string | undefined;
|
|
42
|
+
/** Whether its `generation_config.json` asks for sampling (`do_sample`). */
|
|
43
|
+
readonly doSample?: boolean | undefined;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The set of a checkpoint, the way the Rust engine picks it: a listed
|
|
47
|
+
* repository (case-insensitive) uses its set whatever its
|
|
48
|
+
* `generation_config.json` says; otherwise a checkpoint whose
|
|
49
|
+
* `generation_config.json` does not ask for sampling uses the set of its
|
|
50
|
+
* `model_type`, when listed; otherwise none.
|
|
51
|
+
*/
|
|
52
|
+
export declare function samplingSet(checkpoint: CheckpointTraits): SamplingSet | undefined;
|
|
53
|
+
/** The settings of `set` with or without reasoning (`think` unset: the family's default). */
|
|
54
|
+
export declare function settingsFor(set: SamplingSet, think?: boolean): SamplingDefaults;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sampling settings that model cards recommend: the table of the Rust
|
|
3
|
+
* engine (`crates/webslm-core/src/recommended_sampling.json`, used by the
|
|
4
|
+
* `webslm` command-line tool and server), copied here so that the package
|
|
5
|
+
* needs no file of the repository; `test/sampling.test.ts` checks that both
|
|
6
|
+
* stay identical. See `recommendedSampling` for how a checkpoint gets a set.
|
|
7
|
+
*/
|
|
8
|
+
// Each model card: SmolLM2 (its usage example), LFM2.5 and LFM3
|
|
9
|
+
// ("generation parameters", LFM3's "general" set), Qwen3 (best practices,
|
|
10
|
+
// thinking and non-thinking), Qwen3.5 (the settings its benchmarks used in
|
|
11
|
+
// each mode).
|
|
12
|
+
export const RECOMMENDED_SAMPLING = {
|
|
13
|
+
sets: {
|
|
14
|
+
smollm2: {
|
|
15
|
+
answer: { temperature: 0.2, topP: 0.9 },
|
|
16
|
+
},
|
|
17
|
+
lfm2: {
|
|
18
|
+
answer: { temperature: 0.1, topK: 50, repetitionPenalty: 1.05 },
|
|
19
|
+
},
|
|
20
|
+
lfm3: {
|
|
21
|
+
thinksByDefault: true,
|
|
22
|
+
answer: { temperature: 0.1, topK: 50, repetitionPenalty: 1.1 },
|
|
23
|
+
},
|
|
24
|
+
qwen3: {
|
|
25
|
+
thinksByDefault: true,
|
|
26
|
+
answer: { temperature: 0.7, topK: 20, topP: 0.8, minP: 0 },
|
|
27
|
+
thinking: { temperature: 0.6, topK: 20, topP: 0.95, minP: 0 },
|
|
28
|
+
},
|
|
29
|
+
qwen3_5: {
|
|
30
|
+
answer: { temperature: 0.7, topK: 20, topP: 0.8, minP: 0, presencePenalty: 1.5 },
|
|
31
|
+
thinking: { temperature: 0.6, topK: 20, topP: 0.95, minP: 0, presencePenalty: 1.5 },
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
repos: {
|
|
35
|
+
"HuggingFaceTB/SmolLM2-135M-Instruct": "smollm2",
|
|
36
|
+
"HuggingFaceTB/SmolLM2-360M-Instruct": "smollm2",
|
|
37
|
+
"LiquidAI/LFM2.5-350M": "lfm2",
|
|
38
|
+
"Qwen/Qwen3-0.6B": "qwen3",
|
|
39
|
+
"Qwen/Qwen3.5-0.8B": "qwen3_5",
|
|
40
|
+
"LiquidAI/LFM3-1.7B": "lfm3",
|
|
41
|
+
},
|
|
42
|
+
modelTypes: {
|
|
43
|
+
lfm2: "lfm2",
|
|
44
|
+
lfm3: "lfm3",
|
|
45
|
+
qwen3: "qwen3",
|
|
46
|
+
qwen3_5: "qwen3_5",
|
|
47
|
+
qwen3_5_text: "qwen3_5",
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* The set of a checkpoint, the way the Rust engine picks it: a listed
|
|
52
|
+
* repository (case-insensitive) uses its set whatever its
|
|
53
|
+
* `generation_config.json` says; otherwise a checkpoint whose
|
|
54
|
+
* `generation_config.json` does not ask for sampling uses the set of its
|
|
55
|
+
* `model_type`, when listed; otherwise none.
|
|
56
|
+
*/
|
|
57
|
+
export function samplingSet(checkpoint) {
|
|
58
|
+
const table = RECOMMENDED_SAMPLING;
|
|
59
|
+
const repo = checkpoint.repo?.split("@")[0]?.toLowerCase();
|
|
60
|
+
const byRepo = repo === undefined
|
|
61
|
+
? undefined
|
|
62
|
+
: Object.entries(table.repos).find(([id]) => id.toLowerCase() === repo)?.[1];
|
|
63
|
+
const name = byRepo ??
|
|
64
|
+
(checkpoint.doSample !== true && checkpoint.modelType !== undefined
|
|
65
|
+
? table.modelTypes[checkpoint.modelType]
|
|
66
|
+
: undefined);
|
|
67
|
+
return name === undefined ? undefined : table.sets[name];
|
|
68
|
+
}
|
|
69
|
+
/** The settings of `set` with or without reasoning (`think` unset: the family's default). */
|
|
70
|
+
export function settingsFor(set, think) {
|
|
71
|
+
const thinking = think ?? set.thinksByDefault === true;
|
|
72
|
+
return (thinking ? set.thinking : undefined) ?? set.answer;
|
|
73
|
+
}
|
package/dist/simple.d.ts
CHANGED
|
@@ -7,11 +7,16 @@ import { type Model } from "./models.js";
|
|
|
7
7
|
import type { ChatMessage, LoadModelOptions, ModelInfo, ModelSource } from "./types.js";
|
|
8
8
|
/**
|
|
9
9
|
* The model {@link load}, {@link ask} and {@link stream} use when none is
|
|
10
|
-
* given:
|
|
11
|
-
*
|
|
10
|
+
* given: LFM2.5 350M, small (about 710 MB to download once), ungated, quick
|
|
11
|
+
* on the CPU, and able to follow a conversation (the SmolLM2 checkpoints of
|
|
12
|
+
* the same size lose track of it after a turn or two).
|
|
13
|
+
*/
|
|
14
|
+
export declare const DEFAULT_MODEL = "LiquidAI/LFM2.5-350M";
|
|
15
|
+
/**
|
|
16
|
+
* Sampling temperature used when neither the call, the model card of a
|
|
17
|
+
* curated model (`recommendedSampling`) nor the checkpoint's
|
|
18
|
+
* `generation_config.json` sets one.
|
|
12
19
|
*/
|
|
13
|
-
export declare const DEFAULT_MODEL = "HuggingFaceTB/SmolLM2-360M-Instruct";
|
|
14
|
-
/** Sampling temperature used when neither the call nor the checkpoint's `generation_config.json` sets one. */
|
|
15
20
|
export declare const DEFAULT_TEMPERATURE = 0.7;
|
|
16
21
|
/** Largest number of new tokens of an answer, unless `maxTokens` says otherwise. */
|
|
17
22
|
export declare const DEFAULT_MAX_TOKENS = 512;
|
|
@@ -21,7 +26,9 @@ export interface PromptOptions {
|
|
|
21
26
|
readonly maxTokens?: number;
|
|
22
27
|
/**
|
|
23
28
|
* Sampling temperature; `0` is greedy (deterministic) decoding. Defaults to
|
|
24
|
-
* the
|
|
29
|
+
* the model card's recommended sampling for the curated models
|
|
30
|
+
* (`recommendedSampling`), else the checkpoint's `generation_config.json`
|
|
31
|
+
* value, else 0.7.
|
|
25
32
|
*/
|
|
26
33
|
readonly temperature?: number;
|
|
27
34
|
/** System prompt sent before the user message. */
|
|
@@ -222,12 +229,19 @@ export declare class ReasoningSplitter {
|
|
|
222
229
|
* Text without such a block is all answer. `open` is `true` while the block
|
|
223
230
|
* is not closed yet (a stream still thinking, or an answer cut by `maxTokens`).
|
|
224
231
|
*
|
|
232
|
+
* `opened` says that the prompt already opened the block (templates such as
|
|
233
|
+
* Qwen3.5's and LFM3's end with `<think>` when thinking is on), so the output
|
|
234
|
+
* starts inside it. Left out, it is inferred: an output that closes a block
|
|
235
|
+
* it never opened started inside one.
|
|
236
|
+
*
|
|
225
237
|
* ```ts
|
|
226
238
|
* splitReasoning("<think>\nThe user greets me.\n</think>\n\nHello!");
|
|
227
239
|
* // { text: "Hello!", reasoning: "The user greets me.", open: false }
|
|
240
|
+
* splitReasoning("The user greets me.\n</think>\n\nHello!");
|
|
241
|
+
* // the same
|
|
228
242
|
* ```
|
|
229
243
|
*/
|
|
230
|
-
export declare function splitReasoning(output: string): Answer & {
|
|
244
|
+
export declare function splitReasoning(output: string, opened?: boolean): Answer & {
|
|
231
245
|
readonly open: boolean;
|
|
232
246
|
};
|
|
233
247
|
/** @internal Runs async functions one at a time, in call order. */
|
package/dist/simple.js
CHANGED
|
@@ -4,15 +4,21 @@
|
|
|
4
4
|
* {@link Session}.
|
|
5
5
|
*/
|
|
6
6
|
import { AsyncQueue } from "./async-queue.js";
|
|
7
|
+
import { recommendedSampling } from "./curated.js";
|
|
7
8
|
import { WebslmError, abortError, isAborted, toWebslmError } from "./errors.js";
|
|
8
9
|
import { loadModel } from "./models.js";
|
|
9
10
|
/**
|
|
10
11
|
* The model {@link load}, {@link ask} and {@link stream} use when none is
|
|
11
|
-
* given:
|
|
12
|
-
*
|
|
12
|
+
* given: LFM2.5 350M, small (about 710 MB to download once), ungated, quick
|
|
13
|
+
* on the CPU, and able to follow a conversation (the SmolLM2 checkpoints of
|
|
14
|
+
* the same size lose track of it after a turn or two).
|
|
15
|
+
*/
|
|
16
|
+
export const DEFAULT_MODEL = "LiquidAI/LFM2.5-350M";
|
|
17
|
+
/**
|
|
18
|
+
* Sampling temperature used when neither the call, the model card of a
|
|
19
|
+
* curated model (`recommendedSampling`) nor the checkpoint's
|
|
20
|
+
* `generation_config.json` sets one.
|
|
13
21
|
*/
|
|
14
|
-
export const DEFAULT_MODEL = "HuggingFaceTB/SmolLM2-360M-Instruct";
|
|
15
|
-
/** Sampling temperature used when neither the call nor the checkpoint's `generation_config.json` sets one. */
|
|
16
22
|
export const DEFAULT_TEMPERATURE = 0.7;
|
|
17
23
|
/** Largest number of new tokens of an answer, unless `maxTokens` says otherwise. */
|
|
18
24
|
export const DEFAULT_MAX_TOKENS = 512;
|
|
@@ -128,18 +134,27 @@ export class ReasoningSplitter {
|
|
|
128
134
|
* Text without such a block is all answer. `open` is `true` while the block
|
|
129
135
|
* is not closed yet (a stream still thinking, or an answer cut by `maxTokens`).
|
|
130
136
|
*
|
|
137
|
+
* `opened` says that the prompt already opened the block (templates such as
|
|
138
|
+
* Qwen3.5's and LFM3's end with `<think>` when thinking is on), so the output
|
|
139
|
+
* starts inside it. Left out, it is inferred: an output that closes a block
|
|
140
|
+
* it never opened started inside one.
|
|
141
|
+
*
|
|
131
142
|
* ```ts
|
|
132
143
|
* splitReasoning("<think>\nThe user greets me.\n</think>\n\nHello!");
|
|
133
144
|
* // { text: "Hello!", reasoning: "The user greets me.", open: false }
|
|
145
|
+
* splitReasoning("The user greets me.\n</think>\n\nHello!");
|
|
146
|
+
* // the same
|
|
134
147
|
* ```
|
|
135
148
|
*/
|
|
136
|
-
export function splitReasoning(output) {
|
|
149
|
+
export function splitReasoning(output, opened) {
|
|
137
150
|
let text = "";
|
|
138
151
|
let reasoning = "";
|
|
152
|
+
const close = output.indexOf(CLOSE);
|
|
153
|
+
const startsInside = opened ?? (close !== -1 && !output.slice(0, close).includes(OPEN));
|
|
139
154
|
const splitter = new ReasoningSplitter({
|
|
140
155
|
text: (t) => (text += t),
|
|
141
156
|
reasoning: (t) => (reasoning += t),
|
|
142
|
-
},
|
|
157
|
+
}, startsInside);
|
|
143
158
|
splitter.push(output);
|
|
144
159
|
const open = splitter.inReasoning;
|
|
145
160
|
splitter.end();
|
|
@@ -300,7 +315,13 @@ class LLMImpl {
|
|
|
300
315
|
}, traits.opensReasoning(options.think));
|
|
301
316
|
const generation = {
|
|
302
317
|
maxTokens: options.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
303
|
-
|
|
318
|
+
// Curated models get their model card's sampling from the model
|
|
319
|
+
// itself (`recommendedSampling`).
|
|
320
|
+
...(options.temperature === undefined && hasRecommendedSampling(this.model.info())
|
|
321
|
+
? {}
|
|
322
|
+
: {
|
|
323
|
+
temperature: options.temperature ?? this.model.info().generationConfig.temperature ?? DEFAULT_TEMPERATURE,
|
|
324
|
+
}),
|
|
304
325
|
...(options.seed === undefined ? {} : { seed: options.seed }),
|
|
305
326
|
...(options.stop === undefined ? {} : { stop: options.stop }),
|
|
306
327
|
...passThrough(options),
|
|
@@ -326,6 +347,11 @@ class LLMImpl {
|
|
|
326
347
|
return this.#traits;
|
|
327
348
|
}
|
|
328
349
|
}
|
|
350
|
+
/** Whether the model card's sampling applies to `info` (`recommendedSampling`). */
|
|
351
|
+
function hasRecommendedSampling(info) {
|
|
352
|
+
const traits = { repo: info.repo, modelType: info.modelType, doSample: info.generationConfig.doSample };
|
|
353
|
+
return recommendedSampling(traits) !== undefined;
|
|
354
|
+
}
|
|
329
355
|
/** Generation options the simple API passes through unchanged. */
|
|
330
356
|
function passThrough(options) {
|
|
331
357
|
const keys = [
|
package/dist/types.d.ts
CHANGED
|
@@ -2,10 +2,25 @@
|
|
|
2
2
|
import type { EngineHandle } from "./models.js";
|
|
3
3
|
import type { WasmFlavour } from "./wasm-flavour.js";
|
|
4
4
|
/**
|
|
5
|
-
* Where the model runs. `"auto"` (the default) picks
|
|
6
|
-
* ~100M parameters when an adapter is available, the CPU otherwise.
|
|
5
|
+
* Where the model runs. `"auto"` (the default) picks the GPU for models above
|
|
6
|
+
* ~100M parameters when an adapter is available, the CPU otherwise. `"gpu"`
|
|
7
|
+
* is the GPU of the engine that runs: WebGPU in browsers, Metal, Vulkan or
|
|
8
|
+
* DX12 with the native engine in Node; `"webgpu"` is a synonym kept for
|
|
9
|
+
* existing code. A loaded model reports `"webgpu"` (WebAssembly engine),
|
|
10
|
+
* `"gpu"` (native engine) or `"cpu"`.
|
|
7
11
|
*/
|
|
8
|
-
export type Device = "auto" | "cpu" | "webgpu";
|
|
12
|
+
export type Device = "auto" | "cpu" | "gpu" | "webgpu";
|
|
13
|
+
/**
|
|
14
|
+
* Which build of the engine runs a model in Node (see
|
|
15
|
+
* {@link LoadModelOptions.backend}): `"native"` is the Node-API addon of the
|
|
16
|
+
* engine (native SIMD kernels on every core, the GPU through Metal, Vulkan or
|
|
17
|
+
* DX12), downloaded on first use from the GitHub Release of this version and
|
|
18
|
+
* checked against the SHA-256 the package carries; `"wasm"` the WebAssembly
|
|
19
|
+
* build (the only one in browsers). `"auto"` (the default) picks `"native"`
|
|
20
|
+
* when the addon is available (cached, or downloaded and verified) and the
|
|
21
|
+
* source is a hub model, else `"wasm"`.
|
|
22
|
+
*/
|
|
23
|
+
export type Backend = "auto" | "native" | "wasm";
|
|
9
24
|
/** How weight matrices are stored once loaded. */
|
|
10
25
|
export type Quant = "q8" | "q4" | "f32";
|
|
11
26
|
/**
|
|
@@ -109,11 +124,23 @@ export interface LoadProgress {
|
|
|
109
124
|
}
|
|
110
125
|
export interface LoadModelOptions {
|
|
111
126
|
/**
|
|
112
|
-
* Defaults to `"auto"`:
|
|
113
|
-
* adapter is available
|
|
114
|
-
*
|
|
127
|
+
* Defaults to `"auto"`: the GPU for models above ~100M parameters when an
|
|
128
|
+
* adapter is available (a hardware one with the native engine, which skips
|
|
129
|
+
* software rasterizers and checks the model fits the GPU's memory first),
|
|
130
|
+
* the CPU otherwise. With the WebAssembly engine, `"gpu"` (or `"webgpu"`)
|
|
131
|
+
* falls back to the CPU when no GPU device can be created; with the native
|
|
132
|
+
* engine it fails with `UNSUPPORTED_DEVICE` instead, as `webslm run --device
|
|
133
|
+
* gpu` does. {@link ModelInfo.deviceNote} says why a device was not used.
|
|
115
134
|
*/
|
|
116
135
|
readonly device?: Device;
|
|
136
|
+
/**
|
|
137
|
+
* The engine build in Node (see {@link Backend}). `"native"` fails with
|
|
138
|
+
* `UNSUPPORTED_DEVICE` when the addon is not available (no build for this
|
|
139
|
+
* platform, offline without it in the cache, a failed download or checksum,
|
|
140
|
+
* or a browser); `"wasm"` keeps the WebAssembly build. {@link
|
|
141
|
+
* ModelInfo.runtime} reports which one runs.
|
|
142
|
+
*/
|
|
143
|
+
readonly backend?: Backend;
|
|
117
144
|
/**
|
|
118
145
|
* Weight format. Defaults to `"q8"`; `"q4"` (4-bit, 16-value blocks) is
|
|
119
146
|
* smaller and less accurate. `"auto"` estimates the memory of the model
|
|
@@ -169,6 +196,14 @@ export interface LoadModelOptions {
|
|
|
169
196
|
* `~/.cache/webslm`). Ignored in browsers, which use OPFS.
|
|
170
197
|
*/
|
|
171
198
|
readonly cacheDir?: string;
|
|
199
|
+
/**
|
|
200
|
+
* Keep the converted (Q8_0 or 4-bit) weights of a hub model in the cache
|
|
201
|
+
* (OPFS in browsers, the cache directory in Node), so that the next load
|
|
202
|
+
* reads them instead of converting the checkpoint again; `info().weightCache`
|
|
203
|
+
* reports what the load did. Default `true`; `WEBSLM_QUANT_CACHE=0` turns
|
|
204
|
+
* it off in Node, as for the command-line tool.
|
|
205
|
+
*/
|
|
206
|
+
readonly weightCache?: boolean;
|
|
172
207
|
/**
|
|
173
208
|
* Run on CPU kernels whose results are bit-identical in every WebAssembly
|
|
174
209
|
* build and natively (the `webslm` CLI and the Rust crates), at some cost
|
|
@@ -182,19 +217,22 @@ export interface LoadModelOptions {
|
|
|
182
217
|
* isolated or in Node, `"relaxed"` with relaxed SIMD, else `"simd"`). Each
|
|
183
218
|
* worker instantiates one build: the first model loaded in it decides.
|
|
184
219
|
* A build missing from a package built from source (see the README)
|
|
185
|
-
* fails with `UNSUPPORTED_DEVICE`; the automatic choice skips it.
|
|
220
|
+
* fails with `UNSUPPORTED_DEVICE`; the automatic choice skips it. Under
|
|
221
|
+
* `backend: "auto"`, naming a build runs WebAssembly.
|
|
186
222
|
*/
|
|
187
223
|
readonly wasmFlavour?: WasmFlavour;
|
|
188
224
|
/**
|
|
189
225
|
* Size of the thread pool of the `"threads"` build. Defaults to the number
|
|
190
|
-
* of logical cores, at most 8.
|
|
226
|
+
* of logical cores, at most 8. On the native engine, the size of its CPU
|
|
227
|
+
* thread pool (default: the available cores, capped by the cgroup CPU
|
|
228
|
+
* quota), set by the first native load of the process.
|
|
191
229
|
*/
|
|
192
230
|
readonly threads?: number;
|
|
193
231
|
}
|
|
194
232
|
/** How the CPU backend runs a model. */
|
|
195
233
|
export interface CpuInfo {
|
|
196
|
-
/** The WebAssembly build. */
|
|
197
|
-
readonly flavour: WasmFlavour;
|
|
234
|
+
/** The WebAssembly build, or `"native"` for the native engine. */
|
|
235
|
+
readonly flavour: WasmFlavour | "native";
|
|
198
236
|
/** CPU kernel set, e.g. `"relaxed-simd"`. */
|
|
199
237
|
readonly kernels: string;
|
|
200
238
|
/** Threads the kernels use. */
|
|
@@ -212,11 +250,16 @@ export interface ChatMessage {
|
|
|
212
250
|
readonly role: "system" | "user" | "assistant" | (string & {});
|
|
213
251
|
readonly content: string;
|
|
214
252
|
}
|
|
215
|
-
/**
|
|
253
|
+
/**
|
|
254
|
+
* Options of a generation. Unset sampling options come from the model card's
|
|
255
|
+
* recommendation for the curated models (`CURATED_MODELS`, see
|
|
256
|
+
* `recommendedSampling`), else from the checkpoint's `generation_config.json`
|
|
257
|
+
* (greedy decoding when it does not ask for sampling).
|
|
258
|
+
*/
|
|
216
259
|
export interface GenerateOptions {
|
|
217
260
|
/** Largest number of new tokens (default 512). */
|
|
218
261
|
readonly maxTokens?: number;
|
|
219
|
-
/** `0` is greedy decoding. */
|
|
262
|
+
/** `0` is greedy decoding (and turns the recommended sampling off). */
|
|
220
263
|
readonly temperature?: number;
|
|
221
264
|
readonly topK?: number;
|
|
222
265
|
readonly topP?: number;
|
|
@@ -391,6 +434,19 @@ export interface GenerationConfig {
|
|
|
391
434
|
readonly minP?: number;
|
|
392
435
|
readonly repetitionPenalty?: number;
|
|
393
436
|
}
|
|
437
|
+
/** What a load did with the converted-weight cache. */
|
|
438
|
+
export interface WeightCacheInfo {
|
|
439
|
+
/**
|
|
440
|
+
* `"hit"`: the converted weights were read from the cache instead of
|
|
441
|
+
* converting the checkpoint; `"written"`: they were converted and stored
|
|
442
|
+
* for the next load; `"failed"`: storing them failed (nothing is kept);
|
|
443
|
+
* `"off"`: not used (turned off, a float format such as `"f32"`, a source
|
|
444
|
+
* other than the hub, or storage that cannot hold it).
|
|
445
|
+
*/
|
|
446
|
+
readonly status: "hit" | "written" | "failed" | "off";
|
|
447
|
+
/** Bytes read from the cache, or stored in it. */
|
|
448
|
+
readonly bytes: number;
|
|
449
|
+
}
|
|
394
450
|
/** Description of a loaded model. */
|
|
395
451
|
export interface ModelInfo {
|
|
396
452
|
/** Hub repository id, when loaded from the hub. */
|
|
@@ -399,11 +455,19 @@ export interface ModelInfo {
|
|
|
399
455
|
readonly revision?: string;
|
|
400
456
|
readonly modelType: string;
|
|
401
457
|
readonly architecture: string;
|
|
402
|
-
/**
|
|
458
|
+
/**
|
|
459
|
+
* Device actually used: `"cpu"`, `"webgpu"` (the WebAssembly engine on
|
|
460
|
+
* WebGPU) or `"gpu"` (the native engine on Metal, Vulkan or DX12).
|
|
461
|
+
*/
|
|
403
462
|
readonly device: Exclude<Device, "auto">;
|
|
463
|
+
/** The engine build that runs the model (see {@link LoadModelOptions.backend}). */
|
|
464
|
+
readonly runtime: Exclude<Backend, "auto">;
|
|
465
|
+
/** Why `backend: "auto"` did not pick the native engine, in Node. */
|
|
466
|
+
readonly runtimeNote?: string;
|
|
404
467
|
/**
|
|
405
|
-
* Human-readable backend, e.g. `"CPU (wasm, relaxed SIMD, 8 threads)"
|
|
406
|
-
* `"WebGPU (Apple M3)"
|
|
468
|
+
* Human-readable backend, e.g. `"CPU (wasm, relaxed SIMD, 8 threads)"`,
|
|
469
|
+
* `"WebGPU (Apple M3)"`, `"CPU (native, neon-i8mm, 10 threads)"` or
|
|
470
|
+
* `"GPU (native, Apple M5, Metal)"`.
|
|
407
471
|
*/
|
|
408
472
|
readonly backend: string;
|
|
409
473
|
/** How the CPU backend runs, when the model runs on it. */
|
|
@@ -434,6 +498,8 @@ export interface ModelInfo {
|
|
|
434
498
|
readonly files: readonly LoadedFile[];
|
|
435
499
|
/** Whether every file came from the local cache (no download). */
|
|
436
500
|
readonly fromCache: boolean;
|
|
501
|
+
/** What the load did with the converted-weight cache ({@link LoadModelOptions.weightCache}). */
|
|
502
|
+
readonly weightCache: WeightCacheInfo;
|
|
437
503
|
/** Milliseconds spent downloading and loading. */
|
|
438
504
|
readonly loadTimeMs: number;
|
|
439
505
|
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "0.4.0",
|
|
3
|
+
"assets": {
|
|
4
|
+
"darwin-arm64": {
|
|
5
|
+
"file": "webslm-node-0.4.0-darwin-arm64.node.gz",
|
|
6
|
+
"size": 4528545,
|
|
7
|
+
"sha256": "615b47fbe81fd696b2e7fde769248aecd19e3464293ea1e3eee4f09f0280dd3d",
|
|
8
|
+
"nodeSha256": "d38589792edf3ae420185d3d8801465016a086189cfa8448babecb0bee2c20c7"
|
|
9
|
+
},
|
|
10
|
+
"darwin-x64": {
|
|
11
|
+
"file": "webslm-node-0.4.0-darwin-x64.node.gz",
|
|
12
|
+
"size": 4776800,
|
|
13
|
+
"sha256": "96d2d94899c39189b65638be2a93e3aaf354f78bce4b64d70978151a79a2976f",
|
|
14
|
+
"nodeSha256": "ef8edd095b0cd6d0132ca7e014d3419b80ada134abed1c8e573eff46ab7e1bd9"
|
|
15
|
+
},
|
|
16
|
+
"linux-x64-gnu": {
|
|
17
|
+
"file": "webslm-node-0.4.0-linux-x64-gnu.node.gz",
|
|
18
|
+
"size": 4813554,
|
|
19
|
+
"sha256": "b4ed809cff282998a42cb20a54383009fbf251a5eb082bb8b81933c569a9fb30",
|
|
20
|
+
"nodeSha256": "53301dd894229e70c1a30a2d286459dbcb1e0239933e2bae9af3c5e38702fa82"
|
|
21
|
+
},
|
|
22
|
+
"linux-arm64-gnu": {
|
|
23
|
+
"file": "webslm-node-0.4.0-linux-arm64-gnu.node.gz",
|
|
24
|
+
"size": 4534591,
|
|
25
|
+
"sha256": "11f45efbde6cad74b963a967d376b656047ef41089972e7999d37a70065a1b4d",
|
|
26
|
+
"nodeSha256": "3819d06ada9a926ad72ee61c47aed19eafdda89688e324b79af0402f6a085d2d"
|
|
27
|
+
},
|
|
28
|
+
"linux-x64-musl": {
|
|
29
|
+
"file": "webslm-node-0.4.0-linux-x64-musl.node.gz",
|
|
30
|
+
"size": 5175333,
|
|
31
|
+
"sha256": "5939e77ca6cbfa27a97936ec1c5e801bea08d859265cb8fa29fb4633f1da54c1",
|
|
32
|
+
"nodeSha256": "19179c923bffca73df39d8d8866e56584e7cb8cce934d5579e9e416b90311912"
|
|
33
|
+
},
|
|
34
|
+
"linux-arm64-musl": {
|
|
35
|
+
"file": "webslm-node-0.4.0-linux-arm64-musl.node.gz",
|
|
36
|
+
"size": 5008989,
|
|
37
|
+
"sha256": "5729dfb3c257597d9fec34e1894df4ce6e1e93ea8d47ca5970ca763e2bbdc657",
|
|
38
|
+
"nodeSha256": "285f4f9e86fc9b5e558e78135f20a169c55db235a6df460aedb50dd708afdff9"
|
|
39
|
+
},
|
|
40
|
+
"win32-x64-msvc": {
|
|
41
|
+
"file": "webslm-node-0.4.0-win32-x64-msvc.node.gz",
|
|
42
|
+
"size": 5248826,
|
|
43
|
+
"sha256": "dee69d5d9b6d3d51f68238dc3698e42aca1cd5b752b3047d76d426811e6525cf",
|
|
44
|
+
"nodeSha256": "a43e8046bd46189329b45c8ed3837e7a431dd35dcba63f79d5a4da98ed97076f"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webslm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Run small language models in the browser and in Node, straight from their original Hugging Face checkpoints: a Rust engine compiled to WebAssembly, in a worker, with an offline cache.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Maxime Golfier",
|
|
@@ -50,6 +50,7 @@
|
|
|
50
50
|
},
|
|
51
51
|
"files": [
|
|
52
52
|
"dist",
|
|
53
|
+
"native-manifest.json",
|
|
53
54
|
"wasm/simd/webslm_wasm.js",
|
|
54
55
|
"wasm/simd/webslm_wasm.d.ts",
|
|
55
56
|
"wasm/simd/webslm_wasm_bg.wasm",
|
|
@@ -17,6 +17,21 @@ export class JsSource {
|
|
|
17
17
|
constructor(read: Function, list?: Function | null);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* The converted-weight file of one model load (see the module docs).
|
|
22
|
+
*/
|
|
23
|
+
export class JsWeightCache {
|
|
24
|
+
free(): void;
|
|
25
|
+
[Symbol.dispose](): void;
|
|
26
|
+
/**
|
|
27
|
+
* `key` names the converted checkpoint (`"org/name@sha"`; the load
|
|
28
|
+
* format is appended). `read(start, end)` resolves to bytes
|
|
29
|
+
* `[start, end)` of the stored file of `size` bytes (`0`: none);
|
|
30
|
+
* `write(chunk)` appends to the new file and may return a promise.
|
|
31
|
+
*/
|
|
32
|
+
constructor(key: string, size: number, read: Function, write: Function);
|
|
33
|
+
}
|
|
34
|
+
|
|
20
35
|
/**
|
|
21
36
|
* Incremental SHA-256.
|
|
22
37
|
*/
|
|
@@ -92,8 +107,13 @@ export class WasmModel {
|
|
|
92
107
|
* (default 1), in `adapterMode` (default `"unmerged"`: its delta is
|
|
93
108
|
* added in f32 beside the weights; `"merged"` merges it into the
|
|
94
109
|
* weights as they load).
|
|
110
|
+
*
|
|
111
|
+
* `weightCache`, when given, keeps the converted (block-quantized)
|
|
112
|
+
* weights: a valid stored file serves them instead of converting the
|
|
113
|
+
* checkpoint, else they are written to it as they are converted;
|
|
114
|
+
* `info().weightCache` says which (see [`WeightCacheOutcome`]).
|
|
95
115
|
*/
|
|
96
|
-
static load(source: JsSource, options_json: string, lora?: JsSource | null): Promise<WasmModel>;
|
|
116
|
+
static load(source: JsSource, options_json: string, lora?: JsSource | null, weight_cache?: JsWeightCache | null): Promise<WasmModel>;
|
|
97
117
|
/**
|
|
98
118
|
* Removes the adapter, restoring the base weights exactly. Resolves to
|
|
99
119
|
* the description of the adapter removed, or `null` when there was
|
|
@@ -251,11 +271,13 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl
|
|
|
251
271
|
export interface InitOutput {
|
|
252
272
|
readonly memory: WebAssembly.Memory;
|
|
253
273
|
readonly __wbg_jssource_free: (a: number, b: number) => void;
|
|
274
|
+
readonly __wbg_jsweightcache_free: (a: number, b: number) => void;
|
|
254
275
|
readonly __wbg_sha256hasher_free: (a: number, b: number) => void;
|
|
255
276
|
readonly __wbg_wasmmodel_free: (a: number, b: number) => void;
|
|
256
277
|
readonly __wbg_wasmsession_free: (a: number, b: number) => void;
|
|
257
278
|
readonly buildInfo: () => [number, number, number];
|
|
258
279
|
readonly jssource_new: (a: any, b: number) => number;
|
|
280
|
+
readonly jsweightcache_new: (a: number, b: number, c: number, d: any, e: any) => number;
|
|
259
281
|
readonly parseSafetensorsHeader: (a: number, b: number) => [number, number, number];
|
|
260
282
|
readonly sha256hasher_finish: (a: number) => [number, number];
|
|
261
283
|
readonly sha256hasher_new: () => number;
|
|
@@ -268,7 +290,7 @@ export interface InitOutput {
|
|
|
268
290
|
readonly wasmmodel_detokenize: (a: number, b: number, c: number, d: number) => [number, number, number, number];
|
|
269
291
|
readonly wasmmodel_generate: (a: number, b: number, c: number, d: number, e: number, f: any, g: number) => any;
|
|
270
292
|
readonly wasmmodel_info: (a: number) => [number, number, number];
|
|
271
|
-
readonly wasmmodel_load: (a: number, b: number, c: number, d: number) => any;
|
|
293
|
+
readonly wasmmodel_load: (a: number, b: number, c: number, d: number, e: number) => any;
|
|
272
294
|
readonly wasmmodel_removeAdapter: (a: number) => any;
|
|
273
295
|
readonly wasmmodel_renderChat: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number];
|
|
274
296
|
readonly wasmmodel_reset: (a: number) => [number, number];
|
|
@@ -289,10 +311,10 @@ export interface InitOutput {
|
|
|
289
311
|
readonly wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___js_sys_56365fc49d784db6___Function_fn_wasm_bindgen_8cf186fa7724d16d___JsValue_____wasm_bindgen_8cf186fa7724d16d___sys__Undefined___js_sys_56365fc49d784db6___Function_fn_wasm_bindgen_8cf186fa7724d16d___JsValue_____wasm_bindgen_8cf186fa7724d16d___sys__Undefined_______true_: (a: number, b: number, c: any, d: any) => void;
|
|
290
312
|
readonly wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wasm_bindgen_8cf186fa7724d16d___JsValue__core_608f92abc48d28da___result__Result_____wasm_bindgen_8cf186fa7724d16d___JsError___true_: (a: number, b: number, c: any) => [number, number];
|
|
291
313
|
readonly wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wasm_bindgen_8cf186fa7724d16d___sys__JsNullable_wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuError__GpuError___core_608f92abc48d28da___result__Result_____wasm_bindgen_8cf186fa7724d16d___JsError___true_: (a: number, b: number, c: any) => [number, number];
|
|
292
|
-
readonly wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wasm_bindgen_8cf186fa7724d16d___sys__JsNullable_wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuError__GpuError___core_608f92abc48d28da___result__Result_____wasm_bindgen_8cf186fa7724d16d___JsError___true__37: (a: number, b: number, c: any) => [number, number];
|
|
293
314
|
readonly wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wasm_bindgen_8cf186fa7724d16d___sys__JsNullable_wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuError__GpuError___core_608f92abc48d28da___result__Result_____wasm_bindgen_8cf186fa7724d16d___JsError___true__38: (a: number, b: number, c: any) => [number, number];
|
|
315
|
+
readonly wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wasm_bindgen_8cf186fa7724d16d___sys__JsNullable_wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuError__GpuError___core_608f92abc48d28da___result__Result_____wasm_bindgen_8cf186fa7724d16d___JsError___true__39: (a: number, b: number, c: any) => [number, number];
|
|
294
316
|
readonly wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuDeviceLostInfo__GpuDeviceLostInfo______true_: (a: number, b: number, c: any) => void;
|
|
295
|
-
readonly
|
|
317
|
+
readonly wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuDeviceLostInfo__GpuDeviceLostInfo______true__37: (a: number, b: number, c: any) => void;
|
|
296
318
|
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
297
319
|
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
298
320
|
readonly __wbindgen_exn_store: (a: number) => void;
|