webslm 0.6.0 → 0.7.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 CHANGED
@@ -99,7 +99,7 @@ chat.reset();
99
99
  await llm.unload();
100
100
  ```
101
101
 
102
- `load(model?, options?)` takes what `loadModel` takes: a repository id (default `DEFAULT_MODEL`) or a [source](#sources), and options that are all optional: `device` (`"auto"` (default), `"webgpu"` or `"cpu"`; `"webgpu"` falls back to the CPU when no GPU device can be created, and `"auto"` also when the model does not fit in GPU memory or the device fails while loading; `ModelInfo.deviceNote` says why), `quant` (default `"q8"`; `"q4"`, 4-bit blocks of 16 values, is smaller and less accurate, `"f32"` unquantized, `"auto"` picks `"q8"` unless the model does not fit this environment's memory, then `"q4"`, see [`fitModel`](#hub-helpers-and-cache); `ModelInfo.quantNote` says why), `revision`, `token` (gated or private repositories), `contextLength`, `onProgress`, `signal`, `worker` (default `true`), `cacheDir` (Node), `weightCache` (default `true`, see [the converted-weight cache](#converted-weight-cache)), `deterministic` (CPU only), and the advanced `wasmFlavour` and `threads` (see [CPU builds](#cpu-builds-and-deterministic-mode)).
102
+ `load(model?, options?)` takes what `loadModel` takes: a repository id (default `DEFAULT_MODEL`) or a [source](#sources), and options that are all optional: `device` (`"auto"` (default), `"webgpu"` or `"cpu"`; `"webgpu"` falls back to the CPU when no GPU device can be created, and `"auto"` also when the model does not fit in GPU memory or the device fails while loading; `ModelInfo.deviceNote` says why), `quant` (default `"q8"`; `"q4"`, 4-bit blocks of 16 values, is smaller and less accurate, `"f32"` unquantized, `"auto"` picks `"q8"` unless the model does not fit this environment's memory, then `"q4"`, see [`fitModel`](#hub-helpers-and-cache); `ModelInfo.quantNote` says why), `revision`, `token` (gated or private repositories), `contextLength`, `kvCache` (`"f32"` (default) or `"int8"`: keys and values in 8 bits with the most recent `kvResidual` positions (default 128) in f32, about 3 times less KV cache memory at 4096 positions), `kvBudget` (opt-in KV eviction: at most this many positions per full-attention layer, keeping attention sinks, the recent quarter and the most distinct keys; see [KV cache memory](https://github.com/maxgfr/webslm#kv-cache-memory)), `onProgress`, `signal`, `worker` (default `true`), `cacheDir` (Node), `weightCache` (default `true`, see [the converted-weight cache](#converted-weight-cache)), `deterministic` (CPU only), and the advanced `wasmFlavour` and `threads` (see [CPU builds](#cpu-builds-and-deterministic-mode)).
103
103
 
104
104
  Each chat keeps its own cache in the engine, so a turn only processes the new message; `chat.dispose()` frees it. Calls on one model, and turns of one chat, run one after the other. `llm.model` is the underlying `Model`, for the advanced API.
105
105
 
@@ -458,7 +458,7 @@ export default defineConfig({
458
458
  });
459
459
  ```
460
460
 
461
- Server code that imports webslm must not be bundled: webslm runs from its own files in `node_modules` (Next.js: `serverExternalPackages: ["webslm"]`). Without a bundler, a page can import it from jsDelivr (`https://cdn.jsdelivr.net/npm/webslm@0.6.0/dist/index.js`); webslm starts its workers through same-origin `blob:` workers when its files come from another origin. Guides for Next.js, Vite, webpack, Node servers, Bun, Deno and CDNs, each with a tested example: <https://github.com/maxgfr/webslm#frameworks-bundlers-and-runtimes>.
461
+ Server code that imports webslm must not be bundled: webslm runs from its own files in `node_modules` (Next.js: `serverExternalPackages: ["webslm"]`). Without a bundler, a page can import it from jsDelivr (`https://cdn.jsdelivr.net/npm/webslm@0.7.0/dist/index.js`); webslm starts its workers through same-origin `blob:` workers when its files come from another origin. Guides for Next.js, Vite, webpack, Node servers, Bun, Deno and CDNs, each with a tested example: <https://github.com/maxgfr/webslm#frameworks-bundlers-and-runtimes>.
462
462
 
463
463
  ## Errors
464
464
 
