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 / gpu / amd / gfx942 / fp8gemv / check.om

640 lines · 11201 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note Two references. plain is columns 0..K-1, one accumulator, no lanes, tiles
3note or DPP: s = fma(decode(W[col]), X[col], s), then one per-row scale.
4note gemv is the kernel order. Lane L owns k+L*16..+15. Tiles are 1024 columns,
5note issued in pairs when 2048 remain. Then row_shr 8, 4, 2, 1. ISA 13.3.9:
6note ROW_SR reads lane n-k, and BC=0 does not write a lane whose source is
7note outside the row of 16, so the row sum sits in lanes 15, 31, 47 and 63.
8note The wave result is (lane15+lane31)+(lane47+lane63), then the per-row scale.
9note Both then apply the epilogue and the bf16 round inside the function, which
10note is where the device does them.
11note K<=16 uses only lane 0, in column order, and every other lane adds +0.
12note That is the same association as plain, so the results are bit-exact,
13note epilogue and bf16 included. A NaN product is the same on both: FNUZ NaN
14note is only the byte 0x80 (ISA 7.2 Table 30). 0xFF is -240, not a NaN.
15note fpfma turns any NaN into the canonical 0x7fc00000.
16note Any other K is a different association. Higham's bound on recursive
17note summation says each sum is within gamma_(n-1) of the exact sum, with
18note gamma_m = m*u/(1-m*u) and u = 2^-24. Two such sums differ by at most
19note 2*gamma_(n-1)*sum(|product|), which is under (n-1)*2^-22*sum(|product|)
20note when n*u < 1/2. allow() accepts
21note |yk-yp| <= 2*(K+1)*2^-23*sum(|product|)*|scale|*|inv|*|gamma|
22note plus 8 ulps of |yk|+|yp|. The leading 2 is there because that expression
23note is evaluated in binary32 and must not round down through a difference
24note the estimate allows. Bit-exact is required only for the same association.
25note The seeded inputs and the cross-lane cancellation must not be bit-exact.
26note Per-block scale is reference-only. plain multiplies every column by
27note scale[col>>7]. gemv sums 16 products, multiplies once, then adds. The
28note K=256 ones case is bit-exact because every partial sum is an integer
29note inside the 24-bit mantissa. The device kernel does not do per-block.
30note OCP E4M3 is a reference input only.
31global W uchar 8192
32global X ulong 4096
33global SC ulong 64
34global AC ulong 64
35global AC2 ulong 64
36global CFG ulong 4
37global FUSE ulong 1
38global BF ulong 1
39global RES ulong 1
40global INV ulong 1
41global GAM ulong 1
42global SUM ulong 1
43global RNG ulong 1
44global HIT uchar 32
45text pass "fp8gemv check pass\n"
46
47fn fnuzden mant ulong to ulong
48if eq mant 1
49ret 0x3a800000
50end
51if eq mant 2
52ret 0x3b000000
53end
54if eq mant 3
55ret 0x3b400000
56end
57if eq mant 4
58ret 0x3b800000
59end
60if eq mant 5
61ret 0x3ba00000
62end
63if eq mant 6
64ret 0x3bc00000
65end
66ret 0x3be00000
67end
68
69fn fnuz b ulong to ulong
70if eq b 128
71ret 0x7fc00000
72end
73sign = shl and b 128 24
74mag = and b 127
75if eq mag 0
76ret sign
77end
78exp = shr mag 3
79mant = and mag 7
80if eq exp 0
81d = fnuzden mant
82ret or sign d
83end
84ret or sign or shl add exp 119 23 shl mant 20
85end
86
87fn ocpden mant ulong to ulong
88if eq mant 1
89ret 0x3b000000
90end
91if eq mant 2
92ret 0x3b800000
93end
94if eq mant 3
95ret 0x3bc00000
96end
97if eq mant 4
98ret 0x3c000000
99end
100if eq mant 5
101ret 0x3c200000
102end
103if eq mant 6
104ret 0x3c400000
105end
106ret 0x3c600000
107end
108
109fn ocp b ulong to ulong
110mag = and b 127
111if eq mag 127
112ret 0x7fc00000
113end
114sign = shl and b 128 24
115if eq mag 0
116ret sign
117end
118exp = shr mag 3
119mant = and mag 7
120if eq exp 0
121d = ocpden mant
122ret or sign d
123end
124ret or sign or shl add exp 120 23 shl mant 20
125end
126
127fn dec b ulong to ulong
128if eq get CFG 2 1
129ret ocp b
130end
131ret fnuz b
132end
133
134fn bf16 x ulong to ulong
135lsb = and shr x 16 1
136sum = add x add 32767 lsb
137ret and shr sum 16 65535
138end
139
140fn dppstep n ulong to void
141for i ulong 64
142v = get AC i
143if ge i n
144s = sub i n
145if eq div s 16 div i 16
146v = fpfma v 0x3f800000 get AC s
147end
148end
149put AC2 i v
150end
151for j ulong 64
152put AC j get AC2 j
153end
154end
155
156fn wavesum to ulong
157dppstep 8
158dppstep 4
159dppstep 2
160dppstep 1
161a = fpfma get AC 15 0x3f800000 get AC 31
162b = fpfma get AC 47 0x3f800000 get AC 63
163ret fpfma a 0x3f800000 b
164end
165
166fn grouprow lane ulong base ulong to void
167for b ulong 16
168col = add base add mul lane 16 b
169if lt col get CFG 1
170bits = dec ulong get W add mul get CFG 0 get CFG 1 col
171put AC lane fpfma bits get X col get AC lane
172end
173end
174end
175
176fn groupblk lane ulong base ulong to void
177tmp = ulong 0
178for b ulong 16
179col = add base add mul lane 16 b
180bits = dec ulong get W add mul get CFG 0 get CFG 1 col
181tmp = fpfma bits get X col tmp
182end
183col0 = add base mul lane 16
184sc = get SC shr col0 7
185put AC lane fpfma tmp sc get AC lane
186end
187
188fn tail k ulong to void
189for b ulong 16
190for lane ulong 64
191col = add k add mul lane 16 b
192if lt col get CFG 1
193bits = dec ulong get W add mul get CFG 0 get CFG 1 col
194if eq get CFG 3 0
195put AC lane fpfma bits get X col get AC lane
196else
197p = fpfma bits get X col 0
198sc = get SC shr col 7
199put AC lane fpfma p sc get AC lane
200end
201end
202end
203end
204end
205
206fn applytiles k ulong to ulong
207kk = get CFG 1
208while le add k 2048 kk bound 16
209for lane ulong 64
210if eq get CFG 3 0
211grouprow lane k
212grouprow lane add k 1024
213else
214groupblk lane k
215groupblk lane add k 1024
216end
217end
218k = add k 2048
219end
220if le add k 1024 kk
221for lane ulong 64
222if eq get CFG 3 0
223grouprow lane k
224else
225groupblk lane k
226end
227end
228k = add k 1024
229end
230ret k
231end
232
233fn i2f n ulong to ulong
234if eq n 0
235ret 0
236end
237exp = ulong 0
238t = n
239while ge t 2 bound 24
240t = shr t 1
241exp = add exp 1
242end
243frac = sub n shl 1 exp
244shift = sub 23 exp
245mant = shl frac shift
246expon = add exp 127
247bits = or shl expon 23 mant
248ret bits
249end
250
251fn epilogue s ulong to ulong
252r = s
253if eq get FUSE 0 1
254r = fpfma r 0x3f800000 get RES 0
255r = fpfma r get INV 0 0
256r = fpfma r get GAM 0 0
257end
258if eq get BF 0 1
259r = bf16 r
260end
261ret r
262end
263
264fn gemv to ulong
265for i ulong 64
266put AC i 0
267end
268k = applytiles 0
269tail k
270s = wavesum
271if eq get CFG 3 0
272s = fpfma s get SC get CFG 0 0
273end
274s = epilogue s
275ret s
276end
277
278fn plain to ulong
279row = get CFG 0
280kk = get CFG 1
281blk = get CFG 3
282base = mul row kk
283s = ulong 0
284ab = ulong 0
285for c ulong 8192
286if lt c kk
287bits = dec ulong get W add base c
288xv = get X c
289prod = fpfma bits xv 0
290ab = fpfma ab 0x3f800000 and prod 0x7fffffff
291scb = get SC 0
292if eq blk 1
293scb = get SC shr c 7
294end
295srow = fpfma bits xv s
296sblk = fpfma prod scb s
297s = srow
298if eq blk 1
299s = sblk
300end
301end
302end
303put SUM 0 ab
304if eq blk 0
305s = fpfma s get SC row 0
306end
307s = epilogue s
308ret s
309end
310
311fn setcfg row ulong kk ulong mode ulong blk ulong to void
312put CFG 0 row
313put CFG 1 kk
314put CFG 2 mode
315put CFG 3 blk
316put FUSE 0 0
317put BF 0 0
318put RES 0 0
319put INV 0 0x3f800000
320put GAM 0 0x3f800000
321end
322
323fn allow to ulong
324ab = get SUM 0
325k1 = i2f add get CFG 1 1
326b = fpfma ab k1 0
327b = fpfma b 0x34000000 0
328sc = and get SC get CFG 0 0x7fffffff
329b = fpfma b sc 0
330if eq get FUSE 0 1
331b = fpfma b and get INV 0 0x7fffffff 0
332b = fpfma b and get GAM 0 0x7fffffff 0
333end
334b = fpfma b 0x40000000 0
335ret b
336end
337
338fn order code int to void
339g = gemv
340p = plain
341if eq g p
342exit code
343end
344ga = and g 0x7fffffff
345pa = and p 0x7fffffff
346bad = add code 1
347if ge ga 0x7f800000
348exit bad
349end
350if ge pa 0x7f800000
351exit bad
352end
353diff = and fpfma g 0x3f800000 xor p 0x80000000 0x7fffffff
354b = allow
355span = fpfma ga 0x3f800000 pa
356slack = fpfma span 0x35800000 0
357b = fpfma b 0x3f800000 slack
358if gt diff b
359exit bad
360end
361end
362
363fn xorshift to ulong
364x = get RNG 0
365x = xor x shl x 13
366x = xor x shr x 7
367x = xor x shl x 17
368put RNG 0 x
369ret x
370end
371
372fn fill seed ulong kk ulong to void
373put RNG 0 seed
374for c ulong 8192
375if lt c kk
376a = xorshift
377b = and a 255
378if eq b 128
379b = 127
380end
381put W c uchar b
382e = add 120 and a 15
383m = and shr a 8 8388607
384sgn = shl and shr a 40 1 31
385put X c or sgn or shl e 23 m
386end
387end
388put SC 0 0x3f800000
389end
390
391fn cancel to void
392for i ulong 32
393put W i uchar 0
394put X i 0
395end
396put W 0 uchar 64
397put X 0 0x49800000
398put W 1 uchar 64
399put X 1 0x3a800000
400put W 16 uchar 192
401put X 16 0x49800000
402put W 17 uchar 64
403put X 17 0x3a800000
404put SC 0 0x3f800000
405end
406
407fn ones n ulong wb ulong to void
408for i ulong 4096
409if lt i n
410put W i uchar wb
411put X i 0x3f800000
412end
413end
414put SC 0 0x3f800000
415end
416
417fn swrow id ulong m ulong to ulong
418span = shr add m 7 3
419ret add mul and id 7 span shr id 3
420end
421
422fn swcheck m ulong code int to void
423span = shr add m 7 3
424n = mul 8 span
425for i ulong 32
426put HIT i 0
427end
428for i ulong 64
429if lt i n
430r = swrow i m
431if lt r m
432put HIT r add get HIT r 1
433end
434end
435end
436for i ulong 32
437if lt i m
438if ne get HIT i 1
439exit code
440end
441end
442end
443end
444
445fn expect got ulong want ulong code int to void
446if ne got want
447exit code
448end
449end
450
451fn same want ulong code int to void
452g = gemv
453expect g want code
454p = plain
455expect p g code
456end
457
458fn main to void
459z = fnuz 0
460expect z 0 20
461z = fnuz 64
462expect z 0x3f800000 21
463z = fnuz 128
464expect z 0x7fc00000 22
465z = fnuz 1
466expect z 0x3a800000 23
467z = fnuz 192
468expect z 0xbf800000 24
469z = fnuz 127
470expect z 0x43700000 25
471z = ocp 56
472expect z 0x3f800000 26
473z = ocp 64
474expect z 0x40000000 27
475z = ocp 1
476expect z 0x3b000000 28
477z = ocp 127
478expect z 0x7fc00000 29
479z = ocp 128
480expect z 0x80000000 30
481z = ocp 0
482expect z 0 31
483z = bf16 0x3f800000
484expect z 0x3f80 32
485z = bf16 0x3f808000
486expect z 0x3f80 33
487z = bf16 0x3f818000
488expect z 0x3f82 34
489z = i2f 1
490expect z 0x3f800000 70
491z = i2f 17
492expect z 0x41880000 71
493z = i2f 1026
494expect z 0x44804000 72
495ones 0 64
496setcfg 0 0 0 0
497same 0 1
498ones 1 64
499setcfg 0 1 0 0
500same 0x3f800000 2
501ones 16 64
502setcfg 0 16 0 0
503same 0x41800000 3
504ones 17 64
505setcfg 0 17 0 0
506same 0x41880000 4
507put W 0 64
508put W 1 192
509put W 2 64
510put X 0 0x4b000000
511put X 1 0x4b000000
512put X 2 0x3f800000
513put SC 0 0x3f800000
514setcfg 0 3 0 0
515same 0x3f800000 5
516ones 1 64
517put W 0 1
518setcfg 0 1 0 0
519same 0x3a800000 6
520ones 1 64
521put W 0 128
522setcfg 0 1 0 0
523same 0x7fc00000 7
524ones 1024 64
525setcfg 0 1024 0 0
526same 0x44800000 8
527ones 1025 64
528setcfg 0 1025 0 0
529same 0x44802000 9
530ones 2048 64
531setcfg 0 2048 0 0
532same 0x45000000 10
533ones 3072 64
534setcfg 0 3072 0 0
535same 0x45400000 11
536ones 1 64
537setcfg 0 1 0 0
538put FUSE 0 1
539put RES 0 0x3f800000
540put INV 0 0x3f000000
541put GAM 0 0x40000000
542same 0x40000000 12
543swcheck 10 13
544swcheck 16 14
545ones 256 64
546put SC 0 0x40000000
547put SC 1 0x40400000
548setcfg 0 256 0 1
549same 0x44200000 15
550put W 0 0
551put W 1 64
552put X 0 0x3f800000
553put SC 0 0x3f800000
554put SC 1 0x40000000
555setcfg 1 1 0 0
556same 0x40000000 16
557ones 1 64
558setcfg 0 1 1 0
559same 0x40000000 17
560ones 1 64
561put W 0 uchar 255
562setcfg 0 1 0 0
563same 0xc3700000 36
564ones 1 64
565put W 0 uchar 129
566setcfg 0 1 0 0
567same 0xba800000 37
568ones 1 64
569put W 0 uchar 192
570setcfg 0 1 0 0
571same 0xbf800000 38
572ones 1 64
573put W 0 uchar 127
574put X 0 0x7c800000
575setcfg 0 1 0 0
576same 0x7f800000 39
577ones 16 127
578setcfg 0 16 0 0
579same 0x45700000 43
580ones 20 64
581put W 3 uchar 128
582setcfg 0 20 0 0
583same 0x7fc00000 44
584cancel
585setcfg 0 32 0 0
586g = gemv
587expect g 0 45
588p = plain
589expect p 0x3a800000 46
590cancel
591setcfg 0 32 0 0
592put FUSE 0 1
593put RES 0 0x3f800000
594put INV 0 0x40000000
595put GAM 0 0x40000000
596g = gemv
597expect g 0x40800000 47
598p = plain
599expect p 0x40802000 48
600ones 1 64
601setcfg 0 1 0 0
602put BF 0 1
603same 0x3f80 49
604ones 1 64
605put SC 0 0x3f808000
606setcfg 0 1 0 0
607put BF 0 1
608same 0x3f80 52
609ones 1 64
610put SC 0 0x3f818000
611setcfg 0 1 0 0
612put BF 0 1
613same 0x3f82 53
614ones 1 64
615setcfg 0 1 0 0
616put FUSE 0 1
617put RES 0 0x3f800000
618put INV 0 0x3f000000
619put GAM 0 0x40000000
620put BF 0 1
621same 0x4000 54
622cancel
623setcfg 0 32 0 0
624put BF 0 1
625g = gemv
626expect g 0 55
627p = plain
628expect p 0x3a80 56
629setcfg 0 32 0 0
630fill 1 32
631order 57
632setcfg 0 1025 0 0
633fill 7 1025
634order 59
635nw = write 1 pass 0 len pass
636if lt nw 0
637exit 18
638end
639exit 0
640end