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-rows.om

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