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 / pack / round40.om

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