interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / native / models / nvfp4.om

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