bitgpu 0.19.1 → 0.19.2
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 +18 -5
- package/dist/chat.d.ts +21 -34
- package/dist/chat.js +1268 -418
- package/dist/chat.js.map +1 -1
- package/dist/gguf.d.ts +6 -11
- package/dist/gguf.js +1 -2
- package/dist/index.d.ts +5 -11
- package/dist/index.js +21 -26
- package/dist/index.js.map +1 -1
- package/dist/{types-CBW7T_3s.d.ts → types-_ie0s0iY.d.ts} +1 -4
- package/package.json +12 -12
package/dist/gguf.d.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import { u as Manifest } from "./types-
|
|
2
|
-
|
|
3
|
-
//#region src/gguf.d.ts
|
|
4
|
-
interface GgufModel {
|
|
1
|
+
import { u as Manifest } from "./types-_ie0s0iY.js";
|
|
2
|
+
export interface GgufModel {
|
|
5
3
|
manifest: Manifest;
|
|
6
4
|
aux: Uint8Array;
|
|
7
5
|
/** The GGUF URL itself - the weights stream from it byte-for-byte unchanged. */
|
|
8
6
|
dataUrl: string;
|
|
9
7
|
}
|
|
10
|
-
interface FromGgufOptions {
|
|
8
|
+
export interface FromGgufOptions {
|
|
11
9
|
/** Fetch `len` bytes at `off`. Override for caching or non-HTTP sources. Default: `fetch`
|
|
12
10
|
* with a `Range` header (and a stream-and-cancel fallback for servers that ignore ranges). */
|
|
13
11
|
fetchRange?: (url: string, off: number, len: number) => Promise<ArrayBuffer>;
|
|
@@ -17,17 +15,14 @@ interface FromGgufOptions {
|
|
|
17
15
|
/** The two expansion tables the kernels use, generated from their defining property:
|
|
18
16
|
* LSB-first sign bits -> per-weight codes around the recipe midpoints (2-bit: {1,3},
|
|
19
17
|
* 4-bit: {7,9}). Byte-identical to the tables the ONNX exports carry. */
|
|
20
|
-
declare function ggufLuts(): Uint8Array;
|
|
18
|
+
export declare function ggufLuts(): Uint8Array;
|
|
21
19
|
/** Parse a GGUF header already in memory (e.g. from a File). Grows nothing - the buffer must
|
|
22
20
|
* contain the full header (weights past it are not needed). */
|
|
23
|
-
declare function fromGgufBytes(buf: ArrayBuffer, dataFile: string): {
|
|
21
|
+
export declare function fromGgufBytes(buf: ArrayBuffer, dataFile: string): {
|
|
24
22
|
manifest: Manifest;
|
|
25
23
|
aux: Uint8Array;
|
|
26
24
|
};
|
|
27
25
|
/** Fetch and parse a GGUF header; returns `{ manifest, aux, dataUrl }` for createEngine.
|
|
28
26
|
* Only the header is transferred, progressively: 1 MiB first, then EXTENSION ranges (2x
|
|
29
27
|
* growth) appended to the kept prefix - total transfer stays under 2x the header size. */
|
|
30
|
-
declare function fromGguf(url: string, options?: FromGgufOptions): Promise<GgufModel>;
|
|
31
|
-
//#endregion
|
|
32
|
-
export { FromGgufOptions, GgufModel, fromGguf, fromGgufBytes, ggufLuts };
|
|
33
|
-
//# sourceMappingURL=gguf.d.ts.map
|
|
28
|
+
export declare function fromGguf(url: string, options?: FromGgufOptions): Promise<GgufModel>;
|
package/dist/gguf.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/gguf.ts
|
|
2
2
|
const F32 = 0;
|
|
3
3
|
const Q1_0 = 41;
|
|
4
4
|
const BLK = 18;
|
|
@@ -562,7 +562,6 @@ async function fromGguf(url, options = {}) {
|
|
|
562
562
|
size = next;
|
|
563
563
|
}
|
|
564
564
|
}
|
|
565
|
-
//#endregion
|
|
566
565
|
export { fromGguf, fromGgufBytes, ggufLuts };
|
|
567
566
|
|
|
568
567
|
//# sourceMappingURL=gguf.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,24 +1,18 @@
|
|
|
1
|
-
import { a as ForwardResult, c as KvSnapshot, d as ManifestArch, f as ManifestRef, i as EngineOptions, l as LoadProgress, m as TokenLogprobs, n as Engine, o as GenerateOptions, p as ManifestTensor, r as EngineCapabilities, s as GenerateResult, t as DeviceLostInfo, u as Manifest } from "./types-
|
|
2
|
-
|
|
3
|
-
//#region src/engine.d.ts
|
|
1
|
+
import { a as ForwardResult, c as KvSnapshot, d as ManifestArch, f as ManifestRef, i as EngineOptions, l as LoadProgress, m as TokenLogprobs, n as Engine, o as GenerateOptions, p as ManifestTensor, r as EngineCapabilities, s as GenerateResult, t as DeviceLostInfo, u as Manifest } from "./types-_ie0s0iY.js";
|
|
4
2
|
/** Load a 1-bit model and return an {@link Engine}. Pass a model URL string for defaults. */
|
|
5
|
-
declare function createEngine(options: EngineOptions | string): Promise<Engine>;
|
|
6
|
-
//#endregion
|
|
7
|
-
//#region src/errors.d.ts
|
|
3
|
+
export declare function createEngine(options: EngineOptions | string): Promise<Engine>;
|
|
8
4
|
/** Thrown when WebGPU is unavailable: no `navigator.gpu` (unsupported browser, or a
|
|
9
5
|
* non-secure context) or no adapter could be acquired. Catch this to render a
|
|
10
6
|
* "your browser doesn't support WebGPU yet" fallback instead of crashing. */
|
|
11
|
-
declare class WebGPUUnavailableError extends Error {
|
|
7
|
+
export declare class WebGPUUnavailableError extends Error {
|
|
12
8
|
readonly name = "WebGPUUnavailableError";
|
|
13
9
|
constructor(message: string);
|
|
14
10
|
}
|
|
15
11
|
/** Thrown when the GPU reports out-of-memory while the engine allocates weights or grows the
|
|
16
12
|
* KV cache. Without this check the allocation "succeeds" with invalid buffers and every later
|
|
17
13
|
* generate() returns garbage. Catch it to fall back (smaller maxSeqLen, or a no-LLM mode). */
|
|
18
|
-
declare class GpuOutOfMemoryError extends Error {
|
|
14
|
+
export declare class GpuOutOfMemoryError extends Error {
|
|
19
15
|
readonly name = "GpuOutOfMemoryError";
|
|
20
16
|
constructor(message: string);
|
|
21
17
|
}
|
|
22
|
-
|
|
23
|
-
export { type DeviceLostInfo, type Engine, type EngineCapabilities, type EngineOptions, type ForwardResult, type GenerateOptions, type GenerateResult, GpuOutOfMemoryError, type KvSnapshot, type LoadProgress, type Manifest, type ManifestArch, type ManifestRef, type ManifestTensor, type TokenLogprobs, WebGPUUnavailableError, createEngine };
|
|
24
|
-
//# sourceMappingURL=index.d.ts.map
|
|
18
|
+
export type { DeviceLostInfo, Engine, EngineCapabilities, EngineOptions, ForwardResult, GenerateOptions, GenerateResult, KvSnapshot, LoadProgress, Manifest, ManifestArch, ManifestRef, ManifestTensor, TokenLogprobs };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/shaders.generated.ts
|
|
2
2
|
const SHADERS = {
|
|
3
3
|
"add": "// Elementwise residual add: y[i] = a[i] + b[i].\nstruct Params { n: u32, _p0: u32, _p1: u32, _p2: u32 };\n@group(0) @binding(0) var<uniform> p: Params;\n@group(0) @binding(1) var<storage, read> a: array<f32>;\n@group(0) @binding(2) var<storage, read> b: array<f32>;\n@group(0) @binding(3) var<storage, read_write> y: array<f32>;\n\n@compute @workgroup_size(64)\nfn main(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {\n let i = (wid.y * nwg.x + wid.x) * 64u + lid.x;\n if (i >= p.n) { return; }\n y[i] = a[i] + b[i];\n}\n",
|
|
4
4
|
"argmax": "// GPU argmax over the logits, writing one token id into a GPU buffer so the token never leaves the\n// GPU (enables the deferred-sync decode loop). Single workgroup, WG threads strided-scan the N\n// logits tracking (maxVal, maxIdx), then a shared-mem tree reduction. Tie-break = LOWEST index, to\n// match the CPU argmax (strict > keeps the first max). No subgroup ops -> all devices.\noverride WG: u32 = 256u;\nstruct Params { N: u32, outIdx: u32, _0: u32, _1: u32 };\n\n@group(0) @binding(0) var<uniform> p: Params;\n@group(0) @binding(1) var<storage, read> logits: array<f32>;\n@group(0) @binding(2) var<storage, read_write> outTok: array<u32>; // outTok[p.outIdx] = argmax\n\nvar<workgroup> sval: array<f32, 256>;\nvar<workgroup> sidx: array<u32, 256>;\n\n@compute @workgroup_size(WG)\nfn main(@builtin(local_invocation_id) lid: vec3<u32>) {\n let tid = lid.x;\n var bv = -3.4e38;\n var bi = 0u;\n for (var i = tid; i < p.N; i = i + WG) {\n let v = logits[i];\n if (v > bv) { bv = v; bi = i; } // strict > keeps the lowest index within this thread's stride\n }\n sval[tid] = bv; sidx[tid] = bi;\n workgroupBarrier();\n for (var s = WG / 2u; s > 0u; s = s >> 1u) {\n if (tid < s) {\n let ov = sval[tid + s]; let oi = sidx[tid + s];\n if (ov > sval[tid] || (ov == sval[tid] && oi < sidx[tid])) { sval[tid] = ov; sidx[tid] = oi; }\n }\n workgroupBarrier();\n }\n if (tid == 0u) { outTok[p.outIdx] = sidx[0]; }\n}\n",
|
|
@@ -62,8 +62,7 @@ const SHADERS = {
|
|
|
62
62
|
"split_head": "// De-interleave a per-head doubled projection [S, H, 2*Dh] into [S, H, Dh], taking the half at\n// `off` (0 = query, Dh = gate). The Qwen3.5 gated-attention q_proj packs query and output-gate\n// interleaved per head; this pulls one out into a packed buffer.\nstruct Params { S: u32, H: u32, Dh: u32, off: u32 };\n@group(0) @binding(0) var<uniform> p: Params;\n@group(0) @binding(1) var<storage, read> src: array<f32>; // [S, H, 2*Dh]\n@group(0) @binding(2) var<storage, read_write> dst: array<f32>;// [S, H, Dh]\n\n@compute @workgroup_size(64)\nfn main(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {\n let i = (wid.y * nwg.x + wid.x) * 64u + lid.x;\n if (i >= p.S * p.H * p.Dh) { return; }\n let d = i % p.Dh;\n let sh = i / p.Dh; // s*H + h\n dst[i] = src[sh * (2u * p.Dh) + p.off + d];\n}\n",
|
|
63
63
|
"swiglu": "// SwiGLU gate: y[i] = silu(gate[i]) * up[i], silu(g) = g * sigmoid(g). One invocation per element.\nstruct Params { n: u32, _p0: u32, _p1: u32, _p2: u32 };\n@group(0) @binding(0) var<uniform> p: Params;\n@group(0) @binding(1) var<storage, read> gate: array<f32>;\n@group(0) @binding(2) var<storage, read> up: array<f32>;\n@group(0) @binding(3) var<storage, read_write> y: array<f32>;\n\n@compute @workgroup_size(64)\nfn main(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {\n let i = (wid.y * nwg.x + wid.x) * 64u + lid.x;\n if (i >= p.n) { return; }\n let g = gate[i];\n y[i] = (g / (1.0 + exp(-g))) * up[i];\n}\n"
|
|
64
64
|
};
|
|
65
|
-
|
|
66
|
-
//#region src/errors.ts
|
|
65
|
+
// src/errors.ts
|
|
67
66
|
/** Thrown when WebGPU is unavailable: no `navigator.gpu` (unsupported browser, or a
|
|
68
67
|
* non-secure context) or no adapter could be acquired. Catch this to render a
|
|
69
68
|
* "your browser doesn't support WebGPU yet" fallback instead of crashing. */
|
|
@@ -82,8 +81,7 @@ var GpuOutOfMemoryError = class extends Error {
|
|
|
82
81
|
super(message);
|
|
83
82
|
}
|
|
84
83
|
};
|
|
85
|
-
|
|
86
|
-
//#region src/pld.ts
|
|
84
|
+
// src/pld.ts
|
|
87
85
|
/** Return up to `maxDraft` draft tokens: the continuation after the most recent prior occurrence
|
|
88
86
|
* of the trailing g-gram, trying g = ngramSize down to 2 (1-grams draft too noisily to pay off).
|
|
89
87
|
* Empty when nothing matches. */
|
|
@@ -109,8 +107,7 @@ function pldWorthIt(emitted, steps, sampled) {
|
|
|
109
107
|
if (steps <= 0) return false;
|
|
110
108
|
return emitted / steps >= (sampled ? 1.5 : 2);
|
|
111
109
|
}
|
|
112
|
-
|
|
113
|
-
//#region src/sampler.ts
|
|
110
|
+
// src/sampler.ts
|
|
114
111
|
/** Mersenne Twister 19937, matching transformers.js utils/random.js (= Python's random.Random). */
|
|
115
112
|
var MT19937 = class {
|
|
116
113
|
mt = /* @__PURE__ */ new Uint32Array(624);
|
|
@@ -127,10 +124,10 @@ var MT19937 = class {
|
|
|
127
124
|
const mt = this.mt;
|
|
128
125
|
const u = (a, b) => Math.imul(a, b) >>> 0;
|
|
129
126
|
const key = [];
|
|
130
|
-
for (let v = n || 0; v > 0; v = Math.floor(v / 4294967296)) key.push(v &
|
|
127
|
+
for (let v = n || 0; v > 0; v = Math.floor(v / 4294967296)) key.push(v & 0xffffffff);
|
|
131
128
|
if (!key.length) key.push(0);
|
|
132
129
|
mt[0] = 19650218;
|
|
133
|
-
for (let k = 1; k < 624; ++k) mt[k] = u(
|
|
130
|
+
for (let k = 1; k < 624; ++k) mt[k] = u(0x6c078965, mt[k - 1] ^ mt[k - 1] >>> 30) + k >>> 0;
|
|
134
131
|
let i = 1;
|
|
135
132
|
let j = 0;
|
|
136
133
|
for (let k = Math.max(624, key.length); k > 0; --k, ++i, ++j) {
|
|
@@ -146,30 +143,30 @@ var MT19937 = class {
|
|
|
146
143
|
mt[0] = mt[623];
|
|
147
144
|
i = 1;
|
|
148
145
|
}
|
|
149
|
-
mt[i] = (mt[i] ^ u(mt[i - 1] ^ mt[i - 1] >>> 30,
|
|
146
|
+
mt[i] = (mt[i] ^ u(mt[i - 1] ^ mt[i - 1] >>> 30, 0x5d588b65)) - i >>> 0;
|
|
150
147
|
}
|
|
151
|
-
mt[0] =
|
|
148
|
+
mt[0] = 0x80000000;
|
|
152
149
|
this.idx = 624;
|
|
153
150
|
}
|
|
154
151
|
int32() {
|
|
155
152
|
const mt = this.mt;
|
|
156
153
|
if (this.idx >= 624) {
|
|
157
154
|
for (let k = 0; k < 624; ++k) {
|
|
158
|
-
const y = mt[k] &
|
|
159
|
-
mt[k] = (mt[(k + 397) % 624] ^ y >>> 1 ^ (y & 1 ?
|
|
155
|
+
const y = mt[k] & 0x80000000 | mt[(k + 1) % 624] & 0x7fffffff;
|
|
156
|
+
mt[k] = (mt[(k + 397) % 624] ^ y >>> 1 ^ (y & 1 ? 0x9908b0df : 0)) >>> 0;
|
|
160
157
|
}
|
|
161
158
|
this.idx = 0;
|
|
162
159
|
}
|
|
163
160
|
let y = mt[this.idx++];
|
|
164
161
|
y ^= y >>> 11;
|
|
165
|
-
y ^= y << 7 &
|
|
166
|
-
y ^= y << 15 &
|
|
162
|
+
y ^= y << 7 & 0x9d2c5680;
|
|
163
|
+
y ^= y << 15 & 0xefc60000;
|
|
167
164
|
y ^= y >>> 18;
|
|
168
165
|
return y >>> 0;
|
|
169
166
|
}
|
|
170
167
|
/** Uniform float in [0, 1), matching Python's random.random(). */
|
|
171
168
|
random() {
|
|
172
|
-
return ((this.int32() >>> 5) * 67108864 + (this.int32() >>> 6)) /
|
|
169
|
+
return ((this.int32() >>> 5) * 67108864 + (this.int32() >>> 6)) / 0x20000000000000;
|
|
173
170
|
}
|
|
174
171
|
};
|
|
175
172
|
/** Deduped set of token ids that repetition_penalty applies to (the full prompt+generated history). */
|
|
@@ -274,8 +271,7 @@ function sampleFromCandidates(candIds, candVals, temperature, rng, topP = 1, min
|
|
|
274
271
|
}
|
|
275
272
|
return candIds[m - 1];
|
|
276
273
|
}
|
|
277
|
-
|
|
278
|
-
//#region src/engine.ts
|
|
274
|
+
// src/engine.ts
|
|
279
275
|
const VIEW = {
|
|
280
276
|
FLOAT: Float32Array,
|
|
281
277
|
UINT8: Uint8Array,
|
|
@@ -468,7 +464,7 @@ async function createEngineInner(options, holder) {
|
|
|
468
464
|
if (kv16 || actF16) features.push("shader-f16");
|
|
469
465
|
const hasTS = adapter.features.has("timestamp-query");
|
|
470
466
|
if (hasTS) features.push("timestamp-query");
|
|
471
|
-
const DEFAULT_BINDING =
|
|
467
|
+
const DEFAULT_BINDING = 0x8000000;
|
|
472
468
|
const DEFAULT_BUFFER = 268435456;
|
|
473
469
|
let needBind = 0;
|
|
474
470
|
let weightBytes = 0;
|
|
@@ -841,7 +837,7 @@ async function createEngineInner(options, holder) {
|
|
|
841
837
|
const e = h >> 10 & 31;
|
|
842
838
|
const m = h & 1023;
|
|
843
839
|
if (e === 0) return s * m * 2 ** -24;
|
|
844
|
-
if (e === 31) return m ? NaN : s *
|
|
840
|
+
if (e === 31) return m ? NaN : s * 1e400;
|
|
845
841
|
return s * (1024 + m) * 2 ** (e - 25);
|
|
846
842
|
};
|
|
847
843
|
const wireQ10 = (region, signPush, signFinish, scalePush, scaleFinish) => {
|
|
@@ -2474,7 +2470,7 @@ async function createEngineInner(options, holder) {
|
|
|
2474
2470
|
["u", affLen],
|
|
2475
2471
|
["u", banLen],
|
|
2476
2472
|
["f", penalty],
|
|
2477
|
-
["u",
|
|
2473
|
+
["u", 0xff800000],
|
|
2478
2474
|
["f", presence]
|
|
2479
2475
|
], [affBuf, banBuf], [lg]);
|
|
2480
2476
|
pass.dispatchWorkgroups(1);
|
|
@@ -2610,7 +2606,7 @@ async function createEngineInner(options, holder) {
|
|
|
2610
2606
|
const pVals = [];
|
|
2611
2607
|
const B = 512;
|
|
2612
2608
|
for (let i = 0; i < order.length && pIds.length < K; i += B) {
|
|
2613
|
-
if (all[order[i]] === -
|
|
2609
|
+
if (all[order[i]] === -1e400 && pIds.length > 0) break;
|
|
2614
2610
|
const batch = order.slice(i, i + B);
|
|
2615
2611
|
const ok = new Set(filter(Uint32Array.from(batch), Float32Array.from(batch.map((id) => all[id]))));
|
|
2616
2612
|
for (const id of batch) if (ok.has(id)) {
|
|
@@ -2618,7 +2614,7 @@ async function createEngineInner(options, holder) {
|
|
|
2618
2614
|
pVals.push(all[id]);
|
|
2619
2615
|
if (pIds.length >= K) break;
|
|
2620
2616
|
}
|
|
2621
|
-
if (pVals[0] === -
|
|
2617
|
+
if (pVals[0] === -1e400) {
|
|
2622
2618
|
pIds.length = 1;
|
|
2623
2619
|
pVals.length = 1;
|
|
2624
2620
|
break;
|
|
@@ -2848,7 +2844,7 @@ async function createEngineInner(options, holder) {
|
|
|
2848
2844
|
["u", affLen],
|
|
2849
2845
|
["u", banLen],
|
|
2850
2846
|
["f", penalty],
|
|
2851
|
-
["u",
|
|
2847
|
+
["u", 0xff800000],
|
|
2852
2848
|
["f", presence]
|
|
2853
2849
|
], [affBuf, banBuf], [lg]);
|
|
2854
2850
|
pass.dispatchWorkgroups(1);
|
|
@@ -3167,7 +3163,7 @@ async function createEngineInner(options, holder) {
|
|
|
3167
3163
|
["u", aff.length],
|
|
3168
3164
|
["u", ban.length],
|
|
3169
3165
|
["f", penalty],
|
|
3170
|
-
["u",
|
|
3166
|
+
["u", 0xff800000],
|
|
3171
3167
|
["f", presence]
|
|
3172
3168
|
], [affBuf, banBuf], [lg]);
|
|
3173
3169
|
pass.dispatchWorkgroups(1);
|
|
@@ -3540,7 +3536,6 @@ async function createEngineInner(options, holder) {
|
|
|
3540
3536
|
debugSampler: serialize(debugSampler)
|
|
3541
3537
|
};
|
|
3542
3538
|
}
|
|
3543
|
-
//#endregion
|
|
3544
3539
|
export { GpuOutOfMemoryError, WebGPUUnavailableError, createEngine };
|
|
3545
3540
|
|
|
3546
3541
|
//# sourceMappingURL=index.js.map
|