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 / nvfp4.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note NVFP4: E2M1 elements (OCP MX v1.0 FP4, values 0 .5 1 1.5 2 3 4 6) with one |
| 3 | note unsigned E4M3 (UE4M3, sign bit zero) scale per 16 elements. Needs fp8.om |
| 4 | note (f32norm, e4m3enc) in the same unit. Integer bits except nvfp4scale. |
| 5 | note Packing: element 2i is the low nibble of byte i, element 2i+1 the high. |
| 6 | note Scale codes 0..126 are accepted; 127 (NaN) and 128..255 (sign set) trap 5. |
| 7 | note A product of E2M1 (2 significant bits) and UE4M3 (4) is exact in binary32: |
| 8 | note Wolfram: {6 448, 2^-1 2^-9} -> {2688, 1/1024}, both inside binary32 normals. |
| 9 | note Encode nvfp4enc x scale: nearest even E2M1 value to x/scale, by comparing |
| 10 | note |x| with scale times the midpoints {1,3,5,7,10,14,20}/4 (exact binary32, |
| 11 | note Wolfram: 15 20 < 2^24). Above 6 and Inf saturate to 6; NaN traps 5; |
| 12 | note scale 0 gives signed zero. |
| 13 | note nvfp4scale amax: e4m3enc of the binary32 quotient amax/6 with saturation |
| 14 | note (the per-block scale of the NVIDIA FP32 recipe; no per-tensor scale here). |
| 15 | fn e2m1x2 m uint to uint |
| 16 | if lt m 4 |
| 17 | ret m |
| 18 | end |
| 19 | ret shl add uint 2 and m 1 sub shr m 1 1 |
| 20 | end |
| 21 | fn ue4m3sig scale uint to uint |
| 22 | if gt scale 126 |
| 23 | trap 5 |
| 24 | end |
| 25 | se = shr scale 3 |
| 26 | if eq se 0 |
| 27 | ret and scale 7 |
| 28 | end |
| 29 | ret or and scale 7 8 |
| 30 | end |
| 31 | fn ue4m3exp scale uint to long |
| 32 | se = shr scale 3 |
| 33 | if eq se 0 |
| 34 | ret -9 |
| 35 | end |
| 36 | ret sub long se 10 |
| 37 | end |
| 38 | fn nvfp4bits nib uint scale uint to uint |
| 39 | if gt nib 15 |
| 40 | trap 5 |
| 41 | end |
| 42 | si = ue4m3sig scale |
| 43 | s = shl and nib 8 28 |
| 44 | v = e2m1x2 and nib 7 |
| 45 | if eq v 0 |
| 46 | ret s |
| 47 | end |
| 48 | if eq si 0 |
| 49 | ret s |
| 50 | end |
| 51 | ret or s f32norm mul v si sub ue4m3exp scale 1 |
| 52 | end |
| 53 | fn nvfp4mid k uint to uint |
| 54 | if eq k 0 |
| 55 | ret 1 |
| 56 | end |
| 57 | if eq k 1 |
| 58 | ret 3 |
| 59 | end |
| 60 | if eq k 2 |
| 61 | ret 5 |
| 62 | end |
| 63 | if eq k 3 |
| 64 | ret 7 |
| 65 | end |
| 66 | if eq k 4 |
| 67 | ret 10 |
| 68 | end |
| 69 | if eq k 5 |
| 70 | ret 14 |
| 71 | end |
| 72 | ret 20 |
| 73 | end |
| 74 | fn nvfp4enc bits uint scale uint to uint |
| 75 | si = ue4m3sig scale |
| 76 | s = and shr bits 28 8 |
| 77 | a = and bits 2147483647 |
| 78 | if gt a 2139095040 |
| 79 | trap 5 |
| 80 | end |
| 81 | if eq si 0 |
| 82 | ret s |
| 83 | end |
| 84 | u = sub ue4m3exp scale 2 |
| 85 | c = uint 0 |
| 86 | for k uint 7 |
| 87 | t = f32norm mul si nvfp4mid k u |
| 88 | if lt a t |
| 89 | break |
| 90 | end |
| 91 | if gt a t |
| 92 | c = add k 1 |
| 93 | continue |
| 94 | end |
| 95 | if eq and k 1 1 |
| 96 | c = add k 1 |
| 97 | end |
| 98 | break |
| 99 | end |
| 100 | ret or s c |
| 101 | end |
| 102 | fn nvfp4scale amax uint to uint |
| 103 | if gt amax 2139095040 |
| 104 | trap 5 |
| 105 | end |
| 106 | q = div bitcast f32 amax 6.0f32 |
| 107 | ret e4m3enc bitcast uint q 1 |
| 108 | end |
| 109 | fn nvfp4nib x view uint 16 q edit uchar 8 to uint |
| 110 | amax = uint 0 |
| 111 | for i ulong 16 |
| 112 | a = and get x i 2147483647 |
| 113 | if gt a 2139095040 |
| 114 | trap 5 |
| 115 | end |
| 116 | if gt a amax |
| 117 | amax = a |
| 118 | end |
| 119 | end |
| 120 | sc = nvfp4scale amax |
| 121 | for j ulong 8 |
| 122 | lo = nvfp4enc get x mul j 2 sc |
| 123 | hi = nvfp4enc get x add mul j 2 1 sc |
| 124 | put q j uchar or lo shl hi 4 |
| 125 | end |
| 126 | ret sc |
| 127 | end |