interplane.Get Omega
Read in your language

omega-feel

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

omega-feel / SPEC.md

285 lines · 20922 bytes Raw Blame History Rendered
1# feel: Omega tunes its own kernels while a model runs
2
3Status: the decision core is specified here and checked against Wolfram (section 5). Wiring into an
4inference engine and every GPU measurement are NOT_RUN (section 7). What the core does not do, and what
5its checks do not show, is section 6: read it before relying on any sentence of sections 1 to 3.
6
7## 1. Intent (Drake, 2026-11-18, verbatim)
8
9> "build omega to be able to dynamically adjust kernels to optimize speed while the model is running."
10> "I want omega to be able to feel enough to be able to adjust itself to make itself as fast as
11> possible and use wolfram to verify."
12> "I want Omega to go the other way. It should be able to adapt to whatever model is there."
13
14In one line: while any model runs, Omega measures its own kernels, moves to the faster way of running
15each one, and every decision can be recomputed in Wolfram from the recorded timings. Scope of "never
16runs a way that gives different results": a way is not run as the incumbent until its output was bit-for-bit equal
17to the reference's on `V` launches it was shadowed on (3.2), and one unequal output bans it. After it is
18admitted its output is not checked again by this core (section 6, item 1).
19
20## 2. Shape
21
22Three parts, and only the middle one is in this repository so far.
23
24- **Senses** (the engine): the time each kernel launch took, and whether a candidate's output equals
25 the reference output on the same input (2.1).
26- **feel** (this repository, seeded Omega): decides, per class, which arm runs next.
27- **Muscles** (the engine): the settings a launch can take without a rebuild.
28
29Nothing in feel knows a model. A **class** is whatever the engine says is "the same work": a kernel
30kind plus its shape (rows, columns, element type, batch). A new model brings new classes; feel makes
31a table entry the first time it sees one, and no table has a fixed size (section 4, limits). An **arm**
32is one way to run a class: a kernel variant or a launch setting. Arm 0 is always the reference the
33engine would have used without feel. A class has 2 or more arms.
34
35### 2.1 Terms
36
37- **Equal outputs.** Bitwise: every byte of every output buffer the kernel writes is the same, so the
38 same values, the same signs of zero and the same NaN payloads. The engine reports 1 to `verdict` only
39 for that. There is no tolerance in this core, for any dtype. A tolerance would be a change to what
40 "never runs a way that gives different results" means; it is Drake's decision, not an engine option.
41- **An eligible arm** is one the engine can show gives bitwise-equal output to arm 0 on every input,
42 by construction (it performs each output element's arithmetic in the same order). Eligible: launch
43 settings that do not change that order (grid and block shape of an element-wise or gather kernel,
44 vector width of loads and stores, buffer placement or caching), a tile shape whose per-output
45 accumulation order is the same. Not eligible: a split-K, a different warp or tree reduction, atomics,
46 any variant that changes the order of a floating-point reduction. Such a variant would be banned by
47 its first unequal launch, which is the intended behaviour. An arm that is not bitwise-reproducible
48 against itself is banned the same way. Whether enough eligible arms exist to speed up a real model is
49 NOT_RUN (section 7).
50- **A trial** is any launch in which a challenger runs: a shadow launch (3.2, run into scratch memory in
51 addition to the real launch) or a timed launch of a duel (3.3). The cost of a trial is whatever its
52 time turns out to be; the core cannot know it beforehand (section 6, item 2).
53
54## 3. The rules (exact; all arithmetic is on non-negative integers)
55
56Constants: `H = 12`, `NMIN = 32`, `NMAX = 256`, `V = 3`, `WARM = 64`, `REST0 = 1024`,
57`RESTMAX = 65536`, `MNUM = 63`, `MDEN = 64` (the `64 *` and `63 *` of adopt by mean below).
58`tests/run.sh` requires each of these to be a `const` of `feel.om` with this value.
59
60State of one class: `K` arms; `status[a]` in {0 untested, 1 admitted, 2 banned}, `status[0] = 1`;
61incumbent `inc = 0`; `mode` in {rest, shadow, duel}, starting at rest; `rest = WARM`;
62`restlen = REST0`; round-robin pointer `next = 0`; challenger `ch`; `vcount`; duel counters
63`k, w, l, sI, sC, hI, hC, n`; `tfirst`; order bit `o`; generator `z = 1`; launch counter `i = 0`;
64`counts[a] = 0`.
65
66Each launch of the class is one step, in this order.
67
68**3.1 Choose.** `i += 1`. The arm to run is `inc` unless the duel rule below says otherwise.
69- mode rest, `rest > 0`: `rest -= 1`.
70- mode rest, `rest = 0`: look at arms `(next + s) mod K` for `s = 1..K` and take the first that is
71 not `inc` and not banned. If there is none: `rest = restlen`. Otherwise it becomes `ch` and
72 `next = ch`; if `status[ch] = 0` the mode becomes shadow with `vcount = 0`, else a duel starts.
73- mode shadow (also when just entered): the engine runs `inc` for real and also runs `ch` on the same
74 input into scratch memory, and reports whether the two outputs are equal.
75- mode duel (also when just entered): if `k` is even, `z = (z * 1103515245 + 12345) mod 2^31` and
76 `o` = bit 16 of `z`. The arm is `inc` when (`k` even and `o = 0`) or (`k` odd and `o = 1`), else
77 `ch`. This launch is timed.
78- `counts[arm] += 1`.
79
80A duel starts with `k = w = l = sI = sC = hI = hC = n = 0`.
81
82**3.2 Verdict** (only in shadow mode). Not equal: `status[ch] = 2`, event `B`, mode rest,
83`rest = restlen`. Equal: `vcount += 1`; at `vcount = V`: `status[ch] = 1`, event `M`, and a duel
84starts (the launch that carried the last verdict is not part of it).
85
86**3.3 Observe** (only for a timed launch; `t` is its time). `k` even: `tfirst = t`. `k` odd: the
87pair is complete; `tC` is the challenger's time and `tI` the incumbent's; `sI += tI`; `sC += tC`;
88`w += 1` if `tC < tI`; `l += 1` if `tC > tI`; `n += 1`; if `n = NMAX / 2` then `hI = sI`, `hC = sC`.
89Then the first of these that holds ends the duel:
901. `n >= NMIN` and `l - w >= H` and `sC >= sI`: **reject** (event `R`).
912. `n >= NMAX`. With the first half `(hI, hC)` and the second half `(sI - hI, sC - hC)`:
92 - `hC < hI` and `sC - hC < sI - hI` and `w - l >= H`: **adopt** (event `A`);
93 - otherwise `64 * hC < 63 * hI` and `64 * (sC - hC) < 63 * (sI - hI)`: **adopt by mean**
94 (event `Am`);
95 - otherwise **draw** (event `D`).
96
97Adopt: `inc = ch`, `restlen = REST0`. Reject or draw: `restlen = min(2 * restlen, RESTMAX)`. In both
98cases `rest = restlen` and the mode is rest. Finally `k += 1`.
99
100Why these rules:
101- Pairs, in an order the launch schedule cannot line up with, so that drift and periodic disturbance
102 fall on both arms alike.
103- Losses can reject a challenger early after the minimum pair count. Adoption by count waits
104 for the last pair. Section 5 models these endpoints assuming both sum conditions agree with
105 the count and each pair has a winner. The separate full-rule simulation includes the sum
106 conditions and adoption by mean.
107- The sums say what throughput cares about, the mean. A slower challenger is turned away early,
108 as soon as the count and the sums agree. Nothing is adopted early: an arm that is usually faster
109 but sometimes stalls looks best in a short duel, so adoption waits for all `NMAX` pairs and needs
110 the challenger's sum to be lower in both halves, with either the count on its side or a margin of
111 1/64 in both halves. One outside stall cannot decide a duel, because it falls in one half only. A
112 stall rarer than about one launch in `NMAX` can still go unseen by a whole duel (section 6, item 3).
113 (An earlier draft adopted as soon as rule 1's mirror held after 32 pairs; Wolfram's simulation
114 showed it adopting a challenger with a rare stall of its own about a third of the time. That draft's
115 figures are in the result file as committed at 55d738f, history; the file is regenerated since.)
116- A candidate is not run for real as the incumbent before `V` equal outputs, and one unequal output
117 bans it for the life of the process. Nothing re-checks an admitted arm (section 6, item 1).
118- After a duel the class rests on its incumbent, twice as long after each duel that changed nothing
119 and from the start again after one that did. So the cost of looking falls toward a floor, and a
120 change in the machine or the workload is still noticed.
121
122## 4. The program and the engine entries
123
124`feel` reads a trace on standard input and writes events on standard output.
125
126Input lines: `c CLASS K` declares class `CLASS` with `K` arms (2..49 in a trace); `t CLASS t0 .. t(K-1)
127OKMASK` is one launch of that class: the time each arm would take on this launch, and a mask whose
128bit `a` says arm `a`'s output equals the reference on this launch. The parser validates every field. The decision uses the time of the chosen arm and the mask bit for the shadowed arm. Any other line, a class used before
129it is declared, a class declared twice, or a count outside its range: exit 2 and one line on standard
130error.
131
132Output lines, one per event, in the order they happen: `CLASS B ARM I`, `CLASS M ARM I`, and
133`CLASS A|R|Am|D ARM I N W L SI SC`, where `ARM` is the challenger and `I` the class's launch counter.
134At end of input, for each declared class in increasing order:
135`CLASS F INC RESTLEN COUNTS.. STATUS..`.
136
137Exactly, as `feel.om` does it. A line is its fields with one space between them and a newline at its
138end, the last line included; nothing else, so an empty line is refused. A number is 1 to 15 decimal
139digits (leading zeros allowed). `OKMASK` is below `2^K`. On bad input the events already decided stay
140printed (gate case "a bad line after 67 launches"), then the one line `feel: bad input, line N` goes to
141standard error, the exit status is 2 and no `F` line is written. If standard input cannot be read or
142standard output cannot be written the exit status is 3. If the host refuses memory for the class
143tables the events decided so far stay printed, the one line `feel: out of memory, line N` goes to
144standard error and the exit status is 4. In 3.1, a launch that finds no arm to try sets `rest = restlen`
145and does not count it down.
146
147### 4.1 The engine entries
148
149These four are what an engine calls; the trace reader calls the same four, so every decision in a
150trace is a decision of the same code. Each checks its arguments and the order of calls before it
151changes anything: a refused call changes nothing (the gate builds `tests/api-main.om` against
152`feel.om` and compares what the entries return, `tests/api-expected.txt`, worked out by hand from this
153section). A refusal is a returned code, never a trap and never a write to the wrong place.
154
155| Call | Does | Returns |
156|---|---|---|
157| `setup c K` | declares class `c` with `K` arms, once | 0, or a code |
158| `choose c` | one launch of class `c`, steps 3.1 | `arm + 65536 * ch + 2^32 * owed`, or `ERR + code` |
159| `verdict c equal` | 3.2: `equal` is 1 for bitwise-equal outputs, else 0 | 0, or a code |
160| `observe c t` | 3.3: `t` is the time the chosen arm took | 0, or a code |
161
162In `choose`'s result, `arm` (bits 0..15) is the arm to run; `owed` (bits 32..33) is 0 (nothing), 1 (a
163verdict on arm `ch`, bits 16..31, run on the same input into scratch memory) or 2 (the time of this
164launch). `ERR` is `2^63`; a result below it is never an error.
165
166Codes: 1 class not declared; 2 call out of order; 3 argument out of range; 4 class already declared;
1675 the host refused memory. When several apply, the lowest number is returned, which is the order the
168checks run in.
169
170Order of calls, per class (the illegal transitions are the refused ones). `choose` is allowed only when
171nothing is owed from the last `choose` of that class. After a `choose` that owes a verdict only
172`verdict` is allowed, after one that owes a time only `observe`; each settles the debt. A `choose`
173that owes nothing needs no other call. Every other call is code 2. Different classes are independent.
174
175### 4.2 Limits, and what kind each is
176
177No table has a fixed size. Every per-class and per-arm table is a run-time pool that grows when a class
178is declared (`setup`), and the trace reader's time buffer grows with the arms. What remains is stated
179here with its reason (Drake, 2026-11-11: a limit stays only where it is a width of the machine or
180the host's memory).
181
182| Limit | Value | Kind |
183|---|---|---|
184| a time | below `10^15` | width of the 64-bit word: the largest sum is `256 * 64 * (10^15 - 1)`, below `2^64` (Wolfram, below). `10^15` is 11.57 days in nanoseconds |
185| a class number | below `2^52` | conservative API input ceiling. One 64-bit table at that size alone would require 32 PiB. Memory is proportional to the largest class number declared: number classes from 0 upward |
186| arms per class, API | 2 to 65535 | width of the fields of `choose`'s result (16 bits) |
187| arms per class, trace | 2 to 49 | width of the mask: it is written as a decimal below `10^15`; `2^49 - 1` is below that and `2^50 - 1` is not, so 49 arms fit and 50 do not (Wolfram, below) |
188| memory | what the host gives | a refusal is code 5 / exit 4. Gate: a class number past the address space, and a million classes in 64 MiB |
189| the input and output buffers (65536 bytes) | none | read and write chunk sizes, not limits |
190
191Wolfram: `WolframLanguageEvaluator: {64*256*(10^15-1) < 2^64, N[64*256*(10^15-1)], N[2^64], N[10^15/(10^9*86400)], 2^49-1 < 10^15, 2^50-1 < 10^15} -> {True, 1.6383999999999984*^19, 1.8446744073709552*^19, 11.574074074074074, True, False}`.
192
193The alarm in place of a cap on classes (Drake, 2026-11-11: "just put a TEST that will alarm"): `tests/run.sh`
194declares 100000 classes with the address space limited to 48 MiB and prints `ALARM` with the figures
195when the program no longer fits. The threshold is about 500 bytes per class; it was set above what the
196tables needed when it was written, so a table that starts growing faster, or an arm table per class
197sized for the worst case, trips it. It tells someone to look. It does not stop the program.
198
199The caller must serialize these entries. They share mutable tables across classes, including during growth.
200
201How `feel.om` is called: `setup c K` declares a class; `choose c`, `verdict c equal` and
202`observe c t` are the three steps; `finals` prints the `F` lines. The same three steps (choose, verdict,
203observe) are what an engine will call; the trace form exists so that every decision can be replayed and
204recomputed.
205
206## 5. What Wolfram checked
207
208The result files are produced by the scripts, not typed: each script's final value is one string and
209the file is that string. `tests/wolfram.sha256` (written by `sh tests/seal.sh`) records the SHA-256 of
210each script and of its result; `tests/run.sh` fails when either changed without the other being
211evaluated and sealed again. That catches drift. It cannot show that a result came from its script: the
212independent re-evaluation of a script on a fresh Wolfram kernel does that, and the figures are not
213quoted here: read them in the result files.
214
215- `tests/oracle.wl` -> `tests/expected-wolfram.txt`: the whole state machine of section 3 in exact
216 integers over synthetic traces. The traces: a 5% faster arm, a 10% slower one, a faster arm that is
217 wrong, equal arms, a challenger with rare long stalls, a reference with rare long stalls, a change of
218 which arm is faster part-way, two classes at once, times that are often exactly equal, a run long
219 enough to reach the longest rest, a gain only in the second half, a gain only in the first, equal
220 sums, a faster arm that is wrong on every 500th launch after its admission (`lateWrong`: shows the
221 admitted arm is never banned, section 6 item 1), and an adopt by mean with the challenger winning
222 fewer pairs than it loses (`meanWin`: `Am` with `w < l`, which the count chain does not bound).
223 The gate regenerates each trace with a second generator (`tests/gen.py`), checks its SHA-256 against
224 Wolfram's, and requires `feel` to print exactly Wolfram's events.
225- `tests/wolfram-duel.wl` computes an exact finite two-outcome walk. It removes rejected mass
226 starting at NMIN and evaluates adoption once at NMAX. It assumes the relevant sums agree
227 with the count. The full-rule simulation measures the effect of the sum and mean rules.
228
229- `tests/wolfram-rules-sim.wl` -> `tests/wolfram-rules-sim-result.txt`: 2000 simulated duels per row
230 under the full rules of 3.3, with lognormal noise and outside stalls on both arms, one seed. A rate
231 of 0 in 2000 is not 0: the column "95% upper bound of P(either adoption)" is the one-sided 95% upper bound of
232 the adopt rate (for 0 in 2000, `1 - (1/20)^(1/2000)`), and it is the figure to quote. The rows with a stall of the
233 challenger's own say what an unlucky arm costs (section 6, item 3).
234- `tests/wolfram-slow-trial.wl` -> `tests/wolfram-slow-trial-result.txt`: the exact cost of a challenger
235 that loses every pair, as a share of the reference's time (section 6, item 2). Its rejection
236 launches are required by the gate to equal the `R` launches of scenario `cap`.
237
238These are properties of the model in section 3 on synthetic times. They say nothing yet about a real
239kernel on a real chip.
240
241## 6. Scope: what the rules do not do, and what the checks do not show
242
2431. **An admitted arm is not checked again.** An arm that gave equal outputs on `V` shadowed launches
244 is trusted for the life of the process; the incumbent is never compared with the reference again.
245 An arm that is wrong only now and then (scenario `lateWrong`) is admitted, adopted and never banned;
246 its wrong outputs are delivered. The claim is: the core does not adopt an arm that differed on any of
247 its `V` shadowed launches. Periodic re-verification would need the engine to shadow the incumbent
248 now and then; it is not part of this core and not built.
2492. **A trial of unpredictable slowness.** The core sees a launch's time only after the launch, and it
250 cannot interrupt a launch. What it bounds is the number of launches given to a challenger: a
251 challenger that loses every pair, however slow, is turned away by rule 1 at pair `NMIN`, so it gets
252 `NMIN` timed launches per duel (and `V` shadow launches the first time). It then rests for `restlen`
253 launches, twice as long after each reject, up to `RESTMAX`. What it does not bound is the time those
254 launches take: with a challenger `X` times slower, in units of one incumbent launch, the extra time is `V X`
255 once and `NMIN (X-1)` per duel, and the share of the whole run it adds is in
256 `tests/wolfram-slow-trial-result.txt`: it falls toward `NMIN (X-1) / (RESTMAX + 2 NMIN)` and is large
257 for a very slow `X` (read the `limit` lines). A
258 launch that never returns is outside the core; the engine must give every trial a deadline and report
259 the deadline as its time. No early stop for a catastrophic challenger is built (a rule such as "sum
260 above `k` times the incumbent's after a few pairs" would need its own simulation, its own
261 false-reject figures and a new oracle). Until it is, an engine offers only arms whose slowest
262 plausible case it can afford at that rate.
2633. **Rare stalls.** A challenger whose own stalls are rarer than about one launch in `NMAX` can pass a
264 whole duel without showing one. The row for one stall in 128 launches shows adoption of a challenger that is slower on average. The one-in-512 challenger is faster on average.
2654. **Adopt by mean needs no win count**, by design (3.3.2): the challenger can win fewer pairs than it
266 loses and still be adopted when its sums are lower in both halves by 1/64 (scenario `meanWin`).
2675. **What the gate shows.** `feel.om` equals the oracle on the synthetic traces (the oracle was written
268 from section 3; it is evidence that the program implements these rules, not that the rules are the
269 right ones); the engine entries refuse what 4.1 says (the driver and its expected output were derived
270 by hand); each of the single-line changes to the program or its checks that `tests/run.sh` lists is
271 caught (mutants); the Wolfram files are the sealed outputs of their scripts, and the constants SPEC
272 and the scripts restate are `feel.om`'s. It does not show behaviour on a chip, or that a real kernel
273 pair is eligible (2.1).
2746. **Timing on a GPU perturbs what it measures.** A per-launch time on a GPU needs a per-launch event or
275 a synchronisation; that overhead is paid by every timed launch, can be larger than the difference
276 between two arms, and can change the schedule of the work. It is not modelled anywhere here. The
277 first engine measurement has to measure it.
278
279## 7. Not built, not run
280
281- The engine side: calling `choose`, `verdict` and `observe` from an inference engine; arms for real
282 kernels; the equality check on real outputs (2.1). NOT_RUN.
283- Any measurement on a GPU. NOT_RUN. No speed is claimed.
284- Interaction between classes (an arm that makes the next kernel slower) is not seen by a per-class
285 duel; the end-to-end check for that is part of the engine work.