interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / kflow / kemu.om

1370 lines · 26630 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/kflow/kemu.om: kflow's host emulation stage. A SASS interpreter for the instruction
3note subset the M1 kernels use, written in seeded Omega (program mode, Linux AArch64).
4note
5note Run: kemu ARCH BIN HARNESS SEED
6note ARCH 80 or 121 (sm_80 / sm_121)
7note BIN raw SASS text, 16 bytes per instruction, at most 65536 bytes
8note HARNESS vadd (c[i] = a[i] + b[i] as u32, i < n = 1000, 8 CTAs of 128 threads; the
9note vecadd ABI: a 0x380, b 0x388, c 0x390, n 0x398) or radd (x[i] += a[i] as f32,
10note 2048 values, 1 CTA of 256 threads; the Qwen3 decode RES ADD ABI: x 0x380,
11note a 0x388, five unused parameters through 0x3ac) or tmm1 (D = A x B, 16x16x16, A and
12note B f16 row-major, D f32 row-major, 1 CTA of 32 threads; A 0x380, B 0x388, D 0x390) or
13note tmmt (the tmm1 reference, but the arena holds B transposed: a negative control) or
14note tsmx (softmax of each 32-value f32 row, 16 CTAs of 32 threads, row = CTA; X 0x380,
15note Y 0x388)
16note SEED decimal splitmix64 seed for the inputs
17note
18note The decoder reads fixed field positions of the Volta+ 128-bit encoding (opcode bits 0..11, guard
19note 12..15, destination 16..23, first source 24..31, second source word 1, third source and modifiers
20note word 2), not the asm2 tables, so a wrong table base that still decodes is a different program
21note here. Each modifier the semantics below do not implement is refused (UNSUPPORTED), never
22note ignored, except memory ordering and cache hints, which do not change single-thread results.
23note Each CTA runs warp after warp. The 32 lanes of a warp step in lockstep with their own registers
24note and predicates (uniform registers are per warp); lanes leave at EXIT; lanes that reach different
25note pcs stop the run as DIVERGED (no reconvergence model). HMMA and SHFL act on the whole warp and
26note are refused unless all 32 lanes are running with one guard value. Control words (stall,
27note scoreboards) are not modelled: a schedule that is wrong on silicon can still MATCH here.
28note The arena is 256 KiB filled with 0xa5. The expected arena is the input arena with the harness
29note reference applied (vadd/radd exactly: u32 add; f32 add on the AArch64 FPU, round to nearest
30note even, no flush, finite normal inputs). Every arena byte is compared, so a stray store fails as
31note well as a wrong value; tmm1/tmmt/tsmx compare their output region as f32 against an f64
32note reference within a stated bound (fcmp) and every other byte exactly.
33note Exit: 0 MATCH, 1 MISMATCH, 2 UNSUPPORTED (incomplete model, DIVERGED), 3 usage, unreadable
34note input, a fault (address outside the arena) or a runaway thread. stdout: one line of key=value words.
35om2
36text msg "kemu: usage: kemu ARCH BIN HARNESS SEED\nkemu: arch=\n harness=\n threads=\n insts=\n result=MATCH\n result=MISMATCH arena_offset=\n expected=\n got=\n result=UNSUPPORTED op=\n pc=\n result=FAULT address=\n result=RUNAWAY pc=\nkemu: bin unreadable, empty or not a multiple of 16 bytes\nkemu: unknown harness or arch\n w1=\n w2=\n result=DIVERGED pc=\n tol_used_ppm=\n"
37text hn "vaddraddtmm1tmmttsmx"
38global code uchar 65536
39global gm uchar 262144
40global ex uchar 262144
41global cb uchar 65536
42global rr uint 8192
43global ur uint 256
44global pr uchar 256
45global ls uchar 32
46global sh uint 32
47global ma uint 256
48global mb uint 128
49global mc uint 128
50global fr ulong 2
51global rf f64 2048
52global tl f64 2048
53global sv ulong 16
54global ab uchar 256
55global dig uchar 32
56global bad uint 4
57note sv: 0 arch, 1 ntid.x, 2 ctaid.x, 3 tid.x, 4 URZ index, 5 thread state (0 run, 1 exit,
58note 2 unsupported, 3 fault, 4 runaway, 5 diverged), 6 instructions, 7 rng state, 8 code bytes,
59note 9 fault address, 10 pc of the stopping instruction, 11 CTAs, 12 lane, 13 lane register base,
60note 14 lane predicate base, 15 warp. bad: w0 w1 w2 of an unsupported instruction. ls: lane state.
61note fr: arena offset and count of the f32 region compared within tolerance (rf value, tl bound).
62const GB ulong 0x100000000000
63note ---- output ----
64fn say id ulong to void
65k = ulong 1
66s = ulong 0
67for i ulong len msg
68if eq get msg i 10
69if eq k id
70r = write 1 msg s sub i s
71ret
72end
73k = add k 1
74s = add i 1
75end
76end
77exit 99
78end
79fn newline to void
80put dig 0 10
81r = write 1 dig 0 1
82end
83fn num v ulong to void
84x = v
85n = ulong 0
86while ge x 10 bound 64
87put dig n uchar add 48 rem x 10
88x = div x 10
89n = add n 1
90end
91put dig n uchar add 48 x
92n = add n 1
93for i ulong n
94r = write 1 dig sub sub n i 1 1
95end
96end
97fn hexn v ulong to void
98put dig 0 48
99put dig 1 120
100r = write 1 dig 0 2
101s = ulong 60
102z = true
103for i ulong 16
104d = and shr v s 15
105if or ne d 0 or not z eq s 0
106z = false
107c = uchar add 48 d
108if ge d 10
109c = uchar add 87 d
110end
111put dig 0 c
112r = write 1 dig 0 1
113end
114if ne s 0
115s = sub s 4
116end
117end
118end
119note ---- random inputs (splitmix64) ----
120fn rnd to ulong
121s = add get sv 7 11400714819323198485
122put sv 7 s
123z = mul xor s shr s 30 13787848793156543929
124z = mul xor z shr z 27 10723151780598845931
125ret xor z shr z 31
126end
127note a finite normal f32: exponent field 100..163, any sign and fraction
128fn rndf to uint
129v = rnd
130e = add and shr v 23 63 100
131ret uint or or and v 0x807fffff shl e 23 0
132end
133note ---- registers (rr and pr hold 32 lanes; sv 13 and sv 14 select the current lane) ----
134fn rg r uint to uint
135if eq r 255
136ret 0
137end
138ret get rr add ulong r get sv 13
139end
140fn wr r uint v uint to void
141if ne r 255
142put rr add ulong r get sv 13 v
143end
144end
145fn ug r uint to uint
146if eq ulong r get sv 4
147ret 0
148end
149ret get ur r
150end
151fn wu r uint v uint to void
152if ne ulong r get sv 4
153put ur r v
154end
155end
156fn pg g uint to bool
157i = and g 7
158v = true
159if ne i 7
160v = ne get pr add ulong i get sv 14 0
161end
162if ne and g 8 0
163ret not v
164end
165ret v
166end
167note ---- memory ----
168fn cw pc ulong k ulong to uint
169o = add pc mul k 4
170v = uint get code o
171v = or v shl uint get code add o 1 8
172v = or v shl uint get code add o 2 16
173ret or v shl uint get code add o 3 24
174end
175fn cbw o ulong to uint
176v = uint get cb o
177v = or v shl uint get cb add o 1 8
178v = or v shl uint get cb add o 2 16
179ret or v shl uint get cb add o 3 24
180end
181fn cbput o ulong v uint to void
182put cb o uchar and v 255
183put cb add o 1 uchar and shr v 8 255
184put cb add o 2 uchar and shr v 16 255
185put cb add o 3 uchar shr v 24
186end
187fn gmw o ulong v uint to void
188put gm o uchar and v 255
189put gm add o 1 uchar and shr v 8 255
190put gm add o 2 uchar and shr v 16 255
191put gm add o 3 uchar shr v 24
192end
193fn exw o ulong v uint to void
194put ex o uchar and v 255
195put ex add o 1 uchar and shr v 8 255
196put ex add o 2 uchar and shr v 16 255
197put ex add o 3 uchar shr v 24
198end
199fn gmr o ulong to uint
200v = uint get gm o
201v = or v shl uint get gm add o 1 8
202v = or v shl uint get gm add o 2 16
203ret or v shl uint get gm add o 3 24
204end
205fn exr o ulong to uint
206v = uint get ex o
207v = or v shl uint get ex add o 1 8
208v = or v shl uint get ex add o 2 16
209ret or v shl uint get ex add o 3 24
210end
211note arena offset of n bytes at address a; sets the fault state when outside
212fn madr a ulong n ulong to ulong
213if or lt a GB gt add sub a GB n len gm
214put sv 9 a
215put sv 5 3
216ret 0
217end
218ret sub a GB
219end
220note a 64-bit register pair (RZ pair reads 0)
221fn rp r uint to ulong
222if eq r 255
223ret 0
224end
225ret or ulong rg r shl ulong rg add r 1 32
226end
227fn wp r uint v ulong to void
228wr r uint and v 0xffffffff
229if ne r 255
230wr add r 1 uint shr v 32
231end
232end
233fn unsup w0 uint w1 uint w2 uint to void
234put bad 0 w0
235put bad 1 w1
236put bad 2 w2
237put sv 5 2
238end
239note ---- second source by form (bits 9..11): 1 register, 2/4 number, 6/7 uniform register ----
240fn bsrc f uint w1 uint to uint
241if eq f 1
242ret rg and w1 255
243end
244if or eq f 2 eq f 4
245ret w1
246end
247u = and w1 255
248if eq get sv 0 80
249u = and w1 63
250end
251ret ug u
252end
253fn formok f uint to bool
254ret or or eq f 1 or eq f 2 eq f 4 or eq f 6 eq f 7
255end
256fn lut3 a uint b uint c uint l uint to uint
257r = uint 0
258for k uint 8
259if ne and shr l k 1 0
260t = not uint 0
261if ne and k 4 0
262t = and t a
263else
264t = and t not a
265end
266if ne and k 2 0
267t = and t b
268else
269t = and t not b
270end
271if ne and k 1 0
272t = and t c
273else
274t = and t not c
275end
276r = or r t
277end
278end
279ret r
280end
281fn icmp x0 uint y0 uint cm uint sg uint to bool
282x = x0
283y = y0
284if ne sg 0
285x = xor x 0x80000000
286y = xor y 0x80000000
287end
288if eq cm 1
289ret lt x y
290end
291if eq cm 2
292ret eq x y
293end
294if eq cm 3
295ret le x y
296end
297if eq cm 4
298ret gt x y
299end
300if eq cm 5
301ret ne x y
302end
303if eq cm 6
304ret ge x y
305end
306ret eq cm 7
307end
308fn isalu op uint to bool
309if or or eq op 0x24 eq op 0x25 or eq op 0x0c eq op 0x12
310ret true
311end
312ret or or eq op 0x19 eq op 0x10 or eq op 0x21 eq op 0x02
313end
314note ---- integer and float ALU; false when the base opcode is not one of these ----
315fn alu w0 uint w1 uint w2 uint to bool
316op = and w0 0x1ff
317f = shr and w0 0xfff 9
318d = and shr w0 16 255
319a = rg and shr w0 24 255
320if not isalu op
321ret false
322end
323if not formok f
324unsup w0 w1 w2
325ret true
326end
327b = bsrc f w1
328c = rg and w2 255
329if eq op 0x02
330wr d b
331ret true
332end
333if eq op 0x24
334wr d add mul a b c
335ret true
336end
337if eq op 0x25
338imadw w0 w2 a b
339ret true
340end
341if eq op 0x10
342if or not modok f w1 w2 0x80000000 0x900 ne and shr w2 17 63 63
343unsup w0 w1 w2
344ret true
345end
346wr d add add a b c
347ret true
348end
349if eq op 0x12
350wr d lut3 a b c and shr w2 8 255
351ret true
352end
353if eq op 0x19
354shf w0 w1 w2 b
355ret true
356end
357if eq op 0x0c
358isetp w0 w1 w2 a b
359ret true
360end
361if not modok f w1 w2 0x40000000 0x1e300
362unsup w0 w1 w2
363ret true
364end
365if and eq f 1 ne and w1 0x80000000 0
366b = xor b 0x80000000
367end
368wr d bitcast uint add bitcast f32 a bitcast f32 b
369ret true
370end
371note no word-2 modifier bit in m2, and (register form) no word-1 negate/abs bit in m1
372fn modok f uint w1 uint w2 uint m1 uint m2 uint to bool
373if ne and w2 m2 0
374ret false
375end
376if eq f 1
377ret eq and w1 m1 0
378end
379ret true
380end
381fn imadw w0 uint w2 uint a uint b uint to void
382d = and shr w0 16 255
383x = ulong a
384y = ulong b
385if ne and w2 0x200 0
386if ne and a 0x80000000 0
387x = or x 0xffffffff00000000
388end
389if ne and b 0x80000000 0
390y = or y 0xffffffff00000000
391end
392end
393wp d add mul x y rp and w2 255
394end
395note SHF: only the right funnel shift giving the high word of a U32 pair (type 3, bit 12, bit 16)
396fn shf w0 uint w1 uint w2 uint b uint to void
397if ne and w2 0x11600 0x11600
398unsup w0 w1 w2
399ret
400end
401s = and b 63
402if ne and w2 0x800 0
403s = and b 31
404end
405if gt s 32
406s = uint 32
407end
408v = uint 0
409if lt s 32
410v = shr rg and w2 255 s
411end
412wr and shr w0 16 255 v
413end
414note ISETP: no .EX (bit 8), set operation AND (bits 10..11 = 0)
415fn isetp w0 uint w1 uint w2 uint a uint b uint to void
416if ne and w2 0xd00 0
417unsup w0 w1 w2
418ret
419end
420r = icmp a b and shr w2 12 7 and shr w2 9 1
421r = and r pg and shr w2 23 15
422p = and shr w2 17 7
423if ne p 7
424q = add ulong p get sv 14
425put pr q uchar 0
426if r
427put pr q uchar 1
428end
429end
430end
431note S2R: TID.X 0x21, CTAID.X 0x25; Y and Z (0x22 0x23 0x26 0x27) are 0 in a 1-D launch
432fn s2r w0 uint w1 uint w2 uint to void
433sr = and shr w2 8 255
434v = uint get sv 3
435if eq sr 0x25
436v = uint get sv 2
437end
438if ne sr 0x21
439if ne sr 0x25
440if lt sr 0x22
441unsup w0 w1 w2
442ret
443end
444if and gt sr 0x23 lt sr 0x26
445unsup w0 w1 w2
446ret
447end
448if gt sr 0x27
449unsup w0 w1 w2
450ret
451end
452v = uint 0
453end
454end
455wr and shr w0 16 255 v
456end
457note ---- constant bank, global memory, control ----
458fn memop pc ulong w0 uint w1 uint w2 uint to bool
459op = and w0 0xfff
460d = and shr w0 16 255
461sz = and shr w2 9 7
462if eq op 0x918
463ret true
464end
465if eq op 0x919
466s2r w0 w1 w2
467ret true
468end
469if eq op 0xb82
470if ne shr w0 24 255
471unsup w0 w1 w2
472ret true
473end
474end
475if eq op 0xb82
476if ne and w1 0x7c00000 0
477unsup w0 w1 w2
478ret true
479end
480if not ldc 0 d and shr w1 6 0xffff sz
481unsup w0 w1 w2
482end
483ret true
484end
485if and eq op 0x7ac eq get sv 0 121
486if not ldc 1 d mul and shr w1 8 0xffff 8 sz
487unsup w0 w1 w2
488end
489ret true
490end
491if and eq op 0xab9 eq get sv 0 80
492if not ldc 1 and d 63 and shr w1 6 0xffff sz
493unsup w0 w1 w2
494end
495ret true
496end
497if or eq op 0x981 eq op 0x986
498gmem w0 w1 w2
499ret true
500end
501if eq op 0x94d
502put sv 5 1
503ret true
504end
505if eq op 0x947
506put sv 10 pc
507put sv 5 4
508ret true
509end
510ret false
511end
512fn ldc un uint d uint o uint sz uint to bool
513if or and ne sz 4 ne sz 5 gt o 0xfff8
514ret false
515end
516v = cbw ulong o
517if eq un 1
518wu d v
519if eq sz 5
520wu add d 1 cbw add ulong o 4
521end
522else
523wr d v
524if eq sz 5
525wr add d 1 cbw add ulong o 4
526end
527end
528ret true
529end
530note LDG/STG with a 64-bit address register (word 2 bit 8). sm_80 adds the uniform register in its
531note uniform-address field; sm_121's uniform field is the memory descriptor and adds nothing.
532note Size (word 2 bits 9..11): 0 U8, 1 S8, 2 U16, 3 S16 (loads only), 4 32, 5 64, 6 128 bits.
533fn gmem w0 uint w1 uint w2 uint to void
534sz = and shr w2 9 7
535st = eq and w0 0xfff 0x986
536if or or eq and w2 0x100 0 gt sz 6 and st lt sz 4
537unsup w0 w1 w2
538ret
539end
540off = ulong shr w1 8
541if ge off 0x800000
542off = or off 0xffffffffff000000
543end
544a = add rp and shr w0 24 255 off
545if eq get sv 0 80
546u = and w1 63
547if st
548u = and w2 63
549end
550a = add a ulong ug u
551end
552nb = shl ulong 1 shr sz 1
553if ge sz 4
554nb = shl ulong 4 sub sz 4
555end
556o = madr a nb
557if ne get sv 5 0
558ret
559end
560d = and shr w0 16 255
561if st
562d = and w1 255
563end
564if lt sz 4
565gsmall o d sz
566ret
567end
568xfer st o d shr nb 2
569end
570fn gsmall o ulong d uint sz uint to void
571v = uint get gm o
572if lt sz 2
573if and eq sz 1 ne and v 0x80 0
574v = or v 0xffffff00
575end
576else
577v = or v shl uint get gm add o 1 8
578if and eq sz 3 ne and v 0x8000 0
579v = or v 0xffff0000
580end
581end
582wr d v
583end
584fn xfer st bool o ulong d uint n ulong to void
585for i ulong n
586p = add o mul i 4
587r = add d uint i
588if st
589gmw p rg r
590else
591wr r gmr p
592end
593end
594end
595note ---- FMUL, FMNMX, MUFU.EX2/RCP (register forms); false when the opcode is not one of these ----
596fn fpu w0 uint w1 uint w2 uint to bool
597op = and w0 0xfff
598if and and ne op 0x220 ne op 0x209 ne op 0x308
599ret false
600end
601d = and shr w0 16 255
602a = bitcast f32 rg and shr w0 24 255
603b = bitcast f32 rg and w1 255
604if ne and w1 0xffffff00 0
605unsup w0 w1 w2
606ret true
607end
608if eq op 0x308
609mufu w0 w1 w2 b
610ret true
611end
612if eq op 0x220
613if ne w2 0x00400000
614unsup w0 w1 w2
615ret true
616end
617wr d bitcast uint mul a b
618ret true
619end
620if ne and w2 0xf87fffff 0
621unsup w0 w1 w2
622ret true
623end
624r = fmax a b
625if pg and shr w2 23 15
626r = fmin a b
627end
628wr d bitcast uint r
629ret true
630end
631note MUFU: EX2 (word 2 0x800) and RCP (0x1000) only; source register in word 1
632fn mufu w0 uint w1 uint w2 uint x f32 to void
633d = and shr w0 16 255
634if eq w2 0x800
635wr d bitcast uint ex2m x
636ret
637end
638if eq w2 0x1000
639wr d bitcast uint div 1.0f32 x
640ret
641end
642unsup w0 w1 w2
643end
644note model of MUFU.EX2: 2^x from an f64 series, rounded once to f32; below 2^-126 flushes to 0,
645note from 2^128 up gives +inf (the hardware is within 2 ulp of this; the harness bound allows it)
646fn ex2m x f32 to f32
647y = f64 x
648if lt y -126.0f64
649ret 0.0f32
650end
651if ge y 128.0f64
652ret bitcast f32 uint 0x7f800000
653end
654n = long y
655if gt f64 n y
656n = sub n 1
657end
658f = mul sub y f64 n 0.6931471805599453094f64
659s = 1.0f64
660t = 1.0f64
661for i ulong 24
662t = div mul t f f64 add i 1
663s = add s t
664end
665p = bitcast f64 shl ulong add n 1023 52
666ret f32 mul s p
667end
668note ---- warp-wide HMMA and SHFL ----
669note SHFL.BFLY / SHFL.DOWN with immediate lane and clamp 0x1f over a full warp, predicate out PT
670fn shfl w0 uint w1 uint w2 uint to void
671md = and shr w1 26 3
672k = ulong and shr w1 21 31
673x = or ne and w1 0xf01fe0ff 0 ne and w1 0x1f00 0x1f00
674if or x or lt md 2 ne w2 0x000e0000
675unsup w0 w1 w2
676ret
677end
678a = and shr w0 24 255
679for l ulong 32
680lane l
681put sh l rg a
682end
683d = and shr w0 16 255
684for l ulong 32
685s = xor l k
686if eq md 2
687s = add l k
688if gt s 31
689s = l
690end
691end
692lane l
693wr d get sh s
694end
695end
696note fragment index of accumulator register i of lane l: row g (+8 for i 2,3), column 2t + (i and 1)
697fn cidx l ulong i ulong to ulong
698r = add shr l 2 mul shr i 1 8
699n = add mul and l 3 2 and i 1
700ret add mul r 8 n
701end
702note A registers of lane l (g = l/4, t = l%4): i holds row g (+8 if i odd), columns 2t, 2t+1 (+8 if
703note i >= 2), low half first; C likewise by cidx (RZ reads 0)
704fn hga l ulong a uint c uint to void
705g = shr l 2
706t = and l 3
707for i ulong 4
708v = rg add a uint i
709r = add g mul and i 1 8
710n = add mul t 2 mul shr i 1 8
711o = add mul r 16 n
712put ma o and v 0xffff
713put ma add o 1 shr v 16
714w = uint 0
715if ne c 255
716w = rg add c uint i
717end
718put mc cidx l i w
719end
720end
721note B registers of lane l: i holds rows k = 2t, 2t+1 (+8 if i = 1) of column g, low half first
722fn hgb l ulong b uint to void
723g = shr l 2
724t = and l 3
725for i ulong 2
726v = rg add b uint i
727k = add mul t 2 mul i 8
728put mb add mul k 8 g and v 0xffff
729k = add k 1
730put mb add mul k 8 g shr v 16
731end
732end
733note model: f16/bf16 to f32 by moving bits, each product exact in f32, accumulated in f32 in k order
734fn h2f h uint bf bool to f32
735if bf
736ret bitcast f32 shl h 16
737end
738s = shl and h 0x8000 16
739e = and shr h 10 31
740f = and h 1023
741if eq e 31
742ret bitcast f32 or or s 0x7f800000 shl f 13
743end
744if eq e 0
745x = mul f32 f 5.9604644775390625e-8f32
746if ne s 0
747ret neg x
748end
749ret x
750end
751e = shl add e 112 23
752ret bitcast f32 or or s e shl f 13
753end
754fn hcomp bf bool to void
755for r ulong 16
756for n ulong 8
757acc = bitcast f32 get mc add mul r 8 n
758for k ulong 16
759x = h2f get ma add mul r 16 k bf
760y = h2f get mb add mul k 8 n bf
761acc = add acc mul x y
762end
763put mc add mul r 8 n bitcast uint acc
764end
765end
766end
767note HMMA.16816.F32 (word 2 0x1800 F16 or 0x41800 BF16 inputs): D = A x B + C, m16n8k16
768fn hmma w0 uint w1 uint w2 uint to void
769if not hmmok w0 w1 w2
770unsup w0 w1 w2
771ret
772end
773hmmx w0 w1 w2
774end
775note register pairs/quads aligned, word 1 only the B register, F32 accumulate of F16 or BF16
776fn hmmok w0 uint w1 uint w2 uint to bool
777m = and w2 0xffffff00
778if or ne and w1 0xffffff00 0 and ne m 0x1800 ne m 0x41800
779ret false
780end
781if or ne and shr w0 24 3 0 ne and w1 1 0
782ret false
783end
784if ne and shr w0 16 3 0
785ret false
786end
787c = and w2 255
788ret or eq c 255 eq and c 3 0
789end
790fn hmmx w0 uint w1 uint w2 uint to void
791m = and w2 0xffffff00
792c = and w2 255
793a = and shr w0 24 255
794b = and w1 255
795d = and shr w0 16 255
796for l ulong 32
797lane l
798hga l a c
799hgb l b
800end
801hcomp eq m 0x41800
802for l ulong 32
803lane l
804for i ulong 4
805wr add d uint i get mc cidx l i
806end
807end
808end
809note one instruction at pc for the current lane; gives the next pc
810fn step pc ulong to ulong
811w0 = cw pc 0
812w1 = cw pc 1
813w2 = cw pc 2
814put sv 6 add get sv 6 1
815if not pg and shr w0 12 15
816ret add pc 16
817end
818if alu w0 w1 w2
819ret add pc 16
820end
821if fpu w0 w1 w2
822ret add pc 16
823end
824if memop pc w0 w1 w2
825ret add pc 16
826end
827unsup w0 w1 w2
828ret pc
829end
830note ---- one warp: 32 lanes in lockstep ----
831const ALL ulong 0xffffffffffffffff
832fn lane l ulong to void
833put sv 12 l
834put sv 13 mul l 256
835put sv 14 mul l 8
836put sv 3 add mul get sv 15 32 l
837end
838fn wclear to void
839for i ulong len rr
840put rr i 0
841end
842for i ulong 256
843put ur i 0
844put pr i 0
845end
846for l ulong 32
847put pr add mul l 8 7 1
848put ls l 0
849end
850end
851fn nrun to ulong
852n = ulong 0
853for l ulong 32
854if eq get ls l 0
855n = add n 1
856end
857end
858ret n
859end
860note HMMA and SHFL: false when the instruction at pc is neither; refused unless the warp is full
861note and its guard is uniform
862fn coll pc ulong to bool
863w0 = cw pc 0
864op = and w0 0xfff
865if and ne op 0x23c ne op 0xf89
866ret false
867end
868w1 = cw pc 1
869w2 = cw pc 2
870put sv 6 add get sv 6 nrun
871if ne nrun 32
872unsup w0 w1 w2
873ret true
874end
875g = and shr w0 12 15
876lane 0
877v = pg g
878for l ulong 32
879lane l
880x = pg g
881if or and x not v and v not x
882unsup w0 w1 w2
883ret true
884end
885end
886if not v
887ret true
888end
889if eq op 0x23c
890hmma w0 w1 w2
891else
892shfl w0 w1 w2
893end
894ret true
895end
896note every running lane executes pc; gives their common next pc, ALL when none is left running,
897note sub ALL 1 when the warp stops (state in sv 5)
898fn wstep pc ulong to ulong
899q = ALL
900for l ulong 32
901if eq get ls l 0
902lane l
903put sv 5 0
904x = step pc
905s = get sv 5
906if gt s 1
907ret sub ALL 1
908end
909put ls l uchar s
910if eq s 0
911if eq q ALL
912q = x
913end
914if ne q x
915put sv 5 5
916ret sub ALL 1
917end
918end
919end
920end
921put sv 5 0
922ret q
923end
924fn warp to void
925wclear
926pc = ulong 0
927for k ulong 200000
928if eq nrun 0
929put sv 5 1
930ret
931end
932put sv 10 pc
933put sv 5 0
934if ge pc get sv 8
935put sv 5 4
936ret
937end
938q = add pc 16
939if not coll pc
940q = wstep pc
941end
942if ne get sv 5 0
943ret
944end
945pc = q
946end
947put sv 5 4
948end
949note ---- harnesses ----
950fn arena to void
951for i ulong len gm
952put gm i 165
953end
954for i ulong len cb
955put cb i 0
956end
957end
958fn cbp o ulong v ulong to void
959cbput o uint and v 0xffffffff
960cbput add o 4 uint shr v 32
961end
962fn hvadd to void
963cbput 0x360 128
964cbput 0x364 1
965cbput 0x368 1
966cbput 0x37c 0xfffc
967cbp 0x380 add GB 0x10000
968cbp 0x388 add GB 0x20000
969cbp 0x390 add GB 0x30000
970cbput 0x398 1000
971for i ulong 1000
972gmw add 0x10000 mul i 4 uint and rnd 0xffffffff
973gmw add 0x20000 mul i 4 uint and rnd 0xffffffff
974end
975for i ulong len gm
976put ex i get gm i
977end
978for i ulong 1000
979exw add 0x30000 mul i 4 add gmr add 0x10000 mul i 4 gmr add 0x20000 mul i 4
980end
981put sv 1 128
982put sv 11 8
983end
984fn hradd to void
985cbput 0x360 256
986cbput 0x364 1
987cbput 0x368 1
988cbp 0x380 add GB 0x10000
989cbp 0x388 add GB 0x20000
990for i ulong 2048
991gmw add 0x10000 mul i 4 rndf
992gmw add 0x20000 mul i 4 rndf
993end
994for i ulong len gm
995put ex i get gm i
996end
997for i ulong 2048
998x = bitcast f32 gmr add 0x10000 mul i 4
999y = bitcast f32 gmr add 0x20000 mul i 4
1000exw add 0x10000 mul i 4 bitcast uint add x y
1001end
1002put sv 1 256
1003put sv 11 1
1004end
1005note ---- tile harnesses: an f64 reference by a different route, and a stated bound ----
1006note a normal f16: exponent field 10..20 (2^-5..2^5), any sign and fraction
1007fn rnh to uint
1008v = rnd
1009e = add rem and shr v 16 255 11 10
1010ret uint or and v 0x83ff shl e 10
1011end
1012fn gmh o ulong v uint to void
1013put gm o uchar and v 255
1014put gm add o 1 uchar shr v 8
1015end
1016fn gmhr o ulong to uint
1017ret or uint get gm o shl uint get gm add o 1 8
1018end
1019note an f16 value as f64 by arithmetic on its fields (not the HMMA model's bit moves)
1020fn h64 h uint to f64
1021m = f64 and h 1023
1022e = and shr h 10 31
1023if ne e 0
1024m = add m 1024.0f64
1025else
1026e = uint 1
1027end
1028for i uint e
1029m = mul m 2.0f64
1030end
1031m = mul m 2.98023223876953125e-8f64
1032if ne and h 0x8000 0
1033ret neg m
1034end
1035ret m
1036end
1037note D[r][n] = sum over k of A[r][k] B[k][n] in f64; bound gamma_15 (u = 2^-23) times sum |A B|,
1038note rounded up to 1966085 * 2^-40 (Wolfram, see seed/kflow/README.md)
1039fn mmcell r ulong n ulong to void
1040s = 0.0f64
1041z = 0.0f64
1042for k ulong 16
1043a = h64 gmhr add 0x10000 mul add mul r 16 k 2
1044b = h64 gmhr add 0x20000 mul add mul k 16 n 2
1045s = add s mul a b
1046z = add z fabs mul a b
1047end
1048i = add mul r 16 n
1049put rf i s
1050q = div 1966085.0f64 1099511627776.0f64
1051put tl i mul z q
1052exw add 0x30000 mul i 4 bitcast uint f32 s
1053end
1054note B transposed in place (gm and the expected arena): the tmmt negative control
1055fn btr to void
1056for i ulong 256
1057put ma i gmhr add 0x20000 mul i 2
1058end
1059for k ulong 16
1060for n ulong 16
1061o = add 0x20000 mul add mul k 16 n 2
1062gmh o get ma add mul n 16 k
1063put ex o get gm o
1064put ex add o 1 get gm add o 1
1065end
1066end
1067end
1068fn hmm tr bool to void
1069cbput 0x360 32
1070cbput 0x364 1
1071cbput 0x368 1
1072cbput 0x37c 0xfffc
1073cbp 0x380 add GB 0x10000
1074cbp 0x388 add GB 0x20000
1075cbp 0x390 add GB 0x30000
1076for i ulong 256
1077gmh add 0x10000 mul i 2 rnh
1078gmh add 0x20000 mul i 2 rnh
1079end
1080for i ulong len gm
1081put ex i get gm i
1082end
1083for r ulong 16
1084for n ulong 16
1085mmcell r n
1086end
1087end
1088if tr
1089btr
1090end
1091put fr 0 0x30000
1092put fr 1 256
1093put sv 1 32
1094put sv 11 1
1095end
1096note e^z for z in [-8, 0]: series for e^(z/16), squared four times (no ln 2, no log2 e)
1097fn expr z f64 to f64
1098y = mul z 0.0625f64
1099s = 1.0f64
1100t = 1.0f64
1101for i ulong 24
1102t = div mul t y f64 add i 1
1103s = add s t
1104end
1105for i ulong 4
1106s = mul s s
1107end
1108ret s
1109end
1110fn xin i ulong to f64
1111ret f64 bitcast f32 gmr add 0x10000 mul i 4
1112end
1113note softmax of row r in f64; bound 2528617 * 2^-40 relative (Wolfram, see seed/kflow/README.md)
1114fn smrow r ulong to void
1115b = mul r 32
1116m = xin b
1117for j ulong 32
1118x = xin add b j
1119m = fmax m x
1120end
1121s = 0.0f64
1122for j ulong 32
1123x = xin add b j
1124s = add s expr sub x m
1125end
1126for j ulong 32
1127i = add b j
1128x = xin i
1129y = div expr sub x m s
1130put rf i y
1131q = div 2528617.0f64 1099511627776.0f64
1132put tl i mul y q
1133exw add 0x20000 mul i 4 bitcast uint f32 y
1134end
1135end
1136note X: multiples of 2^-17 in [-4, 4), so x - max is exact in f32
1137fn hsmx to void
1138cbput 0x360 32
1139cbput 0x364 1
1140cbput 0x368 1
1141cbput 0x37c 0xfffc
1142cbp 0x380 add GB 0x10000
1143cbp 0x388 add GB 0x20000
1144for i ulong 512
1145x = mul f32 sub long and rnd 0xfffff 524288 7.62939453125e-6f32
1146gmw add 0x10000 mul i 4 bitcast uint x
1147end
1148for i ulong len gm
1149put ex i get gm i
1150end
1151for r ulong 16
1152smrow r
1153end
1154put fr 0 0x20000
1155put fr 1 512
1156put sv 1 32
1157put sv 11 16
1158end
1159note ---- arguments ----
1160fn argdec i ulong to ulong
1161r = argread i ab 0 20
1162if lt r 1
1163ret 0xffffffffffffffff
1164end
1165v = ulong 0
1166for k ulong ulong r
1167c = get ab k
1168if or lt c 48 gt c 57
1169ret 0xffffffffffffffff
1170end
1171v = add mul v 10 ulong sub c 48
1172end
1173ret v
1174end
1175fn harness to ulong
1176r = argread 3 ab 0 8
1177if ne r 4
1178ret 0
1179end
1180for h ulong 5
1181m = true
1182for k ulong 4
1183if ne get ab k get hn add mul h 4 k
1184m = false
1185end
1186end
1187if m
1188ret add h 1
1189end
1190end
1191ret 0
1192end
1193fn loadbin to bool
1194r = argread 2 ab 0 255
1195if lt r 1
1196ret false
1197end
1198fd = fopen ab 0 ulong r 524288 0
1199if lt fd 0
1200ret false
1201end
1202n = ulong 0
1203for k ulong 4097
1204x = read int fd code n sub len code n
1205if le x 0
1206break
1207end
1208n = add n ulong x
1209if eq n len code
1210break
1211end
1212end
1213y = read int fd ab 0 1
1214c = fclose int fd
1215put sv 8 n
1216ret and and gt n 0 eq rem n 16 0 eq y 0
1217end
1218fn report to int
1219st = get sv 5
1220if eq st 2
1221say 10
1222hexn ulong and get bad 0 0xfff
1223say 11
1224hexn get sv 10
1225say 16
1226hexn ulong get bad 1
1227say 17
1228hexn ulong get bad 2
1229newline
1230ret 2
1231end
1232if eq st 3
1233say 12
1234hexn get sv 9
1235say 11
1236hexn get sv 10
1237newline
1238ret 3
1239end
1240if eq st 5
1241say 18
1242hexn get sv 10
1243newline
1244ret 2
1245end
1246if ne st 1
1247say 13
1248hexn get sv 10
1249newline
1250ret 3
1251end
1252lo = get fr 0
1253hi = add lo mul get fr 1 4
1254for i ulong len gm
1255if and ne get gm i get ex i or lt i lo ge i hi
1256say 7
1257hexn i
1258say 8
1259hexn ulong get ex i
1260say 9
1261hexn ulong get gm i
1262newline
1263ret 1
1264end
1265end
1266if ne fcmp 0
1267ret 1
1268end
1269say 6
1270newline
1271ret 0
1272end
1273note the f32 region: |got - reference| within the bound; prints the largest share of the bound used
1274fn fcmp to int
1275lo = get fr 0
1276w = 0.0f64
1277for i ulong get fr 1
1278o = add lo mul i 4
1279g = f64 bitcast f32 gmr o
1280e = fabs sub g get rf i
1281t = get tl i
1282if not le e t
1283say 7
1284hexn o
1285say 8
1286hexn ulong exr o
1287say 9
1288hexn ulong gmr o
1289newline
1290ret 1
1291end
1292q = div e t
1293w = fmax w q
1294end
1295if ne get fr 1 0
1296say 19
1297num ulong mul w 1000000.0f64
1298end
1299ret 0
1300end
1301note every CTA, warp by warp; false at the first warp that does not end by EXIT in all lanes
1302fn runall to bool
1303for c ulong get sv 11
1304put sv 2 c
1305for wi ulong div get sv 1 32
1306put sv 15 wi
1307warp
1308if ne get sv 5 1
1309ret false
1310end
1311end
1312end
1313ret true
1314end
1315fn main to int
1316if ne argcnt 5
1317say 1
1318newline
1319ret 3
1320end
1321ar = argdec 1
1322sd = argdec 4
1323h = harness
1324if or or eq h 0 eq sd 0xffffffffffffffff and ne ar 80 ne ar 121
1325say 15
1326newline
1327ret 3
1328end
1329if not loadbin
1330say 14
1331newline
1332ret 3
1333end
1334put sv 0 ar
1335put sv 4 255
1336if eq ar 80
1337put sv 4 63
1338end
1339put sv 7 sd
1340arena
1341if eq h 1
1342hvadd
1343end
1344if eq h 2
1345hradd
1346end
1347if or eq h 3 eq h 4
1348hmm eq h 4
1349end
1350if eq h 5
1351hsmx
1352end
1353say 2
1354num ar
1355say 3
1356r = write 1 hn mul sub h 1 4 4
1357if not runall
1358say 4
1359t = add mul get sv 2 get sv 1 get sv 3
1360num t
1361say 5
1362num get sv 6
1363ret report
1364end
1365say 4
1366num mul get sv 11 get sv 1
1367say 5
1368num get sv 6
1369ret report
1370end