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
omega-mv / gpu / nvidia / native / models / mxdot.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note Block-scaled dot products and C = A B^T on the CPU for three formats: |
| 3 | note fmt 0 NVFP4 (E2M1, UE4M3 scale per 16), fmt 1 MXFP4 (E2M1, E8M0 per 32), |
| 4 | note fmt 2 MXFP8 E4M3 (E8M0 per 32). Needs fp8.om and nvfp4.om in the unit. |
| 5 | note A is m rows and B is n rows, each row k elements, K-contiguous (TN, as the |
| 6 | note cuBLASLt block-scaled path). Rows start on block boundaries. Element bytes |
| 7 | note live in one 4096-byte array (FP4: two per byte, low nibble first), scales |
| 8 | note in one 256-byte array, block-major per row. d holds fmt m n k. |
| 9 | note Each block term is computed exactly: the element products are integers |
| 10 | note (E2M1 times 2, E4M3 times 2^9), summed in a long, then times the two scales |
| 11 | note as one binary64 power of two. Terms are added left to right in binary64 |
| 12 | note (round to nearest even). Error bound for nb blocks, u = 2^-53: |
| 13 | note |acc - exact| <= gamma(nb-1) sum|term|, gamma(j) = j u/(1 - j u). |
| 14 | note Wolfram: Max inner products {16 144 225, 32 144, 32 229376^2} -> |
| 15 | note {518400, 4608, 1683627180032} < 2^53. |
| 16 | note Refusals trap 5: NaN element (E4M3 127/255), E8M0 255, UE4M3 above 126, |
| 17 | note a bad fmt, k not a multiple of the block, or arrays too small. |
| 18 | fn e2m1sx nib uint to long |
| 19 | v = long e2m1x2 and nib 7 |
| 20 | if eq and nib 8 8 |
| 21 | ret neg v |
| 22 | end |
| 23 | ret v |
| 24 | end |
| 25 | fn e4m3sx code uint to long |
| 26 | ex = and shr code 3 15 |
| 27 | m = long and code 7 |
| 28 | if eq and code 127 127 |
| 29 | trap 5 |
| 30 | end |
| 31 | v = m |
| 32 | if gt ex 0 |
| 33 | v = shl or m 8 sub long ex 1 |
| 34 | end |
| 35 | if eq and code 128 128 |
| 36 | ret neg v |
| 37 | end |
| 38 | ret v |
| 39 | end |
| 40 | fn pow2f64 k long to f64 |
| 41 | if lt k -1022 |
| 42 | trap 5 |
| 43 | end |
| 44 | if gt k 1023 |
| 45 | trap 5 |
| 46 | end |
| 47 | ret bitcast f64 shl ulong add k 1023 52 |
| 48 | end |
| 49 | fn e8m0exp sc uint to long |
| 50 | if eq sc 255 |
| 51 | trap 5 |
| 52 | end |
| 53 | ret sub long sc 127 |
| 54 | end |
| 55 | fn fp4get a view uchar 4096 i ulong to uint |
| 56 | b = uint get a shr i 1 |
| 57 | ret and shr b uint mul and i 1 4 15 |
| 58 | end |
| 59 | fn nvterm a view uchar 4096 sa view uchar 256 b view uchar 4096 sb view uchar 256 ia ulong ib ulong to f64 |
| 60 | acc = long 0 |
| 61 | for i ulong 16 |
| 62 | x = e2m1sx fp4get a add mul ia 16 i |
| 63 | y = e2m1sx fp4get b add mul ib 16 i |
| 64 | acc = add acc mul x y |
| 65 | end |
| 66 | ca = uint get sa ia |
| 67 | cb = uint get sb ib |
| 68 | w = mul long mul ue4m3sig ca ue4m3sig cb acc |
| 69 | p = add add ue4m3exp ca ue4m3exp cb -2 |
| 70 | ret mul f64 w pow2f64 p |
| 71 | end |
| 72 | fn mx4term a view uchar 4096 sa view uchar 256 b view uchar 4096 sb view uchar 256 ia ulong ib ulong to f64 |
| 73 | acc = long 0 |
| 74 | for i ulong 32 |
| 75 | x = e2m1sx fp4get a add mul ia 32 i |
| 76 | y = e2m1sx fp4get b add mul ib 32 i |
| 77 | acc = add acc mul x y |
| 78 | end |
| 79 | p = add add e8m0exp uint get sa ia e8m0exp uint get sb ib -2 |
| 80 | ret mul f64 acc pow2f64 p |
| 81 | end |
| 82 | fn mx8term a view uchar 4096 sa view uchar 256 b view uchar 4096 sb view uchar 256 ia ulong ib ulong to f64 |
| 83 | acc = long 0 |
| 84 | for i ulong 32 |
| 85 | x = e4m3sx uint get a add mul ia 32 i |
| 86 | y = e4m3sx uint get b add mul ib 32 i |
| 87 | acc = add acc mul x y |
| 88 | end |
| 89 | p = add add e8m0exp uint get sa ia e8m0exp uint get sb ib -18 |
| 90 | ret mul f64 acc pow2f64 p |
| 91 | end |
| 92 | fn mxblk fmt ulong to ulong |
| 93 | if eq fmt 0 |
| 94 | ret 16 |
| 95 | end |
| 96 | if gt fmt 2 |
| 97 | trap 5 |
| 98 | end |
| 99 | ret 32 |
| 100 | end |
| 101 | fn mxterm a view uchar 4096 sa view uchar 256 b view uchar 4096 sb view uchar 256 d view ulong 4 ij ulong to f64 |
| 102 | fmt = get d 0 |
| 103 | n = get d 2 |
| 104 | kb = div get d 3 mxblk fmt |
| 105 | ia = mul div ij n kb |
| 106 | ib = mul rem ij n kb |
| 107 | acc = 0.0f64 |
| 108 | for t ulong kb |
| 109 | if eq fmt 0 |
| 110 | acc = add acc nvterm a sa b sb add ia t add ib t |
| 111 | continue |
| 112 | end |
| 113 | if eq fmt 1 |
| 114 | acc = add acc mx4term a sa b sb add ia t add ib t |
| 115 | continue |
| 116 | end |
| 117 | acc = add acc mx8term a sa b sb add ia t add ib t |
| 118 | end |
| 119 | ret acc |
| 120 | end |
| 121 | fn mxcheck d view ulong 4 to void |
| 122 | fmt = get d 0 |
| 123 | bs = mxblk fmt |
| 124 | m = get d 1 |
| 125 | n = get d 2 |
| 126 | k = get d 3 |
| 127 | if eq k 0 |
| 128 | trap 5 |
| 129 | end |
| 130 | if ne rem k bs 0 |
| 131 | trap 5 |
| 132 | end |
| 133 | note Divide the capacity before comparing so full-width dimensions cannot wrap products. |
| 134 | if ne n 0 |
| 135 | if gt m div ulong 256 n |
| 136 | trap 5 |
| 137 | end |
| 138 | end |
| 139 | kb = div k bs |
| 140 | if gt m div ulong 256 kb |
| 141 | trap 5 |
| 142 | end |
| 143 | if gt n div ulong 256 kb |
| 144 | trap 5 |
| 145 | end |
| 146 | eb = k |
| 147 | if lt fmt 2 |
| 148 | eb = div k 2 |
| 149 | end |
| 150 | if gt m div ulong 4096 eb |
| 151 | trap 5 |
| 152 | end |
| 153 | if gt n div ulong 4096 eb |
| 154 | trap 5 |
| 155 | end |
| 156 | end |
| 157 | fn mxmm a view uchar 4096 sa view uchar 256 b view uchar 4096 sb view uchar 256 c edit f64 256 d view ulong 4 to void |
| 158 | mxcheck d |
| 159 | mn = mul get d 1 get d 2 |
| 160 | for ij ulong mn |
| 161 | put c ij mxterm a sa b sb d ij |
| 162 | end |
| 163 | end |