bitgpu 0.13.0 → 0.14.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 +26 -5
- package/dist/chat.d.ts +24 -3
- package/dist/chat.js +24 -6
- package/dist/chat.js.map +1 -1
- package/dist/gguf.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +134 -40
- package/dist/index.js.map +1 -1
- package/dist/{types-ByYx15Yw.d.ts → types-iFADjEuf.d.ts} +34 -4
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -25,6 +25,22 @@ npm install bitgpu
|
|
|
25
25
|
|
|
26
26
|
ESM-only, zero runtime dependencies.
|
|
27
27
|
|
|
28
|
+
## Benchmark
|
|
29
|
+
|
|
30
|
+
Against the mainstream WebGPU path for the same model - transformers.js with `dtype: 'q1'` - on the
|
|
31
|
+
**identical** 1-bit Bonsai-1.7B weights, same GPU, same prompt, both in one page:
|
|
32
|
+
|
|
33
|
+
| | transformers.js 4.2.0 (`dtype: 'q1'`) | bitgpu |
|
|
34
|
+
| --- | --- | --- |
|
|
35
|
+
| decode | 17.7 tok/s | **23.8 tok/s** (up to ~1.8x at short context) |
|
|
36
|
+
| prefill (156-token prompt) | 5.6 s | **0.9 s** (~6x) |
|
|
37
|
+
|
|
38
|
+
Point-in-time, one machine (Apple M-series, Chrome 150 / WebGPU, 2026-07) - so treat it as a
|
|
39
|
+
ballpark, not a leaderboard. Reproduce it, or run your own model / GPU, with `npm run bench`
|
|
40
|
+
([examples/benchmark.html](examples/benchmark.html) loads both engines side by side and prints the
|
|
41
|
+
numbers). Weights are bit-identical across the two containers (the GGUF sign-bit stream equals the
|
|
42
|
+
onnx-community q1 export), so this measures the runtime, not the model.
|
|
43
|
+
|
|
28
44
|
## Quickstart - no conversion, no hosting
|
|
29
45
|
|
|
30
46
|
Ready-made manifests for all three Bonsai sizes are committed under [`models/`](models/); the
|
|
@@ -34,7 +50,7 @@ weights stream straight from the Hugging Face Hub. This runs as-is:
|
|
|
34
50
|
import { createEngine } from 'bitgpu'
|
|
35
51
|
import { createChat } from 'bitgpu/chat'
|
|
36
52
|
|
|
37
|
-
const REPO = 'https://cdn.jsdelivr.net/gh/stfurkan/bitgpu@v0.
|
|
53
|
+
const REPO = 'https://cdn.jsdelivr.net/gh/stfurkan/bitgpu@v0.14.0/models/bonsai-1.7b-gguf'
|
|
38
54
|
const TOK = 'https://huggingface.co/onnx-community/Bonsai-1.7B-ONNX/resolve/main'
|
|
39
55
|
const engine = await createEngine({
|
|
40
56
|
manifestUrl: `${REPO}/manifest.json`,
|
|
@@ -226,10 +242,15 @@ feasibility, so the model can never be trapped mid-number), and `oneOf` as a **d
|
|
|
226
242
|
union** - object branches sharing one required property whose single-value `enum` differs per
|
|
227
243
|
branch, e.g. a slide that is either `{type: 'bullets', ...}` or `{type: 'quote', ...}`; the
|
|
228
244
|
machine tracks the live branches until the discriminator commits. All nested to any depth.
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
245
|
+
Annotation-only keywords - `description`, `title`, `default`, `examples`, `$schema`, `$id`,
|
|
246
|
+
`deprecated`, `readOnly`, `writeOnly`, `$comment` - are **accepted and ignored** (they carry no
|
|
247
|
+
constraint), so a real MCP / OpenAI tool schema passes through unmodified and a property's
|
|
248
|
+
`description` still reaches the model via the chat template. Genuinely-constraining keywords the
|
|
249
|
+
enforcer does not implement (`pattern`, `format`, float ranges, general `oneOf`, `$ref`, ...)
|
|
250
|
+
**throw up front** - never silently ignored, because those would *look* enforced. The guarantee
|
|
251
|
+
is structural, not semantic: a schema makes the output parse into the right shape, not be true.
|
|
252
|
+
Built on the engine's generic `candidateFilter` hook (see `GenerateOptions`), which is open for
|
|
253
|
+
custom grammars.
|
|
233
254
|
|
|
234
255
|
### Confidence (`logprobs`)
|
|
235
256
|
|
package/dist/chat.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as KvSnapshot, m as TokenLogprobs, n as Engine, o as GenerateOptions } from "./types-
|
|
1
|
+
import { c as KvSnapshot, m as TokenLogprobs, n as Engine, o as GenerateOptions } from "./types-iFADjEuf.js";
|
|
2
2
|
|
|
3
3
|
//#region src/chat/tokenizer.d.ts
|
|
4
4
|
interface ChatMessage {
|
|
@@ -76,6 +76,19 @@ interface JsonSchema {
|
|
|
76
76
|
* combinable with enum (the literals already fix the length). */
|
|
77
77
|
minLength?: number;
|
|
78
78
|
maxLength?: number;
|
|
79
|
+
/** Human-readable docs. Ignored by the enforcer, but the chat template renders the tools list
|
|
80
|
+
* (parameters included) into the model's prompt, so a property `description` DOES reach the
|
|
81
|
+
* model - the reason MCP/OpenAI tool schemas pass through unmodified. */
|
|
82
|
+
description?: string;
|
|
83
|
+
title?: string;
|
|
84
|
+
default?: unknown;
|
|
85
|
+
examples?: unknown[];
|
|
86
|
+
deprecated?: boolean;
|
|
87
|
+
readOnly?: boolean;
|
|
88
|
+
writeOnly?: boolean;
|
|
89
|
+
$comment?: string;
|
|
90
|
+
$schema?: string;
|
|
91
|
+
$id?: string;
|
|
79
92
|
}
|
|
80
93
|
/** Validate a schema against the enforceable subset; throws listing anything unsupported. */
|
|
81
94
|
declare function validateJsonSchema(schema: JsonSchema, path?: string, isRoot?: boolean): void;
|
|
@@ -441,8 +454,16 @@ interface Chat {
|
|
|
441
454
|
* to another worker; restoring it into a chat on the same model and `kvCache` mode makes the
|
|
442
455
|
* next clean-append turn extend the cache exactly as if the conversation never stopped (no
|
|
443
456
|
* re-prefill). Returns null when no conversation is committed. Queues behind in-flight turns
|
|
444
|
-
* like send/stream.
|
|
445
|
-
|
|
457
|
+
* like send/stream.
|
|
458
|
+
*
|
|
459
|
+
* `{ delta: true }` makes a DELTA snapshot that excludes the shared prewarmed prefix (the system
|
|
460
|
+
* messages + tools warmed with {@link Chat.prewarm}), so a per-conversation snapshot drops the
|
|
461
|
+
* redundant system-prompt KV - tens of MB at chat scale, and a smaller structured-clone. Restore
|
|
462
|
+
* it into a chat freshly `prewarm()`ed with the SAME system + tools (restore validates and throws
|
|
463
|
+
* on a mismatch). Requires a prior prewarm(); throws otherwise. */
|
|
464
|
+
save(opts?: {
|
|
465
|
+
delta?: boolean;
|
|
466
|
+
}): Promise<ChatSnapshot | null>;
|
|
446
467
|
/** Replace the current conversation with a saved snapshot (see {@link Chat.save}). Throws when
|
|
447
468
|
* the snapshot does not match this engine's model or `kvCache` mode. */
|
|
448
469
|
restore(snapshot: ChatSnapshot): Promise<void>;
|
package/dist/chat.js
CHANGED
|
@@ -4415,6 +4415,18 @@ const SUPPORTED = /* @__PURE__ */ new Set([
|
|
|
4415
4415
|
"minLength",
|
|
4416
4416
|
"maxLength"
|
|
4417
4417
|
]);
|
|
4418
|
+
const ANNOTATIONS = /* @__PURE__ */ new Set([
|
|
4419
|
+
"description",
|
|
4420
|
+
"title",
|
|
4421
|
+
"default",
|
|
4422
|
+
"examples",
|
|
4423
|
+
"deprecated",
|
|
4424
|
+
"readOnly",
|
|
4425
|
+
"writeOnly",
|
|
4426
|
+
"$comment",
|
|
4427
|
+
"$schema",
|
|
4428
|
+
"$id"
|
|
4429
|
+
]);
|
|
4418
4430
|
const TYPES = /* @__PURE__ */ new Set([
|
|
4419
4431
|
"object",
|
|
4420
4432
|
"array",
|
|
@@ -4435,10 +4447,10 @@ function findDiscriminator(branches) {
|
|
|
4435
4447
|
}
|
|
4436
4448
|
/** Validate a schema against the enforceable subset; throws listing anything unsupported. */
|
|
4437
4449
|
function validateJsonSchema(schema, path = "schema", isRoot = true) {
|
|
4438
|
-
const unknown = Object.keys(schema).filter((k) => !SUPPORTED.has(k));
|
|
4439
|
-
if (unknown.length) throw new Error(`bitgpu/chat: unsupported JSON Schema keyword(s) at ${path}: ${unknown.join(", ")} (enforceable subset: ${[...SUPPORTED].join(", ")})`);
|
|
4450
|
+
const unknown = Object.keys(schema).filter((k) => !SUPPORTED.has(k) && !ANNOTATIONS.has(k));
|
|
4451
|
+
if (unknown.length) throw new Error(`bitgpu/chat: unsupported JSON Schema keyword(s) at ${path}: ${unknown.join(", ")} (enforceable subset: ${[...SUPPORTED].join(", ")}; annotations accepted-and-ignored: ${[...ANNOTATIONS].join(", ")})`);
|
|
4440
4452
|
if (schema.oneOf !== void 0) {
|
|
4441
|
-
const extra = Object.keys(schema).filter((k) => k !== "oneOf");
|
|
4453
|
+
const extra = Object.keys(schema).filter((k) => k !== "oneOf" && !ANNOTATIONS.has(k));
|
|
4442
4454
|
if (extra.length) throw new Error(`bitgpu/chat: oneOf at ${path} cannot combine with other keywords (got ${extra.join(", ")})`);
|
|
4443
4455
|
if (!Array.isArray(schema.oneOf) || schema.oneOf.length < 2) throw new Error(`bitgpu/chat: oneOf at ${path} needs at least 2 branches`);
|
|
4444
4456
|
schema.oneOf.forEach((b, i) => {
|
|
@@ -5413,10 +5425,12 @@ async function createChat(engine, options) {
|
|
|
5413
5425
|
let committed = null;
|
|
5414
5426
|
let cacheEndsAtEos = false;
|
|
5415
5427
|
let committedToolsKey = null;
|
|
5428
|
+
let prewarmLen = 0;
|
|
5416
5429
|
let resetEpoch = 0;
|
|
5417
5430
|
const dropCache = () => {
|
|
5418
5431
|
committed = null;
|
|
5419
5432
|
committedToolsKey = null;
|
|
5433
|
+
prewarmLen = 0;
|
|
5420
5434
|
engine.resetCache();
|
|
5421
5435
|
};
|
|
5422
5436
|
let templateToolsOk = null;
|
|
@@ -5624,11 +5638,13 @@ async function createChat(engine, options) {
|
|
|
5624
5638
|
enableThinking: false,
|
|
5625
5639
|
tools: prep?.tools
|
|
5626
5640
|
}).replace(/\n$/, "");
|
|
5641
|
+
const toks = tk.encode(str, false);
|
|
5627
5642
|
const epoch0 = resetEpoch;
|
|
5628
|
-
await engine.prefill(
|
|
5643
|
+
await engine.prefill(toks);
|
|
5629
5644
|
if (resetEpoch !== epoch0) return;
|
|
5630
5645
|
committed = [...messages];
|
|
5631
5646
|
committedToolsKey = prep ? JSON.stringify(prep.tools) : null;
|
|
5647
|
+
prewarmLen = toks.length;
|
|
5632
5648
|
cacheEndsAtEos = true;
|
|
5633
5649
|
}
|
|
5634
5650
|
return {
|
|
@@ -5683,9 +5699,10 @@ async function createChat(engine, options) {
|
|
|
5683
5699
|
resetEpoch++;
|
|
5684
5700
|
dropCache();
|
|
5685
5701
|
},
|
|
5686
|
-
save: serialize(async () => {
|
|
5702
|
+
save: serialize(async (opts) => {
|
|
5687
5703
|
if (committed === null) return null;
|
|
5688
|
-
|
|
5704
|
+
if (opts?.delta && prewarmLen <= 0) throw new Error("bitgpu/chat: save({ delta: true }) needs a prewarm() first (no shared prefix to exclude)");
|
|
5705
|
+
const eng = await engine.saveCache(opts?.delta ? { from: prewarmLen - 1 } : void 0);
|
|
5689
5706
|
if (!eng) return null;
|
|
5690
5707
|
return {
|
|
5691
5708
|
version: 1,
|
|
@@ -5703,6 +5720,7 @@ async function createChat(engine, options) {
|
|
|
5703
5720
|
committed = JSON.parse(JSON.stringify(snap.committed));
|
|
5704
5721
|
committedToolsKey = snap.toolsKey ?? null;
|
|
5705
5722
|
cacheEndsAtEos = !!snap.cacheEndsAtEos;
|
|
5723
|
+
if (!snap.engine.base) prewarmLen = 0;
|
|
5706
5724
|
}),
|
|
5707
5725
|
eosTokenId: tk.eosTokenId,
|
|
5708
5726
|
tokenizer: tk
|