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/om/gpu/engine/bonsai_nvfp4.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 / om / gpu / engine / bonsai_nvfp4.om` 515 lines ยท 12135 bytes Raw Blame History 1 note SPDX-License-Identifier: AGPL-3.0-only 2 note om/gpu/engine/bonsai_nvfp4.om: Canonical NVIDIA NVFP4 (E2M1 + UE4M3/16, alpha=1/256, 4.50 bpw) 3 note - Builds exact LUT[272] (256 UE4M3*alpha + 16 E2M1) & 65536-entry FP16->(UE4M3, E2M1_mag) table on CPU (<2ms) 4 note - Converts all 25.6B PTQ1_0 weights -> canonical NVFP4 [N, K/2] + [N, K/16] ON GPU via tPtq1ToNvfp4 (<0.2s!) 5 note - Full-APU 20 ARMv9 CPU workers execute canonical 4-bit E2M1 + UE4M3 NVFP4 in parallel with 48 Blackwell SMs! 6 7 global bonUseNvfp4 ulong 1 8 global bonNvLutMap long 1 9 global bonNvLutVa ulong 1 10 global bonNvFp16Map long 1 11 global bonNvFp16Va ulong 1 12 global bonNvRawMap long 1 13 global bonNvRawVa ulong 1 14 global bonNvPktMap long 1 15 16 global bonNvLutCpu f32 272 17 global bonNvFp16Cpu uint 65536 18 global bonNvBestErr f32 1 19 global bonNvBestSc ulong 1 20 global bonNvBestNib ulong 1 21 22 note Shared slab allocation state & Full-APU CPU buffers (used by both PTQ1_0 and NVFP4) 23 global bonCurMap long 1 24 global bonCurOff ulong 1 25 global bonCurVa ulong 1 26 global bonTResVa ulong 1 27 28 global bonCpuX f32 5120 29 global bonCpuX4 f32 20480 30 global bonCpuRowAcc f32 4 31 global bonCpuLut uchar 1280 32 global bonCpuLutF f32 1280 33 global bonCpuRawBuf uchar `4194304` 34 global bonCpuRowStart ulong 1 35 global bonCpuRowCnt ulong 1 36 global bonCpuNvW uchar `4194304` 37 global bonCpuNvS uchar 524288 38 39 text bonMsgModeNv4 "quant_format : NVFP4 Canonical (E2M1 + UE4M3/16, 4.50 bpw) + Hadamard-1024 + BF16-GDN + Full-APU (20 CPU + 48 SM)\n" 40 41 fn bonSlabAllocMap bytes ulong to bool 42 al = mul div add bytes 4095 4096 4096 43 cnt = get engSlabCnt 0 44 if gt cnt 0 45 last = sub cnt 1 46 used = get engSlabUsed last 47 if le add used al ENG_SLAB_SZ 48 put engSlabUsed last add used al 49 put bonCurMap 0 get engSlabMap last 50 put bonCurOff 0 used 51 put bonCurVa 0 add get engSlabVa last used 52 ret true 53 end 54 end 55 if ge cnt 48 56 ret false 57 end 58 sm = nvalloc ENG_SLAB_SZ true 59 if le sm 0 60 ret false 61 end 62 sva = mapaddr sm 63 put engSlabMap cnt sm 64 put engSlabVa cnt sva 65 put engSlabUsed cnt al 66 put engSlabCnt 0 add cnt 1 67 put bonCurMap 0 sm 68 put bonCurOff 0 0 69 put bonCurVa 0 sva 70 ret true 71 end 72 73 fn bonNvBuildUe4m3 c ulong to f32 74 if ge c 127 75 ret f32 0.0f32 76 end 77 se = shr c 3 78 sm = and c 7 79 if eq se 0 80 ret mul f32 sm f32 0.`00000762939453125f32` 81 end 82 mant = f32 add 8 sm 83 expI = sub long se 18 84 v = mant 85 while gt expI 0 bound 16 86 v = mul v f32 2.0f32 87 expI = sub expI 1 88 end 89 while lt expI 0 bound 24 90 v = mul v f32 0.5f32 91 expI = add expI 1 92 end 93 ret v 94 end 95 96 fn bonNvFillCpuLut to void 97 for c ulong 256 98 put bonNvLutCpu c bonNvBuildUe4m3 c 99 end 100 put bonNvLutCpu 256 f32 0.0f32 101 put bonNvLutCpu 257 f32 0.5f32 102 put bonNvLutCpu 258 f32 1.0f32 103 put bonNvLutCpu 259 f32 1.5f32 104 put bonNvLutCpu 260 f32 2.0f32 105 put bonNvLutCpu 261 f32 3.0f32 106 put bonNvLutCpu 262 f32 4.0f32 107 put bonNvLutCpu 263 f32 6.0f32 108 put bonNvLutCpu 264 f32 0.0f32 109 put bonNvLutCpu 265 f32 -0.5f32 110 put bonNvLutCpu 266 f32 -1.0f32 111 put bonNvLutCpu 267 f32 -1.5f32 112 put bonNvLutCpu 268 f32 -2.0f32 113 put bonNvLutCpu 269 f32 -3.0f32 114 put bonNvLutCpu 270 f32 -4.0f32 115 put bonNvLutCpu 271 f32 -6.0f32 116 end 117 118 fn bonNvBinSearch target f32 to ulong 119 lo = ulong 1 120 hi = ulong 126 121 while lt add lo 1 hi bound 8 122 mid = shr add lo hi 1 123 if le get bonNvLutCpu mid target 124 lo = mid 125 else 126 hi = mid 127 end 128 end 129 ret lo 130 end 131 132 fn bonNvCheckCand ad f32 nib ulong cc ulong to void 133 if gt cc 126 134 ret 135 end 136 err = sub mul get bonNvLutCpu cc get bonNvLutCpu add 256 nib ad 137 if lt err f32 0.0f32 138 err = sub f32 0.0f32 err 139 end 140 if lt err get bonNvBestErr 0 141 put bonNvBestErr 0 err 142 put bonNvBestSc 0 cc 143 put bonNvBestNib 0 nib 144 end 145 end 146 147 fn bonNvBestEntry d f32 to uint 148 if eq d f32 0.0f32 149 ret 0 150 end 151 negD = lt d f32 0.0f32 152 ad = d 153 if negD 154 ad = sub f32 0.0f32 ad 155 end 156 if gt ad f32 100.0f32 157 ret 0 158 end 159 put bonNvBestErr 0 f32 `1000000000`.0f32 160 put bonNvBestSc 0 1 161 put bonNvBestNib 0 2 162 for ni ulong 2 163 lo = bonNvBinSearch div ad get bonNvLutCpu add 258 ni 164 bonNvCheckCand ad add 2 ni lo 165 bonNvCheckCand ad add 2 ni add lo 1 166 end 167 mPos = get bonNvBestNib 0 168 mNeg = or mPos 8 169 if negD 170 ret uint or get bonNvBestSc 0 or shl mNeg 8 shl mPos 12 171 end 172 ret uint or get bonNvBestSc 0 or shl mPos 8 shl mNeg 12 173 end 174 175 fn bonNvInitGpu to bool 176 bonNvFillCpuLut 177 put bonNvLutMap 0 nvalloc 4096 false 178 if le get bonNvLutMap 0 0 179 ret false 180 end 181 put bonNvLutVa 0 mapaddr get bonNvLutMap 0 182 for i ulong 272 183 engPutU32Io mul i 4 bitcast uint get bonNvLutCpu i 184 end 185 if ne mapwrite get bonNvLutMap 0 0 engIoBuf 0 1088 1088 186 ret false 187 end 188 put bonNvFp16Map 0 nvalloc 262144 false 189 if le get bonNvFp16Map 0 0 190 ret false 191 end 192 put bonNvFp16Va 0 mapaddr get bonNvFp16Map 0 193 for h ulong 65536 194 expF = and shr h 10 31 195 ent = uint 0 196 if lt expF 31 197 ent = bonNvBestEntry bitcast f32 ggFp16ToF32Bits h 198 end 199 put bonNvFp16Cpu h ent 200 engPutU32Io mul h 4 ent 201 end 202 if ne mapwrite get bonNvFp16Map 0 0 engIoBuf 0 262144 262144 203 ret false 204 end 205 put bonNvRawMap 0 nvalloc `16777216` false 206 if le get bonNvRawMap 0 0 207 ret false 208 end 209 put bonNvRawVa 0 mapaddr get bonNvRawMap 0 210 put bonNvPktMap 0 nvalloc 65536 false 211 if le get bonNvPktMap 0 0 212 ret false 213 end 214 ret true 215 end 216 217 fn bonNvRunConv nBlks ulong dstWVa ulong dstSVa ulong to bool 218 put gbused 0 0 219 gbsetup 220 gbmth 0 40 4 221 gbemit 0 222 gbemit 0 223 gbemit 0 224 gbemit `2281701376` 225 cb = mapaddr get gbmem 1 226 gbdriver cb 227 gbdma cb 224 228 for j ulong 224 229 gbemit get gbcb j 230 end 231 put engBindN 0 0 232 engPtr get bonNvRawVa 0 233 engPtr dstWVa 234 engPtr dstSVa 235 engPtr get bonNvFp16Va 0 236 engArg uint nBlks 237 engPrepQmdGy 27 uint div add nBlks 255 256 1 256 238 words = get engBindN 0 239 endva = add cb add 896 mul words 4 240 put gbcb 212 gbhi endva 241 put gbcb 213 gblo endva 242 gbdma add cb 896 uint words 243 for j2 ulong words 244 gbemit get engBind j2 245 end 246 q = mapaddr get gbmem 2 247 gbinline q false 248 gbdma add q 8192 1 249 gbemit 1 250 gbinline add q 4096 true 251 gbtail 252 tot = get gbused 0 253 if ne mapwrite get bonNvPktMap 0 0 gbcmd 0 tot long tot 254 ret false 255 end 256 if lt mapset get gbmem 2 8192 4 0 0 257 ret false 258 end 259 if lt mapset get gbmem 3 0 4 0 0 260 ret false 261 end 262 if lt mapset get gbmem 3 16 4 0 0 263 ret false 264 end 265 if not nvsend get bonNvPktMap 0 0 uint div tot 4 266 ret false 267 end 268 if not engWaitFast `30000000000` 269 ret false 270 end 271 ret eq mapget get gbmem 3 0 4 `1145324612` 272 end 273 274 fn bonNvReadRawChunk fd int foff ulong rawB ulong to bool 275 rdDone = ulong 0 276 while lt rdDone rawB bound 64 277 ch = sub rawB rdDone 278 if gt ch `1048576` 279 ch = ulong `1048576` 280 end 281 got = pread fd engIoBuf 0 ch long add foff rdDone 282 if le got 0 283 ret false 284 end 285 if ne mapwrite get bonNvRawMap 0 rdDone engIoBuf 0 ulong got got 286 ret false 287 end 288 rdDone = add rdDone ulong got 289 end 290 ret true 291 end 292 293 fn bonNvStreamRange tid ulong nRows ulong dstWVa ulong dstSVa ulong to bool 294 nBlksTot = div mul get ggDim0 tid nRows 128 295 foff = add get ggDataStart 0 get ggTOff tid 296 doneBlks = ulong 0 297 while lt doneBlks nBlksTot bound 256 298 cb = sub nBlksTot doneBlks 299 if gt cb 524288 300 cb = ulong 524288 301 end 302 if not bonNvReadRawChunk get ggFd 0 add foff mul doneBlks 28 mul cb 28 303 ret false 304 end 305 if not bonNvRunConv cb add dstWVa mul doneBlks 64 add dstSVa mul doneBlks 8 306 ret false 307 end 308 doneBlks = add doneBlks cb 309 end 310 ret true 311 end 312 313 fn bonLoadNvfp4_1 tid ulong to bool 314 K = get ggDim0 tid 315 N = get ggDim1 tid 316 wB = div mul N K 2 317 sB = div mul N K 16 318 if not bonSlabAllocMap add wB sB 319 ret false 320 end 321 wVa = get bonCurVa 0 322 put bonTResVa 0 wVa 323 ret bonNvStreamRange tid N wVa add wVa wB 324 end 325 326 fn bonLoadNvfp4_2 t0 ulong t1 ulong to bool 327 K = get ggDim0 t0 328 totN = add get ggDim1 t0 get ggDim1 t1 329 wB = div mul totN K 2 330 if not bonSlabAllocMap add wB div mul totN K 16 331 ret false 332 end 333 wVa = get bonCurVa 0 334 sVa = add wVa wB 335 put bonTResVa 0 wVa 336 if not bonNvStreamRange t0 get ggDim1 t0 wVa sVa 337 ret false 338 end 339 wVa = add wVa div mul get ggDim1 t0 K 2 340 sVa = add sVa div mul get ggDim1 t0 K 16 341 ret bonNvStreamRange t1 get ggDim1 t1 wVa sVa 342 end 343 344 fn bonLoadNvfp4_3 t0 ulong t1 ulong t2 ulong to bool 345 K = get ggDim0 t0 346 totN = add add get ggDim1 t0 get ggDim1 t1 get ggDim1 t2 347 wB = div mul totN K 2 348 if not bonSlabAllocMap add wB div mul totN K 16 349 ret false 350 end 351 wVa = get bonCurVa 0 352 sVa = add wVa wB 353 put bonTResVa 0 wVa 354 if not bonNvStreamRange t0 get ggDim1 t0 wVa sVa 355 ret false 356 end 357 wVa = add wVa div mul get ggDim1 t0 K 2 358 sVa = add sVa div mul get ggDim1 t0 K 16 359 if not bonNvStreamRange t1 get ggDim1 t1 wVa sVa 360 ret false 361 end 362 wVa = add wVa div mul get ggDim1 t1 K 2 363 sVa = add sVa div mul get ggDim1 t1 K 16 364 ret bonNvStreamRange t2 get ggDim1 t2 wVa sVa 365 end 366 367 fn bonLoadNvfp4_Slice tid ulong nRows ulong to bool 368 K = get ggDim0 tid 369 wB = div mul nRows K 2 370 sB = div mul nRows K 16 371 if not bonSlabAllocMap add wB sB 372 ret false 373 end 374 wVa = get bonCurVa 0 375 put bonTResVa 0 wVa 376 ret bonNvStreamRange tid nRows wVa add wVa wB 377 end 378 379 note ========================================================================= 380 note Full-APU 20-Core CPU Canonical NVFP4 Workers (exact E2M1 + UE4M3 dot products!) 381 note ========================================================================= 382 global bonConvMPos ulong 1 383 global bonConvMNeg ulong 1 384 385 fn bonCpuMapTrit tr ulong to ulong 386 if eq tr 0 387 ret get bonConvMNeg 0 388 end 389 if eq tr 2 390 ret get bonConvMPos 0 391 end 392 ret 0 393 end 394 395 fn bonCpuConvPairByte b0 ulong b1 ulong tr ulong to uchar 396 n0 = bonCpuMapTrit ulong get bonCpuLut add b0 tr 397 n1 = bonCpuMapTrit ulong get bonCpuLut add b1 tr 398 ret uchar or n0 shl n1 4 399 end 400 401 fn bonCpuConvBlkG1 src ulong wb ulong to void 402 for m ulong 8 403 b0 = mul ulong get bonCpuRawBuf add src mul m 2 5 404 b1 = mul ulong get bonCpuRawBuf add src add mul m 2 1 5 405 for tr ulong 5 406 put bonCpuNvW add wb add mul tr 8 m bonCpuConvPairByte b0 b1 tr 407 end 408 end 409 end 410 411 fn bonCpuConvBlkG2 src ulong wb ulong to void 412 for m2 ulong 4 413 b0 = mul ulong get bonCpuRawBuf add src add 16 mul m2 2 5 414 b1 = mul ulong get bonCpuRawBuf add src add 16 add mul m2 2 1 5 415 for tr2 ulong 5 416 put bonCpuNvW add wb add 40 add mul tr2 4 m2 bonCpuConvPairByte b0 b1 tr2 417 end 418 end 419 end 420 421 fn bonCpuConvBlkG3 src ulong wb ulong to void 422 q0 = mul ulong get bonCpuRawBuf add src 24 5 423 q1 = mul ulong get bonCpuRawBuf add src 25 5 424 for tr3 ulong 4 425 put bonCpuNvW add wb add 60 tr3 bonCpuConvPairByte q0 q1 tr3 426 end 427 end 428 429 fn bonCpuConvBlkNv4 b ulong to void 430 src = mul b 28 431 ent = ulong get bonNvFp16Cpu or ulong get bonCpuRawBuf add src 26 shl ulong get bonCpuRawBuf add src 27 8 432 put bonConvMPos 0 and shr ent 8 15 433 put bonConvMNeg 0 and shr ent 12 15 434 for si ulong 8 435 put bonCpuNvS add mul b 8 si uchar and ent 255 436 end 437 bonCpuConvBlkG1 src mul b 64 438 bonCpuConvBlkG2 src mul b 64 439 bonCpuConvBlkG3 src mul b 64 440 end 441 442 fn bonWorkerConvertNvfp4 to void 443 nBlks = mul get bonCpuRowCnt 0 40 444 for b ulong nBlks bound 65536 445 bonCpuConvBlkNv4 b 446 end 447 end 448 449 fn bonCpuDotGrpNv4 wb ulong xb ulong to f32 450 bs = f32 0.0f32 451 for j ulong 8 452 by = ulong get bonCpuNvW add wb j 453 x0 = add xb mul j 2 454 w = get bonNvLutCpu add 256 and by 15 455 bs = add bs mul w get bonCpuX x0 456 w = get bonNvLutCpu add 256 shr by 4 457 bs = add bs mul w get bonCpuX add x0 1 458 end 459 ret bs 460 end 461 462 fn bonCpuDotRowNvfp4 rIdx ulong to f32 463 acc = f32 0.0f32 464 wBase = mul rIdx 2560 465 sBase = mul rIdx 320 466 for g ulong 320 467 sc = get bonNvLutCpu ulong get bonCpuNvS add sBase g 468 acc = add acc mul bonCpuDotGrpNv4 add wBase mul g 8 mul g 16 sc 469 end 470 ret acc 471 end 472 473 global bonNvGrpAcc f32 4 474 475 fn bonCpuDotGrpM4Nv4 wb ulong xb ulong to void 476 s0 = f32 0.0f32 477 s1 = f32 0.0f32 478 s2 = f32 0.0f32 479 s3 = f32 0.0f32 480 for j ulong 8 481 by = ulong get bonCpuNvW add wb j 482 x0 = add xb mul j 2 483 w = get bonNvLutCpu add 256 and by 15 484 s0 = add s0 mul w get bonCpuX4 x0 485 s1 = add s1 mul w get bonCpuX4 add x0 5120 486 s2 = add s2 mul w get bonCpuX4 add x0 10240 487 s3 = add s3 mul w get bonCpuX4 add x0 15360 488 w = get bonNvLutCpu add 256 shr by 4 489 s0 = add s0 mul w get bonCpuX4 add x0 1 490 s1 = add s1 mul w get bonCpuX4 add x0 5121 491 s2 = add s2 mul w get bonCpuX4 add x0 10241 492 s3 = add s3 mul w get bonCpuX4 add x0 15361 493 end 494 put bonNvGrpAcc 0 s0 495 put bonNvGrpAcc 1 s1 496 put bonNvGrpAcc 2 s2 497 put bonNvGrpAcc 3 s3 498 end 499 500 fn bonCpuDotRowM4Nvfp4 rIdx ulong to void 501 put bonCpuRowAcc 0 f32 0.0f32 502 put bonCpuRowAcc 1 f32 0.0f32 503 put bonCpuRowAcc 2 f32 0.0f32 504 put bonCpuRowAcc 3 f32 0.0f32 505 wBase = mul rIdx 2560 506 sBase = mul rIdx 320 507 for g ulong 320 508 bonCpuDotGrpM4Nv4 add wBase mul g 8 mul g 16 509 sc = get bonNvLutCpu ulong get bonCpuNvS add sBase g 510 put bonCpuRowAcc 0 add get bonCpuRowAcc 0 mul get bonNvGrpAcc 0 sc 511 put bonCpuRowAcc 1 add get bonCpuRowAcc 1 mul get bonNvGrpAcc 1 sc 512 put bonCpuRowAcc 2 add get bonCpuRowAcc 2 mul get bonNvGrpAcc 2 sc 513 put bonCpuRowAcc 3 add get bonCpuRowAcc 3 mul get bonNvGrpAcc 3 sc 514 end 515 end