bitgpu 0.3.0 → 0.5.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 +43 -3
- package/dist/index.d.ts +15 -4
- package/dist/index.js +364 -105
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -3,10 +3,13 @@
|
|
|
3
3
|
A fast, dependency-free WebGPU runtime for **low-bit LLMs** in the browser.
|
|
4
4
|
|
|
5
5
|
Today it runs **1-bit (binary-weight)** models.
|
|
6
|
-
Reference
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
Reference targets are Bonsai **1.7B, 4B and 8B** (Qwen3 architecture, sign-packed binary linear
|
|
7
|
+
weights + 2/4-bit embeddings, tied or untied lm_head) - every size is gated bit-exact against the
|
|
8
|
+
reference forward on real hardware. GPU-resident decode (greedy or sampled), streaming, EOS stop,
|
|
9
|
+
`AbortSignal`, and cross-turn KV-cache reuse. Runs the fast subgroup path on Apple / NVIDIA /
|
|
9
10
|
recent AMD and falls back to a workgroup-reduction path everywhere else WebGPU is available.
|
|
11
|
+
Device limits are negotiated from the manifest, so the 8B's ~148 MiB lm_head binding is requested
|
|
12
|
+
only when that model needs it and smaller models keep running at WebGPU's guaranteed minimums.
|
|
10
13
|
|
|
11
14
|
## Install
|
|
12
15
|
|
|
@@ -50,6 +53,33 @@ engine.dispose()
|
|
|
50
53
|
Tokenization is intentionally out of scope: the engine operates on token ids, so you can pair it
|
|
51
54
|
with any tokenizer.
|
|
52
55
|
|
|
56
|
+
## Bring your own model
|
|
57
|
+
|
|
58
|
+
bitgpu loads its own small format instead of parsing ONNX at runtime: a `manifest.json` (the
|
|
59
|
+
architecture contract + every tensor mapped to a byte range) and a ~30 KB aux file, both
|
|
60
|
+
produced ONCE, offline, from a standard export - while the big weights file is used
|
|
61
|
+
byte-for-byte unchanged, so it can keep streaming from wherever it already lives (e.g. the
|
|
62
|
+
Hugging Face Hub). Same one-time-conversion model as GGUF/llama.cpp or MLX.
|
|
63
|
+
|
|
64
|
+
```sh
|
|
65
|
+
python tools/convert.py --model <dir with config.json + the q1 .onnx + its data file>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Host the two small files anywhere (they're static), point `createEngine` at them, done:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
createEngine({
|
|
72
|
+
manifestUrl: 'https://your-site.example/model/manifest.json',
|
|
73
|
+
auxUrl: 'https://your-site.example/model/model_q1.aux.bin',
|
|
74
|
+
dataUrl: 'https://huggingface.co/<repo>/resolve/main/onnx/model_q1.onnx_data',
|
|
75
|
+
})
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Compatibility envelope: Qwen3-family models quantized with the onnx-community 1-bit ("q1")
|
|
79
|
+
recipe (silu/SwiGLU, head_dim <= 128, 128-wide scale blocks) - the engine validates the
|
|
80
|
+
manifest loudly at load. Format spec: [docs/FORMAT.md](docs/FORMAT.md); the full pipeline
|
|
81
|
+
including regenerating the verification fixtures for a new model: [tools/README.md](tools/README.md).
|
|
82
|
+
|
|
53
83
|
## API
|
|
54
84
|
|
|
55
85
|
- `createEngine(options: EngineOptions | string): Promise<Engine>` - load a model. A bare string is
|
|
@@ -122,6 +152,16 @@ Or open `http://localhost:8000/examples/verify.html` in a WebGPU browser and cli
|
|
|
122
152
|
gate on real hardware before every release; CI covers only the CPU-checkable parts (types, sampler
|
|
123
153
|
math, drafter, packaging).
|
|
124
154
|
|
|
155
|
+
The gate is model-parametric: `verify.html?model=<tag>` loads `examples/model-<tag>` against
|
|
156
|
+
`test-fixtures/forward-<tag>`, and the headless driver automatically runs every staged variant.
|
|
157
|
+
Fixture sets for all three Bonsai sizes are committed - `forward` (1.7B, hidden 2048),
|
|
158
|
+
`forward-4b` (4B, hidden 2560) and `forward-8b` (8B, hidden 4096, untied lm_head, raised
|
|
159
|
+
device limits) - so engine changes are checked against three geometries; stage the extra
|
|
160
|
+
weights with `ln -s /path/to/bonsai-<size> examples/model-<size>`. To add fixtures for another
|
|
161
|
+
model, run `tools/golden.py` then `tools/reference.py --dump test-fixtures/forward-<tag>` on the
|
|
162
|
+
converted work dir and record the engine's greedy continuation as `known_good` in that set's
|
|
163
|
+
`params.json`.
|
|
164
|
+
|
|
125
165
|
### Releasing
|
|
126
166
|
|
|
127
167
|
Publishing runs through GitHub Actions with npm trusted publishing (OIDC + provenance, no token).
|
package/dist/index.d.ts
CHANGED
|
@@ -24,6 +24,13 @@ interface EngineOptions {
|
|
|
24
24
|
fetchJson?: (url: string) => Promise<unknown>;
|
|
25
25
|
/** Fetch binary (data/aux). Override to add caching (e.g. OPFS) for the ~290MB data file. Default: `fetch(url).arrayBuffer()`. */
|
|
26
26
|
fetchArrayBuffer?: (url: string) => Promise<ArrayBuffer>;
|
|
27
|
+
/** Stream the big weights DATA file: chunks flow straight into GPU buffers, so the whole
|
|
28
|
+
* ~290MB file never sits in memory at once (critical on phones, where the buffered peak gets
|
|
29
|
+
* the tab killed). Used for the data file only (manifest/aux stay on fetchJson/fetchArrayBuffer);
|
|
30
|
+
* when set it takes precedence over fetchArrayBuffer for that file. Point it at a cache stream,
|
|
31
|
+
* e.g. an OPFS file's `stream()`. Default: the data file streams from `fetch(url).body`
|
|
32
|
+
* (or through `fetchArrayBuffer` when that override is provided, for back compatibility). */
|
|
33
|
+
fetchStream?: (url: string) => Promise<ReadableStream<Uint8Array>>;
|
|
27
34
|
/** GPU power preference. Default `'high-performance'` (picks the discrete GPU on multi-GPU machines).
|
|
28
35
|
* (Spelled out rather than `GPUPowerPreference` so the published d.ts resolves without @webgpu/types.) */
|
|
29
36
|
powerPreference?: 'low-power' | 'high-performance';
|
|
@@ -62,7 +69,8 @@ interface DeviceLostInfo {
|
|
|
62
69
|
* to sample; otherwise decoding is greedy (argmax). Sampling matches transformers.js v4.2.0 exactly
|
|
63
70
|
* (repetition_penalty, no_repeat_ngram, temperature, top_k, multinomial via a Mersenne-Twister RNG). */
|
|
64
71
|
interface GenerateOptions {
|
|
65
|
-
/** Maximum number of new tokens to generate. Default `256`.
|
|
72
|
+
/** Maximum number of new tokens to generate. Default `256`. `0` prefills the prompt into the
|
|
73
|
+
* KV cache and emits nothing (like {@link Engine.prefill}, but composable with `reuseCache`). */
|
|
66
74
|
maxTokens?: number;
|
|
67
75
|
/** Reuse the KV cache from the previous turn: treat the passed token ids as the DELTA to append to
|
|
68
76
|
* the cached conversation (not a full prompt), prefilling only those tokens. Requires a prior
|
|
@@ -76,13 +84,16 @@ interface GenerateOptions {
|
|
|
76
84
|
signal?: AbortSignal;
|
|
77
85
|
/** Softmax temperature. A value other than 0 or 1 enables sampling; 0 or 1 (or unset) is greedy. */
|
|
78
86
|
temperature?: number;
|
|
79
|
-
/** Top-k sampling cutoff (candidate count). Default `20` when sampling
|
|
87
|
+
/** Top-k sampling cutoff (candidate count). Default `20` when sampling; clamped to `[1, vocab]`
|
|
88
|
+
* (the GPU reduces the logits to this many candidates, so `0` cannot mean "disabled"). */
|
|
80
89
|
topK?: number;
|
|
81
90
|
/** Top-p (nucleus) cutoff. Accepted for API compatibility but not applied (a no-op, matching transformers.js v4.2.0). */
|
|
82
91
|
topP?: number;
|
|
83
|
-
/** Repetition penalty over the deduped prompt+generated id set (`logit<0 ? *p : /p`). Default `1`
|
|
92
|
+
/** Repetition penalty over the deduped prompt+generated id set (`logit<0 ? *p : /p`). Default `1`
|
|
93
|
+
* (off). Applied under greedy decoding too (the penalized argmax), matching transformers.js. */
|
|
84
94
|
repetitionPenalty?: number;
|
|
85
|
-
/** Block any n-gram of this size from repeating. Default `0` (off).
|
|
95
|
+
/** Block any n-gram of this size from repeating. Default `0` (off). Applied under greedy decoding
|
|
96
|
+
* too, matching transformers.js. */
|
|
86
97
|
noRepeatNgramSize?: number;
|
|
87
98
|
/** Seed for the sampler RNG. Omit to seed from entropy (non-deterministic, like production). */
|
|
88
99
|
seed?: number;
|