gpu-engine 0.0.0-stage → 0.0.1
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/LICENSE +21 -0
- package/README.md +96 -2
- package/SPEC.md +988 -0
- package/THIRD_PARTY_NOTICES.md +29 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +4 -0
- package/dist/harness.d.mts +132 -0
- package/dist/harness.mjs +1 -0
- package/dist/index.d.mts +98 -0
- package/dist/index.mjs +1 -0
- package/dist/load-o_thEAKC.mjs +258 -0
- package/dist/scaffold-CY5xhy-V.mjs +106 -0
- package/dist/types-BwigqFwr.d.mts +868 -0
- package/docs/apps.md +108 -0
- package/docs/cli.md +126 -0
- package/docs/data.md +101 -0
- package/docs/diagnostics.md +509 -0
- package/docs/harness.md +191 -0
- package/docs/layers.md +249 -0
- package/docs/measurements/auto-threshold.json +335 -0
- package/docs/measurements/auto-threshold.md +56 -0
- package/docs/measurements/real-gpu-2026-11-22.json +4718 -0
- package/docs/measurements/real-gpu-2026-11-22.md +75 -0
- package/docs/runtime.md +112 -0
- package/docs/sealed-test-set.md +107 -0
- package/docs/weights-format.md +138 -0
- package/llms.txt +24 -0
- package/package.json +73 -3
- package/skills/gpu-engine/SKILL.md +117 -0
- package/templates/common/AGENTS.md +27 -0
- package/templates/common/github/workflows/ci.yml +35 -0
- package/templates/common/gitignore +3 -0
- package/templates/common/package.template.json +27 -0
- package/templates/common/tsconfig.template.json +17 -0
- package/templates/pooled/NEW_MODEL_NAME.eval.ts +93 -0
- package/templates/pooled/NEW_MODEL_NAME.model.ts +11 -0
- package/templates/pooled/SPEC.md +31 -0
- package/templates/pooled/src/decode.ts +10 -0
- package/templates/pooled/src/index.ts +44 -0
- package/templates/pooled/src/weights.ts +4 -0
- package/templates/retrieve/NEW_MODEL_NAME.eval.ts +149 -0
- package/templates/retrieve/NEW_MODEL_NAME.model.ts +12 -0
- package/templates/retrieve/SPEC.md +33 -0
- package/templates/retrieve/data/table.jsonl +16 -0
- package/templates/retrieve/src/decode.ts +9 -0
- package/templates/retrieve/src/index.ts +45 -0
- package/templates/retrieve/src/table.ts +21 -0
- package/templates/retrieve/src/weights.ts +4 -0
- package/templates/tag/NEW_MODEL_NAME.eval.ts +114 -0
- package/templates/tag/NEW_MODEL_NAME.model.ts +11 -0
- package/templates/tag/SPEC.md +31 -0
- package/templates/tag/src/decode.ts +25 -0
- package/templates/tag/src/index.ts +44 -0
- package/templates/tag/src/weights.ts +4 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Real GPU run, 2026-11-22
|
|
2
|
+
|
|
3
|
+
The first CPU and WGSL comparison on a real GPU in a real browser: [examples/browser-demo](https://github.com/onmax/gpu-engine/tree/main/examples/browser-demo) ran the promoted `examples/tag` and `examples/retrieve` models on both backends on 20 fixed dev inputs each. The page's JSON of every run, as it came back, is in [real-gpu-2026-11-22.json](./real-gpu-2026-11-22.json).
|
|
4
|
+
|
|
5
|
+
The first runs (15:16) took the decoded results and timings from the examples' `src/` decode steps on the library's internal runtime. The page now imports each example's built package, the `dist/index.mjs` that `gpu-engine build` writes and an app imports, and three more Chrome runs (16:21) took the decoded results and timings from it ([Built packages](#built-packages-1621)). The logits come from the internal runtime in every run, since a tag package's `run` returns labels.
|
|
6
|
+
|
|
7
|
+
| Field | Value |
|
|
8
|
+
| -------- | -------------------------------------------------------------------------------------------------------------- |
|
|
9
|
+
| Machine | MacBook Air (M1, 2020), `MacBookAir10,1`, 8 GB |
|
|
10
|
+
| GPU | Apple M1, 7-core GPU, Metal 3 |
|
|
11
|
+
| OS | macOS 14.4.1 (23E224) |
|
|
12
|
+
| Browsers | Google Chrome 155.0.8059.85 (3 + 3 runs), Safari 17.4.1 (1 run) |
|
|
13
|
+
| Adapter | Chrome: `apple metal-3` (vendor `apple`, architecture `metal-3`, no description), not a fallback adapter |
|
|
14
|
+
| Served | `node serve.ts` on `127.0.0.1:4870` on that Mac, cross-origin isolated (clock in microseconds) |
|
|
15
|
+
| Built | `node build.ts` on another machine; the files copied to the Mac were `index.html`, `serve.ts`, `dist/page.mjs` |
|
|
16
|
+
|
|
17
|
+
## Parity in Chrome
|
|
18
|
+
|
|
19
|
+
Every Chrome run passed, with the same numbers each time:
|
|
20
|
+
|
|
21
|
+
| Model | Compared | Largest difference | On input | Tolerance | Decoded results differ | Head outputs differ |
|
|
22
|
+
| ------------------- | ----------------------------------------------------- | -----------------: | ---------------------------------- | --------: | ---------------------: | ------------------: |
|
|
23
|
+
| `examples/tag` | logits per token and label, labels, decoded result | 1.91e-6 | `7,019 milliliters into teaspoons` | 1e-3 | 0 of 20 | 0 of 20 |
|
|
24
|
+
| `examples/retrieve` | score of every table row, three best rows, decoded id | 5.72e-6 | `where is later import data` | 1e-3 | 0 of 20 | 0 of 20 |
|
|
25
|
+
|
|
26
|
+
The software adapter (llvmpipe) gives the same largest difference for the tag model on these inputs (1.91e-6) and a slightly larger one for the retrieve model (7.63e-6), measured in Node with `test/gpu/browser-demo.test.ts`'s setup.
|
|
27
|
+
|
|
28
|
+
## Timings
|
|
29
|
+
|
|
30
|
+
Milliseconds. Load is `loadModel` (decode the weights, and on WebGPU open the device and compile the pipelines). First run is the first input, cold. Median and total cover 5 timed passes over the 20 inputs (100 runs) per backend, `model.run` with featurizing and decoding included.
|
|
31
|
+
|
|
32
|
+
| Run | Model | CPU load | CPU first | CPU median | CPU total | WebGPU load | WebGPU first | WebGPU median | WebGPU total |
|
|
33
|
+
| --------------- | ------------------- | -------: | --------: | ---------: | --------: | ----------: | -----------: | ------------: | -----------: |
|
|
34
|
+
| Chrome 15:16:05 | `examples/tag` | 7.44 | 1.31 | 0.110 | 11.20 | 31.81 | 1.905 | 0.645 | 162.77 |
|
|
35
|
+
| Chrome 15:16:05 | `examples/retrieve` | 16.54 | 0.45 | 0.040 | 4.21 | 22.29 | 1.035 | 0.505 | 124.26 |
|
|
36
|
+
| Chrome 15:16:07 | `examples/tag` | 3.34 | 0.76 | 0.060 | 6.64 | 8.03 | 8.040 | 0.495 | 114.75 |
|
|
37
|
+
| Chrome 15:16:07 | `examples/retrieve` | 14.30 | 0.21 | 0.035 | 4.01 | 16.87 | 0.825 | 0.500 | 115.16 |
|
|
38
|
+
| Chrome 15:16:10 | `examples/tag` | 4.14 | 0.63 | 0.060 | 6.16 | 11.88 | 1.090 | 0.510 | 126.99 |
|
|
39
|
+
| Chrome 15:16:10 | `examples/retrieve` | 14.28 | 0.22 | 0.035 | 3.61 | 17.98 | 0.790 | 0.475 | 115.06 |
|
|
40
|
+
| Safari 15:16:32 | `examples/tag` | 28.58 | 6.20 | 0.120 | 16.10 | not run | | | |
|
|
41
|
+
| Safari 15:16:32 | `examples/retrieve` | 23.92 | 0.88 | 0.060 | 6.30 | not run | | | |
|
|
42
|
+
|
|
43
|
+
On these models the CPU's median run is 6 to 14 times faster than WebGPU's on the M1: the tag model does 960 multiply-adds per token (23,040 at its 24-token limit) and the retrieve tower 832 (9,984 at 12 tokens), while a WebGPU run pays for an upload, a dispatch and a readback, about half a millisecond. `auto` runs them on the CPU (rule 2: no run of either model reaches `AUTO_WEBGPU_MIN_MACS`), which these numbers agree with. They do not measure the threshold itself: that needs `scripts/bench-threshold.ts`'s sweep on this device. An earlier Chrome run, before the server sent the cross-origin isolation headers (clock in 0.1 ms steps, not in the JSON), loaded the tag model on WebGPU in 973 ms, the first pipeline compile with a cold shader cache.
|
|
44
|
+
|
|
45
|
+
## Built packages (16:21)
|
|
46
|
+
|
|
47
|
+
Three Chrome runs of the page that imports the built packages (`dist/page.mjs` SHA-256 `dfaffdd8...7fbdb`), each in a new tab of Chrome 155.0.8059.85 opened through its DevTools port, on the same adapter (`apple metal-3`, not a fallback adapter). Every run passed with the same numbers as before:
|
|
48
|
+
|
|
49
|
+
| Model | Largest difference | On input | Tolerance | Decoded results differ | Head outputs differ |
|
|
50
|
+
| ------------------- | -----------------: | ---------------------------------- | --------: | ---------------------: | ------------------: |
|
|
51
|
+
| `examples/tag` | 1.91e-6 | `7,019 milliliters into teaspoons` | 1e-3 | 0 of 20 | 0 of 20 |
|
|
52
|
+
| `examples/retrieve` | 5.72e-6 | `where is later import data` | 1e-3 | 0 of 20 | 0 of 20 |
|
|
53
|
+
|
|
54
|
+
Milliseconds, as above, but load is the package's `load` and a run is the package's `run`, decoding included:
|
|
55
|
+
|
|
56
|
+
| Run | Model | CPU load | CPU first | CPU median | CPU total | WebGPU load | WebGPU first | WebGPU median | WebGPU total |
|
|
57
|
+
| --------------- | ------------------- | -------: | --------: | ---------: | --------: | ----------: | -----------: | ------------: | -----------: |
|
|
58
|
+
| Chrome 16:21:11 | `examples/tag` | 6.16 | 2.51 | 0.065 | 7.18 | 15.28 | 10.385 | 0.530 | 133.69 |
|
|
59
|
+
| Chrome 16:21:11 | `examples/retrieve` | 20.26 | 0.60 | 0.040 | 4.56 | 21.74 | 1.350 | 0.485 | 108.53 |
|
|
60
|
+
| Chrome 16:21:14 | `examples/tag` | 5.68 | 2.46 | 0.065 | 7.16 | 7.53 | 8.395 | 0.480 | 109.78 |
|
|
61
|
+
| Chrome 16:21:14 | `examples/retrieve` | 19.98 | 0.54 | 0.040 | 4.52 | 27.01 | 1.045 | 0.487 | 120.61 |
|
|
62
|
+
| Chrome 16:21:17 | `examples/tag` | 5.36 | 2.47 | 0.075 | 8.46 | 15.51 | 2.035 | 0.547 | 143.67 |
|
|
63
|
+
| Chrome 16:21:17 | `examples/retrieve` | 21.03 | 0.66 | 0.045 | 5.23 | 24.75 | 1.490 | 0.568 | 172.80 |
|
|
64
|
+
|
|
65
|
+
The built packages run at the speed of the source path: CPU medians of 0.040 to 0.075 ms and WebGPU medians of 0.480 to 0.568 ms, so the CPU is still 7 to 14 times faster per run on these models.
|
|
66
|
+
|
|
67
|
+
## Safari
|
|
68
|
+
|
|
69
|
+
Safari 17.4.1 has no `navigator.gpu` (WebGPU is off by default in that version), so the page ran both models on the CPU only and failed the WebGPU side: `no WebGPU entry point (navigator.gpu is missing)`. Real browser on WebGPU in Safari: not run.
|
|
70
|
+
|
|
71
|
+
## Not covered
|
|
72
|
+
|
|
73
|
+
- `gpu-engine parity` itself (Node, Dawn) did not run on this GPU, so the examples' `parity.json` and `MODEL_CARD.md` still say `realGpu: not run`.
|
|
74
|
+
- One device and one browser engine with WebGPU. Other GPUs, Firefox and Safari with WebGPU: not run.
|
|
75
|
+
- The pooled example is not in the demo.
|
package/docs/runtime.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# Runtime
|
|
2
|
+
|
|
3
|
+
The runtime loads a weights file and runs it on WebGPU or on the CPU. It lives in the `gpu-engine` entry, which imports no `node:` module, so it runs in browsers, workers and Node. [SPEC.md](../SPEC.md) section 9 is the normative text.
|
|
4
|
+
|
|
5
|
+
Measured so far: WebGPU parity and the `auto` threshold were measured on a software adapter (Mesa llvmpipe through Dawn, the `webgpu` package, in Node). One real GPU in a real browser: `examples/browser-demo` in Chrome 155 on an Apple M1 (Metal 3) matched the CPU within 5.72e-6 and decoded the same on every input ([measurement](./measurements/real-gpu-2026-11-22.md)). The threshold on real devices: not measured.
|
|
6
|
+
|
|
7
|
+
## Load and run
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { availability, loadModel } from 'gpu-engine'
|
|
11
|
+
import definition from './my-model.model.ts'
|
|
12
|
+
|
|
13
|
+
await availability({ definition }) // { backend, reason }: what 'auto' would pick, before the weights load
|
|
14
|
+
const model = await loadModel(weights, { backend: 'auto', definition })
|
|
15
|
+
const [error, tags] = await model.run('12 kg to pounds')
|
|
16
|
+
if (error) console.error(error.code, error.message, error.fix)
|
|
17
|
+
model.inspect() // { backend, reason, bytes, macs, inputMax?, head? }
|
|
18
|
+
await model.destroy() // when the model is no longer needed
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`loadModel(weights, options)` never rejects, and `run` never throws. Both report failures as data.
|
|
22
|
+
|
|
23
|
+
| Option | Required | What it does |
|
|
24
|
+
| ------------ | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
25
|
+
| `backend` | yes | `'auto'`, `'webgpu'` or `'cpu'`. There is no default; anything else is `RUN_BACKEND_UNAVAILABLE` |
|
|
26
|
+
| `definition` | no | The model definition the weights were trained from. It types the result (the head's labels type the tags or the class) and must hash to the file's hash, else `WEIGHTS_DEFINITION_MISMATCH` |
|
|
27
|
+
| `gpu` | no | A WebGPU entry point for hosts without `navigator.gpu`. In Node it is `create([])` from the `webgpu` package; keep it referenced while the model lives. Typed `GpuEntryPoint` |
|
|
28
|
+
| `table` | for `retrieve` heads | The `{ id, text }` rows the model scores against, encoded once at load. Without it every `run` is `RUN_TABLE_MISSING`. Other heads ignore it |
|
|
29
|
+
|
|
30
|
+
`weights` is a `Uint8Array` or an `ArrayBuffer` in the [weights format](./weights-format.md). The file carries the definition, so nothing else is needed to run it.
|
|
31
|
+
|
|
32
|
+
A model that fails to load still resolves: every `run` returns the load error and `inspect().reason` names it.
|
|
33
|
+
|
|
34
|
+
## Results
|
|
35
|
+
|
|
36
|
+
`run(input)` returns `[error, undefined]` or `[undefined, data]`.
|
|
37
|
+
|
|
38
|
+
- `input` is a string or `{ text, flags }`, where `flags` maps each declared `features.flag({ name })` to one boolean per token of `tokenize(text)`. An input of any other shape, an undeclared flag or a flag list of the wrong length is `RUN_INPUT_INVALID`.
|
|
39
|
+
- Empty text runs and gives an empty result. Text longer than `input.max` tokens runs on its first `max` tokens; the tokens after them get no output.
|
|
40
|
+
- `tag` head: one label per kept token. `pooled` head: the label (class form) or the numbers (values form). `retrieve` head: one `{ id, score }` per table row, the highest score first, table order on a tie.
|
|
41
|
+
- Runs may overlap: `Promise.all(inputs.map((input) => model.run(input)))` gives each input the result it gets alone. On WebGPU the runs share the model's buffers, so they go one at a time, in call order.
|
|
42
|
+
- The decode step is yours: turn this output into your result in plain code. A model package's `src/decode.ts` holds it, and its `src/index.ts` wraps `loadModel` and the decode into `load({ backend, gpu? })` with `run(text)`.
|
|
43
|
+
|
|
44
|
+
An error is a diagnostic: `{ code, message, why, fix }`. Its type is `RunError`, whose `code` is a `RunCode`, one of the codes below; any other failure inside a run is `RUN_FAILED`, its message starting with the original code. Run-time codes:
|
|
45
|
+
|
|
46
|
+
| Code | When |
|
|
47
|
+
| ----------------------------- | --------------------------------------------------------------------------- |
|
|
48
|
+
| `WEIGHTS_FORMAT_INVALID` | The bytes are not a weights file |
|
|
49
|
+
| `WEIGHTS_VERSION_UNSUPPORTED` | The file is from another format version |
|
|
50
|
+
| `WEIGHTS_DEFINITION_MISMATCH` | The file does not match its own definition or the `definition` option |
|
|
51
|
+
| `RUN_BACKEND_UNAVAILABLE` | No or an unknown `backend`, or `'webgpu'` without an entry point or adapter |
|
|
52
|
+
| `RUN_GPU_FAILED` | A device or pipeline failed at load, a validation error, a lost device |
|
|
53
|
+
| `RUN_INPUT_INVALID` | The input or its flags have the wrong shape |
|
|
54
|
+
| `RUN_TABLE_MISSING` | A `retrieve` model without a valid `table` |
|
|
55
|
+
| `RUN_MODEL_DESTROYED` | A `run` called after `destroy()` |
|
|
56
|
+
| `RUN_OUTPUT_NONFINITE` | A logit or a retrieve score is not finite, so the head does not decode it |
|
|
57
|
+
| `RUN_FAILED` | Any other failure inside a backend |
|
|
58
|
+
|
|
59
|
+
[diagnostics.md](./diagnostics.md) has the why and fix of each.
|
|
60
|
+
|
|
61
|
+
## Types
|
|
62
|
+
|
|
63
|
+
`gpu-engine` exports the types a model file, an eval file and an app use, by name: the definition and builder types, the eval types (`EvalDefinition`, `EvalInput`, `Example`, `TrainOptions`, ...) and the runtime types (`LoadOptions`, `Model`, `ModelInput`, `ModelOutput`, `RunResult`, `RunError`, `RunCode`, `Inspection`, `GpuEntryPoint`, ...). The plan, weights file, training and harness file types (`ModelPlan`, `WeightsFile`, `Project`, `TriesRow`, `Report`, `ParityFile`, ...) come from `gpu-engine/harness`.
|
|
64
|
+
|
|
65
|
+
`GpuEntryPoint` is `{ requestAdapter(): Promise<object | null> }`, the one method the runtime calls on `navigator.gpu` or the `webgpu` package's `create([])`. The declarations name no WebGPU or DOM type, so an app compiled with `lib: ["es2023"]`, no `@webgpu/types` and `skipLibCheck: false` typechecks against them.
|
|
66
|
+
|
|
67
|
+
## Destroy
|
|
68
|
+
|
|
69
|
+
`await model.destroy()` frees the model's WebGPU buffers and destroys its device once the runs already called have finished. It never rejects, and a second call does nothing more. Every `run` called after it returns `RUN_MODEL_DESTROYED`; load the weights again to run the model. A CPU model holds no GPU resources, so `destroy()` only ends its runs.
|
|
70
|
+
|
|
71
|
+
## Backends
|
|
72
|
+
|
|
73
|
+
| Request | Runs on |
|
|
74
|
+
| -------- | ------------------------------------------------------------------------- |
|
|
75
|
+
| `cpu` | The CPU, always |
|
|
76
|
+
| `webgpu` | WebGPU, or `RUN_BACKEND_UNAVAILABLE` without an entry point or an adapter |
|
|
77
|
+
| `auto` | The first rule below that applies |
|
|
78
|
+
|
|
79
|
+
`auto` rules, in order:
|
|
80
|
+
|
|
81
|
+
1. No WebGPU entry point (no `gpu` option and no `navigator.gpu`): CPU.
|
|
82
|
+
2. `macsPerToken * input.max < AUTO_WEBGPU_MIN_MACS`: CPU, without opening a device, since no run of this model can reach the threshold.
|
|
83
|
+
3. The entry point finds no adapter, or the device or a pipeline fails to start: CPU.
|
|
84
|
+
4. Otherwise the model opens WebGPU and picks per run: the CPU when `macsPerToken * tokens < AUTO_WEBGPU_MIN_MACS`, WebGPU from there.
|
|
85
|
+
|
|
86
|
+
`inspect()` returns the backend of the latest run (before any run, of the load), the rule that chose it with its numbers, the byte length of the weights file, the multiply-adds per token, `inputMax`, the loaded definition's token limit (`input.max`), and `head`, its head kind (`tag`, `pooled` or `retrieve`); both are absent when loading failed. `availability({ definition, gpu? })` answers what `auto` would choose for a model of that definition before its weights load: it applies rules 1 and 2 and the adapter half of rule 3, without opening a device. For every model trained here rule 2 applies, so it answers `cpu`, as `auto` runs it. When it answers `webgpu`, its reason names the token count from which a run reaches the threshold; shorter runs still use the CPU.
|
|
87
|
+
|
|
88
|
+
### The threshold
|
|
89
|
+
|
|
90
|
+
`AUTO_WEBGPU_MIN_MACS` is 51,314,688 multiply-adds per run (`src/runtime/auto.ts`). It was measured by `scripts/bench-threshold.ts`, which runs tag models of widths 8 to 256 on inputs of 1 to 256 tokens through `loadModel` on both backends and takes the smallest count from which WebGPU won every measured cell. [measurements/auto-threshold.md](./measurements/auto-threshold.md) has the table.
|
|
91
|
+
|
|
92
|
+
That number comes from a software adapter (llvmpipe) on a shared 4-core VM, so it says nothing about a real GPU. On that machine it is the largest count measured, so `auto` runs almost every input on the CPU. Real GPU: not run. Real browser: not run. Treat it as a placeholder until someone measures real devices; use `backend: 'webgpu'` or `'cpu'` when you know which one you want.
|
|
93
|
+
|
|
94
|
+
## WebGPU in Node
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import { create } from 'webgpu'
|
|
98
|
+
|
|
99
|
+
const gpu = create([]) // keep this referenced while the model lives
|
|
100
|
+
const model = await loadModel(weights, { backend: 'webgpu', gpu, definition })
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
The `webgpu` package (pin `^0.6.2`; 0.3.8 crashes) is Dawn for Node. On Linux without a GPU it runs on Mesa lavapipe (`apt-get install mesa-vulkan-drivers`), the software adapter this repository's CI and measurements use. It is a devDependency only: a shipped model never depends on it.
|
|
104
|
+
|
|
105
|
+
## What runs where
|
|
106
|
+
|
|
107
|
+
- The CPU featurizes every input, on both backends. WebGPU receives the feature rows; there is no second featurizer.
|
|
108
|
+
- The CPU backend calls `forward`, the same function the trainer calls, so trainer and CPU logits are bitwise equal.
|
|
109
|
+
- On WebGPU each op runs its own WGSL template, all passes in one command encoder, with one readback of the logits. Weights upload once at load, as the same float32 array the CPU uses; pipelines compile at load. Softmax, argmax and the decode stay on the CPU.
|
|
110
|
+
- A `retrieve` model encodes every table row once at load with the CPU forward pass and uploads the row vectors after the weights.
|
|
111
|
+
|
|
112
|
+
`gpu-engine parity` checks a promoted model on both backends: trainer and CPU bitwise identical, CPU and WGSL within the eval's `parity.tolerance` with the same decoded result on every fixture. It writes `parity.json` with the adapter name. Parity runs in Node, so the model card always says the real browser run has not been done. On a software adapter (llvmpipe) `realGpu` is `"not run"`; on any other adapter it holds that adapter's CPU and WGSL result. See [harness.md](./harness.md#parity).
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# The sealed test set
|
|
2
|
+
|
|
3
|
+
A model package keeps three sets: `data/train/`, `data/dev/` and `data/test/`. The loop tunes against dev as often as it likes. The test set measures the model once per promotion, and it only measures anything while nothing has been tuned against it. SPEC.md sections 7 and 8 hold the rules; this page is the walkthrough.
|
|
4
|
+
|
|
5
|
+
## The three levels
|
|
6
|
+
|
|
7
|
+
| Level | What stops a read of the test set | On by default |
|
|
8
|
+
| ----- | ----------------------------------------------------------------------------------------- | ------------- |
|
|
9
|
+
| 1 | `doctor` and `loop` fail on code that names `data/test`, except `sealed('data/test/...')` | yes |
|
|
10
|
+
| 2 | Every read is appended to `reads.jsonl`, and the count is in the report and the card | yes |
|
|
11
|
+
| 3 | The test set is encrypted in the repository and only CI holds the key | no |
|
|
12
|
+
|
|
13
|
+
Levels 1 and 2 catch an honest mistake and make every read visible. They do not stop an agent that opens `data/test/main.jsonl` with `cat`. Level 3 does: once it is on, the working tree holds no plaintext test file and no key.
|
|
14
|
+
|
|
15
|
+
Level 3 trusts three things, and [What level 3 does not cover](#what-level-3-does-not-cover) says what happens when one fails: the eval file and everything it imports were reviewed before the promote workflow runs, since that code scores the decrypted rows in CI; no plaintext test file was ever committed, since git history keeps it; and nothing left in the package can rebuild the rows, which is why the test generator must go before the seal.
|
|
16
|
+
|
|
17
|
+
## Turning on level 3
|
|
18
|
+
|
|
19
|
+
Seal after the test set is final. If the eval file generates the test set, remove `test` from `defineDataset({ generate })` first, or move that generator to a script outside the package. The test set it wrote stays in `data/test/`, but with the generator and `data.seed` anyone could write the same rows again without the key, so `promote --seal` refuses while it is there (`SEAL_GENERATOR_FOUND`), and so does `doctor` on a sealed package. The train and dev generators can stay.
|
|
20
|
+
|
|
21
|
+
Then run it once per package:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
gpu-engine promote --seal --key-out ~/secrets/my-model.key
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Without `--key-out` the key goes to `~/.gpu-engine/keys/<model name>.key`. A key path inside the package or its git working tree is refused, and so is a path that exists.
|
|
28
|
+
|
|
29
|
+
The command:
|
|
30
|
+
|
|
31
|
+
1. runs the data step, so every check passes and `data.lock.json` holds the test set's hashes and fingerprints;
|
|
32
|
+
2. writes a new 256-bit key to the key file as 64 hex characters, with mode 0600, and never prints it;
|
|
33
|
+
3. encrypts each test file with AES-256-GCM to `data/test/<set>.jsonl.enc`;
|
|
34
|
+
4. records each sealed file in the lock with its SHA-256, its row count and the SHA-256 of the plaintext it replaced, and keeps the fingerprints;
|
|
35
|
+
5. sets `"level": 3` in `gate.json` and writes `.github/workflows/promote.yml`;
|
|
36
|
+
6. deletes the plaintext test files;
|
|
37
|
+
7. warns with `SEAL_HISTORY_PLAINTEXT` when git history still holds a plaintext test file (below).
|
|
38
|
+
|
|
39
|
+
Then store the key as a CI secret and remove it from the machine:
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
gh secret set GPU_ENGINE_TEST_KEY < ~/secrets/my-model.key
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Keep one copy where no agent can read it, such as a password manager, and delete the key file from every machine an agent works on. A lost key loses the test set: nothing can decrypt it.
|
|
46
|
+
|
|
47
|
+
Commit `data/test/*.enc`, `data.lock.json`, `gate.json` and `.github/workflows/promote.yml`.
|
|
48
|
+
|
|
49
|
+
## Plaintext in git history
|
|
50
|
+
|
|
51
|
+
Sealing encrypts the files in the working tree. Any commit that already holds `data/test/main.jsonl` in plaintext still serves the rows to anyone with a clone, so `promote --seal` prints `SEAL_HISTORY_PLAINTEXT` with the number of files and commits. It checks every plaintext path the test folder ever held on any branch, files since renamed or deleted included. The seal stands; the history is the leak. Two ways out:
|
|
52
|
+
|
|
53
|
+
- Purge the files from every branch and tag, then force-push and replace every clone, fork and CI cache that has the old history:
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
git filter-repo --invert-paths --path data/test/main.jsonl
|
|
57
|
+
git push --force --all && git push --force --tags
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
On a hosted repository, ask the host to drop cached views of the old commits too.
|
|
61
|
+
|
|
62
|
+
- Or treat the old rows as seen: write a new test set that was never committed in plaintext, seal it, and record in the model's SPEC.md that the earlier set was retired.
|
|
63
|
+
|
|
64
|
+
To avoid both, add `data/test/` to `.gitignore` until you seal, and seal before the first push.
|
|
65
|
+
|
|
66
|
+
## Working on a sealed package
|
|
67
|
+
|
|
68
|
+
Nothing changes for the loop. `loop`, `loop --fail`, `doctor`, `inspect`, `parity` and `promote --verify` run without the key:
|
|
69
|
+
|
|
70
|
+
- The guard's digest hashes the sealed files as they are stored and never decrypts them. Row counts and fingerprints come from the lock, so the overlap, group and vocabulary checks still compare every set with the test set.
|
|
71
|
+
- A plaintext file back in `data/test/` is not in the lock, so every command reports `DATA_HASH_CHANGED`, and `doctor` reports `SEAL_PLAINTEXT_FOUND`.
|
|
72
|
+
- `doctor` checks that the lock describes the sealed files (`SEAL_LOCK_INVALID`), that the promote workflow is there and runs `promote` and `promote --scan` (`SEAL_WORKFLOW_MISSING`) and that the eval file declares no test generator (`SEAL_GENERATOR_FOUND`).
|
|
73
|
+
- A report written before the seal still verifies: `promote --verify` and `doctor` compare its plaintext hashes with the `plaintext` field of each sealed file, and accept `gate.json` at level 3 over a report at level 2.
|
|
74
|
+
|
|
75
|
+
`promote` refuses outside CI. It checks, before it reads or prints anything:
|
|
76
|
+
|
|
77
|
+
| Condition | Code |
|
|
78
|
+
| --------------------------------------- | -------------------- |
|
|
79
|
+
| `CI` is not `true` | `PROMOTE_OUTSIDE_CI` |
|
|
80
|
+
| `GPU_ENGINE_TEST_KEY` is unset or empty | `SEAL_KEY_MISSING` |
|
|
81
|
+
| The key is not 64 hex characters | `SEAL_KEY_INVALID` |
|
|
82
|
+
| The key does not decrypt a sealed file | `SEAL_KEY_INVALID` |
|
|
83
|
+
|
|
84
|
+
None of these messages quote the key or a test row. A key that does not decrypt is still a counted read with outcome `failed` in `reads.jsonl`.
|
|
85
|
+
|
|
86
|
+
## Promoting in CI
|
|
87
|
+
|
|
88
|
+
Run the `promote` workflow from the Actions tab (or `gh workflow run promote.yml --ref <branch>`). CI has no candidate, since `.gpu-engine/` is not committed, so the workflow:
|
|
89
|
+
|
|
90
|
+
1. installs the package;
|
|
91
|
+
2. runs `gpu-engine loop --log "promote workflow"`, which trains a candidate from the committed code and data;
|
|
92
|
+
3. runs `gpu-engine promote` with `GPU_ENGINE_TEST_KEY` from the secret. It decrypts the test set in memory, scores, gates, and on a pass writes the weights, `report.json`, `gate.json`, `MODEL_CARD.md` and `RESULTS.md`;
|
|
93
|
+
4. runs `gpu-engine promote --scan` with the key. In a process of its own, without importing the package's code, it scans every file the workflow commits for test inputs: a `reads.jsonl` or `tries.jsonl` line that holds one is removed, any other file that holds one goes back to its committed version. When it cannot open the test set it puts those files back and keeps only well-formed rows in `reads.jsonl`. It exits 0 when it found nothing and 3 after a repair or a reset;
|
|
94
|
+
5. commits what changed back to the branch, `reads.jsonl` and `tries.jsonl` included. It commits after a failed promote and after a scan that exited 3 too, so the read stays counted, and the job then fails. Any other scan exit code (a refusal, or a crash before the files were confirmed safe) commits nothing.
|
|
95
|
+
|
|
96
|
+
A promotion read from the sealed files writes level 3 into `report.json` and `gate.json`, even when `gate.json` was missing or said otherwise. The promoted weights are the ones CI trained. Pull the branch afterwards; `promote --verify` then passes locally without the key.
|
|
97
|
+
|
|
98
|
+
Every shell step runs under `set -euo pipefail`. When the model package is a folder inside a larger repository, move the workflow to the repository's `.github/workflows/` and add `defaults.run.working-directory` with the package folder.
|
|
99
|
+
|
|
100
|
+
## What level 3 does not cover
|
|
101
|
+
|
|
102
|
+
- `CI=true` is a convention. The key is the guarantee: whoever can read it can decrypt the test set. Keep it in the secret store only.
|
|
103
|
+
- The lock's fingerprints are a membership oracle. Someone who guesses a test input can hash it and find it in `data.lock.json`. Fingerprints never reveal an input nobody guessed.
|
|
104
|
+
- Level 3 assumes the eval file and everything it imports were reviewed before the workflow runs. Inside the CI job that code (metric, decode, baselines) runs on decrypted rows. `promote` mutes `console`, `process.stdout` and `process.stderr`, `process._rawDebug` and the `node:fs` writers on descriptors 1 and 2 from the read until the process exits, deferred output included, and fails with `DATA_TEST_PRINTED` on any attempt to print. An unhandled rejection or uncaught exception is counted and dropped unprinted (`PROMOTE_SCORE_FAILED`). `promote` and `promote --scan` remove test inputs the code wrote into the files the workflow commits. Code that opens another file, a socket or a child process is not stopped, so review changes to that code before you run the workflow.
|
|
105
|
+
- Plaintext test files committed before the seal stay readable in git history (Plaintext in git history, above).
|
|
106
|
+
- A test generator and `data.seed` rebuild the test rows without the key. `promote --seal` and `doctor` refuse a sealed package whose eval file still declares one (`SEAL_GENERATOR_FOUND`), but a copy of the generator kept anywhere else, such as a script in another folder or an old commit of the eval file, rebuilds them too. Delete it, or keep it where no agent reads.
|
|
107
|
+
- The train and dev generators stay and run in CI. They never see a test row, but a change to them is eval code like any other: review it before the workflow runs.
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# Weights format
|
|
2
|
+
|
|
3
|
+
A gpu-engine weights file holds a model's definition and every tensor, so `loadModel(bytes)` needs nothing else. The same file runs on the CPU and on WebGPU. This page is for anyone who writes or reads the file without gpu-engine's code, such as an exporter from another trainer. [SPEC.md](../SPEC.md) section 5 (Byte layout) is the normative text; this page adds the tensor order, a worked example and an exporter checklist.
|
|
4
|
+
|
|
5
|
+
In code, `encode(file)` and `decode(bytes)` from `gpu-engine/harness` write and read it, and `encode(decode(bytes))` gives back the same bytes: the reader rejects anything the writer would not write.
|
|
6
|
+
|
|
7
|
+
## Layout
|
|
8
|
+
|
|
9
|
+
Format version 1. Little-endian. Every padding byte, reserved field and unused bit is zero. Offsets count from the start of the file.
|
|
10
|
+
|
|
11
|
+
| Offset | Bytes | Field |
|
|
12
|
+
| ------ | ----- | ----------------------------------------------------------------------------- |
|
|
13
|
+
| 0 | 4 | Magic, the ASCII bytes `GPEW` |
|
|
14
|
+
| 4 | 2 | u16 format version, `1` |
|
|
15
|
+
| 6 | 2 | u16 flags, `0` |
|
|
16
|
+
| 8 | 4 | u32 definition hash, lane 0 |
|
|
17
|
+
| 12 | 4 | u32 definition hash, lane 1 |
|
|
18
|
+
| 16 | 4 | u32 `D`, the byte length of the definition JSON |
|
|
19
|
+
| 20 | 4 | u32 `T`, the tensor count |
|
|
20
|
+
| 24 | `D` | The canonical definition JSON in UTF-8, then zero bytes up to a multiple of 4 |
|
|
21
|
+
| then | | `T` tensor records, in plan order |
|
|
22
|
+
| then | | `T` payloads, in the same order |
|
|
23
|
+
|
|
24
|
+
A tensor record. Every record starts at a multiple of 4:
|
|
25
|
+
|
|
26
|
+
| Bytes | Field |
|
|
27
|
+
| ------- | -------------------------------------------------------------------------------- |
|
|
28
|
+
| 2 | u16 `L`, the byte length of the name |
|
|
29
|
+
| `L` | The tensor name in UTF-8, then zero bytes until the record is at a multiple of 4 |
|
|
30
|
+
| 1 | u8 bit width, `6` or `32` |
|
|
31
|
+
| 1 | u8 rank `R` |
|
|
32
|
+
| 2 | u16 reserved, `0` |
|
|
33
|
+
| `4 * R` | u32 dimensions, outermost first |
|
|
34
|
+
| 4 | f32 scale: the int6 scale, `1` for float32 tensors |
|
|
35
|
+
| 4 | u32 payload bytes `P`: `ceil(6 * count / 8)` for bit width 6, `4 * count` for 32 |
|
|
36
|
+
|
|
37
|
+
A payload is `P` bytes, then zero bytes up to a multiple of 4.
|
|
38
|
+
|
|
39
|
+
- Bit width 32: the values as little-endian f32, row-major.
|
|
40
|
+
- Bit width 6: each value is stored as the code `level + 32` (1 to 63) in 6 bits, packed least significant bit first. Value `k` occupies bits `6k` to `6k + 5` of the payload, where bit `b` is bit `b mod 8` of byte `floor(b / 8)`, and bit 0 of the code comes first. Code 0 is invalid. The weight is `float32(level * scale)`.
|
|
41
|
+
|
|
42
|
+
A definition with `quant` stores every tensor at bit width 6; one without stores every tensor at bit width 32. A file never mixes them.
|
|
43
|
+
|
|
44
|
+
## Quantization
|
|
45
|
+
|
|
46
|
+
One symmetric scale per tensor: `scale = float32(max |w| / 31)`, `level = clamp(round(w / scale), -31, 31)` with JavaScript `Math.round` (halves round toward positive infinity). A tensor of zeros has scale 0 and every level 0. An exporter that trained in float must quantize this way, so that the weights the runtime decodes are the ones it scored.
|
|
47
|
+
|
|
48
|
+
## Definition JSON and hash
|
|
49
|
+
|
|
50
|
+
The JSON is the object `{ head, input, name, net, quant }` (`quant` only when the model has one; `budgets` is never part of it), keys sorted by code unit at every level, no whitespace, as `JSON.stringify` writes it. It is the definition as the builders return it, such as `{"kind":"embed","size":2}` for `layers.embed({ size: 2 })`.
|
|
51
|
+
|
|
52
|
+
The hash is two 32-bit FNV-1a lanes over the JSON's UTF-8 bytes, without the padding. Lane `j` starts at `basis_j = (2166136261 + j * 2654435769) mod 2^32`, so lane 0 starts at `0x811c9dc5` and lane 1 at `0x1f54177e`; then for each byte `h = (h XOR byte) * 16777619 mod 2^32`. As text it is 16 lowercase hex characters, lane 0 then lane 1, which is what `gpu-engine inspect`, `report.json` and `tries.jsonl` print as the definition hash.
|
|
53
|
+
|
|
54
|
+
## Tensor names and order
|
|
55
|
+
|
|
56
|
+
A tensor's name is `<op index>.<kind>.<tensor>`. The ops are the `net` layers in order, then the head, counted from 0. Tensors follow op after op, each op's in this order. `in` is the op's input width: `slots` for `embed`, the previous op's output width otherwise. A matrix `[out, in]` is row-major: element `(o, i)` is value `o * in + i`.
|
|
57
|
+
|
|
58
|
+
| Op | Tensors, in order | Output width |
|
|
59
|
+
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ |
|
|
60
|
+
| `embed({ size })` | `table [rows, size]`, `bias [size]` | `size` |
|
|
61
|
+
| `conv({ taps })` | `kernel [taps, in]`, `bias [in]` | `in` |
|
|
62
|
+
| `biScan({ size })` | `forward.gateWeight [size, in]`, `forward.gateBias [size]`, `forward.candidateWeight [size, in]`, `forward.candidateBias [size]`, the same four `backward` | `2 * size` |
|
|
63
|
+
| `dense({ size })` | `weight [size, in]`, `bias [size]` | `size` |
|
|
64
|
+
| `tag` head | `weight [labels, in]`, `bias [labels]` | `labels` |
|
|
65
|
+
| `pooled` head | `weight [out, 2 * in]`, `bias [out]`; `out` is the label count or `outputs` | `out` |
|
|
66
|
+
| `retrieve` head | `weight [size, 2 * in]`, `bias [size]` | `size` |
|
|
67
|
+
|
|
68
|
+
`rows` and `slots` come from the feature rows (SPEC.md section 5, Feature rows): `features.shape()` is 13 rows and 2 slots, `features.hash({ buckets, k })` is `buckets` rows and `k` slots, `features.flag({ name })` is 1 row and 1 slot, in list order. `plan(definition)` from `gpu-engine/harness` returns every tensor's name, shape and offset; [layers.md](./layers.md) explains what each one computes.
|
|
69
|
+
|
|
70
|
+
## Reader errors
|
|
71
|
+
|
|
72
|
+
| Code | When |
|
|
73
|
+
| ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
74
|
+
| `WEIGHTS_FORMAT_INVALID` | Wrong magic, a nonzero flag, padding or reserved byte, a length past the end or trailing bytes, invalid UTF-8 or JSON, JSON not in canonical form, a definition that fails a definition-time check other than the budget, a bit width other than 6 or 32, a payload size that does not match the shape, a non-finite or negative scale or float, or int6 code 0 |
|
|
75
|
+
| `WEIGHTS_VERSION_UNSUPPORTED` | The magic matches and the version is not 1 |
|
|
76
|
+
| `WEIGHTS_DEFINITION_MISMATCH` | The stored hash is not the hash of the stored JSON, or the tensor count, a name, a shape or a bit width differs from `plan(definition)`; also when `loadModel` gets a `definition` whose hash is not the file's |
|
|
77
|
+
|
|
78
|
+
`loadModel` returns these as the error of every `run`; it never throws. [diagnostics.md](./diagnostics.md) has the fix for each.
|
|
79
|
+
|
|
80
|
+
## Worked example
|
|
81
|
+
|
|
82
|
+
A model with one flag row, `embed({ size: 2 })` and a two-label tag head, quantized to int6:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
defineModel({
|
|
86
|
+
name: 'tiny',
|
|
87
|
+
input: features.tokens({ max: 4, rows: [features.flag({ name: 'x' })] }),
|
|
88
|
+
net: [layers.embed({ size: 2 })],
|
|
89
|
+
head: heads.tag({ labels: ['a', 'b'] }),
|
|
90
|
+
quant: int6({ qatFrom: 0.5 }),
|
|
91
|
+
budgets: { brotliBytes: 1_000, macsPerToken: 100 },
|
|
92
|
+
})
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Its canonical JSON is 198 bytes and hashes to `8c1022082663699b`:
|
|
96
|
+
|
|
97
|
+
```text
|
|
98
|
+
{"head":{"kind":"tag","labels":["a","b"]},"input":{"kind":"tokens","max":4,"rows":[{"kind":"flag","name":"x"}]},"name":"tiny","net":[{"kind":"embed","size":2}],"quant":{"kind":"int6","qatFrom":0.5}}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
The plan has 1 row and 1 slot, so four tensors: `0.embed.table [1, 2]`, `0.embed.bias [2]`, `1.tag.weight [2, 2]`, `1.tag.bias [2]`. With the float weights `[0.5, -0.25]`, `[0.1, -0.1]`, `[1, -1, 0.5, 0]` and `[0.2, -0.2]`, the table quantizes to scale `0.5 / 31` and levels `[31, -16]`, stored as the codes 63 and 16: bits `111111` then `000010` from bit 0, so the payload is `3f 04`. The whole file is 372 bytes:
|
|
102
|
+
|
|
103
|
+
```text
|
|
104
|
+
0000 47 50 45 57 01 00 00 00 08 22 10 8c 9b 69 63 26
|
|
105
|
+
0010 c6 00 00 00 04 00 00 00 7b 22 68 65 61 64 22 3a
|
|
106
|
+
0020 7b 22 6b 69 6e 64 22 3a 22 74 61 67 22 2c 22 6c
|
|
107
|
+
0030 61 62 65 6c 73 22 3a 5b 22 61 22 2c 22 62 22 5d
|
|
108
|
+
0040 7d 2c 22 69 6e 70 75 74 22 3a 7b 22 6b 69 6e 64
|
|
109
|
+
0050 22 3a 22 74 6f 6b 65 6e 73 22 2c 22 6d 61 78 22
|
|
110
|
+
0060 3a 34 2c 22 72 6f 77 73 22 3a 5b 7b 22 6b 69 6e
|
|
111
|
+
0070 64 22 3a 22 66 6c 61 67 22 2c 22 6e 61 6d 65 22
|
|
112
|
+
0080 3a 22 78 22 7d 5d 7d 2c 22 6e 61 6d 65 22 3a 22
|
|
113
|
+
0090 74 69 6e 79 22 2c 22 6e 65 74 22 3a 5b 7b 22 6b
|
|
114
|
+
00a0 69 6e 64 22 3a 22 65 6d 62 65 64 22 2c 22 73 69
|
|
115
|
+
00b0 7a 65 22 3a 32 7d 5d 2c 22 71 75 61 6e 74 22 3a
|
|
116
|
+
00c0 7b 22 6b 69 6e 64 22 3a 22 69 6e 74 36 22 2c 22
|
|
117
|
+
00d0 71 61 74 46 72 6f 6d 22 3a 30 2e 35 7d 7d 00 00
|
|
118
|
+
00e0 0d 00 30 2e 65 6d 62 65 64 2e 74 61 62 6c 65 00
|
|
119
|
+
00f0 06 02 00 00 01 00 00 00 02 00 00 00 08 21 84 3c
|
|
120
|
+
0100 02 00 00 00 0c 00 30 2e 65 6d 62 65 64 2e 62 69
|
|
121
|
+
0110 61 73 00 00 06 01 00 00 02 00 00 00 0d 68 53 3b
|
|
122
|
+
0120 02 00 00 00 0c 00 31 2e 74 61 67 2e 77 65 69 67
|
|
123
|
+
0130 68 74 00 00 06 02 00 00 02 00 00 00 02 00 00 00
|
|
124
|
+
0140 08 21 04 3d 03 00 00 00 0a 00 31 2e 74 61 67 2e
|
|
125
|
+
0150 62 69 61 73 06 01 00 00 02 00 00 00 0d 68 d3 3b
|
|
126
|
+
0160 02 00 00 00 3f 04 00 00 7f 00 00 00 7f 00 83 00
|
|
127
|
+
0170 7f 00 00 00
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Reading it: bytes 8 to 15 are the hash lanes as little-endian u32 (`0x8c102208`, `0x2663699b`); byte 16 says the JSON is `0xc6` = 198 bytes, so it ends at 222 and pads to 224 (`0xe0`); the first record names `0.embed.table` (13 bytes), pads to `0xf0`, then holds bit width 6, rank 2, dimensions 1 and 2, the scale as f32 and a payload of 2 bytes. The payloads start at `0x164`. A test in this repository (`test/docs.test.ts`) encodes this model and compares the bytes with this dump.
|
|
131
|
+
|
|
132
|
+
## Writing an exporter
|
|
133
|
+
|
|
134
|
+
1. Build the definition with gpu-engine's builders, or write the same JSON by hand, and check its plan with `plan(definition)`: it lists every tensor name, shape and offset your trainer must produce.
|
|
135
|
+
2. Train with the layer semantics in [layers.md](./layers.md). A layer that computes anything else gives logits the runtime will not reproduce.
|
|
136
|
+
3. Quantize each tensor as above, or write bit width 32 for a definition without `quant`.
|
|
137
|
+
4. Write the header, the JSON, the records and the payloads as above.
|
|
138
|
+
5. Check the file: `decode(bytes)` must accept it, and `forward(plan(definition), dequantize(decode(bytes), plan(definition)), rows, n)` from `gpu-engine/harness` must give the logits your trainer gives for the same feature rows. In a model package with the same model file, `gpu-engine inspect "<text>"` runs a file placed at `weights/<name>.bin` and prints the feature rows and the logits of one input, so you can compare them with your trainer.
|
package/llms.txt
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# gpu-engine
|
|
2
|
+
|
|
3
|
+
> A TypeScript library, with a dependency-free runtime, to build, train, evaluate and ship tiny models (a few kilobytes to about 1 MB of int6 weights) that run on WebGPU with hand-written WGSL or on the CPU, with a workflow harness that keeps a sealed test set out of the tuning loop.
|
|
4
|
+
|
|
5
|
+
The API is low-level and written for coding agents: `net` is always an explicit list of layers from a closed catalog (`embed`, `conv`, `biScan`, `dense`) followed by one head (`tag`, `pooled`, `retrieve`), and your own code decodes the head's output. Commands: `gpu-engine new`, `loop`, `promote`, `parity`, `inspect`, `doctor`, `build`. Quick start: `npx gpu-engine@latest new my-model --head tag`, then in the package `npm install`, `npx gpu-engine doctor`, `npx gpu-engine loop --log "starter"`, `npx gpu-engine promote`, `npm run build`. Requires Node 24 or later; the CPU is the v0.1 runtime and WebGPU is experimental. WebGPU parity and the CPU or WebGPU threshold were measured on a software adapter (llvmpipe); one real GPU and browser run (Chrome on an Apple M1, examples/browser-demo) matched the CPU within 5.72e-6 with the same decoded results (docs/measurements/real-gpu-2026-11-22.md).
|
|
6
|
+
|
|
7
|
+
## Docs
|
|
8
|
+
|
|
9
|
+
- [Skill](skills/gpu-engine/SKILL.md): starting layouts per head, tuning advice, the agent rules, how to read the tries log and the promoted report
|
|
10
|
+
- [Data](docs/data.md): bring your own rows, the row format per head, groups and vocab, splitting, the lock, regenerating train and dev, replacing the test set
|
|
11
|
+
- [Apps](docs/apps.md): use a built model package in an app: install, `load`, `run`, `destroy`, backends, bundlers, Node with the `webgpu` package, workers, browser support and types
|
|
12
|
+
- [Layers](docs/layers.md): what each layer and head does, its bytes and multiply-adds as formulas, when it helps and when it does not
|
|
13
|
+
- [Harness](docs/harness.md): the model package, `defineEval(model, ...)`, data and its checks, scores, loop, tries log, gate, promote, verify, doctor, inspect, parity, ship
|
|
14
|
+
- [CLI](docs/cli.md): every command and flag, what each reads and writes, exit codes
|
|
15
|
+
- [Runtime](docs/runtime.md): `loadModel`, `run`, `inspect`, `destroy`, `availability`, backends, the `auto` rule and the exported types
|
|
16
|
+
- [Sealed test set](docs/sealed-test-set.md): the three levels, `promote --seal`, and what level 3 trusts
|
|
17
|
+
- [Weights format](docs/weights-format.md): the binary layout byte by byte, with a worked example, for exporters
|
|
18
|
+
- [Diagnostics](docs/diagnostics.md): every diagnostic code with its why and fix
|
|
19
|
+
|
|
20
|
+
## Optional
|
|
21
|
+
|
|
22
|
+
- [Spec](SPEC.md): the source of truth for behaviour
|
|
23
|
+
- [Auto threshold measurement](docs/measurements/auto-threshold.md): the CPU and WebGPU timings behind `AUTO_WEBGPU_MIN_MACS`
|
|
24
|
+
- [README](README.md): requirements, quick start, status
|
package/package.json
CHANGED
|
@@ -1,6 +1,76 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gpu-engine",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Build, tune and ship tiny models for the browser and Node",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"machine-learning",
|
|
7
|
+
"tiny-models",
|
|
8
|
+
"webgpu",
|
|
9
|
+
"wgsl"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://gpu-engine.dev",
|
|
12
|
+
"bugs": "https://github.com/onmax/gpu-engine/issues",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "onmax",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/onmax/gpu-engine.git"
|
|
18
|
+
},
|
|
19
|
+
"bin": {
|
|
20
|
+
"gpu-engine": "./dist/cli.mjs"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"docs/*.md",
|
|
25
|
+
"docs/measurements",
|
|
26
|
+
"skills",
|
|
27
|
+
"templates",
|
|
28
|
+
"llms.txt",
|
|
29
|
+
"SPEC.md",
|
|
30
|
+
"THIRD_PARTY_NOTICES.md"
|
|
31
|
+
],
|
|
32
|
+
"type": "module",
|
|
33
|
+
"sideEffects": false,
|
|
34
|
+
"main": "./dist/index.mjs",
|
|
35
|
+
"types": "./dist/index.d.mts",
|
|
36
|
+
"exports": {
|
|
37
|
+
".": {
|
|
38
|
+
"types": "./dist/index.d.mts",
|
|
39
|
+
"import": "./dist/index.mjs"
|
|
40
|
+
},
|
|
41
|
+
"./harness": {
|
|
42
|
+
"types": "./dist/harness.d.mts",
|
|
43
|
+
"import": "./dist/harness.mjs"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"valibot": "^1.5.0"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@types/node": "^24.19.2",
|
|
54
|
+
"publint": "^0.3.25",
|
|
55
|
+
"typescript": "^6.0.3",
|
|
56
|
+
"vite-doctor": "^0.1.4",
|
|
57
|
+
"vite-plus": "^1.7.0",
|
|
58
|
+
"webgpu": "^0.6.2"
|
|
59
|
+
},
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": ">=24"
|
|
62
|
+
},
|
|
63
|
+
"scripts": {
|
|
64
|
+
"build": "vp pack",
|
|
65
|
+
"check": "vp check",
|
|
66
|
+
"check:fix": "vp check --fix",
|
|
67
|
+
"typecheck": "tsc --noEmit",
|
|
68
|
+
"test": "vp test run",
|
|
69
|
+
"validate": "pnpm build && publint --strict",
|
|
70
|
+
"test:gpu": "GPU_ENGINE_GPU=1 vp test run test/gpu",
|
|
71
|
+
"milestones": "node scripts/milestones.ts",
|
|
72
|
+
"leak": "node scripts/leak.ts",
|
|
73
|
+
"docs:diagnostics": "node scripts/diagnostics.ts",
|
|
74
|
+
"examples": "pnpm build && node scripts/examples.ts"
|
|
75
|
+
}
|
|
6
76
|
}
|