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 / om / gemmex / row.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note One gathered run of the general GEMM (CUDApp#144, src/omega_gpu_gemm_ex.c run_part, scan_bad): n stored elements |
| 3 | note of type t, packed little-endian in src by the C shell, become core-type elements in dst, or are scanned. |
| 4 | note ops 0 1 2 (put_core f32 f16 bf16, non-finite as 0), 3 (e4m3 copy, NaN as 0), 4 (FAST_BF16: RNE bf16) give the dst |
| 5 | note elements written (ops 5 6, the bf16x3 planes, are the x3 unit); 7 gives 1 when any element is non-finite; 8 sets |
| 6 | note dst[j] = 1 for every non-finite j. Ops 0 1 2 4 7 read the run as words from w (the C shell gives the same bytes as |
| 7 | note src and w, word aligned), op 3 and the fp8 table runs read it as bytes from src; 9 (t = any | rowbad << 1, src |
| 8 | note the column flags) sets dst[j] = 1 exactly where the epilogue recomputes. |
| 9 | note Word constants (Wolfram FromDigits of the hex, base 16): the exponent masks 7f8000007f800000 |
| 10 | note 9187343241974906880, 7f807f807f807f80 9187483429707480960, 7c007c007c007c00 8935278002225314816, |
| 11 | note 7f7f7f7f7f7f7f7f 9187201950435737471, 7c7c7c7c7c7c7c7c 8970181431921507452; the lane high bits 8000000080000000 |
| 12 | note 9223372039002259456, 8000800080008000 9223512776490647552, 8080808080808080 9259542123273814144, and shifted |
| 13 | note right by 8 es - 1 the lane ones 4294967297, 281479271743489, 72340172838076673 (Wolfram BitShiftRight). |
| 14 | note The op and the type come as ot = op | t << 4. tab caches the 256 results of ops 0 1 2 4 for an fp8 type (t 3 4): |
| 15 | note tab[256] = ot + 1 of the table it holds (0 when none; the C shell gives it zeroed and keeps it for the call). |
| 16 | |
| 17 | note the exponent mask of type t, repeated over a word |
| 18 | fn gx_wm t ulong to ulong |
| 19 | if eq t 0 |
| 20 | ret ulong 9187343241974906880 |
| 21 | end |
| 22 | if eq t 1 |
| 23 | ret ulong 9187483429707480960 |
| 24 | end |
| 25 | if eq t 2 |
| 26 | ret ulong 8935278002225314816 |
| 27 | end |
| 28 | if eq t 3 |
| 29 | ret ulong 9187201950435737471 |
| 30 | end |
| 31 | ret ulong 8970181431921507452 |
| 32 | end |
| 33 | |
| 34 | note the high bit of every es-byte lane of a word |
| 35 | fn gx_wh es ulong to ulong |
| 36 | if eq es 4 |
| 37 | ret ulong 9223372039002259456 |
| 38 | end |
| 39 | if eq es 2 |
| 40 | ret ulong 9223512776490647552 |
| 41 | end |
| 42 | ret ulong 9259542123273814144 |
| 43 | end |
| 44 | |
| 45 | note the elements e0 .. n - 1 of a last partial word of w, one at a time: 1 when one is non-finite |
| 46 | fn gx_wtail t ulong n ulong e0 ulong w view ulong 8192 to ulong |
| 47 | es = gx_es t |
| 48 | for j ulong sub n e0 |
| 49 | if gx_bad t gx_wel view w add e0 j es |
| 50 | ret 1 |
| 51 | end |
| 52 | end |
| 53 | ret 0 |
| 54 | end |
| 55 | |
| 56 | note op 7, the last nw & 7 whole words one at a time after the ones that go eight at a time (a, the lane bits so far) |
| 57 | fn gx_wrest w view ulong 8192 nw ulong m ulong one ulong a ulong to ulong |
| 58 | for i ulong and nw 7 |
| 59 | x = xor and get w sub nw add i 1 m m |
| 60 | a = or a sub x one |
| 61 | end |
| 62 | ret a |
| 63 | end |
| 64 | |
| 65 | note op 7: 1 when any of the n elements is non-finite (src/omega_gpu_gemm_ex.c run_bad). A lane x = (v & m) ^ m is 0 |
| 66 | note exactly when its exponent bits are all set, and below the lane high bit always (m leaves the sign bit out). x - one |
| 67 | note (one = 1 in every lane) sets a lane's high bit only when that lane or a lower one is 0 (a borrow), so the OR over |
| 68 | note the words has a lane high bit set exactly when an element is non-finite. The whole words go eight at a time with |
| 69 | note no call per word, then the last nw & 7 one at a time, then the elements of a last partial word. A row past the |
| 70 | note window traps up front on the read of word nw, as the scan would at word 8192; with that bound known the C compiler |
| 71 | note drops the per-word bounds checks of the eight-word loop. |
| 72 | fn gx_wbad t ulong n ulong w view ulong 8192 to ulong |
| 73 | m = gx_wm t |
| 74 | one = shr gx_wh gx_es t sub shl gx_es t 3 1 |
| 75 | nw = shr mul n gx_es t 3 |
| 76 | if gt nw 8192 |
| 77 | ret get w nw |
| 78 | end |
| 79 | a = ulong 0 |
| 80 | for i ulong shr nw 3 |
| 81 | j = shl i 3 |
| 82 | x = xor and get w j m m |
| 83 | a = or a sub x one |
| 84 | x = xor and get w add j 1 m m |
| 85 | a = or a sub x one |
| 86 | x = xor and get w add j 2 m m |
| 87 | a = or a sub x one |
| 88 | x = xor and get w add j 3 m m |
| 89 | a = or a sub x one |
| 90 | x = xor and get w add j 4 m m |
| 91 | a = or a sub x one |
| 92 | x = xor and get w add j 5 m m |
| 93 | a = or a sub x one |
| 94 | x = xor and get w add j 6 m m |
| 95 | a = or a sub x one |
| 96 | x = xor and get w add j 7 m m |
| 97 | a = or a sub x one |
| 98 | end |
| 99 | a = gx_wrest view w nw m one a |
| 100 | es = gx_es t |
| 101 | if ne and a gx_wh es 0 |
| 102 | ret 1 |
| 103 | end |
| 104 | ret gx_wtail t n div shl nw 3 es view w |
| 105 | end |
| 106 | |
| 107 | note the core bits of one element (ops 0 1 2 4) |
| 108 | fn gx_cv op ulong t ulong u uint to uint |
| 109 | b = bitcast uint gx_ldf t u |
| 110 | if not gx_fin b |
| 111 | ret 0 |
| 112 | end |
| 113 | if eq op 0 |
| 114 | ret b |
| 115 | end |
| 116 | if eq op 1 |
| 117 | ret gx_f2h b |
| 118 | end |
| 119 | if eq op 2 |
| 120 | ret shr b 16 |
| 121 | end |
| 122 | ret gx_f2bf b |
| 123 | end |
| 124 | |
| 125 | note runs of 256 or more fp8 elements (ops 0 1 2 4, t 3 4; ot = op | t << 4) look each byte up in tab, built on first use |
| 126 | fn gx_tcv ot ulong n ulong src view uchar 65536 tab edit ulong 257 dst edit uchar 65536 ce ulong to ulong |
| 127 | op = and ot 15 |
| 128 | t = shr ot 4 |
| 129 | k = add ot 1 |
| 130 | if ne get tab 256 k |
| 131 | for b ulong 256 |
| 132 | put tab b ulong gx_cv op t uint b |
| 133 | end |
| 134 | put tab 256 k |
| 135 | end |
| 136 | for i ulong n |
| 137 | gx_put edit dst i ce uint get tab ulong get src i |
| 138 | end |
| 139 | ret n |
| 140 | end |
| 141 | |
| 142 | note ops 8 9 |
| 143 | fn gx_scan op ulong t ulong n ulong src view uchar 65536 dst edit uchar 65536 to ulong |
| 144 | if eq op 9 |
| 145 | for j ulong n |
| 146 | v = uchar 0 |
| 147 | if ne and t 1 0 |
| 148 | if or ne and t 2 0 ne get src j 0 |
| 149 | v = uchar 1 |
| 150 | end |
| 151 | end |
| 152 | put dst j v |
| 153 | end |
| 154 | ret n |
| 155 | end |
| 156 | es = gx_es t |
| 157 | for i ulong n |
| 158 | if gx_bad t gx_get view src i es |
| 159 | put dst i uchar 1 |
| 160 | end |
| 161 | end |
| 162 | ret 0 |
| 163 | end |
| 164 | |
| 165 | fn gemm_ex_row ot ulong n ulong src view uchar 65536 w view ulong 8192 tab edit ulong 257 dst edit uchar 65536 to ulong |
| 166 | op = and ot 15 |
| 167 | t = shr ot 4 |
| 168 | if eq op 7 |
| 169 | ret gx_wbad t n view w |
| 170 | end |
| 171 | if ge op 8 |
| 172 | ret gx_scan op t n view src edit dst |
| 173 | end |
| 174 | if eq op 3 |
| 175 | for i ulong n |
| 176 | b = get src i |
| 177 | if eq and b 127 127 |
| 178 | b = uchar 0 |
| 179 | end |
| 180 | put dst i b |
| 181 | end |
| 182 | ret n |
| 183 | end |
| 184 | es = gx_es t |
| 185 | ce = ulong 2 |
| 186 | if eq op 0 |
| 187 | ce = ulong 4 |
| 188 | end |
| 189 | if and ge t 3 ge n 256 |
| 190 | ret gx_tcv ot n view src edit tab edit dst ce |
| 191 | end |
| 192 | for i ulong n |
| 193 | gx_put edit dst i ce gx_cv op t gx_wel view w i es |
| 194 | end |
| 195 | ret n |
| 196 | end |