bitgpu 0.11.0 → 0.12.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 +38 -29
- package/dist/chat.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +160 -28
- package/dist/index.js.map +1 -1
- package/dist/{types-DJ61q1gX.d.ts → types-Bl0FS_nX.d.ts} +4 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,16 +34,16 @@ weights stream straight from the Hugging Face Hub. This runs as-is:
|
|
|
34
34
|
import { createEngine } from 'bitgpu'
|
|
35
35
|
import { createChat } from 'bitgpu/chat'
|
|
36
36
|
|
|
37
|
-
const REPO = 'https://cdn.jsdelivr.net/gh/stfurkan/bitgpu@v0.
|
|
38
|
-
const
|
|
37
|
+
const REPO = 'https://cdn.jsdelivr.net/gh/stfurkan/bitgpu@v0.12.0/models/bonsai-1.7b-gguf'
|
|
38
|
+
const TOK = 'https://huggingface.co/onnx-community/Bonsai-1.7B-ONNX/resolve/main'
|
|
39
39
|
const engine = await createEngine({
|
|
40
40
|
manifestUrl: `${REPO}/manifest.json`,
|
|
41
|
-
auxUrl: `${REPO}/
|
|
42
|
-
dataUrl:
|
|
41
|
+
auxUrl: `${REPO}/Bonsai-1.7B-Q1_0.aux.bin`,
|
|
42
|
+
dataUrl: 'https://huggingface.co/prism-ml/Bonsai-1.7B-gguf/resolve/main/Bonsai-1.7B-Q1_0.gguf',
|
|
43
43
|
})
|
|
44
44
|
const chat = await createChat(engine, {
|
|
45
|
-
tokenizerJsonUrl: `${
|
|
46
|
-
tokenizerConfigUrl: `${
|
|
45
|
+
tokenizerJsonUrl: `${TOK}/tokenizer.json`, // tokenizer from the ONNX repo (same base model)
|
|
46
|
+
tokenizerConfigUrl: `${TOK}/tokenizer_config.json`,
|
|
47
47
|
})
|
|
48
48
|
await chat.send([{ role: 'user', content: 'Hi!' }], { onText: (t) => process.stdout.write(t) })
|
|
49
49
|
```
|
|
@@ -318,14 +318,15 @@ GPU gate proves the reuse paths bit-exact on real hardware. Prefer your own toke
|
|
|
318
318
|
|
|
319
319
|
## Bring your own model
|
|
320
320
|
|
|
321
|
-
bitgpu loads its own small format instead of parsing ONNX at runtime: a
|
|
322
|
-
architecture contract + every tensor mapped to a byte range) and a
|
|
323
|
-
produced ONCE, offline, from a standard export - while the big weights file is
|
|
324
|
-
byte-for-byte unchanged, so it can keep streaming from wherever it already lives (e.g.
|
|
325
|
-
Hugging Face Hub). Same one-time-conversion model as
|
|
321
|
+
bitgpu loads its own small format instead of parsing ONNX or GGUF at runtime: a
|
|
322
|
+
`manifest.json` (the architecture contract + every tensor mapped to a byte range) and a small
|
|
323
|
+
aux file, both produced ONCE, offline, from a standard export - while the big weights file is
|
|
324
|
+
used byte-for-byte unchanged, so it can keep streaming from wherever it already lives (e.g.
|
|
325
|
+
the Hugging Face Hub). Same one-time-conversion model as llama.cpp or MLX. Two converters:
|
|
326
326
|
|
|
327
327
|
```sh
|
|
328
|
-
python tools/convert.py --
|
|
328
|
+
python tools/convert-gguf.py --gguf <a 1-bit Q1_0 .gguf> # the .gguf itself stays the data file
|
|
329
|
+
python tools/convert-onnx.py --model <dir with config.json + the q1 .onnx + its data file>
|
|
329
330
|
```
|
|
330
331
|
|
|
331
332
|
Host the two small files anywhere (they're static), point `createEngine` at them, done:
|
|
@@ -334,18 +335,24 @@ Host the two small files anywhere (they're static), point `createEngine` at them
|
|
|
334
335
|
createEngine({
|
|
335
336
|
manifestUrl: 'https://your-site.example/model/manifest.json',
|
|
336
337
|
auxUrl: 'https://your-site.example/model/model_q1.aux.bin',
|
|
337
|
-
dataUrl: 'https://huggingface.co/<repo>/resolve/main
|
|
338
|
+
dataUrl: 'https://huggingface.co/<repo>/resolve/main/<model>.gguf', // or .../onnx/model_q1.onnx_data
|
|
338
339
|
})
|
|
339
340
|
```
|
|
340
341
|
|
|
341
|
-
Compatibility envelope: Qwen3-family models quantized with the
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
342
|
+
Compatibility envelope: Qwen3-family models quantized with the 1-bit recipe (silu/SwiGLU,
|
|
343
|
+
head_dim <= 128, 128-wide scale blocks, tied or untied lm_head), in either container: GGUFs
|
|
344
|
+
with PrismML's Q1_0 tensor type (the primary path), or ONNX exports with the onnx-community
|
|
345
|
+
"q1" recipe - the engine validates the manifest loudly at load. The two containers carry
|
|
346
|
+
bit-identical weights for the Bonsai releases (verified sign-bit-for-sign-bit on 8B), so pick
|
|
347
|
+
by hosting preference.
|
|
348
|
+
Reference exports: [prism-ml/Bonsai-1.7B-gguf](https://huggingface.co/prism-ml/Bonsai-1.7B-gguf) /
|
|
349
|
+
[4B](https://huggingface.co/prism-ml/Bonsai-4B-gguf) /
|
|
350
|
+
[8B](https://huggingface.co/prism-ml/Bonsai-8B-gguf) (`Bonsai-*-Q1_0.gguf`), and
|
|
351
|
+
[onnx-community/Bonsai-1.7B-ONNX](https://huggingface.co/onnx-community/Bonsai-1.7B-ONNX) /
|
|
352
|
+
[4B](https://huggingface.co/onnx-community/Bonsai-4B-ONNX) /
|
|
353
|
+
[8B](https://huggingface.co/onnx-community/Bonsai-8B-ONNX) (`onnx/model_q1.onnx` + data file). Format spec:
|
|
354
|
+
[docs/FORMAT.md](docs/FORMAT.md); the full pipeline including regenerating the verification
|
|
355
|
+
fixtures for a new model: [tools/README.md](tools/README.md).
|
|
349
356
|
|
|
350
357
|
## API
|
|
351
358
|
|
|
@@ -411,18 +418,19 @@ npm run check:publish # publint + are-the-types-wrong
|
|
|
411
418
|
### GPU verification gate
|
|
412
419
|
|
|
413
420
|
`examples/verify.html` re-runs the full bit-exactness + throughput suite (forward cosines vs the
|
|
414
|
-
committed reference fixtures in `test-fixtures/forward
|
|
421
|
+
committed reference fixtures in `test-fixtures/forward-<tag>/`, known-good greedy ids, sampler kernel
|
|
415
422
|
parity, determinism, KV reuse/growth, prompt-lookup identity, KV snapshot save/restore round
|
|
416
423
|
trips - including into a fresh engine - the f16/q8 KV-mode sections: within-mode exactness
|
|
417
424
|
plus greedy agreement vs f32 out to a 1500-token prompt, the rolling-window section:
|
|
418
425
|
pre-eviction bit-exactness, 600 tokens through a 192-token window, determinism across
|
|
419
426
|
evictions) against the **built package** and prints `PACKAGE OK` or `REGRESSION`.
|
|
420
427
|
|
|
421
|
-
It needs model weights, which are not committed. Point `examples/model` at a directory
|
|
422
|
-
model's `manifest.json` + data/aux files (the reference target is Bonsai-1.7B,
|
|
428
|
+
It needs model weights, which are not committed. Point `examples/model-1.7b` at a directory
|
|
429
|
+
holding the model's `manifest.json` + data/aux files (the reference target is Bonsai-1.7B,
|
|
430
|
+
~290 MB):
|
|
423
431
|
|
|
424
432
|
```sh
|
|
425
|
-
ln -s /path/to/bonsai-model examples/model # or copy the files in
|
|
433
|
+
ln -s /path/to/bonsai-model examples/model-1.7b # or copy the files in
|
|
426
434
|
npm run build
|
|
427
435
|
npm run verify:headless # serves the repo itself + drives system Chrome headlessly
|
|
428
436
|
FAST=1 npm run verify:headless # dev iteration: baseline model, core sections only (~3 min; NOT a release gate)
|
|
@@ -438,10 +446,11 @@ known-good ids.
|
|
|
438
446
|
|
|
439
447
|
The gate is model-parametric: `verify.html?model=<tag>` loads `examples/model-<tag>` against
|
|
440
448
|
`test-fixtures/forward-<tag>`, and the headless driver automatically runs every staged variant.
|
|
441
|
-
|
|
442
|
-
`forward-
|
|
443
|
-
|
|
444
|
-
|
|
449
|
+
Committed fixture sets: `forward-1.7b` (1.7B, hidden 2048), `forward-4b` (4B, hidden 2560),
|
|
450
|
+
`forward-8b` (8B, hidden 4096, untied lm_head, raised device limits), plus the GGUF-container
|
|
451
|
+
twins `forward-{1.7b,4b,8b}-gguf` (q1_0 de-interleave, tied and untied, three synthesized-rope
|
|
452
|
+
configurations) - so engine changes are checked against three geometries and both containers; stage
|
|
453
|
+
the extra weights with `ln -s /path/to/bonsai-<size> examples/model-<size>`. The chat-layer checks need
|
|
445
454
|
`tokenizer.json` + `tokenizer_config.json` in the staged model dir and skip loudly when absent.
|
|
446
455
|
To add fixtures for another model, run `tools/golden.py` then
|
|
447
456
|
`tools/reference.py --dump test-fixtures/forward-<tag>` on the converted work dir and record the
|
package/dist/chat.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as KvSnapshot, n as Engine, o as GenerateOptions, u as TokenLogprobs } from "./types-
|
|
1
|
+
import { c as KvSnapshot, n as Engine, o as GenerateOptions, u as TokenLogprobs } from "./types-Bl0FS_nX.js";
|
|
2
2
|
|
|
3
3
|
//#region src/chat/tokenizer.d.ts
|
|
4
4
|
interface ChatMessage {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as ForwardResult, c as KvSnapshot, i as EngineOptions, l as LoadProgress, n as Engine, o as GenerateOptions, r as EngineCapabilities, s as GenerateResult, t as DeviceLostInfo, u as TokenLogprobs } from "./types-
|
|
1
|
+
import { a as ForwardResult, c as KvSnapshot, i as EngineOptions, l as LoadProgress, n as Engine, o as GenerateOptions, r as EngineCapabilities, s as GenerateResult, t as DeviceLostInfo, u as TokenLogprobs } from "./types-Bl0FS_nX.js";
|
|
2
2
|
|
|
3
3
|
//#region src/engine.d.ts
|
|
4
4
|
/** Load a 1-bit model and return an {@link Engine}. Pass a model URL string for defaults. */
|
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 = {};
|
|
@@ -295,8 +327,33 @@ async function createEngineInner(options, holder) {
|
|
|
295
327
|
if (A.head_dim > 128) throw new Error(`bitgpu: unsupported head_dim ${A.head_dim} (kernels assume <= 128)`);
|
|
296
328
|
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
329
|
if (!T[FINAL_NORM]) throw new Error(`bitgpu: manifest is missing the final norm tensor '${FINAL_NORM}'`);
|
|
298
|
-
if (
|
|
299
|
-
|
|
330
|
+
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)`);
|
|
331
|
+
if (!T.cos_cache !== !T.sin_cache) throw new Error("bitgpu: manifest has only one of cos_cache/sin_cache");
|
|
332
|
+
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");
|
|
333
|
+
for (const [name, t] of Object.entries(T)) {
|
|
334
|
+
if (t.block !== void 0 && t.block !== 128) throw new Error(`bitgpu: tensor ${name} has block ${t.block} (kernels assume 128)`);
|
|
335
|
+
if (t.container === void 0) continue;
|
|
336
|
+
if (t.container !== "q1_0") throw new Error(`bitgpu: tensor ${name} has unknown container '${t.container}'`);
|
|
337
|
+
const N = t.N ?? t.rows;
|
|
338
|
+
const K = t.K ?? t.cols;
|
|
339
|
+
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`);
|
|
340
|
+
const r = t.weight;
|
|
341
|
+
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`);
|
|
342
|
+
t.q1_0 = r;
|
|
343
|
+
t.weight = {
|
|
344
|
+
dtype: "UINT8",
|
|
345
|
+
src: r.src,
|
|
346
|
+
off: r.off,
|
|
347
|
+
len: N * (K / 8)
|
|
348
|
+
};
|
|
349
|
+
t.scales = {
|
|
350
|
+
dtype: "FLOAT",
|
|
351
|
+
src: r.src,
|
|
352
|
+
off: r.off,
|
|
353
|
+
len: N * (K / 128) * 4
|
|
354
|
+
};
|
|
355
|
+
t.zp = void 0;
|
|
356
|
+
}
|
|
300
357
|
const readRef = (ref) => {
|
|
301
358
|
if (ref.src !== "aux") throw new Error("bitgpu: internal - readRef reads aux-file refs; data-file tensors stream through routes");
|
|
302
359
|
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 +417,13 @@ async function createEngineInner(options, holder) {
|
|
|
360
417
|
track(fusedLen(g, "scales"));
|
|
361
418
|
}
|
|
362
419
|
}
|
|
420
|
+
const embZpLen = T.embed_tokens.zp?.len ?? T.embed_tokens.rows * (T.embed_tokens.cols / 128) / 2;
|
|
363
421
|
for (const r of [
|
|
364
422
|
T.embed_tokens.weight,
|
|
365
423
|
T.embed_tokens.scales,
|
|
366
|
-
T.embed_tokens.zp,
|
|
367
424
|
manifest.luts.tgt4
|
|
368
425
|
]) track(r.len);
|
|
426
|
+
track(embZpLen);
|
|
369
427
|
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
428
|
const kvLayerBytes = maxSeqLen * A.kv_heads * A.head_dim * KVB;
|
|
371
429
|
needBind = Math.max(needBind, kvLayerBytes, PREFILL_SEG * Math.max(A.heads * A.head_dim, A.intermediate) * 4, 32 * A.vocab * 4);
|
|
@@ -596,24 +654,65 @@ async function createEngineInner(options, holder) {
|
|
|
596
654
|
const s = gpuSink(buf, base);
|
|
597
655
|
wire(ref, s.push, s.finish);
|
|
598
656
|
};
|
|
657
|
+
const xfSign = (push) => (b) => {
|
|
658
|
+
const o = new Uint8Array(b.length);
|
|
659
|
+
for (let i = 0; i < b.length; i++) o[i] = signTable[b[i]];
|
|
660
|
+
push(o);
|
|
661
|
+
};
|
|
662
|
+
const xfQ2 = (push) => (b) => {
|
|
663
|
+
const o = new Uint8Array(b.length * 2);
|
|
664
|
+
for (let i = 0; i < b.length; i++) {
|
|
665
|
+
o[2 * i] = tgt2[2 * b[i]];
|
|
666
|
+
o[2 * i + 1] = tgt2[2 * b[i] + 1];
|
|
667
|
+
}
|
|
668
|
+
push(o);
|
|
669
|
+
};
|
|
599
670
|
const wireSign = (ref, buf, base = 0) => {
|
|
600
671
|
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);
|
|
672
|
+
wire(ref, xfSign(s.push), s.finish);
|
|
606
673
|
};
|
|
607
674
|
const wireQ2 = (ref, buf) => {
|
|
608
675
|
const s = gpuSink(buf, 0);
|
|
609
|
-
wire(ref, (
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
676
|
+
wire(ref, xfQ2(s.push), s.finish);
|
|
677
|
+
};
|
|
678
|
+
const f16f32 = (h) => {
|
|
679
|
+
const s = h & 32768 ? -1 : 1;
|
|
680
|
+
const e = h >> 10 & 31;
|
|
681
|
+
const m = h & 1023;
|
|
682
|
+
if (e === 0) return s * m * 2 ** -24;
|
|
683
|
+
if (e === 31) return m ? NaN : s * Infinity;
|
|
684
|
+
return s * (1024 + m) * 2 ** (e - 25);
|
|
685
|
+
};
|
|
686
|
+
const wireQ10 = (region, signPush, signFinish, scalePush, scaleFinish) => {
|
|
687
|
+
let phase = 0;
|
|
688
|
+
let scaleLo = 0;
|
|
689
|
+
const push = (b) => {
|
|
690
|
+
const signs = new Uint8Array(b.length);
|
|
691
|
+
const scales = new Float32Array((b.length >> 4) + 2);
|
|
692
|
+
let sn = 0;
|
|
693
|
+
let cn = 0;
|
|
694
|
+
for (let i = 0; i < b.length; i++) if (phase === 0) {
|
|
695
|
+
scaleLo = b[i];
|
|
696
|
+
phase = 1;
|
|
697
|
+
} else if (phase === 1) {
|
|
698
|
+
scales[cn++] = f16f32(scaleLo | b[i] << 8);
|
|
699
|
+
phase = 2;
|
|
700
|
+
} else {
|
|
701
|
+
signs[sn++] = b[i];
|
|
702
|
+
phase = phase === 17 ? 0 : phase + 1;
|
|
703
|
+
}
|
|
704
|
+
if (sn) signPush(signs.subarray(0, sn));
|
|
705
|
+
if (cn) scalePush(new Uint8Array(scales.buffer, 0, cn * 4));
|
|
706
|
+
};
|
|
707
|
+
routes.push({
|
|
708
|
+
off: region.off,
|
|
709
|
+
len: region.len,
|
|
710
|
+
push,
|
|
711
|
+
finish: () => {
|
|
712
|
+
signFinish();
|
|
713
|
+
scaleFinish();
|
|
614
714
|
}
|
|
615
|
-
|
|
616
|
-
}, s.finish);
|
|
715
|
+
});
|
|
617
716
|
};
|
|
618
717
|
const wireCpu = (ref) => {
|
|
619
718
|
const dst = new Uint8Array(ref.len);
|
|
@@ -630,9 +729,15 @@ async function createEngineInner(options, holder) {
|
|
|
630
729
|
const zpChecks = [];
|
|
631
730
|
for (const [name, t] of Object.entries(T)) if (t.kind === "q2") {
|
|
632
731
|
const codes = gbuf(t.weight.len * 2);
|
|
633
|
-
wireQ2(t.weight, codes);
|
|
634
732
|
const scales = gbuf(t.scales.len);
|
|
635
|
-
|
|
733
|
+
if (t.q1_0) {
|
|
734
|
+
const cs = gpuSink(codes, 0);
|
|
735
|
+
const ss = gpuSink(scales, 0);
|
|
736
|
+
wireQ10(t.q1_0, xfQ2(cs.push), cs.finish, ss.push, ss.finish);
|
|
737
|
+
} else {
|
|
738
|
+
wireQ2(t.weight, codes);
|
|
739
|
+
wireRaw(t.scales, scales);
|
|
740
|
+
}
|
|
636
741
|
const w = {
|
|
637
742
|
N: t.N,
|
|
638
743
|
K: t.K,
|
|
@@ -663,9 +768,15 @@ async function createEngineInner(options, holder) {
|
|
|
663
768
|
let so = 0;
|
|
664
769
|
let co = 0;
|
|
665
770
|
for (const p of parts) {
|
|
666
|
-
|
|
771
|
+
if (p.q1_0) {
|
|
772
|
+
const ws = gpuSink(sign, so);
|
|
773
|
+
const ss = gpuSink(scales, co);
|
|
774
|
+
wireQ10(p.q1_0, xfSign(ws.push), ws.finish, ss.push, ss.finish);
|
|
775
|
+
} else {
|
|
776
|
+
wireSign(p.weight, sign, so);
|
|
777
|
+
wireRaw(p.scales, scales, co);
|
|
778
|
+
}
|
|
667
779
|
so += p.weight.len;
|
|
668
|
-
wireRaw(p.scales, scales, co);
|
|
669
780
|
co += p.scales.len;
|
|
670
781
|
}
|
|
671
782
|
return {
|
|
@@ -711,16 +822,37 @@ async function createEngineInner(options, holder) {
|
|
|
711
822
|
wireRaw(ref, buf);
|
|
712
823
|
return buf;
|
|
713
824
|
};
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
825
|
+
let embWqG;
|
|
826
|
+
let embScalesG;
|
|
827
|
+
let embZpG;
|
|
828
|
+
if (T.embed_tokens.q1_0) {
|
|
829
|
+
embWqG = gbuf(T.embed_tokens.weight.len);
|
|
830
|
+
embScalesG = gbuf(T.embed_tokens.scales.len);
|
|
831
|
+
const ws = gpuSink(embWqG, 0);
|
|
832
|
+
const ss = gpuSink(embScalesG, 0);
|
|
833
|
+
wireQ10(T.embed_tokens.q1_0, ws.push, ws.finish, ss.push, ss.finish);
|
|
834
|
+
embZpG = gbuf(embZpLen);
|
|
835
|
+
device.queue.writeBuffer(embZpG, 0, new Uint8Array(embZpLen + 3 & -4).fill(136));
|
|
836
|
+
} else {
|
|
837
|
+
embWqG = wireGpu(T.embed_tokens.weight);
|
|
838
|
+
embScalesG = wireGpu(T.embed_tokens.scales);
|
|
839
|
+
embZpG = wireGpu(T.embed_tokens.zp);
|
|
840
|
+
}
|
|
717
841
|
const tgt4G = wireGpu(manifest.luts.tgt4);
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
842
|
+
let cosCache;
|
|
843
|
+
let sinCache;
|
|
844
|
+
if (T.cos_cache) {
|
|
845
|
+
const cosBytes = wireCpu(T.cos_cache);
|
|
846
|
+
const sinBytes = wireCpu(T.sin_cache);
|
|
847
|
+
cosCache = new Float32Array(cosBytes.buffer);
|
|
848
|
+
sinCache = new Float32Array(sinBytes.buffer);
|
|
849
|
+
const ropePositions = cosCache.length / (A.head_dim / 2);
|
|
850
|
+
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`);
|
|
851
|
+
} else {
|
|
852
|
+
const cap = A.max_positions ?? 40960;
|
|
853
|
+
if (maxSeqLen > cap) throw new Error(`bitgpu: maxSeqLen ${maxSeqLen} exceeds the model's max_positions (${cap})`);
|
|
854
|
+
[cosCache, sinCache] = synthRope(A, maxSeqLen);
|
|
855
|
+
}
|
|
724
856
|
routes.sort((a, b) => a.off - b.off || a.len - b.len);
|
|
725
857
|
const merged = [];
|
|
726
858
|
for (const r of routes) {
|