bitgpu 0.9.0 → 0.10.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,20 @@ 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';
55
60
  /** Called as the model loads. */
56
61
  onProgress?: (progress: LoadProgress) => void;
57
62
  /** Called if the GPU device is lost after creation (driver reset, OS reclaim, tab backgrounding
@@ -189,8 +194,9 @@ interface EngineCapabilities {
189
194
  useSubgroups: boolean;
190
195
  /** Subgroup width when the subgroup path is active. */
191
196
  subgroupSize: number;
192
- /** Active KV-cache storage precision ('f16' only when requested AND the adapter has shader-f16). */
193
- kvCache: 'f32' | 'f16';
197
+ /** Active KV-cache storage precision ('f16' only when requested AND the adapter has shader-f16;
198
+ * 'q8' whenever requested - it needs no adapter feature). */
199
+ kvCache: 'f32' | 'f16' | 'q8';
194
200
  /** The engine's KV window in positions (the resolved maxSeqLen option): prompt + generated
195
201
  * tokens per conversation must fit inside it. */
196
202
  maxSeqLen: number;
@@ -207,6 +213,29 @@ interface EngineCapabilities {
207
213
  maxComputeWorkgroupStorageSize: number;
208
214
  };
209
215
  }
216
+ /** A saved KV cache + conversation token history, from {@link Engine.saveCache}. A plain object
217
+ * holding one `ArrayBuffer`, so it is structured-cloneable: store it in IndexedDB / OPFS or send
218
+ * it over `postMessage` as-is (it is NOT `JSON.stringify`-able - the buffer would be lost).
219
+ * Restore requires the SAME model architecture and the SAME `kvCache` mode; snapshots never
220
+ * convert across modes. Treat the fields as opaque. */
221
+ interface KvSnapshot {
222
+ /** Snapshot format version (currently `1`). */
223
+ version: 1;
224
+ /** KV storage mode the snapshot was taken under. */
225
+ kvCache: 'f32' | 'f16' | 'q8';
226
+ /** Architecture stamp; restore rejects a snapshot from a different model shape. */
227
+ model: {
228
+ layers: number;
229
+ kvHeads: number;
230
+ headDim: number;
231
+ hidden: number;
232
+ vocab: number;
233
+ };
234
+ /** The full conversation token sequence at save time. */
235
+ ids: number[];
236
+ /** Packed per-layer cached K/V bytes (and q8 block scales). */
237
+ data: ArrayBuffer;
238
+ }
210
239
  /** A loaded model ready to generate. Create one with {@link createEngine}. */
211
240
  interface Engine {
212
241
  /** Generate tokens from a prompt given as token ids. */
@@ -222,6 +251,19 @@ interface Engine {
222
251
  forward(tokenIds: number[]): Promise<ForwardResult>;
223
252
  /** Clear the cross-turn KV cache and token history (start a fresh conversation). */
224
253
  resetCache(): void;
254
+ /** Snapshot the current conversation - KV cache contents + token history - as a
255
+ * structured-cloneable {@link KvSnapshot} (GPU -> CPU readback). Restoring it, into this
256
+ * engine or a fresh one on the same model and `kvCache` mode, is bit-identical to having kept
257
+ * the conversation alive, so conversations survive engine disposal and page reloads (persist
258
+ * the snapshot in IndexedDB / OPFS). Size ~ tokens x layers x kvHeads x headDim x 2 x
259
+ * bytes-per-value (~224 KB/token at f32 on Bonsai-1.7B, ~63 KB/token at q8). Returns `null`
260
+ * when the cache is empty. Serialized with generate/prefill/forward like every engine op. */
261
+ saveCache(): Promise<KvSnapshot | null>;
262
+ /** Replace the current conversation with a {@link KvSnapshot} (CPU -> GPU upload). The next
263
+ * `generate(delta, { reuseCache: true })` continues the saved conversation exactly. Throws if
264
+ * the snapshot's model architecture or `kvCache` mode does not match this engine, or if it
265
+ * exceeds this engine's `maxSeqLen`. */
266
+ restoreCache(snapshot: KvSnapshot): Promise<void>;
225
267
  /** Detected GPU capabilities and selected code path. */
226
268
  readonly capabilities: EngineCapabilities;
227
269
  /** Resolves when the GPU device is lost (including via {@link dispose}, with reason 'destroyed').
@@ -231,5 +273,5 @@ interface Engine {
231
273
  dispose(): void;
232
274
  }
233
275
  //#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
276
+ 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 };
277
+ //# sourceMappingURL=types-CcgsXirU.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.10.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",