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