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 / mxfp4.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note MXFP4 E2M1 nibble plus E8M0 block scale to exact IEEE binary32 bits. |
| 3 | note gpt-oss reference: openai/gpt-oss gpt_oss/torch/weights.py FP4_VALUES, |
| 4 | note low nibble precedes high nibble; scales are biased by 127. |
| 5 | note Returns bits so the identical integer unit runs on native and irspirv. |
| 6 | note Scalar primitive, not GEMM or an inference engine. No device access. |
| 7 | note Refuse non-nibbles and reserved E8M0 255 (NaN block) with trap 5. |
| 8 | note Finite overflow produces signed infinity; signed zero is preserved. |
| 9 | note Wolfram: Table[If[m==0,0,If[m==1,2^-1,(1+Mod[m,2]/2) |
| 10 | note 2^(Quotient[m,2]-1)]],{m,0,7}] == {0,1/2,1,3/2,2,3,4,6} -> True. |
| 11 | note Wolfram: {2^23,2^22,2^31} -> {8388608,4194304,2147483648}. |
| 12 | fn mxfp4bits nib uint scale uint to uint |
| 13 | if gt nib 15 |
| 14 | trap 5 |
| 15 | end |
| 16 | if gt scale 254 |
| 17 | trap 5 |
| 18 | end |
| 19 | sign = shl and nib 8 28 |
| 20 | mag = and nib 7 |
| 21 | if eq mag 0 |
| 22 | ret sign |
| 23 | end |
| 24 | expo = sub long scale 1 |
| 25 | frac = uint 0 |
| 26 | if ge mag 2 |
| 27 | t = shr mag 1 |
| 28 | expo = add expo long t |
| 29 | frac = shl and mag 1 22 |
| 30 | end |
| 31 | if ge expo 255 |
| 32 | ret or sign uint 2139095040 |
| 33 | end |
| 34 | if le expo 0 |
| 35 | sig = or frac uint 8388608 |
| 36 | d = sub long 1 expo |
| 37 | sig = shr sig uint d |
| 38 | ret or sign sig |
| 39 | end |
| 40 | ret or sign or shl uint expo 23 frac |
| 41 | end |
| 42 | fn mxfp4byte packed uint lane uint scale uint to uint |
| 43 | if gt packed 255 |
| 44 | trap 5 |
| 45 | end |
| 46 | if gt lane 1 |
| 47 | trap 5 |
| 48 | end |
| 49 | nib = and shr packed mul lane 4 15 |
| 50 | value = mxfp4bits nib scale |
| 51 | ret value |
| 52 | end |