bitgpu 0.9.0 → 0.11.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.
@@ -43,15 +43,34 @@ interface EngineOptions {
43
43
  /** Prefill GEMM tiling: `'auto'` tiles once a prompt fills the 64-row tiles, `'always'`/`'never'` force it. Default `'auto'`. */
44
44
  prefillTiling?: 'auto' | 'always' | 'never';
45
45
  /** Max KV-cache length (prompt + generated positions). Caps VRAM (~`maxSeqLen` x 224 KB at f32,
46
- * half that with `kvCache: 'f16'`). Default `2048`. */
46
+ * half that with `kvCache: 'f16'`, ~a quarter with `'q8'`). Default `2048`. */
47
47
  maxSeqLen?: number;
48
- /** KV-cache STORAGE precision. `'f16'` halves KV memory (the industry-standard choice: all
49
- * arithmetic stays f32; each cached K/V value is rounded once at cache-write). Outputs are no
50
- * longer guaranteed bit-identical to f32 mode, though within f16 mode decoding stays exact and
51
- * deterministic (same seed -> same tokens; cache reuse == full prefill). Falls back to `'f32'`
52
- * silently when the adapter lacks `shader-f16` (see `capabilities.kvCache` for what's active).
53
- * Default `'f32'`. */
54
- kvCache?: 'f32' | 'f16';
48
+ /** KV-cache STORAGE precision. `'f16'` halves KV memory (all arithmetic stays f32; each cached
49
+ * K/V value is rounded once at cache-write); it falls back to `'f32'` silently when the
50
+ * adapter lacks `shader-f16`. `'q8'` QUARTERS KV memory vs f32 (packed 8-bit values with one
51
+ * f32 scale per 32-element block, llama.cpp q8_0-style, ~1.125 bytes/value) and needs no
52
+ * adapter feature, so it works even where f16 does not (and at long context it decodes FASTER
53
+ * than f32 - attention there is memory-bound and reads a quarter of the bytes). Outputs under
54
+ * f16/q8 are no longer
55
+ * guaranteed bit-identical to f32 mode - q8 is the first knowingly-lossy tier, measured per
56
+ * model by the GPU gate - though WITHIN a mode decoding stays exact and deterministic (same
57
+ * seed -> same tokens; cache reuse == full prefill). See `capabilities.kvCache` for what's
58
+ * active. Default `'f32'`. */
59
+ kvCache?: 'f32' | 'f16' | 'q8';
60
+ /** What happens when a conversation outgrows `maxSeqLen`. `'error'` (default): generate
61
+ * throws, exactly as before. `'sinks'`: StreamingLLM-style rolling window - the first
62
+ * `sinkTokens` positions (attention sinks) plus the most recent window are kept and the
63
+ * middle is evicted in batches, so generation and multi-turn chat continue indefinitely in
64
+ * fixed memory. Keys are cached UNROPED and rotated at read by cache-relative position, so
65
+ * eviction never rewrites or (under q8) requantizes cache bytes. Sink mode is its own
66
+ * measured mode like f16/q8: within-mode decoding stays exact and deterministic, and before
67
+ * the first eviction f32+sinks matches default f32 (gated); after eviction the model
68
+ * genuinely forgets evicted middle context - that is the trade. Prompts longer than the
69
+ * window still throw (trim prompt-side, e.g. chat onOverflow). */
70
+ overflow?: 'error' | 'sinks';
71
+ /** Number of initial attention-sink positions kept forever under `overflow: 'sinks'`.
72
+ * Default `4` (the StreamingLLM setting). */
73
+ sinkTokens?: number;
55
74
  /** Called as the model loads. */
56
75
  onProgress?: (progress: LoadProgress) => void;
57
76
  /** Called if the GPU device is lost after creation (driver reset, OS reclaim, tab backgrounding
@@ -189,10 +208,14 @@ interface EngineCapabilities {
189
208
  useSubgroups: boolean;
190
209
  /** Subgroup width when the subgroup path is active. */
191
210
  subgroupSize: number;
192
- /** Active KV-cache storage precision ('f16' only when requested AND the adapter has shader-f16). */
193
- kvCache: 'f32' | 'f16';
194
- /** The engine's KV window in positions (the resolved maxSeqLen option): prompt + generated
195
- * tokens per conversation must fit inside it. */
211
+ /** Active KV-cache storage precision ('f16' only when requested AND the adapter has shader-f16;
212
+ * 'q8' whenever requested - it needs no adapter feature). */
213
+ kvCache: 'f32' | 'f16' | 'q8';
214
+ /** Active overflow policy ('sinks' = rolling window with attention sinks). */
215
+ overflow: 'error' | 'sinks';
216
+ /** The engine's KV window in positions (the resolved maxSeqLen option). Under
217
+ * `overflow: 'error'` prompt + generated tokens must fit inside it; under `'sinks'` it is
218
+ * the rolling window size. */
196
219
  maxSeqLen: number;
