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 / fast / server-cache.om

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