interplane.Get Omega
Read in your language

omega-tool-model

First-party Omega tool-use model source, serving harnesses, and verification records

Brought by drakestapleton

branch main Branches Tags History

omega-tool-model / deployment / speed / timed.om

11505 lines · 244102 bytes Raw Blame History
1global tmarks ulong 180
2global tbytes uchar 1440
3note SPDX-License-Identifier: AGPL-3.0-only
4note omegallm math.om (aien-train#52): first-party f64 exp and log, splitmix64, FNV-1a 64,
5note little-endian byte packing and the stdout writer. First unit; see docs/omegallm/README.md.
6note Constants (Wolfram, 2026-11-01):
7note splitmix64: IntegerString[11400714819323198485,16] -> 9e3779b97f4a7c15,
8note IntegerString[13787848793156543929,16] -> bf58476d1ce4e5b9,
9note IntegerString[10723151780598845931,16] -> 94d049bb133111eb.
10note FNV-1a 64: IntegerString[14695981039346656037,16] -> cbf29ce484222325, prime 100000001b3.
11note 1/ln2: N[1/Log[2]] bits 4609176140021203710 -> 1.4426950408889634.
12note ln2 hi = 0x3FE62E42FEE00000 (low 21 bits zero, so k*hi is exact for |k| < 2^20)
13note = 0.6931471803691238; lo = N[Log[2]-hi] -> 1.9082149292705877*^-10.
14note overflow: N[Log[$MaxMachineNumber]] -> 709.782712893384; underflow to 0 below
15note N[Log[2^-1075]] -> -745.1332191019412.
16note 1/k! for k=2..13 and 1/(2k+1) for k=1..11: Table[ToString[N[1/k!],InputForm],{k,2,13}],
17note Table[ToString[N[1/(2k+1)],InputForm],{k,1,11}] (values below).
18note Taylor remainder of exp at |r| <= ln2/2: N[0.3466^14/14!] -> 4.14*^-18 (degree 13).
19note atanh series remainder at s <= 3-2Sqrt[2]: N[(3-2Sqrt[2])^2]^11/23 -> 6.25*^-19 (to s^23).
20note bit patterns: 2^52 = 4503599627370496, +inf 9218868437227405312, -inf 18442240474082181120,
21note NaN 9221120237041090560, exponent 1023 field 4607182418800017408, 2^54 = 18014398509481984.
22
23const OMOUT ulong 4096
24global oout uchar 4096
25global oon ulong 1
26note og: run geometry shared by all units (slots listed in model.om).
27global og ulong 64
28
29fn smx x ulong to ulong
30z = add x 11400714819323198485
31z = mul xor z shr z 30 13787848793156543929
32z = mul xor z shr z 27 10723151780598845931
33ret xor z shr z 31
34end
35
36note e^x for every f64 x. Cody-Waite reduction x = k ln2 + r with |r| <= ln2/2, then the Taylor
37note polynomial of degree 13 in Horner form, then scaling by 2^k1 * 2^k2 (k1 = k/2) so every
38note intermediate is normal and a subnormal result is rounded once.
39fn fexp x f64 to f64
40if ne x x
41ret x
42end
43if gt x 709.782712893384
44ret bitcast f64 ulong 9218868437227405312
45end
46if lt x -745.1332191019412
47ret 0.0f64
48end
49kf = mul x 1.4426950408889634
50if ge kf 0.0
51kf = add kf 0.5
52else
53kf = sub kf 0.5
54end
55k = long kf
56fk = f64 k
57r = sub sub x mul fk 0.6931471803691238 mul fk 1.9082149292705877e-10
58p = add mul r 1.6059043836821613e-10 2.08767569878681e-9
59p = add mul p r 2.505210838544172e-8
60p = add mul p r 2.755731922398589e-7
61p = add mul p r 2.7557319223985893e-6
62p = add mul p r 0.0000248015873015873
63p = add mul p r 0.0001984126984126984
64p = add mul p r 0.001388888888888889
65p = add mul p r 0.008333333333333333
66p = add mul p r 0.041666666666666664
67p = add mul p r 0.16666666666666666
68p = add mul p r 0.5
69p = add mul p r 1.0
70p = add mul p r 1.0
71k1 = div k 2
72k2 = sub k k1
73s1 = bitcast f64 shl ulong add k1 1023 52
74s2 = bitcast f64 shl ulong add k2 1023 52
75ret mul mul p s1 s2
76end
77
78note natural log for every f64 x: NaN for x < 0 or NaN, -inf for 0, +inf for +inf. x = m 2^e with
79note m in [sqrt(1/2), sqrt 2]; log m = 2 atanh(s), s = (m-1)/(m+1), series to s^23.
80fn flog x f64 to f64
81if ne x x
82ret x
83end
84if lt x 0.0
85ret bitcast f64 ulong 9221120237041090560
86end
87if eq x 0.0
88ret bitcast f64 ulong 18442240474082181120
89end
90b = bitcast ulong x
91if eq b 9218868437227405312
92ret x
93end
94e = long 0
95if lt b 4503599627370496
96b = bitcast ulong mul x 18014398509481984.0
97e = long -54
98end
99e = add e sub long shr b 52 1023
100m = bitcast f64 or and b 4503599627370495 4607182418800017408
101if gt m 1.4142135623730951
102m = mul m 0.5
103e = add e 1
104end
105f = sub m 1.0
106s = div f add f 2.0
107z = mul s s
108q = add mul z 0.043478260869565216 0.047619047619047616
109q = add mul q z 0.05263157894736842
110q = add mul q z 0.058823529411764705
111q = add mul q z 0.06666666666666667
112q = add mul q z 0.07692307692307693
113q = add mul q z 0.09090909090909091
114q = add mul q z 0.1111111111111111
115q = add mul q z 0.14285714285714285
116q = add mul q z 0.2
117q = add mul q z 0.3333333333333333
118q = add mul q z 1.0
119lm = mul add s s q
120fe = f64 e
121ret add mul fe 0.6931471803691238 add lm mul fe 1.9082149292705877e-10
122end
123
124note round(v * 10^6) half away from zero (CONTRACT section 7).
125fn rmic v f64 to long
126x = mul v 1000000.0
127q = long x
128fr = sub x f64 q
129if ge fr 0.5
130q = add q 1
131end
132if le fr -0.5
133q = sub q 1
134end
135ret q
136end
137
138fn oputc c ulong to void
139n = get oon 0
140if ge n OMOUT
141oflush
142n = ulong 0
143end
144put oout n uchar and c 255
145put oon 0 add n 1
146end
147
148fn oputu v ulong to void
149d = new uchar 24 0
150n = ulong 0
151x = v
152while true bound 21
153put d n uchar add rem x 10 48
154n = add n 1
155x = div x 10
156if eq x 0
157break
158end
159end
160for i ulong n
161oputc ulong get d sub sub n 1 i
162end
163end
164
165fn oputl v long to void
166if lt v 0
167oputc 45
168if eq v -9223372036854775808
169oputu 9223372036854775808
170else
171oputu ulong neg v
172end
173else
174oputu ulong v
175end
176end
177
178note 16 lowercase hex digits.
179fn oputx v ulong to void
180for i ulong 16
181h = and shr v mul sub 15 i 4 15
182if lt h 10
183oputc add h 48
184else
185oputc add h 87
186end
187end
188end
189note SPDX-License-Identifier: AGPL-3.0-only
190note omegallm data.om (aien-train#52): argument paths, whole-file data reads, prep (CONTRACT
191note section 4), the splitmix64 batch rule and the add-one byte baselines. Needs math.om before it.
192note A data file lives in two byte pools (od0 then od1), 2^25 bytes in all; a byte is read by odb.
193const ODHALF ulong 16777216
194const ODCAP ulong 33554432
195global od0 uchar 16777216
196global od1 uchar 16777216
197global odn ulong 1
198global oarg uchar 4100
199global ochk uchar 65536
200global obx ulong 16384
201global oby ulong 16384
202global owb0 uchar 65536
203global owb1 uchar 65536
204global owbn ulong 2
205global ocnt ulong 65536
206global ocnu ulong 256
207global ocnr ulong 256
208global oseps uchar 16
209note v1 (CONTRACT-V1 section 3): the window that the platform's positioned read oprd fills; prep,
210note the bpe commands and the windowed data reads all use it.
211const OPWCAP ulong 2097152
212global opw uchar 2097152
213
214note exit codes of this unit: 2 usage, 20 data file empty or too short for T, 21 data file over
215note 2^25 bytes, 70 I/O.
216
217note argument i into oarg; returns its length (1 .. 4096) or exits 2.
218fn oargp i ulong to ulong
219r = oargr i
220if or le r 0 gt r 4096
221exit 2
222end
223ret ulong r
224end
225
226note argv[i] as an unsigned decimal of 1 .. 19 digits.
227fn oargu i ulong to ulong
228n = oargp i
229if gt n 19
230exit 2
231end
232v = ulong 0
233for k ulong n
234c = ulong get oarg k
235if or lt c 48 gt c 57
236exit 2
237end
238v = add mul v 10 sub c 48
239end
240ret v
241end
242
243fn odb i ulong to ulong
244if lt i ODHALF
245ret ulong get od0 i
246end
247ret ulong get od1 sub i ODHALF
248end
249
250fn odset i ulong c ulong to void
251if lt i ODHALF
252put od0 i uchar c
253else
254put od1 sub i ODHALF uchar c
255end
256end
257
258note prep output: two buffered writers (k = 0 TRAIN, k = 1 HELD); owopen, owflush and owclose are
259note the platform unit's (lio.om, eio.om).
260fn owbyte k ulong c ulong to void
261n = get owbn k
262if ge n 65536
263owflush k
264n = ulong 0
265end
266if eq k 0
267put owb0 n uchar c
268else
269put owb1 n uchar c
270end
271put owbn k add n 1
272end
273
274note 1 when the separator <|endoftext|> starts at opw[p] (p + 13 <= n).
275fn oissep p ulong n ulong to bool
276if gt add p 13 n
277ret false
278end
279for k ulong 13
280if ne get opw add p k get oseps k
281ret false
282end
283end
284ret true
285end
286
287fn owsp c ulong to bool
288ret or or eq c 9 eq c 10 or eq c 13 eq c 32
289end
290
291note Emit story opw[a, e) after trimming whitespace; an empty story is dropped. st[0..4] count
292note stories, train stories, held stories, train bytes, held bytes. A story over 999999 bytes is
293note refused with exit 24 (cmdprep refuses it already while scanning; every loop here is bounded).
294fn ostory a ulong e ulong st edit ulong 8 to void
295if gt sub e a 999999
296exit 24
297end
298lo = a
299hi = e
300while lt lo hi bound 999999
301if not owsp ulong get opw lo
302break
303end
304lo = add lo 1
305end
306while gt hi lo bound 999999
307if not owsp ulong get opw sub hi 1
308break
309end
310hi = sub hi 1
311end
312if lt lo hi
313k = ulong 0
314if eq rem get st 0 10 9
315k = ulong 1
316end
317put st 0 add get st 0 1
318put st add k 1 add get st add k 1 1
319for i ulong sub hi lo
320owbyte k ulong get opw add lo i
321end
322owbyte k 0
323put st add k 3 add add get st add k 3 sub hi lo 1
324end
325end
326
327note `<|endoftext|>` = 60 124 101 110 100 111 102 116 101 120 116 124 62.
328fn oseptab to void
329put oseps 0 60
330put oseps 1 124
331put oseps 2 101
332put oseps 3 110
333put oseps 4 100
334put oseps 5 111
335put oseps 6 102
336put oseps 7 116
337put oseps 8 101
338put oseps 9 120
339put oseps 10 116
340put oseps 11 124
341put oseps 12 62
342end
343
344note Stream window over argv[ai] (prep and the bpe commands): opw holds the file's bytes
345note [wb, wb + wn) with opst = wb, wn, eof (1 once a refill came back short). opstart fills it from
346note byte 0; opshift a moves opw[a, wn) to the front and refills the rest of the window.
347global opst ulong 4
348fn opstart ai ulong to void
349wn = oprd ai 0 0 OPWCAP
350put opst 0 0
351put opst 1 wn
352put opst 2 0
353if lt wn OPWCAP
354put opst 2 1
355end
356end
357
358fn opshift ai ulong a ulong to void
359wn = get opst 1
360cnt = sub wn a
361i = ulong 0
362for o ulong 3
363lim = mul add o 1 999999
364if gt lim cnt
365lim = cnt
366end
367while lt i lim bound 999999
368put opw i get opw add a i
369i = add i 1
370end
371end
372wb = add get opst 0 a
373wn = sub wn a
374r = oprd ai add wb wn wn sub OPWCAP wn
375put opst 0 wb
376put opst 1 add wn r
377put opst 2 0
378if lt r sub OPWCAP wn
379put opst 2 1
380end
381end
382
383note prep IN TRAIN HELD (CONTRACT-V1 section 3): IN is read as a stream through the window opw.
384note The current story starts at opw[a]; p scans. When the separator test at p needs bytes past
385note the window, opshift moves [a, wn) to the front and refills. A story whose raw length passes
386note 999999 is exit 24 as soon as the scan shows it (v0 refused the same story, after the same
387note earlier stories, in ostory); so a moved story keeps at most 1000012 bytes and every refill
388note reads at least 1097140. Each inner pass advances p by 1 or 13 or ends the scan: 999999 x 999999
389note passes cover any IN below 2^39 bytes.
390fn cmdprep to void
391if ne oargc 5
392exit 2
393end
394oseptab
395opstart 2
396wn = get opst 1
397eof = ne get opst 2 0
398owopen 0 3
399owopen 1 4
400st = new ulong 8 0
401a = ulong 0
402p = ulong 0
403dn = false
404while not dn bound 999999
405q = ulong 0
406while and not dn lt q 999999 bound 999999
407q = add q 1
408if and gt add p 13 wn not eof
409opshift 2 a
410p = sub p a
411a = ulong 0
412wn = get opst 1
413eof = ne get opst 2 0
414end
415if ge p wn
416dn = true
417else
418if eq get opw p 60
419if oissep p wn
420ostory a p st
421p = add p 13
422a = p
423continue
424end
425end
426p = add p 1
427if gt sub p a 999999
428exit 24
429end
430end
431end
432end
433ostory a wn st
434owclose 0
435owclose 1
436oputc 80
437for i ulong 5
438oputc 32
439oputu get st i
440end
441oputc 10
442oflush
443end
444
445note Batch for step s (CONTRACT section 4): x and y for b = 0..B-1 into obx/oby (stride T); N, the
446note data length in tokens, is odn[0] (odopen).
447note OMUTB is the target offset: 1 in the product, 2 in mutant b (targets shifted by one).
448const OMUTB ulong 1
449fn obatch s ulong to void
450bb = get og 6
451tt = get og 1
452n = get odn 0
453sd = get og 40
454for b ulong bb
455o = rem smx xor sd mul add mul s bb b 11400714819323198485 sub n tt
456obrow b o
457end
458end
459
460note v1 data file (CONTRACT-V1 section 3): argument ai holds bytes when V = 256 and u16
461note little-endian tokens when V > 256 (og 49 = bytes per token, og 47 = the argument). Sets
462note odn[0] = N tokens; exit 21 on an odd byte length at V > 256, 20 when N < T + 1. Rows are read
463note through oprd when used, so a token >= V is refused (25) only in a row actually read.
464fn odopen ai ulong to void
465w = ulong 1
466if gt get og 0 256
467w = 2
468end
469nb = ofsz ai
470if ne rem nb w 0
471exit 21
472end
473put og 47 ai
474put og 49 w
475put odn 0 div nb w
476if le get odn 0 get og 1
477exit 20
478end
479end
480
481note token k of the window last read by odrow. OMUTE: 1 in the product (u16 little-endian); 0 in
482note mutant e (big-endian), which the referee comparison must catch.
483const OMUTE ulong 1
484fn odtk k ulong to ulong
485if eq get og 49 1
486ret ulong get opw k
487end
488lo = ulong get opw mul k 2
489hi = ulong get opw add mul k 2 1
490if eq OMUTE 0
491ret or shl lo 8 hi
492end
493ret or shl hi 8 lo
494end
495
496note read tokens o .. o + n - 1 (fewer at the end of the file; the rest read as 0) into opw;
497note exit 25 when a token read is >= V
498fn odrow o ulong n ulong to void
499w = get og 49
500nb = mul n w
501for i ulong nb
502put opw i 0
503end
504r = oprd get og 47 mul o w 0 nb
505m = div r w
506v = get og 0
507for k ulong m
508if ge odtk k v
509exit 25
510end
511end
512end
513
514fn obrow b ulong o ulong to void
515tt = get og 1
516odrow o add tt OMUTB
517for t ulong tt
518put obx add mul b tt t odtk t
519put oby add mul b tt t odtk add t OMUTB
520end
521end
522
523note base TRAIN HELD (CONTRACT section 7 and agreed reading A5): unigram and bigram add-one
524note counts over all TRAIN bytes and all N-1 adjacent TRAIN pairs; scored on HELD targets 1..M-1.
525fn cmdbase to void
526if ne oargc 4
527exit 2
528end
529n = odread 2
530if lt n 2
531exit 20
532end
533for o ulong 64
534for i ulong 524288
535j = add mul o 524288 i
536if ge j n
537break
538end
539obfit j
540end
541end
542m = odread 3
543if lt m 2
544exit 20
545end
546su = 0.0f64
547sb = 0.0f64
548dn = f64 add n 256
549for o2 ulong 64
550for i2 ulong 524288
551j2 = add mul o2 524288 i2
552if ge j2 m
553break
554end
555if gt j2 0
556su = add su obsu j2 dn
557sb = add sb obsb j2
558end
559end
560end
561fm = f64 sub m 1
562oputc 66
563oputc 32
564oputl rmic div div neg su fm 0.6931471805599453
565oputc 32
566oputl rmic div div neg sb fm 0.6931471805599453
567oputc 10
568oflush
569end
570
571note count TRAIN byte j and the pair (j-1, j)
572fn obfit j ulong to void
573c = odb j
574put ocnu c add get ocnu c 1
575if gt j 0
576pv = odb sub j 1
577q = add mul pv 256 c
578put ocnt q add get ocnt q 1
579put ocnr pv add get ocnr pv 1
580end
581end
582
583note log of the unigram probability (c+1)/(N+256) of HELD byte j
584fn obsu j ulong dn f64 to f64
585ret flog div f64 add get ocnu odb j 1 dn
586end
587
588note log of the bigram probability (c+1)/(row+256) of HELD pair (j-1, j)
589fn obsb j ulong to f64
590pv = odb sub j 1
591q = add mul pv 256 odb j
592ret flog div f64 add get ocnt q 1 f64 add get ocnr pv 256
593end
594note SPDX-License-Identifier: AGPL-3.0-only
595note omegallm lpool.om (aien-train#52, CONTRACT-V3 section 2): the bank-B pools of the Linux targets
596note (aarch64, gb10, x86linux), concatenated after data.om and before model.om; eio targets use
597note epool.om. Sizes at the CONTRACT-V3 maxima (V 4096, T 256, D 384, H 16, F 1536, L 16), Wolfram:
598note oww parameters of layers LA .. L-1: (16 - 7) LSTR = 9 1770240 -> 15932160 (LA = 7)
599note ogb BLK values past OGC: P - OGC = 29995392 - 15833472 -> 14161920
600note oab activations of layers LAA .. L-1: (16 - 6) ASTR = 10 1573376 -> 15733760 (LAA = 6)
601const OWWN ulong 15932160
602global oww f64 15932160
603const OGBN ulong 14161920
604global ogb f64 14161920
605const OABN ulong 15733760
606global oab f64 15733760
607note SPDX-License-Identifier: AGPL-3.0-only
608note omegallm model.om (aien-train#52): geometry, forward pass and hand-derived backward pass
609note (CONTRACT sections 1-3, CONTRACT-V1 section 1). Needs math.om and data.om before it.
610note
611note f64 values live in banked pools (one omc global holds at most 16777216 elements, and all
612note globals together at most 2^30 bytes, omc error 23; CONTRACT-V3 section 2):
613note ow parameter bank A: [0, SB) of the CONTRACT section 2 order, then a one-layer window of
614note LSTR values at og[36] and the final gain gf at og[37]; oww (lpool.om/epool.om) bank B holds
615note layers LA .. L-1 at (l - LA) LSTR. When P <= OWC, SB = GF, LA = L and ow is the v2 layout.
616note Every index outside the kernels is virtual (the section 2 order); owg/owp map it, and opx
617note maps a hot index of bank A or the staged layer (og[48]).
618note the gradient pool [0, 2P): GRD [0, P) in the platform cold store (ocsg/ocsp), BLK [P, 2P) in
619note ogr[j] below OGC and ogb[j - OGC] from OGC (j = i - P), reached through ogrg/ogrp/ogra
620note the Adam pool [0, 2P) (omvg/omvp): m at 0, v at P, at cold-store index P + i
621note oa activations: layers below LAA at l ASTR, later layers in two windows at AW + (l mod 2) ASTR
622note flushed to bank B oab at (l - LAA) ASTR after their forward and staged back before their
623note backward; HF and the backward temporaries follow. When END <= OAC, LAA = L (v2 layout).
624note opa attention probabilities PA[l][n][t][j] of every layer
625note og slots:
626note 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)
627note 10 GRD (base in the gradient pool of the gradient being accumulated: 0, or P for BLK) 11 MOM
628note (0 in the Adam pool) 12 VEL (P) 13 ACT (0 in oa) 14 ASTR (per-layer activation stride) 15 HF
629note 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
630note 29 PSTR (per-layer PA stride H T T in opa) 30 WPE 31 LAY0 32 LSTR (per-layer parameter
631note stride) 33 GF 34 SB (first virtual index of bank B) 35 LA (first layer in bank B) 36 window base
632note in ow 37 gf in ow 38 LAA (first layer in the activation windows) 39 AW (activation window
633note base) 48 the layer staged in the parameter window (L: none)
634note 40 seed S 41 step 42 TOTAL 43 lr bits 44 G (gradient blocks) 45 K (workers)
635note 46 checkpoint magic of the run (1 OLLMCK01, 2 OLLMCK02)
636note Per layer activations (T rows of width D unless noted): HIN, A, Q, K, U, O, HMID, BN, S1[T],
637note S2[T], FP[T][F] (pre-relu), RL[T][F]. Every sum runs over its index ascending.
638note Sizes at the CONTRACT-V2 maxima (V 4096, T 256, D 384, H 16, F 1536, L 8), Wolfram:
639note P = V D + T D + L (4 D^2 + 2 D F + 2 D) + D -> 15833472
640note ASTR = 8 T D + 2 T + 2 T F -> 1573376; END = L ASTR + 10 T D + T + T V + T F + 4096 -> 15016192
641note and at the CONTRACT-V3 maxima (L 16): P 29995392, PA = L H T T -> 16777216; the bank-B sizes
642note are in lpool.om. ogeom refuses any layout past a pool.
643note Bank A capacities: OWC parameters, OGC BLK values, OAC activations (the bank-crossing test
644note build of tests/omegallm/build.sh shrinks all three).
645const OWC ulong 15833472
646global ow f64 15833472
647const OGC ulong 15833472
648global ogr f64 15833472
649const OAC ulong 15016192
650global oa f64 15016192
651const OPAN ulong 16777216
652global opa f64 16777216
653
654note gradient pool element i (i < 2 P)
655fn ogrg i ulong to f64
656p = get og 8
657if lt i p
658ret ocsg i
659end
660j = sub i p
661if lt j OGC
662ret get ogr j
663end
664ret get ogb sub j OGC
665end
666
667fn ogrp i ulong v f64 to void
668p = get og 8
669if lt i p
670ocsp i v
671ret
672end
673j = sub i p
674if lt j OGC
675put ogr j v
676else
677put ogb sub j OGC v
678end
679end
680
681note gradient pool element i += a
682fn ogra i ulong a f64 to void
683p = get og 8
684if lt i p
685ocsp i add ocsg i a
686ret
687end
688j = sub i p
689if lt j OGC
690put ogr j add get ogr j a
691else
692k = sub j OGC
693put ogb k add get ogb k a
694end
695end
696
697note Adam pool element i (i < 2 P), cold-store index P + i
698fn omvg i ulong to f64
699ret ocsg add get og 8 i
700end
701
702fn omvp i ulong v f64 to void
703ocsp add get og 8 i v
704end
705
706note parameter at virtual index i (bank A, bank B or gf), and its store
707fn owg i ulong to f64
708sb = get og 34
709if lt i sb
710ret get ow i
711end
712g = get og 33
713if ge i g
714ret get ow add get og 37 sub i g
715end
716ret get oww sub i sb
717end
718
719fn owp i ulong v f64 to void
720sb = get og 34
721if lt i sb
722put ow i v
723ret
724end
725g = get og 33
726if ge i g
727put ow add get og 37 sub i g v
728ret
729end
730put oww sub i sb v
731end
732
733note ow index of virtual index i for the kernels: bank A as is, gf at og[37], a bank-B layer only
734note when it is the staged layer og[48] (else exit 16, a missing ostw)
735fn opx i ulong to ulong
736if lt i get og 34
737ret i
738end
739g = get og 33
740if ge i g
741ret add get og 37 sub i g
742end
743l = div sub i get og 31 get og 32
744if ne l get og 48
745exit 16
746end
747ret add get og 36 sub i opg1 l
748end
749
750note Mutant y (wrong bank-B layer: ostw stages layer LA's block for every layer): 1 in the product,
751note 0 in the mutant.
752const OMUTY ulong 1
753
754const OCH ulong 999999
755
756note stage bank-B layer l into the parameter window (layers below LA live in bank A)
757fn ostw l ulong to void
758la = get og 35
759if lt l la
760ret
761end
762n = get og 32
763s = mul sub l la n
764if eq OMUTY 0
765s = 0
766end
767ostwc get og 36 s n
768put og 48 l
769end
770
771note ow[wb + i] = oww[s + i] for i < n (chunked: a loop runs at most 999999 passes)
772fn ostwc wb ulong s ulong n ulong to void
773for q ulong ocn n
774i = mul q OCH
775ie = oce q n
776while lt i ie bound 999999
777put ow add wb i get oww add s i
778i = add i 1
779end
780end
781end
782
783note copy layer l's activation block between its window and bank B oab (to oab when fo)
784fn oaflip l ulong fo bool to void
785la = get og 38
786if lt l la
787ret
788end
789n = get og 14
790s = mul sub l la n
791oaflc oahin l s n fo
792end
793
794note oab[s + i] = oa[w + i] for i < n when fo, else the reverse
795fn oaflc w ulong s ulong n ulong fo bool to void
796if fo
797oafo w s n
798else
799oafi w s n
800end
801end
802
803fn oafo w ulong s ulong n ulong to void
804for q ulong ocn n
805i = mul q OCH
806ie = oce q n
807while lt i ie bound 999999
808put oab add s i get oa add w i
809i = add i 1
810end
811end
812end
813
814fn oafi w ulong s ulong n ulong to void
815for q ulong ocn n
816i = mul q OCH
817ie = oce q n
818while lt i ie bound 999999
819put oa add w i get oab add s i
820i = add i 1
821end
822end
823end
824
825note TMP holds V logits (sample) or T attention dots
826const OTMP ulong 4096
827
828note Mutant a (attention backward sign flip in the dK term): 1 in the product, 0 in the mutant.
829const OMUTA ulong 1
830
831note Configure og from og[0..6]; false when a shape is zero, over the maximum, or D mod H != 0, or
832note when the layout does not fit the pools.
833fn ogeom to bool
834if not ogok
835ret false
836end
837t = get og 1
838d = get og 2
839put og 7 div d get og 3
840u = mul 4 mul d d
841u = add u mul 2 mul d get og 4
842u = add u mul 2 d
843put og 32 u
844put og 30 mul get og 0 d
845u = mul t d
846put og 31 add get og 30 u
847u = mul get og 5 get og 32
848put og 33 add get og 31 u
849u = add get og 33 d
850put og 8 u
851put og 9 t
852put og 10 0
853put og 11 0
854put og 12 u
855put og 13 0
856put og 29 mul mul get og 3 t t
857u = mul t d
858put og 14 oastr u
859if not ogwb
860ret false
861end
862if not ogab u
863ret false
864end
865p = get og 8
866if gt p add OGC OGBN
867ret false
868end
869if gt mul 3 p ocsinit
870ret false
871end
872ret le mul get og 5 get og 29 OPAN
873end
874
875note parameter banks: v2 layout when P <= OWC, else LA layers in bank A, the window and gf after
876note them, layers LA .. L-1 in oww
877fn ogwb to bool
878ll = get og 5
879g = get og 33
880put og 34 g
881put og 35 ll
882put og 36 g
883put og 37 g
884put og 48 ll
885if le get og 8 OWC
886ret true
887end
888ls = get og 32
889nd = add add get og 31 ls get og 2
890if lt OWC nd
891ret false
892end
893la = div sub OWC nd ls
894if gt mul sub ll la ls OWWN
895ret false
896end
897sb = add get og 31 mul la ls
898put og 34 sb
899put og 35 la
900put og 36 sb
901put og 37 add sb ls
902ret true
903end
904
905note activation banks: v2 layout when END <= OAC, else LAA layers, two windows, then HF and the
906note backward temporaries
907fn ogab td ulong to bool
908ll = get og 5
909st = get og 14
910put og 38 ll
911put og 39 mul ll st
912ogeom3 td mul ll st
913ogeom2 td
914if le get og 28 OAC
915ret true
916end
917w2 = add sub get og 28 get og 15 mul 2 st
918if lt OAC w2
919ret false
920end
921la = div sub OAC w2 st
922if gt mul sub ll la st OABN
923ret false
924end
925aw = mul la st
926put og 38 la
927put og 39 aw
928ogeom3 td add aw mul 2 st
929ogeom2 td
930ret le get og 28 OAC
931end
932
933fn ogeom3 td ulong hf ulong to void
934put og 15 hf
935put og 16 add get og 15 td
936put og 17 add get og 16 get og 1
937put og 18 add get og 17 td
938end
939
940note per-layer activation stride: 8 T D + 2 T + 2 T F
941fn oastr td ulong to ulong
942t = get og 1
943a = add mul 8 td mul 2 t
944ret add a mul mul 2 t get og 4
945end
946
947note shape limits (CONTRACT-V3 section 1): every shape >= 1, 256 <= V <= 4096, T <= 256,
948note D <= 384, H <= 16, F <= 1536, L <= 16, B <= 64, D mod H = 0
949fn ogok to bool
950for i ulong 7
951if eq get og i 0
952ret false
953end
954end
955if or lt get og 0 256 gt get og 0 4096
956ret false
957end
958if or gt get og 1 256 gt get og 2 384
959ret false
960end
961if or gt get og 3 16 gt get og 4 1536
962ret false
963end
964if or gt get og 5 16 gt get og 6 64
965ret false
966end
967ret eq rem get og 2 get og 3 0
968end
969
970fn ogeom2 td ulong to void
971t = get og 1
972put og 19 add get og 18 mul t get og 0
973put og 20 add get og 19 td
974put og 21 add get og 20 td
975put og 22 add get og 21 td
976put og 23 add get og 22 td
977put og 24 add get og 23 td
978put og 25 add get og 24 td
979put og 26 add get og 25 td
980put og 27 add get og 26 mul t get og 4
981put og 28 add get og 27 OTMP
982end
983
984note parameter offsets of layer l
985fn opg1 l ulong to ulong
986ret add get og 31 mul l get og 32
987end
988fn opwq l ulong to ulong
989ret add opg1 l get og 2
990end
991fn opwk l ulong to ulong
992d = get og 2
993ret add opwq l mul d d
994end
995fn opwv l ulong to ulong
996d = get og 2
997ret add opwk l mul d d
998end
999fn opwo l ulong to ulong
1000d = get og 2
1001ret add opwv l mul d d
1002end
1003fn opg2 l ulong to ulong
1004d = get og 2
1005ret add opwo l mul d d
1006end
1007fn opw1 l ulong to ulong
1008ret add opg2 l get og 2
1009end
1010fn opw2 l ulong to ulong
1011ret add opw1 l mul get og 2 get og 4
1012end
1013
1014note activation offsets of layer l (layers from LAA in the two windows at AW)
1015fn oahin l ulong to ulong
1016if lt l get og 38
1017ret add get og 13 mul l get og 14
1018end
1019ret add get og 39 mul rem l 2 get og 14
1020end
1021fn oatd n ulong to ulong
1022ret mul n mul get og 1 get og 2
1023end
1024fn oaa l ulong to ulong
1025ret add oahin l oatd 1
1026end
1027fn oaq l ulong to ulong
1028ret add oahin l oatd 2
1029end
1030fn oak l ulong to ulong
1031ret add oahin l oatd 3
1032end
1033fn oau l ulong to ulong
1034ret add oahin l oatd 4
1035end
1036fn oao l ulong to ulong
1037ret add oahin l oatd 5
1038end
1039fn oahm l ulong to ulong
1040ret add oahin l oatd 6
1041end
1042fn oabn l ulong to ulong
1043ret add oahin l oatd 7
1044end
1045fn oas1 l ulong to ulong
1046ret add oahin l oatd 8
1047end
1048fn oas2 l ulong to ulong
1049ret add oas1 l get og 1
1050end
1051fn oafp l ulong to ulong
1052ret add oas2 l get og 1
1053end
1054fn oarl l ulong to ulong
1055ret add oafp l mul get og 1 get og 4
1056end
1057note output rows of layer l: the next layer's HIN, or HF after the last layer
1058fn oaout l ulong to ulong
1059if lt add l 1 get og 5
1060ret oahin add l 1
1061end
1062ret get og 15
1063end
1064
1065note Kernels. The fast forms keep every v0 sum: each output is one accumulator that starts at
1066note 0.0 and adds its terms in the v0 index order with the v0 expression (add s mul a w); four
1067note outputs run side by side in separate accumulators, so every result has v0's bits. Results of
1068note the four-wide blocks pass through oq.
1069global oq f64 4
1070
1071note y[0..c) = x[0..r) . W[r][c] (overwrites y); x, y in oa, W in ow at virtual index wv
1072fn kvm xo ulong wv ulong yo ulong r ulong c ulong to void
1073wo = opx wv
1074xe = add xo r
1075j = ulong 0
1076while le add j 4 c bound 999999
1077kvmb xo xe add wo j c
1078kput4 add yo j
1079j = add j 4
1080end
1081while lt j c bound 4
1082put oa add yo j kvm1 xo xe add wo j c
1083j = add j 1
1084end
1085end
1086
1087note oa[yo..yo+4) = oq[0..4)
1088fn kput4 yo ulong to void
1089put oa yo get oq 0
1090put oa add yo 1 get oq 1
1091put oa add yo 2 get oq 2
1092put oa add yo 3 get oq 3
1093end
1094
1095note oq[m] = sum over xo <= i < xe of x[i] W[wi + m + (i - xo) c], m < 4
1096fn kvmb xo ulong xe ulong wi ulong c ulong to void
1097s0 = 0.0f64
1098s1 = 0.0f64
1099s2 = 0.0f64
1100s3 = 0.0f64
1101while lt xo xe bound 999999
1102a = get oa xo
1103s0 = add s0 mul a get ow wi
1104s1 = add s1 mul a get ow add wi 1
1105s2 = add s2 mul a get ow add wi 2
1106s3 = add s3 mul a get ow add wi 3
1107wi = add wi c
1108xo = add xo 1
1109end
1110put oq 0 s0
1111put oq 1 s1
1112put oq 2 s2
1113put oq 3 s3
1114end
1115
1116note sum over xo <= i < xe of x[i] W[wi + (i - xo) c]
1117fn kvm1 xo ulong xe ulong wi ulong c ulong to f64
1118s = 0.0f64
1119while lt xo xe bound 999999
1120a = get oa xo
1121s = add s mul a get ow wi
1122wi = add wi c
1123xo = add xo 1
1124end
1125ret s
1126end
1127
1128note 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
1129note index wv
1130fn kvta dyo ulong wv ulong yo ulong r ulong c ulong to void
1131wo = opx wv
1132de = add dyo c
1133k = ulong 0
1134while le add k 4 r bound 999999
1135kdot4 dyo de add wo mul k c c
1136kacc4 add yo k
1137k = add k 4
1138end
1139while lt k r bound 4
1140a = add get oa add yo k kdot1 dyo de add wo mul k c
1141put oa add yo k a
1142k = add k 1
1143end
1144end
1145
1146note oa[yo + m] = oa[yo + m] + oq[m], m < 4
1147fn kacc4 yo ulong to void
1148put oa yo add get oa yo get oq 0
1149q = add yo 1
1150put oa q add get oa q get oq 1
1151q = add yo 2
1152put oa q add get oa q get oq 2
1153q = add yo 3
1154put oa q add get oa q get oq 3
1155end
1156
1157note oq[m] = sum over xo <= i < xe of x[i] W[wi + m c + (i - xo)], m < 4; x in oa, W in ow
1158fn kdot4 xo ulong xe ulong wi ulong c ulong to void
1159s0 = 0.0f64
1160s1 = 0.0f64
1161s2 = 0.0f64
1162s3 = 0.0f64
1163w2 = add wi add c c
1164while lt xo xe bound 999999
1165a = get oa xo
1166s0 = add s0 mul a get ow wi
1167s1 = add s1 mul a get ow add wi c
1168s2 = add s2 mul a get ow w2
1169s3 = add s3 mul a get ow add w2 c
1170wi = add wi 1
1171w2 = add w2 1
1172xo = add xo 1
1173end
1174put oq 0 s0
1175put oq 1 s1
1176put oq 2 s2
1177put oq 3 s3
1178end
1179
1180note sum over xo <= i < xe of x[i] W[wi + (i - xo)]; x in oa, W in ow
1181fn kdot1 xo ulong xe ulong wi ulong to f64
1182s = 0.0f64
1183while lt xo xe bound 999999
1184a = get oa xo
1185s = add s mul a get ow wi
1186wi = add wi 1
1187xo = add xo 1
1188end
1189ret s
1190end
1191
1192note kdot4 with both operands in oa
1193fn kdot4a xo ulong xe ulong wi ulong c ulong to void
1194s0 = 0.0f64
1195s1 = 0.0f64
1196s2 = 0.0f64
1197s3 = 0.0f64
1198w2 = add wi add c c
1199while lt xo xe bound 999999
1200a = get oa xo
1201s0 = add s0 mul a get oa wi
1202s1 = add s1 mul a get oa add wi c
1203s2 = add s2 mul a get oa w2
1204s3 = add s3 mul a get oa add w2 c
1205wi = add wi 1
1206w2 = add w2 1
1207xo = add xo 1
1208end
1209put oq 0 s0
1210put oq 1 s1
1211put oq 2 s2
1212put oq 3 s3
1213end
1214
1215note kdot1 with both operands in oa
1216fn kdot1a xo ulong xe ulong wi ulong to f64
1217s = 0.0f64
1218while lt xo xe bound 999999
1219a = get oa xo
1220s = add s mul a get oa wi
1221wi = add wi 1
1222xo = add xo 1
1223end
1224ret s
1225end
1226
1227note G[k][j] += x[k] dy[j] for k < r, j < c (outer product into a gradient); x, dy in oa, G in ogr
1228fn kger xo ulong dyo ulong go ulong r ulong c ulong to void
1229for k ulong r
1230kger1 get oa add xo k add go mul k c dyo c
1231end
1232end
1233
1234note G[gi + j] += xv dy[j] for j < c (elements independent, four per pass)
1235fn kger1 xv f64 gi ulong yi ulong c ulong to void
1236ye = add yi c
1237while le add yi 4 ye bound 999999
1238ogra gi mul xv get oa yi
1239ogra add gi 1 mul xv get oa add yi 1
1240ogra add gi 2 mul xv get oa add yi 2
1241ogra add gi 3 mul xv get oa add yi 3
1242gi = add gi 4
1243yi = add yi 4
1244end
1245while lt yi ye bound 4
1246ogra gi mul xv get oa yi
1247gi = add gi 1
1248yi = add yi 1
1249end
1250end
1251
1252note y[i] += a x[i]; x, y in oa
1253fn kaxpy yo ulong xo ulong a f64 n ulong to void
1254for i ulong n
1255q = add yo i
1256v = mul a get oa add xo i
1257put oa q add get oa q v
1258end
1259end
1260
1261note y[i] += a x[i]; x in oa, y in the gradient pool
1262fn kaxpg yo ulong xo ulong a f64 n ulong to void
1263for i ulong n
1264ogra add yo i mul a get oa add xo i
1265end
1266end
1267
1268fn ozero o ulong n ulong to void
1269for i ulong n
1270put oa add o i 0.0f64
1271end
1272end
1273
1274fn ozerog o ulong n ulong to void
1275for q ulong ocn n
1276i = mul q OCH
1277ie = oce q n
1278while lt i ie bound 999999
1279ogrp add o i 0.0f64
1280i = add i 1
1281end
1282end
1283end
1284
1285note A loop runs at most 999999 passes (the while bound; a for over a name has the same guard), so
1286note loops over P or more run in ocn n chunks of OCH; chunk q covers [q OCH, oce q n).
1287fn ocn n ulong to ulong
1288ret div add n 999998 OCH
1289end
1290fn oce q ulong n ulong to ulong
1291e = mul add q 1 OCH
1292if gt e n
1293ret n
1294end
1295ret e
1296end
1297
1298note 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
1299note index gv
1300fn frms vo ulong gv ulong yo ulong so ulong to void
1301go = opx gv
1302d = get og 2
1303ss = 0.0f64
1304for i ulong d
1305v = get oa add vo i
1306ss = add ss mul v v
1307end
1308s = fsqrt add div ss f64 d 0.00001
1309put oa so s
1310for j ulong d
1311v = div get oa add vo j s
1312v = mul v get ow add go j
1313put oa add yo j v
1314end
1315end
1316
1317note rms backward: n = v/s, dn = da g, c = (sum dn n)/D; dv = (dn - n c)/s added to dh;
1318note dg += da n added to the gain's gradient. go is the gain's virtual index.
1319global ofs f64 8
1320fn brms vo ulong go ulong so ulong dao ulong dho ulong to void
1321d = get og 2
1322put ofs 0 get oa so
1323gg = add get og 10 go
1324go = opx go
1325put ofs 1 div brmsc vo go dao get ofs 0 f64 d
1326for j ulong d
1327brms1 add vo j add go j add dao j add dho j add gg j
1328end
1329end
1330
1331note sum over i < D of (da[i] g[i]) (v[i]/s), ascending
1332fn brmsc vo ulong go ulong dao ulong s f64 to f64
1333c = 0.0f64
1334for i ulong get og 2
1335dn = get oa add dao i
1336dn = mul dn get ow add go i
1337v = div get oa add vo i s
1338c = add c mul dn v
1339end
1340ret c
1341end
1342
1343note one element of the rms backward; s = ofs[0], c = ofs[1]
1344fn brms1 vp ulong gp ulong dap ulong dhp ulong ggp ulong to void
1345s = get ofs 0
1346n = div get oa vp s
1347da = get oa dap
1348ogra ggp mul da n
1349dn = mul da get ow gp
1350dn = div sub dn mul n get ofs 1 s
1351put oa dhp add get oa dhp dn
1352end
1353
1354note z[i] = a[i] + b[i] for i < n (all in oa)
1355fn kadd3 zo ulong ao ulong bo ulong n ulong to void
1356for i ulong n
1357v = get oa add ao i
1358v = add v get oa add bo i
1359put oa add zo i v
1360end
1361end
1362
1363note z[i] = a[i] + b[i] for i < n (a, b in ow, z in oa)
1364fn kadd3w zo ulong ao ulong bo ulong n ulong to void
1365for i ulong n
1366v = get ow add ao i
1367v = add v get ow add bo i
1368put oa add zo i v
1369end
1370end
1371
1372note h[t] = wte[x[t]] + wpe[t] for sequence b of the batch
1373fn oemb b ulong to void
1374d = get og 2
1375tt = get og 1
1376for t ulong get og 9
1377wr = get obx add mul b tt t
1378wr = mul wr d
1379pr = add get og 30 mul t d
1380kadd3w add oahin 0 mul t d wr pr d
1381end
1382end
1383
1384note offset in opa of PA[n][t][0] of layer l
1385fn opar l ulong n ulong t ulong to ulong
1386tt = get og 1
1387p = add mul n tt t
1388ret add mul l get og 29 mul p tt
1389end
1390
1391note attention scores and softmax of head n, row t: PA[n][t][j] for j <= t
1392fn fatts l ulong n ulong t ulong to void
1393e = get og 7
1394qo = add add oaq l mul t get og 2 mul n e
1395ko = add oak l mul n e
1396pa = opar l n t
1397m = fattsk qo ko pa add t 1
1398sm = osofp pa add t 1 m
1399end
1400
1401note PA row: s[j] = q.k[j]/sqrt(E) for j < nj, k rows stepping by D; returns the max score
1402fn fattsk qo ulong ko ulong pa ulong nj ulong to f64
1403d = get og 2
1404e = get og 7
1405isq = fsqrt f64 e
1406m = bitcast f64 ulong 18442240474082181120
1407qe = add qo e
1408j = ulong 0
1409while le add j 4 nj bound 999999
1410kdot4a qo qe ko d
1411m = fattp4 add pa j isq m
1412ko = add ko mul 4 d
1413j = add j 4
1414end
1415while lt j nj bound 4
1416s = div kdot1a qo qe ko isq
1417put opa add pa j s
1418m = fmax m s
1419ko = add ko d
1420j = add j 1
1421end
1422ret m
1423end
1424
1425note PA[p + m] = oq[m] / isq for m < 4 in order; returns the running max
1426fn fattp4 p ulong isq f64 m f64 to f64
1427for i ulong 4
1428s = div get oq i isq
1429put opa add p i s
1430m = fmax m s
1431end
1432ret m
1433end
1434
1435note p = exp(x - m) / sum over n entries at o in oa (in place); returns sum
1436fn osoft o ulong n ulong m f64 to f64
1437sm = 0.0f64
1438for j ulong n
1439ev = fexp sub get oa add o j m
1440put oa add o j ev
1441sm = add sm ev
1442end
1443for k ulong n
1444v = div get oa add o k sm
1445put oa add o k v
1446end
1447ret sm
1448end
1449
1450note osoft on opa
1451fn osofp o ulong n ulong m f64 to f64
1452sm = 0.0f64
1453for j ulong n
1454ev = fexp sub get opa add o j m
1455put opa add o j ev
1456sm = add sm ev
1457end
1458for k ulong n
1459v = div get opa add o k sm
1460put opa add o k v
1461end
1462ret sm
1463end
1464
1465note o[t][nE + i] = sum_{j <= t} p[t][j] u[j][nE + i]
1466fn fatto l ulong n ulong t ulong to void
1467e = get og 7
1468oo = add add oao l mul t get og 2 mul n e
1469uo = add oau l mul n e
1470pa = opar l n t
1471ozero oo e
1472fattok oo uo pa add t 1
1473end
1474
1475note o[0..E) += p[j] u[j][0..E) for j < nj, u rows stepping by D
1476fn fattok oo ulong uo ulong pa ulong nj ulong to void
1477d = get og 2
1478e = get og 7
1479for j ulong nj
1480kaxpy oo uo get opa add pa j e
1481uo = add uo d
1482end
1483end
1484
1485fn olayf l ulong to void
1486ostw l
1487for t ulong get og 9
1488olayf1 l t
1489end
1490for n ulong get og 3
1491for t2 ulong get og 9
1492fatts l n t2
1493fatto l n t2
1494end
1495end
1496for t3 ulong get og 9
1497olayf2 l t3
1498end
1499oaflip l true
1500end
1501
1502note row t: a = rms(h, g1); q, k, u = a.wq, a.wk, a.wv
1503fn olayf1 l ulong t ulong to void
1504d = get og 2
1505r = mul t d
1506a = add oaa l r
1507frms add oahin l r opg1 l a add oas1 l t
1508kvm a opwq l add oaq l r d d
1509kvm a opwk l add oak l r d d
1510kvm a opwv l add oau l r d d
1511end
1512
1513note row t: h += o.wo; b = rms(h, g2); out = h + relu(b.w1).w2
1514fn olayf2 l ulong t ulong to void
1515d = get og 2
1516f = get og 4
1517r = mul t d
1518hm = add oahm l r
1519kvm add oao l r opwo l hm d d
1520kadd3 hm add oahin l r hm d
1521frms hm opg2 l add oabn l r add oas2 l t
1522fp = add oafp l mul t f
1523kvm add oabn l r opw1 l fp d f
1524rl = add oarl l mul t f
1525krelu rl fp f
1526ou = add oaout l r
1527kvm rl opw2 l ou f d
1528kadd3 ou hm ou d
1529end
1530
1531note y[j] = x[j] when x[j] > 0, else 0, for j < n
1532fn krelu yo ulong xo ulong n ulong to void
1533for j ulong n
1534v = get oa add xo j
1535if le v 0.0
1536v = 0.0f64
1537end
1538put oa add yo j v
1539end
1540end
1541
1542note loss of row t: the logits at o (n entries) become probabilities; returns
1543note log(sum exp(l - max)) - (l_y - max)
1544fn olossrow o ulong n ulong y ulong to f64
1545m = bitcast f64 ulong 18442240474082181120
1546for j ulong n
1547m = fmax m get oa add o j
1548end
1549ly = get oa add o y
1550sm = osoft o n m
1551ret sub flog sm sub ly m
1552end
1553
1554note final rms, tied head and loss of sequence b; returns the mean loss over CT positions
1555fn ohead b ulong to f64
1556ls = 0.0f64
1557for t ulong get og 9
1558ls = add ls oheadr b t
1559end
1560ret div ls f64 get og 9
1561end
1562
1563note row t of the head: z = rms(HF[t], gf); logits = z . wte^T; returns the row loss
1564fn oheadr b ulong t ulong to f64
1565d = get og 2
1566r = mul t d
1567z = add get og 17 r
1568hf = add get og 15 r
1569frms hf get og 33 z add get og 16 t
1570pr = add get og 18 mul t get og 0
1571klogit z pr get og 0 d
1572y = get oby add mul b get og 1 t
1573ret olossrow pr get og 0 y
1574end
1575
1576note p[c] = z . wte[c] for c < n (wte rows of length d at offset 0 of ow; z, p in oa)
1577fn klogit zo ulong po ulong n ulong d ulong to void
1578ze = add zo d
1579c = ulong 0
1580while le add c 4 n bound 999999
1581kdot4 zo ze mul c d d
1582kput4 add po c
1583c = add c 4
1584end
1585while lt c n bound 4
1586put oa add po c kdot1 zo ze mul c d
1587c = add c 1
1588end
1589end
1590
1591note ogkm[0] = 0: the backward ReLU mask is the sign of this pass's own FP (every command but
1592note gcmp2); 1: offb calls ogmask, which gpu/gg.om defines for gcmp2 (the chip's FP sign) and
1593note nogpu.om defines as a refusal on the CPU-only targets. ogkm[1] = the sequence in flight.
1594global ogkm ulong 2
1595
1596note forward pass of sequence b (x in obx, y in oby); returns its mean loss
1597fn ofwd b ulong to f64
1598put ogkm 1 b
1599oemb b
1600for l ulong get og 5
1601olayf l
1602end
1603ret ohead b
1604end
1605
1606note backward of the head for row t: dl = (p - onehot(y)) sc; dwte += dl z; dz = dl . wte;
1607note DH[t] = rms backward of HF[t].
1608fn ohbwd b ulong t ulong sc f64 to void
1609v = get og 0
1610pr = add get og 18 mul t v
1611y = get oby add mul b get og 1 t
1612kdlog pr v y sc
1613ohbwd2 pr t
1614end
1615
1616note p[c] = (p[c] - [c = y]) sc for c < n
1617fn kdlog po ulong n ulong y ulong sc f64 to void
1618for c ulong n
1619g = get oa add po c
1620if eq c y
1621g = sub g 1.0
1622end
1623put oa add po c mul g sc
1624end
1625end
1626
1627fn ohbwd2 pr ulong t ulong to void
1628d = get og 2
1629v = get og 0
1630r = mul t d
1631z = add get og 17 r
1632kger pr z get og 10 v d
1633dz = add get og 25 r
1634kvm pr ulong 0 dz v d
1635hf = add get og 15 r
1636dh = add get og 19 r
1637brms hf get og 33 add get og 16 t dz dh
1638end
1639
1640note FFN backward of row t of layer l; DH[t] holds d(out) and gains the rms2 path
1641fn offb l ulong t ulong to void
1642f = get og 4
1643dh = add get og 19 mul t get og 2
1644df = add get og 26 mul t f
1645ozero df f
1646kvta dh opw2 l df f get og 2
1647if eq get ogkm 0 0
1648kdrelu df add oafp l mul t f f
1649else
1650ogmask df l t
1651end
1652g2 = add get og 10 opw2 l
1653kger add oarl l mul t f dh g2 f get og 2
1654offb2 l t df
1655end
1656
1657note y[j] = 0 where x[j] <= 0, for j < n
1658fn kdrelu yo ulong xo ulong n ulong to void
1659for j ulong n
1660if le get oa add xo j 0.0
1661put oa add yo j 0.0f64
1662end
1663end
1664end
1665
1666fn offb2 l ulong t ulong df ulong to void
1667d = get og 2
1668r = mul t d
1669g1 = add get og 10 opw1 l
1670kger add oabn l r df g1 d get og 4
1671da = add get og 20 r
1672ozero da d
1673kvta df opw1 l da d get og 4
1674hm = add oahm l r
1675dh = add get og 19 r
1676brms hm opg2 l add oas2 l t da dh
1677end
1678
1679note dp[j] = dO[t].u[j] (head n) into TMP; returns sum_j p[j] dp[j]
1680fn oattd l ulong n ulong t ulong to f64
1681e = get og 7
1682dor = add add get og 24 mul t get og 2 mul n e
1683uo = add oau l mul n e
1684pa = opar l n t
1685ret kattd dor uo pa add t 1
1686end
1687
1688fn kattd dor ulong uo ulong pa ulong nj ulong to f64
1689d = get og 2
1690de = add dor get og 7
1691dt = 0.0f64
1692j = ulong 0
1693while le add j 4 nj bound 999999
1694kdot4a dor de uo d
1695dt = kattd4 add pa j add get og 27 j dt
1696uo = add uo mul 4 d
1697j = add j 4
1698end
1699while lt j nj bound 4
1700dp = kdot1a dor de uo
1701put oa add get og 27 j dp
1702a = get opa add pa j
1703dt = add dt mul a dp
1704uo = add uo d
1705j = add j 1
1706end
1707ret dt
1708end
1709
1710note TMP[tm + m] = oq[m]; dt += PA[p + m] oq[m] for m < 4 in order
1711fn kattd4 p ulong tm ulong dt f64 to f64
1712for i ulong 4
1713dp = get oq i
1714put oa add tm i dp
1715a = get opa add p i
1716dt = add dt mul a dp
1717end
1718ret dt
1719end
1720
1721note scratch offsets of oatta: 0 q[t], 1 dq[t], 2 dO[t], 3 k, 4 dk, 5 du (head n applied)
1722global oz ulong 8
1723
1724note 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]
1725fn oatta l ulong n ulong t ulong dt f64 to void
1726e = get og 7
1727hn = mul n e
1728r = mul t get og 2
1729put ofs 2 dt
1730put oz 0 add add oaq l r hn
1731put oz 1 add add get og 21 r hn
1732put oz 2 add add get og 24 r hn
1733put oz 3 add oak l hn
1734put oz 4 add get og 22 hn
1735put oz 5 add get og 23 hn
1736pa = opar l n t
1737oatta2 pa add t 1
1738end
1739
1740fn oatta2 pa ulong nj ulong to void
1741d = get og 2
1742e = get og 7
1743for j ulong nj
1744jr = mul j d
1745ds = oads pa j
1746kaxpy get oz 1 add get oz 3 jr ds e
1747if eq OMUTA 1
1748kaxpy add get oz 4 jr get oz 0 ds e
1749else
1750kaxpy add get oz 4 jr get oz 0 neg ds e
1751end
1752pj = get opa add pa j
1753kaxpy add get oz 5 jr get oz 2 pj e
1754end
1755end
1756
1757note ds[j] = p[j] (TMP[j] - dot) / sqrt(E), dot in ofs 2
1758fn oads pa ulong j ulong to f64
1759pj = get opa add pa j
1760u = sub get oa add get og 27 j get ofs 2
1761ret div mul pj u fsqrt f64 get og 7
1762end
1763
1764fn olayb l ulong to void
1765ostw l
1766oaflip l false
1767ct = get og 9
1768for t ulong ct
1769offb l t
1770end
1771for t2 ulong ct
1772olaybo l t2
1773end
1774d = get og 2
1775ozero get og 21 mul ct d
1776ozero get og 22 mul ct d
1777ozero get og 23 mul ct d
1778for n ulong get og 3
1779for t3 ulong ct
1780dt = oattd l n t3
1781oatta l n t3 dt
1782end
1783end
1784for t4 ulong ct
1785olayb2 l t4
1786end
1787end
1788
1789note row t: dO[t] = DH[t] . wo^T; dwo += o[t] DH[t]
1790fn olaybo l ulong t ulong to void
1791d = get og 2
1792r = mul t d
1793dor = add get og 24 r
1794dh = add get og 19 r
1795ozero dor d
1796kvta dh opwo l dor d d
1797kger add oao l r dh add get og 10 opwo l d d
1798end
1799
1800note da = dq.wq^T + dk.wk^T + du.wv^T; weight gradients; DH[t] gains the rms1 path
1801fn olayb2 l ulong t ulong to void
1802d = get og 2
1803r = mul t d
1804da = add get og 20 r
1805ozero da d
1806kvta add get og 21 r opwq l da d d
1807kvta add get og 22 r opwk l da d d
1808kvta add get og 23 r opwv l da d d
1809olayb3 l t
1810end
1811
1812fn olayb3 l ulong t ulong to void
1813d = get og 2
1814r = mul t d
1815a = add oaa l r
1816g0 = get og 10
1817kger a add get og 21 r add g0 opwq l d d
1818kger a add get og 22 r add g0 opwk l d d
1819kger a add get og 23 r add g0 opwv l d d
1820hi = add oahin l r
1821dh = add get og 19 r
1822brms hi opg1 l add oas1 l t add get og 20 r dh
1823end
1824
1825note backward pass of sequence b after ofwd b; sc = 1/(T B) scales dl so the gradient is that of
1826note the step loss. Gradients accumulate into the gradient at og[10] in ogr.
1827fn obwd b ulong sc f64 to void
1828ct = get og 9
1829ozero get og 19 mul ct get og 2
1830for t ulong ct
1831ohbwd b t sc
1832end
1833ll = get og 5
1834for i ulong ll
1835olayb sub sub ll 1 i
1836end
1837for t2 ulong ct
1838obwde b t2
1839end
1840end
1841
1842note row t: dwte[x[t]] += DH[t]; dwpe[t] += DH[t]
1843fn obwde b ulong t ulong to void
1844d = get og 2
1845c = get obx add mul b get og 1 t
1846dh = add get og 19 mul t d
1847g0 = get og 10
1848kaxpg add g0 mul c d dh 1.0 d
1849kaxpg add add g0 get og 30 mul t d dh 1.0 d
1850end
1851note SPDX-License-Identifier: AGPL-3.0-only
1852note omegallm adam.om (aien-train#52): init, global-norm clip, Adam with bias correction, the LR
1853note schedule, gradient blocks (CONTRACT-V1 section 5) and one training step (CONTRACT section 5).
1854note Needs math.om, data.om, model.om.
1855note Exact operation order (agreed with the oracle lane, recorded in CONTRACT section 5):
1856note n = sqrt(sum_i g_i^2) (i ascending); if n > 1: c = 1/n, g_i = g_i c.
1857note b^(s+1) by s+1 repeated multiplications starting from 1.0; bc = 1 - b^(s+1).
1858note m = 0.9 m + (1.0 - 0.9) g; v = 0.99 v + (1.0 - 0.99)(g g);
1859note p = p - (lr_s (m/bc1)) / (sqrt(v/bc2) + 1e-8).
1860note lr_s = (lr (s+1))/100 for s < 100, else lr (1 - (0.9 (s-100))/max(1, TOTAL-100)).
1861
1862note Mutant c (bias correction removed): 1 in the product, 0 in the mutant.
1863const OMUTC ulong 1
1864
1865note Mutant d (gradient blocks combined in reverse order, G-1 first): 1 in the product, 0 in the
1866note mutant.
1867const OMUTD ulong 1
1868
1869note per-sequence losses of the current batch
1870global olos f64 64
1871
1872note init (CONTRACT section 5): r_i = (splitmix64(S+i) >> 11) 2^-53 (N[2^-53] ->
1873note 1.1102230246251565*^-16); w = (2 r - 1) 0.034641016151377546 (N[0.02 Sqrt[3]]); wo and w2
1874note times 1/sqrt(2L); gains 1; m = v = 0.
1875fn oinit to void
1876p = get og 8
1877sd = get og 40
1878for q ulong ocn p
1879i = mul q OCH
1880ie = oce q p
1881while lt i ie bound 999999
1882r = mul f64 shr smx add sd i 11 1.1102230246251565e-16
1883owp i mul sub add r r 1.0 0.034641016151377546
1884i = add i 1
1885end
1886end
1887d = get og 2
1888ri = div 1.0f64 fsqrt f64 mul 2 get og 5
1889for l ulong get og 5
1890oiset opg1 l d 1.0
1891oiset opg2 l d 1.0
1892oiscale opwo l mul d d ri
1893oiscale opw2 l mul d get og 4 ri
1894end
1895oiset get og 33 d 1.0
1896ozerom mul 2 p
1897end
1898
1899fn ozerom n ulong to void
1900for q ulong ocn n
1901i = mul q OCH
1902ie = oce q n
1903while lt i ie bound 999999
1904omvp i 0.0f64
1905i = add i 1
1906end
1907end
1908end
1909
1910fn oiset o ulong n ulong v f64 to void
1911for i ulong n
1912owp add o i v
1913end
1914end
1915
1916fn oiscale o ulong n ulong v f64 to void
1917for i ulong n
1918owp add o i mul owg add o i v
1919end
1920end
1921
1922fn olrs s ulong lr f64 to f64
1923if lt s 100
1924ret div mul lr f64 add s 1 100.0
1925end
1926den = ulong 1
1927if gt get og 42 101
1928den = sub get og 42 100
1929end
1930ret mul lr sub 1.0 div mul 0.9 f64 sub s 100 f64 den
1931end
1932
1933fn opowb b f64 s ulong to f64
1934pw = 1.0f64
1935for i ulong add s 1
1936pw = mul pw b
1937end
1938ret pw
1939end
1940
1941note clip the gradient to global norm 1 and apply one Adam update at step og[41]
1942fn oadam to void
1943p = get og 8
1944ss = 0.0f64
1945for q ulong ocn p
1946i = mul q OCH
1947ie = oce q p
1948while lt i ie bound 999999
1949g = ocsg i
1950ss = add ss mul g g
1951i = add i 1
1952end
1953end
1954nr = fsqrt ss
1955if gt nr 1.0
1956oclip div 1.0 nr
1957end
1958s = get og 41
1959lrs = olrs s bitcast f64 get og 43
1960bc1 = sub 1.0 opowb 0.9 s
1961bc2 = sub 1.0 opowb 0.99 s
1962if eq OMUTC 0
1963bc1 = 1.0f64
1964bc2 = 1.0f64
1965end
1966put ofs 3 lrs
1967put ofs 4 bc1
1968put ofs 5 bc2
1969for q2 ulong ocn p
1970k = mul q2 OCH
1971ke = oce q2 p
1972while lt k ke bound 999999
1973oadam1 k
1974k = add k 1
1975end
1976end
1977end
1978
1979note g_i = g_i c for i < P
1980fn oclip c f64 to void
1981p = get og 8
1982for q ulong ocn p
1983j = mul q OCH
1984je = oce q p
1985while lt j je bound 999999
1986ocsp j mul ocsg j c
1987j = add j 1
1988end
1989end
1990end
1991
1992note moments of parameter i: m = 0.9 m + (1 - 0.9) g; v = 0.99 v + (1 - 0.99) g^2
1993fn oadam1 i ulong to void
1994g = ogrg add get og 10 i
1995mo = add get og 11 i
1996m = omvg mo
1997m = mul 0.9 m
1998a = sub 1.0f64 0.9
1999m = add m mul a g
2000omvp mo m
2001vo = add get og 12 i
2002v = omvg vo
2003v = mul 0.99 v
2004a = sub 1.0f64 0.99
2005v = add v mul a mul g g
2006omvp vo v
2007oadam2 i m v
2008end
2009
2010note p[i] -= lr (m/bc1) / (sqrt(v/bc2) + 1e-8) with lr, bc1, bc2 in ofs 3, 4, 5
2011fn oadam2 i ulong m f64 v f64 to void
2012u = div m get ofs 4
2013u = mul get ofs 3 u
2014w = div v get ofs 5
2015w = add fsqrt w 0.00000001
2016u = div u w
2017w = owg i
2018owp i sub w u
2019end
2020
2021note one training step at step og[41] on TRAIN; returns the batch loss at the pre-update
2022note parameters and increments the step. ogradw (lio.om, eio.om) runs the K workers.
2023fn ostep to f64
2024s = get og 41
2025obatch s
2026ls = ogradw
2027oadam
2028put og 41 add s 1
2029ret ls
2030end
2031
2032note gradient of the step loss for the batch in obx/oby (no clip, no update); returns the loss.
2033note CONTRACT-V1 section 5: G = og[44] blocks of R = B/G sequences; block j is accumulated from
2034note zero for b ascending; GRD = block 0, then GRD[i] = GRD[i] + block j[i] for j ascending.
2035note G = 1 is the v0 gradient bit for bit. Every block is accumulated in BLK (CONTRACT-V3 section 2)
2036note and block 0 copied into GRD, the same bits as accumulating it in GRD.
2037fn ograd to f64
2038gg = get og 44
2039for i ulong gg
2040j = i
2041if eq OMUTD 0
2042j = sub sub gg 1 i
2043end
2044oblkc j
2045ogfold eq i 0
2046end
2047ret olsum
2048end
2049
2050note block j into BLK (gradient pool [P, 2P), zeroed first); losses into olos
2051fn oblkc j ulong to void
2052p = get og 8
2053put og 10 p
2054ozerog p p
2055bb = get og 6
2056sc = div 1.0f64 f64 mul get og 1 bb
2057r = div bb get og 44
2058b = mul j r
2059for i ulong r
2060put olos b ofwd b
2061obwd b sc
2062b = add b 1
2063end
2064put og 10 0
2065end
2066
2067note BLK into GRD: copied for the first block, else GRD[i] = GRD[i] + BLK[i] (ogadd)
2068fn ogfold first bool to void
2069if not first
2070ogadd
2071ret
2072end
2073p = get og 8
2074for q ulong ocn p
2075i = mul q OCH
2076ie = oce q p
2077while lt i ie bound 999999
2078ocsp i ogrg add p i
2079i = add i 1
2080end
2081end
2082end
2083
2084note GRD[i] = GRD[i] + BLK[i] for i ascending
2085fn ogadd to void
2086p = get og 8
2087for q ulong ocn p
2088i = mul q OCH
2089ie = oce q p
2090while lt i ie bound 999999
2091ogadd1 i p
2092i = add i 1
2093end
2094end
2095end
2096
2097note GRD[i] += BLK[i] (BLK at P)
2098fn ogadd1 i ulong p ulong to void
2099x = ogrg add p i
2100ocsp i add ocsg i x
2101end
2102
2103note step loss: sum of olos[b] for b ascending from 0.0, divided by f64 B
2104fn olsum to f64
2105ls = 0.0f64
2106bb = get og 6
2107for b ulong bb
2108ls = add ls get olos b
2109end
2110ret div ls f64 bb
2111end
2112
2113note adamck: Adam self-check on the small shape. With a constant gradient g from zero moments,
2114note exact arithmetic gives m/bc1 = g and v/bc2 = g^2 at every step, so after K steps
2115note p = -sum_s lr_s g/(|g| + 1e-8). Prints `A K maxrel_bits_hex maxrel_e15` and exits 41 when
2116note maxrel > 1e-12. Without bias correction (mutant c) the step-1 ratio is exactly 1 and the step-2
2117note ratio is 1.347; the query and the predicted 0.63909 are in docs/omegallm/README.md, "Sizing".
2118fn cmdadamck to void
2119put og 0 256
2120put og 1 8
2121put og 2 16
2122put og 3 2
2123put og 4 32
2124put og 5 2
2125put og 6 2
2126if not ogeom
2127exit 12
2128end
2129put og 42 200
2130put og 43 bitcast ulong 0.001f64
2131put og 41 0
2132p = get og 8
2133for i ulong p
2134x = sub f64 rem i 7 3.0
2135x = mul x 0.0001
2136ogrp add get og 10 i x
2137end
2138for k ulong 5
2139oadam
2140put og 41 add get og 41 1
2141end
2142mx = 0.0f64
2143for j ulong p
2144mx = fmax mx oadamex j
2145end
2146oputc 65
2147oputc 32
2148oputu 5
2149oputc 32
2150oputx bitcast ulong mx
2151oputc 32
2152oputl long mul mx 1000000000000000.0
2153oputc 10
2154oflush
2155if gt mx 0.000000000001
2156exit 41
2157end
2158end
2159
2160fn oadamex j ulong to f64
2161g = ogrg add get og 10 j
2162w = 0.0f64
2163for s ulong 5
2164w = sub w div mul olrs s bitcast f64 get og 43 g add fabs g 0.00000001
2165end
2166d = fabs sub owg j w
2167if eq w 0.0
2168ret d
2169end
2170ret div d fabs w
2171end
2172note SPDX-License-Identifier: AGPL-3.0-only
2173note omegallm ckpt.om (aien-train#52): checkpoint OLLMCK01 / OLLMCK02 and gradient OLLMGR01 files
2174note (CONTRACT section 6, CONTRACT-V1 section 6). Needs math.om, data.om, model.om, adam.om before it.
2175note Files are streamed: a save encodes the f64 payload little-endian in chunks of OCKC values into
2176note ock, hashes each chunk (FNV-1a 64) and writes it at its file position through the platform
2177note unit (ocwopen, ocwput, ocwend; lio.om writes PATH.tmp, fsync, rename; eio.om the file in
2178note place), then writes the header at position 0. A load reads the header and then the payload in
2179note chunks through oprd into opw, hashing and decoding into the pools in one pass.
2180note CK01 header 112 bytes (FNV at 104); CK02 120 bytes (G at 104, FNV at 112); GR01 112 bytes, step
2181note field = B, one payload. Largest file at the v3 maxima: 120 + 24 P with P = 29995392 (Wolfram:
2182note 120+24*29995392 -> 719889528); a file over OCKCAP = 719889529 bytes is refused with 15 before its
2183note header is read, as v0 refused a file over its image (exactly OCKCAP bytes goes to the header
2184note checks, then 15).
2185note Magic as a little-endian u64 (Wolfram FromDigits[Reverse[ToCharacterCode["OLLMCK01"]],256]
2186note -> 3544415659172318287; "OLLMGR01" -> 3544423372933581903; "OLLMCK02" -> 3616473253210246223).
2187note Load refusals (exit codes): 10 file shorter than its header or than header + 24 P, 11 bad magic,
2188note 12 a shape zero or over the maximum, V < 256, D mod H != 0, TOTAL > 999999, or (CK02) G = 0,
2189note G > B or B mod G != 0, 13 P disagrees with the shapes, 14 FNV-1a mismatch, 15 file longer than
2190note header + 24 P; 70 I/O.
2191const OCKCAP ulong 719889529
2192const OCKC ulong 65536
2193global ock uchar 524288
2194
2195fn ocput8 o ulong v ulong to void
2196for k ulong 8
2197put ock add o k uchar and shr v mul k 8 255
2198end
2199end
2200
2201
2202note little-endian u64 at ock[o .. o + 8)
2203fn ocget8 o ulong to ulong
2204v = ulong 0
2205for k ulong 8
2206v = or v shl ulong get ock add o k mul k 8
2207end
2208ret v
2209end
2210
2211note decode m values from ock into gradient pool [o .. o + m) (a worker's gradient block, lio.om)
2212fn ocdecb o ulong m ulong to void
2213for j ulong m
2214ogrp add o j bitcast f64 ocget8 mul j 8
2215end
2216end
2217note little-endian u64 at opw[o .. o + 8)
2218fn opw8 o ulong to ulong
2219v = ulong 0
2220for k ulong 8
2221v = or v shl ulong get opw add o k mul k 8
2222end
2223ret v
2224end
2225
2226note FNV-1a 64 step over ock[0 .. n) from state h (basis cbf29ce484222325, prime 100000001b3)
2227fn ocfnvk h ulong n ulong to ulong
2228x = h
2229for i ulong n
2230x = mul xor x ulong get ock i 1099511628211
2231end
2232ret x
2233end
2234
2235fn ocfnvw h ulong n ulong to ulong
2236x = h
2237for i ulong n
2238x = mul xor x ulong get opw i 1099511628211
2239end
2240ret x
2241end
2242
2243note header fields from og into ock[0 .. 104); magic mg, step field st
2244fn ochead mg ulong st ulong to void
2245ocput8 0 mg
2246for i ulong 7
2247ocput8 add 8 mul i 8 get og i
2248end
2249ocput8 64 get og 40
2250ocput8 72 st
2251ocput8 80 get og 42
2252ocput8 88 get og 43
2253ocput8 96 get og 8
2254end
2255
2256note encode m values of pool k (0 params, 1 m, 2 v, 3 GRD, 4 BLK) from value index i into ock
2257fn ocenc k ulong i ulong m ulong to void
2258if eq k 0
2259for j ulong m
2260ocput8 mul j 8 bitcast ulong owg add i j
2261end
2262ret
2263end
2264if ge k 3
2265o = add get og 10 i
2266if eq k 4
2267o = add get og 8 i
2268end
2269for j ulong m
2270ocput8 mul j 8 bitcast ulong ogrg add o j
2271end
2272ret
2273end
2274o = add get og add 10 k i
2275for j ulong m
2276ocput8 mul j 8 bitcast ulong omvg add o j
2277end
2278end
2279
2280note decode m values from opw into pool k (0 params, 1 m, 2 v) from value index i
2281fn ocdec k ulong i ulong m ulong to void
2282if eq k 0
2283for j ulong m
2284owp add i j bitcast f64 opw8 mul j 8
2285end
2286ret
2287end
2288o = add get og add 10 k i
2289for j ulong m
2290omvp add o j bitcast f64 opw8 mul j 8
2291end
2292end
2293
2294note stream pools ks .. ke-1 (ocenc numbering) after a header of hl bytes; returns the FNV-1a
2295fn ocbody ks ulong ke ulong hl ulong to ulong
2296p = get og 8
2297h = ulong 14695981039346656037
2298pos = hl
2299nc = div add p sub OCKC 1 OCKC
2300for k ulong sub ke ks
2301for c ulong nc
2302i = mul c OCKC
2303m = sub p i
2304if gt m OCKC
2305m = OCKC
2306end
2307ocenc add ks k i m
2308nb = mul m 8
2309h = ocfnvk h nb
2310ocwput pos nb
2311pos = add pos nb
2312end
2313end
2314ret h
2315end
2316
2317note write checkpoint (params, m, v) to argv[ai] with the loaded or initialised magic (og 46)
2318fn ocksave ai ulong to void
2319ocwopen ai
2320hl = ulong 112
2321if eq get og 46 2
2322hl = 120
2323end
2324h = ocbody 0 3 hl
2325if eq hl 120
2326ochead 3616473253210246223 get og 41
2327ocput8 104 get og 44
2328ocput8 112 h
2329else
2330ochead 3544415659172318287 get og 41
2331ocput8 104 h
2332end
2333ocwput 0 hl
2334ocwend ai
2335end
2336
2337note write gradient file GR01: header with step = B, payload GRD (for either checkpoint magic)
2338fn ogrsave ai ulong to void
2339ocwopen ai
2340h = ocbody 3 4 112
2341ochead 3544423372933581903 get og 6
2342ocput8 104 h
2343ocwput 0 112
2344ocwend ai
2345end
2346
2347note load the checkpoint named by argv[ai] into og, ow (params) and omv (m, v)
2348fn ockload ai ulong to void
2349n = ofsz ai
2350if gt n OCKCAP
2351exit 15
2352end
2353hn = oprd ai 0 0 120
2354if lt n 112
2355exit 10
2356end
2357mg = opw8 0
2358hl = ulong 112
2359if eq mg 3616473253210246223
2360hl = 120
2361put og 46 2
2362else
2363if ne mg 3544415659172318287
2364exit 11
2365end
2366put og 46 1
2367end
2368if lt n hl
2369exit 10
2370end
2371for i ulong 7
2372put og i opw8 add 8 mul i 8
2373end
2374if not ogeom
2375exit 12
2376end
2377put og 40 opw8 64
2378put og 41 opw8 72
2379put og 42 opw8 80
2380put og 43 opw8 88
2381if gt get og 42 999999
2382exit 12
2383end
2384g = ulong 1
2385if eq hl 120
2386g = opw8 104
2387end
2388if not ogok2 g
2389exit 12
2390end
2391put og 44 g
2392put og 45 1
2393p = get og 8
2394if ne opw8 96 p
2395exit 13
2396end
2397hs = opw8 sub hl 8
2398pb = mul 24 p
2399if lt n add hl pb
2400exit 10
2401end
2402if gt n add hl pb
2403exit 15
2404end
2405if ne ocklp ai hl p hs 1
2406exit 14
2407end
2408end
2409
2410note one pass over the payload at hl: read, hash and decode each chunk; exit 70 on a short read.
2411note Returns 1 when the payload FNV equals hs, else 0.
2412fn ocklp ai ulong hl ulong p ulong hs ulong to ulong
2413h = ulong 14695981039346656037
2414nc = div add p sub OCKC 1 OCKC
2415pos = hl
2416for k ulong 3
2417for c ulong nc
2418i = mul c OCKC
2419m = sub p i
2420if gt m OCKC
2421m = OCKC
2422end
2423nb = mul m 8
2424if ne oprd ai pos 0 nb nb
2425exit 70
2426end
2427h = ocfnvw h nb
2428ocdec k i m
2429pos = add pos nb
2430end
2431end
2432if eq h hs
2433ret 1
2434end
2435ret 0
2436end
2437
2438note G against the loaded shapes: 1 <= G <= B and B mod G = 0
2439fn ogok2 g ulong to bool
2440b = get og 6
2441if or eq g 0 gt g b
2442ret false
2443end
2444ret eq rem b g 0
2445end
2446note SPDX-License-Identifier: AGPL-3.0-only
2447note omegallm bpe.om (aien-train#52, CONTRACT-V1 sections 2, 7 and 8): the byte-pair tokenizer.
2448note Text is read as a stream through the opw window of data.om (opstart, opshift) and cut into
2449note chunks (2.1). bpefit counts chunk types, then merges with a hashed pair table, per-pair lists
2450note of the types that hold the pair and a lazy max-heap keyed count << 24 | (4095 - a) << 12 |
2451note (4095 - b): the largest key is the largest count, then the smallest a, then the smallest b.
2452note A pair's count only falls once its newest token exists, so a popped key whose count is still
2453note current is the true maximum; a stale one is pushed again with its current count (if any).
2454note Encode applies, per chunk, the present merge of lowest rank until none is left. That equals
2455note merges 0, 1, ... in order (2.4): merge k's pair never reappears after merge k, since a later
2456note merge only makes pairs that hold its own new token. Chunk types cache their tokens.
2457note Exits: 2 usage (bpefit V outside 257..4096); 21 a capacity below, an odd token file, or
2458note MAXBYTES with no byte 0 inside; 25 a token >= V; 26 a bad merges file; 27 V above the file's
2459note V, or V <= 256; 28 no pair left to merge; 70 I/O.
2460note Interface for the model units (section 8): obload, obtlen, obtput, obprompt and obtk.
2461note Capacities: 524288 chunk types, 4194304 pool symbols (2 per byte of type), 900000 pairs,
2462note 4194304 type-in-pair list nodes.
2463const BQTCAP ulong 524288
2464const BQHMSK ulong 1048575
2465const BQSCAP ulong 4194304
2466const BQPCAP ulong 900000
2467const BQPMSK ulong 1048575
2468const BQNCAP ulong 4194304
2469const BQLCAP ulong 1099511627776
2470global obtk ulong 4096
2471global obm uchar 15384
2472global bqma ushort 4096
2473global bqmb ushort 4096
2474global bqln ulong 4096
2475global bqrk uint 8192
2476global bqrt ushort 8192
2477global bqv ulong 2
2478global bqcl uchar 256
2479global bqtm ushort 128
2480global bqsk ushort 4100
2481global bqst ulong 16
2482global bqhs uint 1048576
2483global bqto uint 524288
2484global bqtl uchar 524288
2485global bqtn uchar 524288
2486global bqtc ulong 524288
2487global bqts uint 524288
2488global bqsy ushort 4194304
2489global bqpk uint 1048576
2490global bqpc ulong 1048576
2491global bqph uint 1048576
2492global bqpl uint 1048576
2493global bqnl uint 1048576
2494global bqnt uint 4194304
2495global bqnx uint 4194304
2496global bqhp ulong 1048576
2497
2498note bqst: 0 types, 1 pool symbols used, 2 chunks, 3 bytes read, 4 tokens, 5 pairs, 6 new pairs
2499note of this merge (bqnl), 7 list nodes, 8 heap entries.
2500
2501note class table: bqcl[c] = 1 when A(c) (section 2)
2502fn bpcls to void
2503for c ulong 256
2504v = uchar 0
2505if or or and ge c 48 le c 57 and ge c 65 le c 90 or and ge c 97 le c 122 ge c 128
2506v = uchar 1
2507end
2508put bqcl c v
2509end
2510end
2511
2512note length of the chunk at opw[i] (section 2.1, pieces of at most 64) in a text ending at n > i
2513fn bpclen i ulong n ulong to ulong
2514c = ulong get opw i
2515j = add i 1
2516if eq get bqcl c 0
2517if ne c 32
2518ret 1
2519end
2520if ge j n
2521ret 1
2522end
2523if eq get bqcl get opw j 0
2524ret 1
2525end
2526j = add j 1
2527end
2528e = add i 64
2529if lt n e
2530e = n
2531end
2532while lt j e bound 64
2533if eq get bqcl get opw j 0
2534break
2535end
2536j = add j 1
2537end
2538ret sub j i
2539end
2540
2541note as bpclen, over the argument bytes oarg (obprompt)
2542fn bpclena i ulong n ulong to ulong
2543c = ulong get oarg i
2544j = add i 1
2545if eq get bqcl c 0
2546if ne c 32
2547ret 1
2548end
2549if ge j n
2550ret 1
2551end
2552if eq get bqcl get oarg j 0
2553ret 1
2554end
2555j = add j 1
2556end
2557e = add i 64
2558if lt n e
2559e = n
2560end
2561while lt j e bound 64
2562if eq get bqcl get oarg j 0
2563break
2564end
2565j = add j 1
2566end
2567ret sub j i
2568end
2569
2570note FNV-1a 64 of opw[i .. i + l)
2571fn bphash i ulong l ulong to ulong
2572h = ulong 14695981039346656037
2573for k ulong l
2574h = mul xor h ulong get opw add i k 1099511628211
2575end
2576ret h
2577end
2578
2579note does type y hold the bytes opw[i .. i + l)?
2580fn bpsame y ulong i ulong l ulong to bool
2581if ne ulong get bqtl y l
2582ret false
2583end
2584o = ulong get bqto y
2585for k ulong l
2586if ne ulong get bqsy add o k ulong get opw add i k
2587ret false
2588end
2589end
2590ret true
2591end
2592
2593note new type in empty slot s for opw[i .. i + l): its bytes become symbols at bqsy[o], 2 l pool
2594note symbols are reserved (encode stores the tokens after the bytes); BQTCAP when full
2595fn bpins s ulong i ulong l ulong to ulong
2596nt = get bqst 0
2597o = get bqst 1
2598if ge nt BQTCAP
2599ret BQTCAP
2600end
2601if gt add o mul 2 l BQSCAP
2602ret BQTCAP
2603end
2604put bqhs s uint add nt 1
2605put bqto nt uint o
2606put bqtl nt uchar l
2607put bqtn nt uchar l
2608put bqtc nt 0
2609put bqts nt 0
2610for k ulong l
2611put bqsy add o k ushort get opw add i k
2612end
2613put bqst 0 add nt 1
2614put bqst 1 add o l
2615ret nt
2616end
2617
2618note type id of the chunk opw[i .. i + l), added when absent; BQTCAP when absent and full
2619fn bpfind i ulong l ulong to ulong
2620s = and bphash i l BQHMSK
2621for q ulong 524289
2622y = ulong get bqhs s
2623if eq y 0
2624ret bpins s i l
2625end
2626y = sub y 1
2627if bpsame y i l
2628ret y
2629end
2630s = and add s 1 BQHMSK
2631end
2632ret BQTCAP
2633end
2634
2635note fit: count one chunk
2636fn bpcount i ulong l ulong to void
2637y = bpfind i l
2638if eq y BQTCAP
2639exit 21
2640end
2641put bqtc y add get bqtc y 1
2642put bqst 2 add get bqst 2 1
2643end
2644
2645note append token t to output 0 as u16 LE
2646fn bpwtok t ulong to void
2647owbyte 0 and t 255
2648owbyte 0 shr t 8
2649end
2650
2651note rank of pair (a, b): its merge token, or 65535 when it is not a merge below V
2652fn bprank a ulong b ulong to ulong
2653key = add mul a 4096 b
2654s = shr mul add key 1 ulong 11400714819323198485 51
2655for q ulong 8192
2656k = ulong get bqrk s
2657if eq k 0
2658ret 65535
2659end
2660if eq k add key 1
2661ret ulong get bqrt s
2662end
2663s = and add s 1 8191
2664end
2665ret 65535
2666end
2667
2668note replace in bqtm[0 .. m) every (a, b) of merge token t, left to right without overlap
2669fn bprepl m ulong t ulong to ulong
2670a = get bqma t
2671b = get bqmb t
2672w = ulong 0
2673j = ulong 0
2674while lt j m bound 128
2675x = get bqtm j
2676z = ushort 65535
2677if lt add j 1 m
2678z = get bqtm add j 1
2679end
2680if and eq x a eq z b
2681put bqtm w ushort t
2682j = add j 2
2683else
2684put bqtm w x
2685j = add j 1
2686end
2687w = add w 1
2688end
2689ret w
2690end
2691
2692note encode bqtm[0 .. n) (bytes) in place (section 2.4); returns the token count
2693fn bpenct n ulong to ulong
2694m = n
2695dn = false
2696while and not dn gt m 1 bound 64
2697best = ulong 65535
2698for j ulong sub m 1
2699r = bprank ulong get bqtm j ulong get bqtm add j 1
2700if lt r best
2701best = r
2702end
2703end
2704if eq best 65535
2705dn = true
2706else
2707m = bprepl m best
2708end
2709end
2710ret m
2711end
2712
2713note encode: one chunk opw[i .. i + l) to output 0, through the type cache when it has room
2714fn bpenc1 i ulong l ulong to void
2715put bqst 2 add get bqst 2 1
2716y = bpfind i l
2717if eq y BQTCAP
2718for k ulong l
2719put bqtm k ushort get opw add i k
2720end
2721m = bpenct l
2722for k ulong m
2723bpwtok ulong get bqtm k
2724end
2725put bqst 4 add get bqst 4 m
2726else
2727o = add ulong get bqto y l
2728if eq get bqtc y 0
2729for k ulong l
2730put bqtm k ushort get opw add i k
2731end
2732mt = bpenct l
2733for k ulong mt
2734put bqsy add o k get bqtm k
2735end
2736put bqtn y uchar mt
2737put bqtc y 1
2738put bqst 1 add get bqst 1 mt
2739end
2740m = ulong get bqtn y
2741for k ulong m
2742bpwtok ulong get bqsy add o k
2743end
2744put bqst 4 add get bqst 4 m
2745end
2746end
2747
2748note the chunks of the first lim bytes of argument ai (all of it when shorter) through bpcount
2749note (mode 0) or bpenc1 (mode 1, output 0 opened on argument bo once the text is open); bqst 3 =
2750note bytes read. A chunk needs at most 65 bytes past its start, so the window moves to the
2751note current chunk when fewer are left and the file goes on.
2752note window position of i after a refill: when fewer than need bytes are left from opw[i] and the
2753note file goes on, opw[i ..] moves to the front (0); else i
2754fn bprefil ai ulong i ulong need ulong to ulong
2755if gt add i need get opst 1
2756if eq get opst 2 0
2757opshift ai i
2758ret 0
2759end
2760end
2761ret i
2762end
2763
2764fn bpscan ai ulong lim ulong mode ulong bo ulong to void
2765opstart ai
2766if eq mode 1
2767owopen 0 bo
2768end
2769i = ulong 0
2770dn = false
2771while not dn bound 999999
2772q = ulong 0
2773while and not dn lt q 999999 bound 999999
2774q = add q 1
2775i = bprefil ai i 65
2776wn = get opst 1
2777wb = get opst 0
2778n = wn
2779if lt sub lim wb n
2780n = sub lim wb
2781end
2782if ge i n
2783dn = true
2784put bqst 3 add wb i
2785else
2786l = bpclen i n
2787if eq mode 0
2788bpcount i l
2789else
2790bpenc1 i l
2791end
2792i = add i l
2793end
2794end
2795end
2796end
2797
2798note fitbytes for MAXBYTES m: just after the last byte 0 among the first m bytes of argument ai;
2799note exit 21 when there is none
2800fn bpcut ai ulong m ulong to ulong
2801opstart ai
2802last = ulong 0
2803dn = false
2804while not dn bound 999999
2805wb = get opst 0
2806wn = get opst 1
2807e = wn
2808if lt sub m wb e
2809e = sub m wb
2810end
2811j = ulong 0
2812for o ulong 3
2813lj = mul add o 1 999999
2814if gt lj e
2815lj = e
2816end
2817while lt j lj bound 999999
2818if eq get opw j 0
2819last = add add wb j 1
2820end
2821j = add j 1
2822end
2823end
2824if or lt e wn ne get opst 2 0
2825dn = true
2826else
2827opshift ai wn
2828end
2829end
2830if eq last 0
2831exit 21
2832end
2833ret last
2834end
2835
2836note pair table slot of key a 4096 + b, made (count 0, no list) when absent; new slots are listed
2837note in bqnl; exit 21 past BQPCAP pairs
2838fn bppslot key ulong to ulong
2839s = shr mul add key 1 ulong 11400714819323198485 44
2840for q ulong 1048577
2841k = ulong get bqpk s
2842if eq k 0
2843nf = get bqst 5
2844if ge nf BQPCAP
2845exit 21
2846end
2847put bqpk s uint add key 1
2848put bqpc s 0
2849put bqph s 0
2850put bqpl s 0
2851put bqst 5 add nf 1
2852nn = get bqst 6
2853put bqnl nn uint s
2854put bqst 6 add nn 1
2855ret s
2856end
2857if eq k add key 1
2858ret s
2859end
2860s = and add s 1 BQPMSK
2861end
2862ret s
2863end
2864
2865note list type y under pair slot s (once per type in a row); exit 21 past BQNCAP nodes
2866fn bpnode s ulong y ulong to void
2867if ne ulong get bqpl s add y 1
2868put bqpl s uint add y 1
2869nn = get bqst 7
2870if ge nn BQNCAP
2871exit 21
2872end
2873put bqnt nn uint y
2874put bqnx nn get bqph s
2875put bqph s uint add nn 1
2876put bqst 7 add nn 1
2877end
2878end
2879
2880fn bpkey c ulong a ulong b ulong to ulong
2881ret or or shl c 24 shl sub 4095 a 12 sub 4095 b
2882end
2883
2884fn bphpush x ulong to void
2885i = get bqst 8
2886put bqst 8 add i 1
2887dn = false
2888while and not dn gt i 0 bound 64
2889p = div sub i 1 2
2890v = get bqhp p
2891if ge v x
2892dn = true
2893else
2894put bqhp i v
2895i = p
2896end
2897end
2898put bqhp i x
2899end
2900
2901note pop the heap's largest key (the heap is not empty)
2902fn bphpop to ulong
2903top = get bqhp 0
2904n = sub get bqst 8 1
2905put bqst 8 n
2906x = get bqhp n
2907i = ulong 0
2908dn = false
2909while not dn bound 64
2910l = add mul i 2 1
2911if ge l n
2912dn = true
2913else
2914c = l
2915r = add l 1
2916if lt r n
2917if gt get bqhp r get bqhp l
2918c = r
2919end
2920end
2921v = get bqhp c
2922if ge x v
2923dn = true
2924else
2925put bqhp i v
2926i = c
2927end
2928end
2929end
2930if gt n 0
2931put bqhp i x
2932end
2933ret top
2934end
2935
2936note bqpc[s] += c and -= c, one big array per function (error 12 at this pin otherwise)
2937fn bppcadd s ulong c ulong to void
2938put bqpc s add get bqpc s c
2939end
2940fn bppcsub s ulong c ulong to void
2941put bqpc s sub get bqpc s c
2942end
2943
2944note count the pairs of type y and list y under them
2945fn bpinty y ulong to void
2946o = ulong get bqto y
2947c = get bqtc y
2948for j ulong sub ulong get bqtn y 1
2949x = ulong get bqsy add o j
2950z = ulong get bqsy add add o j 1
2951s = bppslot add mul x 4096 z
2952bppcadd s c
2953bpnode s y
2954end
2955end
2956
2957note count the pairs of every type, list the types under them, heap every pair
2958fn bpinit to void
2959nt = get bqst 0
2960for y ulong nt
2961bpinty y
2962end
2963for s ulong 1048576
2964k = ulong get bqpk s
2965if ne k 0
2966c = get bqpc s
2967if gt c 0
2968k = sub k 1
2969bphpush bpkey c div k 4096 and k 4095
2970end
2971end
2972end
2973end
2974
2975note the pair to merge: the slot of the heap's largest current key; exit 28 when none is left.
2976note Each pair has at most one heap entry, and one pop sequence meets a pair at most twice.
2977fn bpsel to ulong
2978for o ulong 3
2979while gt get bqst 8 0 bound 999999
2980x = bphpop
2981c = shr x 24
2982a = sub 4095 and shr x 12 4095
2983b = sub 4095 and x 4095
2984s = bppslot add mul a 4096 b
2985cc = get bqpc s
2986if eq cc c
2987ret s
2988end
2989if gt cc 0
2990bphpush bpkey cc a b
2991end
2992end
2993end
2994exit 28
2995end
2996
2997fn bphas o ulong n ulong a ulong b ulong to bool
2998for j ulong sub n 1
2999x = ulong get bqsy add o j
3000z = ulong get bqsy add add o j 1
3001if and eq x a eq z b
3002ret true
3003end
3004end
3005ret false
3006end
3007
3008note take count c of every pair in bqsy[o .. o + n)
3009fn bpsub o ulong n ulong c ulong to void
3010for j ulong sub n 1
3011x = ulong get bqsy add o j
3012z = ulong get bqsy add add o j 1
3013s = bppslot add mul x 4096 z
3014bppcsub s c
3015end
3016end
3017
3018note replace (a, b) by t in bqsy[o .. o + n), left to right without overlap; returns the length
3019fn bprpool o ulong n ulong a ulong b ulong t ulong to ulong
3020w = ulong 0
3021j = ulong 0
3022while lt j n bound 128
3023x = ulong get bqsy add o j
3024z = ulong 65535
3025if lt add j 1 n
3026z = ulong get bqsy add add o j 1
3027end
3028if and eq x a eq z b
3029put bqsy add o w ushort t
3030j = add j 2
3031else
3032put bqsy add o w ushort x
3033j = add j 1
3034end
3035w = add w 1
3036end
3037ret w
3038end
3039
3040note add count c of every pair in bqsy[o .. o + m) of type y; list y under the pairs holding t
3041fn bpadd o ulong m ulong c ulong y ulong t ulong to void
3042for j ulong sub m 1
3043x = ulong get bqsy add o j
3044z = ulong get bqsy add add o j 1
3045s = bppslot add mul x 4096 z
3046bppcadd s c
3047if or eq x t eq z t
3048bpnode s y
3049end
3050end
3051end
3052
3053note apply merge (a, b) -> t to type y, moving its pair counts
3054fn bpmtype y ulong a ulong b ulong t ulong to void
3055o = ulong get bqto y
3056n = ulong get bqtn y
3057if bphas o n a b
3058c = get bqtc y
3059bpsub o n c
3060m = bprpool o n a b t
3061put bqtn y uchar m
3062bpadd o m c y t
3063end
3064end
3065
3066note merge t over every type listed under pair slot s (each type once: stamp t - 255)
3067fn bpwalk s ulong a ulong b ulong t ulong to void
3068nd = ulong get bqph s
3069st = uint sub t 255
3070for o ulong 5
3071while gt nd 0 bound 999999
3072y = ulong get bqnt sub nd 1
3073nd = ulong get bqnx sub nd 1
3074if ne get bqts y st
3075put bqts y st
3076bpmtype y a b t
3077end
3078end
3079end
3080end
3081
3082note merges 0 .. v - 257 into bqma/bqmb and obm[24 ..)
3083fn bpmerge v ulong to void
3084for k ulong sub v 256
3085s = bpsel
3086key = sub ulong get bqpk s 1
3087a = div key 4096
3088b = and key 4095
3089t = add 256 k
3090put bqma t ushort a
3091put bqmb t ushort b
3092p = add 24 mul 4 k
3093put obm p uchar and a 255
3094put obm add p 1 uchar shr a 8
3095put obm add p 2 uchar and b 255
3096put obm add p 3 uchar shr b 8
3097put bqst 6 0
3098bpwalk s a b t
3099bpnewp
3100end
3101end
3102
3103note heap every pair slot made by this merge (bqnl[0 .. bqst 6)) that has a count
3104fn bpnewp to void
3105nw = get bqst 6
3106for i ulong nw
3107q = ulong get bqnl i
3108c = get bqpc q
3109if gt c 0
3110kq = sub ulong get bqpk q 1
3111bphpush bpkey c div kq 4096 and kq 4095
3112end
3113end
3114end
3115
3116note obm[p .. p + 8) = v as u64 LE
3117fn bpu64 p ulong v ulong to void
3118for k ulong 8
3119put obm add p k uchar and shr v mul k 8 255
3120end
3121end
3122
3123note FNV-1a 64 of obm[24 .. e)
3124fn bpfnvm e ulong to ulong
3125h = ulong 14695981039346656037
3126for k ulong sub e 24
3127h = mul xor h ulong get obm add 24 k 1099511628211
3128end
3129ret h
3130end
3131
3132note u16 LE at opw[p]
3133fn bprd16 p ulong to ulong
3134lo = ulong get opw p
3135hi = ulong get opw add p 1
3136ret or lo shl hi 8
3137end
3138
3139note u64 LE at opw[p]
3140fn bprd64 p ulong to ulong
3141v = ulong 0
3142for k ulong 8
3143v = or v shl ulong get opw add p k mul k 8
3144end
3145ret v
3146end
3147
3148note read and check the merges file of argument ai (section 2.3; refusals 26) into bqma/bqmb;
3149note returns its V. Uses opw.
3150fn bpload ai ulong to ulong
3151n = oprd ai 0 0 15385
3152if or lt n 24 gt n 15384
3153exit 26
3154end
3155if ne bprd64 0 3550332126946413647
3156exit 26
3157end
3158vm = bprd64 8
3159if or lt vm 257 gt vm 4096
3160exit 26
3161end
3162if ne n add 24 mul 4 sub vm 256
3163exit 26
3164end
3165h = ulong 14695981039346656037
3166for k ulong sub n 24
3167h = mul xor h ulong get opw add 24 k 1099511628211
3168end
3169if ne h bprd64 16
3170exit 26
3171end
3172for k ulong sub vm 256
3173p = add 24 mul 4 k
3174a = bprd16 p
3175b = bprd16 add p 2
3176lk = add 256 k
3177if or ge a lk ge b lk
3178exit 26
3179end
3180t = add 256 k
3181put bqma t ushort a
3182put bqmb t ushort b
3183end
3184put bqv 1 vm
3185ret vm
3186end
3187
3188note use the first v - 256 merges: decoded lengths (held at 2^40) and the rank table
3189fn bpsetv v ulong to void
3190for t ulong 256
3191put bqln t 1
3192end
3193for k ulong sub v 256
3194t = add 256 k
3195ma = ulong get bqma t
3196mb = ulong get bqmb t
3197la = get bqln ma
3198s = add la get bqln mb
3199if gt s BQLCAP
3200s = BQLCAP
3201end
3202put bqln t s
3203end
3204for s ulong 8192
3205put bqrk s 0
3206end
3207for k ulong sub v 256
3208t = add 256 k
3209ma = ulong get bqma t
3210mb = ulong get bqmb t
3211key = add mul ma 4096 mb
3212s = shr mul add key 1 ulong 11400714819323198485 51
3213dn = false
3214while not dn bound 8192
3215if eq get bqrk s 0
3216put bqrk s uint add key 1
3217put bqrt s ushort t
3218dn = true
3219else
3220s = and add s 1 8191
3221end
3222end
3223end
3224put bqv 0 v
3225end
3226
3227note write the decoded bytes of token t to stdout (mode 0) or to output 0 (mode 1)
3228fn bpexp t ulong mode ulong to void
3229put bqsk 0 ushort t
3230sp = ulong 1
3231while gt sp 0 bound 999999
3232q = ulong 0
3233while and gt sp 0 lt q 999999 bound 999999
3234q = add q 1
3235sp = sub sp 1
3236x = ulong get bqsk sp
3237if lt x 256
3238if eq mode 0
3239oputc x
3240else
3241owbyte 0 x
3242end
3243else
3244put bqsk sp get bqmb x
3245put bqsk add sp 1 get bqma x
3246sp = add sp 2
3247end
3248end
3249end
3250end
3251
3252note decode token t to output 0 and add its length to bqst 4
3253fn bpdec1 t ulong to void
3254bpexp t 1
3255put bqst 4 add get bqst 4 get bqln t
3256end
3257
3258note token file argument ai: mode 0 checks it (exit 21 odd length, then 25 a token >= v) and
3259note returns its token count; mode 1 decodes it to output 0 (bqst 4 = bytes)
3260fn bpdscan ai ulong v ulong mode ulong to ulong
3261opstart ai
3262i = ulong 0
3263nt = ulong 0
3264bad = false
3265odd = false
3266dn = false
3267while not dn bound 999999
3268q = ulong 0
3269while and not dn lt q 999999 bound 999999
3270q = add q 1
3271i = bprefil ai i 2
3272wn = get opst 1
3273if gt add i 2 wn
3274dn = true
3275if lt i wn
3276odd = true
3277end
3278else
3279t = bprd16 i
3280if ge t v
3281bad = true
3282end
3283if eq mode 1
3284bpdec1 t
3285end
3286nt = add nt 1
3287i = add i 2
3288end
3289end
3290end
3291if odd
3292exit 21
3293end
3294if bad
3295exit 25
3296end
3297ret nt
3298end
3299
3300note print C a b c (C = 77 M, 75 K, 68 D)
3301fn bpline c ulong a ulong b ulong d ulong to void
3302oputc c
3303oputc 32
3304oputu a
3305oputc 32
3306oputu b
3307if ne c 75
3308if ne c 68
3309oputc 32
3310oputu d
3311end
3312end
3313oputc 10
3314oflush
3315end
3316
3317note section 8: load merges argument ai for vocabulary v (26, 27)
3318fn obload ai ulong v ulong to void
3319vm = bpload ai
3320if or le v 256 gt v vm
3321exit 27
3322end
3323bpsetv v
3324bpcls
3325end
3326
3327fn obtlen t ulong to ulong
3328ret get bqln t
3329end
3330
3331fn obtput t ulong to void
3332bpexp t 0
3333end
3334
3335note section 8: encode argument ai (at most 4000 bytes, CONTRACT 7.2; longer exit 2) into obtk;
3336note returns the token count
3337fn obprompt ai ulong to ulong
3338n = oargp ai
3339if gt n 4000
3340exit 2
3341end
3342i = ulong 0
3343m = ulong 0
3344while lt i n bound 4097
3345l = bpclena i n
3346for k ulong l
3347put bqtm k ushort get oarg add i k
3348end
3349c = bpenct l
3350for k ulong c
3351put obtk add m k ulong get bqtm k
3352end
3353m = add m c
3354i = add i l
3355end
3356ret m
3357end
3358
3359note bpefit TEXT MERGES V [MAXBYTES]
3360fn cmdbpfit to void
3361na = oargc
3362if or lt na 5 gt na 6
3363exit 2
3364end
3365v = oargu 4
3366if or lt v 257 gt v 4096
3367exit 2
3368end
3369lim = ulong 18446744073709551615
3370if eq na 6
3371lim = bpcut 2 oargu 5
3372end
3373bpcls
3374bpscan 2 lim 0 0
3375bpinit
3376bpmerge v
3377nb = add 24 mul 4 sub v 256
3378bpu64 0 3550332126946413647
3379bpu64 8 v
3380bpu64 16 bpfnvm nb
3381obmsave 3 nb
3382oputc 77
3383oputc 32
3384oputu v
3385oputc 32
3386oputu get bqst 0
3387oputc 32
3388oputu get bqst 2
3389oputc 32
3390oputu get bqst 3
3391oputc 10
3392oflush
3393end
3394
3395note bpeenc MERGES V TEXT TOKS
3396fn cmdbpenc to void
3397if ne oargc 6
3398exit 2
3399end
3400v = oargu 3
3401obload 2 v
3402bpscan 4 ulong 18446744073709551615 1 5
3403owclose 0
3404bpline 75 get bqst 3 get bqst 4 0
3405end
3406
3407note bpedec MERGES TOKS OUT (V = the file's V)
3408fn cmdbpdec to void
3409if ne oargc 5
3410exit 2
3411end
3412vm = bpload 2
3413bpsetv vm
3414nt = bpdscan 3 vm 0
3415owopen 0 4
3416put bqst 4 0
3417nd = bpdscan 3 vm 1
3418owclose 0
3419bpline 68 nt get bqst 4 0
3420end
3421note SPDX-License-Identifier: AGPL-3.0-only
3422note omegallm cli.om (aien-train#52): the ollm commands (CONTRACT section 7). The platform unit
3423note (lio.om or eio.om) follows it and holds main.
3424note Command words are matched as little-endian u64 packings of their bytes (Wolfram
3425note FromDigits[Reverse[ToCharacterCode[w]],256]): prep 1885696624, init 1953066601,
3426note grad 1684107879, train 474214396532, eval 1818326629, base 1702060386, sample 111516417089907,
3427note gcheck 118074647274343, mathck 118074698391917, adamck 118074781033569; v1: bpefit
3428note 127996038312034, bpeenc 109325798699106, bpedec 109287127216226 (bpe.om).
3429note Exit codes: 0 ok; 2 usage; 10-15 checkpoint refusals (ckpt.om); 20 data file shorter than
3430note T + 1 bytes; 21 data over 2^25 bytes or OFFS not exactly 8 B bytes; 22 an OFFS offset with
3431note o + T >= N; 23 eval W = 0 or W T + 1 > M; 24 prep story over 999999 bytes; 40 gcheck over
3432note tolerance; 41 adamck over tolerance; 42 mathck over tolerance; 70 I/O.
3433
3434fn ocmdw to ulong
3435r = oargr 1
3436if or le r 0 gt r 8
3437ret 0
3438end
3439v = ulong 0
3440for k ulong ulong r
3441v = or v shl ulong get oarg k mul k 8
3442end
3443ret v
3444end
3445
3446note run the command named by argument 1; the platform main checks that it exists.
3447fn ocmd to int
3448w = ocmdw
3449if eq w 1885696624
3450cmdprep
3451ret 0
3452end
3453if eq w 127996038312034
3454cmdbpfit
3455ret 0
3456end
3457if eq w 109325798699106
3458cmdbpenc
3459ret 0
3460end
3461if eq w 109287127216226
3462cmdbpdec
3463ret 0
3464end
3465if eq w 1953066601
3466cmdinit
3467ret 0
3468end
3469if eq w 1684107879
3470cmdgrad
3471ret 0
3472end
3473if eq w 474214396532
3474cmdtrain
3475ret 0
3476end
3477if eq w 1818326629
3478cmdeval
3479ret 0
3480end
3481if eq w 1702060386
3482cmdbase
3483ret 0
3484end
3485if eq w 111516417089907
3486cmdsamp
3487ret 0
3488end
3489if eq w 118074647274343
3490cmdgchk
3491ret 0
3492end
3493if eq w 118074698391917
3494cmdmath
3495ret 0
3496end
3497if eq w 118074781033569
3498cmdadamck
3499ret 0
3500end
3501exit 2
3502end
3503
3504note init CK V T D H F L B SEED TOTAL LRMICRO [G]; lr = f64(LRMICRO)/1000000.0. Without G the file
3505note is OLLMCK01 (G = 1); with G it is OLLMCK02 (CONTRACT-V1 section 6).
3506fn cmdinit to void
3507na = oargc
3508if and ne na 13 ne na 14
3509exit 2
3510end
3511for i ulong 7
3512put og i oargu add 3 i
3513end
3514if not ogeom
3515exit 12
3516end
3517put og 40 oargu 10
3518tt = oargu 11
3519if gt tt 999999
3520exit 12
3521end
3522g = ulong 1
3523put og 46 1
3524if eq na 14
3525g = oargu 13
3526put og 46 2
3527end
3528if not ogok2 g
3529exit 12
3530end
3531put og 44 g
3532put og 42 tt
3533put og 43 bitcast ulong div f64 oargu 12 1000000.0
3534put og 41 0
3535oinit
3536ocksave 2
3537oputc 73
3538oputc 32
3539oputu get og 8
3540oputc 10
3541oflush
3542end
3543
3544note little-endian u64 at ochk[o..o+8)
3545fn ochk64 o ulong to ulong
3546v = ulong 0
3547for k ulong 8
3548x = ulong get ochk add o k
3549v = or v shl x mul k 8
3550end
3551ret v
3552end
3553
3554note row b from offset ochk[b] (checked by ooffok)
3555fn ooffrow b ulong to void
3556obrow b ochk64 mul b 8
3557end
3558
3559note exit 22 when any offset has o >= N - T (written so that an offset near 2^64 cannot wrap)
3560fn ooffok to void
3561n = get odn 0
3562tt = get og 1
3563for b ulong get og 6
3564if ge ochk64 mul b 8 sub n tt
3565exit 22
3566end
3567end
3568end
3569
3570note grad CK DATA OFFS OUT: the gradient of CONTRACT-V1 section 5 (G blocks, K = 1)
3571fn cmdgrad to void
3572if ne oargc 6
3573exit 2
3574end
3575ockload 2
3576odopen 3
3577ooffrd 4
3578ooffok
3579for b ulong get og 6
3580ooffrow b
3581end
3582ls = ograd
3583ogrsave 5
3584oputc 71
3585oputc 32
3586oputx bitcast ulong ls
3587oputc 32
3588oputl rmic ls
3589oputc 10
3590oflush
3591end
3592
3593note train CK TRAIN UNTIL EVERY [K]: steps step .. min(UNTIL, TOTAL)-1; after step s, when
3594note (s+1) mod EVERY = 0 or s+1 is the end, save CK and print `T s+1 loss_micro(step s)`.
3595note K worker processes (CONTRACT-V1 section 5.1): K < 1, K > G or G mod K != 0 is exit 29.
3596fn cmdtrain to void
3597na = oargc
3598if and ne na 6 ne na 7
3599exit 2
3600end
3601ockload 2
3602odopen 3
3603un = oargu 4
3604ev = oargu 5
3605if eq ev 0
3606exit 2
3607end
3608k = ulong 1
3609if eq na 7
3610k = oargu 6
3611end
3612gg = get og 44
3613if or eq k 0 gt k gg
3614exit 29
3615end
3616if ne rem gg k 0
3617exit 29
3618end
3619put og 45 k
3620en = get og 42
3621if lt un en
3622en = un
3623end
3624while lt get og 41 en bound 999999
3625ls = ostep
3626s1 = get og 41
3627if or eq rem s1 ev 0 eq s1 en
3628ocksave 2
3629oputc 84
3630oputc 32
3631oputu s1
3632oputc 32
3633oputl rmic ls
3634oputc 10
3635oflush
3636end
3637end
3638end
3639
3640note eval CK HELD W [MERGES]: windows at 0, T, .., (W-1)T; prints `E step W loss_micro bpb_micro`.
3641note V > 256 needs MERGES (else exit 2); bpb = ((loss/ln2) f64(W T))/f64(nbytes) with nbytes the
3642note decoded length of the W T targets (CONTRACT-V1 section 7.1); V = 256 keeps loss/ln2.
3643fn cmdeval to void
3644na = oargc
3645if and ne na 5 ne na 6
3646exit 2
3647end
3648ockload 2
3649ww = oargu 4
3650v = get og 0
3651if gt v 256
3652if eq na 5
3653exit 2
3654end
3655obload 5 v
3656end
3657odopen 3
3658tt = get og 1
3659m = get odn 0
3660note W T + 1 > N, tested as W > (N - 1) / T so that a huge W cannot wrap W T
3661if eq ww 0
3662exit 23
3663end
3664if gt ww div sub m 1 tt
3665exit 23
3666end
3667ls = 0.0f64
3668nb = ulong 0
3669for w ulong ww
3670obrow 0 mul w tt
3671ls = add ls ofwd 0
3672if gt v 256
3673nb = add nb oevnb tt
3674end
3675end
3676lo = div ls f64 ww
3677bp = div lo 0.6931471805599453
3678if gt v 256
3679bp = div mul bp f64 mul ww tt f64 nb
3680end
3681oputc 69
3682oputc 32
3683oputu get og 41
3684oputc 32
3685oputu ww
3686oputc 32
3687oputl rmic lo
3688oputc 32
3689oputl rmic bp
3690oputc 10
3691oflush
3692end
3693
3694note decoded bytes of the targets oby[0 .. n)
3695fn oevnb n ulong to ulong
3696s = ulong 0
3697for t ulong n
3698s = add s obtlen get oby t
3699end
3700ret s
3701end
3702
3703note append token c to the context window obx[0 .. CT), keeping the last T tokens
3704fn ocpush c ulong to void
3705cn = get og 9
3706tt = get og 1
3707if lt cn tt
3708put obx cn c
3709put og 9 add cn 1
3710else
3711for i ulong sub tt 1
3712put obx i get obx add i 1
3713end
3714put obx sub tt 1 c
3715end
3716end
3717
3718note sample CK SEED N TEMPMILLI PROMPT [MERGES]. Context = token 0, then PROMPT (bytes when V = 256,
3719note its encoding when V > 256, CONTRACT-V1 section 7.2); the model reads the last T context tokens
3720note at positions 0..; the next token comes from the logits at the last position.
3721note TEMPMILLI 0: argmax (lowest index on ties), no draw. Otherwise softmax(logits/(TEMPMILLI/1000))
3722note and u = (splitmix64 stream out >> 11) 2^-53 with state = SEED, out = mix(state += GOLD);
3723note the token is the smallest c with u < p[0] + .. + p[c], else the last c with p[c] > 0.
3724note A generated token 0 ends the output and is not written; another writes its decoded bytes.
3725fn cmdsamp to void
3726na = oargc
3727if and ne na 7 ne na 8
3728exit 2
3729end
3730ockload 2
3731st = oargu 3
3732nn = oargu 4
3733if gt nn 999999
3734exit 2
3735end
3736tm = oargu 5
3737v = get og 0
3738put og 9 0
3739ocpush 0
3740if gt v 256
3741if eq na 7
3742exit 2
3743end
3744r = oargr 6
3745if or lt r 0 gt r 4000
3746exit 2
3747end
3748obload 7 v
3749np = obprompt 6
3750for i ulong np
3751ocpush get obtk i
3752end
3753else
3754r = oargr 6
3755if or lt r 0 gt r 4096
3756exit 2
3757end
3758for i ulong ulong r
3759ocpush ulong get oarg i
3760end
3761end
3762for g ulong nn
3763lf = ofwd 0
3764olast
3765c = ulong 0
3766if eq tm 0
3767c = oargmax
3768else
3769c = odraw st div f64 tm 1000.0
3770st = add st 11400714819323198485
3771end
3772if eq c 0
3773break
3774end
3775if gt v 256
3776obtput c
3777else
3778oputc c
3779end
3780ocpush c
3781end
3782oflush
3783end
3784
3785note logits of the last context position into TMP[0..V) (oa)
3786fn olast to void
3787d = get og 2
3788z = add get og 17 mul sub get og 9 1 d
3789klogit z get og 27 get og 0 d
3790end
3791
3792fn oargmax to ulong
3793tm = get og 27
3794b = ulong 0
3795bv = get oa tm
3796for c ulong get og 0
3797if gt get oa add tm c bv
3798bv = get oa add tm c
3799b = c
3800end
3801end
3802ret b
3803end
3804
3805fn odraw st ulong tau f64 to ulong
3806tm = get og 27
3807v = get og 0
3808m = bitcast f64 ulong 18442240474082181120
3809for c ulong v
3810x = div get oa add tm c tau
3811put oa add tm c x
3812m = fmax m x
3813end
3814sm = osoft tm v m
3815u = mul f64 shr smx st 11 1.1102230246251565e-16
3816acc = 0.0f64
3817la = ulong 0
3818for k ulong v
3819pk = get oa add tm k
3820acc = add acc pk
3821if lt u acc
3822ret k
3823end
3824if gt pk 0.0
3825la = k
3826end
3827end
3828ret la
3829end
3830
3831note gcheck constants (derivation and Wolfram queries in docs/omegallm/README.md). GCH2 = GCH/4 is
3832note the retry step for an index whose difference at GCH crossed a ReLU kink.
3833const GCH f64 0.000001
3834const GCH2 f64 0.00000025
3835const GCFLOOR f64 0.000001
3836const GCTOL f64 0.001
3837note mathck: max relative error allowed against the correctly rounded reference
3838const MCTOL f64 0.000000000000001
3839
3840note gcheck CK TRAIN: the hand gradient of the step loss for the batch of the header step is
3841note compared with central differences (L(p+h) - L(p-h))/((p+h) - (p-h)) at 4 indices of every
3842note tensor instance (wte, wpe, per layer g1 wq wk wv wo g2 w1 w2, gf), drawn by splitmix64.
3843note rel = |num - g| / max(|num|, |g|, GCFLOOR). An index with rel > GCTOL at GCH is measured
3844note again at GCH2 and keeps the smaller rel (a wrong hand gradient fails at both steps). Prints
3845note one `c index g_e12 num_e12` line per index (num of the kept step), then
3846note `C count maxrel_bits_hex maxrel_e9 worst_index retries`; exit 40 when maxrel > GCTOL.
3847fn cmdgchk to void
3848if ne oargc 4
3849exit 2
3850end
3851ockload 2
3852odopen 3
3853obatch get og 41
3854ls = ograd
3855d = get og 2
3856cnt = ulong 0
3857put og 50 0
3858put og 51 0
3859put og 52 0
3860put og 53 0
3861ogcten 0 mul get og 0 d 0
3862ogcten get og 30 mul get og 1 d 1
3863for l ulong get og 5
3864ogcten opg1 l d 2
3865ogcten opwq l mul d d 3
3866ogcten opwk l mul d d 4
3867ogcten opwv l mul d d 5
3868ogcten opwo l mul d d 6
3869ogcten opg2 l d 7
3870ogcten opw1 l mul d get og 4 8
3871ogcten opw2 l mul d get og 4 9
3872end
3873ogcten get og 33 d 10
3874mx = bitcast f64 get og 51
3875oputc 67
3876oputc 32
3877oputu get og 50
3878oputc 32
3879oputx get og 51
3880oputc 32
3881oputl long mul mx 1000000000.0
3882oputc 32
3883oputu get og 52
3884oputc 32
3885oputu get og 53
3886oputc 10
3887oflush
3888if gt mx GCTOL
3889exit 40
3890end
3891end
3892
3893note step-loss for the batch in obx/oby without touching the gradient
3894fn oloss to f64
3895ls = 0.0f64
3896for b ulong get og 6
3897ls = add ls ofwd b
3898end
3899ret div ls f64 get og 6
3900end
3901
3902fn ogcten o ulong n ulong tag ulong to void
3903for j ulong 4
3904i = add o rem smx xor get og 40 add mul get og 50 4 j n
3905ogc1 i
3906end
3907end
3908
3909note central difference (L(p+h) - L(p-h))/((p+h) - (p-h)) at parameter i; restores p
3910fn ogcnum i ulong h f64 to f64
3911p0 = owg i
3912pp = add p0 h
3913pm = sub p0 h
3914owp i pp
3915lp = oloss
3916owp i pm
3917lm = oloss
3918owp i p0
3919ret div sub lp lm sub pp pm
3920end
3921
3922fn ogcrel num f64 g f64 to f64
3923den = fmax fmax fabs num fabs g GCFLOOR
3924ret div fabs sub num g den
3925end
3926
3927note one gradient check at parameter i; updates og[50] count, og[51] max rel bits, og[52] worst
3928note index, og[53] retries at GCH2
3929fn ogc1 i ulong to void
3930g = ocsg i
3931num = ogcnum i GCH
3932rel = ogcrel num g
3933if gt rel GCTOL
3934put og 53 add get og 53 1
3935n2 = ogcnum i GCH2
3936r2 = ogcrel n2 g
3937if lt r2 rel
3938num = n2
3939rel = r2
3940end
3941end
3942oputc 99
3943oputc 32
3944oputu i
3945oputc 32
3946oputl long mul g 1000000000000.0
3947oputc 32
3948oputl long mul num 1000000000000.0
3949oputc 10
3950put og 50 add get og 50 1
3951if gt rel bitcast f64 get og 51
3952put og 51 bitcast ulong rel
3953put og 52 i
3954end
3955end
3956
3957note mathck REF: REF lines are `e XBITS RBITS` or `l XBITS RBITS` (16 lowercase hex digits each,
3958note 36 bytes per line); RBITS is the correctly rounded Wolfram value of exp or log at the exact
3959note f64 XBITS. Prints `M e count max_ulp maxrel_bits_hex maxrel_e18` and the same for l;
3960note exit 42 when a special value (r = 0, inf or NaN) differs in bits, maxrel > MCTOL, or REF has
3961note no e line or no l line (a check that checked nothing fails).
3962fn cmdmath to void
3963if ne oargc 3
3964exit 2
3965end
3966n = odread 2
3967if ne rem n 36 0
3968exit 21
3969end
3970for k ulong 6
3971put og add 50 k 0
3972end
3973for i ulong div n 36
3974omline mul i 36
3975end
3976omprint 101 0
3977omprint 108 3
3978if or gt bitcast f64 get og 52 MCTOL gt bitcast f64 get og 55 MCTOL
3979exit 42
3980end
3981if or eq get og 50 0 eq get og 53 0
3982exit 42
3983end
3984end
3985
3986fn ohex o ulong to ulong
3987v = ulong 0
3988for k ulong 16
3989c = odb add o k
3990if lt c 58
3991v = or shl v 4 sub c 48
3992else
3993v = or shl v 4 sub c 87
3994end
3995end
3996ret v
3997end
3998
3999note og[50+b]: count, max ulp, max rel bits (b = 0 for exp, 3 for log)
4000fn omline o ulong to void
4001kd = odb o
4002x = bitcast f64 ohex add o 2
4003rb = ohex add o 19
4004b = ulong 0
4005y = 0.0f64
4006if eq kd 101
4007y = fexp x
4008else
4009b = ulong 3
4010y = flog x
4011end
4012yb = bitcast ulong y
4013put og add 50 b add get og add 50 b 1
4014r = bitcast f64 rb
4015if or or eq r 0.0 ne r r eq fabs r bitcast f64 ulong 9218868437227405312
4016if ne yb rb
4017exit 42
4018end
4019ret
4020end
4021ul = ulong 0
4022if gt yb rb
4023ul = sub yb rb
4024else
4025ul = sub rb yb
4026end
4027if gt ul get og add 51 b
4028put og add 51 b ul
4029end
4030rel = div fabs sub y r fabs r
4031if gt rel bitcast f64 get og add 52 b
4032put og add 52 b bitcast ulong rel
4033end
4034end
4035
4036fn omprint c ulong b ulong to void
4037oputc 77
4038oputc 32
4039oputc c
4040oputc 32
4041oputu get og add 50 b
4042oputc 32
4043oputu get og add 51 b
4044oputc 32
4045oputx get og add 52 b
4046oputc 32
4047oputl long mul bitcast f64 get og add 52 b 1e18
4048oputc 10
4049oflush
4050end
4051note SPDX-License-Identifier: AGPL-3.0-only
4052note omegallm lio.om (aien-train#52): the Linux platform unit, the only unit that uses host words.
4053note It is concatenated last for targets aarch64 and x86linux; eio.om replaces it for x86efi. Both
4054note define the same functions: oargc, oargr, oflush, ocwopen, ocwput, ocwend, ofsz, ogradw, ocsinit,
4055note ocsg, ocsp, odread, owflush, owopen, owclose, ooffrd, oprd, obmsave and main. Every other unit is
4056note free of host words.
4057global owfd int 2
4058global opth uchar 2100
4059global opk uchar 4200
4060global ocfd int 1
4061global opp uchar 8
4062global ost uchar 8
4063global owrf int 64
4064global owpd int 64
4065
4066note the argument count (argv[0] included)
4067fn oargc to ulong
4068ret ulong argcnt
4069end
4070
4071note argv[i] into oarg (at most 4096 bytes); returns the argread result (negative: no such argument)
4072fn oargr i ulong to long
4073ret installedargs i
4074end
4075
4076fn ocpumain to int
4077if lt oargc 2
4078exit 2
4079end
4080ret ocmd
4081end
4082
4083fn oflush to void
4084n = get oon 0
4085p = ulong 0
4086while lt p n bound 4097
4087w = write 1 oout p sub n p
4088if le w 0
4089exit 70
4090end
4091p = add p ulong w
4092end
4093put oon 0 0
4094end
4095
4096note v1 checkpoint stream (ckpt.om): open PATH.tmp for the path in argv[ai] (created, emptied)
4097fn ocwopen ai ulong to void
4098pl = oargp ai
4099if gt pl 2000
4100exit 2
4101end
4102for i ulong pl
4103put opth i get oarg i
4104end
4105put opth pl 46
4106put opth add pl 1 116
4107put opth add pl 2 109
4108put opth add pl 3 112
4109fd = int fopen opth 0 add pl 4 577 420
4110if lt fd 0
4111exit 70
4112end
4113put ocfd 0 fd
4114end
4115
4116note write ock[0 .. n) at byte position pos of the open PATH.tmp
4117fn ocwput pos ulong n ulong to void
4118fd = get ocfd 0
4119p = ulong 0
4120while lt p n bound 999999
4121w = pwrite fd ock p sub n p long add pos p
4122if le w 0
4123exit 70
4124end
4125p = add p ulong w
4126end
4127end
4128
4129note fsync and close PATH.tmp, then rename it onto the path in argv[ai]
4130fn ocwend ai ulong to void
4131fd = get ocfd 0
4132if ne fsync fd 0
4133exit 70
4134end
4135if ne fclose fd 0
4136exit 70
4137end
4138ocrename oargp ai
4139end
4140
4141note rename opth[0 .. pl+4) to opth[0 .. pl) via a packed two-path list
4142fn ocrename pl ulong to void
4143put opk 0 2
4144put opk 1 0
4145put opk 2 uchar and add pl 4 255
4146put opk 3 uchar shr add pl 4 8
4147for i ulong add pl 4
4148put opk add 4 i get opth i
4149end
4150q = add 8 pl
4151put opk q uchar and pl 255
4152put opk add q 1 uchar shr pl 8
4153for j ulong pl
4154put opk add add q 2 j get opth j
4155end
4156r = renames opk 0 add add q 2 pl 0
4157if ne r 0
4158exit 70
4159end
4160end
4161
4162note size in bytes of the file named by argv[ai] (seek to its end)
4163fn ofsz ai ulong to ulong
4164n = oargp ai
4165fd = int fopen oarg 0 n 0 0
4166if lt fd 0
4167exit 70
4168end
4169sz = fseek fd 0 2
4170if lt sz 0
4171exit 70
4172end
4173if ne fclose fd 0
4174exit 70
4175end
4176ret ulong sz
4177end
4178
4179note cold store (CONTRACT-V3 section 2): GRD and the Adam moments, 3 P values at the CONTRACT-V3
4180note maxima (3 29995392 -> 89986176 values, 719889408 bytes), in one private anonymous mapping made
4181note on first use (mapnew prot 3 read/write, flags 34 private|anonymous; zero-filled, and inherited
4182note copy-on-write by the gradient workers). A failed mapping is an I/O refusal (exit 70).
4183const OCSN ulong 89986176
4184global ocsh long 1
4185global ocsf ulong 1
4186
4187note capacity of the cold store in f64 values (maps it on the first call)
4188fn ocsinit to ulong
4189if eq get ocsf 0 0
4190h = mapnew int -1 0 mul OCSN 8 3 34
4191if lt h 0
4192exit 70
4193end
4194put ocsh 0 h
4195put ocsf 0 1
4196end
4197ret OCSN
4198end
4199
4200fn ocsg i ulong to f64
4201ret bitcast f64 mapget get ocsh 0 mul i 8 8
4202end
4203
4204fn ocsp i ulong v f64 to void
4205x = mapset get ocsh 0 mul i 8 8 bitcast ulong v
4206end
4207
4208note gradient of the step loss with K = og[45] processes (CONTRACT-V1 section 5.1). K = 1 is
4209note ograd. For K > 1 the parent forks workers w = 1 .. K-1, each with its own pipe; worker w
4210note computes blocks w G/K .. (w+1) G/K - 1 (each from zero into its own BLK copy), writes each
4211note block's f64 bits and then its sequences' losses to the pipe and exits 0. The parent computes
4212note blocks 0 .. G/K - 1 itself, then reads the workers in ascending w, adding each block into GRD
4213note in ascending j as ograd does, and the losses into olos; olsum sums them in ascending b. So the
4214note gradient and loss are the same bits for every K. A failed fork, pipe, read or a worker that
4215note does not exit 0 is an I/O refusal (exit 70).
4216fn ogradw to f64
4217k = get og 45
4218if le k 1
4219ret ograd
4220end
4221per = div get og 44 k
4222oflush
4223for w ulong sub k 1
4224ofork1 w per
4225end
4226for j ulong per
4227oblkc j
4228ogfold eq j 0
4229end
4230for w ulong sub k 1
4231ojoin1 w per
4232end
4233ret olsum
4234end
4235
4236note fork worker w (blocks (w+1) per .. (w+2) per - 1); keep its pipe read end and pid
4237fn ofork1 w ulong per ulong to void
4238r = pipefds opp 0 8 0
4239if ne r 0
4240exit 70
4241end
4242rf = int or ulong get opp 0 shl ulong get opp 1 8
4243wf = int or ulong get opp 4 shl ulong get opp 5 8
4244pid = fork
4245if lt pid 0
4246exit 70
4247end
4248if eq pid 0
4249c = fclose rf
4250owork wf mul add w 1 per per
4251exit 0
4252end
4253c2 = fclose wf
4254put owrf w rf
4255put owpd w int pid
4256end
4257
4258note read worker w's blocks into GRD in ascending j and its losses into olos, then reap it
4259fn ojoin1 w ulong per ulong to void
4260rf = get owrf w
4261for j ulong per
4262orecvb rf
4263ogadd
4264end
4265r = div get og 6 get og 44
4266m = mul per r
4267orecvl rf mul add w 1 m m
4268c = fclose rf
4269pd = get owpd w
4270x = waitpid pd ost 0 4 0
4271if ne x long pd
4272exit 70
4273end
4274s0 = get ost 0
4275s1 = get ost 1
4276if ne or s0 s1 0
4277exit 70
4278end
4279end
4280
4281note worker: blocks j0 .. j0 + n - 1 into BLK (gradient pool from P), each sent as it is done, then
4282note the losses of its sequences
4283fn owork fd int j0 ulong n ulong to void
4284for t ulong n
4285oblkc add j0 t
4286osendb fd
4287end
4288r = div get og 6 get og 44
4289b0 = mul j0 r
4290m = mul n r
4291for i ulong m
4292ocput8 mul i 8 bitcast ulong get olos add b0 i
4293end
4294owall fd mul m 8
4295end
4296
4297fn owall fd int n ulong to void
4298p = ulong 0
4299while lt p n bound 999999
4300w = write fd ock p sub n p
4301if le w 0
4302exit 70
4303end
4304p = add p ulong w
4305end
4306end
4307
4308fn ordall fd int n ulong to void
4309p = ulong 0
4310while lt p n bound 999999
4311r = read fd ock p sub n p
4312if le r 0
4313exit 70
4314end
4315p = add p ulong r
4316end
4317end
4318
4319note send BLK (gradient pool [P .. 2P)) in chunks of OCKC values
4320fn osendb fd int to void
4321p = get og 8
4322nc = div add p sub OCKC 1 OCKC
4323for c ulong nc
4324i = mul c OCKC
4325m = sub p i
4326if gt m OCKC
4327m = OCKC
4328end
4329ocenc 4 i m
4330owall fd mul m 8
4331end
4332end
4333
4334note receive one block into BLK (gradient pool [P .. 2P))
4335fn orecvb fd int to void
4336p = get og 8
4337nc = div add p sub OCKC 1 OCKC
4338for c ulong nc
4339i = mul c OCKC
4340m = sub p i
4341if gt m OCKC
4342m = OCKC
4343end
4344ordall fd mul m 8
4345ocdecb add p i m
4346end
4347end
4348
4349note receive n losses into olos[b0 ..)
4350fn orecvl fd int b0 ulong n ulong to void
4351ordall fd mul n 8
4352for i ulong n
4353put olos add b0 i bitcast f64 ocget8 mul i 8
4354end
4355end
4356
4357note Read the file named by argv[i] into od0/od1; refuse more than ODCAP bytes (exit 21).
4358note Each read pass adds at least one byte or ends, so ODCAP + 2 passes suffice: 64 outer passes
4359note of at most 999999 inner passes each cover 2^25 + 2.
4360fn odread i ulong to ulong
4361n = oargp i
4362fd = int fopen oarg 0 n 0 0
4363if lt fd 0
4364exit 70
4365end
4366p = ulong 0
4367dn = false
4368for o ulong 64
4369q = ulong 0
4370while and not dn lt q 999999 bound 999999
4371q = add q 1
4372r = long 0
4373if lt p ODHALF
4374r = read fd od0 p sub ODHALF p
4375else
4376if lt p ODCAP
4377r = read fd od1 sub p ODHALF sub ODCAP p
4378else
4379r = read fd ochk 0 1
4380end
4381end
4382if lt r 0
4383exit 70
4384end
4385if eq r 0
4386dn = true
4387else
4388p = add p ulong r
4389if gt p ODCAP
4390exit 21
4391end
4392end
4393end
4394if dn
4395break
4396end
4397end
4398if not dn
4399exit 70
4400end
4401c = fclose fd
4402put odn 0 p
4403ret p
4404end
4405
4406note prep output: two buffered writers (k = 0 TRAIN, k = 1 HELD).
4407fn owflush k ulong to void
4408n = get owbn k
4409fd = get owfd k
4410p = ulong 0
4411while lt p n bound 65537
4412w = long 0
4413if eq k 0
4414w = write fd owb0 p sub n p
4415else
4416w = write fd owb1 p sub n p
4417end
4418if le w 0
4419exit 70
4420end
4421p = add p ulong w
4422end
4423put owbn k 0
4424end
4425
4426fn owopen k ulong i ulong to void
4427n = oargp i
4428fd = int fopen oarg 0 n 577 420
4429if lt fd 0
4430exit 70
4431end
4432put owfd k fd
4433put owbn k 0
4434end
4435
4436fn owclose k ulong to void
4437owflush k
4438fd = get owfd k
4439if ne fsync fd 0
4440exit 70
4441end
4442if ne fclose fd 0
4443exit 70
4444end
4445end
4446
4447note read argv[ai] (B little-endian u64 offsets) into ochk; exit 21 unless it holds exactly 8 B bytes
4448fn ooffrd ai ulong to void
4449n = oargp ai
4450fd = int fopen oarg 0 n 0 0
4451if lt fd 0
4452exit 70
4453end
4454p = ulong 0
4455dn = false
4456while not dn bound 70000
4457r = read fd ochk p sub 65536 p
4458if lt r 0
4459exit 70
4460end
4461if eq r 0
4462dn = true
4463else
4464p = add p ulong r
4465if ge p 65536
4466exit 21
4467end
4468end
4469end
4470c = fclose fd
4471if ne p mul 8 get og 6
4472exit 21
4473end
4474end
4475
4476
4477note v1 positioned read (CONTRACT-V1 section 3): read up to n bytes of the file named by argv[ai],
4478note from byte pos, into opw[off .. off + n). The file is opened and closed on every call. Returns
4479note the bytes read: n, fewer only when the file ends first, 0 at or after its end. Exit 70 on an
4480note I/O error, 2 on a bad argument; off + n over len opw traps 3. Each pread pass adds at least one
4481note byte or ends, so n + 1 <= 2097153 passes suffice: 3 outer passes of at most 999999 inner ones.
4482fn oprd ai ulong pos ulong off ulong n ulong to ulong
4483pl = oargp ai
4484fd = int fopen oarg 0 pl 0 0
4485if lt fd 0
4486exit 70
4487end
4488p = ulong 0
4489dn = false
4490for o ulong 3
4491while and not dn lt p n bound 999999
4492r = pread fd opw add off p sub n p long add pos p
4493if lt r 0
4494exit 70
4495end
4496if eq r 0
4497dn = true
4498else
4499p = add p ulong r
4500end
4501end
4502end
4503if ne fclose fd 0
4504exit 70
4505end
4506ret p
4507end
4508
4509note v1 (CONTRACT-V1 section 7): write obm[0 .. n) (bpe.om) to the path in argv[ai] as ocwrite does:
4510note PATH.tmp, fsync, rename
4511fn obmsave ai ulong n ulong to void
4512pl = oargp ai
4513if gt pl 2000
4514exit 2
4515end
4516for i ulong pl
4517put opth i get oarg i
4518end
4519put opth pl 46
4520put opth add pl 1 116
4521put opth add pl 2 109
4522put opth add pl 3 112
4523fd = int fopen opth 0 add pl 4 577 420
4524if lt fd 0
4525exit 70
4526end
4527p = ulong 0
4528while lt p n bound 999999
4529w = write fd obm p sub n p
4530if le w 0
4531exit 70
4532end
4533p = add p ulong w
4534end
4535if ne fsync fd 0
4536exit 70
4537end
4538if ne fclose fd 0
4539exit 70
4540end
4541ocrename pl
4542end
4543note SPDX-License-Identifier: AGPL-3.0-only
4544note Native continuation of canonical nvrm/nvrm.c, source349913f1, NVIDIA ABI580.173.02.
4545note One explicitly owned context, bounded buffers, channel, submission and completion.
4546text nvctl "/dev/nvidiactl"
4547text nvdev "/dev/nvidia0"
4548text nvuvm "/dev/nvidia-uvm"
4549global nvfd int 4
4550global nvobj uint 16
4551global nvhdr uchar 32
4552global nvpar uchar 16384
4553global nvcard uchar 4608
4554global nvseq uint 1
4555global nverr long 1
4556global nvstage uint 1
4557global nvid uint 1
4558global nvinst uint 1
4559global nvsm uint 1
4560global nvcls uint 3
4561global nvuuid uchar 16
4562global nvreg uchar 2
4563note Context phases:0 closed,1 driver resources live,2 descriptors closed with mapping cleanup pending.
4564global nvstate uchar 1
4565global nvmm uint 1
4566global nvfault uchar 1
4567global nvnext ulong 1
4568global nvbar long 1
4569global nvfifo long 1
4570global nvnotify long 1
4571global nvchva long 1
4572note A mapping not yet published to its caller remains owned after failed cleanup.
4573global nvunpub long 1
4574global nvtoken uint 1
4575global nvchreg uchar 1
4576global nvsched uchar 1
4577global nvput ulong 1
4578global nvret ulong 1
4579global nvretry uint 1
4580global nvmobj uint 96
4581global nvmmap long 96
4582global nvmaddr ulong 96
4583global nvmlen ulong 96
4584global nvmflag uchar 96
4585fn nhput off ulong v uint to void
4586for i ulong 4
4587put nvhdr add off i uchar and shr v mul i 8 255
4588end
4589end
4590fn nhget off ulong to uint
4591v = uint 0
4592for i ulong 4
4593v = or v shl uint get nvhdr add off i mul i 8
4594end
4595ret v
4596end
4597fn npput off ulong v uint to void
4598for i ulong 4
4599put nvpar add off i uchar and shr v mul i 8 255
4600end
4601end
4602fn npget off ulong to uint
4603v = uint 0
4604for i ulong 4
4605v = or v shl uint get nvpar add off i mul i 8
4606end
4607ret v
4608end
4609fn npp64 off ulong v ulong to void
4610for i ulong 8
4611put nvpar add off i uchar and shr v mul i 8 255
4612end
4613end
4614fn npg64 off ulong to ulong
4615v = ulong 0
4616for i ulong 8
4617v = or v shl ulong get nvpar add off i mul i 8
4618end
4619ret v
4620end
4621fn npuuid to void
4622npzero
4623for i ulong 16
4624put nvpar i get nvuuid i
4625end
4626end
4627fn ncget off ulong to uint
4628v = uint 0
4629for i ulong 4
4630v = or v shl uint get nvcard add off i mul i 8
4631end
4632ret v
4633end
4634fn nhzero to void
4635for i ulong len nvhdr
4636put nvhdr i 0
4637end
4638end
4639fn npzero to void
4640for i ulong len nvpar
4641put nvpar i 0
4642end
4643end
4644fn nvos r long status uint to bool
4645if ne r 0
4646put nverr 0 r
4647ret false
4648end
4649if ne status 0
4650put nverr 0 long status
4651ret false
4652end
4653ret true
4654end
4655fn nvalc parent uint cls uint n ulong to uint
4656nhzero
4657nhput 0 get nvobj 0
4658nhput 4 parent
4659if ne cls 65
4660if eq get nvseq 0 4294967295
4661put nverr 0 neg long 75
4662ret 0
4663end
4664nhput 8 get nvseq 0
4665put nvseq 0 add get nvseq 0 1
4666end
4667nhput 12 cls
4668r = devcall get nvfd 0 uint 3223340587 nvhdr 0 32 nvpar 0 n
4669if not nvos r nhget 28
4670ret 0
4671end
4672ret nhget 8
4673end
4674fn nvctrl obj uint cmd uint n ulong to bool
4675nhzero
4676nhput 0 get nvobj 0
4677nhput 4 obj
4678nhput 8 cmd
4679r = devcall get nvfd 0 uint 3223340586 nvhdr 0 32 nvpar 0 n
4680ret nvos r nhget 28
4681end
4682fn nvfree parent uint obj uint to bool
4683if eq obj 0
4684ret true
4685end
4686nhzero
4687nhput 0 get nvobj 0
4688nhput 4 parent
4689nhput 8 obj
4690r = devctl get nvfd 0 uint 3222292009 nvhdr 0 16
4691ret nvos r nhget 12
4692end
4693fn nvstop to bool
4694if eq get nvstate 0 0
4695ret true
4696end
4697if eq get nvstate 0 2
4698ret nvmapfinish
4699end
4700ok = true
4701if nvchstop
4702if not nvmclose
4703ok = false
4704end
4705else
4706ok = false
4707end
4708if gt get nvbar 0 0
4709r = mapdrop get nvbar 0
4710if ne r 0
4711put nverr 0 r
4712ok = false
4713else
4714put nvbar 0 0
4715end
4716end
4717if ne get nvobj 5 0
4718if not nvfree get nvobj 2 get nvobj 5
4719ok = false
4720end
4721put nvobj 5 0
4722end
4723if ne get nvreg 1 0
4724npuuid
4725r = uvmctl get nvfd 2 uint 26 nvpar 0 20
4726if not nvos r npget 16
4727ok = false
4728end
4729put nvreg 1 0
4730end
4731if ne get nvreg 0 0
4732npuuid
4733r = uvmctl get nvfd 2 uint 38 nvpar 0 20
4734if not nvos r npget 16
4735ok = false
4736end
4737put nvreg 0 0
4738end
4739if ne get nvobj 4 0
4740if not nvfree get nvobj 1 get nvobj 4
4741ok = false
4742end
4743put nvobj 4 0
4744end
4745if ne get nvobj 3 0
4746if not nvfree get nvobj 1 get nvobj 3
4747ok = false
4748end
4749put nvobj 3 0
4750end
4751if ne get nvobj 2 0
4752if not nvfree get nvobj 1 get nvobj 2
4753ok = false
4754end
4755put nvobj 2 0
4756end
4757if ne get nvobj 1 0
4758if not nvfree get nvobj 0 get nvobj 1
4759ok = false
4760end
4761put nvobj 1 0
4762end
4763if ne get nvobj 0 0
4764if not nvfree get nvobj 0 get nvobj 0
4765ok = false
4766end
4767put nvobj 0 0
4768end
4769for j ulong len nvfd
4770i = sub 3 j
4771if ge get nvfd i 0
4772if ne fclose get nvfd i 0
4773ok = false
4774end
4775put nvfd i neg int 1
4776end
4777end
4778note Close is attempted once per descriptor. Never repeat driver calls after this phase.
4779put nvstate 0 2
4780put nvsched 0 0
4781if not nvmapfinish
4782ok = false
4783end
4784if not ok
4785put nvfault 0 1
4786end
4787ret ok
4788end
4789note The driver descriptors are closed; only owned CPU mappings may be retried here.
4790fn nvmapfinish to bool
4791if ne get nvstate 0 2
4792put nverr 0 neg long 16
4793ret false
4794end
4795ok = true
4796if gt get nvunpub 0 0
4797r = mapdrop get nvunpub 0
4798if ne r 0
4799put nverr 0 r
4800ok = false
4801else
4802put nvunpub 0 0
4803end
4804end
4805if gt get nvbar 0 0
4806r = mapdrop get nvbar 0
4807if ne r 0
4808put nverr 0 r
4809ok = false
4810else
4811put nvbar 0 0
4812end
4813end
4814if gt get nvchva 0 0
4815r = mapdrop get nvchva 0
4816if ne r 0
4817put nverr 0 r
4818ok = false
4819else
4820put nvchva 0 0
4821end
4822end
4823if not nvmforce
4824ok = false
4825end
4826if not ok
4827put nvfault 0 1
4828ret false
4829end
4830note Descriptor closure retired every driver object, including a refused channel teardown.
4831for i ulong len nvobj
4832put nvobj i 0
4833end
4834for i ulong len nvreg
4835put nvreg i 0
4836end
4837put nvchreg 0 0
4838put nvput 0 0
4839put nvret 0 0
4840put nvfifo 0 0
4841put nvnotify 0 0
4842put nvstate 0 0
4843ret true
4844end
4845fn nvstart to bool
4846if ne get nvfault 0 0
4847put nverr 0 neg long 5
4848ret false
4849end
4850if ne get nvstate 0 0
4851put nverr 0 neg long 16
4852ret false
4853end
4854put nvstate 0 1
4855put nvnext 0 68719476736
4856for i ulong len nvfd
4857put nvfd i neg int 1
4858end
4859for i ulong len nvobj
4860put nvobj i 0
4861end
4862put nvseq 0 3472883713
4863put nverr 0 0
4864put nvstage 0 1
4865r = fopen nvctl 0 len nvctl 524290 0
4866if lt r 0
4867put nverr 0 r
4868ret false
4869end
4870put nvfd 0 int r
4871put nvstage 0 2
4872npzero
4873root = nvalc 0 65 0
4874if eq root 0
4875ret false
4876end
4877put nvobj 0 root
4878put nvstage 0 3
4879for i ulong len nvcard
4880put nvcard i 0
4881end
4882r = devctl get nvfd 0 uint 3523233480 nvcard 0 4608
4883if not nvos r 0
4884ret false
4885end
4886found = uint 0
4887for i ulong 64
4888off = mul i 72
4889if ne get nvcard off 0
4890found = add found 1
4891if ne ncget add off 56 0
4892put nverr 0 neg long 19
4893ret false
4894end
4895put nvid 0 ncget add off 16
4896end
4897end
4898if ne found 1
4899put nverr 0 neg long 19
4900ret false
4901end
4902put nvstage 0 4
4903r = fopen nvdev 0 len nvdev 524290 0
4904if lt r 0
4905put nverr 0 r
4906ret false
4907end
4908put nvfd 1 int r
4909npzero
4910npput 0 uint get nvfd 0
4911r = devctl get nvfd 1 uint 3221505737 nvpar 0 4
4912if not nvos r 0
4913ret false
4914end
4915put nvstage 0 5
4916npzero
4917npput 0 get nvid 0
4918if not nvctrl root 517 32
4919ret false
4920end
4921put nvinst 0 npget 8
4922put nvstage 0 6
4923npzero
4924npput 0 get nvinst 0
4925npput 4 root
4926dev = nvalc root 128 56
4927if eq dev 0
4928ret false
4929end
4930put nvobj 1 dev
4931put nvstage 0 7
4932npzero
4933subd = nvalc dev 8320 4
4934if eq subd 0
4935ret false
4936end
4937put nvobj 2 subd
4938put nvstage 0 8
4939npzero
4940if not nvctrl dev 8389266 804
4941ret false
4942end
4943n = npget 0
4944if gt n 200
4945put nverr 0 neg long 75
4946ret false
4947end
4948for i ulong 3
4949put nvcls i 0
4950end
4951for i ulong ulong n
4952c = npget add 4 mul i 4
4953if eq c 52928
4954put nvcls 0 c
4955end
4956if eq c 51567
4957put nvcls 1 c
4958end
4959if eq c 50017
4960put nvcls 2 c
4961end
4962end
4963for i ulong 3
4964if eq get nvcls i 0
4965put nverr 0 neg long 19
4966ret false
4967end
4968end
4969put nvstage 0 9
4970npzero
4971npput 0 1
4972npput 4 12
4973if not nvctrl subd 545264168 488
4974ret false
4975end
4976put nvsm 0 npget 8
4977note RM reports SM10.04 on GB10; CUDA target sm121 uses a different numbering contract.
4978note Matches canonical evidence/m16-blackwell-native-path-requalification-receipt.json.
4979if ne get nvsm 0 2564
4980put nverr 0 neg long 19
4981ret false
4982end
4983put nvstage 0 10
4984ret true
4985end
4986
4987note Low GPU command addresses are reserved without replacement before an owned bind.
4988note BAR mappings may use Linux-selected addresses; GPU buffers stay below 40 bits.
4989fn nvcpumap obj uint size ulong system bool flags uint to long
4990if or ne get nvfault 0 0 ne get nvstate 0 1
4991put nverr 0 neg long 5
4992ret neg long 1
4993end
4994fd = neg long 1
4995if system
4996fd = fopen nvctl 0 len nvctl 524290 0
4997else
4998fd = fopen nvdev 0 len nvdev 524290 0
4999end
5000if lt fd 0
5001put nverr 0 fd
5002ret fd
5003end
5004if not system
5005npzero
5006npput 0 uint get nvfd 0
5007r = devctl int fd uint 3221505737 nvpar 0 4
5008if not nvos r 0
5009r = fclose int fd
5010ret neg long 1
5011end
5012end
5013npzero
5014npput 0 get nvobj 0
5015npput 4 get nvobj 1
5016npput 8 obj
5017npp64 24 size
5018npput 44 flags
5019npput 48 uint fd
5020r = devctl get nvfd 0 uint 3224913486 nvpar 0 56
5021if not nvos r npget 40
5022r = fclose int fd
5023ret neg long 1
5024end
5025m = neg long 1
5026if system
5027if gt get nvnext 0 sub 1099511627776 size
5028put nverr 0 neg long 75
5029r = fclose int fd
5030ret neg long 1
5031end
5032m = maphold get nvnext 0 size
5033if gt m 0
5034put nvnext 0 add get nvnext 0 size
5035r = mapbind m int fd 0 3
5036if ne r 0
5037m = r
5038end
5039end
5040else
5041m = mapnew int fd 0 size 3 1
5042end
5043r = fclose int fd
5044if ne r 0
5045put nverr 0 r
5046put nvfault 0 1
5047if gt m 0
5048r = mapdrop m
5049if ne r 0
5050put nvunpub 0 m
5051end
5052end
5053ret neg long 1
5054end
5055if lt m 0
5056put nverr 0 m
5057end
5058ret m
5059end
5060
5061note Release flags change only after confirmed acceptance. Failed entries stay quarantined.
5062fn nvmrel slot ulong to bool
5063f = get nvmflag slot
5064if ne and f 4 0
5065npzero
5066npp64 0 get nvmaddr slot
5067npp64 8 get nvmlen slot
5068r = uvmctl get nvfd 2 uint 34 nvpar 0 24
5069if not nvos r npget 16
5070put nvfault 0 1
5071ret false
5072end
5073f = and f 251
5074put nvmflag slot f
5075end
5076if ne and f 8 0
5077npzero
5078npput 0 get nvobj 0
5079npput 4 get nvobj 1
5080npput 8 get nvobj 3
5081npput 12 get nvmobj slot
5082npp64 24 get nvmaddr slot
5083npp64 32 get nvmlen slot
5084r = devctl get nvfd 0 uint 3224389208 nvpar 0 48
5085if not nvos r npget 40
5086put nvfault 0 1
5087ret false
5088end
5089f = and f 247
5090put nvmflag slot f
5091end
5092if ne and f 1 0
5093if not nvfree get nvobj 1 get nvmobj slot
5094put nvfault 0 1
5095ret false
5096end
5097f = and f 254
5098put nvmflag slot f
5099end
5100if ne and f 2 0
5101r = mapdrop get nvmmap slot
5102if not nvos r 0
5103put nvfault 0 1
5104ret false
5105end
5106end
5107put nvmflag slot 0
5108put nvmobj slot 0
5109put nvmmap slot 0
5110put nvmaddr slot 0
5111put nvmlen slot 0
5112ret true
5113end
5114fn nvmclose to bool
5115ok = true
5116for i ulong len nvmflag
5117if ne get nvmflag i 0
5118if not nvmrel i
5119ok = false
5120end
5121end
5122end
5123ret ok
5124end
5125note Called only after the owning driver context is closed, including error teardown.
5126fn nvmforce to bool
5127if ne get nvstate 0 2
5128put nverr 0 neg long 16
5129ret false
5130end
5131ok = true
5132for i ulong len nvmflag
5133note Driver-owned flags and handles retired when the descriptors closed.
5134put nvmflag i and get nvmflag i 2
5135put nvmobj i 0
5136if ne and get nvmflag i 2 0
5137r = mapdrop get nvmmap i
5138if ne r 0
5139put nverr 0 r
5140ok = false
5141else
5142put nvmflag i 0
5143put nvmmap i 0
5144put nvmaddr i 0
5145put nvmlen i 0
5146end
5147else
5148put nvmmap i 0
5149put nvmaddr i 0
5150put nvmlen i 0
5151end
5152end
5153ret ok
5154end
5155fn nvdrop m long to bool
5156if ne get nvput 0 get nvret 0
5157put nverr 0 neg long 16
5158ret false
5159end
5160if and ne get nvobj 8 0 or eq m get nvfifo 0 eq m get nvnotify 0
5161put nverr 0 neg long 16
5162ret false
5163end
5164for i ulong len nvmmap
5165if and gt m 0 eq get nvmmap i m
5166ret nvmrel i
5167end
5168end
5169put nverr 0 neg long 22
5170ret false
5171end
5172fn nvalloc want ulong cached bool to long
5173if or eq get nvreg 1 0 ne get nvfault 0 0
5174put nverr 0 neg long 5
5175ret neg long 1
5176end
5177if or eq want 0 gt want 68719476736
5178put nverr 0 neg long 22
5179ret neg long 1
5180end
5181size = mul div add want 4095 4096 4096
5182slot = ulong 96
5183for i ulong 96
5184if and eq slot 96 eq get nvmflag i 0
5185slot = i
5186end
5187end
5188if eq slot 96
5189put nverr 0 neg long 24
5190ret neg long 1
5191end
5192put nvstage 0 21
5193npzero
5194npput 0 get nvobj 0
5195npput 8 49409
5196npput 24 436207616
5197attr = uint 9
5198if cached
5199attr = 5
5200end
5201npput 28 attr
5202npput 32 6
5203npp64 64 size
5204npp64 72 4096
5205npp64 88 sub size 1
5206h = nvalc get nvobj 1 62 128
5207if eq h 0
5208put nvfault 0 1
5209ret neg long 1
5210end
5211put nvmobj slot h
5212put nvmlen slot size
5213put nvmflag slot 1
5214put nvstage 0 22
5215m = nvcpumap h size true 0
5216if le m 0
5217put nvfault 0 1
5218ret neg long 1
5219end
5220put nvmmap slot m
5221put nvmaddr slot mapaddr m
5222put nvmflag slot 3
5223va = get nvmaddr slot
5224if or ne and va 4095 0 gt va sub 562949869535232 size
5225put nverr 0 neg long 75
5226put nvfault 0 1
5227ret neg long 1
5228end
5229put nvstage 0 23
5230npzero
5231npp64 0 va
5232npp64 8 size
5233r = uvmctl get nvfd 2 uint 73 nvpar 0 24
5234if not nvos r npget 16
5235put nvfault 0 1
5236ret neg long 1
5237end
5238put nvmflag slot 7
5239put nvstage 0 24
5240npzero
5241npput 0 get nvobj 0
5242npput 4 get nvobj 1
5243npput 8 get nvobj 3
5244npput 12 h
5245npp64 24 size
5246npput 32 33040
5247npp64 48 va
5248r = devctl get nvfd 0 uint 3225437783 nvpar 0 64
5249if not nvos r npget 56
5250put nvfault 0 1
5251ret neg long 1
5252end
5253put nvmflag slot 15
5254if ne npg64 48 va
5255put nverr 0 neg long 22
5256put nvfault 0 1
5257ret neg long 1
5258end
5259put nvstage 0 25
5260npzero
5261npp64 0 va
5262npp64 8 size
5263for i ulong 16
5264put nvpar add i 24 get nvuuid i
5265end
5266npput 40 1
5267npp64 9240 1
5268npput 9248 uint get nvfd 0
5269npput 9252 get nvobj 0
5270npput 9256 h
5271r = uvmctl get nvfd 2 uint 33 nvpar 0 9264
5272if not nvos r npget 9260
5273put nvfault 0 1
5274ret neg long 1
5275end
5276npzero
5277off = ulong 0
5278while lt off size
5279n = sub size off
5280if gt n 16384
5281n = ulong 16384
5282end
5283r = mapwrite m off nvpar 0 n
5284off = add off n
5285end
5286put nvstage 0 26
5287ret m
5288end
5289
5290note Register this private GPU virtual address space with UVM, no buffers or kernels.
5291fn nvfull to bool
5292if not nvstart
5293ret false
5294end
5295put nvstage 0 11
5296for i ulong 2
5297r = fopen nvuvm 0 len nvuvm 524290 0
5298if lt r 0
5299put nverr 0 r
5300ret false
5301end
5302put nvfd add i 2 int r
5303end
5304npzero
5305r = uvmctl get nvfd 2 uint 805306369 nvpar 0 16
5306if not nvos r npget 8
5307ret false
5308end
5309put nvstage 0 12
5310npzero
5311npput 0 uint get nvfd 2
5312r = uvmctl get nvfd 3 uint 75 nvpar 0 8
5313if ne r 0
5314put nverr 0 r
5315ret false
5316end
5317note Driver platforms may return NV_WARN_NOTHING_TO_DO. Retain the returned status.
5318put nvmm 0 npget 4
5319if and ne get nvmm 0 0 ne get nvmm 0 65542
5320put nverr 0 long get nvmm 0
5321ret false
5322end
5323put nvstage 0 13
5324npzero
5325npp64 8 562949953421311
5326v = nvalc get nvobj 1 112 24
5327if eq v 0
5328ret false
5329end
5330put nvobj 3 v
5331put nvstage 0 14
5332npzero
5333npput 4 72
5334npp64 8 562949869535232
5335npp64 40 4096
5336v = nvalc get nvobj 1 37105 56
5337if eq v 0
5338ret false
5339end
5340put nvobj 4 v
5341put nvstage 0 15
5342npzero
5343npput 4 2
5344npput 8 16
5345if not nvctrl get nvobj 2 545259850 268
5346ret false
5347end
5348if ne npget 8 16
5349put nverr 0 neg long 22
5350ret false
5351end
5352for i ulong 16
5353put nvuuid i get nvpar add i 12
5354end
5355npuuid
5356npput 24 4294967295
5357r = uvmctl get nvfd 2 uint 37 nvpar 0 40
5358if not nvos r npget 36
5359ret false
5360end
5361put nvreg 0 1
5362put nvstage 0 16
5363npuuid
5364npput 16 uint get nvfd 0
5365npput 20 get nvobj 0
5366npput 24 get nvobj 4
5367r = uvmctl get nvfd 2 uint 25 nvpar 0 32
5368if not nvos r npget 28
5369ret false
5370end
5371put nvreg 1 1
5372put nvstage 0 17
5373ret true
5374end
5375
5376fn nvfind m long to ulong
5377for i ulong len nvmmap
5378if and gt m 0 eq get nvmmap i m
5379ret i
5380end
5381end
5382ret 96
5383end
5384note Reverse partial channel construction before releasing its buffers or address space.
5385fn nvchstop to bool
5386if ne get nvchreg 0 0
5387npuuid
5388npput 16 get nvobj 0
5389npput 20 get nvobj 8
5390r = uvmctl get nvfd 2 uint 28 nvpar 0 28
5391if not nvos r npget 24
5392put nvfault 0 1
5393ret false
5394end
5395put nvchreg 0 0
5396end
5397for i ulong 4
5398obj = sub 9 i
5399parent = ulong 6
5400if eq obj 9
5401parent = ulong 8
5402end
5403if eq obj 6
5404parent = ulong 1
5405end
5406if ne get nvobj obj 0
5407if not nvfree get nvobj parent get nvobj obj
5408put nvfault 0 1
5409ret false
5410end
5411put nvobj obj 0
5412end
5413end
5414if gt get nvchva 0 0
5415r = mapdrop get nvchva 0
5416if not nvos r 0
5417put nvfault 0 1
5418ret false
5419end
5420put nvchva 0 0
5421end
5422put nvsched 0 0
5423put nvput 0 0
5424put nvret 0 0
5425put nvfifo 0 0
5426put nvnotify 0 0
5427ret true
5428end
5429
5430note Empty queue creation and scheduling; this routine submits no command packets.
5431fn nvchan to bool
5432if or eq get nvreg 1 0 ne get nvfault 0 0
5433put nverr 0 neg long 16
5434ret false
5435end
5436if or ne get nvobj 6 0 ne get nvobj 5 0
5437put nverr 0 neg long 16
5438ret false
5439end
5440put nvstage 0 31
5441npzero
5442u = nvalc get nvobj 2 50017 0
5443if eq u 0
5444ret false
5445end
5446put nvobj 5 u
5447m = nvcpumap u 65536 false 0
5448if le m 0
5449ret false
5450end
5451put nvbar 0 m
5452put nvstage 0 32
5453npzero
5454npput 12 1
5455g = nvalc get nvobj 1 41068 20
5456if eq g 0
5457ret false
5458end
5459put nvobj 6 g
5460put nvstage 0 33
5461npzero
5462npput 0 get nvobj 4
5463npput 4 1
5464c = nvalc g 36967 12
5465if eq c 0
5466ret false
5467end
5468put nvobj 7 c
5469m = nvalloc 65536 false
5470if le m 0
5471ret false
5472end
5473put nvfifo 0 m
5474m = nvalloc 1048576 true
5475if le m 0
5476ret false
5477end
5478put nvnotify 0 m
5479put nvstage 0 34
5480npzero
5481npput 0 get nvmobj nvfind get nvnotify 0
5482npput 4 get nvmobj nvfind get nvfifo 0
5483npp64 8 mapaddr get nvfifo 0
5484npput 16 1024
5485npput 24 c
5486npput 32 get nvmobj nvfind get nvfifo 0
5487npp64 64 8192
5488f = nvalc g 51567 368
5489if eq f 0
5490ret false
5491end
5492put nvobj 8 f
5493put nvstage 0 35
5494npzero
5495put nvretry 0 0
5496k = nvalc f 52928 0
5497while eq k 0
5498if or ne get nverr 0 81 ge get nvretry 0 50
5499ret false
5500end
5501put nvretry 0 add get nvretry 0 1
5502r = sleepns long 100000000
5503if ne r 0
5504put nverr 0 r
5505ret false
5506end
5507k = nvalc f 52928 0
5508end
5509put nvobj 9 k
5510put nverr 0 0
5511put nvstage 0 36
5512npzero
5513npput 0 4294967295
5514if not nvctrl f 3278831880 4
5515ret false
5516end
5517put nvtoken 0 npget 0
5518put nvstage 0 37
5519if gt get nvnext 0 1099444518912
5520put nverr 0 neg long 75
5521ret false
5522end
5523v = maphold get nvnext 0 67108864
5524if le v 0
5525put nverr 0 v
5526ret false
5527end
5528put nvchva 0 v
5529put nvnext 0 add get nvnext 0 67108864
5530npuuid
5531npput 16 uint get nvfd 0
5532npput 20 get nvobj 0
5533npput 24 f
5534npp64 32 mapaddr v
5535npp64 40 67108864
5536r = uvmctl get nvfd 2 uint 27 nvpar 0 56
5537if not nvos r npget 48
5538ret false
5539end
5540put nvchreg 0 1
5541put nvstage 0 38
5542npzero
5543put nvpar 0 1
5544if not nvctrl g 2691432705 3
5545ret false
5546end
5547put nvsched 0 1
5548put nvstage 0 39
5549ret true
5550end
5551
5552note One outstanding packet. Completion must be observed before reuse or any buffer release.
5553fn nvsend m long off ulong words uint to bool
5554if or ne get nvfault 0 0 eq get nvsched 0 0
5555put nverr 0 neg long 5
5556ret false
5557end
5558if ne get nvput 0 get nvret 0
5559put nverr 0 neg long 16
5560ret false
5561end
5562s = nvfind m
5563if eq s 96
5564put nverr 0 neg long 22
5565ret false
5566end
5567if or or eq words 0 gt words 2097151 ne and off 3 0
5568put nverr 0 neg long 22
5569ret false
5570end
5571size = get nvmlen s
5572if gt off size
5573put nverr 0 neg long 22
5574ret false
5575end
5576if gt mul ulong words 4 sub size off
5577put nverr 0 neg long 22
5578ret false
5579end
5580va = add get nvmaddr s off
5581if ge va 1099511627776
5582put nverr 0 neg long 75
5583ret false
5584end
5585p = get nvput 0
5586if eq p 18446744073709551615
5587put nverr 0 neg long 75
5588ret false
5589end
5590entry = or or va shl ulong words 42 2199023255552
5591ret nvpush p entry
5592end
5593fn nvpush p ulong entry ulong to bool
5594r = mapset get nvfifo 0 mul rem p 1024 8 8 entry
5595p = add p 1
5596put nvput 0 p
5597r = mapset get nvfifo 0 8332 4 rem p 1024
5598r = mapset get nvbar 0 144 4 ulong get nvtoken 0
5599ret true
5600end
5601
5602fn nvwait m long off ulong value uint budget ulong to bool
5603if or ne get nvfault 0 0 eq get nvsched 0 0
5604put nverr 0 neg long 22
5605ret false
5606end
5607if eq get nvput 0 get nvret 0
5608put nverr 0 neg long 22
5609ret false
5610end
5611s = nvfind m
5612if or eq s 96 or eq budget 0 gt budget 30000000000
5613put nverr 0 neg long 22
5614ret false
5615end
5616size = get nvmlen s
5617if or gt off sub size 4 ne and off 3 0
5618put nverr 0 neg long 22
5619ret false
5620end
5621start = monons
5622if lt start 0
5623put nverr 0 start
5624put nvfault 0 1
5625ret false
5626end
5627while true
5628if eq mapget m off 4 ulong value
5629put nvret 0 get nvput 0
5630ret true
5631end
5632now = monons
5633if or lt now start ge sub now start long budget
5634put nverr 0 neg long 110
5635put nvfault 0 1
5636ret false
5637end
5638r = sleepns long 100000
5639if ne r 0
5640put nverr 0 r
5641put nvfault 0 1
5642ret false
5643end
5644end
5645ret false
5646end
5647note SPDX-License-Identifier: AGPL-3.0-only
5648note Seed vecadd.s2 instruction forms, handwritten Omega emission of the same512bytes.
5649note Each quad is one128-bit instruction. References: Omega57f5f425 seed/sm121.
5650global gbcode uchar 4096
5651global gblen ulong 1
5652fn gbword v uint to void
5653for j ulong 4
5654put gbcode get gblen 0 uchar and shr v mul j 8 255
5655put gblen 0 add get gblen 0 1
5656end
5657end
5658fn gbquad a uint b uint c uint d uint to void
5659gbword a
5660gbword b
5661gbword c
5662gbword d
5663end
5664fn gbvec to void
5665put gblen 0 0
5666note Stack constant, block ID, block size, thread ID and element count.
5667gbquad 4278287234 57088 2048 1040896
5668gbquad 620825 0 9472 926208
5669gbquad 4278482860 27648 134219776 926208
5670gbquad 31001 0 8448 926208
5671gbquad 4278548396 29440 134219776 942592
5672note Linear index and bounds predicate; inactive threads exit before any access.
5673gbquad 151616548 4 260964864 2083328
5674gbquad 151026700 5 200302704 3136000
5675gbquad 2381 0 58720256 1042944
5676note Input/output addresses and memory descriptor from the constant bank.
5677gbquad 4278352770 57344 2560 926208
5678gbquad 4278482860 27392 134220288 945664
5679gbquad 4278483842 57856 2560 962560
5680gbquad 4278614914 58368 2560 975360
5681note Address formation and independent input loads, retaining scheduling waits.
5682gbquad 151156773 4 126746626 2083840
5683gbquad 33716609 4 203299072 3088896
5684gbquad 151287845 4 126746628 5229568
5685gbquad 67467649 4 203299072 991744
5686gbquad 151418917 4 126746630 9429504
5687note Unsigned32-bit addition, output store, exit and unreachable self-branch guard.
5688gbquad 34173456 5 134209791 17811968
5689gbquad 100694406 9 202381572 1040896
5690gbquad 31053 0 58720256 1042944
5691gbquad 16546119 4294967292 58982399 1032192
5692for i ulong 11
5693gbquad 31000 0 0 1032192
5694end
5695end
5696note SPDX-License-Identifier: AGPL-3.0-only
5697note Native VECTOR_1D launch ABI continued from canonical CUDApp349913f1.
5698note QMD, driver constant template and packet sequence: omega_blackwell_qmd.c/engine.c.
5699global gbmem long 8
5700global gbmax uint 1
5701global gbq0 uint 96
5702global gbq1 uint 96
5703global gbcb uint 224
5704global gbargs uint 7
5705global gbcmd uchar 65536
5706global gbused ulong 1
5707fn gblo v ulong to uint
5708ret uint and v 4294967295
5709end
5710fn gbhi v ulong to uint
5711ret uint shr v 32
5712end
5713fn gbemit w uint to void
5714for i ulong 4
5715put gbcmd get gbused 0 uchar and shr w mul i 8 255
5716put gbused 0 add get gbused 0 1
5717end
5718end
5719fn gbmth subc uint method uint count uint to void
5720gbemit or or or uint 536870912 shl count 16 shl subc 13 shr method 2
5721end
5722fn gbptr va ulong to void
5723gbemit gbhi va
5724gbemit gblo va
5725end
5726fn gbdriver cb ulong to void
5727for i ulong 224
5728put gbcb i 0
5729end
5730note Nonzero words of the existing observed driver template, not a new inferred ABI.
5731put gbcb 0 641859584
5732put gbcb 1 3
5733put gbcb 48 2499697920
5734put gbcb 49 3893478299
5735put gbcb 62 1048575
5736put gbcb 64 536870912
5737put gbcb 65 3
5738put gbcb 66 570425344
5739put gbcb 67 3
5740put gbcb 68 gbhi cb
5741put gbcb 69 gblo cb
5742put gbcb 70 604045312
5743put gbcb 71 3
5744put gbcb 80 1
5745put gbcb 82 67158016
5746put gbcb 83 2
5747put gbcb 91 1024
5748put gbcb 108 79311560
5749put gbcb 114 1
5750put gbcb 118 288
5751put gbcb 126 632711424
5752put gbcb 127 3
5753for i ulong 3
5754put gbcb add 128 i 1
5755put gbcb add 132 i 1065353216
5756end
5757put gbcb 135 1024
5758for i ulong 4
5759put gbcb add 136 i 1
5760end
5761put gbcb 148 79311560
5762put gbcb 154 1
5763put gbcb 158 288
5764put gbcb 166 632711424
5765put gbcb 167 3
5766for i ulong 3
5767put gbcb add 168 i 1
5768put gbcb add 172 i 1065353216
5769end
5770put gbcb 175 1024
5771for i ulong 4
5772put gbcb add 176 i 1
5773end
5774put gbcb 180 48
5775put gbcb 183 16777216
5776put gbcb 185 59840
5777put gbcb 189 59840
5778put gbcb 190 16777216
5779put gbcb 191 59839
5780put gbcb 210 gbhi add cb 896
5781put gbcb 211 gblo add cb 896
5782put gbcb 212 gbhi add cb 924
5783put gbcb 213 gblo add cb 924
5784put gbcb 216 64
5785put gbcb 217 1
5786put gbcb 218 1
5787put gbcb 220 1
5788put gbcb 221 1
5789put gbcb 222 1
5790put gbcb 223 16776640
5791end
5792fn gbqmd n uint to void
5793q = mapaddr get gbmem 2
5794cb = mapaddr get gbmem 1
5795code = mapaddr get gbmem 4
5796scratch = add q 16384
5797for i ulong 96
5798put gbq0 i 0
5799put gbq1 i 0
5800end
5801put gbq0 4 8388610
5802put gbq0 10 2293760
5803put gbq0 12 gblo shr add q 4096 8
5804put gbq0 14 5243392
5805put gbq0 34 65537
5806put gbq0 35 2049
5807put gbq1 4 20905984
5808put gbq1 9 3
5809put gbq1 10 1638400
5810put gbq1 12 gblo shr q 8
5811put gbq1 14 793772964
5812put gbq1 15 gblo add q 8192
5813put gbq1 16 gbhi add q 8192
5814put gbq1 17 6
5815put gbq1 19 2172715008
5816put gbq1 20 56
5817put gbq1 22 67108864
5818put gbq1 32 gblo shr code 4
5819put gbq1 33 or uint and shr code 36 2097151 20971520
5820put gbq1 34 65600
5821put gbq1 35 135169
5822put gbq1 36 78923784
5823put gbq1 39 div add n 63 64
5824put gbq1 40 1
5825put gbq1 41 1
5826put gbq1 42 gblo shr cb 6
5827put gbq1 43 or uint and shr cb 38 524287 33554432
5828put gbq1 44 gblo shr scratch 6
5829put gbq1 45 or uint and shr scratch 38 524287 75497472
5830put gbq1 52 gblo shr add cb 768 6
5831put gbq1 53 or uint and shr add cb 768 38 524287 8388608
5832put gbq1 56 gblo shr scratch 6
5833put gbq1 57 or uint and shr scratch 38 524287 2147483648
5834put gbq1 58 271581201
5835put gbq1 59 gblo shr code 8
5836gbdriver cb
5837for i ulong 3
5838va = mapaddr get gbmem add i 5
5839put gbargs mul i 2 gblo va
5840put gbargs add mul i 2 1 gbhi va
5841end
5842put gbargs 6 n
5843end
5844fn gbsetup to void
5845gbemit 536944737
5846gbemit 52928
5847gbemit 536944786
5848gbemit 1
5849gbemit 536944808
5850gbemit 15
5851gbemit 536946013
5852gbemit 3
5853gbemit 536946014
5854gbemit 536870912
5855gbemit 536946015
5856gbemit 1048575
5857gbemit 536946007
5858gbemit 3
5859gbemit 536946008
5860gbemit 570425344
5861gbemit 536946009
5862gbemit 0
5863end
5864fn gbdma va ulong count uint to void
5865gbmth 1 392 2
5866gbptr va
5867gbmth 1 384 2
5868gbemit mul count 4
5869gbemit 1
5870gbmth 1 432 1
5871gbemit 65
5872gbemit or or uint 1610621037 shl count 16 0
5873end
5874fn gbinline va ulong which bool to void
5875gbmth 1 792 98
5876gbemit or uint 1073741824 uint and shr va 40 511
5877gbemit gblo shr va 8
5878for i ulong 96
5879if which
5880gbemit get gbq1 i
5881else
5882gbemit get gbq0 i
5883end
5884end
5885end
5886fn gbrel va ulong value uint to void
5887gbmth 0 92 5
5888gbemit gblo va
5889gbemit gbhi va
5890gbemit value
5891gbemit 0
5892gbemit 1048577
5893end
5894fn gbbuild n uint to void
5895gbqmd n
5896put gbused 0 0
5897gbsetup
5898cb = mapaddr get gbmem 1
5899q = mapaddr get gbmem 2
5900mark = mapaddr get gbmem 3
5901gbdma cb 224
5902for i ulong 224
5903gbemit get gbcb i
5904end
5905gbdma add cb 896 7
5906for i ulong 7
5907gbemit get gbargs i
5908end
5909gbinline q false
5910gbdma add q 8192 1
5911gbemit 1
5912gbinline add q 4096 true
5913gbrel mark 1145324612
5914note C3: flush GPU L2 dirty lines, then a second WFI release before CPU readback.
5915gbmth 0 40 4
5916gbemit 0
5917gbemit 0
5918gbemit 0
5919gbemit 2147483648
5920gbrel add mark 16 1179010630
5921end
5922fn gbopen n uint to bool
5923if or eq n 0 gt n 1048576
5924put nverr 0 neg long 22
5925ret false
5926end
5927if not nvfull
5928ret false
5929end
5930if not nvchan
5931ret false
5932end
5933put gbmax 0 n
5934for i ulong 8
5935size = ulong 4096
5936if or eq i 0 eq i 2
5937size = ulong 65536
5938end
5939if ge i 5
5940size = mul ulong n 4
5941end
5942m = nvalloc size true
5943if le m 0
5944ret false
5945end
5946put gbmem i m
5947end
5948gbvec
5949r = mapwrite get gbmem 4 0 gbcode 0 get gblen 0
5950ret eq r long get gblen 0
5951end
5952fn gblaunch n uint to bool
5953if or or eq n 0 gt n get gbmax 0 ne get nvfault 0 0
5954put nverr 0 neg long 22
5955ret false
5956end
5957put nvstage 0 40
5958gbbuild n
5959r = mapset get gbmem 2 8192 4 0
5960r = mapset get gbmem 3 0 4 0
5961r = mapset get gbmem 3 16 4 0
5962r = mapwrite get gbmem 0 0 gbcmd 0 get gbused 0
5963put nvstage 0 41
5964if not nvsend get gbmem 0 0 uint div get gbused 0 4
5965ret false
5966end
5967put nvstage 0 42
5968if not nvwait get gbmem 3 16 1179010630 5000000000
5969ret false
5970end
5971put nvstage 0 43
5972if ne mapget get gbmem 3 0 4 1145324612
5973put nverr 0 neg long 5
5974put nvfault 0 1
5975ret false
5976end
5977if ne mapget get gbmem 2 8192 4 6
5978put nverr 0 neg long 5
5979put nvfault 0 1
5980ret false
5981end
5982ret true
5983end
5984note SPDX-License-Identifier: AGPL-3.0-only
5985note gk.om: general sm_121 launcher and f32 kernel emitters for the Omega LLM.
5986note Needs context.om (CUDApp native/nvrm), vector.om and launch.om (CUDApp native/blackwell), vendored
5987note unmodified from aien-dev/CUDApp dcba8bb8 (last change 2e242169) into native/omegallm/gpu/.
5988note QMD words follow omega_blackwell_qmd.c; control words follow omega_blackwell_codegen_gemm.c.
5989note Each kernel id owns a 16 KiB slot of gkcode (64 slots); gkthr/gkgpr/gksm describe its launch shape.
5990global gkcode uchar 1048576
5991global gkat ulong 1
5992global gkpend uint 1
5993global gkthr uint 64
5994global gkgpr uint 64
5995global gksm uint 64
5996global gkarg uint 32
5997global gkna uint 1
5998global gkmd uint 2
5999fn gkw v uint to void
6000at = get gkat 0
6001put gkcode at uchar and v 255
6002put gkcode add at 1 uchar and shr v 8 255
6003put gkcode add at 2 uchar and shr v 16 255
6004put gkcode add at 3 uchar shr v 24
6005put gkat 0 add at 4
6006end
6007fn gkq a uint b uint c uint d uint to void
6008gkw a
6009gkw b
6010gkw c
6011gkw d
6012end
6013fn gkcw st uint wb uint rb uint wt uint to uint
6014ret or or or shl st 9 shl wb 14 shl rb 17 shl wt 20
6015end
6016note Scheduling kinds: fixed, predicate, variable load, more loads, uniform load, store, more stores.
6017fn gkfx a uint b uint c uint to void
6018gkq a b c gkcw 6 7 7 get gkpend 0
6019put gkpend 0 0
6020end
6021fn gkpr a uint b uint c uint to void
6022gkq a b c gkcw 14 7 7 get gkpend 0
6023put gkpend 0 0
6024end
6025fn gkvr a uint b uint c uint to void
6026gkq a b c or gkcw 4 0 1 get gkpend 0 8192
6027put gkpend 0 3
6028end
6029fn gkvm a uint b uint c uint to void
6030gkq a b c or gkcw 4 0 1 and get gkpend 0 2 8192
6031put gkpend 0 or get gkpend 0 3
6032end
6033fn gkvar a uint b uint c uint more bool to void
6034if more
6035gkvm a b c
6036else
6037gkvr a b c
6038end
6039end
6040fn gkst a uint b uint c uint more bool to void
6041w = get gkpend 0
6042if more
6043w = and w 4294967293
6044end
6045gkq a b c or gkcw 4 7 1 w 8192
6046put gkpend 0 or w 2
6047end
6048fn gksync to void
6049gkq 31517 0 65536 gkcw 6 7 7 get gkpend 0
6050put gkpend 0 0
6051end
6052fn gkexit to void
6053gkq 31053 0 58720256 or gkcw 5 7 7 get gkpend 0 8192
6054put gkpend 0 0
6055gkq 16546119 4294967292 58982399 1032192
6056end
6057note Instruction words, verified against nvdisasm -b SM121.
6058fn gkldc d uint off uint more bool to void
6059gkvar or 4278221698 shl d 16 shl div off 4 8 2048 more
6060end
6061fn gkldc64 d uint off uint to void
6062gkvr or 4278221698 shl d 16 shl div off 4 8 2560
6063end
6064fn gks2r d uint sr uint more bool to void
6065gkvar or 31001 shl d 16 0 shl sr 8 more
6066end
6067fn gkimadi d uint a uint imm uint c uint to void
6068gkfx or or 30756 shl d 16 shl a 24 imm or 126747136 c
6069end
6070fn gkimad d uint a uint b uint c uint to void
6071gkfx or or 29220 shl d 16 shl a 24 b or 126747136 c
6072end
6073fn gkimadw d uint a uint imm uint c uint to void
6074gkfx or or 30757 shl d 16 shl a 24 imm or 126746624 c
6075end
6076fn gkimadh d uint a uint b uint c uint to void
6077gkfx or or 29223 shl d 16 shl a 24 b or 126746624 c
6078end
6079fn gkshr d uint a uint n uint to void
6080gkfx or 4278220825 shl d 16 n or 71168 a
6081end
6082fn gkandi d uint a uint imm uint to void
6083gkfx or or 30738 shl d 16 shl a 24 imm 126796031
6084end
6085fn gkmovi d uint imm uint to void
6086gkfx or 30722 shl d 16 imm 3840
6087end
6088fn gkmovz d uint to void
6089gkfx or 29186 shl d 16 255 3840
6090end
6091fn gkiadd d uint a uint b uint to void
6092gkfx or or 29200 shl d 16 shl a 24 b 134209791
6093end
6094note ISETP.GE P p = R a >= imm (signed); the destination predicate is word 2 bit 17, as in gkisetp
6095fn gkisge p uint a uint imm uint to void
6096gkpr or 30732 shl a 24 imm or 66085488 shl p 17
6097end
6098note ISETP P p = R a cc R b (signed; cc LT 1, EQ 2, LE 3, GT 4, NE 5, GE 6)
6099fn gkisetp p uint a uint b uint cc uint to void
6100gkpr or 29196 shl a 24 b or or 66060400 shl cc 12 shl p 17
6101end
6102note IMAD.WIDE.U32 R d:d+1 = R a * R b + R c:c+1
6103fn gkimadwr d uint a uint b uint c uint to void
6104gkfx or or 29221 shl d 16 shl a 24 b or 126746624 c
6105end
6106note @P p EXIT (no trailing self-branch: the block continues when P p is false)
6107fn gkexitp p uint to void
6108gkq or 2381 shl p 12 0 58720256 or gkcw 5 7 7 get gkpend 0 8192
6109put gkpend 0 0
6110end
6111note @P p LDG/STG: the guard predicate is bits 12-15 of word 0 (p 7 = PT, p + 8 negates), so the
6112note unguarded forms below are p 7 and keep their words
6113fn gkldgp d uint a uint off uint more bool p uint to void
6114gkvar or or or 2433 shl p 12 shl d 16 shl a 24 or 4 shl off 8 203299072 more
6115end
6116fn gkstgp a uint b uint off uint more bool p uint to void
6117gkst or or 2438 shl p 12 shl a 24 or b shl off 8 202381572 more
6118end
6119fn gkldg d uint a uint off uint more bool to void
6120gkldgp d a off more 7
6121end
6122fn gkstg a uint b uint off uint more bool to void
6123gkstgp a b off more 7
6124end
6125fn gksts a uint off uint b uint more bool to void
6126gkst or 31112 shl a 24 or b shl off 8 134220031 more
6127end
6128fn gklds d uint a uint off uint sb uint wt uint to void
6129gkq or or 31108 shl d 16 shl a 24 shl off 8 3072 or gkcw 2 sb 7 wt 8192
6130end
6131note FFMA d = a b + c; ru is the operand reuse mask (bit 0 Ra, bit 1 Rb, bit 2 Rc; control bits 122-124).
6132note A nonzero mask also sets control bit 109 (word-3 bit 13): nvdisasm decodes .reuse only with it set,
6133note and ptxas (CUDA 13 sm_121) emits every .reuse instruction with it set. gkcw never sets it.
6134fn gkffma d uint a uint b uint c uint wt uint ru uint to void
6135y = uint 0
6136if ne ru 0
6137y = or shl ru 26 8192
6138end
6139gkq or or 29219 shl d 16 shl a 24 b c or gkcw 1 7 7 wt y
6140end
6141fn gkfmulf d uint a uint b uint to void
6142gkfx or or 29216 shl d 16 shl a 24 b 4194304
6143end
6144fn gkffmaf d uint a uint b uint c uint to void
6145gkfx or or 29219 shl d 16 shl a 24 b c
6146end
6147fn gkbra p uint tgt ulong to void
6148here = div get gkat 0 16
6149d = uint and sub sub tgt here 1 4294967295
6150w1 = shl shr d 6 2
6151c = uint 58720256
6152if le tgt here
6153w1 = or w1 4026531840
6154c = 58982399
6155end
6156gkq or or 2375 shl p 12 shl and d 63 18 w1 c gkcw 5 7 7 get gkpend 0
6157put gkpend 0 0
6158end
6159note GEMM operand setup. Mode 0 (O): thread loads outer tid&31, depth tid>>5, step depth 2.
6160note Mode 1 (K): outer tid>>4, depth tid&15, step outer 4. Args of operand ab start at word 8*ab:
6161note ptr lo/hi, outer stride, depth stride, batch stride 1, batch stride 2, per-load delta, tile step.
6162fn gkopnd mode uint blk uint ab uint to void
6163o = add uint 896 mul ab 32
6164if eq mode 0
6165gkandi 11 0 31
6166gkshr 12 0 5
6167else
6168gkshr 11 0 4
6169gkandi 12 0 15
6170end
6171gkimadi 13 11 4 255
6172gkimadi add 92 ab 12 144 13
6173gkimadi 13 blk 32 11
6174gkldc 14 add o 8 false
6175gkldc 15 add o 12 true
6176gkimad 13 13 14 255
6177gkimad 13 12 15 13
6178gkldc 14 add o 16 false
6179gkldc 15 add o 20 true
6180gkimad 13 6 14 13
6181gkimad 13 7 15 13
6182gkldc64 10 o
6183gkimadw add 88 mul ab 2 13 1 10
6184gkldc add 97 ab add o 24 false
6185gkldc add 99 ab add o 28 true
6186end
6187note One k step of the tile: prefetch k+1 fragments into the other set, then 16 FFMA on set kk&1.
6188note Set 0 is a R32-35 b R36-39 on SB3; set 1 is a R40-43 b R44-47 on SB4. Element k accumulates in
6189note R16+k; the FFMAs of one row share a and carry gkru.
6190fn gkfrag k uint a uint b uint w uint ru uint to void
6191c = add 16 k
6192gkffma c a b c w ru
6193end
6194fn gkstep kk uint to void
6195s = and kk 1
6196if lt kk 15
6197o = sub 1 s
6198gklds add 32 mul o 8 94 mul add kk 1 144 add 3 o 0
6199gklds add 36 mul o 8 95 add 2304 mul add kk 1 144 add 3 o 0
6200end
6201w = shl uint 1 add 3 s
6202ra = add 32 mul s 8
6203k = uint 0
6204for i uint 4
6205for j uint 4
6206gkfrag k add ra i add add ra 4 j w gkru j 3
6207w = 0
6208k = add k 1
6209end
6210end
6211end
6212note Causal attention (gkmm cz). gkczx: the block exits when its column tile R2 exceeds R3 + word 28
6213note (word 28 is 0 when only C on and below the diagonal is needed, 2^30 otherwise). gkczk: K tile
6214note count R kn = R3 word 26 + word 22, and both operand pointers (pa, pa + 2) start R3 word 27 tiles
6215note in (tile steps st, st + 1). Dense: word 26 = word 27 = 0. Uses R12, R13 before the main loop.
6216fn gkczx to void
6217gkldc 12 1008 false
6218gkiadd 12 12 3
6219gkisetp 0 2 12 4
6220gkexitp 0
6221end
6222fn gkczk kn uint st uint pa uint to void
6223gkldc kn 984 false
6224gkldc 12 1000 true
6225gkldc 13 1004 true
6226gkimad kn 3 12 kn
6227gkimad 13 3 13 255
6228gkimadwr pa 13 st pa
6229gkimadwr add pa 2 13 add st 1 add pa 2
6230end
6231note One 32x32 tile of C per 64-thread block, 4x4 per thread, K tiles of 16 through shared memory.
6232note C args: ptr words 16/17, row stride 18, col stride 19, batch strides 20/21; 22 ktiles (base),
6233note 23 batch magic, 24 minus level-2 count, 25 alpha bits, 26-28 causal (gkczx, gkczk), 30/31 the
6234note fused epilogue's second pointer (gkope). beta true adds the old C; ep is the fused epilogue.
6235fn gkgemm ma uint mb uint beta bool ep uint to void
6236put gkpend 0 0
6237gkldc 1 892 false
6238gks2r 0 33 true
6239gks2r 2 37 true
6240gks2r 3 38 true
6241gks2r 5 39 true
6242gkq 4278482860 27392 134220288 or gkcw 4 2 7 get gkpend 0 8192
6243put gkpend 0 4
6244gkldc 8 988 false
6245gkldc 9 992 true
6246gkimadh 6 5 8 255
6247gkimad 7 6 9 5
6248gkczx
6249gkshr 94 0 3
6250gkimadi 94 94 16 255
6251gkandi 95 0 7
6252gkimadi 95 95 16 255
6253gkopnd ma 3 0
6254gkopnd mb 2 1
6255gkczk 96 99 88
6256gkmovi 101 4294967295
6257for i uint 16
6258gkmovz add 16 i
6259end
6260da = uint 288
6261if eq ma 1
6262da = 16
6263end
6264db = uint 288
6265if eq mb 1
6266db = 16
6267end
6268top = div get gkat 0 16
6269put gkpend 0 63
6270for r uint 8
6271gkimadw add 56 mul r 2 97 r 88
6272end
6273for r uint 8
6274gkimadw add 72 mul r 2 98 r 90
6275end
6276gkimadw 88 99 1 88
6277gkimadw 90 100 1 90
6278for r uint 8
6279gkldg add 40 r add 56 mul r 2 0 ne r 0
6280end
6281for r uint 8
6282gkldg add 48 r add 72 mul r 2 0 true
6283end
6284gksync
6285for r uint 8
6286gksts 92 mul r da add 40 r ne r 0
6287end
6288for r uint 8
6289gksts 93 add 2304 mul r db add 48 r true
6290end
6291gksync
6292gklds 32 94 0 3 0
6293gklds 36 95 2304 3 0
6294for kk uint 16
6295gkstep kk
6296end
6297gkiadd 96 96 101
6298gkisge 0 96 1
6299gkbra 0 ulong top
6300gkepi beta ep
6301end
6302note GEMM epilogue: C address of the thread's 4x4 block, alpha scale, optional old-C add, store.
6303note Fused epilogue ep (gkope, beta false): 1 also stores Y = C > 0 ? C : 0 at the second pointer
6304note (words 30/31, base R90), the FFN ReLU forward; 2 loads M at the second pointer's same offsets
6305note into R40-55 and stores M > 0 ? C : 0, the FFN ReLU backward mask (NaN M gives 0).
6306fn gkepi beta bool ep uint to void
6307gkshr 12 0 3
6308gkimadi 12 12 4 255
6309gkimadi 12 3 32 12
6310gkandi 13 0 7
6311gkimadi 13 13 4 255
6312gkimadi 13 2 32 13
6313gkldc 14 968 false
6314gkldc 15 972 true
6315gkimad 12 12 14 255
6316gkimad 12 13 15 12
6317gkldc 10 976 false
6318gkldc 11 980 true
6319gkimad 12 6 10 12
6320gkimad 12 7 11 12
6321gkldc64 10 960
6322gkimadw 88 12 1 10
6323if ne ep 0
6324gkldc64 10 1016
6325gkimadw 90 12 1 10
6326end
6327gkldc 12 996 false
6328if eq ep 2
6329gkadr32 90
6330for k uint 16
6331gkldg add 40 k add 56 mul k 2 0 ne k 0
6332end
6333end
6334gkadr32 88
6335if beta
6336for k uint 16
6337gkldg add 40 k add 56 mul k 2 0 ne k 0
6338end
6339for k uint 16
6340gkffmaf add 16 k add 16 k 12 add 40 k
6341end
6342else
6343for k uint 16
6344gkfmulf add 16 k add 16 k 12
6345end
6346end
6347if eq ep 2
6348t = gkthm 92
6349for k uint 16
6350gkepm add 16 k add 40 k t
6351end
6352end
6353for k uint 16
6354gkstg add 56 mul k 2 add 16 k 0 ne k 0
6355end
6356if eq ep 1
6357for k uint 16
6358gkepm add 16 k add 16 k 255
6359end
6360gkadr32 90
6361for k uint 16
6362gkstg add 56 mul k 2 add 16 k 0 ne k 0
6363end
6364end
6365gkexit
6366end
6367note Address pairs R56-87 of the thread's 4x4 block (row stride R14, column stride R15) from base b.
6368fn gkadr32 b uint to void
6369for i uint 4
6370r0 = add 56 mul i 8
6371gkimadw r0 14 i b
6372for j uint 3
6373gkimadw add r0 mul add j 1 2 15 add j 1 r0
6374end
6375end
6376end
6377note Fused epilogue select: P2 = R m > R t, R e = P2 ? R e : 0 (FSETP.GT, FSEL; gr.om's encoders,
6378note verified against nvdisasm there). t is RZ (255) except under mutant m.
6379fn gkepm e uint m uint t uint to void
6380grfsetp 2 m t 4
6381grfsel e e 255 2
6382end
6383note Mutant m: 1 in the product, 0 in the mutant, whose fused backward mask (ep 2) is M > 2^-10 (the
6384note forward and its stored FP unchanged). gkthm loads that threshold into R r and returns the
6385note mask's compare register: RZ (255) in the product, r in the mutant.
6386const OMUTM ulong 1
6387fn gkthm r uint to uint
6388if eq OMUTM 0
6389gkmovi r grf 0.0009765625f64
6390ret r
6391end
6392ret 255
6393end
6394note 64x64 GEMM: one 64x64 tile of C per 64-thread block, 8x8 per thread at rows ty*4+i and 32+ty*4+i,
6395note columns tx*4+j and 32+tx*4+j (ty = tid>>3, tx = tid&7), K tiles of 8 through shared memory
6396note (272-byte k rows, B at 2176). Each C element is still one FFMA chain over k in order, so results
6397note equal the 32x32 kernel bit for bit. The K loop is software pipelined (k8): two 4352-byte shared
6398note buffers; each tile first issues the global loads of the next tile (guarded by P1 = ktiles left
6399note >= 2) into staging registers, runs its 64 FFMA steps from the current buffer, then stores the
6400note staging into the other buffer behind one barrier. Registers: R16-79 accumulators; R80-111
6401note fragments (set 0 a R80-87 b R88-95 on SB3, set 1 a R96-103 b R104-111 on SB4), later epilogue
6402note addresses; R112-127 load addresses, later old C; R128/R130 operand pointers, R132/133 STS bases,
6403note R134/135 LDS bases, R136 ktiles, R137/138 per-load delta, R139/140 tile step, R141 minus one,
6404note later alpha; R142-149 A staging, R150-157 B staging; R158 buffer step (+4352 or -4352).
6405note OMUTT: 1 in the product; 0 in mutant t, whose 64x64 kernels drop the k = 7 product of the thread's
6406note C element (row 0, column 0).
6407const OMUTT ulong 1
6408note OKREU: 1 sets the Ra reuse bit (control bit 122, with bit 109) on each GEMM FFMA whose successor
6409note reads the same a fragment; 0 (variant r0) leaves both clear as k6 did. It does not change the FFMA
6410note chain of any C element, so both builds give the same bits. k7's window also measured a remap of C
6411note column j to accumulator column j xor 1, which moves every k-loop b fragment and accumulator to
6412note different low two index bits; it made no measured difference on sm_121 and was removed
6413note (evidence/OMEGALLM/gpu-k7/).
6414const OKREU ulong 1
6415note gkru j last: the reuse operand mask of the FFMA at column j of a run that ends at column last.
6416fn gkru j uint last uint to uint
6417if and eq OKREU 1 lt j last
6418ret 1
6419end
6420ret 0
6421end
6422note Mode 0 (O): thread loads outer tid, depths 0..7. Mode 1 (K): outer tid>>3 step 8, depth tid&7.
6423fn gkopnd64 mode uint blk uint ab uint to void
6424o = add uint 896 mul ab 32
6425if eq mode 0
6426gkandi 11 0 63
6427gkmovz 12
6428else
6429gkshr 11 0 3
6430gkandi 12 0 7
6431end
6432gkimadi 13 11 4 255
6433gkimadi add 132 ab 12 272 13
6434gkimadi 13 blk 64 11
6435gkldc 14 add o 8 false
6436gkldc 15 add o 12 true
6437gkimad 13 13 14 255
6438gkimad 13 12 15 13
6439gkldc 14 add o 16 false
6440gkldc 15 add o 20 true
6441gkimad 13 6 14 13
6442gkimad 13 7 15 13
6443gkldc64 10 o
6444gkimadw add 128 mul ab 2 13 1 10
6445gkldc add 137 ab add o 24 false
6446gkldc add 139 ab add o 28 true
6447end
6448fn gkstep64 kk uint to void
6449s = and kk 1
6450if lt kk 7
6451o = sub 1 s
6452d = add 80 mul o 16
6453ko = mul add kk 1 272
6454gklds d 134 ko add 3 o 0
6455gklds add d 4 134 add ko 128 add 3 o 0
6456gklds add d 8 135 add 2176 ko add 3 o 0
6457gklds add d 12 135 add 2304 ko add 3 o 0
6458end
6459w = shl uint 1 add 3 s
6460ra = add 80 mul s 16
6461cut = and eq OMUTT 0 eq kk 7
6462for i uint 8
6463for j uint 8
6464k = add mul i 8 j
6465if or not cut ne k 0
6466c = add 16 k
6467u = gkru j 7
6468gkffma c add ra i add add ra 8 j c w u
6469w = 0
6470end
6471end
6472end
6473end
6474note Load round q of a K tile into staging under predicate p (7 = PT): four A and four B loads (tile
6475note loads 4q..4q+3); round 1 advances the operand pointers after its addresses are formed. Round 1
6476note waits only for round 0's address reads.
6477fn gkld64 q uint p uint to void
6478if gt q 0
6479put gkpend 0 2
6480end
6481l = mul q 4
6482for r uint 4
6483gkimadw add 112 mul r 2 137 add l r 128
6484end
6485for r uint 4
6486gkimadw add 120 mul r 2 138 add l r 130
6487end
6488if gt q 0
6489gkimadw 128 139 1 128
6490gkimadw 130 140 1 130
6491end
6492for r uint 4
6493gkldgp add 142 add l r add 112 mul r 2 0 ne r 0 p
6494end
6495for r uint 4
6496gkldgp add 150 add l r add 120 mul r 2 0 true p
6497end
6498end
6499fn gksts64 ma uint mb uint to void
6500da = uint 272
6501if eq ma 1
6502da = 32
6503end
6504db = uint 272
6505if eq mb 1
6506db = 32
6507end
6508for r uint 8
6509gksts 132 mul r da add 142 r ne r 0
6510end
6511for r uint 8
6512gksts 133 add 2176 mul r db add 150 r true
6513end
6514end
6515fn gkgemm64 ma uint mb uint beta bool ep uint to void
6516put gkpend 0 0
6517gkldc 1 892 false
6518gks2r 0 33 true
6519gks2r 2 37 true
6520gks2r 3 38 true
6521gks2r 5 39 true
6522gkq 4278482860 27392 134220288 or gkcw 4 2 7 get gkpend 0 8192
6523put gkpend 0 4
6524gkldc 8 988 false
6525gkldc 9 992 true
6526gkimadh 6 5 8 255
6527gkimad 7 6 9 5
6528gkczx
6529gkshr 134 0 3
6530gkimadi 134 134 16 255
6531gkandi 135 0 7
6532gkimadi 135 135 16 255
6533gkopnd64 ma 3 0
6534gkopnd64 mb 2 1
6535gkczk 136 139 128
6536gkmovi 141 4294967295
6537for i uint 64
6538gkmovz add 16 i
6539end
6540put gkpend 0 63
6541gkld64 0 7
6542gkld64 1 7
6543gksts64 ma mb
6544gksync
6545gkmovi 158 4352
6546gkiadd 132 132 158
6547gkiadd 133 133 158
6548top = div get gkat 0 16
6549gkisge 1 136 2
6550gklds 80 134 0 3 0
6551gklds 84 134 128 3 0
6552gklds 88 135 2176 3 0
6553gklds 92 135 2304 3 0
6554gkld64 0 1
6555gkld64 1 1
6556for kk uint 8
6557gkstep64 kk
6558end
6559gksts64 ma mb
6560gksync
6561gkiadd 134 134 158
6562gkiadd 135 135 158
6563gkimad 158 158 141 255
6564gkiadd 132 132 158
6565gkiadd 133 133 158
6566gkiadd 136 136 141
6567gkisge 0 136 1
6568gkbra 0 ulong top
6569gkepi64 beta ep
6570end
6571note Accumulator register of element k (row k>>2, column k&3) of quadrant (rh, ch).
6572fn gkacc64 rh uint ch uint k uint to uint
6573ret add add 16 mul add mul rh 4 shr k 2 8 add mul ch 4 and k 3
6574end
6575note One 4x4 quadrant of the 64x64 epilogue: address pairs R80-111 from base R128 (+32 rows R10,
6576note +32 columns R11), alpha R141, old C in R112-127 when beta, store. Fused epilogue ep as in gkepi,
6577note second base R142 (quadrant base R144); ep 2 loads M into R112-127 (beta is false there).
6578fn gkquad64 rh uint ch uint beta bool ep uint to void
6579if eq ep 2
6580gkqadr rh ch 142 144
6581for k uint 16
6582gkldg add 112 k add 80 mul k 2 0 ne k 0
6583end
6584end
6585gkqadr rh ch 128 130
6586if beta
6587for k uint 16
6588gkldg add 112 k add 80 mul k 2 0 ne k 0
6589end
6590end
6591for k uint 16
6592e = gkacc64 rh ch k
6593if beta
6594gkffmaf e e 141 add 112 k
6595else
6596gkfmulf e e 141
6597end
6598end
6599if eq ep 2
6600t = gkthm 140
6601for k uint 16
6602gkepm gkacc64 rh ch k add 112 k t
6603end
6604end
6605for k uint 16
6606e = gkacc64 rh ch k
6607gkstg add 80 mul k 2 e 0 ne k 0
6608end
6609if eq ep 1
6610for k uint 16
6611e = gkacc64 rh ch k
6612gkepm e e 255
6613end
6614gkqadr rh ch 142 144
6615for k uint 16
6616e = gkacc64 rh ch k
6617gkstg add 80 mul k 2 e 0 ne k 0
6618end
6619end
6620end
6621note Address pairs R80-111 of quadrant (rh, ch) from thread base b (quadrant base in R t when moved).
6622fn gkqadr rh uint ch uint b uint t uint to void
6623qb = b
6624if eq rh 1
6625gkimadw t 10 1 b
6626qb = t
6627end
6628if eq ch 1
6629gkimadw t 11 1 qb
6630qb = t
6631end
6632for i uint 4
6633r0 = add 80 mul i 8
6634gkimadw r0 14 i qb
6635for j uint 3
6636gkimadw add r0 mul add j 1 2 15 add j 1 r0
6637end
6638end
6639end
6640note 64x64 epilogue: C base of the thread (R128), second base R142 when fused, quadrant offsets
6641note R10/R11, then four quadrants.
6642fn gkepi64 beta bool ep uint to void
6643gkshr 12 0 3
6644gkimadi 12 12 4 255
6645gkimadi 12 3 64 12
6646gkandi 13 0 7
6647gkimadi 13 13 4 255
6648gkimadi 13 2 64 13
6649gkldc 14 968 false
6650gkldc 15 972 true
6651gkimad 12 12 14 255
6652gkimad 12 13 15 12
6653gkldc 10 976 false
6654gkldc 11 980 true
6655gkimad 12 6 10 12
6656gkimad 12 7 11 12
6657gkldc64 10 960
6658gkimadw 128 12 1 10
6659if ne ep 0
6660gkldc64 10 1016
6661gkimadw 142 12 1 10
6662end
6663gkimadi 10 14 32 255
6664gkimadi 11 15 32 255
6665gkldc 141 996 false
6666for h uint 4
6667gkquad64 shr h 1 and h 1 beta ep
6668end
6669gkexit
6670end
6671note BF16 tensor-core GEMM (ids 27..36, docs/omegallm/CONTRACT-BF16.md): one 64x64 tile of C per
6672note 128-thread block, K tiles of 32, f32 operands rounded to bf16 (round to nearest even) by F2FP while
6673note staging into shared memory, HMMA.16816.F32.BF16 accumulating in f32, the f32 epilogue of gkepi64.
6674note Words verified with nvdisasm -b SM121 against ptxas 13.0 sm_121 output for mma.sync.aligned
6675note .m16n8k16.row.col.f32.bf16.bf16.f32, cvt.rn.bf16x2.f32 and ld.global.v4.f32.
6676note HMMA d a b c: d, c and a 4-aligned, b 2-aligned. ptxas keeps 16 cycles between the HMMAs of a warp
6677note (stall 15, then gknop of stall 1); a dependent HMMA comes 128 cycles later here.
6678fn gkhmma d uint a uint b uint c uint wt uint to void
6679gkq or or 29244 shl d 16 shl a 24 b or c 268288 gkcw 15 7 7 wt
6680gknop
6681end
6682fn gknop to void
6683gkq 31000 0 0 gkcw 1 7 7 0
6684end
6685note F2FP.BF16.F32.PACK_AB d a b: d = bf16(a) << 16 | bf16(b), round to nearest even; rz rounds toward
6686note zero (mutant v only). Stall 2 as ptxas.
6687const OMUTV ulong 1
6688fn gkf2fp d uint a uint b uint wt uint to void
6689c = uint 4351
6690if eq OMUTV 0
6691c = 102655
6692end
6693gkq or or 29246 shl d 16 shl a 24 b c or gkcw 2 7 7 wt 8192
6694end
6695note LDG.E.128 R d..d+3 = [R a:a+1] under predicate p (7 = PT)
6696fn gkldgq d uint a uint more bool p uint to void
6697gkvar or or or 2433 shl p 12 shl d 16 shl a 24 4 203300096 more
6698end
6699note R d = ((tid >> s) & m) mult (+ R d unless first); R8 is the scratch.
6700fn gkbt d uint s uint m uint mult uint first bool to void
6701if eq s 0
6702gkandi 8 0 m
6703else
6704gkshr 8 0 s
6705gkandi 8 8 m
6706end
6707c = d
6708if first
6709c = 255
6710end
6711gkimadi d 8 mult c
6712end
6713note Operand ab of the BF16 tile. Mode 0 (O, outer stride 4): thread loads outers 4 (tid & 15) + 0..3,
6714note depths 4 (tid >> 4) + j, j < 4 (one LDG.128 per depth, delta = depth stride). Mode 1 (K, depth
6715note stride 4): outers (tid >> 3) + 16 i, i < 4 (delta 16 outer strides), depths 4 (tid & 7) + 0..3.
6716note Pointer R112 + 2 ab, delta R137 + ab, tile step (32 depths) R139 + ab.
6717fn gkopndb mode uint blk uint ab uint to void
6718o = add uint 896 mul ab 32
6719if eq mode 0
6720gkandi 11 0 15
6721gkimadi 11 11 4 255
6722gkshr 12 0 4
6723gkimadi 12 12 4 255
6724else
6725gkshr 11 0 3
6726gkandi 12 0 7
6727gkimadi 12 12 4 255
6728end
6729gkimadi 13 blk 64 11
6730gkldc 14 add o 8 false
6731gkldc 15 add o 12 true
6732gkimad 13 13 14 255
6733gkimad 13 12 15 13
6734gkldc 14 add o 16 false
6735gkldc 15 add o 20 true
6736gkimad 13 6 14 13
6737gkimad 13 7 15 13
6738gkldc64 10 o
6739gkimadw add 112 mul ab 2 13 1 10
6740gkldc add 137 ab add o 24 false
6741gkldc add 139 ab add o 28 true
6742end
6743note Shared memory, per 8192-byte buffer, k pair p = k >> 1 (0..15), outer r (0..63): A at
6744note 2048 (p >> 3) + 512 (r >> 4) + 64 (r & 7) + 16 (p & 3) + 4 ((r >> 3) & 1) + 8 ((p >> 2) & 1),
6745note B at 4096 + the same with the weights 4 and 8 exchanged, so that one LDS.128 at 512 j + 16 lane
6746note (+ 2048 per k16 step) gives a lane its four A registers (rows g, g + 8; k 2t, 2t + 8) of m16
6747note block j, or the two B registers of n8 blocks 2j and 2j + 1 (g = lane >> 2, t = lane & 3).
6748note gkstsb: the thread's STS base R132 + ab for operand mode (o = 4 for A, 8 for B is the r-bit-3 weight).
6749fn gkstsb mode uint ab uint to void
6750d = add 132 ab
6751ro = add 4 mul ab 4
6752ko = sub 12 ro
6753if eq mode 1
6754gkbt d 2 1 2048 true
6755gkbt d 3 7 64 false
6756gkbt d 0 1 32 false
6757gkbt d 6 1 ro false
6758gkbt d 1 1 ko false
6759else
6760gkbt d 6 1 2048 true
6761gkbt d 2 3 512 false
6762gkbt d 0 1 256 false
6763gkbt d 4 1 32 false
6764gkbt d 1 1 ro false
6765gkbt d 5 1 ko false
6766end
6767end
6768note LDS bases: R134 = 1024 wm + 16 lane (A, warp row wm = tid >> 6), R135 = 1024 wn + 16 lane (B, warp
6769note column wn = (tid >> 5) & 1). Mutant l (OMUTL 0) swaps the A rows of lanes g and g ^ 1.
6770const OMUTL ulong 1
6771fn gkldsb to void
6772gkbt 134 6 1 1024 true
6773gkbt 134 0 31 16 false
6774if eq OMUTL 0
6775gkbt 134 2 1 4294967168 false
6776gkmovi 8 64
6777gkiadd 134 134 8
6778end
6779gkbt 135 5 1 1024 true
6780gkbt 135 0 31 16 false
6781end
6782note Load a K tile into staging under predicate p: A in R80-95, B in R96-111 (addresses R116-131),
6783note then advance both operand pointers by their tile steps.
6784fn gkldb p uint to void
6785for i uint 4
6786gkimadw add 116 mul i 2 137 i 112
6787end
6788for i uint 4
6789gkimadw add 124 mul i 2 138 i 114
6790end
6791gkimadw 112 139 1 112
6792gkimadw 114 140 1 114
6793for i uint 4
6794gkldgq add 80 mul i 4 add 116 mul i 2 ne i 0 p
6795end
6796for i uint 4
6797gkldgq add 96 mul i 4 add 124 mul i 2 true p
6798end
6799end
6800note Packed word x (0..7) of a staged operand at s: mode 1 is (i, e) = (x >> 1, x & 1), the k pair e of
6801note outer load i; mode 0 is (jp, c) = (x >> 2, x & 3), depth pair jp of outer c. gkcvd is the packed
6802note register (in place), gkcvl the lower-k f32 register (the hi one follows it, + 1 or + 4), gkcvo
6803note the STS offset.
6804fn gkcvl md uint x uint s uint to uint
6805if eq md 1
6806ret add add s mul shr x 1 4 mul and x 1 2
6807end
6808ret add add s mul shr x 2 8 and x 3
6809end
6810fn gkcvd md uint x uint s uint to uint
6811if eq md 1
6812ret add add s mul shr x 1 4 and x 1
6813end
6814ret gkcvl md x s
6815end
6816fn gkcvo md uint x uint to uint
6817if eq md 1
6818ret add mul shr x 1 512 mul and x 1 16
6819end
6820ret add mul and x 3 64 mul shr x 2 16
6821end
6822note Round the staged tile to bf16 pairs (the first F2FP waits for the loads), then store it.
6823fn gkcvsb ma uint mb uint to void
6824w = get gkpend 0
6825put gkpend 0 0
6826for ab uint 2
6827md = ma
6828if eq ab 1
6829md = mb
6830end
6831s = add 80 mul ab 16
6832hd = uint 1
6833if eq md 0
6834hd = 4
6835end
6836for x uint 8
6837l = gkcvl md x s
6838gkf2fp gkcvd md x s add l hd l w
6839w = 0
6840end
6841end
6842for ab uint 2
6843md = ma
6844if eq ab 1
6845md = mb
6846end
6847gkcvst md ab add 80 mul ab 16
6848end
6849end
6850note gkcvsb's stores of operand ab (mode md, staged at register s)
6851fn gkcvst md uint ab uint s uint to void
6852for x uint 8
6853gksts add 132 ab add gkcvo md x mul ab 4096 gkcvd md x s or ne x 0 ne ab 0
6854end
6855end
6856note Mutant o (OMUTO 0) runs one K tile fewer than the tile count.
6857const OMUTO ulong 1
6858fn gkgemmb ma uint mb uint beta bool ep uint to void
6859put gkpend 0 0
6860gkldc 1 892 false
6861gks2r 0 33 true
6862gks2r 2 37 true
6863gks2r 3 38 true
6864gks2r 5 39 true
6865gkq 4278482860 27392 134220288 or gkcw 4 2 7 get gkpend 0 8192
6866put gkpend 0 4
6867gkldc 8 988 false
6868gkldc 9 992 true
6869gkimadh 6 5 8 255
6870gkimad 7 6 9 5
6871gkczx
6872gkldsb
6873gkstsb ma 0
6874gkstsb mb 1
6875gkopndb ma 3 0
6876gkopndb mb 2 1
6877gkczk 136 139 112
6878gkmovi 141 4294967295
6879if eq OMUTO 0
6880gkiadd 136 136 141
6881end
6882for i uint 32
6883gkmovz add 16 i
6884end
6885put gkpend 0 63
6886gkldb 7
6887gkcvsb ma mb
6888gksync
6889gkmovi 142 8192
6890gkiadd 132 132 142
6891gkiadd 133 133 142
6892top = div get gkat 0 16
6893gkisge 1 136 2
6894for kk uint 2
6895s = add 3 kk
6896f = add 48 mul kk 16
6897ko = mul kk 2048
6898gklds f 134 ko s 0
6899gklds add f 4 134 add ko 512 s 0
6900gklds add f 8 135 add ko 4096 s 0
6901gklds add f 12 135 add ko 4608 s 0
6902end
6903gkldb 1
6904for kk uint 2
6905gkmmak kk
6906end
6907gkcvsb ma mb
6908gksync
6909gkiadd 134 134 142
6910gkiadd 135 135 142
6911gkimad 142 142 141 255
6912gkiadd 132 132 142
6913gkiadd 133 133 142
6914gkiadd 136 136 141
6915gkisge 0 136 1
6916gkbra 0 ulong top
6917gkepib beta ep
6918end
6919note gkgemmb's eight HMMAs of k16 step kk (the first waits on barrier 3 + kk of the LDS loads)
6920fn gkmmak kk uint to void
6921w = shl uint 1 add 3 kk
6922f = add 48 mul kk 16
6923for mi uint 2
6924for ni uint 4
6925d = add add 16 mul mi 16 mul ni 4
6926b = add add add f 8 mul shr ni 1 4 mul and ni 1 2
6927gkhmma d add f mul mi 4 b d w
6928w = 0
6929end
6930end
6931end
6932note Address pairs R48 + 2 e of the 16 elements e = 4 ni + c of one m16 half (row g + 8 (c >> 1),
6933note column 8 ni + 2t + (c & 1)) from half base hb; row stride R14, column stride R15.
6934fn gkhadrb hb uint to void
6935gkimadw 52 14 8 hb
6936gkimadw 48 15 0 hb
6937for e uint 16
6938if and ne e 0 ne e 2
6939src = hb
6940if ge and e 3 2
6941src = 52
6942end
6943gkimadw add 48 mul e 2 15 add mul shr e 2 8 and e 1 src
6944end
6945end
6946end
6947note One m16 half mi of the thread's accumulators (R16 + 16 mi + e): ep 2 loads M into R80-95, beta
6948note the old C; alpha R141; fused epilogue as gkquad64. Half bases: C R112 + 4 mi, second R114 + 4 mi.
6949fn gkhalfb mi uint beta bool ep uint to void
6950hc = add 112 mul mi 4
6951hy = add 114 mul mi 4
6952a0 = add 16 mul mi 16
6953if eq ep 2
6954gkhadrb hy
6955for e uint 16
6956gkldg add 80 e add 48 mul e 2 0 ne e 0
6957end
6958end
6959gkhadrb hc
6960if beta
6961for e uint 16
6962gkldg add 80 e add 48 mul e 2 0 ne e 0
6963end
6964end
6965for e uint 16
6966if beta
6967gkffmaf add a0 e add a0 e 141 add 80 e
6968else
6969gkfmulf add a0 e add a0 e 141
6970end
6971end
6972if eq ep 2
6973t = gkthm 140
6974for e uint 16
6975gkepm add a0 e add 80 e t
6976end
6977end
6978for e uint 16
6979gkstg add 48 mul e 2 add a0 e 0 ne e 0
6980end
6981if eq ep 1
6982for e uint 16
6983gkepm add a0 e add a0 e 255
6984end
6985gkhadrb hy
6986for e uint 16
6987gkstg add 48 mul e 2 add a0 e 0 ne e 0
6988end
6989end
6990end
6991note Thread C base R112 (and second base R114 when fused): row 64 R3 + 32 wm + g, column
6992note 64 R2 + 32 wn + 2t; half 1 bases R116 / R118 are 16 rows further.
6993fn gkepib beta bool ep uint to void
6994gkbt 12 6 1 32 true
6995gkbt 12 2 7 1 false
6996gkimadi 12 3 64 12
6997gkbt 13 5 1 32 true
6998gkbt 13 0 3 2 false
6999gkimadi 13 2 64 13
7000gkldc 14 968 false
7001gkldc 15 972 true
7002gkimad 12 12 14 255
7003gkimad 12 13 15 12
7004gkldc 10 976 false
7005gkldc 11 980 true
7006gkimad 12 6 10 12
7007gkimad 12 7 11 12
7008gkldc64 10 960
7009gkimadw 112 12 1 10
7010if ne ep 0
7011gkldc64 10 1016
7012gkimadw 114 12 1 10
7013end
7014gkimadw 116 14 16 112
7015if ne ep 0
7016gkimadw 118 14 16 114
7017end
7018gkldc 141 996 false
7019for mi uint 2
7020gkhalfb mi beta ep
7021end
7022gkexit
7023end
7024note Kernel table: ids 0..7 = 32x32 gemm (one 16 KiB slot each), ids 40, 42, ..., 54 = 64x64 gemm
7025note (40 + 2 (ma + 2 mb + 4 beta), two slots each: the pipelined K loop does not fit in one). Fused
7026note epilogue kernels (gkope, beta false): 25 (32x32) and 56 (64x64) ep 1 with ma 1 mb 0 (the FFN W1
7027note forward, ggxw), 26 and 58 ep 2 with ma 1 mb 1 (the FFN dF backward, ggxwt).
7028note gkgpr must be at least the highest GPR the kernel touches + 3: the hardware burns 2 GPRs of the
7029note QMD count for the program counter (Mesa NAK sm70.rs hw_reserved_gprs), so R(n-2) and R(n-1) are
7030note outside the allocation and a kernel using them hangs (launch wait -110). The 64x64 kernels use
7031note R158 (buffer step), so 160 hung (evidence/OMEGALLM/gpu-k8 attempt 1); 168 is the next multiple of 8.
7032note tests/omegallm/gpu-disasm.sh checks this rule per slot.
7033fn gkslot id uint to void
7034put gkat 0 mul ulong id 16384
7035end
7036fn gkall to bool
7037for g uint 20
7038id = g
7039ma = and g 1
7040mb = and shr g 1 1
7041bt = ne and g 4 0
7042ep = uint 0
7043if ge g 16
7044ep = add and g 1 1
7045ma = 1
7046mb = sub ep 1
7047bt = false
7048end
7049big = and ge g 8 and ne g 16 ne g 17
7050if ge g 8
7051id = add 40 mul sub g 8 2
7052end
7053if ge g 16
7054id = add 24 ep
7055end
7056if ge g 18
7057id = add 54 mul ep 2
7058end
7059if not gkemit id ma mb bt ep big
7060ret false
7061end
7062end
7063for g uint 10
7064ma = and g 1
7065mb = and shr g 1 1
7066bt = ne and g 4 0
7067ep = uint 0
7068if ge g 8
7069ep = sub g 7
7070ma = 1
7071mb = sub ep 1
7072bt = false
7073end
7074if not gkemitb add 27 g ma mb bt ep
7075ret false
7076end
7077end
7078ret true
7079end
7080note Emit the BF16 kernel (ma, mb, beta, ep) into slot id: ids 27 + ma + 2 mb + 4 beta, 35 ep 1 (ma 1
7081note mb 0), 36 ep 2 (ma 1 mb 1); highest GPR R142, so 152 (multiple of 8 at least 145).
7082fn gkemitb id uint ma uint mb uint bt bool ep uint to bool
7083gkslot id
7084gkgemmb ma mb bt ep
7085put gkgpr id 152
7086put gksm id 16384
7087if gt get gkat 0 mul ulong add id 1 16384
7088ret false
7089end
7090put gkthr id 128
7091ret true
7092end
7093note Emit one GEMM kernel into slot id (two slots when big, the 64x64 kernel); false on overflow.
7094fn gkemit id uint ma uint mb uint bt bool ep uint big bool to bool
7095gkslot id
7096ns = uint 1
7097if big
7098gkgemm64 ma mb bt ep
7099put gkgpr id 168
7100put gksm id 8704
7101ns = 2
7102else
7103gkgemm ma mb bt ep
7104put gkgpr id 104
7105put gksm id 4608
7106end
7107if gt get gkat 0 mul ulong add id ns 16384
7108ret false
7109end
7110put gkthr id 64
7111ret true
7112end
7113note Launch: QMD for kernel id with grid gx gy gz; args gkarg[0..gkna).
7114fn gkqq to void
7115q = mapaddr get gbmem 2
7116for i ulong 96
7117put gbq0 i 0
7118put gbq1 i 0
7119end
7120put gbq0 4 8388610
7121put gbq0 10 2293760
7122put gbq0 12 gblo shr add q 4096 8
7123put gbq0 14 5243392
7124put gbq0 34 65537
7125put gbq0 35 2049
7126put gbq1 4 20905984
7127put gbq1 9 3
7128put gbq1 10 1638400
7129put gbq1 12 gblo shr q 8
7130put gbq1 14 793772964
7131put gbq1 15 gblo add q 8192
7132put gbq1 16 gbhi add q 8192
7133put gbq1 17 6
7134put gbq1 19 2172715008
7135put gbq1 20 56
7136put gbq1 22 67108864
7137put gbq1 58 271581201
7138end
7139fn gkqcb to void
7140cb = mapaddr get gbmem 1
7141scr = add mapaddr get gbmem 2 16384
7142put gbq1 42 gblo shr cb 6
7143put gbq1 43 or uint and shr cb 38 524287 33554432
7144put gbq1 44 gblo shr scr 6
7145put gbq1 45 or uint and shr scr 38 524287 75497472
7146put gbq1 52 gblo shr add cb 768 6
7147put gbq1 53 or uint and shr add cb 768 38 524287 8388608
7148put gbq1 56 gblo shr scr 6
7149put gbq1 57 or uint and shr scr 38 524287 2147483648
7150gbdriver cb
7151e = add cb add 896 mul ulong get gkna 0 4
7152put gbcb 212 gbhi e
7153put gbcb 213 gblo e
7154end
7155note QMD word 36 (QMDV05_00 bits 1152-1180): SHARED_MEMORY_SIZE_SHIFTED7, then the MIN (32 KiB, hw 9),
7156note MAX (100 KiB, hw 26) and TARGET SM shared-memory configs, hw value = KiB / 4 + 1 from the GB10
7157note carveouts 0 8 16 32 64 100. TARGET is the smallest carveout that holds the kernel's shared memory
7158note times its register-limited blocks per SM, 65536 / (gkgpr * gkthr). Mesa NAK qmd.rs
7159note gv100_get_hw_smem_sizes sizes TARGET the same way from warp-limited workgroups; the register limit is
7160note the tighter bound here. A fixed 32 KiB TARGET under-sized the 32x32 GEMM (9 blocks * 4608 B) and the
7161note pipelined 64x64 GEMM (6 * 8704 B): under it the pipelined kernel was 1.12x slower than the k7b one,
7162note under this rule 0.91x (evidence/OMEGALLM/gpu-k8). Only ids with gkthr set may call this.
7163fn gksmw id uint to uint
7164sm = div add get gksm id 127 128
7165if lt sm 8
7166sm = 8
7167end
7168need = mul mul div 65536 mul get gkgpr id get gkthr id sm 128
7169tg = uint 9
7170if gt need 32768
7171tg = 17
7172end
7173if gt need 65536
7174tg = 26
7175end
7176ret or or 3426304 shl tg 23 sm
7177end
7178fn gkqmd id uint gx uint gy uint gz uint to void
7179gkqq
7180code = add mapaddr get gbmem 4 mul ulong id 16384
7181put gbq1 32 gblo shr code 4
7182put gbq1 33 or uint and shr code 36 2097151 20971520
7183put gbq1 59 gblo shr code 8
7184put gbq1 34 or get gkthr id 65536
7185put gbq1 35 or or 1 shl get gkgpr id 8 131072
7186put gbq1 36 gksmw id
7187put gbq1 39 gx
7188put gbq1 40 gy
7189put gbq1 41 gz
7190gkqcb
7191put gbcb 216 get gkthr id
7192end
7193note set after host writes to the arena; the next launch invalidates GPU L2 sysmem lines first
7194global gkinv uint 1
7195fn gkbuild id uint gx uint gy uint gz uint to void
7196gkqmd id gx gy gz
7197put gbused 0 0
7198gbsetup
7199if ne get gkinv 0 0
7200note L2_SYSMEM_NCOH_INVALIDATE (clc96f.h:73, MEM_OP_D operation 0x11 in bits 31:27): drop L2 lines
7201note the host rewrote in the arena; the previous launch ended with L2_FLUSH_DIRTY, so none is dirty
7202gbmth 0 40 4
7203gbemit 0
7204gbemit 0
7205gbemit 0
7206gbemit 2281701376
7207put gkinv 0 0
7208end
7209cb = mapaddr get gbmem 1
7210q = mapaddr get gbmem 2
7211mark = mapaddr get gbmem 3
7212gbdma cb 224
7213for i ulong 224
7214gbemit get gbcb i
7215end
7216na = get gkna 0
7217gbdma add cb 896 na
7218for i uint 32
7219if lt i na
7220gbemit get gkarg i
7221end
7222end
7223gbinline q false
7224gbdma add q 8192 1
7225gbemit 1
7226gbinline add q 4096 true
7227gbrel mark 1145324612
7228gbmth 0 40 4
7229gbemit 0
7230gbemit 0
7231gbemit 0
7232gbemit 2147483648
7233gbrel add mark 16 1179010630
7234end
7235note Launch profile (gprof): while gkpf[0] is 1, each launch appends id gx gy gz m n k wait_ns to gkpl
7236note (8 words, at most 1024 launches); gkmm sets gkps to its m n k first, other launches log zeros.
7237global gkpf uint 1
7238global gkpc ulong 1
7239global gkpl ulong 8192
7240global gkps uint 3
7241fn gkplog id uint gx uint gy uint gz uint w long to void
7242c = get gkpc 0
7243if and eq get gkpf 0 1 lt c 1024
7244o = mul c 8
7245put gkpl o ulong id
7246put gkpl add o 1 ulong gx
7247put gkpl add o 2 ulong gy
7248put gkpl add o 3 ulong gz
7249put gkpl add o 4 ulong get gkps 0
7250put gkpl add o 5 ulong get gkps 1
7251put gkpl add o 6 ulong get gkps 2
7252put gkpl add o 7 ulong w
7253put gkpc 0 add c 1
7254end
7255put gkps 0 0
7256put gkps 1 0
7257put gkps 2 0
7258end
7259note launch accounting: 0 launches, 1 build ns, 2 wait ns (send to the QMD release semaphore),
7260note 3 max wait ns
7261global gkt long 4
7262note wait for the release: spin up to 2 ms on the mark (vendored context.om nvwait sleeps 100 us
7263note between polls, which cost each launch about 110-150 us in the rung-6 gprof ZERO round trips),
7264note then hand the rest of the budget to nvwait, which keeps its checks, sleep and timeout
7265fn gkwait m long off ulong value uint budget ulong to bool
7266if eq get nvfault 0 0
7267start = monons
7268while lt sub monons start 2000000
7269if eq mapget m off 4 ulong value
7270if ne get nvput 0 get nvret 0
7271put nvret 0 get nvput 0
7272ret true
7273end
7274end
7275end
7276end
7277ret nvwait m off value budget
7278end
7279note wait for a word nvwait cannot (it refuses once nvret has caught up with nvput): spin 2 ms,
7280note then poll every 100 us up to budget ns; a timeout sets nverr -110 and nvfault as nvwait does
7281fn gkpoll m long off ulong value uint budget ulong to bool
7282if ne get nvfault 0 0
7283put nverr 0 neg long 22
7284ret false
7285end
7286start = monons
7287while true
7288if eq mapget m off 4 ulong value
7289ret true
7290end
7291el = sub monons start
7292if or lt el 0 ge el long budget
7293put nverr 0 neg long 110
7294put nvfault 0 1
7295ret false
7296end
7297if ge el 2000000
7298r = sleepns long 100000
7299if ne r 0
7300put nverr 0 r
7301put nvfault 0 1
7302ret false
7303end
7304end
7305end
7306ret false
7307end
7308note submit the built pushbuffer and wait for the launch to retire; returns the wait in ns, or -1
7309fn gksend to long
7310r = mapset get gbmem 2 8192 4 0
7311r = mapset get gbmem 3 0 4 0
7312r = mapset get gbmem 3 16 4 0
7313n = get gbused 0
7314r = mapwrite get gbmem 0 0 gbcmd 0 n
7315put nvstage 0 41
7316if ne r long n
7317put nverr 0 neg long 5
7318ret neg long 1
7319end
7320t1 = monons
7321if not nvsend get gbmem 0 0 uint div n 4
7322ret neg long 1
7323end
7324put nvstage 0 42
7325if not gkwait get gbmem 3 16 1179010630 5000000000
7326ret neg long 1
7327end
7328put nvstage 0 43
7329if ne mapget get gbmem 3 0 4 1145324612
7330put nverr 0 neg long 5
7331put nvfault 0 1
7332ret neg long 1
7333end
7334note the QMD's own release semaphore is written after the grid retires and may land after the
7335note second mark (omega_blackwell_engine.c be_wait_release_semaphore waits for it): wait, not peek
7336put nvstage 0 44
7337if not gkpoll get gbmem 2 8192 6 1000000000
7338ret neg long 1
7339end
7340w = sub monons t1
7341put gkt 2 add get gkt 2 w
7342if gt w get gkt 3
7343put gkt 3 w
7344end
7345ret w
7346end
7347fn gklaunch id uint gx uint gy uint gz uint to bool
7348if or or or eq gx 0 eq gy 0 eq gz 0 ne get nvfault 0 0
7349put nverr 0 neg long 22
7350ret false
7351end
7352if or eq get gkna 0 0 gt get gkna 0 32
7353put nverr 0 neg long 22
7354ret false
7355end
7356put nvstage 0 40
7357t0 = monons
7358gkbuild id gx gy gz
7359put gkt 0 add get gkt 0 1
7360put gkt 1 add get gkt 1 sub monons t0
7361w = gksend
7362if lt w 0
7363ret false
7364end
7365gkplog id gx gy gz w
7366ret true
7367end
7368note Open the chip: slots 0 cmd, 1 cbank, 2 qmd, 3 marks, 4 code, 5 arena of asz bytes.
7369fn gkopen asz ulong to bool
7370if not nvfull
7371ret false
7372end
7373if not nvchan
7374ret false
7375end
7376for i ulong 6
7377size = ulong 4096
7378if or eq i 0 eq i 2
7379size = 65536
7380end
7381if eq i 4
7382size = 1048576
7383end
7384if eq i 5
7385size = asz
7386end
7387note slots 0-4 (pushbuffer, cbank, QMD, marks, code) are CPU-written or host-polled: GPU-uncached
7388note as in omega_blackwell_engine.c alloc_checked; cached, a release sat in L2 about 1.06 s (cortex
7389note d03a999a). The arena (slot 5) stays cached: gkinv invalidates it after host writes and every
7390note launch ends with L2_FLUSH_DIRTY before its second mark.
7391m = nvalloc size eq i 5
7392if le m 0
7393ret false
7394end
7395put gbmem i m
7396end
7397if not gkall
7398put nverr 0 neg long 7
7399ret false
7400end
7401r = mapwrite get gbmem 4 0 gkcode 0 1048576
7402ret eq r long 1048576
7403end
7404fn gkbase to ulong
7405ret mapaddr get gbmem 5
7406end
7407note GEMM host side. Operand ab (0 = A, 1 = B): device address, outer/depth/batch strides in bytes.
7408note Mode 1 (K) when the depth stride is 4; gkmm derives per-load delta and tile step for the tile.
7409fn gkopa ab uint ptr ulong so uint sk uint s1 uint s2 uint to void
7410o = mul ab 8
7411put gkarg o gblo ptr
7412put gkarg add o 1 gbhi ptr
7413put gkarg add o 2 so
7414put gkarg add o 3 sk
7415put gkarg add o 4 s1
7416put gkarg add o 5 s2
7417md = uint 0
7418if eq sk 4
7419md = 1
7420end
7421put gkmd ab md
7422end
7423fn gkopc ptr ulong scm uint scn uint s1 uint s2 uint al f32 to void
7424put gkarg 16 gblo ptr
7425put gkarg 17 gbhi ptr
7426put gkarg 18 scm
7427put gkarg 19 scn
7428put gkarg 20 s1
7429put gkarg 21 s2
7430put gkarg 25 bitcast uint al
7431end
7432note C[z] = alpha A[z] B[z] (+ C[z] if beta), z = z1 h2 + z2, z < nz; m n multiples of 32, k of 16.
7433note m and n both multiples of 64 take the 64x64 kernels (ids 40..54 even), else the 32x32 ones (ids 0..7).
7434fn gkshape m uint n uint k uint nz uint h2 uint to bool
7435if ne rem m 32 0
7436ret false
7437end
7438if ne rem n 32 0
7439ret false
7440end
7441if or ne rem k 16 0 eq k 0
7442ret false
7443end
7444if or eq nz 0 gt nz 65535
7445ret false
7446end
7447ret ne h2 0
7448end
7449note Batch split magic: z1 = umulhi(z, mg) must equal z / h2 for every z < nz; else nverr -33.
7450fn gkmagic nz uint h2 uint to uint
7451mg = uint 0
7452if gt h2 1
7453mg = uint div add 4294967296 ulong sub h2 1 ulong h2
7454end
7455ok = true
7456for z uint nz
7457if ne uint shr mul ulong z ulong mg 32 div z h2
7458ok = false
7459end
7460end
7461if not ok
7462put nverr 0 neg long 33
7463end
7464ret mg
7465end
7466note Per-load delta (word 6) and tile step (word 7) of operand ab for tile width t (32 or 64).
7467fn gkopt ab uint t uint to void
7468o = mul ab 8
7469so = get gkarg add o 2
7470sk = get gkarg add o 3
7471gd = mul sk 2
7472st = mul sk 16
7473if eq t 64
7474gd = sk
7475st = mul sk 8
7476end
7477if eq get gkmd ab 1
7478gd = mul so 4
7479if eq t 64
7480gd = mul so 8
7481end
7482end
7483put gkarg add o 6 gd
7484put gkarg add o 7 st
7485end
7486note Tile width: 64 when m and n are both multiples of 64, else 32.
7487fn gktile m uint n uint to uint
7488if and eq rem m 64 0 eq rem n 64 0
7489ret 64
7490end
7491ret 32
7492end
7493note Causal mode cz, set by gkopz before gkmm (attention over a T x T score matrix, zero above its
7494note diagonal); gkmm consumes it, so a GEMM without gkopz is dense. 0 dense; 1 A is
7495note zero above its diagonal (C = P U, DQ = DS K), so row tile r stops after K column (r + 1) t;
7496note 2 A is zero below its diagonal (C = P^T DO, DK = DS^T Q), so row tile r starts at K row r t;
7497note 3 only C on and below the diagonal is needed (S = Q K^T, DP = DO U^T), so blocks above it exit.
7498note Modes 1 and 2 need k = m, mode 3 needs m = n. Mutant q (OMUTQ 0) stops mode 1 one K tile short.
7499const OMUTQ ulong 1
7500note BF16 path (docs/omegallm/CONTRACT-BF16.md): gkbf 1 (gtrainb, gprofb and the gb.om gates) runs every
7501note 64x64 gkmm on the tensor-core kernels 27..36; 32x32 GEMMs (n = E) stay f32. gkbck 1 checks each
7502note BF16 launch against gb.om's sampled reference (gbpre before, gbpost after), with gkbp holding
7503note m n k nz h2 beta ep cz. No fallback: a GEMM the kernels cannot take fails with nverr -22.
7504global gkbf uint 1
7505global gkbck uint 1
7506global gkbp uint 8
7507global gkcz uint 1
7508fn gkopz cz uint to void
7509put gkcz 0 cz
7510end
7511fn gkczok m uint n uint k uint cz uint to bool
7512if eq cz 0
7513ret true
7514end
7515if le cz 2
7516ret eq k m
7517end
7518if eq cz 3
7519ret eq m n
7520end
7521ret false
7522end
7523note GEMM K-loop words: 22 K tiles (kd deep), 26 ka, 27 kc, 28 X for tile t and the gkopz mode,
7524note which it consumes
7525fn gkczarg t uint k uint to void
7526cz = get gkcz 0
7527put gkcz 0 0
7528kd = uint 16
7529if eq t 64
7530kd = 8
7531if eq get gkbf 0 1
7532kd = 32
7533end
7534end
7535pt = div t kd
7536put gkarg 22 div k kd
7537put gkarg 26 0
7538put gkarg 27 0
7539put gkarg 28 1073741824
7540if eq cz 1
7541put gkarg 22 pt
7542put gkarg 26 pt
7543if eq OMUTQ 0
7544put gkarg 22 sub pt 1
7545end
7546end
7547if eq cz 2
7548put gkarg 26 neg pt
7549put gkarg 27 pt
7550end
7551if eq cz 3
7552put gkarg 28 0
7553end
7554end
7555note Fused epilogue ep for the next gkmm, which consumes it (see gkepi): 1 also stores
7556note Y = C > 0 ? C : 0 at y, 2 stores M > 0 ? C : 0 with M read at y; y has C's strides. ep 1 needs
7557note operand modes ma 1 mb 0 and ep 2 ma 1 mb 1 (the only fused kernels, gkall), both beta false.
7558note Measured (k9, evidence/OMEGALLM/gpu-k9): replaces gr.om RELUF/RELUB, 16 launches per max-shape step
7559note fewer; summed GPU wait 144.56 -> 133.54 ms/step (-7.62%, three rounds each, non-quiet); t200 checkpoint
7560note bit-identical to the unfused k8 product.
7561global gkep uint 1
7562fn gkope ep uint y ulong to void
7563put gkep 0 ep
7564put gkarg 30 gblo y
7565put gkarg 31 gbhi y
7566end
7567note true when fused epilogue ep may run with the pending operand modes and beta
7568fn gkepok ep uint beta bool to bool
7569if eq ep 0
7570ret true
7571end
7572if or gt ep 2 beta
7573ret false
7574end
7575if ne get gkmd 0 1
7576ret false
7577end
7578ret eq get gkmd 1 sub ep 1
7579end
7580fn gkmm m uint n uint k uint nz uint h2 uint beta bool to bool
7581ep = get gkep 0
7582put gkep 0 0
7583if not gkepok ep beta
7584put gkcz 0 0
7585put nverr 0 neg long 22
7586ret false
7587end
7588if or not gkshape m n k nz h2 not gkczok m n k get gkcz 0
7589put gkcz 0 0
7590put nverr 0 neg long 22
7591ret false
7592end
7593put nverr 0 0
7594put gkarg 23 gkmagic nz h2
7595if ne get nverr 0 0
7596put gkcz 0 0
7597ret false
7598end
7599put gkarg 24 neg h2
7600t = gktile m n
7601bf = and eq get gkbf 0 1 eq t 64
7602if bf
7603if not gkbfok k
7604put gkcz 0 0
7605put nverr 0 neg long 22
7606ret false
7607end
7608else
7609gkopt 0 t
7610gkopt 1 t
7611end
7612put gkbp 7 get gkcz 0
7613gkczarg t k
7614put gkps 0 m
7615put gkps 1 n
7616put gkps 2 k
7617id = gkmmid t beta ep
7618if bf
7619id = gkmmb beta ep
7620end
7621if not and bf eq get gkbck 0 1
7622ret gklaunch id div n t div m t nz
7623end
7624gkbpset m n k nz h2 beta
7625put gkbp 6 ep
7626gbpre
7627if not gklaunch id div n t div m t nz
7628ret false
7629end
7630ret gbpost
7631end
7632note BF16 operands both take the 16-byte tile (gkbfop) and k is a multiple of the K tile 32
7633fn gkbfok k uint to bool
7634ba = gkbfop 0
7635bb = gkbfop 1
7636ret and and ba bb eq rem k 32 0
7637end
7638note gkbp words 0..5 for gb.om's check: m n k nz h2 beta
7639fn gkbpset m uint n uint k uint nz uint h2 uint beta bool to void
7640put gkbp 0 m
7641put gkbp 1 n
7642put gkbp 2 k
7643put gkbp 3 nz
7644put gkbp 4 h2
7645put gkbp 5 0
7646if beta
7647put gkbp 5 1
7648end
7649end
7650note Operand ab for the BF16 tile: 16-byte aligned pointer and batch strides, and 16-byte loads
7651note (mode 1: so a multiple of 16; mode 0: so = 4, sk a multiple of 16); sets delta and tile step.
7652fn gkbfop ab uint to bool
7653o = mul ab 8
7654so = get gkarg add o 2
7655sk = get gkarg add o 3
7656ok = eq rem get gkarg o 16 0
7657ok = and ok eq rem get gkarg add o 4 16 0
7658ok = and ok eq rem get gkarg add o 5 16 0
7659if eq get gkmd ab 1
7660ok = and ok eq rem so 16 0
7661put gkarg add o 6 mul so 16
7662else
7663ok = and ok and eq so 4 eq rem sk 16 0
7664put gkarg add o 6 sk
7665end
7666put gkarg add o 7 mul sk 32
7667ret ok
7668end
7669note BF16 kernel id for beta and fused epilogue ep; sets the arg count
7670fn gkmmb beta bool ep uint to uint
7671if ne ep 0
7672put gkna 0 32
7673ret add 34 ep
7674end
7675put gkna 0 29
7676id = add add 27 get gkmd 0 mul 2 get gkmd 1
7677if beta
7678id = add id 4
7679end
7680ret id
7681end
7682note kernel id of a gkmm (gkall's table) for tile t, beta and fused epilogue ep; sets the arg count
7683fn gkmmid t uint beta bool ep uint to uint
7684if ne ep 0
7685put gkna 0 32
7686if eq t 64
7687ret add 54 mul ep 2
7688end
7689ret add 24 ep
7690end
7691put gkna 0 29
7692id = add get gkmd 0 mul 2 get gkmd 1
7693if beta
7694id = add id 4
7695end
7696if eq t 64
7697id = add 40 mul id 2
7698end
7699ret id
7700end
7701note SPDX-License-Identifier: AGPL-3.0-only
7702note gr.om: row and elementwise SM121 kernels for the ollm GPU step (slots 9..24), emitted after
7703note gkall with the model dimensions baked in as immediates. Words verified with nvdisasm -b SM121.
7704note Launch shape: 64 threads per block, R4 = ctaid.x 64 + tid.x, R3 = ctaid.y, R7 = ctaid.z;
7705note arg 0 is the R4 bound (threads at or past it exit). Registers: R8-R23 pointers and ints,
7706note R24-R29 constants, R26 = 1, R30 loop counter, R31 = -1, R32-R63 data, R64-R69 scalars.
7707global grd ulong 8
7708note grd: 0 V 1 T 2 D 3 H 4 F 5 B
7709fn grf v f64 to uint
7710ret bitcast uint f32 v
7711end
7712fn grarg i uint to uint
7713ret add 896 mul i 4
7714end
7715fn grfsetp p uint a uint b uint cc uint to void
7716gkpr or 29195 shl a 24 b or or 66060288 shl cc 12 shl p 17
7717end
7718fn grfsel d uint a uint b uint p uint to void
7719gkfx or or 29192 shl d 16 shl a 24 b shl p 23
7720end
7721fn grfmax d uint a uint b uint to void
7722gkfx or or 29193 shl d 16 shl a 24 b 125829120
7723end
7724fn grfadd d uint a uint b uint to void
7725gkfx or or 29217 shl d 16 shl a 24 b 0
7726end
7727fn grfsub d uint a uint b uint to void
7728gkfx or or 29217 shl d 16 shl a 24 or b 2147483648 0
7729end
7730fn grmufu d uint a uint op uint to void
7731gkvr or 29448 shl d 16 a op
7732end
7733note pointer number i (odd) sits in arg slots i+1, i+2 so its LDC.64 is 8-byte aligned
7734fn grptr d uint i uint idx uint scale uint to void
7735gkldc64 d grarg add i 1
7736gkimadw d idx scale d
7737end
7738fn grtop c uint n uint to ulong
7739gkmovi c n
7740ret div get gkat 0 16
7741end
7742fn grbot c uint top ulong to void
7743gkiadd c c 31
7744gkisge 0 c 1
7745gkbra 0 top
7746end
7747fn gradv p uint n uint to void
7748gkimadw p 26 n p
7749end
7750fn grld8 d uint p uint first bool to void
7751for u uint 8
7752gkldg add d u p mul u 4 or ne u 0 not first
7753end
7754end
7755fn grst8 p uint s uint to void
7756for u uint 8
7757gkstg p add s u mul u 4 ne u 0
7758end
7759end
7760fn grpro to void
7761gkldc 1 892 false
7762gks2r 0 33 true
7763gks2r 2 37 true
7764gks2r 3 38 true
7765gks2r 7 39 true
7766gkq 4278482860 27392 134220288 or gkcw 4 2 7 get gkpend 0 8192
7767put gkpend 0 4
7768gkimadi 4 2 64 0
7769gkldc 5 896 false
7770gkisetp 0 4 5 6
7771gkexitp 0
7772gkmovi 31 4294967295
7773gkmovi 26 1
7774end
7775note 9 EMB: out[b T + t][i] = wte[x[b T + t]][i] + wpe[t][i]; grid (D/64, T, B); args D out wte wpe x
7776fn gremb to void
7777d = uint get grd 2
7778t = uint get grd 1
7779grpro
7780gkimadi 8 7 t 3
7781grptr 12 7 8 4
7782gkldg 9 12 0 false
7783gkimadi 10 9 d 4
7784grptr 14 3 10 4
7785gkimadi 11 3 d 4
7786grptr 16 5 11 4
7787gkimadi 20 8 d 4
7788grptr 18 1 20 4
7789gkldg 32 14 0 false
7790gkldg 33 16 0 true
7791grfadd 34 32 33
7792gkstg 18 34 0 false
7793gkexit
7794end
7795note RMS kernels: one warp per row (threads R*32, arg 0); lane l holds columns l + 32 u, u < D/32,
7796note so each warp load and store is one coalesced 128-byte line and every row is read once.
7797note R8 = row, R9 = lane; ld/st w: n words at p + 128 u (gkldg/gkstg chain flag: first false).
7798fn grrrow to void
7799grpro
7800gkshr 8 4 5
7801gkandi 9 4 31
7802end
7803fn grrptr d uint i uint d4 uint to void
7804grptr d i 8 d4
7805gkimadw d 9 4 d
7806end
7807fn grldw d uint p uint n uint first bool to void
7808for u uint n
7809gkldg add d u p mul u 128 or ne u 0 not first
7810end
7811end
7812fn grstw p uint s uint n uint to void
7813for u uint n
7814gkstg p add s u mul u 128 ne u 0
7815end
7816end
7817note RMS register bank k (k < 4) of n = D/32 words: R32 + 8 k while n <= 8 (the v1 layout, scalars at
7818note R64..R67 above it); from n = 9 the banks sit above the scalars at R72 + n k, so D <= 384 (n <= 12)
7819note ends at R119 and grall raises the slot's GPR count to 128 (gk.om: highest GPR + 3, multiple of 8).
7820fn grrb k uint n uint to uint
7821if le n 8
7822ret add 32 mul 8 k
7823end
7824ret add 72 mul n k
7825end
7826note 10 RMSF: per row r < R: s = 1/sqrt(sum v^2 / D + 1e-5); y = v s g; args R*32 v g y s
7827fn grrmsf to void
7828d = uint get grd 2
7829d4 = mul d 4
7830n = div d 32
7831grrrow
7832gkmovi 24 grf div 1.0f64 f64 d
7833gkmovi 25 grf 0.00001f64
7834grrptr 10 1 d4
7835gkldc64 14 grarg 4
7836gkimadw 14 9 4 14
7837b0 = grrb 0 n
7838b1 = grrb 1 n
7839grldw b0 10 n true
7840grldw b1 14 n false
7841gkmovz 64
7842for u uint n
7843gkffmaf 64 add b0 u add b0 u 64
7844end
7845grwred 0 0
7846gkffmaf 65 64 24 25
7847grmufu 66 65 5120
7848grptr 12 7 8 4
7849gkstg 12 66 0 false
7850grrptr 16 5 d4
7851for u uint n
7852gkfmulf add b0 u add b0 u 66
7853gkfmulf add b0 u add b0 u add b1 u
7854end
7855grstw 16 b0 n
7856gkexit
7857end
7858note Mutant r: 1 in the product, 0 in the mutant, whose RMSB row sum sum(da g v) skips the lane-1
7859note butterfly step (k1 from half the row), which gcmp2 must catch.
7860const OMUTR ulong 1
7861note 11 RMSB: per row: k1 = s^2 sum(da g v)/D; dh += (da g - v k1) s; w = da v s; args R*32 v g s da dh w
7862fn grrmsb to void
7863d = uint get grd 2
7864d4 = mul d 4
7865n = div d 32
7866grrrow
7867gkmovi 24 grf div 1.0f64 f64 d
7868grrptr 10 1 d4
7869gkldc64 12 grarg 4
7870gkimadw 12 9 4 12
7871grrptr 14 7 d4
7872grrptr 16 9 d4
7873grptr 20 5 8 4
7874b0 = grrb 0 n
7875b1 = grrb 1 n
7876b2 = grrb 2 n
7877b3 = grrb 3 n
7878grldw b0 14 n true
7879grldw b1 12 n false
7880grldw b2 10 n false
7881grldw b3 16 n false
7882gkldg 67 20 0 true
7883gkmovz 64
7884for u uint n
7885gkfmulf add b1 u add b0 u add b1 u
7886gkffmaf 64 add b1 u add b2 u 64
7887end
7888if eq OMUTR 0
7889grwred 0 1
7890else
7891grwred 0 0
7892end
7893gkfmulf 66 67 67
7894gkfmulf 66 66 64
7895gkfmulf 66 66 24
7896grrptr 18 11 d4
7897for u uint n
7898gkfmulf add b0 u add b0 u add b2 u
7899gkfmulf add b0 u add b0 u 67
7900gkfmulf add b2 u add b2 u 66
7901grfsub add b1 u add b1 u add b2 u
7902gkfmulf add b1 u add b1 u 67
7903grfadd add b3 u add b3 u add b1 u
7904end
7905grstw 16 b3 n
7906grstw 18 b0 n
7907gkexit
7908end
7909note 13/14 COLSUM: out[c ostr + i] (=, +=) sum_k in[c cstr + k kstr + i]; grid (n/64, chunks)
7910note arg slots: 0 n, 1 kcnt (>= 1), 2-3 in, 4 kstr, 5 cstr, 6-7 out, 8 ostr (strides in bytes)
7911fn grcol acc bool to void
7912grpro
7913grptr 10 1 4 4
7914gkldc 20 grarg 5 false
7915gkimadwr 10 3 20 10
7916gkldc 21 grarg 4 false
7917gkldc 30 grarg 1 false
7918gkmovz 64
7919top = div get gkat 0 16
7920gkldg 32 10 0 false
7921grfadd 64 64 32
7922gkimadwr 10 26 21 10
7923grbot 30 top
7924grptr 12 5 4 4
7925gkldc 22 grarg 8 false
7926gkimadwr 12 3 22 12
7927if acc
7928gkldg 33 12 0 false
7929grfadd 64 64 33
7930end
7931gkstg 12 64 0 false
7932gkexit
7933end
7934note SHFL.BFLY PT, Rd, Ra, lane, 0x1f (codegen.c:671 SHFL.DOWN with the BFLY mode bits, sm121.tbl:183)
7935fn grshfl d uint a uint lane uint to void
7936gkvr or or 32649 shl d 16 shl a 24 or 201334528 shl lane 21 917504
7937end
7938note Softmax kernels: one warp per row; lane l holds columns l + 32 u, u < T/32, in R32.. (p) and
7939note R48.. (dp), so every load and store of a warp is one coalesced 128-byte line. Threads: T 32
7940note per score block (arg 0), grid (T/2, Z); R21 = row t, R22 = lane, R10/R12 = row base + 4 lane.
7941fn grsrow tt uint dp bool to void
7942grpro
7943gkshr 21 4 5
7944gkandi 22 4 31
7945gkimadi 8 3 tt 21
7946grptr 10 1 8 mul tt 4
7947gkimadw 10 22 4 10
7948if dp
7949grptr 12 3 8 mul tt 4
7950gkimadw 12 22 4 12
7951end
7952end
7953note sum (op 0) or max (op 1) of R64 across the warp by butterfly, R65 scratch
7954fn grwred op uint skip uint to void
7955for i uint 5
7956o = uint shr 16 i
7957if ne o skip
7958grshfl 65 64 o
7959if eq op 0
7960grfadd 64 64 65
7961else
7962grfmax 64 64 65
7963end
7964end
7965end
7966end
7967note Mutant s: 1 in the product, 0 in the mutant, whose SOFTF row sum skips the lane-1 butterfly step
7968note (each lane then normalizes by half the row), which gcmp and gcmp2 must catch.
7969const OMUTS ulong 1
7970note Causal chunks (k6): row t (R21) loads 32-column chunk u only if t >= 32 u (P u-1 for u 1..7;
7971note chunk 0 and chunks from 8 on stay PT, loaded and masked), and stores chunk u only if
7972note t >= 64 floor(u/2). The causal GEMMs read a P or DS row only up to the end of its diagonal tile
7973note (32 or 64 columns), so later chunks are neither read nor written and keep stale arena data.
7974note Predicates are set before the loads: an ISETP between them would wait on their scoreboard.
7975note Mutant w: 1 in the product, 0 in the mutant, whose loads also skip the diagonal chunk (t >= 32 u + 32).
7976const OMUTW ulong 1
7977fn grcpred n uint off uint to void
7978for u uint n
7979if and ge u 1 le u 7
7980gkisge sub u 1 21 add mul u 32 off
7981end
7982end
7983end
7984fn grcp u uint to uint
7985if or eq u 0 ge u 8
7986ret 7
7987end
7988ret sub u 1
7989end
7990fn grcwo to uint
7991if eq OMUTW 0
7992ret 32
7993end
7994ret 0
7995end
7996note 15 SOFTF: causal softmax of row t of score block z in place, zero above t; args T*32 P
7997fn grsoftf to void
7998tt = uint get grd 1
7999n = div tt 32
8000grsrow tt false
8001gkmovi 27 4286578688
8002gkmovi 28 grf 1.4426950408889634f64
8003gkmovi 29 grf neg 1.4426950408889634f64
8004gkmovi 23 32
8005grcpred n grcwo
8006for u uint n
8007gkldgp add 32 u 10 mul u 128 ne u 0 grcp u
8008end
8009gkimadi 20 22 1 255
8010gkmovi 64 4286578688
8011for u uint n
8012gkisetp 1 20 21 4
8013grfsel add 32 u 27 add 32 u 1
8014gkiadd 20 20 23
8015grfmax 64 64 add 32 u
8016end
8017grwred 1 0
8018gkfmulf 66 64 29
8019gkmovz 64
8020for u uint n
8021gkffmaf add 32 u add 32 u 28 66
8022grmufu add 32 u add 32 u 2048
8023grfadd 64 64 add 32 u
8024end
8025if eq OMUTS 0
8026grwred 0 1
8027else
8028grwred 0 0
8029end
8030grmufu 67 64 4096
8031grcpred n 0
8032for u uint n
8033gkfmulf add 32 u add 32 u 67
8034gkstgp 10 add 32 u mul u 128 ne u 0 grcp and u 4294967294
8035end
8036gkexit
8037end
8038note 16 SOFTB: ds = p (dp - sum p dp) / sqrt(E) in place over dp; args T*32 P DP. p and dp above the
8039note diagonal (column > t) read as 0: their chunks may be unloaded (k6) or stale (the causal DP GEMM,
8040note gkmm cz 3, skips those tiles), and 0 times a stale Inf or NaN is NaN.
8041fn grsoftb to void
8042tt = uint get grd 1
8043n = div tt 32
8044e = div get grd 2 get grd 3
8045grsrow tt true
8046gkmovi 29 grf div 1.0f64 fsqrt f64 e
8047grcpred n grcwo
8048for u uint n
8049gkldgp add 32 u 10 mul u 128 ne u 0 grcp u
8050end
8051for u uint n
8052gkldgp add 48 u 12 mul u 128 true grcp u
8053end
8054gkmovi 23 32
8055gkimadi 20 22 1 255
8056for u uint n
8057gkisetp 1 20 21 4
8058grfsel add 48 u 255 add 48 u 1
8059grfsel add 32 u 255 add 32 u 1
8060gkiadd 20 20 23
8061end
8062gkmovz 64
8063for u uint n
8064gkffmaf 64 add 32 u add 48 u 64
8065end
8066grwred 0 0
8067grcpred n 0
8068for u uint n
8069grfsub add 48 u add 48 u 64
8070gkfmulf add 48 u add 48 u add 32 u
8071gkfmulf add 48 u add 48 u 29
8072gkstgp 12 add 48 u mul u 128 ne u 0 grcp and u 4294967294
8073end
8074gkexit
8075end
8076note 19 COPY y = x; 20 ZERO; args n then pointers. The FFN ReLU forward and backward mask are fused
8077note into the GEMM epilogue (gk.om gkepi, kernels 25/26/56/58), so slots 17/18 stay empty.
8078fn grew kind uint to void
8079grpro
8080grptr 10 1 4 4
8081if eq kind 20
8082gkstg 10 255 0 false
8083else
8084grptr 12 3 4 4
8085gkldg 32 10 0 false
8086grfadd 34 32 255
8087gkstg 12 34 0 false
8088end
8089gkexit
8090end
8091note 21 CE: per row r of logits L (R x V): m, e = exp(l - m), sum; L <- (e/sum - [c = y]) sc;
8092note stats[r] = (m, sum, l_y); sc = 1/(T B); args R*32 L y st. One warp per row (grrrow): lane l
8093note holds columns l + 32 u, read as 8 coalesced 128-byte lines per step of 256 columns. Pass 1 takes
8094note the lane max, then the warp max. Pass 2 sums e per lane without storing it, then the warp sum
8095note of the lane totals. Pass 3 recomputes e with the same FFMA and MUFU (bit-identical to pass 2),
8096note scales it and stores it. The lane that owns column y (lane y & 31, the thread that stored it)
8097note subtracts sc there and writes stats, so no lane reads another lane's store. l_y is loaded
8098note before any store, and the next instruction waits for that load.
8099note Each lane's sum is compensated (Kahan: R40 carries the lost low part, R41 y, R42 t) and the
8100note lane total is s - c. A plain sequential f32 sum over V 4096 in one thread biased log sum low by
8101note about 1.04e-5 relative on trained checkpoints (gdiag receipt). Mutant k (OMUTK 0) keeps the
8102note plain per-lane sum. Mutant x (OMUTX 0) skips the lane-1 butterfly step of the warp sum, which
8103note gcmp2 and geval must catch. Kernels launch with 72 GPRs (gkgpr in grall); Kahan state in
8104note R69-R71 hung this kernel (launch wait -110, evidence/OMEGALLM/geval/a1), so it uses R40-R42.
8105note The warp sum overwrites R64 and its scratch R65 (grwred), so R43 keeps m and pass 3 recomputes
8106note R65 = -m log2e from it (the same FMUL as pass 2, so the same value).
8107const OMUTK ulong 1
8108const OMUTX ulong 1
8109fn grce to void
8110v = uint get grd 0
8111v4 = mul v 4
8112it = div v 256
8113sc = div 1.0f64 f64 mul get grd 1 get grd 5
8114grrrow
8115gkmovi 28 grf 1.4426950408889634f64
8116gkmovi 29 grf neg 1.4426950408889634f64
8117gkmovi 25 grf sc
8118grptr 12 3 8 4
8119gkldg 20 12 0 false
8120grptr 14 1 8 v4
8121gkimadw 14 20 4 14
8122gkldg 68 14 0 false
8123grrptr 10 1 v4
8124gkmovi 64 4286578688
8125top = grtop 30 it
8126grldw 32 10 8 true
8127for u uint 8
8128grfmax 64 64 add 32 u
8129end
8130gradv 10 1024
8131grbot 30 top
8132grwred 1 0
8133grfadd 43 64 255
8134gkfmulf 65 64 29
8135grrptr 10 1 v4
8136gkmovz 66
8137gkmovz 40
8138top2 = grtop 30 it
8139grldw 32 10 8 true
8140for u uint 8
8141gkffmaf add 32 u add 32 u 28 65
8142grmufu add 32 u add 32 u 2048
8143if eq OMUTK 0
8144grfadd 66 66 add 32 u
8145end
8146if ne OMUTK 0
8147grfsub 41 add 32 u 40
8148grfadd 42 66 41
8149grfsub 66 42 66
8150grfsub 40 66 41
8151grfadd 66 42 255
8152end
8153end
8154gradv 10 1024
8155grbot 30 top2
8156grfsub 64 66 40
8157if eq OMUTX 0
8158grwred 0 1
8159else
8160grwred 0 0
8161end
8162grmufu 67 64 4096
8163gkfmulf 67 67 25
8164gkfmulf 65 43 29
8165grrptr 10 1 v4
8166top3 = grtop 30 it
8167grldw 32 10 8 true
8168for u uint 8
8169gkffmaf add 32 u add 32 u 28 65
8170grmufu add 32 u add 32 u 2048
8171gkfmulf add 32 u add 32 u 67
8172end
8173grstw 10 32 8
8174gradv 10 1024
8175grbot 30 top3
8176gkandi 21 20 31
8177gkisetp 1 9 21 5
8178gkexitp 1
8179gkldg 33 14 0 false
8180grfsub 33 33 25
8181gkstg 14 33 0 false
8182grptr 16 5 8 12
8183gkstg 16 43 0 false
8184gkstg 16 64 4 true
8185gkstg 16 68 8 true
8186gkexit
8187end
8188note 22 CSRG: gw[c][i] += sum over k in [start[c], start[c+1]) of dh[ord[k]][i]; grid (D/64, V)
8189note args D dh start ord gw (start: V + 1 uints, ord: rows sorted by token, ascending within a token)
8190fn grcsr to void
8191d = uint get grd 2
8192grpro
8193grptr 10 3 3 4
8194gkldg 20 10 0 false
8195gkldg 21 10 4 true
8196gkisetp 1 20 21 2
8197gkexitp 1
8198gkimad 30 20 31 21
8199grptr 12 5 20 4
8200grptr 16 1 4 4
8201gkmovz 64
8202top = div get gkat 0 16
8203gkldg 22 12 0 false
8204gkimadw 14 22 mul d 4 16
8205gkldg 32 14 0 false
8206grfadd 64 64 32
8207gradv 12 4
8208grbot 30 top
8209gkimadi 23 3 d 4
8210grptr 18 7 23 4
8211gkldg 33 18 0 false
8212grfadd 64 64 33
8213gkstg 18 64 0 false
8214gkexit
8215end
8216note 23 SQN: out[c] = sum over i in [512 c, 512 c + 512) of g[i]^2 (f32); args nchunk g out. The
8217note gradient buffer is padded with zeros to a multiple of 512 (ggplan, ggbwd), so no tail guard.
8218fn grsqn to void
8219grpro
8220grptr 10 1 4 2048
8221gkmovz 64
8222top = grtop 30 64
8223grld8 32 10 true
8224for u uint 8
8225gkffmaf 64 add 32 u add 32 u 64
8226end
8227gradv 10 32
8228grbot 30 top
8229grptr 12 3 4 4
8230gkstg 12 64 0 false
8231gkexit
8232end
8233note OMUTH: 1 in the product; 0 in mutant h (beta1 0.8 on chip), which gacmp must catch.
8234const OMUTH ulong 1
8235note 24 ADAM (f32, on chip): g = g c; m = b1 m + (1 - b1) g; v = b2 v + (1 - b2) g^2;
8236note w -= lrb m / (sqrt(v ibc2) + 1e-8), the f32 image of og.om oadam1/oadam2 with lrb = lr/bc1 and
8237note ibc2 = 1/bc2 from the host. Args P w m v g c lrb ibc2 (slots 0, 2-9, 10, 11, 12).
8238fn gradam to void
8239grpro
8240b1 = 0.9f64
8241if eq OMUTH 0
8242b1 = 0.8f64
8243end
8244gkmovi 24 grf b1
8245gkmovi 25 grf sub 1.0f64 0.9
8246gkmovi 27 grf 0.99f64
8247gkmovi 28 grf sub 1.0f64 0.99
8248gkmovi 29 grf 0.00000001f64
8249grptr 10 1 4 4
8250grptr 12 3 4 4
8251grptr 14 5 4 4
8252grptr 16 7 4 4
8253gkldc 64 grarg 10 false
8254gkldc 65 grarg 11 true
8255gkldc 66 grarg 12 true
8256gkldg 32 16 0 true
8257gkldg 33 12 0 true
8258gkldg 34 14 0 true
8259gkldg 35 10 0 true
8260gkfmulf 32 32 64
8261gkfmulf 33 33 24
8262gkffmaf 33 32 25 33
8263gkfmulf 36 32 32
8264gkfmulf 34 34 27
8265gkffmaf 34 36 28 34
8266gkfmulf 37 34 66
8267grmufu 38 37 8192
8268grfadd 38 38 29
8269grmufu 39 38 4096
8270gkfmulf 40 33 65
8271gkfmulf 40 40 39
8272grfsub 35 35 40
8273gkstg 10 35 0 false
8274gkstg 12 33 0 true
8275gkstg 14 34 0 true
8276gkexit
8277end
8278note emit slots 9..24 (12, 17 and 18 empty) for dims V T D H F B; false when a kernel overflows its 16 KiB slot
8279fn grset v ulong t ulong d ulong h ulong f ulong b ulong to void
8280put grd 0 v
8281put grd 1 t
8282put grd 2 d
8283put grd 3 h
8284put grd 4 f
8285put grd 5 b
8286end
8287fn grall to bool
8288note the warp-per-row RMS kernels hold D/32 columns per lane (D <= 384: four banks end at R119, grrb),
8289note softmax T/32, and CE steps 256 columns
8290if or ne rem get grd 2 32 0 ne rem get grd 1 32 0
8291ret false
8292end
8293if gt get grd 2 384
8294ret false
8295end
8296if ne rem get grd 0 256 0
8297ret false
8298end
8299for k uint 16
8300id = add k 9
8301gkslot id
8302if eq id 9
8303gremb
8304end
8305if eq id 10
8306grrmsf
8307end
8308if eq id 11
8309grrmsb
8310end
8311if eq id 13
8312grcol false
8313end
8314if eq id 14
8315grcol true
8316end
8317if eq id 15
8318grsoftf
8319end
8320if eq id 16
8321grsoftb
8322end
8323if and ge id 19 le id 20
8324grew id
8325end
8326if eq id 21
8327grce
8328end
8329if eq id 22
8330grcsr
8331end
8332if eq id 23
8333grsqn
8334end
8335if eq id 24
8336gradam
8337end
8338if gt get gkat 0 mul ulong add id 1 16384
8339ret false
8340end
8341put gkthr id 64
8342put gkgpr id 72
8343if and gt get grd 2 256 or eq id 10 eq id 11
8344put gkgpr id 128
8345end
8346put gksm id 0
8347end
8348ret true
8349end
8350note open the chip with the row slots 9..24 for the given dims, then gkopen's GEMM slots, arena asz bytes
8351fn gropen asz ulong to bool
8352if not grall
8353put nverr 0 neg long 7
8354ret false
8355end
8356ret gkopen asz
8357end
8358note SPDX-License-Identifier: AGPL-3.0-only
8359note gb.om: ollm BF16 tensor-core path, commands and gates (docs/omegallm/CONTRACT-BF16.md). With gkbf 1,
8360note gk.om runs every 64x64 gkmm on kernels 27..36 (HMMA.16816.F32.BF16; the kernel rounds both operands
8361note to bf16 RN with F2FP and accumulates in f32). Commands: gtrainb and gprofb (gtrainf and gprof with
8362note gkbf 1), gbcmp (sampled check of every BF16 GEMM of one step, exit 48), gbcmp2 (gcmp2's step parity
8363note with the BF16 bound, exit 49) and gbfrag (exact check of all ten kernels on fixed data, exit 50).
8364note Concatenated before gg.om: it calls gg.om functions but uses none of gg.om's globals.
8365
8366note OMUTU: 1 in the product; 0 in mutant u, whose reference skips the bf16 rounding (gbfrag must then
8367note fail; gbcmp only reports it, CONTRACT-BF16 section 5).
8368const OMUTU ulong 1
8369
8370note OMUTN: 1 in the product; 0 in mutant n, the negative control of gbfrag's padding check: after each
8371note short case's launch the host writes 0.0 into C's first padding word (block 0, row 0, column n), so
8372note only that check can fail (CONTRACT-BF16 section 4.1).
8373const OMUTN ulong 1
8374
8375note bf16 round to nearest even of the f32 bits u, as f32 bits (NaN unchanged)
8376fn gbbf u uint to uint
8377if eq OMUTU 0
8378ret u
8379end
8380if and eq and shr u 23 255 255 ne and u 8388607 0
8381ret u
8382end
8383v = add add u 32767 and shr u 16 1
8384ret and v 4294901760
8385end
8386
8387note f32 bits x rounded to bf16, as f64
8388fn gbrv x uint to f64
8389ret f64 bitcast f32 gbbf x
8390end
8391
8392fn gbabs x f64 to f64
8393if lt x 0.0f64
8394ret neg x
8395end
8396ret x
8397end
8398
8399note arena word at host byte offset o (offset from gkbase)
8400fn gbw o ulong to uint
8401ret uint mapget get gbmem 5 o 4
8402end
8403
8404fn gbput o ulong x uint to void
8405m = mapset get gbmem 5 o 4 ulong x
8406end
8407
8408note host byte offset of the device address held in arg words w, w + 1
8409fn gbhp w uint to ulong
8410lo = ulong get gkarg w
8411hi = ulong get gkarg add w 1
8412ret sub or lo shl hi 32 gkbase
8413end
8414
8415note batch offset z1 s1 + z2 s2 (z = z1 h2 + z2) with s1, s2 in arg words w, w + 1
8416fn gbzo z uint h2 uint w uint to ulong
8417a = mul ulong div z h2 ulong get gkarg w
8418ret add a mul ulong rem z h2 ulong get gkarg add w 1
8419end
8420
8421note verdict line GBCMP (c 48), GBCMP2 (49) or GBFRAG (50): PASS, or FAIL and exit c
8422fn gbvd c ulong ok bool to void
8423oputc 71
8424oputc 66
8425if eq c 50
8426oputc 70
8427oputc 82
8428oputc 65
8429oputc 71
8430else
8431oputc 67
8432oputc 77
8433oputc 80
8434if eq c 49
8435oputc 50
8436end
8437end
8438oputc 32
8439if ok
8440oputc 80
8441oputc 65
8442oputc 83
8443oputc 83
8444oputc 10
8445oflush
8446ret
8447end
8448oputc 70
8449oputc 65
8450oputc 73
8451oputc 76
8452oputc 10
8453oflush
8454if eq c 48
8455exit 48
8456end
8457if eq c 49
8458exit 49
8459end
8460exit 50
8461end
8462
8463note gbcmp state. gbst: 0 BF16 GEMMs checked, 1 samples past their bound, 2 kernel id mask, 3 cz mask,
8464note 4 ep mask, 5 beta mask, 6 the sample generator. gbmx[0]: the worst |chip - ref| / bound. gbsp: the
8465note 66 sampled (z, r, c) of the launch in flight, gbold the old C there (beta), gbacc the reference sums.
8466global gbst ulong 8
8467global gbmx f64 1
8468global gbsp uint 198
8469global gbold f64 66
8470global gbacc f64 3
8471
8472note next value in [0, n) of the sample generator (31-bit LCG)
8473fn gbrnd n uint to uint
8474s = rem add mul get gbst 6 1103515245 12345 2147483648
8475put gbst 6 s
8476ret uint rem shr s 8 ulong n
8477end
8478
8479note host byte offset at sample i in C (w 16) or in the epilogue's Y / M (w 30, C's strides)
8480fn gbco i ulong w uint to ulong
8481o = mul i 3
8482z = get gbsp o
8483b = add gbhp w gbzo z get gkbp 4 20
8484b = add b mul ulong get gbsp add o 1 ulong get gkarg 18
8485ret add b mul ulong get gbsp add o 2 ulong get gkarg 19
8486end
8487
8488note gkmm's hook before a checked BF16 launch (gkbp = m n k nz h2 beta ep cz): 64 random (z, r, c)
8489note and the corners (0, 0, 0), (nz - 1, m - 1, n - 1); with cz 3 a position in a block above the
8490note diagonal (not computed) is mirrored below it. Keeps the old C when beta.
8491fn gbpre to void
8492for i ulong 66
8493gbpre1 i
8494end
8495end
8496
8497note one sample i of gbpre
8498fn gbpre1 i ulong to void
8499z = gbrnd get gkbp 3
8500r = gbrnd get gkbp 0
8501c = gbrnd get gkbp 1
8502if eq i 64
8503z = 0
8504r = 0
8505c = 0
8506end
8507if eq i 65
8508z = sub get gkbp 3 1
8509r = sub get gkbp 0 1
8510c = sub get gkbp 1 1
8511end
8512if eq get gkbp 7 3
8513if gt div c 64 div r 64
8514t = r
8515r = c
8516c = t
8517end
8518end
8519o = mul i 3
8520put gbsp o z
8521put gbsp add o 1 r
8522put gbsp add o 2 c
8523put gbold i 0.0f64
8524if eq get gkbp 5 1
8525put gbold i f64 bitcast f32 gbw gbco i 16
8526end
8527end
8528
8529note reference sums of sample i over the K range the kernel reads (cz 1: k < (r / 64 + 1) 64,
8530note cz 2: k >= (r / 64) 64): gbacc 0 sum a b, 1 sum |a b|, 2 the term count; a, b rounded to bf16
8531fn gbref i ulong to void
8532r = ulong get gbsp add mul i 3 1
8533k = ulong get gkbp 2
8534lo = ulong 0
8535hi = mul add div r 64 1 64
8536cz = get gkbp 7
8537if eq cz 1
8538if lt hi k
8539k = hi
8540end
8541end
8542if eq cz 2
8543lo = mul div r 64 64
8544end
8545gbdot gbsa i 0 gbsa i 1 lo sub k lo
8546end
8547
8548note host address of sample i's A row (w 0) or B column (w 1) at k = 0
8549fn gbsa i ulong w uint to ulong
8550o = mul i 3
8551z = get gbsp o
8552h2 = get gkbp 4
8553g = mul w 8
8554x = ulong get gbsp add o ulong add w 1
8555p = add gbhp g gbzo z h2 add g 4
8556ret add p mul x ulong get gkarg add g 2
8557end
8558
8559fn gbdot pa ulong pb ulong lo ulong kn ulong to void
8560ka = ulong get gkarg 3
8561kb = ulong get gkarg 11
8562s = 0.0f64
8563sa = 0.0f64
8564for j ulong kn
8565kk = add lo j
8566p = mul gbrv gbw add pa mul kk ka gbrv gbw add pb mul kk kb
8567s = add s p
8568sa = add sa gbabs p
8569end
8570put gbacc 0 s
8571put gbacc 1 sa
8572put gbacc 2 f64 kn
8573end
8574
8575note d / b, 0 when both are 0; 1e9 when d > b with b = 0 or the ratio non-finite or past 1e9
8576fn gbrat d f64 b f64 to f64
8577if le d b
8578if gt b 0.0f64
8579ret div d b
8580end
8581ret 0.0f64
8582end
8583if gt b 0.0f64
8584q = div d b
8585if lt q 1000000000.0f64
8586ret q
8587end
8588end
8589ret 1000000000.0f64
8590end
8591
8592note |chip - ref| / bound at sample i, bound (kn 2^-23 + 2^-22)(|alpha| sum |a b| + |old C|); 1e9
8593note for a wrong epilogue (ep 2: C must be +0 where M is not > 0; ep 1: Y = C when C > 0, else +0)
8594fn gbchk i ulong to f64
8595co = gbco i 16
8596cb = gbw co
8597ep = get gkbp 6
8598if eq ep 2
8599if not gt f64 bitcast f32 gbw gbco i 30 0.0f64
8600if eq cb 0
8601ret 0.0f64
8602end
8603ret 1000000000.0f64
8604end
8605end
8606if eq ep 1
8607w = uint 0
8608if gt f64 bitcast f32 cb 0.0f64
8609w = cb
8610end
8611if ne gbw gbco i 30 w
8612ret 1000000000.0f64
8613end
8614end
8615gbref i
8616al = f64 bitcast f32 get gkarg 25
8617old = get gbold i
8618d = gbabs sub f64 bitcast f32 cb add mul al get gbacc 0 old
8619e = add div get gbacc 2 8388608.0f64 div 1.0f64 4194304.0f64
8620ret gbrat d mul e add mul gbabs al get gbacc 1 gbabs old
8621end
8622
8623note kernel id of the launch in flight (gk.om gkmmb)
8624fn gbid to ulong
8625ep = ulong get gkbp 6
8626if ne ep 0
8627ret add 34 ep
8628end
8629id = add 27 ulong get gkmd 0
8630id = add id mul 2 ulong get gkmd 1
8631ret add id mul 4 ulong get gkbp 5
8632end
8633
8634note gkmm's hook after a checked BF16 launch: check the samples, print
8635note `GB id m n k nz h2 beta ep cz worst_e6 bad`; true (the step goes on, gbcmp gives the verdict)
8636fn gbpost to bool
8637bad = ulong 0
8638mr = 0.0f64
8639for i ulong 66
8640q = gbchk i
8641if gt q 1.0f64
8642bad = add bad 1
8643end
8644if gt q mr
8645mr = q
8646end
8647end
8648gbnote bad mr
8649ret true
8650end
8651
8652fn gbnote bad ulong mr f64 to void
8653id = gbid
8654put gbst 0 add get gbst 0 1
8655put gbst 1 add get gbst 1 bad
8656put gbst 2 or get gbst 2 shl ulong 1 id
8657put gbst 3 or get gbst 3 shl ulong 1 ulong get gkbp 7
8658put gbst 4 or get gbst 4 shl ulong 1 ulong get gkbp 6
8659put gbst 5 or get gbst 5 shl ulong 1 ulong get gkbp 5
8660if gt mr get gbmx 0
8661put gbmx 0 mr
8662end
8663oputc 71
8664oputc 66
8665oputc 32
8666oputu id
8667for j ulong 8
8668oputc 32
8669oputu ulong get gkbp j
8670end
8671oputc 32
8672oputl rmic mr
8673oputc 32
8674oputu bad
8675oputc 10
8676end
8677
8678note gbcmp CK DATA S: one gtrainf step's gradient pass (gggrad, no Adam) with gkbf 1 and gkbck 1, so
8679note gk.om checks every BF16 launch (gbpre, gbpost). Prints the GB lines, `GBC gemms bad worst_e6
8680note idmask czmask epmask betamask` and GBCMP PASS, or GBCMP FAIL (exit 48): a sample past its bound,
8681note no BF16 GEMM, or coverage short of the step's BF16 combinations (epilogues 1 and 2, cz 3, beta
8682note false and true).
8683fn gbcmp to void
8684if ne oargc 5
8685exit 2
8686end
8687ockload 2
8688odopen 3
8689obatch oargu 4
8690put og 10 0
8691put gbst 6 12345
8692put gkbf 0 1
8693put gkbck 0 1
8694ggopen
8695l = gggrad
8696put gkbck 0 0
8697r = nvstop
8698oputc 71
8699oputc 66
8700oputc 67
8701for j ulong 2
8702oputc 32
8703oputu get gbst j
8704end
8705oputc 32
8706oputl rmic get gbmx 0
8707for j ulong 4
8708oputc 32
8709oputu get gbst add j 2
8710end
8711oputc 10
8712ok = and eq get gbst 1 0 gt get gbst 0 0
8713cv = eq and get gbst 4 6 6
8714cv = and cv ne and get gbst 3 8 0
8715cv = and cv eq get gbst 5 3
8716gbvd 48 and ok cv
8717end
8718
8719note gbcmp2 bound: tolb = n_c (2u + u^2), u = 2^-8, n_c = 10 L + 3 BF16 GEMMs on a step's longest
8720note dependency chain (per layer forward QKV, S, WO, W1, W2; backward dF, dA of W1, dN, DP, dA of QKV;
8721note plus the logits, dlogits to dH and the last weight gradient); a first-order normwise heuristic
8722fn gbtolb to f64
8723nc = f64 add mul 10 get og 5 3
8724ret mul nc add div 1.0f64 128.0f64 div 1.0f64 65536.0f64
8725end
8726
8727note gcmp2's fail bits under gkbf 1: 1 non-finite, 2 rho >= tolb, 4 rel1 >= tolb, 8 |dloss| >= tolb loss
8728fn gbc2fl lg f64 l0 f64 rho f64 nf ulong to ulong
8729tb = gbtolb
8730fl = ulong 0
8731if ne nf 0
8732fl = or fl 1
8733end
8734if not lt ggclp rho tb
8735fl = or fl 2
8736end
8737if not lt gg2rl 2 tb
8738fl = or fl 4
8739end
8740if not lt gbabs sub lg l0 mul tb gbabs l0
8741fl = or fl 8
8742end
8743ret fl
8744end
8745
8746note cosine of the chip gradient (omv[0, p)) and K0 (omv[p, 2p))
8747global gbcq f64 3
8748fn gbcos p ulong to f64
8749for k ulong 3
8750put gbcq k 0.0f64
8751end
8752for q ulong ocn p
8753i0 = mul q OCH
8754for j ulong sub oce q p i0
8755gbcq1 add i0 j p
8756end
8757end
8758d = fsqrt mul get gbcq 1 get gbcq 2
8759if gt d 0.0f64
8760ret ggclp div get gbcq 0 d
8761end
8762ret 0.0f64
8763end
8764
8765fn gbcq1 i ulong p ulong to void
8766x = omvg i
8767y = omvg add p i
8768put gbcq 0 add get gbcq 0 mul x y
8769put gbcq 1 add get gbcq 1 mul x x
8770put gbcq 2 add get gbcq 2 mul y y
8771end
8772
8773note gbcmp2's line `GB2 L n_c tolb_e9 cos_e9 rel0_e9 rel1_e9 dlossrel_e9` and verdict (exit 49)
8774fn gbc2vd lg f64 l0 f64 fl ulong to void
8775cs = gbcos get og 8
8776oputc 71
8777oputc 66
8778oputc 50
8779oputc 32
8780oputu get og 5
8781oputc 32
8782oputu add mul 10 get og 5 3
8783oputc 32
8784oputl rmic mul gbtolb 1000.0f64
8785oputc 32
8786oputl rmic mul cs 1000.0f64
8787oputc 32
8788oputl rmic mul gg2rl 0 1000.0f64
8789oputc 32
8790oputl rmic mul gg2rl 2 1000.0f64
8791oputc 32
8792oputl rmic mul ggclp div gbabs sub lg l0 gbabs l0 1000.0f64
8793oputc 10
8794gbvd 49 eq fl 0
8795end
8796
8797note gbcmp2 CK DATA S: gcmp2 with gkbf 1 (gg.om ggc2fl and ggc2pr branch to gbc2fl and gbc2vd)
8798fn gbcmp2 to void
8799put gkbf 0 1
8800ggcmp2
8801end
8802
8803note gtrainb: gtrainf with gkbf 1 (geval stays f32)
8804fn gbtrain to void
8805put gkbf 0 1
8806ggtrainf
8807end
8808
8809note gprofb: gprof with gkbf 1
8810fn gbprof to void
8811put gkbf 0 1
8812ggprof
8813end
8814
8815note gbfrag CK: every BF16 kernel (ids 27..36) on fixed data with exact f32 sums, compared element by
8816note element against a reference built from the bf16-rounded operands. One operand holds integers in
8817note [-5, 5], the other values (n - 2) + j 2^-9 (n < 5, j < 64), so every product is a multiple of 2^-9
8818note (2^-10 with alpha 1/2) and every sum stays below 2^11: the chip's f32 result is exact. Thirteen
8819note cases (eight plain kernels, epilogues 1 and 2, cz 1, 2 and 3), each with the operand roles
8820note swapped; nz 6, h2 3, padded strides, NaN in every operand word the kernel must not read and in the
8821note eight padding columns of every C row (and of Y / M with a fused epilogue), which must still be NaN
8822note afterwards. Data in the logits buffer (A, B, C, Y at 0, 1, 2, 3 MiB). Prints `GF pass case id k cz ep bad` per case,
8823note then the eight long-K cases (gbfl, `GFL pass ma mb bad`), `GFS bad` and GBFRAG PASS, or GBFRAG FAIL
8824note (exit 50). Needs a logits buffer of at least 4 Mi floats (B 8: 8 Mi).
8825note gbcs: 0 ma, 1 mb (1 = K-contiguous), 2 beta, 3 ep, 4 cz, 5 k, 6 m, 7 n, 8 pass, 9 alpha 1/2
8826global gbcs uint 10
8827global gbfb ulong 1
8828global gbha f64 98304
8829global gbhb f64 147456
8830
8831fn gbfrag to void
8832if ne oargc 3
8833exit 2
8834end
8835ockload 2
8836ggopen
8837if lt mul ggr get og 0 4194304
8838ggdie 3
8839end
8840put gkbf 0 1
8841put gkbck 0 0
8842put gbfb 0 sub gga1 11 gkbase
8843bad = ulong 0
8844for p ulong 2
8845for i ulong 13
8846bad = add bad gbf1 i p
8847end
8848end
8849for p ulong 2
8850for q uint 4
8851bad = add bad gbfl p and q 1 shr q 1
8852end
8853end
8854r = nvstop
8855oputc 71
8856oputc 70
8857oputc 83
8858oputc 32
8859oputu bad
8860oputc 10
8861gbvd 50 eq bad 0
8862end
8863
8864note case i of pass p into gbcs
8865fn gbcase i ulong p ulong to void
8866for j ulong 10
8867put gbcs j 0
8868end
8869put gbcs 8 uint p
8870put gbcs 6 128
8871put gbcs 7 192
8872put gbcs 5 128
8873if eq rem i 3 0
8874put gbcs 5 32
8875end
8876if eq rem i 3 1
8877put gbcs 5 96
8878put gbcs 9 1
8879end
8880if lt i 8
8881put gbcs 0 uint and i 1
8882put gbcs 1 uint and shr i 1 1
8883put gbcs 2 uint and shr i 2 1
8884ret
8885end
8886put gbcs 9 0
8887put gbcs 0 1
8888if eq i 8
8889put gbcs 3 1
8890put gbcs 5 96
8891end
8892if eq i 9
8893put gbcs 1 1
8894put gbcs 3 2
8895end
8896note cz 1 needs k = m
8897if eq i 10
8898put gbcs 4 1
8899put gbcs 5 128
8900end
8901if eq i 11
8902put gbcs 0 0
8903put gbcs 2 1
8904put gbcs 4 2
8905end
8906if eq i 12
8907put gbcs 1 1
8908put gbcs 4 3
8909put gbcs 5 32
8910put gbcs 7 128
8911put gbcs 9 1
8912end
8913end
8914
8915note one case: write the data, launch, compare; the mismatch count
8916fn gbf1 i ulong p ulong to ulong
8917gbcase i p
8918gbnan
8919gbwop 0
8920gbwop 1
8921gbwc
8922put gkinv 0 1
8923if not gbflch
8924ggdie 98
8925end
8926if eq OMUTN 0
8927n = ulong get gbcs 7
8928gbput add gbcro 0 0 mul ulong get gbcs 6 add n 8 mul n 4 0
8929end
8930b = gbfcmp
8931ep = ulong get gbcs 3
8932id = add 27 ulong get gbcs 0
8933id = add id mul 2 ulong get gbcs 1
8934id = add id mul 4 ulong get gbcs 2
8935if ne ep 0
8936id = add 34 ep
8937end
8938oputc 71
8939oputc 70
8940oputc 32
8941oputu p
8942oputc 32
8943oputu i
8944oputc 32
8945oputu id
8946oputc 32
8947oputu ulong get gbcs 5
8948oputc 32
8949oputu ulong get gbcs 4
8950oputc 32
8951oputu ep
8952oputc 32
8953oputu b
8954oputc 10
8955ret b
8956end
8957
8958note NaN in every A and B word (2 MiB)
8959fn gbnan to void
8960b = get gbfb 0
8961for i ulong 524288
8962gbput add b mul i 4 2143289344
8963end
8964end
8965
8966note operand layout in floats: mode 1 rows of k + 8, mode 0 rows of w + 16 (w = m for A, n for B);
8967note batch z at (z / 3)(3 zs + 64) + (z % 3) zs
8968fn gbzs md uint w ulong kd ulong to ulong
8969if eq md 1
8970ret mul w add kd 8
8971end
8972ret mul kd add w 16
8973end
8974
8975fn gbzof z ulong zs ulong to ulong
8976ret add mul div z 3 add mul zs 3 64 mul rem z 3 zs
8977end
8978
8979fn gbopf md uint x ulong kk ulong w ulong kd ulong to ulong
8980if eq md 1
8981ret add mul x add kd 8 kk
8982end
8983ret add x mul kk add w 16
8984end
8985
8986note the fractional value (n - 2) + j 2^-9 and the integer value at (x, kk, z)
8987fn gbfr x ulong kk ulong z ulong to f64
8988s = add mul x 7 mul kk 3
8989s = add s mul z 5
8990a = sub f64 rem s 5 2.0f64
8991s = add mul x 13 mul kk 11
8992s = add s mul z 3
8993b = f64 rem s 64
8994ret add a div b 512.0f64
8995end
8996
8997fn gbint x ulong kk ulong z ulong to f64
8998s = add mul x 5 mul kk 7
8999s = add s mul z 3
9000ret sub f64 rem s 11 5.0f64
9001end
9002
9003note write operand ab (0 A, 1 B) for z < 6, x < m (A) or n (B), kk < k; keep the bf16-rounded
9004note values in gbha / gbhb (index (z rows + x) 128 + kk). Pass 0: A fractional, B integer; pass 1 swapped.
9005fn gbwop ab uint to void
9006md = get gbcs ab
9007k = ulong get gbcs 5
9008w = ulong get gbcs add 6 ab
9009zs = gbzs md w k
9010base = add get gbfb 0 mul ulong ab 1048576
9011fr = eq get gbcs 8 ab
9012for z ulong 6
9013for x ulong w
9014gbwrow ab z x base zs fr
9015end
9016end
9017end
9018
9019fn gbwrow ab uint z ulong x ulong base ulong zs ulong fr bool to void
9020zb = add base mul gbzof z zs 4
9021t = add 128 mul 64 ulong ab
9022hi = mul add mul z t x 128
9023k = ulong get gbcs 5
9024for kk ulong k
9025gbwel ab zb x kk gbwv x kk z fr hi
9026end
9027end
9028
9029note operand value at (x, kk, z) as f32 bits: fractional when fr, else integer
9030fn gbwv x ulong kk ulong z ulong fr bool to uint
9031v = gbint x kk z
9032if fr
9033v = gbfr x kk z
9034end
9035ret bitcast uint f32 v
9036end
9037
9038note store bits u of operand ab at (x, kk) of the z block at zb; keep its bf16 value at gbha / gbhb hi + kk
9039fn gbwel ab uint zb ulong x ulong kk ulong u uint hi ulong to void
9040md = get gbcs ab
9041w = ulong get gbcs add 6 ab
9042o = gbopf md x kk w ulong get gbcs 5
9043gbput add zb mul o 4 u
9044if eq ab 0
9045put gbha add hi kk gbrv u
9046else
9047put gbhb add hi kk gbrv u
9048end
9049end
9050
9051note old C ((3 r + 5 c + z) mod 17) - 8, and the epilogue-2 mask M: NaN when (5 r + c) mod 13 = 0,
9052note else ((r + 3 c + z) mod 3) - 1, as f32 bits
9053fn gbold3 z ulong r ulong c ulong to f64
9054s = add mul r 3 mul c 5
9055s = add s z
9056ret sub f64 rem s 17 8.0f64
9057end
9058
9059fn gbmv z ulong r ulong c ulong to uint
9060if eq rem add mul r 5 c 13 0
9061ret 2143289344
9062end
9063ret bitcast uint f32 sub f64 rem add add r mul c 3 z 3 1.0f64
9064end
9065
9066note C (old values) and Y: 77 for epilogue 1, M for epilogue 2; C rows of n + 8, whose 8 padding columns
9067note (and Y's / M's with a fused epilogue) are NaN
9068fn gbwc to void
9069m = ulong get gbcs 6
9070zs = mul m add ulong get gbcs 7 8
9071for z ulong 6
9072for r ulong m
9073gbwcr z r zs
9074end
9075end
9076end
9077
9078fn gbwcr z ulong r ulong zs ulong to void
9079n = ulong get gbcs 7
9080ep = get gbcs 3
9081o = gbcro z r zs
9082for c ulong n
9083oc = add o mul c 4
9084gbput oc bitcast uint f32 gbold3 z r c
9085if eq ep 1
9086gbput add oc 1048576 bitcast uint f32 77.0f64
9087end
9088if eq ep 2
9089gbput add oc 1048576 gbmv z r c
9090end
9091end
9092for c ulong 8
9093op = add o mul add n c 4
9094gbput op 2143289344
9095if ne ep 0
9096gbput add op 1048576 2143289344
9097end
9098end
9099end
9100
9101note operand ab's gkopa for the case at device address p
9102fn gbfopa ab uint p ulong to void
9103md = get gbcs ab
9104k = get gbcs 5
9105w = get gbcs add 6 ab
9106zs = uint gbzs md ulong w ulong k
9107so = mul add k 8 4
9108sk = uint 4
9109if eq md 0
9110so = 4
9111sk = mul add w 16 4
9112end
9113gkopa ab p so sk mul add mul zs 3 64 4 mul zs 4
9114end
9115
9116fn gbflch to bool
9117d = add gkbase get gbfb 0
9118gbfopa 0 d
9119gbfopa 1 add d 1048576
9120n = get gbcs 7
9121zc = mul get gbcs 6 add n 8
9122al = 1.0f64
9123if eq get gbcs 9 1
9124al = 0.5f64
9125end
9126sm = mul add n 8 4
9127s1 = mul add mul zc 3 64 4
9128s2 = mul zc 4
9129gkopc add d 2097152 sm 4 s1 s2 f32 al
9130if ne get gbcs 3 0
9131gkope get gbcs 3 add d 3145728
9132end
9133if ne get gbcs 4 0
9134gkopz get gbcs 4
9135end
9136m = get gbcs 6
9137k = get gbcs 5
9138bt = eq get gbcs 2 1
9139ret gkmm m n k 6 3 bt
9140end
9141
9142note mismatches over every C element of the case (and Y for epilogue 1), plus padding words no longer NaN
9143fn gbfcmp to ulong
9144bad = ulong 0
9145for z ulong 6
9146for r ulong ulong get gbcs 6
9147bad = add bad gbfrow z r
9148end
9149end
9150ret bad
9151end
9152
9153note host byte offset of C row r of block z (row stride n + 8 f32)
9154fn gbcro z ulong r ulong zs ulong to ulong
9155n = ulong get gbcs 7
9156b = add get gbfb 0 2097152
9157b = add b mul gbzof z zs 4
9158ret add b mul mul r add n 8 4
9159end
9160
9161fn gbfrow z ulong r ulong to ulong
9162n = ulong get gbcs 7
9163zs = mul ulong get gbcs 6 add n 8
9164o = gbcro z r zs
9165bad = ulong 0
9166for c ulong n
9167if not gbfel z r c add o mul c 4
9168bad = add bad 1
9169end
9170end
9171for c ulong 8
9172op = add o mul add n c 4
9173if ne gbw op 2143289344
9174bad = add bad 1
9175end
9176if ne get gbcs 3 0
9177if ne gbw add op 1048576 2143289344
9178bad = add bad 1
9179end
9180end
9181end
9182ret bad
9183end
9184
9185note element (z, r, c) at host offset oc: cz 3 blocks above the diagonal keep the old C; epilogue 2
9186note gives +0 where M is not > 0; else C equals the exact reference; epilogue 1 Y = C when C > 0, else +0
9187fn gbfel z ulong r ulong c ulong oc ulong to bool
9188cb = gbw oc
9189old = gbold3 z r c
9190if and eq get gbcs 4 3 gt div c 64 div r 64
9191ret eq cb bitcast uint f32 old
9192end
9193ep = get gbcs 3
9194if eq ep 2
9195if not gt f64 bitcast f32 gbmv z r c 0.0f64
9196ret eq cb 0
9197end
9198end
9199s = gbfref z r c
9200if eq get gbcs 2 1
9201s = add s old
9202end
9203if ne f64 bitcast f32 cb s
9204ret false
9205end
9206if eq ep 1
9207w = uint 0
9208if gt s 0.0f64
9209w = cb
9210end
9211ret eq gbw add oc 1048576 w
9212end
9213ret true
9214end
9215
9216note exact reference alpha sum a b over the K range the kernel reads (bf16-rounded operands)
9217fn gbfref z ulong r ulong c ulong to f64
9218k = ulong get gbcs 5
9219lo = ulong 0
9220hi = mul add div r 64 1 64
9221cz = get gbcs 4
9222if and eq cz 1 lt hi k
9223k = hi
9224end
9225if eq cz 2
9226lo = mul div r 64 64
9227end
9228ia = mul add mul z 128 r 128
9229s = gbfdot ia mul add mul z 192 c 128 lo sub k lo
9230if eq get gbcs 9 1
9231s = mul s 0.5f64
9232end
9233ret s
9234end
9235
9236fn gbfdot ia ulong ib ulong lo ulong kn ulong to f64
9237s = 0.0f64
9238for j ulong kn
9239kk = add lo j
9240a = get gbha add ia kk
9241s = add s mul a get gbhb add ib kk
9242end
9243ret s
9244end
9245
9246note long-K case of pass p, operand modes ma, mb: m = n = 64, k = 16384 (512 K tiles), nz 1, beta 0,
9247note alpha 1. Operands are zero except kk = 0 mod 29 (565 terms, at least one in every 32-wide K tile),
9248note so a dropped, repeated or misplaced K tile changes C; sums stay below 565 5 17/8 < 2^13 in multiples
9249note of 2^-9, so the f32 result is exact. Logits buffer: A at 0, B at 6 MiB, C (rows of 72) at 12 MiB,
9250note NaN everywhere first; C's padding columns must stay NaN. Prints `GFL pass ma mb bad`.
9251fn gbfl p ulong ma uint mb uint to ulong
9252for j ulong 10
9253put gbcs j 0
9254end
9255put gbcs 0 ma
9256put gbcs 1 mb
9257put gbcs 5 16384
9258put gbcs 6 64
9259put gbcs 7 64
9260put gbcs 8 uint p
9261b = get gbfb 0
9262for i ulong 3162112
9263gbput add b mul i 4 2143289344
9264end
9265gblw 0
9266gblw 1
9267put gkinv 0 1
9268if not gbllch
9269ggdie 98
9270end
9271bad = ulong 0
9272for r ulong 64
9273bad = add bad gblrow r
9274end
9275oputc 71
9276oputc 70
9277oputc 76
9278oputc 32
9279oputu p
9280oputc 32
9281oputu ulong ma
9282oputc 32
9283oputu ulong mb
9284oputc 32
9285oputu bad
9286oputc 10
9287ret bad
9288end
9289
9290note operand ab of the long case: nonzero words only at kk = 0 mod 29
9291fn gblw ab uint to void
9292md = get gbcs ab
9293base = add get gbfb 0 mul ulong ab 6291456
9294fr = eq get gbcs 8 ab
9295for x ulong 64
9296gblwr md base x fr
9297end
9298end
9299
9300fn gblwr md uint base ulong x ulong fr bool to void
9301w = ulong 64
9302kd = ulong 16384
9303for kk ulong kd
9304u = uint 0
9305if eq rem kk 29 0
9306u = gbwv x kk 0 fr
9307end
9308o = gbopf md x kk w kd
9309gbput add base mul o 4 u
9310end
9311end
9312
9313note launch the long case: mode 1 rows of k + 8 floats, mode 0 K rows of 80 floats
9314fn gbllch to bool
9315d = add gkbase get gbfb 0
9316gblopa 0 d
9317gblopa 1 add d 6291456
9318gkopc add d 12582912 288 4 0 0 f32 1.0f64
9319gkopz 0
9320ret gkmm 64 64 16384 1 1 false
9321end
9322
9323fn gblopa ab uint p ulong to void
9324so = uint 65568
9325sk = uint 4
9326if eq get gbcs ab 0
9327so = 4
9328sk = 320
9329end
9330gkopa ab p so sk 0 0
9331end
9332
9333note row r of the long case: 64 exact values, then 8 padding words that must still be NaN
9334fn gblrow r ulong to ulong
9335o = add add get gbfb 0 12582912 mul r 288
9336bad = ulong 0
9337for c ulong 64
9338cb = gbw add o mul c 4
9339if ne f64 bitcast f32 cb gblref r c
9340bad = add bad 1
9341end
9342end
9343for c ulong 8
9344if ne gbw add o mul add 64 c 4 2143289344
9345bad = add bad 1
9346end
9347end
9348ret bad
9349end
9350
9351note exact reference: sum over kk = 29 j of the bf16-rounded A(r, kk) B(c, kk)
9352fn gblref r ulong c ulong to f64
9353fa = eq get gbcs 8 0
9354fb = eq get gbcs 8 1
9355s = 0.0f64
9356for j ulong 565
9357kk = mul j 29
9358a = gbrv gbwv r kk 0 fa
9359s = add s mul a gbrv gbwv c kk 0 fb
9360end
9361ret s
9362end
9363note SPDX-License-Identifier: AGPL-3.0-only
9364note gg.om: ollm GPU gradient (aien-train#52, rung 4). The step loss and its gradient for the batch in
9365note obx/oby, computed on the GB10 by gk.om GEMMs and gr.om row kernels over all R = B T rows at
9366note once in f32; the gradient lands in ogr[0..P) as f64 and the loss in olos, so oadam and olsum run
9367note unchanged on the CPU. Arena (gbmem 5) byte offsets in gga: 0 W, 1 G, 2 X, 3 Y, 4 CSR start
9368note (V + 1), 5 CSR order (R), 6 first layer, 7 layer stride, 8 HF, 9 SF, 10 Z, 11 logits (R V),
9369note 12 CE stats (R 3), 13 DH, 14 DA, 15 DQ DK DU (contiguous), 16 DO, 17 DF, 18 DP, 19 WS, 20 CS;
9370note layer fields 24 + f: 0 hin 1 a 2 q 3 k 4 u 5 o 6 hm 7 bn 8 s1 9 s2 10 fp 11 rl 12 P;
9371note 40 the arena device address, 47 the plan cursor. s1, s2 and SF hold 1/s (the CPU keeps s).
9372global gga ulong 48
9373global ggcn ulong 65537
9374note byte strides: 0 D, 1 T D, 2 E, 3 T, 4 T T, 5 H T T, 6 R D, 7 D D (each times 4)
9375global ggs uint 8
9376note OMUTG: 1 in the product; 0 in mutant g (DK = -DS^T Q), which the parity check must catch.
9377const OMUTG ulong 1
9378
9379fn ggdie c ulong to void
9380oputc 71
9381oputc 88
9382oputc 32
9383oputu c
9384oputc 32
9385oputl get nverr 0
9386oputc 32
9387oputu ulong get nvstage 0
9388note diagnostics: launches done, mark1, mark2, QMD semaphore
9389oputc 32
9390oputl get gkt 0
9391oputc 32
9392oputu mapget get gbmem 3 0 4
9393oputc 32
9394oputu mapget get gbmem 3 16 4
9395oputc 32
9396oputu mapget get gbmem 2 8192 4
9397oputc 10
9398oflush
9399exit 84
9400end
9401
9402fn ggcd n ulong k ulong to ulong
9403ret div add n sub k 1 k
9404end
9405
9406fn ggr to ulong
9407ret mul get og 6 get og 1
9408end
9409
9410note P rounded up to the SQN chunk of 512; the gradient buffer is that long and zeroed to it
9411fn ggp5 to ulong
9412ret mul ggcd get og 8 512 512
9413end
9414
9415fn ggput i ulong n ulong to void
9416c = get gga 47
9417put gga i c
9418b = mul div add mul n 4 255 256 256
9419put gga 47 add c b
9420end
9421
9422note arena plan for the dims in og; returns its size in bytes
9423fn ggplan to ulong
9424tt = get og 1
9425d = get og 2
9426h = get og 3
9427f = get og 4
9428bb = get og 6
9429r = ggr
9430rd = mul r d
9431pp = mul mul bb h mul tt tt
9432put gga 47 0
9433for i ulong 8
9434ggput add 24 i rd
9435end
9436ggput 32 r
9437ggput 33 r
9438ggput 34 mul r f
9439ggput 35 mul r f
9440ggput 36 pp
9441put gga 7 get gga 47
9442put gga 47 0
9443ggput 0 get og 8
9444ggput 1 ggp5
9445ggput 2 r
9446ggput 3 r
9447ggput 4 add get og 0 1
9448ggput 5 r
9449put gga 6 get gga 47
9450ls = mul get gga 7 get og 5
9451put gga 47 add get gga 47 ls
9452ggput 8 rd
9453ggput 9 r
9454ggput 10 rd
9455ggput 11 mul r get og 0
9456ggput 12 mul r 3
9457ggput 13 rd
9458ggput 14 rd
9459ggput 15 mul rd 3
9460ggput 16 rd
9461ggput 17 mul r f
9462ggput 18 pp
9463ggput 19 rd
9464ggput 20 mul bb d
9465ggput 21 get og 8
9466ggput 22 get og 8
9467ggput 23 div ggp5 512
9468note 37: split-K partials for ggsk, at most 2^21 floats (ggks stops doubling once a grid has 1024 blocks)
9469ggput 37 2097152
9470ret get gga 47
9471end
9472
9473fn gga1 i ulong to ulong
9474ret add get gga 40 get gga i
9475end
9476
9477fn ggl l ulong f ulong to ulong
9478o = add get gga 6 mul l get gga 7
9479ret add add get gga 40 o get gga add 24 f
9480end
9481
9482note address of the output of layer l (the next hin, or HF after the last layer)
9483fn ggout l ulong to ulong
9484if lt add l 1 get og 5
9485ret ggl add l 1 0
9486end
9487ret gga1 8
9488end
9489
9490fn ggw o ulong to ulong
9491ret add gga1 0 mul o 4
9492end
9493
9494fn ggg o ulong to ulong
9495ret add gga1 1 mul o 4
9496end
9497
9498note GEMM tiles need m, n multiples of 32 and k of 16: V T D F E multiples of 32; R = B T;
9499note softmax rows keep T/32 values per lane in R32..R47, so T is at most 512
9500fn ggok to bool
9501for i ulong 5
9502v = get og i
9503if eq i 3
9504v = get og 7
9505end
9506if eq i 1
9507v = get og 1
9508end
9509if or eq v 0 ne rem v 32 0
9510ret false
9511end
9512end
9513if gt get og 0 65536
9514ret false
9515end
9516if gt get og 1 512
9517ret false
9518end
9519ret le mul get og 6 get og 3 65535
9520end
9521
9522note open the chip and emit the kernels for the dims in og
9523fn ggopen to void
9524if not ggok
9525ggdie 1
9526end
9527asz = ggplan
9528v = get og 0
9529tt = get og 1
9530d = get og 2
9531h = get og 3
9532f = get og 4
9533bb = get og 6
9534grset v tt d h f bb
9535if not gropen asz
9536ggdie 2
9537end
9538put gga 40 gkbase
9539ggstr
9540end
9541
9542note pointer j (1-based) into arg slots 2j, 2j + 1
9543fn ggpt j uint a ulong to void
9544put gkarg mul j 2 gblo a
9545put gkarg add mul j 2 1 gbhi a
9546end
9547
9548fn ggu s uint v ulong to void
9549put gkarg s uint v
9550end
9551
9552fn ggrun id uint na uint gx ulong gy ulong gz ulong to void
9553put gkna 0 na
9554x = uint gx
9555y = uint gy
9556z = uint gz
9557if not gklaunch id x y z
9558ggdie add 100 ulong id
9559end
9560end
9561
9562fn ggmm m ulong n ulong k ulong nz ulong h2 ulong bt bool to void
9563a = uint m
9564b = uint n
9565c = uint k
9566e = uint nz
9567g = uint h2
9568if not gkmm a b c e g bt
9569ggdie 99
9570end
9571end
9572
9573note C[R][n] (+)= X[R][k] W[k][n]
9574fn ggxw x ulong w ulong c ulong n ulong k ulong bt bool to void
9575k4 = uint mul k 4
9576n4 = uint mul n 4
9577gkopa 0 x k4 4 0 0
9578gkopa 1 w 4 n4 0 0
9579gkopc c n4 4 0 0 f32 1.0f64
9580ggmm ggr n k 1 1 bt
9581end
9582
9583note C[R][n] (+)= X[R][k] W[n][k]^T
9584fn ggxwt x ulong w ulong c ulong n ulong k ulong bt bool to void
9585k4 = uint mul k 4
9586n4 = uint mul n 4
9587gkopa 0 x k4 4 0 0
9588gkopa 1 w k4 4 0 0
9589gkopc c n4 4 0 0 f32 1.0f64
9590ggmm ggr n k 1 1 bt
9591end
9592
9593note G[kk][n] += X[R][kk]^T DY[R][n], split over R when the grid is small (ggks)
9594fn ggwg x ulong dy ulong g ulong kk ulong n ulong to void
9595k4 = uint mul kk 4
9596n4 = uint mul n 4
9597s = ggks kk n 1
9598if eq s 1
9599gkopa 0 x 4 k4 0 0
9600gkopa 1 dy 4 n4 0 0
9601gkopc g n4 4 0 0 f32 1.0f64
9602ggmm kk n ggr 1 1 true
9603ret
9604end
9605sl = div ggr s
9606gkopa 0 x 4 k4 0 uint mul mul sl kk 4
9607gkopa 1 dy 4 n4 0 uint mul mul sl n 4
9608ggsk kk n 1 s g 0
9609end
9610
9611note Split-K for the weight gradients, whose depth is R = B T. A grid of (m/32)(n/32)nz blocks
9612note leaves most SMs idle (rung-6 gprof: id 4 at 1.7-6 TFLOP/s on 64-256 blocks), so ggks doubles
9613note the split s (up to 16, R/s a multiple of 16) until the grid reaches 1024 blocks: then
9614note blocks s < 2048, so the m n nz s partials fit the 2^21 floats of slot 37.
9615fn ggks m ulong n ulong nz ulong to ulong
9616b = mul mul div m 32 div n 32 nz
9617r = ggr
9618s = ulong 1
9619while lt mul b s 1024 bound 8
9620s2 = mul s 2
9621if or gt s2 16 ne rem r mul s2 16 0
9622ret s
9623end
9624s = s2
9625end
9626ret s
9627end
9628
9629note Operands A and B are set (gkopa) with the slice stride as s2. Batch z = z1 s + z2 (z1 < nz the
9630note caller's batch, z2 < s the slice) writes partial P[z1][z2] = A[z1 z2]^T B[z1 z2] into slot 37,
9631note then COLSUM 14 adds G[z1] (byte stride gz) += sum over z2 of P[z1][z2], in slice order.
9632note Mutant p (OMUTP 0) adds one slice fewer, which gcmp2 must catch.
9633const OMUTP ulong 1
9634fn ggsk m ulong n ulong nz ulong s ulong g ulong gz uint to void
9635mn = mul m n
9636p = gga1 37
9637n4 = uint mul n 4
9638c1 = uint mul mul mn s 4
9639c2 = uint mul mn 4
9640gkopc p n4 4 c1 c2 f32 1.0f64
9641k = div ggr s
9642z = mul nz s
9643ggmm m n k z s false
9644ggskr mn nz s g gz
9645end
9646
9647note COLSUM 14 over slot 37: G[c gz + i] += sum over z2 < s of P[c][z2][i], i < mn, c < nz
9648fn ggskr mn ulong nz ulong s ulong g ulong gz uint to void
9649kc = s
9650if eq OMUTP 0
9651kc = sub s 1
9652end
9653ggu 0 mn
9654ggu 1 kc
9655ggpt 1 gga1 37
9656ggu 4 mul mn 4
9657ggu 5 mul mul mn s 4
9658ggpt 3 g
9659ggu 8 ulong gz
9660ggrun 14 9 ggcd mn 64 nz 1
9661end
9662
9663note 19 COPY (x into y), 20 ZERO (x) over n elements (the FFN ReLU is fused into the GEMMs, gkope)
9664fn ggew id uint n ulong x ulong y ulong to void
9665ggu 0 n
9666ggpt 1 x
9667ggpt 2 y
9668ggrun id 6 ggcd n 64 1 1
9669end
9670
9671note RMSF/RMSB run one warp per row: arg 0 and the thread count are R*32
9672fn ggrmsf v ulong g ulong y ulong s ulong to void
9673r = mul ggr 32
9674ggu 0 r
9675ggpt 1 v
9676ggpt 2 g
9677ggpt 3 y
9678ggpt 4 s
9679ggrun 10 10 ggcd r 64 1 1
9680end
9681
9682note dh += RMS backward of da; the gain terms go to WS
9683fn ggrmsb v ulong g ulong s ulong to void
9684r = mul ggr 32
9685ggu 0 r
9686ggpt 1 v
9687ggpt 2 g
9688ggpt 3 s
9689ggpt 4 gga1 14
9690ggpt 5 gga1 13
9691ggpt 6 gga1 19
9692ggrun 11 14 ggcd r 64 1 1
9693end
9694
9695note gain gradient at parameter offset go += column sums of WS: per sequence (slot 13), then over B
9696fn ggain go ulong to void
9697d = get og 2
9698tt = get og 1
9699bb = get og 6
9700ggu 0 d
9701ggu 1 tt
9702ggpt 1 gga1 19
9703ggu 4 mul d 4
9704ggu 5 mul mul tt d 4
9705ggpt 3 gga1 20
9706ggu 8 mul d 4
9707ggrun 13 9 ggcd d 64 bb 1
9708ggu 0 d
9709ggu 1 bb
9710ggpt 1 gga1 20
9711ggu 4 mul d 4
9712ggu 5 0
9713ggpt 3 ggg go
9714ggu 8 0
9715ggrun 14 9 ggcd d 64 1 1
9716end
9717
9718note softmax rows: one warp per row, T 32 threads per score block, grid (T/2, B H)
9719fn ggsoft id uint p ulong dp ulong to void
9720tt = get og 1
9721ggu 0 mul tt 32
9722ggpt 1 p
9723ggpt 2 dp
9724ggrun id 6 div tt 2 mul get og 6 get og 3 1
9725end
9726
9727note attention GEMM over the B H (sequence, head) batch; operands set by gghd/ggpl/ggch/ggcp first;
9728note cz: gkopz causal mode (1 A = P or DS, 2 A = P^T or DS^T, 3 C = S or DP)
9729fn ggbh m ulong n ulong k ulong bt bool cz uint to void
9730gkopz cz
9731ggmm m n k mul get og 6 get og 3 get og 3 bt
9732end
9733
9734note head-layout operand (rows of E inside D-wide rows, batch b then head); tr: k runs along rows
9735fn gghd ab uint p ulong tr bool to void
9736s0 = get ggs 0
9737s1 = get ggs 1
9738s2 = get ggs 2
9739if tr
9740gkopa ab p 4 s0 s1 s2
9741else
9742gkopa ab p s0 4 s1 s2
9743end
9744end
9745
9746note T x T score operand (batch b then head); tr: transposed
9747fn ggpl ab uint p ulong tr bool to void
9748s3 = get ggs 3
9749s4 = get ggs 4
9750s5 = get ggs 5
9751if tr
9752gkopa ab p 4 s3 s5 s4
9753else
9754gkopa ab p s3 4 s5 s4
9755end
9756end
9757
9758fn ggch c ulong al f64 to void
9759s0 = get ggs 0
9760s1 = get ggs 1
9761s2 = get ggs 2
9762gkopc c s0 4 s1 s2 f32 al
9763end
9764
9765fn ggcp c ulong al f64 to void
9766s3 = get ggs 3
9767s4 = get ggs 4
9768s5 = get ggs 5
9769gkopc c s3 4 s5 s4 f32 al
9770end
9771
9772note stride table for the dims in og (bytes)
9773fn ggstr to void
9774d = get og 2
9775tt = get og 1
9776put ggs 0 uint mul d 4
9777put ggs 1 uint mul tt mul d 4
9778put ggs 2 uint mul get og 7 4
9779put ggs 3 uint mul tt 4
9780put ggs 4 uint mul tt mul tt 4
9781put ggs 5 uint mul get og 3 mul tt mul tt 4
9782put ggs 6 uint mul ggr mul d 4
9783put ggs 7 uint mul d mul d 4
9784end
9785
9786note q, k, u = a wq, a wk, a wv as one batched GEMM (wq wk wv and q k u are contiguous)
9787fn ggqkv l ulong to void
9788d = get og 2
9789a = ggl l 1
9790w = ggw opwq l
9791c = ggl l 2
9792s0 = get ggs 0
9793s6 = get ggs 6
9794s7 = get ggs 7
9795gkopa 0 a s0 4 0 0
9796gkopa 1 w 4 s0 0 s7
9797gkopc c s0 4 0 s6 f32 1.0f64
9798ggmm ggr d d 3 3 false
9799end
9800
9801note P = softmax(q k^T / sqrt(E)), o = P u; causal: only S on and below the diagonal is formed
9802note (gkmm cz 3) and o stops at the diagonal (cz 1), since P is exactly 0 above it
9803fn ggatt l ulong to void
9804tt = get og 1
9805e = get og 7
9806pp = ggl l 12
9807q = ggl l 2
9808k = ggl l 3
9809gghd 0 q false
9810gghd 1 k false
9811isq = div 1.0f64 fsqrt f64 e
9812ggcp pp isq
9813ggbh tt tt e false 3
9814ggsoft 15 pp pp
9815u = ggl l 4
9816o = ggl l 5
9817ggpl 0 pp false
9818gghd 1 u true
9819ggch o 1.0f64
9820ggbh tt e tt false 1
9821end
9822
9823note hm = hin + o wo; bn = rms(hm) g2; out = hm + relu(bn w1) w2
9824fn ggffn l ulong to void
9825d = get og 2
9826f = get og 4
9827rd = mul ggr d
9828hm = ggl l 6
9829x = ggl l 0
9830ggew 19 rd x hm
9831x = ggl l 5
9832w = ggw opwo l
9833ggxw x w hm d d true
9834x = ggl l 7
9835w = ggw opg2 l
9836s = ggl l 9
9837ggrmsf hm w x s
9838fp = ggl l 10
9839w = ggw opw1 l
9840y = ggl l 11
9841gkope 1 y
9842ggxw x w fp f d false
9843x = y
9844o = ggout l
9845ggew 19 rd hm o
9846w = ggw opw2 l
9847ggxw x w o d f true
9848end
9849
9850fn ggfl l ulong to void
9851x = ggl l 0
9852w = ggw opg1 l
9853a = ggl l 1
9854s = ggl l 8
9855ggrmsf x w a s
9856ggqkv l
9857ggatt l
9858ggffn l
9859end
9860
9861fn ggfwd to void
9862d = get og 2
9863x = ggl 0 0
9864ggu 0 d
9865ggpt 1 x
9866x = ggw 0
9867ggpt 2 x
9868x = ggw get og 30
9869ggpt 3 x
9870x = gga1 2
9871ggpt 4 x
9872tt = get og 1
9873bb = get og 6
9874ggrun 9 10 ggcd d 64 tt bb
9875for l ulong get og 5
9876ggfl l
9877end
9878x = gga1 8
9879w = ggw get og 33
9880z = gga1 10
9881s = gga1 9
9882ggrmsf x w z s
9883x = gga1 11
9884w = ggw 0
9885v = get og 0
9886ggxwt z w x v d false
9887r = mul ggr 32
9888ggu 0 r
9889ggpt 1 x
9890x = gga1 3
9891ggpt 2 x
9892x = gga1 12
9893ggpt 3 x
9894ggrun 21 8 ggcd r 64 1 1
9895end
9896
9897note layer l FFN backward: DH holds d out on entry, d hm on exit. The dF GEMM's fused epilogue (gkope 2)
9898note applies the ReLU mask FP > 0; the forward W1 GEMM (ggffn, gkope 1) stores both FP and Y.
9899fn ggbffn l ulong to void
9900d = get og 2
9901f = get og 4
9902dh = gga1 13
9903df = gga1 17
9904da = gga1 14
9905w = ggw opw2 l
9906x = ggl l 10
9907gkope 2 x
9908ggxwt dh w df f d false
9909x = ggl l 11
9910w = ggg opw2 l
9911ggwg x dh w f d
9912x = ggl l 7
9913w = ggg opw1 l
9914ggwg x df w d f
9915w = ggw opw1 l
9916ggxwt df w da d f false
9917x = ggl l 6
9918w = ggw opg2 l
9919s = ggl l 9
9920ggrmsb x w s
9921ggain opg2 l
9922end
9923
9924note layer l attention backward: DO, DP, DU, DS (in DP), DQ, DK, then DA. Causal as in ggatt: DP
9925note on and below the diagonal (cz 3, SOFTB reads dp above it as 0), DU and DK start at the diagonal
9926note (cz 2), DQ stops there (cz 1).
9927fn ggbatt l ulong to void
9928d = get og 2
9929tt = get og 1
9930e = get og 7
9931dh = gga1 13
9932dn = gga1 16
9933w = ggw opwo l
9934ggxwt dh w dn d d false
9935x = ggl l 5
9936w = ggg opwo l
9937ggwg x dh w d d
9938dp = gga1 18
9939x = ggl l 4
9940gghd 0 dn false
9941gghd 1 x false
9942ggcp dp 1.0f64
9943ggbh tt tt e false 3
9944pp = ggl l 12
9945dq = gga1 15
9946dk = add dq ulong get ggs 6
9947du = add dk ulong get ggs 6
9948ggpl 0 pp true
9949gghd 1 dn true
9950ggch du 1.0f64
9951ggbh tt e tt false 2
9952ggsoft 16 pp dp
9953x = ggl l 3
9954ggpl 0 dp false
9955gghd 1 x true
9956ggch dq 1.0f64
9957ggbh tt e tt false 1
9958al = 1.0f64
9959if eq OMUTG 0
9960al = neg 1.0f64
9961end
9962x = ggl l 2
9963ggpl 0 dp true
9964gghd 1 x true
9965ggch dk al
9966ggbh tt e tt false 2
9967end
9968
9969note DA = DQ wq^T + DK wk^T + DU wv^T; G wq wk wv += a^T (DQ DK DU) batched; then the rms backward
9970fn ggbqkv l ulong to void
9971d = get og 2
9972da = gga1 14
9973dq = gga1 15
9974dk = add dq ulong get ggs 6
9975du = add dk ulong get ggs 6
9976w = ggw opwq l
9977ggxwt dq w da d d false
9978w = ggw opwk l
9979ggxwt dk w da d d true
9980w = ggw opwv l
9981ggxwt du w da d d true
9982a = ggl l 1
9983w = ggg opwq l
9984s0 = get ggs 0
9985s6 = get ggs 6
9986s7 = get ggs 7
9987k = ggks d d 3
9988if eq k 1
9989gkopa 0 a 4 s0 0 0
9990gkopa 1 dq 4 s0 0 s6
9991gkopc w s0 4 0 s7 f32 1.0f64
9992ggmm d d ggr 3 3 true
9993else
9994sl = uint div ggr k
9995gkopa 0 a 4 s0 0 mul sl s0
9996gkopa 1 dq 4 s0 s6 mul sl s0
9997ggsk d d 3 k w s7
9998end
9999x = ggl l 0
10000w = ggw opg1 l
10001s = ggl l 8
10002ggrmsb x w s
10003ggain opg1 l
10004end
10005
10006fn ggbl l ulong to void
10007ggbffn l
10008ggbatt l
10009ggbqkv l
10010end
10011
10012fn ggbwd to void
10013d = get og 2
10014v = get og 0
10015tt = get og 1
10016r = ggr
10017ggew 20 ggp5 gga1 1 gga1 1
10018ggew 20 mul r d gga1 13 gga1 13
10019ggxw gga1 11 ggw 0 gga1 14 d v false
10020ggwg gga1 11 gga1 10 ggg 0 v d
10021ggrmsb gga1 8 ggw get og 33 gga1 9
10022ggain get og 33
10023l = get og 5
10024while gt l 0 bound 1000
10025l = sub l 1
10026ggbl l
10027end
10028td = mul tt d
10029ggu 0 td
10030ggu 1 get og 6
10031ggpt 1 gga1 13
10032ggu 4 mul td 4
10033ggu 5 0
10034ggpt 3 ggg get og 30
10035ggu 8 0
10036ggrun 14 9 ggcd td 64 1 1
10037ggu 0 d
10038ggpt 1 gga1 13
10039ggpt 2 gga1 4
10040ggpt 3 gga1 5
10041ggpt 4 ggg 0
10042ggrun 22 10 ggcd d 64 v 1
10043end
10044
10045note Mutant f: 1 in the product, 0 in the mutant, which uploads layer 0's W1 times 1 + 2^-10 (a
10046note forward error on the chip that gcmp2's FP check must name).
10047const OMUTF ulong 1
10048
10049note W[i] as f32 for i in chunk q of P (a loop runs at most 999999 passes)
10050fn ggupc q ulong p ulong to void
10051h = get gbmem 5
10052wo = get gga 0
10053i0 = mul q OCH
10054for j ulong sub oce q p i0
10055i = add i0 j
10056x = bitcast uint f32 ggupv i
10057m = mapset h add wo mul i 4 4 ulong x
10058end
10059end
10060
10061note the uploaded value of W[i]: the parameter at virtual index i in the product
10062fn ggupv i ulong to f64
10063w = owg i
10064if eq OMUTF 0
10065a = opw1 0
10066if and ge i a lt i add a mul get og 2 get og 4
10067w = mul w 1.0009765625f64
10068end
10069end
10070ret w
10071end
10072
10073note upload the f32 parameters
10074fn ggupw to void
10075p = get og 8
10076for q ulong ocn p
10077ggupc q p
10078end
10079put gkinv 0 1
10080end
10081
10082note upload the batch tokens and the CSR of x (rows sorted by token)
10083fn ggupb to void
10084ggupt
10085ggcsr
10086put gkinv 0 1
10087end
10088
10089note n values of the Adam pool from s as f32 to arena byte offset o + 4 j, and back (one chunk of an
10090note Adam moment)
10091fn ggupmc s ulong o ulong n ulong to void
10092h = get gbmem 5
10093for j ulong n
10094x = bitcast uint f32 omvg add s j
10095m = mapset h add o mul j 4 4 ulong x
10096end
10097end
10098
10099fn ggdnmc s ulong o ulong n ulong to void
10100h = get gbmem 5
10101for j ulong n
10102x = uint mapget h add o mul j 4 4
10103omvp add s j f64 bitcast f32 x
10104end
10105end
10106
10107note chip parameters of chunk q back into the parameter banks as f64
10108fn ggdnwc q ulong p ulong to void
10109h = get gbmem 5
10110o = get gga 0
10111i0 = mul q OCH
10112for j ulong sub oce q p i0
10113i = add i0 j
10114x = uint mapget h add o mul i 4 4
10115owp i f64 bitcast f32 x
10116end
10117end
10118
10119note optimizer state (parameters, Adam pool) to the chip as f32 (m in gga 21, v in gga 22), and back
10120fn ggupst to void
10121ggupw
10122p = get og 8
10123for q ulong ocn p
10124i0 = mul q OCH
10125n = sub oce q p i0
10126ggupmc add get og 11 i0 add get gga 21 mul i0 4 n
10127ggupmc add get og 12 i0 add get gga 22 mul i0 4 n
10128end
10129put gkinv 0 1
10130end
10131
10132fn ggdnst to void
10133p = get og 8
10134for q ulong ocn p
10135ggdnwc q p
10136i0 = mul q OCH
10137n = sub oce q p i0
10138ggdnmc add get og 11 i0 add get gga 21 mul i0 4 n
10139ggdnmc add get og 12 i0 add get gga 22 mul i0 4 n
10140end
10141end
10142
10143fn ggupt to void
10144h = get gbmem 5
10145xo = get gga 2
10146yo = get gga 3
10147for i ulong ggr
10148tk = get obx i
10149m = mapset h add xo mul i 4 4 tk
10150ty = get oby i
10151m = mapset h add yo mul i 4 4 ty
10152end
10153end
10154
10155note counting sort of the R rows by input token: start (V + 1) and order (R) arrays
10156fn ggcsr to void
10157h = get gbmem 5
10158v = get og 0
10159for c ulong add v 1
10160put ggcn c 0
10161end
10162for i ulong ggr
10163c1 = add get obx i 1
10164n = add get ggcn c1 1
10165put ggcn c1 n
10166end
10167for c ulong v
10168c1 = add c 1
10169n = add get ggcn c1 get ggcn c
10170put ggcn c1 n
10171end
10172so = get gga 4
10173for c ulong add v 1
10174n = get ggcn c
10175m = mapset h add so mul c 4 4 n
10176end
10177so = get gga 5
10178for i ulong ggr
10179c = get obx i
10180p = get ggcn c
10181m = mapset h add so mul p 4 4 i
10182put ggcn c add p 1
10183end
10184end
10185
10186note GRD[i] = G[i] for i in chunk q of P
10187fn ggdnc q ulong p ulong to void
10188h = get gbmem 5
10189go = get gga 1
10190i0 = mul q OCH
10191for j ulong sub oce q p i0
10192i = add i0 j
10193x = uint mapget h add go mul i 4 4
10194ocsp i f64 bitcast f32 x
10195end
10196end
10197
10198fn ggdown to void
10199p = get og 8
10200for q ulong ocn p
10201ggdnc q p
10202end
10203end
10204
10205note one f32 at arena byte offset o, as f64
10206fn ggf o ulong to f64
10207x = uint mapget get gbmem 5 o 4
10208ret f64 bitcast f32 x
10209end
10210
10211note row loss from the CE stats (m, sum, l_y) of row r: log(sum) + m - l_y
10212fn ggrl r ulong to f64
10213o = add get gga 12 mul r 12
10214m = ggf o
10215sm = ggf add o 4
10216ly = ggf add o 8
10217ret sub add flog sm m ly
10218end
10219
10220fn ggloss to f64
10221tt = get og 1
10222for b ulong get og 6
10223s = 0.0f64
10224for t ulong tt
10225s = add s ggrl add mul b tt t
10226end
10227put olos b div s f64 tt
10228end
10229ret olsum
10230end
10231
10232note the step loss and gradient for the batch in obx/oby; ggopen first. full: upload the f32
10233note parameters first and download the gradient into ogr[0..P) after; otherwise both stay on chip.
10234note phase wall times (ns): 0 upload, 1 forward, 2 backward, 3 download, 4 loss, 5 on-chip Adam
10235global ggtm long 6
10236fn ggpass full bool to f64
10237t = monons
10238if full
10239ggupw
10240end
10241ggupb
10242u = monons
10243put ggtm 0 sub u t
10244ggfwd
10245t = monons
10246put ggtm 1 sub t u
10247ggbwd
10248u = monons
10249put ggtm 2 sub u t
10250if full
10251ggdown
10252end
10253t = monons
10254put ggtm 3 sub t u
10255l = ggloss
10256u = monons
10257put ggtm 4 sub u t
10258ret l
10259end
10260
10261fn gggrad to f64
10262ret ggpass true
10263end
10264
10265note clip and Adam on chip at step og[41], the f32 image of oadam: SQN partial sums of g^2, the
10266note norm, clip factor, schedule and bias corrections on the host in f64, then ADAM over P
10267fn ggadam to void
10268p = get og 8
10269nc = div ggp5 512
10270ggu 0 nc
10271ggpt 1 gga1 1
10272ggpt 2 gga1 23
10273ggrun 23 6 ggcd nc 64 1 1
10274h = get gbmem 5
10275so = get gga 23
10276ss = 0.0f64
10277for c ulong nc
10278x = uint mapget h add so mul c 4 4
10279ss = add ss f64 bitcast f32 x
10280end
10281nr = fsqrt ss
10282cl = 1.0f64
10283if gt nr 1.0
10284cl = div 1.0 nr
10285end
10286s = get og 41
10287lrs = olrs s bitcast f64 get og 43
10288bc1 = sub 1.0 opowb 0.9 s
10289bc2 = sub 1.0 opowb 0.99 s
10290if eq OMUTC 0
10291bc1 = 1.0f64
10292bc2 = 1.0f64
10293end
10294ggu 0 p
10295ggpt 1 gga1 0
10296ggpt 2 gga1 21
10297ggpt 3 gga1 22
10298ggpt 4 gga1 1
10299ggu 10 ulong grf cl
10300ggu 11 ulong grf div lrs bc1
10301ggu 12 ulong grf div 1.0f64 bc2
10302ggrun 24 13 ggcd p 64 1 1
10303end
10304
10305fn ggtms to void
10306oputc 71
10307oputc 84
10308for i ulong 6
10309oputc 32
10310oputl get ggtm i
10311end
10312for i ulong 4
10313oputc 32
10314oputl get gkt i
10315end
10316oputc 10
10317end
10318
10319note keep the CPU gradient: BLK[i] = GRD[i] (chunked: a loop runs at most 999999 passes)
10320fn ggkeep p ulong to void
10321for q ulong ocn p
10322i0 = mul q OCH
10323for j ulong sub oce q p i0
10324i = add i0 j
10325ogrp add p i ocsg i
10326end
10327end
10328end
10329
10330note ggcm[0] = max |GRD[i] - BLK[i]|, ggcm[1] = max |BLK[i]| over i < P
10331global ggcm f64 2
10332fn ggmaxd p ulong to void
10333put ggcm 0 0.0f64
10334put ggcm 1 0.0f64
10335for q ulong ocn p
10336i0 = mul q OCH
10337for j ulong sub oce q p i0
10338i = add i0 j
10339rf = ogrg add p i
10340dd = sub ocsg i rf
10341if lt dd 0.0f64
10342dd = neg dd
10343end
10344if lt rf 0.0f64
10345rf = neg rf
10346end
10347if gt dd get ggcm 0
10348put ggcm 0 dd
10349end
10350if gt rf get ggcm 1
10351put ggcm 1 rf
10352end
10353end
10354end
10355end
10356
10357note gcmp CK DATA S: CPU ograd and GPU gradient for batch S; prints
10358note `GC P maxabs_e12 maxref_e12 rel_e9 cpu_loss_micro gpu_loss_micro gpu_ns tol_e9` and GCMP PASS
10359note (rel < tol, |loss difference| < 1e-5) or GCMP FAIL (exit 45). tol = R 2^-24 is the first-order
10360note bound gamma_R on the f32 error of a sum of R = B T rows, the longest reduction of the weight
10361note gradients, relative to the sum of magnitudes; the CPU reference is f64. ReLU units whose f64
10362note pre-activation lies within f32 resolution of zero can take the other side on the GPU (receipt:
10363note evidence/OMEGALLM/gpu/README.md), and both sides are valid subgradients.
10364fn ggcmp to void
10365if ne oargc 5
10366exit 2
10367end
10368ockload 2
10369odopen 3
10370obatch oargu 4
10371put og 10 0
10372lc = ograd
10373p = get og 8
10374ggkeep p
10375ggopen
10376t0 = monons
10377lg = gggrad
10378t1 = monons
10379ggmaxd p
10380mx = get ggcm 0
10381mr = get ggcm 1
10382rel = div mx mr
10383dl = sub lg lc
10384if lt dl 0.0f64
10385dl = neg dl
10386end
10387oputc 71
10388oputc 67
10389oputc 32
10390oputu p
10391oputc 32
10392oputl rmic mul mx 1000000.0f64
10393oputc 32
10394oputl rmic mul mr 1000000.0f64
10395oputc 32
10396oputl rmic mul rel 1000.0f64
10397oputc 32
10398oputl rmic lc
10399oputc 32
10400oputl rmic lg
10401oputc 32
10402oputl sub t1 t0
10403oputc 32
10404tol = div f64 ggr 16777216.0f64
10405oputl rmic mul tol 1000.0f64
10406oputc 10
10407ggtms
10408ok = and lt rel tol lt dl 0.00001f64
10409r = nvstop
10410oputc 71
10411oputc 67
10412oputc 77
10413oputc 80
10414oputc 32
10415if ok
10416oputc 80
10417oputc 65
10418oputc 83
10419oputc 83
10420oputc 10
10421oflush
10422ret
10423end
10424oputc 70
10425oputc 65
10426oputc 73
10427oputc 76
10428oputc 10
10429oflush
10430exit 45
10431end
10432
10433note true when x is finite (every comparison with NaN is false)
10434fn ggfin x f64 to bool
10435a = x
10436if lt a 0.0f64
10437a = neg a
10438end
10439ret le a bitcast f64 ulong 9218868437227405311
10440end
10441
10442note x for printing: 1e6 when x is not finite or above 1e6 (rmic traps past the long range)
10443fn ggclp x f64 to f64
10444if ggfin x
10445if lt x 1000000.0f64
10446ret x
10447end
10448end
10449ret 1000000.0f64
10450end
10451
10452note gcmp2 state. ggfs[j] = s_j for the row in flight (F <= 1536, CONTRACT-V2). ggk2: [0] ReLU
10453note flips (sign of FP_chip differs from FP_ref), [1] non-finite FP values (chip or reference),
10454note [2] layer of the largest rho, [3] non-finite gradient values (chip, K0 or K1).
10455note ggk3: [0] rho max, [1] max |FP_ref| at a flip, [2] max |FP_chip - FP_ref|.
10456global ggfs f64 1536
10457global ggk2 ulong 4
10458global ggk3 f64 3
10459
10460note s_j = sum_k |bn_k| |W1[k F + j]| for row t of layer l (k < D ascending), the magnitude sum of
10461note the FP dot product the chip rounds (layer l is staged: ogmask runs inside olayb)
10462fn ggsj l ulong t ulong to void
10463d = get og 2
10464f = get og 4
10465bn = add oabn l mul t d
10466w = opx opw1 l
10467for j ulong f
10468put ggfs j 0.0f64
10469end
10470for k ulong d
10471a = get oa add bn k
10472if lt a 0.0f64
10473a = neg a
10474end
10475ggsjk a add w mul k f f
10476end
10477end
10478
10479fn ggsjk a f64 wo ulong f ulong to void
10480for j ulong f
10481x = get ow add wo j
10482if lt x 0.0f64
10483x = neg x
10484end
10485put ggfs j add get ggfs j mul a x
10486end
10487end
10488
10489note model.om's ReLU mask hook (ogkm[0] = 1, gcmp2 only): for row t of layer l of the sequence in
10490note flight, check every unit's reference FP against the chip's FP (left in the arena by gggrad),
10491note then zero dF where the chip's FP is not > 0, the chip's fused dF mask (gkope 2; NaN included).
10492fn ogmask yo ulong l ulong t ulong to void
10493f = get og 4
10494xo = add oafp l mul t f
10495ggsj l t
10496row = add mul get ogkm 1 get og 1 t
10497c0 = add sub ggl l 10 get gga 40 mul mul row f 4
10498for j ulong f
10499c = ggf add c0 mul j 4
10500ggk1 get oa add xo j c j l
10501if not gt c 0.0f64
10502put oa add yo j 0.0f64
10503end
10504end
10505end
10506
10507note one unit: non-finite count, |FP_chip - FP_ref|, rho = that over s_j (inf when s_j = 0 and the
10508note values differ), and the flip count
10509fn ggk1 v f64 c f64 j ulong l ulong to void
10510if not and ggfin v ggfin c
10511put ggk2 1 add get ggk2 1 1
10512ret
10513end
10514dd = sub c v
10515if lt dd 0.0f64
10516dd = neg dd
10517end
10518if gt dd get ggk3 2
10519put ggk3 2 dd
10520end
10521ggkr dd get ggfs j l
10522ggkfl v c
10523end
10524
10525fn ggkr dd f64 s f64 l ulong to void
10526if not gt dd 0.0f64
10527ret
10528end
10529rho = bitcast f64 ulong 9218868437227405312
10530if gt s 0.0f64
10531rho = div dd s
10532end
10533if gt rho get ggk3 0
10534put ggk3 0 rho
10535put ggk2 2 l
10536end
10537end
10538
10539fn ggkfl v f64 c f64 to void
10540a = gt v 0.0f64
10541b = gt c 0.0f64
10542if eq a b
10543ret
10544end
10545put ggk2 0 add get ggk2 0 1
10546if lt v 0.0f64
10547v = neg v
10548end
10549if gt v get ggk3 1
10550put ggk3 1 v
10551end
10552end
10553
10554note Adam pool [o + i] = GRD[i] for i < P (the chip gradient at o = 0, K0 at o = P; gcmp2 has no
10555note Adam step)
10556fn ggstash p ulong o ulong to void
10557for q ulong ocn p
10558ggstq q p o
10559end
10560end
10561
10562fn ggstq q ulong p ulong o ulong to void
10563i0 = mul q OCH
10564for j ulong sub oce q p i0
10565i = add i0 j
10566omvp add o i ocsg i
10567end
10568end
10569
10570note ggc2: [0] max |G_chip - K0|, [1] max |K0|, [2] max |G_chip - K1| over i < P, with the chip
10571note gradient in the Adam pool [0, P), K0 in [P, 2P) and K1 in GRD; non-finite values count in ggk2[3]
10572global ggc2 f64 3
10573fn gg2cmp p ulong to void
10574for k ulong 3
10575put ggc2 k 0.0f64
10576end
10577for q ulong ocn p
10578i0 = mul q OCH
10579for j ulong sub oce q p i0
10580gg2c1 add i0 j p
10581end
10582end
10583end
10584
10585fn gg2c1 i ulong p ulong to void
10586c = omvg i
10587r0 = omvg add p i
10588r1 = ocsg i
10589if not and and ggfin c ggfin r0 ggfin r1
10590put ggk2 3 add get ggk2 3 1
10591ret
10592end
10593gg2mx 0 sub c r0
10594gg2mx 1 r0
10595gg2mx 2 sub c r1
10596end
10597
10598fn gg2mx k ulong x f64 to void
10599if lt x 0.0f64
10600x = neg x
10601end
10602if gt x get ggc2 k
10603put ggc2 k x
10604end
10605end
10606
10607note gcmp2 CK DATA S: branch-conditioned parity (evidence/OMEGALLM/gpu-max/README.md). The chip
10608note gradient runs first and stays in the Adam pool; K0 is the CPU ograd with its own ReLU mask (gcmp's
10609note reference), K1 the CPU ograd with the chip's FP sign as the ReLU mask (ogmask), which also checks
10610note every FFN pre-activation. Prints `GC2 P rel0_e9 rel1_e9 tolr_e9 rho_e9 tolf_e9 rho_layer flips
10611note vflip_e12 fpdiff_e12 nonfinite cpu_loss_micro gpu_loss_micro gpu_ns fails` and GCMP2 PASS, or
10612note GCMP2 FAIL (exit 47). rel0 = max|G_chip - K0| / max|K0| (gcmp's rel), rel1 = max|G_chip - K1| /
10613note max|K0|; rho = max |FP_chip - FP_ref| / s_j. fails is a bit set: 1 a non-finite FP or gradient
10614note value, 2 rho >= tolf = max(D, F, T) 2^-24, 4 rel1 >= tolr = R 2^-24 (gcmp's gamma_R), 8 |chip
10615note loss - K0 loss| >= 1e-5.
10616fn ggcmp2 to void
10617if ne oargc 5
10618exit 2
10619end
10620ockload 2
10621odopen 3
10622obatch oargu 4
10623p = get og 8
10624ggopen
10625t0 = monons
10626lg = gggrad
10627t1 = monons
10628ggstash p 0
10629put og 10 0
10630put ogkm 0 0
10631l0 = ograd
10632ggstash p p
10633for k ulong 4
10634put ggk2 k 0
10635end
10636for k ulong 3
10637put ggk3 k 0.0f64
10638end
10639put ogkm 0 1
10640put og 10 0
10641l1 = ograd
10642put ogkm 0 0
10643gg2cmp p
10644ggc2pr lg l0 sub t1 t0
10645end
10646
10647note the GC2 line and verdict of gcmp2 (lg chip loss, l0 K0 loss, ns chip gradient time); gbcmp2
10648note (gkbf 1) takes gb.om's verdict
10649fn ggc2pr lg f64 l0 f64 ns long to void
10650fl = ggc2fl lg l0
10651ggc2ln lg l0 ns fl
10652ggtms
10653r = nvstop
10654if eq get gkbf 0 1
10655gbc2vd lg l0 fl
10656ret
10657end
10658ggc2vd eq fl 0
10659end
10660
10661note ggc2[k] / max |K0|, clamped for printing
10662fn gg2rl k ulong to f64
10663ret ggclp div get ggc2 k get ggc2 1
10664end
10665
10666note tolf = max(D, F, T) 2^-24
10667fn gg2tf to f64
10668n = fmax f64 get og 2 fmax f64 get og 4 f64 get og 1
10669ret div n 16777216.0f64
10670end
10671
10672note tolr = R 2^-24 (gcmp's gamma_R)
10673fn gg2tr to f64
10674ret div f64 ggr 16777216.0f64
10675end
10676
10677note the gcmp2 fail bits (see ggcmp2); gb.om's BF16 bits under gkbf 1
10678fn ggc2fl lg f64 l0 f64 to ulong
10679if eq get gkbf 0 1
10680ret gbc2fl lg l0 get ggk3 0 add get ggk2 1 get ggk2 3
10681end
10682dl = sub lg l0
10683if lt dl 0.0f64
10684dl = neg dl
10685end
10686fl = ulong 0
10687if ne add get ggk2 1 get ggk2 3 0
10688fl = or fl 1
10689end
10690if not lt ggclp get ggk3 0 gg2tf
10691fl = or fl 2
10692end
10693if not lt gg2rl 2 gg2tr
10694fl = or fl 4
10695end
10696if not lt dl 0.00001f64
10697fl = or fl 8
10698end
10699ret fl
10700end
10701
10702fn ggc2ln lg f64 l0 f64 ns long fl ulong to void
10703oputc 71
10704oputc 67
10705oputc 50
10706oputc 32
10707oputu get og 8
10708gg2pe gg2rl 0
10709gg2pe gg2rl 2
10710gg2pe gg2tr
10711gg2pe ggclp get ggk3 0
10712gg2pe gg2tf
10713oputc 32
10714oputu get ggk2 2
10715oputc 32
10716oputu get ggk2 0
10717oputc 32
10718oputl rmic mul ggclp get ggk3 1 1000000.0f64
10719oputc 32
10720oputl rmic mul ggclp get ggk3 2 1000000.0f64
10721oputc 32
10722oputu add get ggk2 1 get ggk2 3
10723oputc 32
10724oputl rmic ggclp l0
10725oputc 32
10726oputl rmic ggclp lg
10727oputc 32
10728oputl ns
10729oputc 32
10730oputu fl
10731oputc 10
10732end
10733
10734note " x_e9"
10735fn gg2pe x f64 to void
10736oputc 32
10737oputl rmic mul x 1000.0f64
10738end
10739
10740fn ggc2vd ok bool to void
10741oputc 71
10742oputc 67
10743oputc 77
10744oputc 80
10745oputc 50
10746oputc 32
10747if ok
10748oputc 80
10749oputc 65
10750oputc 83
10751oputc 83
10752oputc 10
10753oflush
10754ret
10755end
10756oputc 70
10757oputc 65
10758oputc 73
10759oputc 76
10760oputc 10
10761oflush
10762exit 47
10763end
10764
10765note gtrain CK TRAIN UNTIL EVERY: cmdtrain with the gradient from the GPU (K = 1, no workers); prints
10766note `T s+1 loss_micro` at save points like train
10767fn ggtrain to void
10768if ne oargc 6
10769exit 2
10770end
10771ockload 2
10772odopen 3
10773un = oargu 4
10774ev = oargu 5
10775if eq ev 0
10776exit 2
10777end
10778en = get og 42
10779if lt un en
10780en = un
10781end
10782ggopen
10783while lt get og 41 en bound 999999
10784s = get og 41
10785obatch s
10786put og 10 0
10787ls = gggrad
10788oadam
10789s1 = add s 1
10790put og 41 s1
10791if or eq rem s1 ev 0 eq s1 en
10792ocksave 2
10793oputc 84
10794oputc 32
10795oputu s1
10796oputc 32
10797oputl rmic ls
10798oputc 10
10799oflush
10800end
10801end
10802r = nvstop
10803end
10804
10805note BLK[i] = the f64 parameter i before the step
10806fn ggkw q ulong p ulong to void
10807i0 = mul q OCH
10808for j ulong sub oce q p i0
10809i = add i0 j
10810ogrp add p i owg i
10811end
10812end
10813
10814note GRD[i] = chip parameter i as f64
10815fn ggdw q ulong p ulong to void
10816h = get gbmem 5
10817wo = get gga 0
10818i0 = mul q OCH
10819for j ulong sub oce q p i0
10820i = add i0 j
10821x = uint mapget h add wo mul i 4 4
10822ocsp i f64 bitcast f32 x
10823end
10824end
10825
10826note over i < P with w0 = BLK[i], c = parameter i (CPU f64 result), g = GRD[i] (chip f32 result):
10827note u = c - w0, d = (g - f32(w0)) - u; ggam 0 max |d|, 1 max |u|, 2 max (|d| - 2^-24 |c|), the part
10828note of d above the half-ulp rounding of the f32 master weight
10829global ggam f64 3
10830fn ggad1 i ulong p ulong to void
10831w0 = ogrg add p i
10832c = owg i
10833u = sub c w0
10834g = sub ocsg i f64 f32 w0
10835d = fabs sub g u
10836u = fabs u
10837e = sub d mul fabs c 0.000000059604644775390625f64
10838if gt d get ggam 0
10839put ggam 0 d
10840end
10841if gt u get ggam 1
10842put ggam 1 u
10843end
10844if gt e get ggam 2
10845put ggam 2 e
10846end
10847end
10848
10849fn ggadc q ulong p ulong to void
10850i0 = mul q OCH
10851for j ulong sub oce q p i0
10852ggad1 add i0 j p
10853end
10854end
10855
10856note gacmp CK DATA S: one step from CK on batch S with the chip gradient, Adam on the CPU (oadam, f64)
10857note and on the chip (ggadam, f32); prints `GA P maxd_e12 maxu_e12 rel_e9 resid_e9 tol_e9 adam_ns
10858note loss_micro` and GACMP PASS (resid = max(|d| - 2^-24 |c|) / max |u| < tol = 2^-16) or GACMP FAIL
10859note (exit 46)
10860fn ggacmp to void
10861if ne oargc 5
10862exit 2
10863end
10864ockload 2
10865odopen 3
10866obatch oargu 4
10867put og 10 0
10868p = get og 8
10869ggopen
10870ggupst
10871lg = ggpass true
10872for q ulong ocn p
10873ggkw q p
10874end
10875oadam
10876t0 = monons
10877ggadam
10878t1 = monons
10879for q2 ulong ocn p
10880ggdw q2 p
10881end
10882for k ulong 3
10883put ggam k 0.0f64
10884end
10885for q3 ulong ocn p
10886ggadc q3 p
10887end
10888mu = get ggam 1
10889rel = div get ggam 0 mu
10890res = div get ggam 2 mu
10891tol = div 1.0f64 65536.0f64
10892oputc 71
10893oputc 65
10894oputc 32
10895oputu p
10896oputc 32
10897oputl rmic mul get ggam 0 1000000.0f64
10898oputc 32
10899oputl rmic mul mu 1000000.0f64
10900oputc 32
10901oputl rmic mul rel 1000.0f64
10902oputc 32
10903oputl rmic mul res 1000.0f64
10904oputc 32
10905oputl rmic mul tol 1000.0f64
10906oputc 32
10907oputl sub t1 t0
10908oputc 32
10909oputl rmic lg
10910oputc 10
10911r = nvstop
10912oputc 71
10913oputc 65
10914oputc 67
10915oputc 77
10916oputc 80
10917oputc 32
10918if lt res tol
10919oputc 80
10920oputc 65
10921oputc 83
10922oputc 83
10923oputc 10
10924oflush
10925ret
10926end
10927oputc 70
10928oputc 65
10929oputc 73
10930oputc 76
10931oputc 10
10932oflush
10933exit 46
10934end
10935
10936note gtrainf CK TRAIN UNTIL EVERY: gtrain with the parameters and Adam state resident on the chip in
10937note f32 (ggadam); only the batch goes up each step. At save points the state comes back to f64 for
10938note ocksave and `T s+1 loss_micro` is printed; GT at the end has the last step's phase times.
10939fn ggtrainf to void
10940if ne oargc 6
10941exit 2
10942end
10943ockload 2
10944odopen 3
10945un = oargu 4
10946ev = oargu 5
10947if eq ev 0
10948exit 2
10949end
10950en = get og 42
10951if lt un en
10952en = un
10953end
10954ggopen
10955ggupst
10956while lt get og 41 en bound 999999
10957s = get og 41
10958obatch s
10959put og 10 0
10960ls = ggpass false
10961t = monons
10962ggadam
10963put ggtm 5 sub monons t
10964s1 = add s 1
10965put og 41 s1
10966if or eq rem s1 ev 0 eq s1 en
10967ggdnst
10968ocksave 2
10969oputc 84
10970oputc 32
10971oputu s1
10972oputc 32
10973oputl rmic ls
10974oputc 10
10975oflush
10976end
10977end
10978ggtms
10979oflush
10980r = nvstop
10981end
10982
10983note gprof CK TRAIN N: N gtrainf steps from CK, nothing saved. The last step runs with the launch
10984note profile on (gk.om gkpf), then 64 ZERO launches of 64 floats measure the bare launch round trip.
10985note Prints `T steps loss_micro`, one `GP id gx gy gz m n k wait_ns` per profiled launch, then GT.
10986fn ggprof to void
10987if ne oargc 5
10988exit 2
10989end
10990ockload 2
10991odopen 3
10992nn = oargu 4
10993if eq nn 0
10994exit 2
10995end
10996ggopen
10997ggupst
10998ls = 0.0f64
10999for i ulong nn
11000s = get og 41
11001obatch s
11002put og 10 0
11003if eq add i 1 nn
11004put gkpf 0 1
11005end
11006ls = ggpass false
11007t = monons
11008ggadam
11009put ggtm 5 sub monons t
11010put og 41 add s 1
11011end
11012for i ulong 64
11013ggew 20 64 ggg 0 ggg 0
11014end
11015put gkpf 0 0
11016oputc 84
11017oputc 32
11018oputu get og 41
11019oputc 32
11020oputl rmic ls
11021oputc 10
11022for q ulong get gkpc 0
11023o = mul q 8
11024oputc 71
11025oputc 80
11026for j ulong 8
11027oputc 32
11028oputu get gkpl add o j
11029end
11030oputc 10
11031end
11032ggtms
11033oflush
11034r = nvstop
11035end
11036
11037note geval CK HELD W [MERGES]: eval (cli.om cmdeval) with the forward pass on the chip in f32. Windows
11038note w < W at offsets w T, B per forward (og 6); the last set pads its spare rows with window 0 and
11039note does not count them. Each window adds its mean row loss in window order and, when V > 256, the
11040note decoded length of its T targets, as cmdeval does. Prints `E step W loss_micro bpb_micro` like
11041note eval, then `GE gpu_ns` (upload, forwards and loss reads). Exits 2 and 23 as eval.
11042global ggev f64 1
11043global ggen ulong 1
11044fn ggeval to void
11045ww = ggevw
11046t = monons
11047ggopen
11048ggupw
11049put ggev 0 0.0f64
11050put ggen 0 0
11051bb = get og 6
11052nc = div add ww sub bb 1 bb
11053for c ulong nc
11054ggevc c ww
11055end
11056ns = sub monons t
11057ggevpr ww
11058oputc 71
11059oputc 69
11060oputc 32
11061oputl ns
11062oputc 10
11063oflush
11064r = nvstop
11065end
11066
11067note eval's argument checks; loads CK, MERGES and HELD and returns W
11068fn ggevw to ulong
11069na = oargc
11070if and ne na 5 ne na 6
11071exit 2
11072end
11073ockload 2
11074ww = oargu 4
11075v = get og 0
11076if gt v 256
11077if eq na 5
11078exit 2
11079end
11080obload 5 v
11081end
11082odopen 3
11083tt = get og 1
11084m = get odn 0
11085if eq ww 0
11086exit 23
11087end
11088if gt ww div sub m 1 tt
11089exit 23
11090end
11091ret ww
11092end
11093
11094note window set c: rows b < B hold window c B + b (window 0 past W), one chip forward, then the sums
11095fn ggevc c ulong ww ulong to void
11096bb = get og 6
11097tt = get og 1
11098for b ulong bb
11099w = add mul c bb b
11100if ge w ww
11101w = 0
11102end
11103obrow b mul w tt
11104end
11105ggupb
11106ggfwd
11107for b ulong bb
11108w = add mul c bb b
11109if lt w ww
11110ggevr b
11111end
11112end
11113end
11114
11115note add row b's mean loss (CE stats left by ggfwd) and, when V > 256, its decoded target bytes
11116fn ggevr b ulong to void
11117tt = get og 1
11118o = mul b tt
11119s = 0.0f64
11120for t ulong tt
11121s = add s ggrl add o t
11122end
11123put ggev 0 add get ggev 0 div s f64 tt
11124if gt get og 0 256
11125n = get ggen 0
11126for t ulong tt
11127n = add n obtlen get oby add o t
11128end
11129put ggen 0 n
11130end
11131end
11132
11133note the E line of cmdeval from the sums
11134fn ggevpr ww ulong to void
11135lo = div get ggev 0 f64 ww
11136bp = div lo 0.6931471805599453
11137if gt get og 0 256
11138tt = get og 1
11139bp = div mul bp f64 mul ww tt f64 get ggen 0
11140end
11141oputc 69
11142oputc 32
11143oputu get og 41
11144oputc 32
11145oputu ww
11146oputc 32
11147oputl rmic lo
11148oputc 32
11149oputl rmic bp
11150oputc 10
11151end
11152
11153note gdiag CK DATA S: the CE kernel's per-row stats (max m, sum of exp(l - m), logit l_y) on the chip
11154note in f32 against the CPU's f64 values for the same forward of batch S. Prints R, then the mean
11155note over rows times 1e9 of: d m, d l_y, d log sum, d loss, (sum of the chip's p) - 1, d (l_y - m).
11156note It located the CE sum bias that the Kahan sum in gr.om grce removes.
11157global gdacc f64 6
11158global gdcpu f64 3
11159fn ggdiag to void
11160if ne oargc 5
11161exit 2
11162end
11163ockload 2
11164odopen 3
11165obatch oargu 4
11166put og 10 0
11167ggopen
11168ggupw
11169ggupb
11170ggfwd
11171for i ulong 6
11172put gdacc i 0.0f64
11173end
11174for b ulong get og 6
11175x = ofwd b
11176for t ulong get og 1
11177ggdrow b t
11178end
11179end
11180n = f64 ggr
11181oputu ggr
11182for i ulong 6
11183oputc 32
11184oputl rmic mul div get gdacc i n 1000.0f64
11185end
11186oputc 10
11187oflush
11188r = nvstop
11189end
11190
11191fn ggdrow b ulong t ulong to void
11192r = add mul b get og 1 t
11193ggdcpu t get oby r
11194ggdacc r
11195put gdacc 4 add get gdacc 4 sub ggdps r 1.0f64
11196end
11197
11198note CPU f64 stats of row t of the last ofwd: max logit, log sum exp(l - max), logit of y
11199fn ggdcpu t ulong y ulong to void
11200d = get og 2
11201v = get og 0
11202pr = add get og 18 mul t v
11203klogit add get og 17 mul t d pr v d
11204m = bitcast f64 ulong 18442240474082181120
11205for j ulong v
11206m = fmax m get oa add pr j
11207end
11208s = 0.0f64
11209for k ulong v
11210s = add s fexp sub get oa add pr k m
11211end
11212put gdcpu 0 m
11213put gdcpu 1 flog s
11214put gdcpu 2 get oa add pr y
11215end
11216
11217fn ggdacc r ulong to void
11218o = add get gga 12 mul r 12
11219mg = sub ggf o get gdcpu 0
11220ls = sub flog ggf add o 4 get gdcpu 1
11221lg = sub ggf add o 8 get gdcpu 2
11222put gdacc 0 add get gdacc 0 mg
11223put gdacc 1 add get gdacc 1 lg
11224put gdacc 2 add get gdacc 2 ls
11225put gdacc 3 add get gdacc 3 sub add ls mg lg
11226put gdacc 5 add get gdacc 5 sub lg mg
11227end
11228
11229note sum of the chip's probabilities of row r, recovered from the CE output L = (p - [j = y]) sc
11230fn ggdps r ulong to f64
11231v = get og 0
11232sc = div 1.0f64 f64 ggr
11233o = add get gga 11 mul mul r v 4
11234s = 0.0f64
11235for j ulong v
11236s = add s div ggf add o mul j 4 sc
11237end
11238ret add s 1.0f64
11239end
11240
11241note gb10 platform main: gcmp, gcmp2, gtrain, gtrainf, gacmp, geval, gdiag and gprof here, the BF16
11242note commands gtrainb, gprofb, gbcmp, gbcmp2 and gbfrag in gb.om, every other command through the CPU
11243note main (lio.om)
11244fn trainingmain to int
11245if lt oargc 2
11246exit 2
11247end
11248w = ocmdw
11249if eq w 1886217063
11250ggcmp
11251ret 0
11252end
11253if eq w 216634581863
11254ggcmp2
11255ret 0
11256end
11257if eq w 121398885512295
11258ggtrain
11259ret 0
11260end
11261if eq w 28831846509999207
11262ggtrainf
11263ret 0
11264end
11265if eq w 482871566695
11266ggacmp
11267ret 0
11268end
11269if eq w 465491617127
11270ggeval
11271ret 0
11272end
11273if eq w 444015928423
11274ggdiag
11275ret 0
11276end
11277if eq w 439956435047
11278ggprof
11279ret 0
11280end
11281if eq w 27705946603156583
11282gbtrain
11283ret 0
11284end
11285if eq w 108192095957095
11286gbprof
11287ret 0
11288end
11289if eq w 482871566951
11290gbcmp
11291ret 0
11292end
11293if eq w 55458452955751
11294gbcmp2
11295ret 0
11296end
11297if eq w 113668228801127
11298gbfrag
11299ret 0
11300end
11301ret ocpumain
11302end
11303note Experimental greedy GPU sampler; existing f32 forward kernels, one sequence.
11304note Args CK N PROMPT MERGES. No checkpoint writes and no tool execution.
11305fn gsforward to void
11306d = get og 2
11307x = ggl 0 0
11308ggu 0 d
11309ggpt 1 x
11310x = ggw 0
11311ggpt 2 x
11312x = ggw get og 30
11313ggpt 3 x
11314x = gga1 2
11315ggpt 4 x
11316ggrun 9 10 ggcd d 64 get og 1 get og 6
11317for l ulong get og 5
11318ggfl l
11319end
11320x = gga1 8
11321w = ggw get og 33
11322z = gga1 10
11323s = gga1 9
11324ggrmsf x w z s
11325x = gga1 11
11326w = ggw 0
11327ggxwt z w x get og 0 d false
11328end
11329fn gsnext to ulong
11330ggupb
11331gsforward
11332o = add get gga 11 mul mul sub get og 9 1 get og 0 4
11333best = ulong 0
11334bv = ggf o
11335for c ulong get og 0
11336v = ggf add o mul c 4
11337if gt v bv
11338bv = v
11339best = c
11340end
11341end
11342ret best
11343end
11344fn main to int
11345put tmarks 0 ulong monons
11346iread
11347put tmarks 1 ulong monons
11348nn = ulong 160
11349ockload 1
11350put tmarks 2 ulong monons
11351put og 6 1
11352put og 9 0
11353ocpush 0
11354obload 4 get og 0
11355np = obprompt 3
11356put tmarks 7 np
11357put tmarks 3 ulong monons
11358if ge np get og 1
11359ret 3
11360end
11361for i ulong np
11362ocpush get obtk i
11363end
11364ggopen
11365ggupw
11366put tmarks 4 ulong monons
11367for i ulong nn
11368c = gsnext
11369if eq c 0
11370put tmarks 9 1
11371break
11372end
11373put tmarks add i 10 ulong monons
11374put tmarks 8 add i 1
11375obtput c
11376ocpush c
11377end
11378put tmarks 5 ulong monons
11379oflush
11380r = nvstop
11381put tmarks 6 ulong monons
11382timereport
11383ret 0
11384end
11385
11386text imodel "$HOME/.local/share/omega-tool-model/round40/model.ck"
11387text imerges "$HOME/.local/share/omega-tool-model/round40/merges.bin"
11388text example "Task: Fix the error in this file and confirm its goal.\nFile: jobs/item76666.om\nGoal: calc starts with x and adds 4 exactly 3 times in a for loop. main calls calc with 70.\nState: unread\nAnswer:\n"
11389note Native installed sampler input. No generated text is executed.
11390global iprom uchar 4001
11391global icount ulong 1
11392global iflag uchar 16
11393text idemo "--demo"
11394text ihelp "--help"
11395text iusage "Omega tool model, round40.\nUse: omega-tool-model < prompt.txt\nDemo: omega-tool-model --demo\nReads one compact current-state prompt and prints model text.\nNo tools are executed. No chat service is started.\nInput: 1..4000 bytes, no zero bytes, fewer than 256 tokens.\n"
11396
11397fn iusageout to void
11398off = ulong 0
11399while lt off len iusage bound 4096
11400r = write 1 iusage off sub len iusage off
11401if le r 0
11402exit 70
11403end
11404off = add off ulong r
11405end
11406end
11407
11408fn iread to void
11409if eq argcnt 2
11410r = argread 1 iflag 0 len iflag
11411if ne r 6
11412iusageout
11413exit 2
11414end
11415d = true
11416h = true
11417for i ulong 6
11418if ne get iflag i get idemo i
11419d = false
11420end
11421if ne get iflag i get ihelp i
11422h = false
11423end
11424end
11425if h
11426iusageout
11427exit 0
11428end
11429if not d
11430iusageout
11431exit 2
11432end
11433for i ulong len example
11434put iprom i get example i
11435end
11436put icount 0 len example
11437ret
11438end
11439if ne argcnt 1
11440iusageout
11441exit 2
11442end
11443n = ulong 0
11444while lt n len iprom bound 4001
11445r = read 0 iprom n sub len iprom n
11446if lt r 0
11447exit 70
11448end
11449if eq r 0
11450break
11451end
11452n = add n ulong r
11453end
11454if or eq n 0 gt n 4000
11455iusageout
11456exit 2
11457end
11458for i ulong n
11459if eq get iprom i 0
11460exit 2
11461end
11462end
11463put icount 0 n
11464end
11465
11466note Preserve the sampler's internal argument-index interface, with installed paths.
11467fn installedargs i ulong to long
11468if eq i 1
11469for k ulong len imodel
11470put oarg k get imodel k
11471end
11472ret long len imodel
11473end
11474if eq i 4
11475for k ulong len imerges
11476put oarg k get imerges k
11477end
11478ret long len imerges
11479end
11480if eq i 3
11481for k ulong get icount 0
11482put oarg k get iprom k
11483end
11484ret long get icount 0
11485end
11486ret neg long 22
11487end
11488
11489note Emit timing only after model output and device cleanup; 180 little-endian u64 values.
11490fn timereport to void
11491for i ulong 180
11492for j ulong 8
11493v = and shr get tmarks i mul j 8 255
11494put tbytes add mul i 8 j uchar v
11495end
11496end
11497p = ulong 0
11498while lt p len tbytes bound 1440
11499r = write 2 tbytes p sub len tbytes p
11500if le r 0
11501exit 70
11502end
11503p = add p ulong r
11504end
11505end