interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / om / lm1b / README.md

186 lines · 12454 bytes Raw Blame History Rendered
1# lm1b: a 1-billion-weight language model written in Omega
2
3`lm1b.om` is one Omega program. The seed-born Omega compiler (`omc`) turns it into one small Linux
4program for the Arm cores of the Spark. It reads the pinned Llama-3.2-1B-Instruct model file once,
5keeps every weight in its own memory, then answers request lines one after another without reloading.
6Its answers match the Rust reference engine (`crates/aienos-infer`, plain f32 path) to the last bit.
7
8The same program bytes are meant to run later with no Linux under, inside a boot fixture. This folder
9covers the hosted run only (on Linux). Part of arch#314.
10
11## What works (measured)
12
13| Check | Result | Raw log |
14|---|---|---|
15| Load: size, header, pinned manifest, full-file SHA-256 | `load ok`, 12.78 s wall (13.33 s in an earlier run) | `evidence/lm1b-load.time` |
16| Job 1: cow prompt (19 ids), 8 greedy tokens | tokens and logit bits equal the reference | `evidence/lm1b-main.out`, `evidence/ref-cow8.txt` |
17| Job 2: rainbow prompt (17 ids), 8 tokens | tokens and logit bits equal the reference | `evidence/ref-rainbow8.txt` |
18| Job 3: cow prompt again, same process | identical to job 1 (weights stayed resident) | `evidence/lm1b-main.out` |
19| Three jobs, one process | 303.92 s wall, 76 forward passes, exit 0 | `evidence/lm1b-main.time` |
20| Negative controls and the whole rerun | `gate.sh` prints `RC=0` | `evidence/gate.log` |
21
22"Logit bits" means the exact 32-bit pattern of the winning score at every step, so a match is a
23bit-for-bit match of the whole calculation, not a near match. The reference prints those bits when
24`AIENOS_BITS=1` is set; that one opt-in switch was added to `crates/aienos-infer/examples/model_probe.rs`
25on this branch for this purpose (it changes nothing else).
26
27Speed, measured on one Cortex-X925 core, hosted:
28- Load: 12.78 s wall (`evidence/lm1b-load.time`). The 50 file reads take 0.25 s of it
29 (`evidence/lm1b-load-strace.txt`); the rest is computing, mostly the SHA-256 of the whole file.
30- Forward pass: 3.824 s on average over the 76 passes of the three-job run
31 (`(303.92 - 13.33) / 76`, calc:7b4d2b843c61a5ee12038ecb8f05facfb9d0b506f083d8b07047978faa5566c7).
32- The Rust reference on the same core: 0.7146 s per generated token (`evidence/ref-cow8.txt`), so this
33 first Omega version is 5.35 times slower per pass
34 (calc:f036175030a053abf4b2c7cb963433d2f47e0f9e6874a3fc0b9e2533f30bae2b; the Omega average includes
35 prefill passes, which skip the large output matrix, so this ratio is an upper bound on the gap). No
36 attempt at speed was made yet: the goal of this slice is identical numbers.
37
38## The numeric contract
39
40Every number is computed with the same operations in the same order as the Rust reference, one IEEE
41rounding per operation, round to nearest even, no fused multiply-add (Rust never fuses; omc never fuses,
42OMEGESE.md 9.9). In detail:
43- Weights: Q4_K and Q6_K blocks are unpacked exactly as `src/quant.rs` does, and each 256-weight block's
44 products are summed in index order starting from -0.0 (Rust's `sum` for f32), then added to a row total
45 that starts at +0.0.
46- Math: `sqrt` is the reference's five Newton steps from a bit seed (not the hardware square root),
47 `exp` is its Cody-Waite reduction plus degree-6 series, `ln` its atanh series, `pow = exp(e * ln b)`.
48- The rotation angles use `%` on f32, which is an exact remainder. It is implemented exactly here with
49 whole-number long division on the mantissas (as musl's `fmodf`), not `x - trunc(x/y)*y`.
50- Constants like 1/6 are correctly rounded f32 divisions done once at start-up; pi, 2 pi, ln 2 and the
51 others are the exact f32 bit patterns Rust uses, entered with `bitcast`.
52- Argmax takes the first index on ties (strictly greater wins).
53
54## Memory budget
55
56Omega has no heap: everything is a fixed pool in the program image (at most 1 GiB of pools).
57
58| Pool | Bytes |
59|---|---|
60| The model file, whole, in 49 byte pools (48 of 16,777,216 bytes, the last 2,388,000, calc:7220fb82e9cb86995dfcefc580d79d848e22af2d8959971c81378ddd18d1187f) | 807,694,368 |
61| Attention memory (keys and values): 16 layers x 256 positions x 512 values x 4 bytes x 2 (calc:f6585eec2ae1d95b472256131dc174ca953f0d978e0ce698dedff437d9d816a3) | 16,777,216 |
62| Activations and logits (161,024 f32) | 644,096 |
63| Norm weights (67,584 f32) | 270,336 |
64| Row buffer, line buffers, tables, constants (calc:09d16d2723e799c6eb2e3874ee598e3ef9f4af941062d0e70ec3131ce356fd12) | 222,824 |
65| Total of the pools above (calc:d52ac033781d8f2cbb8c4fb37a9a81666e7c58ce68e0c4db243e77da267b46ea) | 825,608,840 |
66
67The compiled program's read-write region is 0x31b5cd50 bytes: the pools above, the 696 bytes of the
68vendored SHA-256 state and the fixed 8 MiB stack, 712 bytes over the sum above
69(calc:fe8d0bec2ca48588975eedf813c0e6a96ae84ea963c7c74e9ce200c2a17010bf), leaving 239,743,664 bytes
70under 1 GiB (calc:9901dc10ccd11f1d1c93792dd00f1ee280766343394a3c580845e77dfcdd7629). Measured peak
71resident memory: 791,568 KB (`evidence/lm1b-main.time`).
72
73A weight row is copied (at most 6,720 bytes) from its pool into the row buffer before its dot product;
74a row may cross from one pool into the next, and the copy handles that.
75
76## I/O contract v1 (the boot fixture depends on this)
77
78- fd 3 is the model, read only with `pread`. fd 0 carries requests, fd 1 carries responses. Nothing else.
79- The program uses only these Linux services: read (63), write (64), pread64 (67), exit (93). No
80 arguments, environment or clocks. `gate.sh` checks both the source words and the service numbers in
81 the compiled bytes.
82- Request line (ASCII, single spaces):
83 `job <id> gen <n> budget <steps> stop <eos> fault <0|1> ids <t1> ... <tn>`
84 - `<id>`: 1 to 32 printable characters, echoed back as written.
85 - `budget`: the most forward passes this job may use. When it runs out the job ends
86 `status cancelled` at a pass boundary; its attention memory is wiped; the weights stay; the next
87 job runs normally.
88 - `fault 1`: the program stops itself on purpose in the middle of the job (pass `n / 2`, layer 8)
89 with trap 12, so the process exits with status 112. This is for testing the supervisor's recovery.
90- Responses:
91 - once: `load ok bytes <n> tensors <n> sha256 <hex> passes 0`
92 - per job: `job <id> status <ok|cancelled> passes <n> tokens <id> ...`, then
93 `bits <u32> ...` (the f32 bits of each chosen logit, decimal), then
94 `acct job <id> rowbytes <weight bytes read> passes <n>` (counts only, no clocks).
95 - a request that cannot run: `job <id|?> status refused reason <word>`, then the next line is served.
96 Reasons: `syntax`, `toolong` (line over 65,535 bytes), `toomany` (more than 256 ids), `badid`
97 (id not in the vocabulary), `empty`, `range` (gen 0 or fault not 0/1), `context` (prompt plus
98 generated tokens would pass 256 positions). Requests are refused, never cut short. Empty lines
99 are skipped.
100 - a model that fails admission: `load refused reason <word>` and exit status 3. Reasons: `read`,
101 `size`, `magic`, `header`, `manifest`, `sha256`.
102- End of input on fd 0: exit 0.
103
104## Admission (identity of the model)
105
106At load the program checks, in order: the file is exactly 807,694,368 bytes; the header starts with
107`GGUF`; the header rebuilt as text (version, every metadata key with its type and value, every tensor
108with its name, type, dimensions and offset, the data start) is byte-for-byte equal to `manifest.txt`,
109which is pinned inside the program; and the SHA-256 of the whole file equals the pinned digest.
110`manifest.txt` was written by `oracle/manifest-dump` using the reference engine's own file parser, so
111two independent readers agree on it (`gate.sh` regenerates and compares it). Hyperparameters (16
112layers, width 2048, 32 heads, 8 key/value heads, feed-forward 8192, vocabulary 128,256, rope base
113500000, norm epsilon) are part of that manifest.
114
115## Files
116
117| File | What |
118|---|---|
119| `lm1b.in.om` | the program, hand-written (edit this) |
120| `gen.sh` | expands the template: the 49 pools and the code that names each pool (Omega cannot pass a pool to a function), the pinned manifest, tensor slots and digest |
121| `lm1b.om` | the generated program (committed; `gate.sh` checks it is in sync) |
122| `sha256.om` | SHA-256 in Omega, vendored unchanged from aien-dev/benchmarks-mv `om/data/sha256.om` at 567c5da |
123| `manifest.txt` | the pinned model manifest |
124| `oracle/manifest-dump/` | small Rust tool that prints the manifest with the reference parser |
125| `prompts/` | the two prompts as token ids and the three-job request file |
126| `gate.sh` | reruns everything from a clean checkout and prints `RC=0` |
127| `evidence/` | raw logs of the runs quoted here |
128
129Run: `sh gen.sh && omc < lm1b.om > lm1b && chmod +x lm1b && ./lm1b 3< MODEL < prompts/main-jobs.txt`.
130Gate: `sh gate.sh` (needs the pinned omc, the pinned model and cargo; it copies the model once for the
131flipped-byte test). From a clean checkout it took 501.75 s and printed `RC=0` (`evidence/gate.log`; its
132raw outputs are in `evidence/gate/`). The ELF it built has the same SHA-256 as the recorded run
133(`evidence/gate/lm1b.sha256`, `evidence/shas.txt`), so the build is reproducible.
134
135## Negative controls (all in `gate.sh`)
136
137- One byte of tensor data flipped in a copy of the model: `load refused reason sha256`, exit 3.
138- A copy of the program whose manifest names type 14 instead of 12 for one tensor: `load refused reason
139 manifest`, exit 3.
140- 257 ids: `toomany`. 250 ids with gen 8: `context`. Id 128256: `badid`. Two malformed lines: `syntax`.
141 The process keeps serving after each.
142- Budget 5 on a 19-id prompt: `status cancelled passes 5`, then the next job's tokens and bits equal the
143 reference.
144- `fault 1`: exit status 112 and no reply line for that job.
145- Bounds traps (aienux#481): three copies of the program each change one line of `fn mv`. One reads a row 100
146 bytes before the end of the model, so the copy runs past the 2,388,000-byte last pool. One copies a row
147 one byte longer than `rb`. One rewrites the line with the same value as a control. The first two exit
148 103 (trap 3) with no reply line. The control answers with the reference's first two tokens. See
149 "Bounds checks" below.
150
151## Bounds checks
152
153`rowcp` splits a row across two pools and assumes the next pool is long enough. With the pinned tensor
154offsets this always holds; if it did not, the fallback is the compiler's per-access check. omc has no
155option to turn that check off. It reads the program on stdin and takes no arguments, and `elemaddr`
156(`seed/omc/omc.om` at omega-mv 0d72560a, the source of the pinned omc) emits `CMP` plus `B.HS` to trap 3
157for every run-time index of a `get` or `put`. A constant index outside the array is compile error 21. The
158boot fixture is meant to run these same ELF bytes, so the checks travel with them. gate.sh step 8e shows
159them firing in this program, for both `get` and `put`. [INFERENCE] until the fixture exists: a fixture
160that rebuilt the program with another compiler or another pin would need step 8e rerun against it.
161
162## Limits
163
164- CPU only, one core, scalar code. No GPU, no SIMD, no threads.
165- Hosted on Linux only so far; the boot fixture is separate work.
166- The tokenizer is outside: prompts arrive as token ids (made with the reference tokenizer).
167- 256 positions per job.
168- This is the plain f32 path of the reference, not llama.cpp's int8-activation path; on the cow prompt
169 both give the same 8 tokens (`crates/aienos-infer/tests/fixtures/ref_cow.txt`).
170
171## Compiler findings (recorded for the omc owner, ledger L408)
172
173- A function declared `to f32` is refused with error 5 (`fn f to f32` / `ret` of an f32), although
174 OMEGESE.md 9.9 lists floats as allowed returns; f64 returns work. Here every float helper returns the
175 f32 bits as `uint` and the caller uses `bitcast`.
176- A function may use at most 16 registers: parameters, every name alive in the current block, and the
177 temporaries of the current statement. An access to an array of 4,096 or more elements uses one extra
178 register. Large functions were split for this.
179- A local name may not equal a global's name (error 10), so pool names `m0`..`m48` are taken.
180
181## Provenance
182
183omc sha256 734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61 (born by
184`sh seed/omega-from-seed.sh` on aien-dev/omega-mv main 0d72560a). Model sha256
1853f5a22426976ab26cfe84dba63c1d08391717abb1af893e10f1b2968d862dcc1. The ELF and source hashes of the
186recorded run are in `evidence/shas.txt`.