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 / core / sovereign-core / om / qwen-native / flash / project.om
| 1 | note Flash-Next matrix layer on GB10. Dense BF16 matrices stay resident in bump arenas. |
| 2 | note Routed experts occupy fixed device slots, least recently used first out. Host mode computes the same products on the CPU. |
| 3 | global qxy long 2 |
| 4 | global qdarena long 8 |
| 5 | global qdused ulong 8 |
| 6 | global qdcur ulong 1 |
| 7 | global qgoff ulong 196608 |
| 8 | global qearena long 32 |
| 9 | global qemap uint 24576 |
| 10 | global qeown uint 26624 |
| 11 | global qestamp ulong 26624 |
| 12 | note qecfg: 0 host mode, 1 slot limit, 2 clock, 3 slots filled, 4 expert loads, 5 evictions, 6 device launches. |
| 13 | global qecfg ulong 8 |
| 14 | const qdsize ulong 2147483648 |
| 15 | const qeslots ulong 832 |
| 16 | const qestride ulong 4919296 |
| 17 | fn qtransfer fd int origin ulong n ulong dev long devoff ulong to void |
| 18 | off = ulong 0 |
| 19 | while lt off n bound 65536 |
| 20 | w = sub n off |
| 21 | if gt w len qbuf |
| 22 | w = len qbuf |
| 23 | end |
| 24 | if ne pread fd qbuf 0 w long add origin off long w |
| 25 | qwfail 48 |
| 26 | end |
| 27 | if ne mapwrite dev add devoff off qbuf 0 w long w |
| 28 | qwfail 48 |
| 29 | end |
| 30 | off = add off w |
| 31 | end |
| 32 | end |
| 33 | |
| 34 | fn qfdof id ulong to int |
| 35 | ret int get qwfd ulong get qwfile id |
| 36 | end |
| 37 | |
| 38 | fn qdense id ulong to ulong |
| 39 | g = get qgoff id |
| 40 | if ne g 0 |
| 41 | ret sub g 1 |
| 42 | end |
| 43 | bytes = mul get qwcount id ulong get qwdtype id |
| 44 | need = mul div add bytes 4095 4096 4096 |
| 45 | if gt need qdsize |
| 46 | qwfail 49 |
| 47 | end |
| 48 | a = get qdcur 0 |
| 49 | if gt add get qdused a need qdsize |
| 50 | a = add a 1 |
| 51 | put qdcur 0 a |
| 52 | end |
| 53 | if ge a 8 |
| 54 | qwfail 49 |
| 55 | end |
| 56 | h = get qdarena a |
| 57 | if le h 0 |
| 58 | h = nvalloc qdsize true |
| 59 | if le h 0 |
| 60 | qwfail 50 |
| 61 | end |
| 62 | put qdarena a h |
| 63 | end |
| 64 | off = get qdused a |
| 65 | qtransfer qfdof id get qwoff id bytes h off |
| 66 | put qdused a add off need |
| 67 | loc = or shl a 40 off |
| 68 | put qgoff id add loc 1 |
| 69 | ret loc |
| 70 | end |
| 71 | |
| 72 | fn qvictim to ulong |
| 73 | total = get qecfg 1 |
| 74 | used = get qecfg 3 |
| 75 | if lt used total |
| 76 | put qecfg 3 add used 1 |
| 77 | ret used |
| 78 | end |
| 79 | slot = ulong 0 |
| 80 | best = get qestamp 0 |
| 81 | for i ulong total |
| 82 | if lt get qestamp i best |
| 83 | best = get qestamp i |
| 84 | slot = i |
| 85 | end |
| 86 | end |
| 87 | old = ulong get qeown slot |
| 88 | if eq old 0 |
| 89 | qwfail 49 |
| 90 | end |
| 91 | put qemap sub old 1 0 |
| 92 | put qecfg 5 add get qecfg 5 1 |
| 93 | ret slot |
| 94 | end |
| 95 | |
| 96 | fn qeput h long base ulong id ulong j ulong to void |
| 97 | qtransfer qfdof id get qwoff id 1638400 h add base mul j 1638400 |
| 98 | sid = ulong get qscale id |
| 99 | qtransfer qfdof sid get qwoff sid 200 h add add base 4915200 mul j 200 |
| 100 | end |
| 101 | |
| 102 | fn qefill layer ulong e ulong slot ulong to void |
| 103 | a = div slot qeslots |
| 104 | h = get qearena a |
| 105 | if le h 0 |
| 106 | h = nvalloc mul qeslots qestride false |
| 107 | if le h 0 |
| 108 | qwfail 50 |
| 109 | end |
| 110 | put qearena a h |
| 111 | end |
| 112 | first = add add mul layer 1600 64 mul e 3 |
| 113 | for j ulong 3 |
| 114 | qeput h mul rem slot qeslots qestride ulong get qrole add first j j |
| 115 | end |
| 116 | end |
| 117 | |
| 118 | fn qexpert layer ulong e ulong to ulong |
| 119 | key = add mul layer 512 e |
| 120 | clock = add get qecfg 2 1 |
| 121 | put qecfg 2 clock |
| 122 | s = ulong get qemap key |
| 123 | if ne s 0 |
| 124 | put qestamp sub s 1 clock |
| 125 | ret sub s 1 |
| 126 | end |
| 127 | slot = qvictim |
| 128 | qefill layer e slot |
| 129 | put qemap key uint add slot 1 |
| 130 | put qeown slot uint add key 1 |
| 131 | put qestamp slot clock |
| 132 | put qecfg 4 add get qecfg 4 1 |
| 133 | ret slot |
| 134 | end |
| 135 | |
| 136 | fn qgpuopen to void |
| 137 | if not lqopen |
| 138 | qwfail 50 |
| 139 | end |
| 140 | x = nvalloc 1048576 true |
| 141 | if le x 0 |
| 142 | qwfail 50 |
| 143 | end |
| 144 | put qxy 0 x |
| 145 | y = nvalloc 1048576 true |
| 146 | if le y 0 |
| 147 | qwfail 50 |
| 148 | end |
| 149 | put qxy 1 y |
| 150 | end |
| 151 | |
| 152 | fn qbf j ulong to f32 |
| 153 | a = mul j 2 |
| 154 | bits = or uint get qbuf a shl uint get qbuf add a 1 8 |
| 155 | ret bitcast f32 shl bits 16 |
| 156 | end |
| 157 | |
| 158 | note Round a binary32 value to the nearest BF16 value, ties to even, as the reference tensors store it. |
| 159 | fn qrb x f32 to f32 |
| 160 | u = bitcast uint x |
| 161 | a = and u 2139095040 |
| 162 | if eq a 2139095040 |
| 163 | ret x |
| 164 | end |
| 165 | ret bitcast f32 shl shr add add u 32767 and shr u 16 1 16 16 |
| 166 | end |
| 167 | |
| 168 | note Matrix products read their input from fin and leave BF16-rounded results in fout. |
| 169 | global fin f32 10240 |
| 170 | global fout f32 248320 |
| 171 | fn qfillinput n ulong to void |
| 172 | if gt n len fin |
| 173 | qwfail 52 |
| 174 | end |
| 175 | for j ulong n |
| 176 | bits = bitcast uint get fin j |
| 177 | if ne mapset get qxy 0 mul j 4 4 ulong bits 0 |
| 178 | qwfail 52 |
| 179 | end |
| 180 | end |
| 181 | end |
| 182 | |
| 183 | note One device product y[pos..pos+m] = W[m,k] x. W and its scale tiles lie in one mapping. |
| 184 | note qmvarg: 0 mapping, 1 weight offset, 2 scale offset, 3 format, 4 rows, 5 columns. |
| 185 | global qmvarg ulong 6 |
| 186 | fn qmv to void |
| 187 | h = long get qmvarg 0 |
| 188 | fmt = uint get qmvarg 3 |
| 189 | m = uint get qmvarg 4 |
| 190 | k = uint get qmvarg 5 |
| 191 | if gt ulong m len fout |
| 192 | qwfail 52 |
| 193 | end |
| 194 | qfillinput ulong k |
| 195 | sr = uint 0 |
| 196 | sc = uint 0 |
| 197 | put lqmap 0 h |
| 198 | put lqoff 0 get qmvarg 1 |
| 199 | put lqlen 0 mul mul ulong m ulong k sub 2 ulong fmt |
| 200 | put lqlen 1 0 |
| 201 | if eq fmt 1 |
| 202 | sr = add div sub m 1 128 1 |
| 203 | sc = add div sub k 1 128 1 |
| 204 | put lqmap 1 h |
| 205 | put lqoff 1 get qmvarg 2 |
| 206 | put lqlen 1 mul mul ulong sr ulong sc 2 |
| 207 | end |
| 208 | put lqmap 2 get qxy 0 |
| 209 | put lqmap 3 get qxy 1 |
| 210 | put lqoff 2 0 |
| 211 | put lqoff 3 0 |
| 212 | put lqlen 2 mul ulong k 4 |
| 213 | put lqlen 3 mul ulong m 4 |
| 214 | if not lqrun fmt m k sr sc |
| 215 | qwfail 53 |
| 216 | end |
| 217 | put qecfg 6 add get qecfg 6 1 |
| 218 | for j ulong ulong m |
| 219 | bits = uint mapget get qxy 1 mul j 4 4 |
| 220 | put fout j qrb bitcast f32 bits |
| 221 | end |
| 222 | end |
| 223 | |
| 224 | note Host product with binary64 accumulation. Rows are read from the checkpoint in bounded batches. |
| 225 | note qhs: 0 rows, 1 columns, 2 element bytes, 3 scale columns, 4 first row of the batch in qbuf. |
| 226 | global qhscale f32 512 |
| 227 | global qhs ulong 5 |
| 228 | fn qhrow i ulong to f32 |
| 229 | k = get qhs 1 |
| 230 | base = mul i mul k get qhs 2 |
| 231 | acc = 0.0f64 |
| 232 | if eq get qhs 2 2 |
| 233 | base = div base 2 |
| 234 | for j ulong k |
| 235 | acc = add acc mul f64 qbf add base j f64 get fin j |
| 236 | end |
| 237 | ret qrb f32 acc |
| 238 | end |
| 239 | tile = mul div add get qhs 4 i 128 get qhs 3 |
| 240 | for j ulong k |
| 241 | wv = mul bitcast f32 lqbits uint get qbuf add base j get qhscale add tile div j 128 |
| 242 | acc = add acc mul f64 wv f64 get fin j |
| 243 | end |
| 244 | ret qrb f32 acc |
| 245 | end |
| 246 | |
| 247 | fn qhscales id ulong to void |
| 248 | put qhs 3 0 |
| 249 | sid = ulong get qscale id |
| 250 | if eq sid 0 |
| 251 | ret |
| 252 | end |
| 253 | n = get qwcount sid |
| 254 | if gt n 512 |
| 255 | qwfail 52 |
| 256 | end |
| 257 | put qhs 3 get qwshape add mul sid 4 1 |
| 258 | qwread sid 0 n |
| 259 | for j ulong n |
| 260 | put qhscale j qbf j |
| 261 | end |
| 262 | end |
| 263 | |
| 264 | fn qhostmv id ulong to void |
| 265 | m = get qwshape mul id 4 |
| 266 | k = get qwshape add mul id 4 1 |
| 267 | put qhs 0 m |
| 268 | put qhs 1 k |
| 269 | put qhs 2 ulong get qwdtype id |
| 270 | if or gt m len fout gt k len fin |
| 271 | qwfail 52 |
| 272 | end |
| 273 | qhscales id |
| 274 | per = div len qbuf mul k get qhs 2 |
| 275 | if eq per 0 |
| 276 | qwfail 52 |
| 277 | end |
| 278 | r = ulong 0 |
| 279 | while lt r m bound 1048576 |
| 280 | nr = sub m r |
| 281 | if gt nr per |
| 282 | nr = per |
| 283 | end |
| 284 | qwread id mul r k mul nr k |
| 285 | put qhs 4 r |
| 286 | for i ulong nr |
| 287 | put fout add r i qhrow i |
| 288 | end |
| 289 | r = add r nr |
| 290 | end |
| 291 | end |
| 292 | |
| 293 | fn fmat id ulong to void |
| 294 | if eq get qecfg 0 1 |
| 295 | qhostmv id |
| 296 | ret |
| 297 | end |
| 298 | if ne get qwdtype id 2 |
| 299 | qwfail 52 |
| 300 | end |
| 301 | a = mul id 4 |
| 302 | loc = qdense id |
| 303 | put qmvarg 0 ulong get qdarena shr loc 40 |
| 304 | put qmvarg 1 and loc 1099511627775 |
| 305 | put qmvarg 2 0 |
| 306 | put qmvarg 3 0 |
| 307 | put qmvarg 4 get qwshape a |
| 308 | put qmvarg 5 get qwshape add a 1 |
| 309 | qmv |
| 310 | end |
| 311 | |
| 312 | note Product with one matrix of a routed expert. The key is (layer * 512 + expert) * 4 + matrix (0 gate, 1 up, 2 down). |
| 313 | fn fexpert key ulong to void |
| 314 | j = rem key 4 |
| 315 | e = rem div key 4 512 |
| 316 | layer = div key 2048 |
| 317 | id = ulong get qrole add add mul layer 1600 64 add mul e 3 j |
| 318 | if eq get qecfg 0 1 |
| 319 | qhostmv id |
| 320 | ret |
| 321 | end |
| 322 | slot = qexpert layer e |
| 323 | h = get qearena div slot qeslots |
| 324 | base = mul rem slot qeslots qestride |
| 325 | a = mul id 4 |
| 326 | put qmvarg 0 ulong h |
| 327 | put qmvarg 1 add base mul j 1638400 |
| 328 | put qmvarg 2 add add base 4915200 mul j 200 |
| 329 | put qmvarg 3 1 |
| 330 | put qmvarg 4 get qwshape a |
| 331 | put qmvarg 5 get qwshape add a 1 |
| 332 | qmv |
| 333 | end |