interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / gpu / nvidia / native / models / fp8.om

165 lines · 3429 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note OCP OFP8 rev 1.0 (2023-12-01): E4M3 (bias 7) and E5M2 (bias 15) to and
3note from exact IEEE binary32 bits. Integer bit operations only, no device access.
4note Decode: E4M3 S.1111.111 is NaN, no infinities; E5M2 S.11111.00 is Inf,
5note S.11111.{01,10,11} NaN. A NaN decodes to quiet binary32 NaN with its sign.
6note Encode (spec 5.2.1): round to nearest even, then sat 1 clamps to max normal
7note (E4M3 126 = 448, E5M2 123 = 57344) and sat 0 gives E4M3 NaN 127 or E5M2
8note Inf 124. Infinity follows Table 3. Below half the minimum subnormal gives
9note signed zero. A NaN source keeps its sign: E4M3 127, E5M2 126.
10note Refusals trap 5: a code above 255, sat above 1, f32norm out of range.
11note Wolfram: (1+6/8) 2^(15-7) -> 448; (1+3/4) 2^(30-15) -> 57344;
12note 2^-9 -> 1/512 (E4M3 min subnormal); 2^-16 -> 1/65536 (E5M2 min subnormal).
13note Wolfram: FromDigits hex 7FC00000 7F800000 7FFFFF 800000 7FFFFFFF ->
14note 2143289344 2139095040 8388607 8388608 2147483647.
15fn f32norm n uint p long to uint
16if eq n 0
17trap 5
18end
19if gt n 16777215
20trap 5
21end
22b = long 0
23t = shr n 1
24while ne t 0 bound 24
25b = add b 1
26t = shr t 1
27end
28ex = add add b p 127
29if lt ex 1
30trap 5
31end
32if gt ex 254
33trap 5
34end
35sh = uint sub 23 b
36m = and shl n sh 8388607
37ret or shl uint ex 23 m
38end
39fn e4m3bits code uint to uint
40if gt code 255
41trap 5
42end
43s = shl and code 128 24
44ex = and shr code 3 15
45m = and code 7
46if eq ex 15
47if eq m 7
48ret or s 2143289344
49end
50end
51if eq ex 0
52if eq m 0
53ret s
54end
55ret or s f32norm m -9
56end
57ret or s or shl add ex 120 23 shl m 20
58end
59fn e5m2bits code uint to uint
60if gt code 255
61trap 5
62end
63s = shl and code 128 24
64ex = and shr code 2 31
65m = and code 3
66if eq ex 31
67if eq m 0
68ret or s 2139095040
69end
70ret or s 2143289344
71end
72if eq ex 0
73if eq m 0
74ret s
75end
76ret or s f32norm m -16
77end
78ret or s or shl add ex 112 23 shl m 21
79end
80note rnesh returns sig / 2^sh rounded to nearest, ties to even (1 <= sh <= 24).
81note This is the single rounding step of every encoder in this unit.
82fn rnesh sig uint sh uint to uint
83q = shr sig sh
84rm = and sig sub shl uint 1 sh 1
85hf = shl uint 1 sub sh 1
86if gt rm hf
87ret add q 1
88end
89if eq rm hf
90if eq and q 1 1
91ret add q 1
92end
93end
94ret q
95end
96note fp8mag rounds a finite binary32 magnitude a to mb mantissa bits with
97note minimum exponent emin, nearest even, and returns the unsigned code
98note (biased exponent << mb) + mantissa. A carry out of the mantissa moves into
99note the exponent field by the add. Callers compare against their max code.
100fn fp8mag a uint mb long emin long bias long to uint
101ef = shr a 23
102if eq ef 0
103ret 0
104end
105e = sub long ef 127
106if gt e 40
107ret 4095
108end
109ue = e
110if lt ue emin
111ue = emin
112end
113sh = sub add sub ue e 23 mb
114if gt sh 24
115ret 0
116end
117q = rnesh or and a 8388607 8388608 uint sh
118if lt e emin
119ret q
120end
121umb = uint mb
122ret add shl uint add e bias umb sub q shl uint 1 umb
123end
124fn e4m3enc bits uint sat uint to uint
125if gt sat 1
126trap 5
127end
128s = and shr bits 24 128
129a = and bits 2147483647
130if gt a 2139095040
131ret or s 127
132end
133c = uint 127
134if lt a 2139095040
135c = fp8mag a 3 -6 7
136end
137if gt c 126
138if eq sat 1
139ret or s 126
140end
141ret or s 127
142end
143ret or s c
144end
145fn e5m2enc bits uint sat uint to uint
146if gt sat 1
147trap 5
148end
149s = and shr bits 24 128
150a = and bits 2147483647
151if gt a 2139095040
152ret or s 126
153end
154c = uint 124
155if lt a 2139095040
156c = fp8mag a 2 -14 15
157end
158if gt c 123
159if eq sat 1
160ret or s 123
161end
162ret or s 124
163end
164ret or s c
165end