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.
Files changed (54) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +96 -2
  3. package/SPEC.md +988 -0
  4. package/THIRD_PARTY_NOTICES.md +29 -0
  5. package/dist/cli.d.mts +1 -0
  6. package/dist/cli.mjs +4 -0
  7. package/dist/harness.d.mts +132 -0
  8. package/dist/harness.mjs +1 -0
  9. package/dist/index.d.mts +98 -0
  10. package/dist/index.mjs +1 -0
  11. package/dist/load-o_thEAKC.mjs +258 -0
  12. package/dist/scaffold-CY5xhy-V.mjs +106 -0
  13. package/dist/types-BwigqFwr.d.mts +868 -0
  14. package/docs/apps.md +108 -0
  15. package/docs/cli.md +126 -0
  16. package/docs/data.md +101 -0
  17. package/docs/diagnostics.md +509 -0
  18. package/docs/harness.md +191 -0
  19. package/docs/layers.md +249 -0
  20. package/docs/measurements/auto-threshold.json +335 -0
  21. package/docs/measurements/auto-threshold.md +56 -0
  22. package/docs/measurements/real-gpu-2026-11-22.json +4718 -0
  23. package/docs/measurements/real-gpu-2026-11-22.md +75 -0
  24. package/docs/runtime.md +112 -0
  25. package/docs/sealed-test-set.md +107 -0
  26. package/docs/weights-format.md +138 -0
  27. package/llms.txt +24 -0
  28. package/package.json +73 -3
  29. package/skills/gpu-engine/SKILL.md +117 -0
  30. package/templates/common/AGENTS.md +27 -0
  31. package/templates/common/github/workflows/ci.yml +35 -0
  32. package/templates/common/gitignore +3 -0
  33. package/templates/common/package.template.json +27 -0
  34. package/templates/common/tsconfig.template.json +17 -0
  35. package/templates/pooled/NEW_MODEL_NAME.eval.ts +93 -0
  36. package/templates/pooled/NEW_MODEL_NAME.model.ts +11 -0
  37. package/templates/pooled/SPEC.md +31 -0
  38. package/templates/pooled/src/decode.ts +10 -0
  39. package/templates/pooled/src/index.ts +44 -0
  40. package/templates/pooled/src/weights.ts +4 -0
  41. package/templates/retrieve/NEW_MODEL_NAME.eval.ts +149 -0
  42. package/templates/retrieve/NEW_MODEL_NAME.model.ts +12 -0
  43. package/templates/retrieve/SPEC.md +33 -0
  44. package/templates/retrieve/data/table.jsonl +16 -0
  45. package/templates/retrieve/src/decode.ts +9 -0
  46. package/templates/retrieve/src/index.ts +45 -0
  47. package/templates/retrieve/src/table.ts +21 -0
  48. package/templates/retrieve/src/weights.ts +4 -0
  49. package/templates/tag/NEW_MODEL_NAME.eval.ts +114 -0
  50. package/templates/tag/NEW_MODEL_NAME.model.ts +11 -0
  51. package/templates/tag/SPEC.md +31 -0
  52. package/templates/tag/src/decode.ts +25 -0
  53. package/templates/tag/src/index.ts +44 -0
  54. package/templates/tag/src/weights.ts +4 -0