package/dist/engine.js CHANGED
@@ -550,6 +550,9 @@ export class Engine {
550
550
  ...(gguf === undefined ? {} : { ggufFile: gguf }),
551
551
  device: options.device ?? "auto",
552
552
  ...(options.contextLength === undefined ? {} : { contextLength: options.contextLength }),
553
+ ...(options.kvCache === undefined ? {} : { kvCache: options.kvCache }),
554
+ ...(options.kvResidual === undefined ? {} : { kvResidual: options.kvResidual }),
555
+ ...(options.kvBudget === undefined ? {} : { kvBudget: options.kvBudget }),
553
556
  ...(options.deterministic === true ? { deterministic: true } : {}),
554
557
  ...(options.vision === false ? { vision: false } : {}),
555
558
  ...(options.activations === undefined ? {} : { activations: options.activations }),
@@ -663,6 +666,9 @@ export class Engine {
663
666
  format: options.autoQuant === true ? "auto" : options.quant,
664
667
  device: options.device ?? "auto",
665
668
  ...(options.contextLength === undefined ? {} : { contextLength: options.contextLength }),
669
+ ...(options.kvCache === undefined ? {} : { kvCache: options.kvCache }),
670
+ ...(options.kvResidual === undefined ? {} : { kvResidual: options.kvResidual }),
671
+ ...(options.kvBudget === undefined ? {} : { kvBudget: options.kvBudget }),
666
672
  ...(options.deterministic === true ? { deterministic: true } : {}),
667
673
  ...(options.vision === false ? { vision: false } : {}),
668
674
  ...(options.activations === undefined ? {} : { activations: options.activations }),
package/dist/fit.d.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  * tries Q8_0 then the 4-bit format, each on every device before the next.
7
7
  */
8
8
  import { type CheckModelOptions, type ModelCheck } from "./hub.js";
9
- import { type MemoryEstimate } from "./memory.js";
9
+ import { type EstimateOptions, type MemoryEstimate } from "./memory.js";
10
10
  import type { Quant } from "./types.js";
11
11
  /**
12
12
  * How a model's memory compares to what a device can hold: under 80% of
@@ -166,7 +166,7 @@ export interface CheckpointShapes {
166
166
  /** Reads `config.json` and the safetensors headers (no weights) through `read`. */
167
167
  export declare function readCheckpointShapes(read: CheckpointReader): Promise<CheckpointShapes>;
168
168
  /** What `quant: "auto"` picks for a checkpoint, and why when it is not Q8_0. */
169
- export declare function autoQuant(shapes: CheckpointShapes, resources: BrowserResources, contextLength?: number, vision?: boolean): {
169
+ export declare function autoQuant(shapes: CheckpointShapes, resources: BrowserResources, contextLength?: number, vision?: boolean, kv?: Pick<EstimateOptions, "kvCache" | "kvResidual" | "kvBudget">): {
170
170
  readonly quant: Quant;
171
171
  readonly note?: string;
172
172
  };
package/dist/fit.js CHANGED
@@ -93,7 +93,7 @@ export function fitLevel(estimate, device) {
93
93
  * 80% of `usable`, or undefined when even a short one does not fit.
94
94
  */
95
95
  export function maxContextLength(estimate, usable, limit) {
96
- const fixed = estimate.total - estimate.kvPerToken * estimate.contextLength;
96
+ const fixed = Math.max(estimate.total - estimate.kvPerToken * estimate.contextLength, 0);
97
97
  const free = Math.floor(usable * 0.8) - fixed;
98
98
  if (free < 0)
99
99
  return undefined;
@@ -271,11 +271,14 @@ export async function readCheckpointShapes(read) {
271
271
  return { config, tensors, params };
272
272
  }
273
273
  /** What `quant: "auto"` picks for a checkpoint, and why when it is not Q8_0. */
274
- export function autoQuant(shapes, resources, contextLength, vision) {
274
+ export function autoQuant(shapes, resources, contextLength, vision, kv = {}) {
275
275
  const estimates = AUTO_QUANTS.map((quant) => estimateModelMemory(shapes.config, shapes.tensors, {
276
276
  quant,
277
277
  ...(contextLength === undefined ? {} : { contextLength }),
278
278
  ...(vision === undefined ? {} : { vision }),
279
+ ...(kv.kvCache === undefined ? {} : { kvCache: kv.kvCache }),
280
+ ...(kv.kvResidual === undefined ? {} : { kvResidual: kv.kvResidual }),
281
+ ...(kv.kvBudget === undefined ? {} : { kvBudget: kv.kvBudget }),
279
282
  }));
280
283
  const all = deviceLimits(resources);
281
284
  const devices = shapes.params > AUTO_GPU_MIN_PARAMS ? all : all.filter((d) => d.device === "cpu");
package/dist/index.d.ts CHANGED
@@ -18,7 +18,7 @@ import type { SafetensorsHeader } from "./safetensors.js";
18
18
  export { ask, DEFAULT_MAX_TOKENS, DEFAULT_MODEL, DEFAULT_TEMPERATURE, load, splitReasoning, stream, unloadAll, type Answer, type AskOptions, type Chat, type ChatOptions, type ChatTurnOptions, type LLM, type PromptOptions, type SharedAskOptions, type SharedStreamOptions, type TextStream, } from "./simple.js";
19
19
  export { detectCapabilities, getWebGpuAdapterSummary, type Capabilities, type CapabilityEnvironment, type GpuAdapterLike, type GpuLike, type WebGpuAdapterSummary, } from "./capabilities.js";
20
20
  export { createEngine, EngineHandle, loadModel, Model, MODEL_FAMILIES, Session, type ModelFamily, } from "./models.js";
21
- export type { Activations, AdapterMode, AdapterSource, ApplyAdapterOptions, Backend, ChatInputMessage, ChatMessage, ChatStream, ContentPart, GeneratedToken, CpuInfo, CompletionOptions, Device, FetchFile, FileData, FinishReason, GenerateOptions, GenerationConfig, GenerationResult, ImageInput, MediaOptions, MediaPart, MultimodalMessage, RawImage, VideoFrames, VideoInput, WeightCacheInfo, LoadedFile, LoadModelOptions, LoadProgress, LoraInfo, ModelInfo, ModelSource, Quant, ScoreOptions, ScoreOutput, StepOptions, } from "./types.js";
21
+ export type { Activations, AdapterMode, AdapterSource, ApplyAdapterOptions, Backend, ChatInputMessage, ChatMessage, ChatStream, ContentPart, GeneratedToken, CpuInfo, CompletionOptions, Device, FetchFile, FileData, FinishReason, GenerateOptions, GenerationConfig, GenerationResult, ImageInput, MediaOptions, MediaPart, MultimodalMessage, RawImage, VideoFrames, VideoInput, WeightCacheInfo, LoadedFile, LoadModelOptions, LoadProgress, LoraInfo, ModelInfo, ModelMemory, ModelSource, Quant, ScoreOptions, ScoreOutput, StepOptions, } from "./types.js";
22
22
  export { WebslmError, type WebslmErrorCode } from "./errors.js";
23
23
  export { CURATED_MODELS, curatedModel, RECOMMENDED_SAMPLING, recommendedSampling, type CheckpointTraits, type CuratedModel, type SamplingDefaults, type SamplingSet, } from "./curated.js";
24
24
  export { checkModel, DEFAULT_HUB_URL, EMBEDDING_REASON, isEmbeddingModel, formatParams, searchModels, SUPPORTED_MODEL_TYPES, type CheckModelOptions, type HubModel, type HubOptions, type ModelCheck, type SearchModelsOptions, } from "./hub.js";
@@ -27,7 +27,7 @@ export type { Dtype, SafetensorsHeader, TensorInfo } from "./safetensors.js";
27
27
  export { selectWasmFlavour, supportsRelaxedSimd, supportsWasmThreads, type FlavourEnvironment, type WasmFlavour, } from "./wasm-flavour.js";
28
28
  export { loadWasm, type WasmExports, type WasmSource } from "./wasm.js";
29
29
  export { classifyFit, fitModel, fitParams, getBrowserResources, AUTO_QUANTS, WASM_MEMORY_LIMIT, type BrowserResources, type DeviceFit, type DeviceLimits, type FitChoice, type FitLevel, type FitModelOptions, type ModelFit, } from "./fit.js";
30
- export { estimateModelMemory, type EstimateFormat, type EstimateOptions, type KvLayers, type MemoryEstimate, } from "./memory.js";
30
+ export { estimateModelMemory, kvCacheBytes, type EstimateFormat, type EstimateOptions, type KvCacheType, type KvLayers, type MemoryEstimate, } from "./memory.js";
31
31
  /** Version of the Rust engine, read from the WebAssembly module. */
32
32
  export declare function version(): Promise<string>;
33
33
  /**
package/dist/index.js CHANGED
@@ -25,7 +25,7 @@ export { cacheUsage, deleteCachedModel, listCachedModels, } from "./cache-manage
25
25
  export { selectWasmFlavour, supportsRelaxedSimd, supportsWasmThreads, } from "./wasm-flavour.js";
26
26
  export { loadWasm } from "./wasm.js";
27
27
  export { classifyFit, fitModel, fitParams, getBrowserResources, AUTO_QUANTS, WASM_MEMORY_LIMIT, } from "./fit.js";
28
- export { estimateModelMemory, } from "./memory.js";
28
+ export { estimateModelMemory, kvCacheBytes, } from "./memory.js";
29
29
  /** Version of the Rust engine, read from the WebAssembly module. */
30
30
  export async function version() {
31
31
  const wasm = await loadWasm();
package/dist/memory.d.ts CHANGED
@@ -2,11 +2,12 @@
2
2
  * Memory a model needs, estimated from its `config.json` and safetensors
3
3
  * headers before downloading it: a port of the Rust estimate
4
4
  * (`crates/webslm-models/src/memory.rs`). Weights in the load format, the
5
- * KV cache layer by layer (full attention grows with the context,
6
- * sliding-window layers keep a ring of `2 × window - 1` positions,
7
- * convolution and linear-attention layers a fixed state) and the
8
- * activations of a 512-token prefill. Both implementations are checked
9
- * against the same cases (`fixtures/memory/estimates.json`).
5
+ * KV cache layer by layer (full attention grows with the context, in f32
6
+ * or int8, up to the KV eviction budget; sliding-window layers keep a ring
7
+ * of `2 × window - 1` positions, convolution and linear-attention layers a
8
+ * fixed state) and the activations of a 512-token prefill. Both
9
+ * implementations are checked against the same cases
10
+ * (`fixtures/memory/estimates.json`).
10
11
  */
11
12
  import type { HeaderTensors } from "./hub.js";
12
13
  import type { Quant } from "./types.js";
@@ -24,11 +25,11 @@ export interface MemoryEstimate {
24
25
  readonly weights: number;
25
26
  /** KV caches and recurrent states of one sequence at `contextLength`. */
26
27
  readonly kvCache: number;
27
- /** What one more position adds to the KV cache. */
28
+ /** What one more position adds to the KV cache (0 once KV eviction caps it below the context). */
28
29
  readonly kvPerToken: number;
29
30
  /** Activations of a prefill batch. */
30
31
  readonly activations: number;
31
- /** The largest single buffer (WebGPU binds each buffer whole). */
32
+ /** The largest single buffer: the widest activation (KV caches are paged below the device's limit). */
32
33
  readonly largestBuffer: number;
33
34
  /** Positions the estimate assumes. */
34
35
  readonly contextLength: number;
@@ -47,6 +48,8 @@ export interface MemoryEstimate {
47
48
  readonly linearAttention: KvLayers;
48
49
  };
49
50
  }
51
+ /** How full-attention layers store keys and values (`kvCache` load option). */
52
+ export type KvCacheType = "f32" | "int8";
50
53
  export interface EstimateOptions {
51
54
  /** Weight format (default `"q8"`). */
52
55
  readonly quant?: EstimateFormat;
@@ -57,7 +60,21 @@ export interface EstimateOptions {
57
60
  * (default `true`, as the load option `vision`).
58
61
  */
59
62
  readonly vision?: boolean;
63
+ /** KV cache storage of full-attention layers (default `"f32"`). */
64
+ readonly kvCache?: KvCacheType;
65
+ /** Int8 KV cache: the most recent positions kept in f32 (default 128). */
66
+ readonly kvResidual?: number;
67
+ /** KV eviction: most positions a full-attention layer holds. */
68
+ readonly kvBudget?: number;
60
69
  }
70
+ /**
71
+ * Bytes of one full-attention cache holding `positions` positions: f32, or
72
+ * int8 in the KIVI layout (8-bit codes, a minimum and scale per channel
73
+ * and group of 32 positions for keys, per position and head for values,
74
+ * and an f32 ring of `residual + 32` positions). `KvCacheOptions::bytes`
75
+ * in Rust.
76
+ */
77
+ export declare function kvCacheBytes(kvHeads: number, headDim: number, positions: number, type?: KvCacheType, residual?: number): number;
61
78
  export type LayerType = "full" | "sliding" | "conv" | "linear";
62
79
  /** The text decoder hyperparameters the estimate reads (see `TextConfig` in Rust). */
63
80
  export interface TextConfig {
package/dist/memory.js CHANGED
@@ -2,13 +2,39 @@
2
2
  * Memory a model needs, estimated from its `config.json` and safetensors
3
3
  * headers before downloading it: a port of the Rust estimate
4
4
  * (`crates/webslm-models/src/memory.rs`). Weights in the load format, the
5
- * KV cache layer by layer (full attention grows with the context,
6
- * sliding-window layers keep a ring of `2 × window - 1` positions,
7
- * convolution and linear-attention layers a fixed state) and the
8
- * activations of a 512-token prefill. Both implementations are checked
9
- * against the same cases (`fixtures/memory/estimates.json`).
5
+ * KV cache layer by layer (full attention grows with the context, in f32
6
+ * or int8, up to the KV eviction budget; sliding-window layers keep a ring
7
+ * of `2 × window - 1` positions, convolution and linear-attention layers a
8
+ * fixed state) and the activations of a 512-token prefill. Both
9
+ * implementations are checked against the same cases
10
+ * (`fixtures/memory/estimates.json`).
10
11
  */
11
12
  import { WebslmError } from "./errors.js";
13
+ /** Positions an int8 KV cache quantizes together. */
14
+ const KV_GROUP = 32;
15
+ /** Most recent positions an int8 KV cache keeps in f32 by default. */
16
+ const DEFAULT_KV_RESIDUAL = 128;
17
+ /**
18
+ * Bytes of one full-attention cache holding `positions` positions: f32, or
19
+ * int8 in the KIVI layout (8-bit codes, a minimum and scale per channel
20
+ * and group of 32 positions for keys, per position and head for values,
21
+ * and an f32 ring of `residual + 32` positions). `KvCacheOptions::bytes`
22
+ * in Rust.
23
+ */
24
+ export function kvCacheBytes(kvHeads, headDim, positions, type = "f32", residual = DEFAULT_KV_RESIDUAL) {
25
+ const f32Position = 2 * kvHeads * headDim * 4;
26
+ if (type === "f32")
27
+ return positions * f32Position;
28
+ const residualPositions = Math.ceil(residual / KV_GROUP) * KV_GROUP;
29
+ const quantized = Math.floor(Math.max(positions - residualPositions, 0) / KV_GROUP) * KV_GROUP;
30
+ const ring = Math.min(residualPositions + KV_GROUP, positions);
31
+ return Math.ceil(int8BytesPerPosition(kvHeads, headDim) * quantized) + ring * f32Position;
32
+ }
33
+ /** Bytes of one quantized position of an int8 KV cache. */
34
+ function int8BytesPerPosition(kvHeads, headDim) {
35
+ const width = kvHeads * headDim;
36
+ return 2 * width + (width * 8) / KV_GROUP + kvHeads * 8;
37
+ }
12
38
  /** Rows of the prefill batch the activation estimate assumes. */
13
39
  const PREFILL_ROWS = 512;
14
40
  /** Context length when none is given, capped by the model. */
@@ -229,7 +255,18 @@ export function estimateModelMemory(config, tensors, options = {}) {
229
255
  const context = options.contextLength ?? Math.min(text.maxPositionEmbeddings ?? DEFAULT_CONTEXT, DEFAULT_CONTEXT);
230
256
  const kvWidth = text.numKeyValueHeads * text.headDim;
231
257
  const hidden = text.hiddenSize;
232
- const fullPerToken = 2 * kvWidth * 4;
258
+ const f32PerToken = 2 * kvWidth * 4;
259
+ const kvType = options.kvCache ?? "f32";
260
+ if (kvType !== "f32" && kvType !== "int8") {
261
+ throw new WebslmError("INVALID_ARGUMENT", "kvCache must be f32 or int8");
262
+ }
263
+ const held = options.kvBudget === undefined ? context : Math.min(context, options.kvBudget);
264
+ const fullBytes = kvCacheBytes(text.numKeyValueHeads, text.headDim, held, kvType, options.kvResidual ?? DEFAULT_KV_RESIDUAL);
265
+ const fullPerToken = held < context
266
+ ? 0
267
+ : kvType === "f32"
268
+ ? f32PerToken
269
+ : Math.ceil(int8BytesPerPosition(text.numKeyValueHeads, text.headDim));
233
270
  const kv = {
234
271
  fullAttention: { layers: 0, bytes: 0 },
235
272
  slidingAttention: { layers: 0, bytes: 0 },
@@ -242,7 +279,7 @@ export function estimateModelMemory(config, tensors, options = {}) {
242
279
  case "sliding": {
243
280
  const window = text.slidingWindow ?? context;
244
281
  kv.slidingAttention.layers += 1;
245
- kv.slidingAttention.bytes += fullPerToken * Math.min(Math.max(2 * window - 1, 0), context);
282
+ kv.slidingAttention.bytes += f32PerToken * Math.min(Math.max(2 * window - 1, 0), context);
246
283
  break;
247
284
  }
248
285
  case "conv":
@@ -259,7 +296,7 @@ export function estimateModelMemory(config, tensors, options = {}) {
259
296
  case "full":
260
297
  kvPerToken += fullPerToken;
261
298
  kv.fullAttention.layers += 1;
262
- kv.fullAttention.bytes += fullPerToken * context;
299
+ kv.fullAttention.bytes += fullBytes;
263
300
  break;
264
301
  }
265
302
  }
@@ -268,9 +305,10 @@ export function estimateModelMemory(config, tensors, options = {}) {
268
305
  const vocab = text.vocabSize ?? 0;
269
306
  const rows = Math.min(PREFILL_ROWS, Math.max(context, 1));
270
307
  const activations = rows * (6 * hidden + 3 * intermediate) * 4 + vocab * 4;
271
- const largestBuffer = Math.max(context * kvWidth * 4, rows * 2 * intermediate * 4, rows * Math.max(widestMatrix, hidden) * 4);
272
- // The vision encoder is not read to decode a token.
273
- const decodeBytes = weights - inputEmbedding + kvCache - Math.floor((kvPerToken * context) / 2);
308
+ const largestBuffer = Math.max(rows * 2 * intermediate * 4, rows * Math.max(widestMatrix, hidden) * 4);
309
+ // The full-attention caches at half the context, on average; the vision
310
+ // encoder is not read to decode a token.
311
+ const decodeBytes = weights - inputEmbedding + kvCache - Math.floor(kv.fullAttention.bytes / 2);
274
312
  const allWeights = weights + visionWeights;
275
313
  return {
276
314
  quant,
package/dist/models.js CHANGED
@@ -193,6 +193,20 @@ function checkScale(scale) {
193
193
  throw new WebslmError("INVALID_ARGUMENT", "the adapter scale must be a finite number");
194
194
  }
195
195
  }
196
+ const KV_CACHE_TYPES = ["f32", "int8"];
197
+ /** Smallest `kvBudget` (`MIN_KV_BUDGET` in Rust). */
198
+ const MIN_KV_BUDGET = 16;
199
+ function checkKvOptions(options) {
200
+ if (options.kvCache !== undefined && !KV_CACHE_TYPES.includes(options.kvCache)) {
201
+ throw new WebslmError("INVALID_ARGUMENT", `kvCache must be one of ${KV_CACHE_TYPES.join(", ")}`);
202
+ }
203
+ if (options.kvResidual !== undefined && !(Number.isInteger(options.kvResidual) && options.kvResidual >= 0)) {
204
+ throw new WebslmError("INVALID_ARGUMENT", "kvResidual must be a non-negative integer");
205
+ }
206
+ if (options.kvBudget !== undefined && !(Number.isInteger(options.kvBudget) && options.kvBudget >= MIN_KV_BUDGET)) {
207
+ throw new WebslmError("INVALID_ARGUMENT", `kvBudget must be an integer of at least ${MIN_KV_BUDGET}`);
208
+ }
209
+ }
196
210
  let persistRequested = false;
197
211
  /** Asks the browser to keep the cache under storage pressure (once per page). */
198
212
  function requestPersistentStorage() {
@@ -232,6 +246,7 @@ export async function loadModel(source, options = {}) {
232
246
  if (options.adapterMode !== undefined && !ADAPTER_MODES.includes(options.adapterMode)) {
233
247
  throw new WebslmError("INVALID_ARGUMENT", `adapterMode must be one of ${ADAPTER_MODES.join(", ")}`);
234
248
  }
249
+ checkKvOptions(options);
235
250
  if (isAborted(options.signal))
236
251
  throw abortError(options.signal);
237
252
  if (normalized.kind === "hub" && !isNode())
@@ -261,6 +276,9 @@ export async function loadModel(source, options = {}) {
261
276
  ...(options.adapterScale === undefined ? {} : { adapterScale: options.adapterScale }),
262
277
  ...(options.adapterMode === undefined ? {} : { adapterMode: options.adapterMode }),
263
278
  ...(options.contextLength === undefined ? {} : { contextLength: options.contextLength }),
279
+ ...(options.kvCache === undefined ? {} : { kvCache: options.kvCache }),
280
+ ...(options.kvResidual === undefined ? {} : { kvResidual: options.kvResidual }),
281
+ ...(options.kvBudget === undefined ? {} : { kvBudget: options.kvBudget }),
264
282
  ...(options.cacheDir === undefined ? {} : { cacheDir: options.cacheDir }),
265
283
  ...(options.weightCache === false ? { weightCache: false } : {}),
266
284
  ...(options.deterministic === true ? { deterministic: true } : {}),
@@ -316,7 +334,7 @@ export async function resolveAutoQuant(source, options) {
316
334
  else {
317
335
  shapes = await readCheckpointShapes(checkpointReader(source));
318
336
  }
319
- return autoQuant(shapes, resources, options.contextLength, options.vision);
337
+ return autoQuant(shapes, resources, options.contextLength, options.vision, options);
320
338
  }
321
339
  catch (error) {
322
340
  if (isAborted(options.signal))
@@ -7,6 +7,7 @@
7
7
  */
8
8
  import type { SerializedError } from "./errors.js";
9
9
  import type { Activations, AdapterMode, Backend, FileData, LoadProgress, LoraInfo, ModelInfo } from "./types.js";
10
+ import type { KvCacheType } from "./memory.js";
10
11
  import type { WasmFlavour } from "./wasm-flavour.js";
11
12
  /** A model source in a form that can be posted to a worker. */
12
13
  export type WireSource = {
@@ -40,6 +41,9 @@ export interface WireLoadOptions {
40
41
  /** `"auto"` (default), `"native"` or `"wasm"`. */
41
42
  readonly backend?: Backend;
42
43
  readonly contextLength?: number;
44
+ readonly kvCache?: KvCacheType;
45
+ readonly kvResidual?: number;
46
+ readonly kvBudget?: number;
43
47
  /** Scale of the adapter of a `load` request. */
44
48
  readonly adapterScale?: number;
45
49
  readonly adapterMode?: AdapterMode;
package/dist/types.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  /** Public types of the webslm API. */
2
+ import type { KvCacheType } from "./memory.js";
2
3
  import type { EngineHandle } from "./models.js";
3
4
  import type { WasmFlavour } from "./wasm-flavour.js";
4
5
  /**
@@ -177,6 +178,27 @@ export interface LoadModelOptions {
177
178
  readonly token?: string;
178
179
  /** Largest number of positions a session holds. Defaults to min(4096, model maximum). */
179
180
  readonly contextLength?: number;
181
+ /**
182
+ * How full-attention layers store keys and values (default `"f32"`).
183
+ * `"int8"` quantizes them in the KIVI layout (keys per channel, values
184
+ * per token, the most recent {@link LoadModelOptions.kvResidual} positions
185
+ * kept in f32): about 3 times less memory at 4096 positions, on the CPU
186
+ * and WebGPU, at a small cost in accuracy. Sliding-window and recurrent
187
+ * layers keep their own small states.
188
+ */
189
+ readonly kvCache?: KvCacheType;
190
+ /** Int8 KV cache: the most recent positions kept in f32 (default 128, rounded up to a multiple of 32). */
191
+ readonly kvResidual?: number;
192
+ /**
193
+ * Opt-in KV eviction: most positions a full-attention layer keeps (at
194
+ * least 16). Past it, the cache drops positions instead of growing: it
195
+ * keeps the first 4 (attention sinks), the most recent quarter of the
196
+ * budget, and the keys least similar to their head's mean key (KeyDiff),
197
+ * so generation goes on past the budget in bounded memory. Off by
198
+ * default; deterministic. Answers that need a detail from the middle of a
199
+ * long prompt may lose it.
200
+ */
201
+ readonly kvBudget?: number;
180
202
  /**
181
203
  * A PEFT LoRA adapter applied at load, in {@link LoadModelOptions.adapterMode}.
182
204
  * It can be swapped or removed later with {@link Model.applyAdapter} and
@@ -548,6 +570,31 @@ export interface WeightCacheInfo {
548
570
  /** Bytes read from the cache, or stored in it. */
549
571
  readonly bytes: number;
550
572
  }
573
+ /** Memory of a loaded model ({@link ModelInfo.memory}), in bytes. */
574
+ export interface ModelMemory {
575
+ /** Weights in their stored formats. */
576
+ readonly weights: number;
577
+ /** KV caches and recurrent states of one session at {@link ModelMemory.contextLength} positions. */
578
+ readonly kvCache: number;
579
+ /** What one more position adds to the KV cache (0 once {@link LoadModelOptions.kvBudget} caps it). */
580
+ readonly kvPerToken: number;
581
+ /** How full-attention layers store keys and values ({@link LoadModelOptions.kvCache}). */
582
+ readonly kvCacheType: KvCacheType;
583
+ /** Positions an int8 KV cache keeps in f32. */
584
+ readonly kvResidual?: number;
585
+ /** The KV eviction budget ({@link LoadModelOptions.kvBudget}), when on. */
586
+ readonly kvBudget?: number;
587
+ /** Activations of a prefill batch. */
588
+ readonly scratch: number;
589
+ /** Weights, KV cache and scratch together. */
590
+ readonly total: number;
591
+ /** Positions {@link ModelMemory.kvCache} is sized for. */
592
+ readonly contextLength: number;
593
+ /** Bytes the device holds with good performance, when known. */
594
+ readonly deviceBudget?: number;
595
+ /** Largest single buffer the device allocates, when limited (KV caches are paged below it). */
596
+ readonly maxBuffer?: number;
597
+ }
551
598
  /** Description of a loaded model. */
552
599
  export interface ModelInfo {
553
600
  /** Hub repository id, when loaded from the hub. */
@@ -609,6 +656,8 @@ export interface ModelInfo {
609
656
  readonly fromCache: boolean;
610
657
  /** What the load did with the converted-weight cache ({@link LoadModelOptions.weightCache}). */
611
658
  readonly weightCache: WeightCacheInfo;
659
+ /** Memory of the model, estimated at load: weights, KV cache and scratch, against the device's limits. */
660
+ readonly memory: ModelMemory;
612
661
  /** Milliseconds spent downloading and loading. */
613
662
  readonly loadTimeMs: number;
614
663
  }
@@ -1,47 +1,47 @@
1
1
  {
2
- "version": "0.6.0",
2
+ "version": "0.7.0",
3
3
  "assets": {
4
4
  "darwin-arm64": {
5
- "file": "webslm-node-0.6.0-darwin-arm64.node.gz",
6
- "size": 5143165,
7
- "sha256": "f2700ee1eb7d60ccd0c50053cf67e15d34c6056413f22b4fd69e73ed8ef45783",
8
- "nodeSha256": "66bfdd8160ddb128beb6055f42161e0c47d21f82772035cdd09e58db6f6fd502"
5
+ "file": "webslm-node-0.7.0-darwin-arm64.node.gz",
6
+ "size": 5186619,
7
+ "sha256": "b71fd469f2a2e6b81cbf51fab65bf6dbbc32c4a054f7003aa7a4d65afec87cff",
8
+ "nodeSha256": "34b9a88ebf50ca0f249c7191523919bf090e77288a50784d7093193d7aa75a7e"
9
9
  },
10
10
  "darwin-x64": {
11
- "file": "webslm-node-0.6.0-darwin-x64.node.gz",
12
- "size": 5416993,
13
- "sha256": "4390851cb0bf1698b0308b7932dee58a10bd6cdb0390333fefc8e316fb7f9601",
14
- "nodeSha256": "5f9cfd9c62d275415a04c6f3fb12c9fa7435e538a8952cef80d4992e4b52d31f"
11
+ "file": "webslm-node-0.7.0-darwin-x64.node.gz",
12
+ "size": 5463196,
13
+ "sha256": "0bfa4bd88609a85ea6b0f6cdd0db62a65647bd683d2442b39e621554bd22fc4d",
14
+ "nodeSha256": "3f7b6f3804695c80da8da56b2d586b46e5e02be6aedd00251f87582c1621dd58"
15
15
  },
16
16
  "linux-x64-gnu": {
17
- "file": "webslm-node-0.6.0-linux-x64-gnu.node.gz",
18
- "size": 5453454,
19
- "sha256": "66b4efe9cd4b944addd326c0d927abada2e78f96425e4976aa230756aca75d88",
20
- "nodeSha256": "8d77ea1816275d4516253f49cd94720a2c722f9f2e9ce218efb437c32ebe6acd"
17
+ "file": "webslm-node-0.7.0-linux-x64-gnu.node.gz",
18
+ "size": 5500625,
19
+ "sha256": "642ba1ff52d5244486e9b0995ed8b670b2fa1a75aef014262121d553023b27bb",
20
+ "nodeSha256": "836a4b1b1b2ea37c478871f744c88062a135cf9889ef58be6435d2dbf0adf814"
21
21
  },
22
22
  "linux-arm64-gnu": {
23
- "file": "webslm-node-0.6.0-linux-arm64-gnu.node.gz",
24
- "size": 5121058,
25
- "sha256": "84bf11007c312761dc2377de89725bd839cb538834c01f80b26e1b0ba9456d7a",
26
- "nodeSha256": "4596e8f5180ed2e1cb20721ac01142f4a1fa8e062a86280053acf377159d8ba2"
23
+ "file": "webslm-node-0.7.0-linux-arm64-gnu.node.gz",
24
+ "size": 5160915,
25
+ "sha256": "574a67dc1cf3f2e78368b987f1084e28542a28fda25d7640a23d293aab21c7f9",
26
+ "nodeSha256": "9fe42ed8ce64b79a0dd549d3677de7562e18061458ab843e7f73d57bb6481c6e"
27
27
  },
28
28
  "linux-x64-musl": {
29
- "file": "webslm-node-0.6.0-linux-x64-musl.node.gz",
30
- "size": 5842409,
31
- "sha256": "9a8ddc2a0f0649714ae8cbca9e31fa2405fdaf628ac14ec97296877c37e4506f",
32
- "nodeSha256": "25f73331e30d7e2e2295af07fcbc7f0d747f5ef80803da2830fd175f61e670b1"
29
+ "file": "webslm-node-0.7.0-linux-x64-musl.node.gz",
30
+ "size": 5891685,
31
+ "sha256": "a447a0d7b4e62acf7a816e7155053cf2a49657a6a2f147bfa87ba7991db72813",
32
+ "nodeSha256": "8dbf43b1db37ebfb1ed80c8cd7aae5749662ec7cbc53fcc787a9990f2e1987a2"
33
33
  },
34
34
  "linux-arm64-musl": {
35
- "file": "webslm-node-0.6.0-linux-arm64-musl.node.gz",
36
- "size": 5627078,
37
- "sha256": "44178e33156cb2b6321414f74d4d4f9944183b2a2625cd5be19d21e16fd6dc21",
38
- "nodeSha256": "3d76dacae55375d839b3d731634704fed52a84429288c2c9878be017d9a5ac78"
35
+ "file": "webslm-node-0.7.0-linux-arm64-musl.node.gz",
36
+ "size": 5670254,
37
+ "sha256": "c475896a044e2524b9affd4d1405fa3cc3c8cf6b615a757c6db1794b0686ca68",
38
+ "nodeSha256": "ecaa553f3ad3a8d259014031ad44d28552d755b55a01ac992dee086edd7bab52"
39
39
  },
40
40
  "win32-x64-msvc": {
41
- "file": "webslm-node-0.6.0-win32-x64-msvc.node.gz",
42
- "size": 5886895,
43
- "sha256": "9c96790b22159549d6e620460311aa243bee8c217d8ff0d536f29e4381792c2d",
44
- "nodeSha256": "911cfe54c80b1e4f8476c3778981e73dc774e34cc1108ed848fbe93240e16e8d"
41
+ "file": "webslm-node-0.7.0-win32-x64-msvc.node.gz",
42
+ "size": 5937878,
43
+ "sha256": "474409ee9825da5d28725ce3658eff6bc6c51c7085f9929c61defa94034070e9",
44
+ "nodeSha256": "9920eeddbe937e3b986a15d3af28456245da2ce08b8984eb1ebf3a0e6fa621ef"
45
45
  }
46
46
  }
47
47
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webslm",
3
- "version": "0.6.0",
3
+ "version": "0.7.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",
@@ -99,7 +99,8 @@ export class WasmModel {
99
99
  * contextLength?: number, deterministic?: boolean,
100
100
  * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto",
101
101
  * adapterScale?: number, adapterMode?: "unmerged" | "merged",
102
- * vision?: boolean }` (`vision: false` skips the vision encoder of a
102
+ * vision?: boolean, kvCache?: "f32" | "int8", kvResidual?: number,
103
+ * kvBudget?: number }` (`vision: false` skips the vision encoder of a
103
104
  * vision-language checkpoint).
104
105
  *
105
106
  * `device` defaults to `"cpu"`. `"auto"` picks WebGPU when an adapter
@@ -268,7 +268,8 @@ export class WasmModel {
268
268
  * contextLength?: number, deterministic?: boolean,
269
269
  * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto",
270
270
  * adapterScale?: number, adapterMode?: "unmerged" | "merged",
271
- * vision?: boolean }` (`vision: false` skips the vision encoder of a
271
+ * vision?: boolean, kvCache?: "f32" | "int8", kvResidual?: number,
272
+ * kvBudget?: number }` (`vision: false` skips the vision encoder of a
272
273
  * vision-language checkpoint).
273
274
  *
274
275
  * `device` defaults to `"cpu"`. `"auto"` picks WebGPU when an adapter
@@ -1483,32 +1484,32 @@ function __wbg_get_imports() {
1483
1484
  arg0.writeBuffer(arg1, arg2, getArrayU8FromWasm0(arg3, arg4), arg5, arg6);
1484
1485
  }, arguments); },
1485
1486
  __wbindgen_generic_0000000000000001: function(arg0, arg1) {
1486
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 330, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1487
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 331, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1487
1488
  const ret = makeMutClosure(arg0, arg1, wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wasm_bindgen_8cf186fa7724d16d___JsValue__core_608f92abc48d28da___result__Result_____wasm_bindgen_8cf186fa7724d16d___JsError___true_);
1488
1489
  return ret;
1489
1490
  },
1490
1491
  __wbindgen_generic_0000000000000002: function(arg0, arg1) {
1491
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUDevice")], shim_idx: 262, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1492
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUDevice")], shim_idx: 263, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1492
1493
  const ret = makeMutClosure(arg0, arg1, 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_);
1493
1494
  return ret;
1494
1495
  },
1495
1496
  __wbindgen_generic_0000000000000003: function(arg0, arg1) {
1496
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUDeviceLostInfo")], shim_idx: 266, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1497
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUDeviceLostInfo")], shim_idx: 267, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1497
1498
  const ret = makeMutClosure(arg0, arg1, wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuDeviceLostInfo__GpuDeviceLostInfo______true_);
1498
1499
  return ret;
1499
1500
  },
1500
1501
  __wbindgen_generic_0000000000000004: function(arg0, arg1) {
1501
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUUncapturedErrorEvent")], shim_idx: 266, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1502
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUUncapturedErrorEvent")], shim_idx: 267, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1502
1503
  const ret = makeMutClosure(arg0, arg1, wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuDeviceLostInfo__GpuDeviceLostInfo______true__39);
1503
1504
  return ret;
1504
1505
  },
1505
1506
  __wbindgen_generic_0000000000000005: function(arg0, arg1) {
1506
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("any")], shim_idx: 262, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1507
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("any")], shim_idx: 263, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1507
1508
  const ret = makeMutClosure(arg0, arg1, 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__40);
1508
1509
  return ret;
1509
1510
  },
1510
1511
  __wbindgen_generic_0000000000000006: function(arg0, arg1) {
1511
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("undefined")], shim_idx: 262, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1512
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("undefined")], shim_idx: 263, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1512
1513
  const ret = makeMutClosure(arg0, arg1, 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__41);
1513
1514
  return ret;
1514
1515
  },
Binary file
@@ -99,7 +99,8 @@ export class WasmModel {
99
99
  * contextLength?: number, deterministic?: boolean,
100
100
  * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto",
101
101
  * adapterScale?: number, adapterMode?: "unmerged" | "merged",
102
- * vision?: boolean }` (`vision: false` skips the vision encoder of a
102
+ * vision?: boolean, kvCache?: "f32" | "int8", kvResidual?: number,
103
+ * kvBudget?: number }` (`vision: false` skips the vision encoder of a
103
104
  * vision-language checkpoint).
104
105
  *
105
106
  * `device` defaults to `"cpu"`. `"auto"` picks WebGPU when an adapter
@@ -268,7 +268,8 @@ export class WasmModel {
268
268
  * contextLength?: number, deterministic?: boolean,
269
269
  * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto",
270
270
  * adapterScale?: number, adapterMode?: "unmerged" | "merged",
271
- * vision?: boolean }` (`vision: false` skips the vision encoder of a
271
+ * vision?: boolean, kvCache?: "f32" | "int8", kvResidual?: number,
272
+ * kvBudget?: number }` (`vision: false` skips the vision encoder of a
272
273
  * vision-language checkpoint).
273
274
  *
274
275
  * `device` defaults to `"cpu"`. `"auto"` picks WebGPU when an adapter
@@ -1483,32 +1484,32 @@ function __wbg_get_imports() {
1483
1484
  arg0.writeBuffer(arg1, arg2, getArrayU8FromWasm0(arg3, arg4), arg5, arg6);
1484
1485
  }, arguments); },
1485
1486
  __wbindgen_generic_0000000000000001: function(arg0, arg1) {
1486
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 321, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1487
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 322, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1487
1488
  const ret = makeMutClosure(arg0, arg1, wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wasm_bindgen_8cf186fa7724d16d___JsValue__core_608f92abc48d28da___result__Result_____wasm_bindgen_8cf186fa7724d16d___JsError___true_);
1488
1489
  return ret;
1489
1490
  },
1490
1491
  __wbindgen_generic_0000000000000002: function(arg0, arg1) {
1491
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUDevice")], shim_idx: 253, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1492
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUDevice")], shim_idx: 254, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1492
1493
  const ret = makeMutClosure(arg0, arg1, 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_);
1493
1494
  return ret;
1494
1495
  },
1495
1496
  __wbindgen_generic_0000000000000003: function(arg0, arg1) {
1496
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUDeviceLostInfo")], shim_idx: 257, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1497
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUDeviceLostInfo")], shim_idx: 258, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1497
1498
  const ret = makeMutClosure(arg0, arg1, wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuDeviceLostInfo__GpuDeviceLostInfo______true_);
1498
1499
  return ret;
1499
1500
  },
1500
1501
  __wbindgen_generic_0000000000000004: function(arg0, arg1) {
1501
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUUncapturedErrorEvent")], shim_idx: 257, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1502
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUUncapturedErrorEvent")], shim_idx: 258, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1502
1503
  const ret = makeMutClosure(arg0, arg1, wasm_bindgen_8cf186fa7724d16d___convert__closures_____invoke___wgpu_54917290532eaf6b___backend__webgpu__webgpu_sys__gen_GpuDeviceLostInfo__GpuDeviceLostInfo______true__39);
1503
1504
  return ret;
1504
1505
  },
1505
1506
  __wbindgen_generic_0000000000000005: function(arg0, arg1) {
1506
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("any")], shim_idx: 253, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1507
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("any")], shim_idx: 254, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1507
1508
  const ret = makeMutClosure(arg0, arg1, 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__40);
1508
1509
  return ret;
1509
1510
  },
1510
1511
  __wbindgen_generic_0000000000000006: function(arg0, arg1) {
1511
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("undefined")], shim_idx: 253, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1512
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("undefined")], shim_idx: 254, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1512
1513
  const ret = makeMutClosure(arg0, arg1, 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__41);
1513
1514
  return ret;
1514
1515
  },
Binary file
@@ -99,7 +99,8 @@ export class WasmModel {
99
99
  * contextLength?: number, deterministic?: boolean,
100
100
  * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto",
101
101
  * adapterScale?: number, adapterMode?: "unmerged" | "merged",
102
- * vision?: boolean }` (`vision: false` skips the vision encoder of a
102
+ * vision?: boolean, kvCache?: "f32" | "int8", kvResidual?: number,
103
+ * kvBudget?: number }` (`vision: false` skips the vision encoder of a
103
104
  * vision-language checkpoint).
104
105
  *
105
106
  * `device` defaults to `"cpu"`. `"auto"` picks WebGPU when an adapter
@@ -270,7 +270,8 @@ export class WasmModel {
270
270
  * contextLength?: number, deterministic?: boolean,
271
271
  * activations?: "quantized" | "f32", device?: "cpu" | "webgpu" | "auto",
272
272
  * adapterScale?: number, adapterMode?: "unmerged" | "merged",
273
- * vision?: boolean }` (`vision: false` skips the vision encoder of a
273
+ * vision?: boolean, kvCache?: "f32" | "int8", kvResidual?: number,
274
+ * kvBudget?: number }` (`vision: false` skips the vision encoder of a
274
275
  * vision-language checkpoint).
275
276
  *
276
277
  * `device` defaults to `"cpu"`. `"auto"` picks WebGPU when an adapter
@@ -1643,42 +1644,42 @@ function __wbg_get_imports(memory) {
1643
1644
  arg0.writeBuffer(arg1, arg2, getArrayU8FromWasm0(arg3, arg4), arg5, arg6);
1644
1645
  }, arguments); },
1645
1646
  __wbindgen_generic_0000000000000001: function(arg0, arg1) {
1646
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 391, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1647
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 392, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1647
1648
  const ret = makeMutClosure(arg0, arg1, wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___wasm_bindgen_3c612a990bfab728___JsValue______true_);
1648
1649
  return ret;
1649
1650
  },
1650
1651
  __wbindgen_generic_0000000000000002: function(arg0, arg1) {
1651
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 931, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1652
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 932, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1652
1653
  const ret = makeMutClosure(arg0, arg1, wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___wasm_bindgen_3c612a990bfab728___JsValue__core_2b10d7c4e784534e___result__Result_____wasm_bindgen_3c612a990bfab728___JsError___true_);
1653
1654
  return ret;
1654
1655
  },
1655
1656
  __wbindgen_generic_0000000000000003: function(arg0, arg1) {
1656
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 933, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1657
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 934, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1657
1658
  const ret = makeMutClosure(arg0, arg1, wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___js_sys_b608be85a12bc1d0___futures__task__wait_async_polyfill__MessageEvent______true_);
1658
1659
  return ret;
1659
1660
  },
1660
1661
  __wbindgen_generic_0000000000000004: function(arg0, arg1) {
1661
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUDevice")], shim_idx: 389, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1662
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUDevice")], shim_idx: 390, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1662
1663
  const ret = makeMutClosure(arg0, arg1, wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___wasm_bindgen_3c612a990bfab728___sys__JsNullable_wgpu_4ecf802b864e9046___backend__webgpu__webgpu_sys__gen_GpuError__GpuError___core_2b10d7c4e784534e___result__Result_____wasm_bindgen_3c612a990bfab728___JsError___true_);
1663
1664
  return ret;
1664
1665
  },
1665
1666
  __wbindgen_generic_0000000000000005: function(arg0, arg1) {
1666
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUDeviceLostInfo")], shim_idx: 391, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1667
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUDeviceLostInfo")], shim_idx: 392, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1667
1668
  const ret = makeMutClosure(arg0, arg1, wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___wasm_bindgen_3c612a990bfab728___JsValue______true__46);
1668
1669
  return ret;
1669
1670
  },
1670
1671
  __wbindgen_generic_0000000000000006: function(arg0, arg1) {
1671
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUUncapturedErrorEvent")], shim_idx: 391, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1672
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("GPUUncapturedErrorEvent")], shim_idx: 392, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1672
1673
  const ret = makeMutClosure(arg0, arg1, wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___wasm_bindgen_3c612a990bfab728___JsValue______true__47);
1673
1674
  return ret;
1674
1675
  },
1675
1676
  __wbindgen_generic_0000000000000007: function(arg0, arg1) {
1676
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("any")], shim_idx: 389, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1677
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("any")], shim_idx: 390, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1677
1678
  const ret = makeMutClosure(arg0, arg1, wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___wasm_bindgen_3c612a990bfab728___sys__JsNullable_wgpu_4ecf802b864e9046___backend__webgpu__webgpu_sys__gen_GpuError__GpuError___core_2b10d7c4e784534e___result__Result_____wasm_bindgen_3c612a990bfab728___JsError___true__48);
1678
1679
  return ret;
1679
1680
  },
1680
1681
  __wbindgen_generic_0000000000000008: function(arg0, arg1) {
1681
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("undefined")], shim_idx: 389, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1682
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("undefined")], shim_idx: 390, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1682
1683
  const ret = makeMutClosure(arg0, arg1, wasm_bindgen_3c612a990bfab728___convert__closures_____invoke___wasm_bindgen_3c612a990bfab728___sys__JsNullable_wgpu_4ecf802b864e9046___backend__webgpu__webgpu_sys__gen_GpuError__GpuError___core_2b10d7c4e784534e___result__Result_____wasm_bindgen_3c612a990bfab728___JsError___true__49);
1683
1684
  return ret;
1684
1685
  },
@@ -1725,7 +1726,7 @@ function __wbg_get_imports(memory) {
1725
1726
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
1726
1727
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
1727
1728
  },
1728
- memory: memory || new WebAssembly.Memory({initial:29,maximum:65536,shared:true}),
1729
+ memory: memory || new WebAssembly.Memory({initial:30,maximum:65536,shared:true}),
1729
1730
  };
1730
1731
  return {
1731
1732
  __proto__: null,
Binary file