bitgpu 0.11.0 → 0.13.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 +61 -29
- package/dist/chat.d.ts +1 -1
- package/dist/gguf.d.ts +33 -0
- package/dist/gguf.js +394 -0
- package/dist/gguf.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +166 -31
- package/dist/index.js.map +1 -1
- package/dist/{types-DJ61q1gX.d.ts → types-ByYx15Yw.d.ts} +77 -3
- package/package.json +7 -2
package/dist/index.js
CHANGED
|
@@ -234,6 +234,38 @@ const eqBytes = (a, b) => {
|
|
|
234
234
|
for (let i = 0; i < b.length; i++) if (a[i] !== b[i]) return false;
|
|
235
235
|
return true;
|
|
236
236
|
};
|
|
237
|
+
/** Synthesize f32 rope tables ([positions, head_dim/2]) from arch.rope for manifests without
|
|
238
|
+
* baked caches (v2/GGUF). Plain rope or YaRN (transformers formula: beta_fast 32, beta_slow 1,
|
|
239
|
+
* mscale = 0.1*ln(factor)+1). Angles accumulate in f64 and round once to f32 per entry -
|
|
240
|
+
* tools/reference.py implements the identical recipe for fixture generation. */
|
|
241
|
+
function synthRope(A, positions) {
|
|
242
|
+
const half = A.head_dim / 2;
|
|
243
|
+
const rope = A.rope;
|
|
244
|
+
const base = rope.rope_theta;
|
|
245
|
+
const factor = rope.rope_type === "yarn" ? rope.factor ?? 1 : 1;
|
|
246
|
+
const inv = new Float64Array(half);
|
|
247
|
+
const orig = rope.original_max_position_embeddings ?? 0;
|
|
248
|
+
const lo = factor === 1 ? 0 : Math.max(0, Math.floor(A.head_dim * Math.log(orig / (64 * Math.PI)) / (2 * Math.log(base))));
|
|
249
|
+
const hi = factor === 1 ? 0 : Math.min(half - 1, Math.ceil(A.head_dim * Math.log(orig / (2 * Math.PI)) / (2 * Math.log(base))));
|
|
250
|
+
for (let j = 0; j < half; j++) {
|
|
251
|
+
const pf = base ** (2 * j / A.head_dim);
|
|
252
|
+
if (factor === 1) {
|
|
253
|
+
inv[j] = 1 / pf;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
const ramp = Math.min(1, Math.max(0, (j - lo) / (hi - lo)));
|
|
257
|
+
inv[j] = 1 / (factor * pf) * ramp + 1 / pf * (1 - ramp);
|
|
258
|
+
}
|
|
259
|
+
const mscale = factor === 1 ? 1 : Math.fround(.1 * Math.log(factor) + 1);
|
|
260
|
+
const cos = new Float32Array(positions * half);
|
|
261
|
+
const sin = new Float32Array(positions * half);
|
|
262
|
+
for (let p = 0; p < positions; p++) for (let j = 0; j < half; j++) {
|
|
263
|
+
const a = p * inv[j];
|
|
264
|
+
cos[p * half + j] = Math.fround(Math.cos(a) * mscale);
|
|
265
|
+
sin[p * half + j] = Math.fround(Math.sin(a) * mscale);
|
|
266
|
+
}
|
|
267
|
+
return [cos, sin];
|
|
268
|
+
}
|
|
237
269
|
/** Load a 1-bit model and return an {@link Engine}. Pass a model URL string for defaults. */
|
|
238
270
|
async function createEngine(options) {
|
|
239
271
|
const holder = {};
|
|
@@ -247,7 +279,8 @@ async function createEngine(options) {
|
|
|
247
279
|
async function createEngineInner(options, holder) {
|
|
248
280
|
const opts = typeof options === "string" ? { modelUrl: options } : options;
|
|
249
281
|
const modelDir = opts.modelUrl ? opts.modelUrl.replace(/\/$/, "") : null;
|
|
250
|
-
if (!modelDir && !opts.manifestUrl) throw new Error("createEngine: provide modelUrl or
|
|
282
|
+
if (!modelDir && !opts.manifestUrl && !opts.manifest) throw new Error("createEngine: provide modelUrl, manifestUrl, or an in-memory manifest");
|
|
283
|
+
if (opts.manifest && !modelDir && !opts.dataUrl) throw new Error("createEngine: an in-memory manifest needs dataUrl (or modelUrl) for the weights file");
|
|
251
284
|
const powerPreference = opts.powerPreference ?? "high-performance";
|
|
252
285
|
const fetchJson = opts.fetchJson ?? (async (url) => {
|
|
253
286
|
const res = await fetch(url);
|
|
@@ -284,10 +317,12 @@ async function createEngineInner(options, holder) {
|
|
|
284
317
|
});
|
|
285
318
|
if (typeof navigator === "undefined" || !navigator.gpu) throw new WebGPUUnavailableError("WebGPU is not available (no navigator.gpu). Use a WebGPU-capable browser over a secure context.");
|
|
286
319
|
opts.onProgress?.({ phase: "manifest" });
|
|
287
|
-
const manifest = await fetchJson(opts.manifestUrl ?? `${modelDir}/manifest.json`);
|
|
320
|
+
const manifest = opts.manifest ?? await fetchJson(opts.manifestUrl ?? `${modelDir}/manifest.json`);
|
|
288
321
|
opts.onProgress?.({ phase: "weights" });
|
|
289
322
|
const dataUrl = opts.dataUrl ?? `${modelDir}/${manifest.data_file}`;
|
|
290
|
-
let aux
|
|
323
|
+
let aux;
|
|
324
|
+
if (opts.aux) aux = opts.aux instanceof Uint8Array ? new Uint8Array(opts.aux).buffer : opts.aux;
|
|
325
|
+
else aux = await fetchBytes(opts.auxUrl ?? `${modelDir}/${manifest.aux_file}`);
|
|
291
326
|
const A = manifest.arch;
|
|
292
327
|
const T = manifest.tensors;
|
|
293
328
|
const FINAL_NORM = `layers.${A.layers}.final_norm_layernorm`;
|
|
@@ -295,8 +330,33 @@ async function createEngineInner(options, holder) {
|
|
|
295
330
|
if (A.head_dim > 128) throw new Error(`bitgpu: unsupported head_dim ${A.head_dim} (kernels assume <= 128)`);
|
|
296
331
|
if (A.heads % A.kv_heads !== 0) throw new Error(`bitgpu: heads ${A.heads} not divisible by kv_heads ${A.kv_heads} (GQA kernels assume an integer group size)`);
|
|
297
332
|
if (!T[FINAL_NORM]) throw new Error(`bitgpu: manifest is missing the final norm tensor '${FINAL_NORM}'`);
|
|
298
|
-
if (
|
|
299
|
-
|
|
333
|
+
if (manifest.version !== void 0 && manifest.version !== 1 && manifest.version !== 2) throw new Error(`bitgpu: unsupported manifest version ${manifest.version} (this engine reads versions 1 and 2)`);
|
|
334
|
+
if (!T.cos_cache !== !T.sin_cache) throw new Error("bitgpu: manifest has only one of cos_cache/sin_cache");
|
|
335
|
+
if (!T.cos_cache && !(A.rope && A.rope.rope_theta)) throw new Error("bitgpu: manifest has neither baked cos_cache/sin_cache RoPE tensors nor arch.rope parameters");
|
|
336
|
+
for (const [name, t] of Object.entries(T)) {
|
|
337
|
+
if (t.block !== void 0 && t.block !== 128) throw new Error(`bitgpu: tensor ${name} has block ${t.block} (kernels assume 128)`);
|
|
338
|
+
if (t.container === void 0) continue;
|
|
339
|
+
if (t.container !== "q1_0") throw new Error(`bitgpu: tensor ${name} has unknown container '${t.container}'`);
|
|
340
|
+
const N = t.N ?? t.rows;
|
|
341
|
+
const K = t.K ?? t.cols;
|
|
342
|
+
if (!N || !K || K % 128 !== 0) throw new Error(`bitgpu: tensor ${name}: q1_0 container needs N/K (or rows/cols) with K a multiple of 128`);
|
|
343
|
+
const r = t.weight;
|
|
344
|
+
if (!r || r.src !== "data" || r.len !== N * (K / 128) * 18) throw new Error(`bitgpu: tensor ${name}: q1_0 region is ${r?.len} bytes in '${r?.src}', expected ${N * (K / 128) * 18} in the data file`);
|
|
345
|
+
t.q1_0 = r;
|
|
346
|
+
t.weight = {
|
|
347
|
+
dtype: "UINT8",
|
|
348
|
+
src: r.src,
|
|
349
|
+
off: r.off,
|
|
350
|
+
len: N * (K / 8)
|
|
351
|
+
};
|
|
352
|
+
t.scales = {
|
|
353
|
+
dtype: "FLOAT",
|
|
354
|
+
src: r.src,
|
|
355
|
+
off: r.off,
|
|
356
|
+
len: N * (K / 128) * 4
|
|
357
|
+
};
|
|
358
|
+
t.zp = void 0;
|
|
359
|
+
}
|
|
300
360
|
const readRef = (ref) => {
|
|
301
361
|
if (ref.src !== "aux") throw new Error("bitgpu: internal - readRef reads aux-file refs; data-file tensors stream through routes");
|
|
302
362
|
if (ref.off + ref.len > aux.byteLength) throw new Error(`bitgpu: tensor range ${ref.off}+${ref.len} exceeds the aux file (${aux.byteLength} bytes); the download is truncated or the manifest does not match it`);
|
|
@@ -360,12 +420,13 @@ async function createEngineInner(options, holder) {
|
|
|
360
420
|
track(fusedLen(g, "scales"));
|
|
361
421
|
}
|
|
362
422
|
}
|
|
423
|
+
const embZpLen = T.embed_tokens.zp?.len ?? T.embed_tokens.rows * (T.embed_tokens.cols / 128) / 2;
|
|
363
424
|
for (const r of [
|
|
364
425
|
T.embed_tokens.weight,
|
|
365
426
|
T.embed_tokens.scales,
|
|
366
|
-
T.embed_tokens.zp,
|
|
367
427
|
manifest.luts.tgt4
|
|
368
428
|
]) track(r.len);
|
|
429
|
+
track(embZpLen);
|
|
369
430
|
if (kv8 && A.head_dim % 32 !== 0) throw new Error(`bitgpu: kvCache 'q8' needs head_dim divisible by 32 (got ${A.head_dim}); use 'f16' or 'f32' for this model`);
|
|
370
431
|
const kvLayerBytes = maxSeqLen * A.kv_heads * A.head_dim * KVB;
|
|
371
432
|
needBind = Math.max(needBind, kvLayerBytes, PREFILL_SEG * Math.max(A.heads * A.head_dim, A.intermediate) * 4, 32 * A.vocab * 4);
|
|
@@ -596,24 +657,65 @@ async function createEngineInner(options, holder) {
|
|
|
596
657
|
const s = gpuSink(buf, base);
|
|
597
658
|
wire(ref, s.push, s.finish);
|
|
598
659
|
};
|
|
660
|
+
const xfSign = (push) => (b) => {
|
|
661
|
+
const o = new Uint8Array(b.length);
|
|
662
|
+
for (let i = 0; i < b.length; i++) o[i] = signTable[b[i]];
|
|
663
|
+
push(o);
|
|
664
|
+
};
|
|
665
|
+
const xfQ2 = (push) => (b) => {
|
|
666
|
+
const o = new Uint8Array(b.length * 2);
|
|
667
|
+
for (let i = 0; i < b.length; i++) {
|
|
668
|
+
o[2 * i] = tgt2[2 * b[i]];
|
|
669
|
+
o[2 * i + 1] = tgt2[2 * b[i] + 1];
|
|
670
|
+
}
|
|
671
|
+
push(o);
|
|
672
|
+
};
|
|
599
673
|
const wireSign = (ref, buf, base = 0) => {
|
|
600
674
|
const s = gpuSink(buf, base);
|
|
601
|
-
wire(ref, (
|
|
602
|
-
const o = new Uint8Array(b.length);
|
|
603
|
-
for (let i = 0; i < b.length; i++) o[i] = signTable[b[i]];
|
|
604
|
-
s.push(o);
|
|
605
|
-
}, s.finish);
|
|
675
|
+
wire(ref, xfSign(s.push), s.finish);
|
|
606
676
|
};
|
|
607
677
|
const wireQ2 = (ref, buf) => {
|
|
608
678
|
const s = gpuSink(buf, 0);
|
|
609
|
-
wire(ref, (
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
679
|
+
wire(ref, xfQ2(s.push), s.finish);
|
|
680
|
+
};
|
|
681
|
+
const f16f32 = (h) => {
|
|
682
|
+
const s = h & 32768 ? -1 : 1;
|
|
683
|
+
const e = h >> 10 & 31;
|
|
684
|
+
const m = h & 1023;
|
|
685
|
+
if (e === 0) return s * m * 2 ** -24;
|
|
686
|
+
if (e === 31) return m ? NaN : s * Infinity;
|
|
687
|
+
return s * (1024 + m) * 2 ** (e - 25);
|
|
688
|
+
};
|
|
689
|
+
const wireQ10 = (region, signPush, signFinish, scalePush, scaleFinish) => {
|
|
690
|
+
let phase = 0;
|
|
691
|
+
let scaleLo = 0;
|
|
692
|
+
const push = (b) => {
|
|
693
|
+
const signs = new Uint8Array(b.length);
|
|
694
|
+
const scales = new Float32Array((b.length >> 4) + 2);
|
|
695
|
+
let sn = 0;
|
|
696
|
+
let cn = 0;
|
|
697
|
+
for (let i = 0; i < b.length; i++) if (phase === 0) {
|
|
698
|
+
scaleLo = b[i];
|
|
699
|
+
phase = 1;
|
|
700
|
+
} else if (phase === 1) {
|
|
701
|
+
scales[cn++] = f16f32(scaleLo | b[i] << 8);
|
|
702
|
+
phase = 2;
|
|
703
|
+
} else {
|
|
704
|
+
signs[sn++] = b[i];
|
|
705
|
+
phase = phase === 17 ? 0 : phase + 1;
|
|
706
|
+
}
|
|
707
|
+
if (sn) signPush(signs.subarray(0, sn));
|
|
708
|
+
if (cn) scalePush(new Uint8Array(scales.buffer, 0, cn * 4));
|
|
709
|
+
};
|
|
710
|
+
routes.push({
|
|
711
|
+
off: region.off,
|
|
712
|
+
len: region.len,
|
|
713
|
+
push,
|
|
714
|
+
finish: () => {
|
|
715
|
+
signFinish();
|
|
716
|
+
scaleFinish();
|
|
614
717
|
}
|
|
615
|
-
|
|
616
|
-
}, s.finish);
|
|
718
|
+
});
|
|
617
719
|
};
|
|
618
720
|
const wireCpu = (ref) => {
|
|
619
721
|
const dst = new Uint8Array(ref.len);
|
|
@@ -630,9 +732,15 @@ async function createEngineInner(options, holder) {
|
|
|
630
732
|
const zpChecks = [];
|
|
631
733
|
for (const [name, t] of Object.entries(T)) if (t.kind === "q2") {
|
|
632
734
|
const codes = gbuf(t.weight.len * 2);
|
|
633
|
-
wireQ2(t.weight, codes);
|
|
634
735
|
const scales = gbuf(t.scales.len);
|
|
635
|
-
|
|
736
|
+
if (t.q1_0) {
|
|
737
|
+
const cs = gpuSink(codes, 0);
|
|
738
|
+
const ss = gpuSink(scales, 0);
|
|
739
|
+
wireQ10(t.q1_0, xfQ2(cs.push), cs.finish, ss.push, ss.finish);
|
|
740
|
+
} else {
|
|
741
|
+
wireQ2(t.weight, codes);
|
|
742
|
+
wireRaw(t.scales, scales);
|
|
743
|
+
}
|
|
636
744
|
const w = {
|
|
637
745
|
N: t.N,
|
|
638
746
|
K: t.K,
|
|
@@ -663,9 +771,15 @@ async function createEngineInner(options, holder) {
|
|
|
663
771
|
let so = 0;
|
|
664
772
|
let co = 0;
|
|
665
773
|
for (const p of parts) {
|
|
666
|
-
|
|
774
|
+
if (p.q1_0) {
|
|
775
|
+
const ws = gpuSink(sign, so);
|
|
776
|
+
const ss = gpuSink(scales, co);
|
|
777
|
+
wireQ10(p.q1_0, xfSign(ws.push), ws.finish, ss.push, ss.finish);
|
|
778
|
+
} else {
|
|
779
|
+
wireSign(p.weight, sign, so);
|
|
780
|
+
wireRaw(p.scales, scales, co);
|
|
781
|
+
}
|
|
667
782
|
so += p.weight.len;
|
|
668
|
-
wireRaw(p.scales, scales, co);
|
|
669
783
|
co += p.scales.len;
|
|
670
784
|
}
|
|
671
785
|
return {
|
|
@@ -711,16 +825,37 @@ async function createEngineInner(options, holder) {
|
|
|
711
825
|
wireRaw(ref, buf);
|
|
712
826
|
return buf;
|
|
713
827
|
};
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
828
|
+
let embWqG;
|
|
829
|
+
let embScalesG;
|
|
830
|
+
let embZpG;
|
|
831
|
+
if (T.embed_tokens.q1_0) {
|
|
832
|
+
embWqG = gbuf(T.embed_tokens.weight.len);
|
|
833
|
+
embScalesG = gbuf(T.embed_tokens.scales.len);
|
|
834
|
+
const ws = gpuSink(embWqG, 0);
|
|
835
|
+
const ss = gpuSink(embScalesG, 0);
|
|
836
|
+
wireQ10(T.embed_tokens.q1_0, ws.push, ws.finish, ss.push, ss.finish);
|
|
837
|
+
embZpG = gbuf(embZpLen);
|
|
838
|
+
device.queue.writeBuffer(embZpG, 0, new Uint8Array(embZpLen + 3 & -4).fill(136));
|
|
839
|
+
} else {
|
|
840
|
+
embWqG = wireGpu(T.embed_tokens.weight);
|
|
841
|
+
embScalesG = wireGpu(T.embed_tokens.scales);
|
|
842
|
+
embZpG = wireGpu(T.embed_tokens.zp);
|
|
843
|
+
}
|
|
717
844
|
const tgt4G = wireGpu(manifest.luts.tgt4);
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
845
|
+
let cosCache;
|
|
846
|
+
let sinCache;
|
|
847
|
+
if (T.cos_cache) {
|
|
848
|
+
const cosBytes = wireCpu(T.cos_cache);
|
|
849
|
+
const sinBytes = wireCpu(T.sin_cache);
|
|
850
|
+
cosCache = new Float32Array(cosBytes.buffer);
|
|
851
|
+
sinCache = new Float32Array(sinBytes.buffer);
|
|
852
|
+
const ropePositions = cosCache.length / (A.head_dim / 2);
|
|
853
|
+
if (maxSeqLen > ropePositions) throw new Error(`bitgpu: maxSeqLen ${maxSeqLen} exceeds the model's baked RoPE cache (${ropePositions} positions); lower maxSeqLen or re-export with a longer cache`);
|
|
854
|
+
} else {
|
|
855
|
+
const cap = A.max_positions ?? 40960;
|
|
856
|
+
if (maxSeqLen > cap) throw new Error(`bitgpu: maxSeqLen ${maxSeqLen} exceeds the model's max_positions (${cap})`);
|
|
857
|
+
[cosCache, sinCache] = synthRope(A, maxSeqLen);
|
|
858
|
+
}
|
|
724
859
|
routes.sort((a, b) => a.off - b.off || a.len - b.len);
|
|
725
860
|
const merged = [];
|
|
726
861
|
for (const r of routes) {
|