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