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

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