197
220
  /** Adapter identification, when the browser exposes it. */
198
221
  adapter: {
@@ -207,6 +230,35 @@ interface EngineCapabilities {
207
230
  maxComputeWorkgroupStorageSize: number;
208
231
  };
209
232
  }
233
+ /** A saved KV cache + conversation token history, from {@link Engine.saveCache}. A plain object
234
+ * holding one `ArrayBuffer`, so it is structured-cloneable: store it in IndexedDB / OPFS or send
235
+ * it over `postMessage` as-is (it is NOT `JSON.stringify`-able - the buffer would be lost).
236
+ * Restore requires the SAME model architecture and the SAME `kvCache` mode; snapshots never
237
+ * convert across modes. Treat the fields as opaque. */
238
+ interface KvSnapshot {
239
+ /** Snapshot format version: `1` = default mode, `2` = saved under `overflow: 'sinks'`
240
+ * (the cache holds unroped keys and a rolled window; restore requires sink mode). */
241
+ version: 1 | 2;
242
+ /** KV storage mode the snapshot was taken under. */
243
+ kvCache: 'f32' | 'f16' | 'q8';
244
+ /** Version 2 only: rolling-window state (sink count and filled cache slots). */
245
+ roll?: {
246
+ sinkTokens: number;
247
+ cacheLen: number;
248
+ };
249
+ /** Architecture stamp; restore rejects a snapshot from a different model shape. */
250
+ model: {
251
+ layers: number;
252
+ kvHeads: number;
253
+ headDim: number;
254
+ hidden: number;
255
+ vocab: number;
256
+ };
257
+ /** The full conversation token sequence at save time. */
258
+ ids: number[];
259
+ /** Packed per-layer cached K/V bytes (and q8 block scales). */
260
+ data: ArrayBuffer;
261
+ }
210
262
  /** A loaded model ready to generate. Create one with {@link createEngine}. */
211
263
  interface Engine {
212
264
  /** Generate tokens from a prompt given as token ids. */
@@ -222,6 +274,19 @@ interface Engine {
222
274
  forward(tokenIds: number[]): Promise<ForwardResult>;
223
275
  /** Clear the cross-turn KV cache and token history (start a fresh conversation). */
224
276
  resetCache(): void;
277
+ /** Snapshot the current conversation - KV cache contents + token history - as a
278
+ * structured-cloneable {@link KvSnapshot} (GPU -> CPU readback). Restoring it, into this
279
+ * engine or a fresh one on the same model and `kvCache` mode, is bit-identical to having kept
280
+ * the conversation alive, so conversations survive engine disposal and page reloads (persist
281
+ * the snapshot in IndexedDB / OPFS). Size ~ tokens x layers x kvHeads x headDim x 2 x
282
+ * bytes-per-value (~224 KB/token at f32 on Bonsai-1.7B, ~63 KB/token at q8). Returns `null`
283
+ * when the cache is empty. Serialized with generate/prefill/forward like every engine op. */
284
+ saveCache(): Promise<KvSnapshot | null>;
285
+ /** Replace the current conversation with a {@link KvSnapshot} (CPU -> GPU upload). The next
286
+ * `generate(delta, { reuseCache: true })` continues the saved conversation exactly. Throws if
287
+ * the snapshot's model architecture or `kvCache` mode does not match this engine, or if it
288
+ * exceeds this engine's `maxSeqLen`. */
289
+ restoreCache(snapshot: KvSnapshot): Promise<void>;
225
290
  /** Detected GPU capabilities and selected code path. */
226
291
  readonly capabilities: EngineCapabilities;
227
292
  /** Resolves when the GPU device is lost (including via {@link dispose}, with reason 'destroyed').
@@ -231,5 +296,5 @@ interface Engine {
231
296
  dispose(): void;
232
297
  }
233
298
  //#endregion
234
- export { ForwardResult as a, LoadProgress as c, EngineOptions as i, TokenLogprobs as l, Engine as n, GenerateOptions as o, EngineCapabilities as r, GenerateResult as s, DeviceLostInfo as t };
235
- //# sourceMappingURL=types-C5f2IKgm.d.ts.map
299
+ export { ForwardResult as a, KvSnapshot as c, EngineOptions as i, LoadProgress as l, Engine as n, GenerateOptions as o, EngineCapabilities as r, GenerateResult as s, DeviceLostInfo as t, TokenLogprobs as u };
300
+ //# sourceMappingURL=types-DJ61q1gX.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bitgpu",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "description": "Fast WebGPU runtime for 1-bit (binary-weight) LLMs in the browser. Bit-exact, zero runtime dependencies.",
5
5
  "type": "module",
6
6
  "license": "MIT",