webslm 0.2.1 → 0.3.1

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.
@@ -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, FileData, LoadProgress, LoraInfo, ModelInfo } from "./types.js";
9
+ import type { Activations, AdapterMode, 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 = {
@@ -30,6 +30,7 @@ export interface WireLoadOptions {
30
30
  readonly contextLength?: number;
31
31
  /** Scale of the adapter of a `load` request. */
32
32
  readonly adapterScale?: number;
33
+ readonly adapterMode?: AdapterMode;
33
34
  readonly cacheDir?: string;
34
35
  readonly deterministic?: boolean;
35
36
  readonly activations?: Activations;
@@ -72,7 +73,7 @@ type WithId<T> = T & {
72
73
  export type Request = WithId<{
73
74
  readonly type: "load";
74
75
  readonly source: WireSource;
75
- /** A LoRA adapter merged at load. */
76
+ /** A LoRA adapter applied at load. */
76
77
  readonly adapter?: WireSource;
77
78
  readonly options: WireLoadOptions;
78
79
  } | {
package/dist/types.d.ts CHANGED
@@ -15,6 +15,19 @@ export type Quant = "q8" | "q4" | "f32";
15
15
  * dequantizes the weights instead. WebGPU always uses f32 activations.
16
16
  */
17
17
  export type Activations = "quantized" | "f32";
18
+ /**
19
+ * How LoRA adapters run on a model. `"unmerged"` (the default) keeps the
20
+ * adapter beside the weights and adds its delta in f32 (`y = W·x + s·B(A·x)`,
21
+ * PEFT's definition): the weights and the adapter each keep their precision,
22
+ * so `quant: "q8"` with `activations: "f32"` on Q8_0-exact weights gives the
23
+ * logits of `quant: "f32"` with the same adapter bit for bit on the CPU, and
24
+ * applying, swapping or removing an adapter reads no base weights. It costs
25
+ * two small matmuls per adapted layer (a few percent of decode speed on
26
+ * quantized weights). `"merged"` merges the adapter into the weights before
27
+ * they are quantized: no decode cost, but `W + s·B·A` is quantized again,
28
+ * which loses part of the adapter.
29
+ */
30
+ export type AdapterMode = "unmerged" | "merged";
18
31
  /** Checkpoint files held by the page: bytes from OPFS, IndexedDB, an upload, a training job... */
19
32
  export type FileData = Blob | ArrayBuffer | Uint8Array;
20
33
  /**
@@ -58,15 +71,17 @@ export interface ApplyAdapterOptions {
58
71
  readonly onProgress?: (progress: LoadProgress) => void;
59
72
  readonly signal?: AbortSignal;
60
73
  }
61
- /** A LoRA adapter merged into a model's weights. */
74
+ /** A LoRA adapter applied to a model. */
62
75
  export interface LoraInfo {
63
- /** Modules whose weights changed, e.g. `"model.layers.0.self_attn.q_proj"`. */
76
+ /** Adapted modules, e.g. `"model.layers.0.self_attn.q_proj"`. */
64
77
  readonly modules: readonly string[];
65
78
  readonly scale: number;
66
79
  /** Largest rank over the modules. */
67
80
  readonly rank: number;
68
81
  /** `base_model_name_or_path` from `adapter_config.json`. */
69
82
  readonly baseModel?: string;
83
+ /** How the adapter runs (see {@link AdapterMode}). */
84
+ readonly mode: AdapterMode;
70
85
  /** Hub repository id, when the adapter came from the hub. */
71
86
  readonly repo?: string;
72
87
  /** Commit sha of the hub revision, when the adapter came from the hub. */
@@ -101,9 +116,14 @@ export interface LoadModelOptions {
101
116
  readonly device?: Device;
102
117
  /**
103
118
  * Weight format. Defaults to `"q8"`; `"q4"` (4-bit, 16-value blocks) is
104
- * smaller and less accurate.
119
+ * smaller and less accurate. `"auto"` estimates the memory of the model
120
+ * before downloading it (config.json and the safetensors headers) and
121
+ * loads Q8_0 unless it does not fit this environment's memory
122
+ * (`navigator.deviceMemory`, the WebGPU adapter limits, the 4 GiB of
123
+ * WebAssembly), then Q4 ({@link ModelInfo.quantNote} says why; see
124
+ * `fitModel`).
105
125
  */
106
- readonly quant?: Quant;
126
+ readonly quant?: Quant | "auto";
107
127
  /**
108
128
  * Precision of the activations against quantized weights (default
109
129
  * `"quantized"`). With `"f32"`, `quant: "q8"` on a checkpoint whose
@@ -119,13 +139,18 @@ export interface LoadModelOptions {
119
139
  /** Largest number of positions a session holds. Defaults to min(4096, model maximum). */
120
140
  readonly contextLength?: number;
121
141
  /**
122
- * A PEFT LoRA adapter merged into the weights as they load (in f32,
123
- * before quantization). It can be swapped or removed later with
124
- * {@link Model.applyAdapter} and {@link Model.removeAdapter}.
142
+ * A PEFT LoRA adapter applied at load, in {@link LoadModelOptions.adapterMode}.
143
+ * It can be swapped or removed later with {@link Model.applyAdapter} and
144
+ * {@link Model.removeAdapter}.
125
145
  */
126
146
  readonly adapter?: AdapterSource;
127
147
  /** Scale of {@link LoadModelOptions.adapter} (default `1`). */
128
148
  readonly adapterScale?: number;
149
+ /**
150
+ * How LoRA adapters run on this model, the one given at load and those
151
+ * applied later (default `"unmerged"`; see {@link AdapterMode}).
152
+ */
153
+ readonly adapterMode?: AdapterMode;
129
154
  readonly onProgress?: (progress: LoadProgress) => void;
130
155
  readonly signal?: AbortSignal;
131
156
  /**
@@ -388,6 +413,8 @@ export interface ModelInfo {
388
413
  /** The LoRA adapter merged into the weights, if any. */
389
414
  readonly lora?: LoraInfo;
390
415
  readonly quant: string;
416
+ /** Why `quant: "auto"` did not load Q8_0, when it did not. */
417
+ readonly quantNote?: string;
391
418
  /** Precision of the activations against quantized weights (`"f32"` on WebGPU). */
392
419
  readonly activations: Activations;
393
420
  readonly hiddenSize: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webslm",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
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",
@@ -43,8 +43,8 @@ export class WasmModel {
43
43
  free(): void;
44
44
  [Symbol.dispose](): void;
45
45
  /**
46
- * Merges a PEFT LoRA adapter served by `lora` into the weights,
47
- * replacing the adapter applied before (if any). `options_json`:
46
+ * Applies a PEFT LoRA adapter served by `lora` (in the model's
47
+ * `adapterMode`), replacing the adapter applied before (if any). `options_json`:
48
48
  * `{ scale?: number }` (default 1). Resolves to the adapter's
49
49
  * description (see [`LoraInfo`]); rejects with `INVALID_ADAPTER`,
50
50
  * leaving the model unchanged, when the adapter does not fit the
@@ -77,7 +77,8 @@ export class WasmModel {
77
77
  * Loads a checkpoint from `source`. `options_json` is a JSON object:
78
78
  * `{ format?: "q8" | "q4" | "q4_0" | "f32" | "f16" | "bf16",
79
79
  * contextLength?: number, deterministic?: boolean,
80
- * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto" }`.
80
+ * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto",
81
+ * adapterScale?: number, adapterMode?: "unmerged" | "merged" }`.
81
82
  *
82
83
  * `device` defaults to `"cpu"`. `"auto"` picks WebGPU when an adapter
83
84
  * is available and the model has more than
@@ -87,8 +88,10 @@ export class WasmModel {
87
88
  * results depend on the adapter.
88
89
  *
89
90
  * `lora`, when given, serves a PEFT LoRA adapter (`adapter_config.json`
90
- * and `adapter_model.safetensors`) merged into the weights as they
91
- * load, at `adapterScale` (default 1).
91
+ * and `adapter_model.safetensors`) applied at load, at `adapterScale`
92
+ * (default 1), in `adapterMode` (default `"unmerged"`: its delta is
93
+ * added in f32 beside the weights; `"merged"` merges it into the
94
+ * weights as they load).
92
95
  */
93
96
  static load(source: JsSource, options_json: string, lora?: JsSource | null): Promise<WasmModel>;
94
97
  /**
@@ -104,8 +104,8 @@ export class WasmModel {
104
104
  wasm.__wbg_wasmmodel_free(ptr, 0);
105
105
  }
106
106
  /**
107
- * Merges a PEFT LoRA adapter served by `lora` into the weights,
108
- * replacing the adapter applied before (if any). `options_json`:
107
+ * Applies a PEFT LoRA adapter served by `lora` (in the model's
108
+ * `adapterMode`), replacing the adapter applied before (if any). `options_json`:
109
109
  * `{ scale?: number }` (default 1). Resolves to the adapter's
110
110
  * description (see [`LoraInfo`]); rejects with `INVALID_ADAPTER`,
111
111
  * leaving the model unchanged, when the adapter does not fit the
@@ -208,7 +208,8 @@ export class WasmModel {
208
208
  * Loads a checkpoint from `source`. `options_json` is a JSON object:
209
209
  * `{ format?: "q8" | "q4" | "q4_0" | "f32" | "f16" | "bf16",
210
210
  * contextLength?: number, deterministic?: boolean,
211
- * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto" }`.
211
+ * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto",
212
+ * adapterScale?: number, adapterMode?: "unmerged" | "merged" }`.
212
213
  *
213
214
  * `device` defaults to `"cpu"`. `"auto"` picks WebGPU when an adapter
214
215
  * is available and the model has more than
@@ -218,8 +219,10 @@ export class WasmModel {
218
219
  * results depend on the adapter.
219
220
  *
220
221
  * `lora`, when given, serves a PEFT LoRA adapter (`adapter_config.json`
221
- * and `adapter_model.safetensors`) merged into the weights as they
222
- * load, at `adapterScale` (default 1).
222
+ * and `adapter_model.safetensors`) applied at load, at `adapterScale`
223
+ * (default 1), in `adapterMode` (default `"unmerged"`: its delta is
224
+ * added in f32 beside the weights; `"merged"` merges it into the
225
+ * weights as they load).
223
226
  * @param {JsSource} source
224
227
  * @param {string} options_json
225
228
  * @param {JsSource | null} [lora]
Binary file
@@ -43,8 +43,8 @@ export class WasmModel {
43
43
  free(): void;
44
44
  [Symbol.dispose](): void;
45
45
  /**
46
- * Merges a PEFT LoRA adapter served by `lora` into the weights,
47
- * replacing the adapter applied before (if any). `options_json`:
46
+ * Applies a PEFT LoRA adapter served by `lora` (in the model's
47
+ * `adapterMode`), replacing the adapter applied before (if any). `options_json`:
48
48
  * `{ scale?: number }` (default 1). Resolves to the adapter's
49
49
  * description (see [`LoraInfo`]); rejects with `INVALID_ADAPTER`,
50
50
  * leaving the model unchanged, when the adapter does not fit the
@@ -77,7 +77,8 @@ export class WasmModel {
77
77
  * Loads a checkpoint from `source`. `options_json` is a JSON object:
78
78
  * `{ format?: "q8" | "q4" | "q4_0" | "f32" | "f16" | "bf16",
79
79
  * contextLength?: number, deterministic?: boolean,
80
- * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto" }`.
80
+ * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto",
81
+ * adapterScale?: number, adapterMode?: "unmerged" | "merged" }`.
81
82
  *
82
83
  * `device` defaults to `"cpu"`. `"auto"` picks WebGPU when an adapter
83
84
  * is available and the model has more than
@@ -87,8 +88,10 @@ export class WasmModel {
87
88
  * results depend on the adapter.
88
89
  *
89
90
  * `lora`, when given, serves a PEFT LoRA adapter (`adapter_config.json`
90
- * and `adapter_model.safetensors`) merged into the weights as they
91
- * load, at `adapterScale` (default 1).
91
+ * and `adapter_model.safetensors`) applied at load, at `adapterScale`
92
+ * (default 1), in `adapterMode` (default `"unmerged"`: its delta is
93
+ * added in f32 beside the weights; `"merged"` merges it into the
94
+ * weights as they load).
92
95
  */
93
96
  static load(source: JsSource, options_json: string, lora?: JsSource | null): Promise<WasmModel>;
94
97
  /**
@@ -104,8 +104,8 @@ export class WasmModel {
104
104
  wasm.__wbg_wasmmodel_free(ptr, 0);
105
105
  }
106
106
  /**
107
- * Merges a PEFT LoRA adapter served by `lora` into the weights,
108
- * replacing the adapter applied before (if any). `options_json`:
107
+ * Applies a PEFT LoRA adapter served by `lora` (in the model's
108
+ * `adapterMode`), replacing the adapter applied before (if any). `options_json`:
109
109
  * `{ scale?: number }` (default 1). Resolves to the adapter's
110
110
  * description (see [`LoraInfo`]); rejects with `INVALID_ADAPTER`,
111
111
  * leaving the model unchanged, when the adapter does not fit the
@@ -208,7 +208,8 @@ export class WasmModel {
208
208
  * Loads a checkpoint from `source`. `options_json` is a JSON object:
209
209
  * `{ format?: "q8" | "q4" | "q4_0" | "f32" | "f16" | "bf16",
210
210
  * contextLength?: number, deterministic?: boolean,
211
- * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto" }`.
211
+ * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto",
212
+ * adapterScale?: number, adapterMode?: "unmerged" | "merged" }`.
212
213
  *
213
214
  * `device` defaults to `"cpu"`. `"auto"` picks WebGPU when an adapter
214
215
  * is available and the model has more than
@@ -218,8 +219,10 @@ export class WasmModel {
218
219
  * results depend on the adapter.
219
220
  *
220
221
  * `lora`, when given, serves a PEFT LoRA adapter (`adapter_config.json`
221
- * and `adapter_model.safetensors`) merged into the weights as they
222
- * load, at `adapterScale` (default 1).
222
+ * and `adapter_model.safetensors`) applied at load, at `adapterScale`
223
+ * (default 1), in `adapterMode` (default `"unmerged"`: its delta is
224
+ * added in f32 beside the weights; `"merged"` merges it into the
225
+ * weights as they load).
223
226
  * @param {JsSource} source
224
227
  * @param {string} options_json
225
228
  * @param {JsSource | null} [lora]
Binary file
@@ -43,8 +43,8 @@ export class WasmModel {
43
43
  free(): void;
44
44
  [Symbol.dispose](): void;
45
45
  /**
46
- * Merges a PEFT LoRA adapter served by `lora` into the weights,
47
- * replacing the adapter applied before (if any). `options_json`:
46
+ * Applies a PEFT LoRA adapter served by `lora` (in the model's
47
+ * `adapterMode`), replacing the adapter applied before (if any). `options_json`:
48
48
  * `{ scale?: number }` (default 1). Resolves to the adapter's
49
49
  * description (see [`LoraInfo`]); rejects with `INVALID_ADAPTER`,
50
50
  * leaving the model unchanged, when the adapter does not fit the
@@ -77,7 +77,8 @@ export class WasmModel {
77
77
  * Loads a checkpoint from `source`. `options_json` is a JSON object:
78
78
  * `{ format?: "q8" | "q4" | "q4_0" | "f32" | "f16" | "bf16",
79
79
  * contextLength?: number, deterministic?: boolean,
80
- * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto" }`.
80
+ * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto",
81
+ * adapterScale?: number, adapterMode?: "unmerged" | "merged" }`.
81
82
  *
82
83
  * `device` defaults to `"cpu"`. `"auto"` picks WebGPU when an adapter
83
84
  * is available and the model has more than
@@ -87,8 +88,10 @@ export class WasmModel {
87
88
  * results depend on the adapter.
88
89
  *
89
90
  * `lora`, when given, serves a PEFT LoRA adapter (`adapter_config.json`
90
- * and `adapter_model.safetensors`) merged into the weights as they
91
- * load, at `adapterScale` (default 1).
91
+ * and `adapter_model.safetensors`) applied at load, at `adapterScale`
92
+ * (default 1), in `adapterMode` (default `"unmerged"`: its delta is
93
+ * added in f32 beside the weights; `"merged"` merges it into the
94
+ * weights as they load).
92
95
  */
93
96
  static load(source: JsSource, options_json: string, lora?: JsSource | null): Promise<WasmModel>;
94
97
  /**
@@ -106,8 +106,8 @@ export class WasmModel {
106
106
  wasm.__wbg_wasmmodel_free(ptr, 0);
107
107
  }
108
108
  /**
109
- * Merges a PEFT LoRA adapter served by `lora` into the weights,
110
- * replacing the adapter applied before (if any). `options_json`:
109
+ * Applies a PEFT LoRA adapter served by `lora` (in the model's
110
+ * `adapterMode`), replacing the adapter applied before (if any). `options_json`:
111
111
  * `{ scale?: number }` (default 1). Resolves to the adapter's
112
112
  * description (see [`LoraInfo`]); rejects with `INVALID_ADAPTER`,
113
113
  * leaving the model unchanged, when the adapter does not fit the
@@ -210,7 +210,8 @@ export class WasmModel {
210
210
  * Loads a checkpoint from `source`. `options_json` is a JSON object:
211
211
  * `{ format?: "q8" | "q4" | "q4_0" | "f32" | "f16" | "bf16",
212
212
  * contextLength?: number, deterministic?: boolean,
213
- * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto" }`.
213
+ * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto",
214
+ * adapterScale?: number, adapterMode?: "unmerged" | "merged" }`.
214
215
  *
215
216
  * `device` defaults to `"cpu"`. `"auto"` picks WebGPU when an adapter
216
217
  * is available and the model has more than
@@ -220,8 +221,10 @@ export class WasmModel {
220
221
  * results depend on the adapter.
221
222
  *
222
223
  * `lora`, when given, serves a PEFT LoRA adapter (`adapter_config.json`
223
- * and `adapter_model.safetensors`) merged into the weights as they
224
- * load, at `adapterScale` (default 1).
224
+ * and `adapter_model.safetensors`) applied at load, at `adapterScale`
225
+ * (default 1), in `adapterMode` (default `"unmerged"`: its delta is
226
+ * added in f32 beside the weights; `"merged"` merges it into the
227
+ * weights as they load).
225
228
  * @param {JsSource} source
226
229
  * @param {string} options_json
227
230
  * @param {JsSource | null} [lora]
Binary file