@@ -0,0 +1,16 @@
1
+ {"id":"tomato-soup","text":"tomato soup"}
2
+ {"id":"onion-soup","text":"onion soup"}
3
+ {"id":"pea-soup","text":"pea soup"}
4
+ {"id":"mushroom-soup","text":"mushroom soup"}
5
+ {"id":"tomato-salad","text":"tomato salad"}
6
+ {"id":"potato-salad","text":"potato salad"}
7
+ {"id":"bean-salad","text":"bean salad"}
8
+ {"id":"egg-salad","text":"egg salad"}
9
+ {"id":"apple-pie","text":"apple pie"}
10
+ {"id":"cherry-pie","text":"cherry pie"}
11
+ {"id":"mushroom-pie","text":"mushroom pie"}
12
+ {"id":"potato-pie","text":"potato pie"}
13
+ {"id":"apple-cake","text":"apple cake"}
14
+ {"id":"carrot-cake","text":"carrot cake"}
15
+ {"id":"lemon-cake","text":"lemon cake"}
16
+ {"id":"banana-cake","text":"banana cake"}
@@ -0,0 +1,9 @@
1
+ // Turns the ranking into the result an app uses: the id of the best row. The eval scores this result, and the shipped
2
+ // entry returns it.
3
+ import type { RetrieveMatch } from 'gpu-engine'
4
+
5
+ export type Result = string
6
+
7
+ export function decode(ranking: readonly RetrieveMatch[]): Result {
8
+ return ranking[0]?.id ?? ''
9
+ }
@@ -0,0 +1,45 @@
1
+ // The shipped entry: the promoted weights, the table, the runtime and the decode step. `npm run build` bundles it into
2
+ // dist/index.mjs, one module with no imports, that runs in browsers and in Node.
3
+ import { loadModel } from 'gpu-engine'
4
+ import type { BackendRequest, GpuEntryPoint, Inspection, RunResult } from 'gpu-engine'
5
+ import type model from '../NEW_MODEL_NAME.model.ts'
6
+ import { decode } from './decode.ts'
7
+ import type { Result } from './decode.ts'
8
+ import { table } from './table.ts'
9
+ import { weightsHex } from './weights.ts'
10
+
11
+ export type { Result } from './decode.ts'
12
+
13
+ export interface LoadOptions {
14
+ /** `auto` picks WebGPU or the CPU per run; `inspect()` says which and why. */
15
+ readonly backend: BackendRequest
16
+ /** A WebGPU entry point where `navigator.gpu` is missing, such as Node with the `webgpu` package. */
17
+ readonly gpu?: GpuEntryPoint
18
+ }
19
+
20
+ export interface Loaded {
21
+ /** Never throws: `[error, undefined]` or `[undefined, result]`. */
22
+ run(text: string): Promise<RunResult<Result>>
23
+ inspect(): Inspection
24
+ /** Frees the GPU buffers and device once started runs finish; later runs return `RUN_MODEL_DESTROYED`. */
25
+ destroy(): Promise<void>
26
+ }
27
+
28
+ function bytes(hex: string): Uint8Array {
29
+ return Uint8Array.from({ length: hex.length / 2 }, (_, index) =>
30
+ Number.parseInt(hex.slice(index * 2, index * 2 + 2), 16),
31
+ )
32
+ }
33
+
34
+ /** Loads the promoted model and encodes the table once. Never rejects: a model that cannot load returns its error. */
35
+ export async function load(options: LoadOptions): Promise<Loaded> {
36
+ const runtime = await loadModel<typeof model>(bytes(weightsHex), { ...options, table })
37
+ return {
38
+ async run(text) {
39
+ const [error, ranking] = await runtime.run(text)
40
+ return error === undefined ? [undefined, decode(ranking)] : [error, undefined]
41
+ },
42
+ inspect: () => runtime.inspect(),
43
+ destroy: () => runtime.destroy(),
44
+ }
45
+ }
@@ -0,0 +1,21 @@
1
+ // Written by `gpu-engine build` from data/table.jsonl, the table the eval names. Change that file, not this one.
2
+ import type { TableRow } from 'gpu-engine'
3
+
4
+ export const table: readonly TableRow[] = [
5
+ { id: 'tomato-soup', text: 'tomato soup' },
6
+ { id: 'onion-soup', text: 'onion soup' },
7
+ { id: 'pea-soup', text: 'pea soup' },
8
+ { id: 'mushroom-soup', text: 'mushroom soup' },
9
+ { id: 'tomato-salad', text: 'tomato salad' },
10
+ { id: 'potato-salad', text: 'potato salad' },
11
+ { id: 'bean-salad', text: 'bean salad' },
12
+ { id: 'egg-salad', text: 'egg salad' },
13
+ { id: 'apple-pie', text: 'apple pie' },
14
+ { id: 'cherry-pie', text: 'cherry pie' },
15
+ { id: 'mushroom-pie', text: 'mushroom pie' },
16
+ { id: 'potato-pie', text: 'potato pie' },
17
+ { id: 'apple-cake', text: 'apple cake' },
18
+ { id: 'carrot-cake', text: 'carrot cake' },
19
+ { id: 'lemon-cake', text: 'lemon cake' },
20
+ { id: 'banana-cake', text: 'banana cake' },
21
+ ]
@@ -0,0 +1,4 @@
1
+ // Replaced by `gpu-engine promote` with the promoted weights. Until then the shipped entry has no model to load.
2
+
3
+ /** The promoted weights file, lowercase hex. Empty until the first promotion. */
4
+ export const weightsHex: string = ''
@@ -0,0 +1,114 @@
1
+ // How the model is trained and judged. The generators write each set the first time `loop` runs. Every set has its
2
+ // own sentence frames (its groups) and its own people's names (its vocab), so dev and test hold sentences and names the
3
+ // model never trained on; the verbs, the items, the numbers and the words before an item are shared.
4
+ import { defineDataset, defineEval, sealed, tokenize } from 'gpu-engine'
5
+ import type { Example, Random } from 'gpu-engine'
6
+ import { decode } from './src/decode.ts'
7
+ import type { Result, Tag } from './src/decode.ts'
8
+ import model from './NEW_MODEL_NAME.model.ts'
9
+
10
+ type Frames = Readonly<Record<string, readonly string[]>>
11
+
12
+ const items = [
13
+ 'apples',
14
+ 'pens',
15
+ 'chairs',
16
+ 'cups',
17
+ 'books',
18
+ 'boxes',
19
+ 'lamps',
20
+ 'plates',
21
+ 'shirts',
22
+ 'eggs',
23
+ 'keys',
24
+ 'mugs',
25
+ ]
26
+ const modifiers = ['more', 'extra', 'spare', 'small', 'big', 'old', 'new', 'clean']
27
+
28
+ function pick<T>(random: Random, list: readonly T[]): T {
29
+ const item = list[Math.floor(random() * list.length)]
30
+ if (item === undefined) throw new Error('pick from an empty list')
31
+ return item
32
+ }
33
+
34
+ const kept = ['bought', 'ordered', 'got', 'took']
35
+ const dropped = ['returned', 'lost', 'sold', 'broke']
36
+
37
+ /**
38
+ * Sentences about what someone kept and, in some, what they dropped. `{kept}` and `{dropped}` are a verb, a count and
39
+ * an item, with a modifier before half the items; only the kept count and item are tagged.
40
+ */
41
+ function sentences(frames: Frames, names: readonly string[], size: number) {
42
+ return (random: Random): Example<Result>[] =>
43
+ Array.from({ length: size }, () => {
44
+ const [group, frame] = pick(random, Object.entries(frames))
45
+ const name = pick(random, names)
46
+ const words: [string, Tag][] = []
47
+ for (const slot of frame) {
48
+ if (slot === '{name}') words.push([name, 'other'])
49
+ else if (slot === '{kept}' || slot === '{dropped}') {
50
+ const tagged = slot === '{kept}'
51
+ words.push([pick(random, tagged ? kept : dropped), 'other'])
52
+ words.push([String(2 + Math.floor(random() * 98)), tagged ? 'count' : 'other'])
53
+ if (random() < 0.5) words.push([pick(random, modifiers), 'other'])
54
+ words.push([pick(random, items), tagged ? 'item' : 'other'])
55
+ } else words.push([slot, 'other'])
56
+ }
57
+ return { input: words.map(([word]) => word).join(' '), gold: words.map(([, tag]) => tag), group, vocab: [name] }
58
+ })
59
+ }
60
+
61
+ /** The rule baseline: the first number is the count and the word after it is the item. */
62
+ function rules(input: string): Result {
63
+ const words = tokenize(input).map((token) => token.text)
64
+ const at = words.findIndex((word) => /^\p{Nd}+$/u.test(word))
65
+ if (at === -1) return { count: null, item: null }
66
+ return { count: Number(words[at]), item: words[at + 1] ?? null }
67
+ }
68
+
69
+ export default defineEval(model, {
70
+ sets: { train: 'data/train/*.jsonl', dev: 'data/dev/*.jsonl', test: sealed('data/test/*.jsonl') },
71
+ data: defineDataset<Result>({
72
+ seed: 1,
73
+ minSize: { train: 600, dev: 120, test: 120 },
74
+ generate: {
75
+ train: sentences(
76
+ {
77
+ 'train-one': ['{name}', '{kept}'],
78
+ 'train-one-party': ['{name}', '{kept}', 'for', 'the', 'party'],
79
+ 'train-and': ['{name}', '{kept}', 'and', '{dropped}'],
80
+ 'train-and-back': ['{name}', '{dropped}', 'and', '{kept}'],
81
+ 'train-then': ['{name}', '{dropped}', 'then', '{kept}', 'today'],
82
+ 'train-after': ['after', 'lunch', '{name}', '{kept}', 'and', '{dropped}'],
83
+ },
84
+ ['Ana', 'Ben', 'Chloe', 'Dara', 'Eli', 'Fay', 'Gus', 'Hana', 'Ivo', 'Jade'],
85
+ 600,
86
+ ),
87
+ dev: sentences(
88
+ {
89
+ 'dev-one': ['yesterday', '{name}', '{kept}'],
90
+ 'dev-but': ['{name}', '{kept}', 'but', '{dropped}'],
91
+ 'dev-but-back': ['{name}', '{dropped}', 'but', '{kept}', 'later'],
92
+ },
93
+ ['Kai', 'Lena', 'Milo', 'Nora'],
94
+ 120,
95
+ ),
96
+ test: sentences(
97
+ {
98
+ 'test-one': ['{name}', '{kept}', 'this', 'morning'],
99
+ 'test-also': ['{name}', '{dropped}', 'and', 'also', '{kept}'],
100
+ 'test-also-back': ['{name}', '{kept}', 'and', 'also', '{dropped}'],
101
+ },
102
+ ['Omar', 'Pia', 'Quinn', 'Rosa'],
103
+ 120,
104
+ ),
105
+ },
106
+ }),
107
+ train: { seeds: [1, 2, 3], epochs: 12, batch: 16, learningRate: 0.02, warmup: 0.1, budgetSeconds: 30 },
108
+ decode,
109
+ metric: (pred, gold) => (pred.count === gold.count && pred.item === gold.item ? 1 : 0),
110
+ baselines: { rules },
111
+ gate: { floors: 'promoted', noise: 0.05 },
112
+ parity: { tolerance: 1e-3 },
113
+ ship: { brotliBytes: 40_000, files: ['dist/index.mjs', 'dist/index.d.mts'] },
114
+ })
@@ -0,0 +1,11 @@
1
+ // The model: what it reads, its layers and its head. SPEC.md says what it is for; a change to that lands there first.
2
+ import { defineModel, features as f, heads as h, int6, layers as l } from 'gpu-engine'
3
+
4
+ export default defineModel({
5
+ name: 'NEW_MODEL_NAME',
6
+ input: f.tokens({ max: 16, rows: [f.shape(), f.hash({ buckets: 256, k: 2 })] }),
7
+ net: [l.embed({ size: 16 }), l.conv({ taps: 3 }), l.dense({ size: 16, activation: 'relu' })],
8
+ head: h.tag({ labels: ['count', 'item', 'other'] }),
9
+ quant: int6({ qatFrom: 0.7 }),
10
+ budgets: { brotliBytes: 8_000, macsPerToken: 2_000 },
11
+ })
@@ -0,0 +1,31 @@
1
+ # NEW_MODEL_NAME spec
2
+
3
+ This file is the source of truth for this model. A change to what it does lands here first, then in the eval, then in the model or the data.
4
+
5
+ ## Task
6
+
7
+ Read a short sentence about things someone got, and sometimes gave up, and return `{ count, item }`: the number and the thing they got.
8
+
9
+ | Input | Result |
10
+ | ------------------------------------------- | ------------------------------- |
11
+ | `Ana bought 12 apples` | `{ count: 12, item: 'apples' }` |
12
+ | `Ben returned 3 pens and got 40 spare cups` | `{ count: 40, item: 'cups' }` |
13
+ | `Chloe ordered 5 old lamps and lost 2 keys` | `{ count: 5, item: 'lamps' }` |
14
+
15
+ The model is a `tag` head: one label per token, `count`, `item` or `other`. Only the count and item of a verb such as `bought`, `ordered`, `got` or `took` are tagged; those after `returned`, `lost`, `sold` or `broke` are `other`. `src/decode.ts` turns the tags into the result.
16
+
17
+ ## Data
18
+
19
+ `NEW_MODEL_NAME.eval.ts` generates three sets with a fixed seed the first time `gpu-engine loop` runs: train, dev and the sealed test set. Each set uses its own sentence frames, recorded as the row's `group`, and its own people's names, recorded as the row's `vocab`, so the data checks prove that dev and test hold frames and names the model never trained on. The verbs, the items, the numbers and a word such as `spare` before the item are shared by every set.
20
+
21
+ ## Judged by
22
+
23
+ - Metric: 1 when both `count` and `item` are right, else 0. The set score is the mean.
24
+ - Rule baseline `rules`: the first number is the count and the word after it is the item. It fails when the first number was given up, or when a word such as `spare` comes between the number and the item.
25
+ - The model must beat the baseline on dev before a promotion is worth it. The oracle row shows the ceiling the decode step allows.
26
+ - The gate: each set's score at least the promoted score minus `noise`, within the budgets in `NEW_MODEL_NAME.model.ts`.
27
+ - Room to tune: the starter model reads one word to each side (`conv`). An item's tag depends on the verb two or three words before it, so the first `loop` misses many of the sentences that name two things. Adding `biScan` lets each token read the whole sentence.
28
+
29
+ ## Ship
30
+
31
+ `gpu-engine promote` writes `weights/NEW_MODEL_NAME.bin` and `src/weights.ts`. `npm run build` writes `dist/index.mjs` and its types, `dist/index.d.mts`; the module exports `load({ backend, gpu? })`; `run(text)` returns `[error, result]` and never throws.
@@ -0,0 +1,25 @@
1
+ // Turns the tags into the result an app uses. The eval scores this result, and the shipped entry returns it.
2
+ import { tokenize } from 'gpu-engine'
3
+ import type model from '../NEW_MODEL_NAME.model.ts'
4
+
5
+ export type Tag = (typeof model)['head']['labels'][number]
6
+
7
+ export interface Result {
8
+ /** The first number tagged `count`. */
9
+ readonly count: number | null
10
+ /** The first word tagged `item`. */
11
+ readonly item: string | null
12
+ }
13
+
14
+ export function decode(tags: readonly Tag[], input: string): Result {
15
+ const tokens = tokenize(input)
16
+ let count: number | null = null
17
+ let item: string | null = null
18
+ for (const [index, tag] of tags.entries()) {
19
+ const text = tokens[index]?.text
20
+ if (text === undefined) break
21
+ if (tag === 'count' && count === null && /^\p{Nd}+$/u.test(text)) count = Number(text)
22
+ if (tag === 'item' && item === null) item = text
23
+ }
24
+ return { count, item }
25
+ }
@@ -0,0 +1,44 @@
1
+ // The shipped entry: the promoted weights, the runtime and the decode step. `npm run build` bundles it into
2
+ // dist/index.mjs, one module with no imports, that runs in browsers and in Node.
3
+ import { loadModel } from 'gpu-engine'
4
+ import type { BackendRequest, GpuEntryPoint, Inspection, RunResult } from 'gpu-engine'
5
+ import type model from '../NEW_MODEL_NAME.model.ts'
6
+ import { decode } from './decode.ts'
7
+ import type { Result } from './decode.ts'
8
+ import { weightsHex } from './weights.ts'
9
+
10
+ export type { Result } from './decode.ts'
11
+
12
+ export interface LoadOptions {
13
+ /** `auto` picks WebGPU or the CPU per run; `inspect()` says which and why. */
14
+ readonly backend: BackendRequest
15
+ /** A WebGPU entry point where `navigator.gpu` is missing, such as Node with the `webgpu` package. */
16
+ readonly gpu?: GpuEntryPoint
17
+ }
18
+
19
+ export interface Loaded {
20
+ /** Never throws: `[error, undefined]` or `[undefined, result]`. */
21
+ run(text: string): Promise<RunResult<Result>>
22
+ inspect(): Inspection
23
+ /** Frees the GPU buffers and device once started runs finish; later runs return `RUN_MODEL_DESTROYED`. */
24
+ destroy(): Promise<void>
25
+ }
26
+
27
+ function bytes(hex: string): Uint8Array {
28
+ return Uint8Array.from({ length: hex.length / 2 }, (_, index) =>
29
+ Number.parseInt(hex.slice(index * 2, index * 2 + 2), 16),
30
+ )
31
+ }
32
+
33
+ /** Loads the promoted model. Never rejects: a model that cannot load returns its error from every `run`. */
34
+ export async function load(options: LoadOptions): Promise<Loaded> {
35
+ const runtime = await loadModel<typeof model>(bytes(weightsHex), options)
36
+ return {
37
+ async run(text) {
38
+ const [error, tags] = await runtime.run(text)
39
+ return error === undefined ? [undefined, decode(tags, text)] : [error, undefined]
40
+ },
41
+ inspect: () => runtime.inspect(),
42
+ destroy: () => runtime.destroy(),
43
+ }
44
+ }
@@ -0,0 +1,4 @@
1
+ // Replaced by `gpu-engine promote` with the promoted weights. Until then the shipped entry has no model to load.
2
+
3
+ /** The promoted weights file, lowercase hex. Empty until the first promotion. */
4
+ export const weightsHex: string = ''