interplane.Get Omega
Read in your language

omega-blas

aien-dev/omega-blas

Brought by drakestapleton

branch main Branches Tags History

omega-blas / om / blas.om

596 lines · 13781 bytes Raw Blame History
1note blas.om: the omega-blas compute unit. OM-2, bound through omega-bridge native ABI v2 (tools/native).
2note Matrices are column-major (Fortran BLAS); the C layer maps CBLAS row-major onto it by swapping
3note A and B. ABI v2 views are integer-only, so f32 data crosses as uint bits and is bitcast here;
4note f16 and bf16 cross as ushort, fp8 (OCP e4m3 / e5m2) as uchar. Every product is f32 x f32 with an
5note f32 accumulator: the low-precision formats are widened exactly to f32 while packing (BLIS-style
6note panels), so one kernel serves sgemm, hgemm, bf16 gemm and fp8 gemm. No FMA: one rounding per
7note operator (OMEGESE 9.9), which the tolerance contract in docs/TOLERANCE.md accounts for.
8note A loop runs at most 1000000 passes (trap 4), so each dimension is below 1000000.
9om2
10layout gp 128
11field m ulong at 0 little
12field n ulong at 8 little
13field k ulong at 16 little
14field ldc ulong at 24 little
15field offc ulong at 32 little
16field alpha uint at 40 little
17field beta uint at 44 little
18field act ulong at 48 little
19field wsa ulong at 56 little
20field wsb ulong at 64 little
21field wsbias ulong at 72 little
22field mp ulong at 80 little
23field np ulong at 88 little
24field pad ulong 4 at 96 little
25end
26layout pk 64
27field rows ulong at 0 little
28field cols ulong at 8 little
29field si ulong at 16 little
30field off ulong at 24 little
31field sq ulong at 32 little
32field w ulong at 40 little
33field dst ulong at 48 little
34field dt ulong at 56 little
35end
36layout gv 80
37field m ulong at 0 little
38field n ulong at 8 little
39field lda ulong at 16 little
40field offa ulong at 24 little
41field ta ulong at 32 little
42field alpha uint at 40 little
43field beta uint at 44 little
44field wx ulong at 48 little
45field wy ulong at 56 little
46field pad ulong 2 at 64 little
47end
48layout cp 48
49field n ulong at 0 little
50field os ulong at 8 little
51field is long at 16 little
52field od ulong at 24 little
53field id long at 32 little
54field pad ulong at 40 little
55end
56layout ck 80
57field ta long at 0 little
58field tb long at 8 little
59field m long at 16 little
60field n long at 24 little
61field k long at 32 little
62field lda long at 40 little
63field ldb long at 48 little
64field ldc long at 56 little
65field incx long at 64 little
66field incy long at 72 little
67end
68note ---- format conversions (exact widening; round-to-nearest-even narrowing) ----
69fn h2f h ushort to uint
70x = uint h
71s = shl and x 32768 16
72e = and shr x 10 31
73f = and x 1023
74if eq e 31
75ret or s or 2139095040 shl f 13
76end
77if eq e 0
78v = mul f32 f bitcast f32 uint 864026624
79ret or s bitcast uint v
80end
81ret or s or shl add e 112 23 shl f 13
82end
83fn b2f h ushort to uint
84ret shl uint h 16
85end
86fn e4f b uchar to uint
87x = uint b
88s = shl and x 128 24
89e = and shr x 3 15
90f = and x 7
91if and eq e 15 eq f 7
92ret or s 2143289344
93end
94if eq e 0
95v = mul f32 f bitcast f32 uint 989855744
96ret or s bitcast uint v
97end
98ret or s or shl add e 120 23 shl f 20
99end
100fn e5f b uchar to uint
101x = uint b
102s = shl and x 128 24
103e = and shr x 2 31
104f = and x 3
105if eq e 31
106ret or s or 2139095040 shl f 21
107end
108if eq e 0
109v = mul f32 f bitcast f32 uint 931135488
110ret or s bitcast uint v
111end
112ret or s or shl add e 112 23 shl f 21
113end
114fn f2b u uint to ushort
115if gt and u 2147483647 2139095040
116ret ushort or shr u 16 64
117end
118r = add u add 32767 and shr u 16 1
119ret ushort shr r 16
120end
121fn f2h u uint to ushort
122s = and shr u 16 32768
123a = and u 2147483647
124if gt a 2139095040
125ret ushort or s or 32256 and shr a 13 1023
126end
127if ge a 1199570944
128ret ushort or s 31744
129end
130if ge a 947912704
131r = add a add 4095 and shr a 13 1
132r = sub r 939524096
133ret ushort or s shr r 13
134end
135t = mul bitcast f32 a 16777216.0f32
136q = uint t
137fr = sub t f32 q
138if gt fr 0.5f32
139q = add q 1
140end
141if eq fr 0.5f32
142q = add q and q 1
143end
144ret ushort or s q
145end
146note Array converters (the bridge binds only ulong / long scalars, so the formats travel as views).
147note widen16: d[i] = f32 bits of s[i] as f16 (dt 0) or bf16 (dt 1); narrow16: the reverse, RNE.
148export fn widen16 dt ulong s view ushort * d edit uint * to void
149for i ulong len s
150if eq dt 1
151put d i b2f get s i
152else
153put d i h2f get s i
154end
155end
156end
157note widen8: d[i] = f32 bits of s[i] as OCP e4m3 (dt 0) or e5m2 (dt 1).
158export fn widen8 dt ulong s view uchar * d edit uint * to void
159for i ulong len s
160if eq dt 1
161put d i e5f get s i
162else
163put d i e4f get s i
164end
165end
166end
167export fn narrow16 dt ulong s view uint * d edit ushort * to void
168for i ulong len s
169if eq dt 1
170put d i f2b get s i
171else
172put d i f2h get s i
173end
174end
175end
176note ---- epilogue: alpha, beta, bias, activation (act: 0 none, 1 relu, 2 gelu-tanh; +16 bias) ----
177fn expd x f64 to f64
178h = 0.5f64
179if lt x 0.0f64
180h = -0.5f64
181end
182n = long add mul x 1.4426950408889634f64 h
183g = f64 n
184r = sub sub x mul g 0.6931471803691238f64 mul g 1.9082149292705877e-10f64
185s = 1.0f64
186t = 1.0f64
187for i ulong 13
188t = div mul t r f64 add i 1
189s = add s t
190end
191pw = bitcast f64 shl ulong add n 1023 52
192ret mul s pw
193end
194fn tanhd y f64 to f64
195if ne y y
196ret y
197end
198if gt y 20.0f64
199ret 1.0f64
200end
201if lt y -20.0f64
202ret -1.0f64
203end
204e = expd mul 2.0f64 y
205ret div sub e 1.0f64 add e 1.0f64
206end
207fn gelu x f32 to f32
208if lt x -3.0e38f32
209ret -0.0f32
210end
211d = f64 x
212y = mul 0.7978845608028654f64 add d mul 0.044715f64 mul d mul d d
213t = tanhd y
214ret f32 mul mul 0.5f64 d add 1.0f64 t
215end
216export fn gelu32 s view uint * d edit uint * to void
217for i ulong len s
218put d i bitcast uint gelu bitcast f32 get s i
219end
220end
221fn ep p view gp v f32 cv f32 bv f32 to f32
222r = mul bitcast f32 get p alpha v
223be = bitcast f32 get p beta
224if ne be 0.0f32
225r = add r mul be cv
226end
227if ne and get p act 16 0
228r = add r bv
229end
230a = and get p act 15
231if eq a 1
232if lt r 0.0f32
233r = 0.0f32
234end
235end
236if eq a 2
237r = gelu r
238end
239ret r
240end
241note ---- packing: op(X) rows into 2-wide panels, d[dst + ip*2*cols + q*2 + t] = op(X)[2*ip + t, q];
242note element (i, q) of op(X) is s[off + i*si + q*sq]; a row past the end is zero. The C layer sets
243note (si, sq) = (1, ld) for an untransposed operand and (ld, 1) for a transposed one, and w = 2 cols - 2
244note (the step from the odd row of one panel to the even row of the next). One column per helper call:
245note a function has 16 registers, and two views plus two runtime loops leave too few for the body.
246fn pc32 p view pk s view uint * d edit uint * q ulong to void
247y = add get p off mul q get p sq
248x = add get p dst add q q
249for i ulong get p rows
250put d x get s y
251y = add y get p si
252x = add x 1
253if eq and i 1 1
254x = add x get p w
255end
256end
257if eq and get p rows 1 1
258put d x uint 0
259end
260end
261export fn pack32 p view pk s view uint * d edit uint * to void
262for q ulong get p cols
263pc32 p s d q
264end
265end
266fn pc16 p view pk s view ushort * d edit uint * q ulong to void
267y = add get p off mul q get p sq
268x = add get p dst add q q
269for i ulong get p rows
270if eq get p dt 1
271put d x b2f get s y
272else
273put d x h2f get s y
274end
275y = add y get p si
276x = add x 1
277if eq and i 1 1
278x = add x get p w
279end
280end
281if eq and get p rows 1 1
282put d x uint 0
283end
284end
285export fn pack16 p view pk s view ushort * d edit uint * to void
286for q ulong get p cols
287pc16 p s d q
288end
289end
290fn pc8 p view pk s view uchar * d edit uint * q ulong to void
291y = add get p off mul q get p sq
292x = add get p dst add q q
293for i ulong get p rows
294if eq get p dt 1
295put d x e5f get s y
296else
297put d x e4f get s y
298end
299y = add y get p si
300x = add x 1
301if eq and i 1 1
302x = add x get p w
303end
304end
305if eq and get p rows 1 1
306put d x uint 0
307end
308end
309export fn pack8 p view pk s view uchar * d edit uint * to void
310for q ulong get p cols
311pc8 p s d q
312end
313end
314note stage16: widen an f16 (dt 0) or bf16 (dt 1) rows x cols block s[off + i*si + j*sq] into
315note d[dst ..] column-major with ld = rows (the C operand of a half-output gemm, before beta).
316fn sc16 p view pk s view ushort * d edit uint * j ulong to void
317y = add get p off mul j get p sq
318x = add get p dst mul j get p rows
319for i ulong get p rows
320if eq get p dt 1
321put d x b2f get s y
322else
323put d x h2f get s y
324end
325y = add y get p si
326x = add x 1
327end
328end
329export fn stage16 p view pk s view ushort * d edit uint * to void
330for j ulong get p cols
331sc16 p s d j
332end
333end
334note unpack16: s[dst ..] holds a rows x cols column-major f32 block (ld = rows); write it to
335note d[off + i*si + j*sq] as f16 (dt 0) or bf16 (dt 1), round to nearest even.
336fn uc16 p view pk s view uint * d edit ushort * j ulong to void
337x = add get p dst mul j get p rows
338y = add get p off mul j get p sq
339for i ulong get p rows
340if eq get p dt 1
341put d y f2b get s x
342else
343put d y f2h get s x
344end
345y = add y get p si
346x = add x 1
347end
348end
349export fn unpack16 p view pk s view uint * d edit ushort * to void
350for j ulong get p cols
351uc16 p s d j
352end
353end
354note ---- the gemm macro-kernel over packed panels: 2 x 2 register tile (16 registers per function) ----
355note mk: t[8] = ia, t[9] = ib, t[10] = kl in; t[0..3] = the f32 bits of
356note sum_q Ap[ia + 2q + r] * Bp[ib + 2q + s] at t[2s + r] out.
357fn mk w view uint * t edit uint 16 to void
358ia = ulong get t 8
359ib = ulong get t 9
360kl = ulong get t 10
361c0 = 0.0f32
362c1 = 0.0f32
363c2 = 0.0f32
364c3 = 0.0f32
365while ne kl 0
366b0 = bitcast f32 get w ib
367b1 = bitcast f32 get w add ib 1
368a = bitcast f32 get w ia
369c0 = add c0 mul a b0
370c2 = add c2 mul a b1
371a = bitcast f32 get w add ia 1
372c1 = add c1 mul a b0
373c3 = add c3 mul a b1
374ia = add ia 2
375ib = add ib 2
376kl = sub kl 1
377end
378put t 0 bitcast uint c0
379put t 1 bitcast uint c1
380put t 2 bitcast uint c2
381put t 3 bitcast uint c3
382end
383note te1: the epilogue of one element: tile slot e = t[6] holds the sum of row t[12] + (e and 1),
384note column t[13] + (e shr 1).
385fn te1 p view gp w view uint * c edit uint * t view uint 16 to void
386i = ulong add get t 12 and get t 6 1
387j = ulong add get t 13 shr get t 6 1
388if lt i get p m
389if lt j get p n
390x = add get p offc add i mul j get p ldc
391cv = 0.0f32
392if ne bitcast f32 get p beta 0.0f32
393cv = bitcast f32 get c x
394end
395bv = 0.0f32
396if ne and get p act 16 0
397if ne and get p act 32 0
398i = j
399end
400bv = bitcast f32 get w add get p wsbias i
401end
402put c x bitcast uint ep p bitcast f32 get t ulong get t 6 cv bv
403end
404end
405end
406note gcol: one column panel jp of C, every row panel against it.
407fn gcol p view gp w view uint * c edit uint * jp ulong to void
408t = new uint 16 0
409put t 8 uint get p wsa
410put t 9 uint add get p wsb mul mul jp get p k 2
411if ne bitcast f32 get p alpha 0.0f32
412put t 10 uint get p k
413end
414put t 11 uint mul get p k 2
415put t 13 uint add jp jp
416for ip ulong get p mp
417mk w edit t
418put t 8 add get t 8 get t 11
419put t 12 uint add ip ip
420for e ulong 4
421put t 6 uint e
422te1 p w c view t
423end
424end
425end
426note C[i, j] = ep(sum_q Ap[i, q] * Bp[j, q]); Ap at wsa in 2-row panels, Bp at wsb in 2-column panels,
427note the bias vector at wsbias (act +16: by row i; +48: by column j). Reference SGEMM semantics:
428note alpha 0 reads no A or B, beta 0 reads no C, and (alpha 0 or k 0) with beta 1 changes nothing.
429export fn gemm_k p view gp w view uint * c edit uint * to void
430if or eq get p m 0 eq get p n 0
431ret
432end
433if or eq get p k 0 eq bitcast f32 get p alpha 0.0f32
434if eq bitcast f32 get p beta 1.0f32
435if eq and get p act 31 0
436ret
437end
438end
439end
440for jp ulong get p np
441gcol p w c jp
442end
443end
444note ---- gemv: y = alpha op(A) x + beta y, with x and y gathered contiguous into w (wx, wy) ----
445note Reference SGEMV order: y = beta y first, then the axpy form (ta 0) or the dot form (ta 1).
446fn gvy p view gv w edit uint * to void
447be = bitcast f32 get p beta
448ly = get p m
449if ne get p ta 0
450ly = get p n
451end
452for i ulong ly
453y = add get p wy i
454if eq be 0.0f32
455put w y uint 0
456else
457put w y bitcast uint mul be bitcast f32 get w y
458end
459end
460end
461note gn1: y += (alpha x[j]) A[:, j] (one column of the axpy form)
462fn gn1 p view gv a view uint * w edit uint * j ulong to void
463tp = mul bitcast f32 get p alpha bitcast f32 get w add get p wx j
464ja = add get p offa mul j get p lda
465y = get p wy
466for i ulong get p m
467u = mul tp bitcast f32 get a ja
468u = add bitcast f32 get w y u
469put w y bitcast uint u
470y = add y 1
471ja = add ja 1
472end
473end
474fn gvn p view gv a view uint * w edit uint * to void
475for j ulong get p n
476gn1 p a w j
477end
478end
479note gt1: y[j] += alpha (A[:, j] . x) (one column of the dot form)
480fn gt1 p view gv a view uint * w edit uint * j ulong to void
481tp = 0.0f32
482ja = add get p offa mul j get p lda
483x = get p wx
484for i ulong get p m
485tp = add tp mul bitcast f32 get a ja bitcast f32 get w x
486x = add x 1
487ja = add ja 1
488end
489x = add get p wy j
490put w x bitcast uint add bitcast f32 get w x mul bitcast f32 get p alpha tp
491end
492fn gvt p view gv a view uint * w edit uint * to void
493for j ulong get p n
494gt1 p a w j
495end
496end
497export fn gemv_k p view gv a view uint * w edit uint * to void
498if or eq get p m 0 eq get p n 0
499ret
500end
501al = bitcast f32 get p alpha
502be = bitcast f32 get p beta
503if and eq al 0.0f32 eq be 1.0f32
504ret
505end
506if ne be 1.0f32
507gvy p w
508end
509if eq al 0.0f32
510ret
511end
512if eq get p ta 0
513gvn p a w
514else
515gvt p a w
516end
517end
518note ---- strided copy (BLAS inc, negative allowed): d[od + i*id] = s[os + i*is] ----
519export fn copy32 p view cp s view uint * d edit uint * to void
520x = long get p os
521y = long get p od
522for i ulong get p n
523put d ulong y get s ulong x
524x = add x get p is
525y = add y get p id
526end
527end
528note ---- argument checks: the reference BLAS info numbers (0 = valid) ----
529fn tok c long to bool
530ret or or eq c 78 eq c 110 or or eq c 84 eq c 116 or eq c 67 eq c 99
531end
532fn notr c long to bool
533ret or eq c 78 eq c 110
534end
535fn mx1 a long to long
536if lt a 1
537ret 1
538end
539ret a
540end
541export fn gemm_info p view ck to ulong
542ra = get p k
543if notr get p ta
544ra = get p m
545end
546rb = get p n
547if notr get p tb
548rb = get p k
549end
550if not tok get p ta
551ret 1
552end
553if not tok get p tb
554ret 2
555end
556if lt get p m 0
557ret 3
558end
559if lt get p n 0
560ret 4
561end
562if lt get p k 0
563ret 5
564end
565if lt get p lda mx1 ra
566ret 8
567end
568if lt get p ldb mx1 rb
569ret 10
570end
571if lt get p ldc mx1 get p m
572ret 13
573end
574ret 0
575end
576export fn gemv_info p view ck to ulong
577if not tok get p ta
578ret 1
579end
580if lt get p m 0
581ret 2
582end
583if lt get p n 0
584ret 3
585end
586if lt get p lda mx1 get p m
587ret 6
588end
589if eq get p incx 0
590ret 8
591end
592if eq get p incy 0
593ret 11
594end
595ret 0
596end