interplane.Get Omega
Read in your language

omega-helix

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

omega-helix / native / model.om

2189 lines · 48004 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note omegallm model.om (aien-train#52): geometry, forward pass and hand-derived backward pass
3note (CONTRACT sections 1-3, CONTRACT-V1 section 1). Needs math.om and data.om before it.
4note
5note f64 values live in banked pools (one omc global holds at most 16777216 elements, and all
6note globals together at most 2^30 bytes, omc error 23; CONTRACT-V3 section 2):
7note ow parameter bank A: [0, SB) of the CONTRACT section 2 order, then a one-layer window of
8note LSTR values at og[36] and the final gain gf at og[37]; oww (lpool.om/epool.om) bank B holds
9note layers LA .. L-1 at (l - LA) LSTR. When P <= OWC, SB = GF, LA = L and ow is the v2 layout.
10note Every index outside the kernels is virtual (the section 2 order); owg/owp map it, and opx
11note maps a hot index of bank A or the staged layer (og[48]).
12note the gradient pool [0, 2P): GRD [0, P) in the platform cold store (ocsg/ocsp), BLK [P, 2P) in
13note ogr[j] below OGC and ogb[j - OGC] from OGC (j = i - P), reached through ogrg/ogrp/ogra
14note the Adam pool [0, 2P) (omvg/omvp): m at 0, v at P, at cold-store index P + i
15note oa activations: layers below LAA at l ASTR, later layers in two windows at AW + (l mod 2) ASTR
16note flushed to bank B oab at (l - LAA) ASTR after their forward and staged back before their
17note backward; HF and the backward temporaries follow. When END <= OAC, LAA = L (v2 layout).
18note opa attention probabilities PA[l][n][t][j] of every layer
19note og slots:
20note 0 V 1 T 2 D 3 H 4 F 5 L 6 B 7 E=D/H 8 P 9 CT (positions in use, <= T)
21note 10 GRD (base in the gradient pool of the gradient being accumulated: 0, or P for BLK) 11 MOM
22note (0 in the Adam pool) 12 VEL (P) 13 ACT (0 in oa) 14 ASTR (per-layer activation stride) 15 HF
23note 16 SF 17 Z 18 PR 19 DH 20 DA 21 DQ 22 DK 23 DU 24 DO 25 DZ 26 DF 27 TMP 28 END
24note 29 PSTR (per-layer PA stride H T T in opa) 30 WPE 31 LAY0 32 LSTR (per-layer parameter
25note stride) 33 GF 34 SB (first virtual index of bank B) 35 LA (first layer in bank B) 36 window base
26note in ow 37 gf in ow 38 LAA (first layer in the activation windows) 39 AW (activation window
27note base) 48 the layer staged in the parameter window (L: none)
28note 40 seed S 41 step 42 TOTAL 43 lr bits 44 G (gradient blocks) 45 K (workers)
29note 46 checkpoint magic of the run (1 OLLMCK01, 2 OLLMCK02, 3 OLLMCK03)
30note 56 LV (layer applications) 57 LP 58 LC 59 NP (the loop, below) 60 set by a command that runs
31note looped checkpoints, before it loads one (ckpt.om refuses a looped one otherwise, exit 17)
32note The loop (docs/LOOP.md): L stays the number of parameter layers. With NP > 1 the LC layers from
33note layer LP (the core) are applied NP times in a row with the same weights, between the LP
34note layers before them (the prelude) and the L - LP - LC after them (the coda), so a sequence
35note passes LV = L + LC (NP - 1) layer applications. Application l runs parameter layer olp l.
36note Activations, attention probabilities, the decode's row cache and the two activation banks are
37note per application (oahin, opar, oaflip, okvkv take l); parameters and their gradients are per
38note parameter layer (opg1 .. opw2 take the parameter layer, ovg1 .. ovw2 take l). Every gradient
39note write adds, so a core weight's gradient is the sum over its NP applications. NP 0 or 1 is no
40note loop: LV = L, olp l = l, and every result has the bits it had before the loop existed.
41note Per layer activations (T rows of width D unless noted): HIN, A, Q, K, U, O, HMID, BN, S1[T],
42note S2[T], FP[T][F] (pre-relu), RL[T][F]. Every sum runs over its index ascending.
43note Sizes at the CONTRACT-V2 maxima (V 4096, T 256, D 384, H 16, F 1536, L 8), Wolfram:
44note P = V D + T D + L (4 D^2 + 2 D F + 2 D) + D -> 15833472
45note ASTR = 8 T D + 2 T + 2 T F -> 1573376; END = L ASTR + 10 T D + T + T V + T F + 4096 -> 15016192
46note and at the CONTRACT-V3 maxima (L 16): P 29995392, PA = L H T T -> 16777216; the bank-B sizes
47note are in lpool.om. ogeom refuses any layout past a pool.
48note Bank A capacities: OWC parameters, OGC BLK values, OAC activations (the bank-crossing test
49note build of tests/omegallm/build.sh shrinks all three).
50const OWC ulong 15833472
51global ow f64 15833472
52const OGC ulong 15833472
53global ogr f64 15833472
54const OAC ulong 16777216
55global oa f64 16777216
56const OPAN ulong 16777216
57global opa f64 16777216
58
59fn opag i ulong to f64
60if lt i OPAN
61ret get opa i
62end
63ret get opab sub i OPAN
64end
65
66fn opap i ulong v f64 to void
67if lt i OPAN
68put opa i v
69else
70put opab sub i OPAN v
71end
72end
73
74note Neuralese Phase 1: non-destructive steering, adaptive looped-core thought, LTP0 latent handoff & live telemetry.
75note All disabled when onlc[0]=0, onlc[4]=0, onlc[6]=0, onlc[10]=0 -> bit-identical to stock ollm.
76note onlc: [0]=steer_on, [1]=steer_layer (999=all), [2]=steer_kind (1=neuron scale, 2=resid bias, 3=head scale),
77note [3]=steer_idx, [4]=adapt_extra (0..4), [5]=adapt_thresh_milli (nats*1000),
78note [6]=ltp_inject_on, [7]=ltp_inject_layer, [8]=last_extra_loops, [9]=last_lv_used, [10]=trace_on
79global onlc ulong 16
80note onlf: [0]=steer_val, [1]=ltp_alpha, [2]=last_entropy, [3]=last_surprisal, [4]=last_max_prob, [5]=last_hf_norm
81global onlf f64 8
82global onlv f64 2048
83global onlh f64 2048
84global onln f64 32
85global onla f64 32
86global onltn_i ulong 16
87global onltn_v f64 16
88
89note Adaptive loop (docs/LOOP.md, "Per-token exit and messages between passes"). Every control 0 ->
90note the hooks never run and every result keeps its bits. onlx: [0] exit threshold (a row's
91note normalized core delta |out - in| / |in| of a pass, times 1e6; 0 off) [1] inter-pass message gain
92note times 1e6 (0 off) [2] 1 = the LTP0 vector onlv (scaled to rms onlf[1]) is the pass-0 message
93note [6] 1 = run the hooks for telemetry only (no value changes). Hooks run only when NP > 1.
94global onlx ulong 8
95note onlxp[t]: the core passes row t ran in the last forward (NP when it never exited early)
96global onlxp ulong 512
97note onlxd[t]: row t's normalized delta of pass 0; onlmn[t]: the norm of its last message
98global onlxd f64 512
99global onlmn f64 512
100note onlmsg[t D + i]: row t's inter-pass message (the core update of its last pass);
101note onlpin[t D + i]: row t's input to the current pass (after the message is mixed in)
102global onlmsg f64 98304
103global onlpin f64 98304
104global onlxs f64 4
105note Mutant x (the cached decode skips the end-of-pass hook: no exits, no messages): 1 in the
106note product, 0 in the mutant, which adaptcmp must catch.
107const OMUTAX ulong 1
108
109note gradient pool element i (i < 2 P)
110fn ogrg i ulong to f64
111p = get og 8
112if lt i p
113ret ocsg i
114end
115j = sub i p
116if lt j OGC
117ret get ogr j
118end
119ret get ogb sub j OGC
120end
121
122fn ogrp i ulong v f64 to void
123p = get og 8
124if lt i p
125ocsp i v
126ret
127end
128j = sub i p
129if lt j OGC
130put ogr j v
131else
132put ogb sub j OGC v
133end
134end
135
136note gradient pool element i += a
137fn ogra i ulong a f64 to void
138p = get og 8
139if lt i p
140ocsp i add ocsg i a
141ret
142end
143j = sub i p
144if lt j OGC
145put ogr j add get ogr j a
146else
147k = sub j OGC
148put ogb k add get ogb k a
149end
150end
151
152note Adam pool element i (i < 2 P), cold-store index P + i
153fn omvg i ulong to f64
154ret ocsg add get og 8 i
155end
156
157fn omvp i ulong v f64 to void
158ocsp add get og 8 i v
159end
160
161note parameter at virtual index i (bank A, bank B or gf), and its store
162fn owg i ulong to f64
163sb = get og 34
164if lt i sb
165ret get ow i
166end
167g = get og 33
168if ge i g
169ret get ow add get og 37 sub i g
170end
171ret get oww sub i sb
172end
173
174fn owp i ulong v f64 to void
175sb = get og 34
176if lt i sb
177put ow i v
178ret
179end
180g = get og 33
181if ge i g
182put ow add get og 37 sub i g v
183ret
184end
185put oww sub i sb v
186end
187
188note ow index of virtual index i for the kernels: bank A as is, gf at og[37], a bank-B layer only
189note when it is the staged layer og[48] (else exit 16, a missing ostw)
190fn opx i ulong to ulong
191if lt i get og 34
192ret i
193end
194g = get og 33
195if ge i g
196ret add get og 37 sub i g
197end
198l = div sub i get og 31 get og 32
199if ne l get og 48
200exit 16
201end
202ret add get og 36 sub i opg1 l
203end
204
205note Mutant y (wrong bank-B layer: ostw stages layer LA's block for every layer): 1 in the product,
206note 0 in the mutant.
207const OMUTY ulong 1
208
209const OCH ulong 999999
210
211note stage bank-B layer l into the parameter window (layers below LA live in bank A; direct-oww mode skips staging)
212fn ostw l ulong to void
213if eq get og 36 OWC
214put og 48 l
215ret
216end
217la = get og 35
218if lt l la
219ret
220end
221n = get og 32
222s = mul sub l la n
223if eq OMUTY 0
224s = 0
225end
226ostwc get og 36 s n
227put og 48 l
228end
229
230fn owinb wv ulong to bool
231ret and eq get og 36 OWC and ge wv get og 34 lt wv get og 33
232end
233
234note ow[wb + i] = oww[s + i] for i < n (chunked: a loop runs at most 999999 passes)
235fn ostwc wb ulong s ulong n ulong to void
236for q ulong ocn n
237i = mul q OCH
238ie = oce q n
239while lt i ie bound 999999
240put ow add wb i get oww add s i
241i = add i 1
242end
243end
244end
245
246note copy layer l's activation block between its window and bank B oab (to oab when fo)
247fn oaflip l ulong fo bool to void
248la = get og 38
249if lt l la
250ret
251end
252n = get og 14
253s = mul sub l la n
254oaflc oahin l s n fo
255end
256
257note oab[s + i] = oa[w + i] for i < n when fo, else the reverse
258fn oaflc w ulong s ulong n ulong fo bool to void
259if fo
260oafo w s n
261else
262oafi w s n
263end
264end
265
266fn oafo w ulong s ulong n ulong to void
267for q ulong ocn n
268i = mul q OCH
269ie = oce q n
270while lt i ie bound 999999
271put oab add s i get oa add w i
272i = add i 1
273end
274end
275end
276
277fn oafi w ulong s ulong n ulong to void
278for q ulong ocn n
279i = mul q OCH
280ie = oce q n
281while lt i ie bound 999999
282put oa add w i get oab add s i
283i = add i 1
284end
285end
286end
287
288note TMP holds V logits (sample) or T attention dots
289const OTMP ulong 4096
290
291note Mutant a (attention backward sign flip in the dK term): 1 in the product, 0 in the mutant.
292const OMUTA ulong 1
293
294note Configure og from og[0..6]; false when a shape is zero, over the maximum, or D mod H != 0, or
295note when the layout does not fit the pools.
296fn ogeom to bool
297if not ogok
298ret false
299end
300t = get og 1
301d = get og 2
302put og 7 div d get og 3
303u = mul 4 mul d d
304u = add u mul 2 mul d get og 4
305u = add u mul 2 d
306put og 32 u
307put og 30 mul get og 0 d
308u = mul t d
309put og 31 add get og 30 u
310u = mul get og 5 get og 32
311put og 33 add get og 31 u
312u = add get og 33 d
313put og 8 u
314put og 9 t
315put og 10 0
316put og 11 0
317put og 12 u
318put og 13 0
319put og 29 mul mul get og 3 t t
320if not oglv
321ret false
322end
323u = mul t d
324put og 14 oastr u
325if not ogwb
326ret false
327end
328if not ogab u
329ret false
330end
331p = get og 8
332if gt p add OGC OGBN
333ret false
334end
335if gt p OGC
336if not olp_grow 0 sub p OGC 0
337ret false
338end
339end
340if gt mul 3 p ocsinit
341ret false
342end
343pa = mul get og 56 get og 29
344if gt pa add OPAN OPABN
345ret false
346end
347if gt pa OPAN
348ret olp_grow_pa sub pa OPAN
349end
350ret true
351end
352
353note the layer applications og[56] from L and the loop og[57 .. 59]; false when the core is empty,
354note does not fit in the L layers, or runs more passes than the attention pool has rows for
355fn oglv to bool
356ll = get og 5
357np = get og 59
358if le np 1
359put og 56 ll
360ret true
361end
362lc = get og 58
363if or eq lc 0 gt np OPAN
364ret false
365end
366if gt add get og 57 lc ll
367ret false
368end
369put og 56 add ll mul lc sub np 1
370ret true
371end
372
373note the parameter layer that layer application l runs
374fn olp l ulong to ulong
375np = get og 59
376if le np 1
377ret l
378end
379lp = get og 57
380if lt l lp
381ret l
382end
383lc = get og 58
384j = sub l lp
385if lt j mul lc np
386ret add lp rem j lc
387end
388ret sub l mul lc sub np 1
389end
390
391note parameter banks: v2 layout when P <= OWC, else LA layers in bank A, the window and gf after
392note them, layers LA .. L-1 in oww
393fn ogwb to bool
394ll = get og 5
395g = get og 33
396put og 34 g
397put og 35 ll
398put og 36 g
399put og 37 g
400put og 48 ll
401if le get og 8 OWC
402ret true
403end
404ls = get og 32
405nd = add add get og 31 ls get og 2
406if lt OWC nd
407if lt OWC add get og 31 get og 2
408ret false
409end
410nbw = mul ll ls
411if gt nbw OWWN
412ret false
413end
414if not olp_grow nbw 0 0
415ret false
416end
417put og 34 get og 31
418put og 35 0
419put og 36 OWC
420put og 37 get og 31
421ret true
422end
423la = div sub OWC nd ls
424nbw = mul sub ll la ls
425if gt nbw OWWN
426ret false
427end
428if not olp_grow nbw 0 0
429ret false
430end
431sb = add get og 31 mul la ls
432put og 34 sb
433put og 35 la
434put og 36 sb
435put og 37 add sb ls
436ret true
437end
438
439note activation banks: v2 layout when END <= OAC, else LAA layers, two windows, then HF and the
440note backward temporaries
441fn ogab td ulong to bool
442ll = get og 56
443st = get og 14
444put og 38 ll
445put og 39 mul ll st
446ogeom3 td mul ll st
447ogeom2 td
448if le get og 28 OAC
449ret true
450end
451w2 = add sub get og 28 get og 15 mul 2 st
452if lt OAC w2
453ret false
454end
455la = div sub OAC w2 st
456nba = mul sub ll la st
457if gt nba OABN
458ret false
459end
460if not olp_grow 0 0 nba
461ret false
462end
463aw = mul la st
464put og 38 la
465put og 39 aw
466ogeom3 td add aw mul 2 st
467ogeom2 td
468ret le get og 28 OAC
469end
470
471fn ogeom3 td ulong hf ulong to void
472put og 15 hf
473put og 16 add get og 15 td
474put og 17 add get og 16 get og 1
475put og 18 add get og 17 td
476end
477
478note per-layer activation stride: 8 T D + 2 T + 2 T F
479fn oastr td ulong to ulong
480t = get og 1
481a = add mul 8 td mul 2 t
482ret add a mul mul 2 t get og 4
483end
484
485note shape limits: dynamic-pool targets (OWWN > 16M: gb10, aarch64, macos) support up to 500M+
486note (T <= 512, D <= 2048, H <= 32, F <= 8192, L <= 24, looped LV = L_phys x NP); static-pool targets
487note (x86linux, UEFI) keep CONTRACT-V3 limits (T <= 256, D <= 384, H <= 16, F <= 1536, L <= 16).
488fn ogok to bool
489for i ulong 7
490if eq get og i 0
491ret false
492end
493end
494if or lt get og 0 256 gt get og 0 4096
495ret false
496end
497maxt = ulong 256
498maxd = ulong 384
499maxh = ulong 16
500maxf = ulong 1536
501maxl = ulong 16
502if gt OWWN 16000000
503maxt = 512
504maxd = 2048
505maxh = 32
506maxf = 8192
507maxl = 24
508end
509if or gt get og 1 maxt gt get og 2 maxd
510ret false
511end
512if or gt get og 3 maxh gt get og 4 maxf
513ret false
514end
515if or gt get og 5 maxl gt get og 6 64
516ret false
517end
518ret eq rem get og 2 get og 3 0
519end
520
521fn ogeom2 td ulong to void
522t = get og 1
523put og 19 add get og 18 mul t get og 0
524put og 20 add get og 19 td
525put og 21 add get og 20 td
526put og 22 add get og 21 td
527put og 23 add get og 22 td
528put og 24 add get og 23 td
529put og 25 add get og 24 td
530put og 26 add get og 25 td
531put og 27 add get og 26 mul t get og 4
532put og 28 add get og 27 OTMP
533end
534
535note parameter offsets of layer l
536fn opg1 l ulong to ulong
537ret add get og 31 mul l get og 32
538end
539fn opwq l ulong to ulong
540ret add opg1 l get og 2
541end
542fn opwk l ulong to ulong
543d = get og 2
544ret add opwq l mul d d
545end
546fn opwv l ulong to ulong
547d = get og 2
548ret add opwk l mul d d
549end
550fn opwo l ulong to ulong
551d = get og 2
552ret add opwv l mul d d
553end
554fn opg2 l ulong to ulong
555d = get og 2
556ret add opwo l mul d d
557end
558fn opw1 l ulong to ulong
559ret add opg2 l get og 2
560end
561fn opw2 l ulong to ulong
562ret add opw1 l mul get og 2 get og 4
563end
564
565note parameter offsets of layer application l
566fn ovg1 l ulong to ulong
567ret opg1 olp l
568end
569fn ovwq l ulong to ulong
570ret opwq olp l
571end
572fn ovwk l ulong to ulong
573ret opwk olp l
574end
575fn ovwv l ulong to ulong
576ret opwv olp l
577end
578fn ovwo l ulong to ulong
579ret opwo olp l
580end
581fn ovg2 l ulong to ulong
582ret opg2 olp l
583end
584fn ovw1 l ulong to ulong
585ret opw1 olp l
586end
587fn ovw2 l ulong to ulong
588ret opw2 olp l
589end
590
591note activation offsets of layer l (layers from LAA in the two windows at AW)
592fn oahin l ulong to ulong
593if lt l get og 38
594ret add get og 13 mul l get og 14
595end
596ret add get og 39 mul rem l 2 get og 14
597end
598fn oatd n ulong to ulong
599ret mul n mul get og 1 get og 2
600end
601fn oaa l ulong to ulong
602ret add oahin l oatd 1
603end
604fn oaq l ulong to ulong
605ret add oahin l oatd 2
606end
607fn oak l ulong to ulong
608ret add oahin l oatd 3
609end
610fn oau l ulong to ulong
611ret add oahin l oatd 4
612end
613fn oao l ulong to ulong
614ret add oahin l oatd 5
615end
616fn oahm l ulong to ulong
617ret add oahin l oatd 6
618end
619fn oabn l ulong to ulong
620ret add oahin l oatd 7
621end
622fn oas1 l ulong to ulong
623ret add oahin l oatd 8
624end
625fn oas2 l ulong to ulong
626ret add oas1 l get og 1
627end
628fn oafp l ulong to ulong
629ret add oas2 l get og 1
630end
631fn oarl l ulong to ulong
632ret add oafp l mul get og 1 get og 4
633end
634note output rows of layer application l: the next one's HIN, or HF after the last
635fn oaout l ulong to ulong
636if lt add l 1 get og 56
637ret oahin add l 1
638end
639ret get og 15
640end
641
642note Kernels. The fast forms keep every v0 sum: each output is one accumulator that starts at
643note 0.0 and adds its terms in the v0 index order with the v0 expression (add s mul a w); four
644note outputs run side by side in separate accumulators, so every result has v0's bits. Results of
645note the four-wide blocks pass through oq.
646global oq f64 4
647
648note y[0..c) = x[0..r) . W[r][c] (overwrites y); x, y in oa, W in ow at virtual index wv (or direct oww)
649fn kvm xo ulong wv ulong yo ulong r ulong c ulong to void
650if owinb wv
651kvm_b xo sub wv get og 34 yo r c
652ret
653end
654wo = opx wv
655xe = add xo r
656j = ulong 0
657while le add j 4 c bound 999999
658kvmb xo xe add wo j c
659kput4 add yo j
660j = add j 4
661end
662while lt j c bound 4
663put oa add yo j kvm1 xo xe add wo j c
664j = add j 1
665end
666end
667
668fn kvm_b xo ulong wo ulong yo ulong r ulong c ulong to void
669xe = add xo r
670j = ulong 0
671while le add j 4 c bound 999999
672kvmb_b xo xe add wo j c
673kput4 add yo j
674j = add j 4
675end
676while lt j c bound 4
677put oa add yo j kvm1_b xo xe add wo j c
678j = add j 1
679end
680end
681
682fn kvmb_b xo ulong xe ulong wi ulong c ulong to void
683s0 = 0.0f64
684s1 = 0.0f64
685s2 = 0.0f64
686s3 = 0.0f64
687while lt xo xe bound 999999
688a = get oa xo
689s0 = add s0 mul a get oww wi
690s1 = add s1 mul a get oww add wi 1
691s2 = add s2 mul a get oww add wi 2
692s3 = add s3 mul a get oww add wi 3
693wi = add wi c
694xo = add xo 1
695end
696put oq 0 s0
697put oq 1 s1
698put oq 2 s2
699put oq 3 s3
700end
701
702fn kvm1_b xo ulong xe ulong wi ulong c ulong to f64
703s = 0.0f64
704while lt xo xe bound 999999
705a = get oa xo
706s = add s mul a get oww wi
707wi = add wi c
708xo = add xo 1
709end
710ret s
711end
712
713note oa[yo..yo+4) = oq[0..4)
714fn kput4 yo ulong to void
715put oa yo get oq 0
716put oa add yo 1 get oq 1
717put oa add yo 2 get oq 2
718put oa add yo 3 get oq 3
719end
720
721note oq[m] = sum over xo <= i < xe of x[i] W[wi + m + (i - xo) c], m < 4
722fn kvmb xo ulong xe ulong wi ulong c ulong to void
723s0 = 0.0f64
724s1 = 0.0f64
725s2 = 0.0f64
726s3 = 0.0f64
727while lt xo xe bound 999999
728a = get oa xo
729s0 = add s0 mul a get ow wi
730s1 = add s1 mul a get ow add wi 1
731s2 = add s2 mul a get ow add wi 2
732s3 = add s3 mul a get ow add wi 3
733wi = add wi c
734xo = add xo 1
735end
736put oq 0 s0
737put oq 1 s1
738put oq 2 s2
739put oq 3 s3
740end
741
742note sum over xo <= i < xe of x[i] W[wi + (i - xo) c]
743fn kvm1 xo ulong xe ulong wi ulong c ulong to f64
744s = 0.0f64
745while lt xo xe bound 999999
746a = get oa xo
747s = add s mul a get ow wi
748wi = add wi c
749xo = add xo 1
750end
751ret s
752end
753
754note y[k] += sum_j dy[j] W[k][j] for k < r, j < c (y += dy . W^T); dy, y in oa, W in ow at virtual
755note index wv (or direct oww)
756fn kvta dyo ulong wv ulong yo ulong r ulong c ulong to void
757if owinb wv
758kvta_b dyo sub wv get og 34 yo r c
759ret
760end
761wo = opx wv
762de = add dyo c
763k = ulong 0
764while le add k 4 r bound 999999
765kdot4 dyo de add wo mul k c c
766kacc4 add yo k
767k = add k 4
768end
769while lt k r bound 4
770a = add get oa add yo k kdot1 dyo de add wo mul k c
771put oa add yo k a
772k = add k 1
773end
774end
775
776fn kvta_b dyo ulong wo ulong yo ulong r ulong c ulong to void
777de = add dyo c
778k = ulong 0
779while le add k 4 r bound 999999
780kdot4_b dyo de add wo mul k c c
781kacc4 add yo k
782k = add k 4
783end
784while lt k r bound 4
785a = add get oa add yo k kdot1_b dyo de add wo mul k c
786put oa add yo k a
787k = add k 1
788end
789end
790
791fn kdot4_b xo ulong xe ulong wi ulong c ulong to void
792s0 = 0.0f64
793s1 = 0.0f64
794s2 = 0.0f64
795s3 = 0.0f64
796w2 = add wi add c c
797while lt xo xe bound 999999
798a = get oa xo
799s0 = add s0 mul a get oww wi
800s1 = add s1 mul a get oww add wi c
801s2 = add s2 mul a get oww w2
802s3 = add s3 mul a get oww add w2 c
803wi = add wi 1
804w2 = add w2 1
805xo = add xo 1
806end
807put oq 0 s0
808put oq 1 s1
809put oq 2 s2
810put oq 3 s3
811end
812
813fn kdot1_b xo ulong xe ulong wi ulong to f64
814s = 0.0f64
815while lt xo xe bound 999999
816a = get oa xo
817s = add s mul a get oww wi
818wi = add wi 1
819xo = add xo 1
820end
821ret s
822end
823
824note oa[yo + m] = oa[yo + m] + oq[m], m < 4
825fn kacc4 yo ulong to void
826put oa yo add get oa yo get oq 0
827q = add yo 1
828put oa q add get oa q get oq 1
829q = add yo 2
830put oa q add get oa q get oq 2
831q = add yo 3
832put oa q add get oa q get oq 3
833end
834
835note oq[m] = sum over xo <= i < xe of x[i] W[wi + m c + (i - xo)], m < 4; x in oa, W in ow
836fn kdot4 xo ulong xe ulong wi ulong c ulong to void
837s0 = 0.0f64
838s1 = 0.0f64
839s2 = 0.0f64
840s3 = 0.0f64
841w2 = add wi add c c
842while lt xo xe bound 999999
843a = get oa xo
844s0 = add s0 mul a get ow wi
845s1 = add s1 mul a get ow add wi c
846s2 = add s2 mul a get ow w2
847s3 = add s3 mul a get ow add w2 c
848wi = add wi 1
849w2 = add w2 1
850xo = add xo 1
851end
852put oq 0 s0
853put oq 1 s1
854put oq 2 s2
855put oq 3 s3
856end
857
858note sum over xo <= i < xe of x[i] W[wi + (i - xo)]; x in oa, W in ow
859fn kdot1 xo ulong xe ulong wi ulong to f64
860s = 0.0f64
861while lt xo xe bound 999999
862a = get oa xo
863s = add s mul a get ow wi
864wi = add wi 1
865xo = add xo 1
866end
867ret s
868end
869
870note kdot4 with both operands in oa
871fn kdot4a xo ulong xe ulong wi ulong c ulong to void
872s0 = 0.0f64
873s1 = 0.0f64
874s2 = 0.0f64
875s3 = 0.0f64
876w2 = add wi add c c
877while lt xo xe bound 999999
878a = get oa xo
879s0 = add s0 mul a get oa wi
880s1 = add s1 mul a get oa add wi c
881s2 = add s2 mul a get oa w2
882s3 = add s3 mul a get oa add w2 c
883wi = add wi 1
884w2 = add w2 1
885xo = add xo 1
886end
887put oq 0 s0
888put oq 1 s1
889put oq 2 s2
890put oq 3 s3
891end
892
893note kdot1 with both operands in oa
894fn kdot1a xo ulong xe ulong wi ulong to f64
895s = 0.0f64
896while lt xo xe bound 999999
897a = get oa xo
898s = add s mul a get oa wi
899wi = add wi 1
900xo = add xo 1
901end
902ret s
903end
904
905note G[k][j] += x[k] dy[j] for k < r, j < c (outer product into a gradient); x, dy in oa, G in ogr
906fn kger xo ulong dyo ulong go ulong r ulong c ulong to void
907for k ulong r
908kger1 get oa add xo k add go mul k c dyo c
909end
910end
911
912note G[gi + j] += xv dy[j] for j < c (elements independent, four per pass)
913fn kger1 xv f64 gi ulong yi ulong c ulong to void
914ye = add yi c
915while le add yi 4 ye bound 999999
916ogra gi mul xv get oa yi
917ogra add gi 1 mul xv get oa add yi 1
918ogra add gi 2 mul xv get oa add yi 2
919ogra add gi 3 mul xv get oa add yi 3
920gi = add gi 4
921yi = add yi 4
922end
923while lt yi ye bound 4
924ogra gi mul xv get oa yi
925gi = add gi 1
926yi = add yi 1
927end
928end
929
930note y[i] += a x[i]; x, y in oa
931fn kaxpy yo ulong xo ulong a f64 n ulong to void
932for i ulong n
933q = add yo i
934v = mul a get oa add xo i
935put oa q add get oa q v
936end
937end
938
939note y[i] += a x[i]; x in oa, y in the gradient pool
940fn kaxpg yo ulong xo ulong a f64 n ulong to void
941for i ulong n
942ogra add yo i mul a get oa add xo i
943end
944end
945
946fn ozero o ulong n ulong to void
947for i ulong n
948put oa add o i 0.0f64
949end
950end
951
952fn ozerog o ulong n ulong to void
953for q ulong ocn n
954i = mul q OCH
955ie = oce q n
956while lt i ie bound 999999
957ogrp add o i 0.0f64
958i = add i 1
959end
960end
961end
962
963note A loop runs at most 999999 passes (the while bound; a for over a name has the same guard), so
964note loops over P or more run in ocn n chunks of OCH; chunk q covers [q OCH, oce q n).
965fn ocn n ulong to ulong
966ret div add n 999998 OCH
967end
968fn oce q ulong n ulong to ulong
969e = mul add q 1 OCH
970if gt e n
971ret n
972end
973ret e
974end
975
976note y = rms(v, g): s = sqrt((sum v^2)/D + 1e-5) stored at so; y[i] = (v[i]/s) g[i]; g at virtual
977note index gv
978fn frms vo ulong gv ulong yo ulong so ulong to void
979inb = owinb gv
980go = ulong 0
981if inb
982go = sub gv get og 34
983else
984go = opx gv
985end
986d = get og 2
987ss = 0.0f64
988for i ulong d
989v = get oa add vo i
990ss = add ss mul v v
991end
992s = fsqrt add div ss f64 d 0.00001
993put oa so s
994if inb
995for j2 ulong d
996v = div get oa add vo j2 s
997v = mul v get oww add go j2
998put oa add yo j2 v
999end
1000ret
1001end
1002for j ulong d
1003v = div get oa add vo j s
1004v = mul v get ow add go j
1005put oa add yo j v
1006end
1007end
1008
1009note rms backward: n = v/s, dn = da g, c = (sum dn n)/D; dv = (dn - n c)/s added to dh;
1010note dg += da n added to the gain's gradient. go is the gain's virtual index.
1011global ofs f64 8
1012fn brms vo ulong go ulong so ulong dao ulong dho ulong to void
1013d = get og 2
1014put ofs 0 get oa so
1015gg = add get og 10 go
1016if owinb go
1017bo = sub go get og 34
1018put ofs 1 div brmsc_b vo bo dao get ofs 0 f64 d
1019for j2 ulong d
1020brms1_b add vo j2 add bo j2 add dao j2 add dho j2 add gg j2
1021end
1022ret
1023end
1024go = opx go
1025put ofs 1 div brmsc vo go dao get ofs 0 f64 d
1026for j ulong d
1027brms1 add vo j add go j add dao j add dho j add gg j
1028end
1029end
1030
1031fn brmsc_b vo ulong go ulong dao ulong s f64 to f64
1032c = 0.0f64
1033for i ulong get og 2
1034dn = get oa add dao i
1035dn = mul dn get oww add go i
1036v = div get oa add vo i s
1037c = add c mul dn v
1038end
1039ret c
1040end
1041
1042fn brms1_b vp ulong gp ulong dap ulong dhp ulong ggp ulong to void
1043s = get ofs 0
1044n = div get oa vp s
1045da = get oa dap
1046ogra ggp mul da n
1047dn = mul da get oww gp
1048dn = div sub dn mul n get ofs 1 s
1049put oa dhp add get oa dhp dn
1050end
1051
1052note sum over i < D of (da[i] g[i]) (v[i]/s), ascending
1053fn brmsc vo ulong go ulong dao ulong s f64 to f64
1054c = 0.0f64
1055for i ulong get og 2
1056dn = get oa add dao i
1057dn = mul dn get ow add go i
1058v = div get oa add vo i s
1059c = add c mul dn v
1060end
1061ret c
1062end
1063
1064note one element of the rms backward; s = ofs[0], c = ofs[1]
1065fn brms1 vp ulong gp ulong dap ulong dhp ulong ggp ulong to void
1066s = get ofs 0
1067n = div get oa vp s
1068da = get oa dap
1069ogra ggp mul da n
1070dn = mul da get ow gp
1071dn = div sub dn mul n get ofs 1 s
1072put oa dhp add get oa dhp dn
1073end
1074
1075note z[i] = a[i] + b[i] for i < n (all in oa)
1076fn kadd3 zo ulong ao ulong bo ulong n ulong to void
1077for i ulong n
1078v = get oa add ao i
1079v = add v get oa add bo i
1080put oa add zo i v
1081end
1082end
1083
1084note z[i] = a[i] + b[i] for i < n (a, b in ow, z in oa)
1085fn kadd3w zo ulong ao ulong bo ulong n ulong to void
1086for i ulong n
1087v = get ow add ao i
1088v = add v get ow add bo i
1089put oa add zo i v
1090end
1091end
1092
1093note h[t] = wte[x[t]] + wpe[t] for sequence b of the batch
1094fn oemb b ulong to void
1095d = get og 2
1096tt = get og 1
1097for t ulong get og 9
1098wr = get obx add mul b tt t
1099wr = mul wr d
1100pr = add get og 30 mul t d
1101kadd3w add oahin 0 mul t d wr pr d
1102end
1103end
1104
1105note offset in opa of PA[n][t][0] of layer l
1106fn opar l ulong n ulong t ulong to ulong
1107tt = get og 1
1108p = add mul n tt t
1109ret add mul l get og 29 mul p tt
1110end
1111
1112note attention scores and softmax of head n, row t: PA[n][t][j] for j <= t
1113fn fatts l ulong n ulong t ulong to void
1114e = get og 7
1115qo = add add oaq l mul t get og 2 mul n e
1116ko = add oak l mul n e
1117pa = opar l n t
1118m = fattsk qo ko pa add t 1
1119sm = osofp pa add t 1 m
1120end
1121
1122note PA row: s[j] = q.k[j]/sqrt(E) for j < nj, k rows stepping by D; returns the max score
1123fn fattsk qo ulong ko ulong pa ulong nj ulong to f64
1124d = get og 2
1125e = get og 7
1126isq = fsqrt f64 e
1127m = bitcast f64 ulong 18442240474082181120
1128qe = add qo e
1129j = ulong 0
1130while le add j 4 nj bound 999999
1131kdot4a qo qe ko d
1132m = fattp4 add pa j isq m
1133ko = add ko mul 4 d
1134j = add j 4
1135end
1136while lt j nj bound 4
1137s = div kdot1a qo qe ko isq
1138opap add pa j s
1139m = fmax m s
1140ko = add ko d
1141j = add j 1
1142end
1143ret m
1144end
1145
1146note PA[p + m] = oq[m] / isq for m < 4 in order; returns the running max
1147fn fattp4 p ulong isq f64 m f64 to f64
1148for i ulong 4
1149s = div get oq i isq
1150opap add p i s
1151m = fmax m s
1152end
1153ret m
1154end
1155
1156note p = exp(x - m) / sum over n entries at o in oa (in place); returns sum
1157fn osoft o ulong n ulong m f64 to f64
1158sm = 0.0f64
1159for j ulong n
1160ev = fexp sub get oa add o j m
1161put oa add o j ev
1162sm = add sm ev
1163end
1164for k ulong n
1165v = div get oa add o k sm
1166put oa add o k v
1167end
1168ret sm
1169end
1170
1171note osoft on opa
1172fn osofp o ulong n ulong m f64 to f64
1173sm = 0.0f64
1174for j ulong n
1175ev = fexp sub opag add o j m
1176opap add o j ev
1177sm = add sm ev
1178end
1179for k ulong n
1180v = div opag add o k sm
1181opap add o k v
1182end
1183ret sm
1184end
1185
1186note o[t][nE + i] = sum_{j <= t} p[t][j] u[j][nE + i]
1187fn fatto l ulong n ulong t ulong to void
1188e = get og 7
1189oo = add add oao l mul t get og 2 mul n e
1190uo = add oau l mul n e
1191pa = opar l n t
1192ozero oo e
1193fattok oo uo pa add t 1
1194end
1195
1196note o[0..E) += p[j] u[j][0..E) for j < nj, u rows stepping by D
1197fn fattok oo ulong uo ulong pa ulong nj ulong to void
1198d = get og 2
1199e = get og 7
1200for j ulong nj
1201kaxpy oo uo opag add pa j e
1202uo = add uo d
1203end
1204end
1205
1206fn olayf l ulong to void
1207ostw olp l
1208for t ulong get og 9
1209olayf1 l t
1210end
1211for n ulong get og 3
1212for t2 ulong get og 9
1213fatts l n t2
1214fatto l n t2
1215end
1216end
1217for t3 ulong get og 9
1218olayf2x l t3
1219end
1220oaflip l true
1221end
1222
1223note olayf2 of row t, or, for a row that left the loop before this core pass, its input unchanged
1224note (its K and V above still come from that frozen row, so later rows see the same keys)
1225fn olayf2x l ulong t ulong to void
1226if ne get onlx 0 0
1227if onlfz l t
1228d = get og 2
1229r = mul t d
1230onlcopy_oa add oaout l r add oahin l r d
1231ret
1232end
1233end
1234olayf2 l t
1235end
1236
1237note row t: a = rms(h, g1); q, k, u = a.wq, a.wk, a.wv
1238fn olayf1 l ulong t ulong to void
1239d = get og 2
1240r = mul t d
1241a = add oaa l r
1242frms add oahin l r ovg1 l a add oas1 l t
1243kvm a ovwq l add oaq l r d d
1244kvm a ovwk l add oak l r d d
1245kvm a ovwv l add oau l r d d
1246end
1247
1248note row t: h += o.wo; b = rms(h, g2); out = h + relu(b.w1).w2
1249fn olayf2 l ulong t ulong to void
1250d = get og 2
1251f = get og 4
1252r = mul t d
1253hm = add oahm l r
1254kvm add oao l r ovwo l hm d d
1255kadd3 hm add oahin l r hm d
1256frms hm ovg2 l add oabn l r add oas2 l t
1257fp = add oafp l mul t f
1258kvm add oabn l r ovw1 l fp d f
1259rl = add oarl l mul t f
1260krelu rl fp f
1261if ne get onlc 0 0
1262onlsteer_ffn l t
1263end
1264ou = add oaout l r
1265kvm rl ovw2 l ou f d
1266kadd3 ou hm ou d
1267if ne get onlc 0 0
1268onlsteer_res l t
1269end
1270end
1271
1272note y[j] = x[j] when x[j] > 0, else 0, for j < n
1273fn krelu yo ulong xo ulong n ulong to void
1274for j ulong n
1275v = get oa add xo j
1276if le v 0.0
1277v = 0.0f64
1278end
1279put oa add yo j v
1280end
1281end
1282
1283note loss of row t: the logits at o (n entries) become probabilities; returns
1284note log(sum exp(l - max)) - (l_y - max)
1285fn olossrow o ulong n ulong y ulong to f64
1286m = bitcast f64 ulong 18442240474082181120
1287for j ulong n
1288m = fmax m get oa add o j
1289end
1290ly = get oa add o y
1291sm = osoft o n m
1292ret sub flog sm sub ly m
1293end
1294
1295note final rms, tied head and loss of sequence b; returns the mean loss over CT positions
1296fn ohead b ulong to f64
1297ls = 0.0f64
1298for t ulong get og 9
1299ls = add ls oheadr b t
1300end
1301ret div ls f64 get og 9
1302end
1303
1304note row t of the head: z = rms(HF[t], gf); logits = z . wte^T; returns the row loss
1305fn oheadr b ulong t ulong to f64
1306d = get og 2
1307r = mul t d
1308z = add get og 17 r
1309hf = add get og 15 r
1310frms hf get og 33 z add get og 16 t
1311pr = add get og 18 mul t get og 0
1312klogit z pr get og 0 d
1313y = get oby add mul b get og 1 t
1314ret olossrow pr get og 0 y
1315end
1316
1317note p[c] = z . wte[c] for c < n (wte rows of length d at offset 0 of ow; z, p in oa)
1318fn klogit zo ulong po ulong n ulong d ulong to void
1319ze = add zo d
1320c = ulong 0
1321while le add c 4 n bound 999999
1322kdot4 zo ze mul c d d
1323kput4 add po c
1324c = add c 4
1325end
1326while lt c n bound 4
1327put oa add po c kdot1 zo ze mul c d
1328c = add c 1
1329end
1330end
1331
1332note ogkm[0] = 0: the backward ReLU mask is the sign of this pass's own FP (every command but
1333note gcmp2); 1: offb calls ogmask, which gpu/gg.om defines for gcmp2 (the chip's FP sign) and
1334note nogpu.om defines as a refusal on the CPU-only targets. ogkm[1] = the sequence in flight.
1335global ogkm ulong 2
1336note ofzlay: when > 0, obwd stops after layer ofzlay[0] and skips obwde (frozen base layers).
1337global ofzlay ulong 1
1338
1339note okv (ham/ollm-speed): the CPU decode's row reuse. okv[0] = the rows of obx row 0 whose layer
1340note activations the last ofwdi left valid, okvt[0..okv[0]) their tokens. ofwd clears okv[0]: it
1341note rewrites the same activation rows for any sequence b.
1342global okv ulong 1
1343global okvt ulong 16384
1344note OMUTI: 1 in the product; 0 in mutant i (okvlcp trusts okv[0] without comparing the tokens), which
1345note kvcmp must catch.
1346const OMUTI ulong 1
1347
1348note forward pass of sequence b (x in obx, y in oby); returns its mean loss
1349fn ofwd b ulong to f64
1350put okv 0 0
1351put ogkm 1 b
1352oemb b
1353xo = onlxon
1354for l ulong get og 56
1355if xo
1356onlxpre l 0 get og 9
1357end
1358olayf l
1359if xo
1360onlxpost l 0 get og 9
1361end
1362end
1363ret ohead b
1364end
1365
1366note the first row ofwdi computes: the common prefix of obx row 0 and okvt, at most okv[0] and
1367note CT - 1 (the last row is always recomputed). Bank-B layers (l >= LAA) restore/save only their
1368note K/V slices (oak/oau) in oab without touching oahin, so KV cache works identically for all L.
1369fn okvlcp to ulong
1370ct = get og 9
1371n = get okv 0
1372if ge n ct
1373n = sub ct 1
1374end
1375if eq OMUTI 0
1376ret n
1377end
1378for i ulong n
1379if ne get obx i get okvt i
1380ret i
1381end
1382end
1383ret n
1384end
1385
1386note ofwdi: ofwd 0 for the decode (olastf on the CPU): rows t0..CT-1 of every layer, t0 = okvlcp,
1387note then the final RMS of row CT - 1 only (olast's z; no head for the other rows, no loss). A causal
1388note row reads only rows at or before it, rows below t0 hold what the same model.om row functions
1389note computed for the same tokens at the same positions, so olast's logits are bit-identical to
1390note ofwd 0 then olast (kvcmp checks every step). Returns t0.
1391fn ofwdi to ulong
1392t0 = okvlcp
1393put okv 0 0
1394put ogkm 1 0
1395ct = get og 9
1396nr = sub ct t0
1397okvemb t0 nr
1398xo = onlxon
1399for l ulong get og 56
1400if xo
1401onlxpre l t0 nr
1402end
1403okvlay l t0 nr
1404if and xo eq OMUTAX 1
1405onlxpost l t0 nr
1406end
1407end
1408okvend ct
1409ret t0
1410end
1411
1412
1413note oemb for rows t0 .. t0 + nr - 1 of sequence 0
1414fn okvemb t0 ulong nr ulong to void
1415d = get og 2
1416for i ulong nr
1417t = add t0 i
1418wr = mul get obx t d
1419pr = add get og 30 mul t d
1420kadd3w add oahin 0 mul t d wr pr d
1421end
1422end
1423
1424note Bank-B KV slice transfer for layer l >= LAA: copy rows [r0 .. r0 + cnt) of oak and oau
1425note between oa and oab (to oab when fo, else from oab). Never touches oahin.
1426fn okvkv l ulong r0 ulong cnt ulong fo bool to void
1427la = get og 38
1428if or lt l la eq cnt 0
1429ret
1430end
1431d = get og 2
1432off = mul r0 d
1433n = mul cnt d
1434s = mul sub l la get og 14
1435oaflc add oak l off add add s oatd 3 off n fo
1436oaflc add oau l off add add s oatd 4 off n fo
1437end
1438
1439note olayf l for rows t0 .. t0 + nr - 1
1440fn okvlay l ulong t0 ulong nr ulong to void
1441ostw olp l
1442okvkv l 0 t0 false
1443if ne get onlc 6 0
1444for i0 ulong nr
1445onlinject l add t0 i0
1446end
1447end
1448for i ulong nr
1449olayf1 l add t0 i
1450end
1451for n ulong get og 3
1452for i2 ulong nr
1453t = add t0 i2
1454fatts l n t
1455fatto l n t
1456end
1457end
1458if ne get onlc 0 0
1459for ih ulong nr
1460onlsteer_head l add t0 ih
1461end
1462end
1463for i3 ulong nr
1464olayf2x l add t0 i3
1465end
1466okvkv l t0 nr true
1467if ne get onlc 10 0
1468onlrec_lay l sub add t0 nr 1
1469end
1470end
1471
1472note the final RMS of row CT - 1 into z (olast reads it), then okv = the CT context tokens
1473fn okvend ct ulong to void
1474d = get og 2
1475t = sub ct 1
1476r = mul t d
1477hf = add get og 15 r
1478z = add get og 17 r
1479frms hf get og 33 z add get og 16 t
1480for i ulong ct
1481put okvt i get obx i
1482end
1483put okv 0 ct
1484if ne get onlc 10 0
1485onlrec_top t
1486end
1487end
1488
1489note ======== Neuralese Phase 1 helpers (non-destructive steering, LTP0 injection, telemetry, adaptive looping)
1490
1491fn onlreset to void
1492for i ulong 16
1493put onlc i 0
1494end
1495for j ulong 8
1496put onlf j 0.0f64
1497put onlx j 0
1498end
1499end
1500
1501fn onlmatchl l ulong to bool
1502tl = get onlc 1
1503ret or eq tl 999 eq tl l
1504end
1505
1506fn onlsteer_head l ulong t ulong to void
1507if or ne get onlc 2 3 not onlmatchl l
1508ret
1509end
1510hi = get onlc 3
1511if ge hi get og 3
1512ret
1513end
1514e = get og 7
1515oo = add add oao l mul t get og 2 mul hi e
1516sc = get onlf 0
1517for i ulong e
1518q = add oo i
1519put oa q mul get oa q sc
1520end
1521end
1522
1523fn onlsteer_ffn l ulong t ulong to void
1524if or ne get onlc 2 1 not onlmatchl l
1525ret
1526end
1527idx = get onlc 3
1528f = get og 4
1529if ge idx f
1530ret
1531end
1532q = add add oarl l mul t f idx
1533put oa q mul get oa q get onlf 0
1534end
1535
1536fn onlsteer_res l ulong t ulong to void
1537if or ne get onlc 2 2 not onlmatchl l
1538ret
1539end
1540idx = get onlc 3
1541d = get og 2
1542if ge idx d
1543ret
1544end
1545q = add add oaout l mul t d idx
1546put oa q add get oa q get onlf 0
1547end
1548
1549fn onlv_rms d ulong to f64
1550ss = 0.0f64
1551for i ulong d
1552v = get onlv i
1553ss = add ss mul v v
1554end
1555ret fsqrt add div ss f64 d 1.0e-12f64
1556end
1557
1558fn onlinject l ulong t ulong to void
1559if ne l get onlc 7
1560ret
1561end
1562d = get og 2
1563ho = add oahin l mul t d
1564sc = div get onlf 1 onlv_rms d
1565for i ulong d
1566q = add ho i
1567dv = mul sc get onlv i
1568put oa q add get oa q dv
1569end
1570end
1571
1572fn onlrec_lay l ulong t ulong to void
1573if ge l 32
1574ret
1575end
1576d = get og 2
1577ou = add oaout l mul t d
1578ss = 0.0f64
1579for i ulong d
1580v = get oa add ou i
1581ss = add ss mul v v
1582end
1583put onln l fsqrt ss
1584end
1585
1586fn onlrec_hf t ulong to void
1587d = get og 2
1588hf = add get og 15 mul t d
1589ss = 0.0f64
1590for i ulong d
1591v = get oa add hf i
1592put onlh i v
1593ss = add ss mul v v
1594end
1595put onlf 5 fsqrt ss
1596end
1597
1598fn onlrec_att ll ulong t ulong to void
1599h = get og 3
1600for n ulong h
1601pa = opar ll n t
1602mx = 0.0f64
1603for j ulong add t 1
1604pv = opag add pa j
1605if gt pv mx
1606mx = pv
1607end
1608end
1609put onla n mx
1610end
1611end
1612
1613fn onlrec_top t ulong to void
1614onlrec_hf t
1615ll = sub get og 56 1
1616onlrec_att ll t
1617onlrec_neur ll t
1618end
1619
1620fn onlrec_neur ll ulong t ulong to void
1621f = get og 4
1622rl = add oarl ll mul t f
1623for k ulong 16
1624put onltn_i k 0
1625put onltn_v k -1.0f64
1626end
1627for j ulong f
1628v = get oa add rl j
1629if gt v get onltn_v 15
1630onlins_neur j v
1631end
1632end
1633end
1634
1635fn onlins_neur idx ulong val f64 to void
1636pos = ulong 15
1637while gt pos 0 bound 16
1638pv = get onltn_v sub pos 1
1639if le val pv
1640break
1641end
1642put onltn_i pos get onltn_i sub pos 1
1643put onltn_v pos pv
1644pos = sub pos 1
1645end
1646put onltn_i pos idx
1647put onltn_v pos val
1648end
1649
1650fn onlent_max o ulong v ulong to f64
1651m = bitcast f64 ulong 18442240474082181120
1652for c ulong v
1653x = get oa add o c
1654if gt x m
1655m = x
1656end
1657end
1658ret m
1659end
1660
1661fn onlent_lsm o ulong v ulong m f64 to f64
1662sm = 0.0f64
1663for c ulong v
1664sm = add sm fexp sub get oa add o c m
1665end
1666ret flog sm
1667end
1668
1669note Non-destructive Shannon entropy (nats) and max-prob of current TMP[0..V) logits
1670fn onlentropy to f64
1671o = get og 27
1672v = get og 0
1673m = onlent_max o v
1674lsm = onlent_lsm o v m
1675ent = 0.0f64
1676mxp = 0.0f64
1677for c ulong v
1678lp = sub sub get oa add o c m lsm
1679p = fexp lp
1680if gt p mxp
1681mxp = p
1682end
1683if gt p 1.0e-15f64
1684ent = sub ent mul p lp
1685end
1686end
1687put onlf 2 ent
1688put onlf 4 mxp
1689ret ent
1690end
1691
1692fn onlcopy_oa dst ulong src ulong d ulong to void
1693for i ulong d
1694put oa add dst i get oa add src i
1695end
1696end
1697
1698note Single extra continuous-thought pass through core layers [c0 .. c0+cn) from pre-coda core output, then coda on row t
1699fn onlpass1 t ulong c0 ulong cn ulong to void
1700d = get og 2
1701r = mul t d
1702coda = sub get og 5 add c0 cn
1703lv = get og 56
1704if gt coda 0
1705csrc = add oaout sub sub lv coda 1 r
1706onlcopy_oa add oahin c0 r csrc d
1707else
1708onlcopy_oa add oahin c0 r add get og 15 r d
1709end
1710for k ulong cn
1711okvlay add c0 k t 1
1712end
1713src = add oaout sub add c0 cn 1 r
1714if gt coda 0
1715cstart = sub lv coda
1716onlcopy_oa add oahin cstart r src d
1717for k2 ulong coda
1718okvlay add cstart k2 t 1
1719end
1720else
1721onlcopy_oa add get og 15 r src d
1722end
1723end
1724
1725note Entropy-gated adaptive continuous-thought looping after olastf (when onlc[4] > 0)
1726fn onladapt to void
1727put onlc 8 0
1728put onlc 9 get og 56
1729ent = onlentropy
1730ex = get onlc 4
1731if eq ex 0
1732ret
1733end
1734if gt ex 4
1735ex = 4
1736end
1737th = div f64 get onlc 5 1000.0
1738if and gt get onlc 5 0 lt ent th
1739ret
1740end
1741onladapt_run ex th
1742end
1743
1744fn onladapt_run ex ulong th f64 to void
1745ct = get og 9
1746t = sub ct 1
1747c0 = get og 57
1748cn = get og 58
1749if eq cn 0
1750c0 = sub get og 5 1
1751cn = 1
1752end
1753for ps ulong ex
1754onlpass1 t c0 cn
1755okvend ct
1756olast
1757ent = onlentropy
1758put onlc 8 add ps 1
1759put onlc 9 add get onlc 9 cn
1760if and gt get onlc 5 0 lt ent th
1761break
1762end
1763end
1764end
1765
1766note ======== Adaptive loop: per-token exit and inter-pass messages (onlx, docs/LOOP.md)
1767note A core pass p runs applications LP + p LC .. LP + (p + 1) LC - 1. Before pass 0 (onlxpre) every
1768note row of the range is active and its input is kept in onlpin. After pass p (onlxpost) every row
1769note still active gets its normalized delta rel = |out - in| / max(|in|, 1e-12); its message
1770note becomes out - in; when rel is below the threshold and a pass remains, the row leaves the loop
1771note (onlxp[t] = p + 1) and olayf2x copies it unchanged through the remaining core applications.
1772note A row still active enters pass p + 1 with out + gain * message. Every quantity of row t reads
1773note only row t, so the full forward (ofwd) and the cached decode (ofwdi) compute the same bits.
1774
1775note true when the hooks run: a loop, and an exit threshold, a message gain or telemetry
1776fn onlxon to bool
1777if le get og 59 1
1778ret false
1779end
1780ret or or ne get onlx 0 0 ne get onlx 1 0 ne get onlx 6 0
1781end
1782
1783note the core pass of application l, or NP when l is outside the core (or there is no loop)
1784fn onlxpass l ulong to ulong
1785np = get og 59
1786if le np 1
1787ret np
1788end
1789lp = get og 57
1790if lt l lp
1791ret np
1792end
1793j = sub l lp
1794lc = get og 58
1795if ge j mul lc np
1796ret np
1797end
1798ret div j lc
1799end
1800
1801note true when row t left the loop before the core pass of application l
1802fn onlfz l ulong t ulong to bool
1803p = onlxpass l
1804if or eq p 0 ge p get og 59
1805ret false
1806end
1807ret le get onlxp t p
1808end
1809
1810note before application l = LP (pass 0): rows t0 .. t0 + nr - 1 active, inputs kept, optional LTP0 message
1811fn onlxpre l ulong t0 ulong nr ulong to void
1812if ne l get og 57
1813ret
1814end
1815for i ulong nr
1816onlxpre1 l add t0 i
1817end
1818end
1819
1820fn onlxpre1 l ulong t ulong to void
1821put onlxp t get og 59
1822put onlmn t 0.0f64
1823put onlxd t 0.0f64
1824d = get og 2
1825r = mul t d
1826ho = add oahin l r
1827if and ne get onlx 2 0 ne get onlx 1 0
1828onlxseed r d
1829onlxmix ho r d
1830put onlmn t onlxnrm r d
1831ret
1832end
1833for i ulong d
1834put onlpin add r i get oa add ho i
1835put onlmsg add r i 0.0f64
1836end
1837end
1838
1839note row t's message = onlf[1] onlv / rms(onlv) (the LTP0 vector handed off as the pass-0 message)
1840fn onlxseed r ulong d ulong to void
1841sc = div get onlf 1 onlv_rms d
1842for i ulong d
1843put onlmsg add r i mul sc get onlv i
1844end
1845end
1846
1847note |onlmsg row at r|
1848fn onlxnrm r ulong d ulong to f64
1849ss = 0.0f64
1850for i ulong d
1851v = get onlmsg add r i
1852ss = add ss mul v v
1853end
1854ret fsqrt ss
1855end
1856
1857note after application l: when it ends core pass p, every row still active in it (onlxrow)
1858fn onlxpost l ulong t0 ulong nr ulong to void
1859p = onlxpass l
1860if ge p get og 59
1861ret
1862end
1863lc = get og 58
1864if ne rem sub l get og 57 lc sub lc 1
1865ret
1866end
1867for i ulong nr
1868t = add t0 i
1869if gt get onlxp t p
1870onlxrow l t p
1871end
1872end
1873end
1874
1875note row t after core pass p (application l its last): delta, message, exit, the next pass's input
1876fn onlxrow l ulong t ulong p ulong to void
1877d = get og 2
1878r = mul t d
1879ou = add oaout l r
1880rel = onlxdel ou r d
1881if eq p 0
1882put onlxd t rel
1883end
1884if ne get onlx 1 0
1885put onlmn t get onlxs 0
1886end
1887q = add p 1
1888if ge q get og 59
1889ret
1890end
1891if ne get onlx 0 0
1892if lt rel div f64 get onlx 0 1000000.0
1893put onlxp t q
1894ret
1895end
1896end
1897onlxmix ou r d
1898end
1899
1900note rel = |out - in| / max(|in|, 1e-12) of the row (out at ou in oa, in at onlpin r); |out - in| into
1901note onlxs[0]; the message onlmsg row = out - in when the gain is on
1902fn onlxdel ou ulong r ulong d ulong to f64
1903ss = 0.0f64
1904si = 0.0f64
1905wm = ne get onlx 1 0
1906for i ulong d
1907pv = get onlpin add r i
1908dv = sub get oa add ou i pv
1909ss = add ss mul dv dv
1910si = add si mul pv pv
1911if wm
1912put onlmsg add r i dv
1913end
1914end
1915put onlxs 0 fsqrt ss
1916ret div fsqrt ss fmax fsqrt si 1.0e-12f64
1917end
1918
1919note the row at ou becomes out + gain * message (gain on) and the next pass's input onlpin
1920fn onlxmix ou ulong r ulong d ulong to void
1921g = div f64 get onlx 1 1000000.0
1922wm = ne get onlx 1 0
1923for i ulong d
1924q = add ou i
1925v = get oa q
1926if wm
1927v = add v mul g get onlmsg add r i
1928put oa q v
1929end
1930put onlpin add r i v
1931end
1932end
1933
1934note backward of the head for row t: dl = (p - onehot(y)) sc; dwte += dl z; dz = dl . wte;
1935note DH[t] = rms backward of HF[t].
1936fn ohbwd b ulong t ulong sc f64 to void
1937v = get og 0
1938pr = add get og 18 mul t v
1939y = get oby add mul b get og 1 t
1940kdlog pr v y sc
1941ohbwd2 pr t
1942end
1943
1944note p[c] = (p[c] - [c = y]) sc for c < n
1945fn kdlog po ulong n ulong y ulong sc f64 to void
1946for c ulong n
1947g = get oa add po c
1948if eq c y
1949g = sub g 1.0
1950end
1951put oa add po c mul g sc
1952end
1953end
1954
1955fn ohbwd2 pr ulong t ulong to void
1956d = get og 2
1957v = get og 0
1958r = mul t d
1959z = add get og 17 r
1960kger pr z get og 10 v d
1961dz = add get og 25 r
1962kvm pr ulong 0 dz v d
1963hf = add get og 15 r
1964dh = add get og 19 r
1965brms hf get og 33 add get og 16 t dz dh
1966end
1967
1968note Mutant lp (the w2 gradient of a layer application that repeats an earlier parameter layer is
1969note dropped, so a core w2 gets only the gradient of its first pass): 1 in the product, 0 in the
1970note mutant, which gcheck on a looped checkpoint must catch. Without a loop the two are the same.
1971note (Named OMUTLP: OMUTL is gpu/gk.om's BF16 mutant l, and the gb10 unit holds both files.)
1972const OMUTLP ulong 1
1973
1974note FFN backward of row t of layer l; DH[t] holds d(out) and gains the rms2 path
1975fn offb l ulong t ulong to void
1976f = get og 4
1977dh = add get og 19 mul t get og 2
1978df = add get og 26 mul t f
1979ozero df f
1980kvta dh ovw2 l df f get og 2
1981if eq get ogkm 0 0
1982kdrelu df add oafp l mul t f f
1983else
1984ogmask df l t
1985end
1986g2 = add get og 10 ovw2 l
1987if or eq OMUTLP 1 eq olp l l
1988kger add oarl l mul t f dh g2 f get og 2
1989end
1990offb2 l t df
1991end
1992
1993note y[j] = 0 where x[j] <= 0, for j < n
1994fn kdrelu yo ulong xo ulong n ulong to void
1995for j ulong n
1996if le get oa add xo j 0.0
1997put oa add yo j 0.0f64
1998end
1999end
2000end
2001
2002fn offb2 l ulong t ulong df ulong to void
2003d = get og 2
2004r = mul t d
2005g1 = add get og 10 ovw1 l
2006kger add oabn l r df g1 d get og 4
2007da = add get og 20 r
2008ozero da d
2009kvta df ovw1 l da d get og 4
2010hm = add oahm l r
2011dh = add get og 19 r
2012brms hm ovg2 l add oas2 l t da dh
2013end
2014
2015note dp[j] = dO[t].u[j] (head n) into TMP; returns sum_j p[j] dp[j]
2016fn oattd l ulong n ulong t ulong to f64
2017e = get og 7
2018dor = add add get og 24 mul t get og 2 mul n e
2019uo = add oau l mul n e
2020pa = opar l n t
2021ret kattd dor uo pa add t 1
2022end
2023
2024fn kattd dor ulong uo ulong pa ulong nj ulong to f64
2025d = get og 2
2026de = add dor get og 7
2027dt = 0.0f64
2028j = ulong 0
2029while le add j 4 nj bound 999999
2030kdot4a dor de uo d
2031dt = kattd4 add pa j add get og 27 j dt
2032uo = add uo mul 4 d
2033j = add j 4
2034end
2035while lt j nj bound 4
2036dp = kdot1a dor de uo
2037put oa add get og 27 j dp
2038a = opag add pa j
2039dt = add dt mul a dp
2040uo = add uo d
2041j = add j 1
2042end
2043ret dt
2044end
2045
2046note TMP[tm + m] = oq[m]; dt += PA[p + m] oq[m] for m < 4 in order
2047fn kattd4 p ulong tm ulong dt f64 to f64
2048for i ulong 4
2049dp = get oq i
2050put oa add tm i dp
2051a = opag add p i
2052dt = add dt mul a dp
2053end
2054ret dt
2055end
2056
2057note scratch offsets of oatta: 0 q[t], 1 dq[t], 2 dO[t], 3 k, 4 dk, 5 du (head n applied)
2058global oz ulong 8
2059
2060note ds[j] = p[j](dp[j] - dot)/sqrt(E); dq[t] += ds k[j]; dk[j] += ds q[t]; du[j] += p[j] dO[t]
2061fn oatta l ulong n ulong t ulong dt f64 to void
2062e = get og 7
2063hn = mul n e
2064r = mul t get og 2
2065put ofs 2 dt
2066put oz 0 add add oaq l r hn
2067put oz 1 add add get og 21 r hn
2068put oz 2 add add get og 24 r hn
2069put oz 3 add oak l hn
2070put oz 4 add get og 22 hn
2071put oz 5 add get og 23 hn
2072pa = opar l n t
2073oatta2 pa add t 1
2074end
2075
2076fn oatta2 pa ulong nj ulong to void
2077d = get og 2
2078e = get og 7
2079for j ulong nj
2080jr = mul j d
2081ds = oads pa j
2082kaxpy get oz 1 add get oz 3 jr ds e
2083if eq OMUTA 1
2084kaxpy add get oz 4 jr get oz 0 ds e
2085else
2086kaxpy add get oz 4 jr get oz 0 neg ds e
2087end
2088pj = opag add pa j
2089kaxpy add get oz 5 jr get oz 2 pj e
2090end
2091end
2092
2093note ds[j] = p[j] (TMP[j] - dot) / sqrt(E), dot in ofs 2
2094fn oads pa ulong j ulong to f64
2095pj = opag add pa j
2096u = sub get oa add get og 27 j get ofs 2
2097ret div mul pj u fsqrt f64 get og 7
2098end
2099
2100fn olayb l ulong to void
2101ostw olp l
2102oaflip l false
2103ct = get og 9
2104for t ulong ct
2105offb l t
2106end
2107for t2 ulong ct
2108olaybo l t2
2109end
2110d = get og 2
2111ozero get og 21 mul ct d
2112ozero get og 22 mul ct d
2113ozero get og 23 mul ct d
2114for n ulong get og 3
2115for t3 ulong ct
2116dt = oattd l n t3
2117oatta l n t3 dt
2118end
2119end
2120for t4 ulong ct
2121olayb2 l t4
2122end
2123end
2124
2125note row t: dO[t] = DH[t] . wo^T; dwo += o[t] DH[t]
2126fn olaybo l ulong t ulong to void
2127d = get og 2
2128r = mul t d
2129dor = add get og 24 r
2130dh = add get og 19 r
2131ozero dor d
2132kvta dh ovwo l dor d d
2133kger add oao l r dh add get og 10 ovwo l d d
2134end
2135
2136note da = dq.wq^T + dk.wk^T + du.wv^T; weight gradients; DH[t] gains the rms1 path
2137fn olayb2 l ulong t ulong to void
2138d = get og 2
2139r = mul t d
2140da = add get og 20 r
2141ozero da d
2142kvta add get og 21 r ovwq l da d d
2143kvta add get og 22 r ovwk l da d d
2144kvta add get og 23 r ovwv l da d d
2145olayb3 l t
2146end
2147
2148fn olayb3 l ulong t ulong to void
2149d = get og 2
2150r = mul t d
2151a = add oaa l r
2152g0 = get og 10
2153kger a add get og 21 r add g0 ovwq l d d
2154kger a add get og 22 r add g0 ovwk l d d
2155kger a add get og 23 r add g0 ovwv l d d
2156hi = add oahin l r
2157dh = add get og 19 r
2158brms hi ovg1 l add oas1 l t add get og 20 r dh
2159end
2160
2161note backward pass of sequence b after ofwd b; sc = 1/(T B) scales dl so the gradient is that of
2162note the step loss. Gradients accumulate into the gradient at og[10] in ogr.
2163fn obwd b ulong sc f64 to void
2164ct = get og 9
2165ozero get og 19 mul ct get og 2
2166for t ulong ct
2167ohbwd b t sc
2168end
2169ll = get og 56
2170fl = get ofzlay 0
2171for i ulong sub ll fl
2172olayb sub sub ll 1 i
2173end
2174if eq fl 0
2175for t2 ulong ct
2176obwde b t2
2177end
2178end
2179end
2180
2181note row t: dwte[x[t]] += DH[t]; dwpe[t] += DH[t]
2182fn obwde b ulong t ulong to void
2183d = get og 2
2184c = get obx add mul b get og 1 t
2185dh = add get og 19 mul t d
2186g0 = get og 10
2187kaxpg add g0 mul c d dh 1.0 d
2188kaxpg add add g0 get og 30 mul t d dh 1.0 d
2189end