Repository navigation
Expand file tree
/
Copy pathtest_float_encoding.py
More file actions
303 lines (277 loc) · 18.3 KB
/
Copy pathtest_float_encoding.py
File metadata and controls
303 lines (277 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# SPDX-FileCopyrightText: Copyright (c) <2026> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
import math
import struct
import pytest
import torch
import cuda.tile as ct
from cuda.tile._cext import decode_float, encode_float
def test_float64_from_bits():
for val in [0.0, -0.0, math.inf, -math.inf, 1.0, 1.5, -1.0]:
bits = struct.unpack("<Q", struct.pack("<d", val))[0]
assert val == decode_float(bits, ct.float64)
assert math.isnan(decode_float(0xffffffffffffffff, ct.float64))
def test_float16_from_bits():
for bits in range(1 << 16):
val = decode_float(bits, ct.float16)
expected = struct.unpack("<e", struct.pack("<H", bits))[0]
if math.isnan(expected):
assert math.isnan(val)
else:
assert expected == val
assert encode_float(val, ct.float16) == bits
def test_bfloat16_from_bits():
bf16_tensor = torch.zeros(1, dtype=torch.bfloat16)
u16_view = bf16_tensor.view(torch.uint16)
for bits in range(1 << 16):
val = decode_float(bits, ct.bfloat16)
u16_view[0] = bits
expected = bf16_tensor[0]
if math.isnan(expected):
assert math.isnan(val)
else:
assert expected == val
assert encode_float(val, ct.bfloat16) == bits
f4e2m1fn_values = [
0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0,
-0.0, -0.5, -1.0, -1.5, -2.0, -3.0, -4.0, -6.0
]
f8e4m3fn_values = [
0.0, 0.001953125, 0.00390625, 0.005859375,
0.0078125, 0.009765625, 0.01171875, 0.013671875,
0.015625, 0.017578125, 0.01953125, 0.021484375,
0.0234375, 0.025390625, 0.02734375, 0.029296875,
0.03125, 0.03515625, 0.0390625, 0.04296875,
0.046875, 0.05078125, 0.0546875, 0.05859375,
0.0625, 0.0703125, 0.078125, 0.0859375,
0.09375, 0.1015625, 0.109375, 0.1171875,
0.125, 0.140625, 0.15625, 0.171875,
0.1875, 0.203125, 0.21875, 0.234375,
0.25, 0.28125, 0.3125, 0.34375,
0.375, 0.40625, 0.4375, 0.46875,
0.5, 0.5625, 0.625, 0.6875,
0.75, 0.8125, 0.875, 0.9375,
1.0, 1.125, 1.25, 1.375,
1.5, 1.625, 1.75, 1.875,
2.0, 2.25, 2.5, 2.75,
3.0, 3.25, 3.5, 3.75,
4.0, 4.5, 5.0, 5.5,
6.0, 6.5, 7.0, 7.5,
8.0, 9.0, 10.0, 11.0,
12.0, 13.0, 14.0, 15.0,
16.0, 18.0, 20.0, 22.0,
24.0, 26.0, 28.0, 30.0,
32.0, 36.0, 40.0, 44.0,
48.0, 52.0, 56.0, 60.0,
64.0, 72.0, 80.0, 88.0,
96.0, 104.0, 112.0, 120.0,
128.0, 144.0, 160.0, 176.0,
192.0, 208.0, 224.0, 240.0,
256.0, 288.0, 320.0, 352.0,
384.0, 416.0, 448.0, math.nan,
-0.0, -0.001953125, -0.00390625, -0.005859375,
-0.0078125, -0.009765625, -0.01171875, -0.013671875,
-0.015625, -0.017578125, -0.01953125, -0.021484375,
-0.0234375, -0.025390625, -0.02734375, -0.029296875,
-0.03125, -0.03515625, -0.0390625, -0.04296875,
-0.046875, -0.05078125, -0.0546875, -0.05859375,
-0.0625, -0.0703125, -0.078125, -0.0859375,
-0.09375, -0.1015625, -0.109375, -0.1171875,
-0.125, -0.140625, -0.15625, -0.171875,
-0.1875, -0.203125, -0.21875, -0.234375,
-0.25, -0.28125, -0.3125, -0.34375,
-0.375, -0.40625, -0.4375, -0.46875,
-0.5, -0.5625, -0.625, -0.6875,
-0.75, -0.8125, -0.875, -0.9375,
-1.0, -1.125, -1.25, -1.375,
-1.5, -1.625, -1.75, -1.875,
-2.0, -2.25, -2.5, -2.75,
-3.0, -3.25, -3.5, -3.75,
-4.0, -4.5, -5.0, -5.5,
-6.0, -6.5, -7.0, -7.5,
-8.0, -9.0, -10.0, -11.0,
-12.0, -13.0, -14.0, -15.0,
-16.0, -18.0, -20.0, -22.0,
-24.0, -26.0, -28.0, -30.0,
-32.0, -36.0, -40.0, -44.0,
-48.0, -52.0, -56.0, -60.0,
-64.0, -72.0, -80.0, -88.0,
-96.0, -104.0, -112.0, -120.0,
-128.0, -144.0, -160.0, -176.0,
-192.0, -208.0, -224.0, -240.0,
-256.0, -288.0, -320.0, -352.0,
-384.0, -416.0, -448.0, -math.nan,
]
f8e8m0fnu_values = [
5.877471754111438e-39, 1.1754943508222875e-38, 2.350988701644575e-38, 4.70197740328915e-38,
9.4039548065783e-38, 1.88079096131566e-37, 3.76158192263132e-37, 7.52316384526264e-37,
1.504632769052528e-36, 3.009265538105056e-36, 6.018531076210112e-36, 1.2037062152420224e-35,
2.407412430484045e-35, 4.81482486096809e-35, 9.62964972193618e-35, 1.925929944387236e-34,
3.851859888774472e-34, 7.703719777548943e-34, 1.5407439555097887e-33, 3.0814879110195774e-33,
6.162975822039155e-33, 1.232595164407831e-32, 2.465190328815662e-32, 4.930380657631324e-32,
9.860761315262648e-32, 1.9721522630525295e-31, 3.944304526105059e-31, 7.888609052210118e-31,
1.5777218104420236e-30, 3.1554436208840472e-30, 6.310887241768095e-30, 1.262177448353619e-29,
2.524354896707238e-29, 5.048709793414476e-29, 1.0097419586828951e-28, 2.0194839173657902e-28,
4.0389678347315804e-28, 8.077935669463161e-28, 1.6155871338926322e-27, 3.2311742677852644e-27,
6.462348535570529e-27, 1.2924697071141057e-26, 2.5849394142282115e-26, 5.169878828456423e-26,
1.0339757656912846e-25, 2.0679515313825692e-25, 4.1359030627651384e-25, 8.271806125530277e-25,
1.6543612251060553e-24, 3.308722450212111e-24, 6.617444900424222e-24, 1.3234889800848443e-23,
2.6469779601696886e-23, 5.293955920339377e-23, 1.0587911840678754e-22, 2.117582368135751e-22,
4.235164736271502e-22, 8.470329472543003e-22, 1.6940658945086007e-21, 3.3881317890172014e-21,
6.776263578034403e-21, 1.3552527156068805e-20, 2.710505431213761e-20, 5.421010862427522e-20,
1.0842021724855044e-19, 2.168404344971009e-19, 4.336808689942018e-19, 8.673617379884035e-19,
1.734723475976807e-18, 3.469446951953614e-18, 6.938893903907228e-18, 1.3877787807814457e-17,
2.7755575615628914e-17, 5.551115123125783e-17, 1.1102230246251565e-16, 2.220446049250313e-16,
4.440892098500626e-16, 8.881784197001252e-16, 1.7763568394002505e-15, 3.552713678800501e-15,
7.105427357601002e-15, 1.4210854715202004e-14, 2.842170943040401e-14, 5.684341886080802e-14,
1.1368683772161603e-13, 2.2737367544323206e-13, 4.547473508864641e-13, 9.094947017729282e-13,
1.8189894035458565e-12, 3.637978807091713e-12, 7.275957614183426e-12, 1.4551915228366852e-11,
2.9103830456733704e-11, 5.820766091346741e-11, 1.1641532182693481e-10, 2.3283064365386963e-10,
4.656612873077393e-10, 9.313225746154785e-10, 1.862645149230957e-09, 3.725290298461914e-09,
7.450580596923828e-09, 1.4901161193847656e-08, 2.9802322387695312e-08, 5.960464477539063e-08,
1.1920928955078125e-07, 2.384185791015625e-07, 4.76837158203125e-07, 9.5367431640625e-07,
1.9073486328125e-06, 3.814697265625e-06, 7.62939453125e-06, 1.52587890625e-05,
3.0517578125e-05, 6.103515625e-05, 0.0001220703125, 0.000244140625,
0.00048828125, 0.0009765625, 0.001953125, 0.00390625,
0.0078125, 0.015625, 0.03125, 0.0625,
0.125, 0.25, 0.5, 1.0,
2.0, 4.0, 8.0, 16.0,
32.0, 64.0, 128.0, 256.0,
512.0, 1024.0, 2048.0, 4096.0,
8192.0, 16384.0, 32768.0, 65536.0,
131072.0, 262144.0, 524288.0, 1048576.0,
2097152.0, 4194304.0, 8388608.0, 16777216.0,
33554432.0, 67108864.0, 134217728.0, 268435456.0,
536870912.0, 1073741824.0, 2147483648.0, 4294967296.0,
8589934592.0, 17179869184.0, 34359738368.0, 68719476736.0,
137438953472.0, 274877906944.0, 549755813888.0, 1099511627776.0,
2199023255552.0, 4398046511104.0, 8796093022208.0, 17592186044416.0,
35184372088832.0, 70368744177664.0, 140737488355328.0, 281474976710656.0,
562949953421312.0, 1125899906842624.0, 2251799813685248.0, 4503599627370496.0,
9007199254740992.0, 1.8014398509481984e+16, 3.602879701896397e+16, 7.205759403792794e+16,
1.4411518807585587e+17, 2.8823037615171174e+17, 5.764607523034235e+17, 1.152921504606847e+18,
2.305843009213694e+18, 4.611686018427388e+18, 9.223372036854776e+18, 1.8446744073709552e+19,
3.6893488147419103e+19, 7.378697629483821e+19, 1.4757395258967641e+20, 2.9514790517935283e+20,
5.902958103587057e+20, 1.1805916207174113e+21, 2.3611832414348226e+21, 4.722366482869645e+21,
9.44473296573929e+21, 1.888946593147858e+22, 3.777893186295716e+22, 7.555786372591432e+22,
1.5111572745182865e+23, 3.022314549036573e+23, 6.044629098073146e+23, 1.2089258196146292e+24,
2.4178516392292583e+24, 4.835703278458517e+24, 9.671406556917033e+24, 1.9342813113834067e+25,
3.8685626227668134e+25, 7.737125245533627e+25, 1.5474250491067253e+26, 3.094850098213451e+26,
6.189700196426902e+26, 1.2379400392853803e+27, 2.4758800785707605e+27, 4.951760157141521e+27,
9.903520314283042e+27, 1.9807040628566084e+28, 3.961408125713217e+28, 7.922816251426434e+28,
1.5845632502852868e+29, 3.1691265005705735e+29, 6.338253001141147e+29, 1.2676506002282294e+30,
2.535301200456459e+30, 5.070602400912918e+30, 1.0141204801825835e+31, 2.028240960365167e+31,
4.056481920730334e+31, 8.112963841460668e+31, 1.6225927682921336e+32, 3.2451855365842673e+32,
6.490371073168535e+32, 1.298074214633707e+33, 2.596148429267414e+33, 5.192296858534828e+33,
1.0384593717069655e+34, 2.076918743413931e+34, 4.153837486827862e+34, 8.307674973655724e+34,
1.661534994731145e+35, 3.32306998946229e+35, 6.64613997892458e+35, 1.329227995784916e+36,
2.658455991569832e+36, 5.316911983139664e+36, 1.0633823966279327e+37, 2.1267647932558654e+37,
4.253529586511731e+37, 8.507059173023462e+37, 1.7014118346046923e+38, math.nan,
]
f8e5m3fnu_values = [
0.0, 7.62939453125e-06, 1.52587890625e-05, 2.288818359375e-05,
3.0517578125e-05, 3.814697265625e-05, 4.57763671875e-05, 5.340576171875e-05,
6.103515625e-05, 6.866455078125e-05, 7.62939453125e-05, 8.392333984375e-05,
9.1552734375e-05, 9.918212890625e-05, 0.0001068115234375, 0.00011444091796875,
0.0001220703125, 0.0001373291015625, 0.000152587890625, 0.0001678466796875,
0.00018310546875, 0.0001983642578125, 0.000213623046875, 0.0002288818359375,
0.000244140625, 0.000274658203125, 0.00030517578125, 0.000335693359375,
0.0003662109375, 0.000396728515625, 0.00042724609375, 0.000457763671875,
0.00048828125, 0.00054931640625, 0.0006103515625, 0.00067138671875,
0.000732421875, 0.00079345703125, 0.0008544921875, 0.00091552734375,
0.0009765625, 0.0010986328125, 0.001220703125, 0.0013427734375,
0.00146484375, 0.0015869140625, 0.001708984375, 0.0018310546875,
0.001953125, 0.002197265625, 0.00244140625, 0.002685546875,
0.0029296875, 0.003173828125, 0.00341796875, 0.003662109375,
0.00390625, 0.00439453125, 0.0048828125, 0.00537109375,
0.005859375, 0.00634765625, 0.0068359375, 0.00732421875,
0.0078125, 0.0087890625, 0.009765625, 0.0107421875,
0.01171875, 0.0126953125, 0.013671875, 0.0146484375,
0.015625, 0.017578125, 0.01953125, 0.021484375,
0.0234375, 0.025390625, 0.02734375, 0.029296875,
0.03125, 0.03515625, 0.0390625, 0.04296875,
0.046875, 0.05078125, 0.0546875, 0.05859375,
0.0625, 0.0703125, 0.078125, 0.0859375,
0.09375, 0.1015625, 0.109375, 0.1171875,
0.125, 0.140625, 0.15625, 0.171875,
0.1875, 0.203125, 0.21875, 0.234375,
0.25, 0.28125, 0.3125, 0.34375,
0.375, 0.40625, 0.4375, 0.46875,
0.5, 0.5625, 0.625, 0.6875,
0.75, 0.8125, 0.875, 0.9375,
1.0, 1.125, 1.25, 1.375,
1.5, 1.625, 1.75, 1.875,
2.0, 2.25, 2.5, 2.75,
3.0, 3.25, 3.5, 3.75,
4.0, 4.5, 5.0, 5.5,
6.0, 6.5, 7.0, 7.5,
8.0, 9.0, 10.0, 11.0,
12.0, 13.0, 14.0, 15.0,
16.0, 18.0, 20.0, 22.0,
24.0, 26.0, 28.0, 30.0,
32.0, 36.0, 40.0, 44.0,
48.0, 52.0, 56.0, 60.0,
64.0, 72.0, 80.0, 88.0,
96.0, 104.0, 112.0, 120.0,
128.0, 144.0, 160.0, 176.0,
192.0, 208.0, 224.0, 240.0,
256.0, 288.0, 320.0, 352.0,
384.0, 416.0, 448.0, 480.0,
512.0, 576.0, 640.0, 704.0,
768.0, 832.0, 896.0, 960.0,
1024.0, 1152.0, 1280.0, 1408.0,
1536.0, 1664.0, 1792.0, 1920.0,
2048.0, 2304.0, 2560.0, 2816.0,
3072.0, 3328.0, 3584.0, 3840.0,
4096.0, 4608.0, 5120.0, 5632.0,
6144.0, 6656.0, 7168.0, 7680.0,
8192.0, 9216.0, 10240.0, 11264.0,
12288.0, 13312.0, 14336.0, 15360.0,
16384.0, 18432.0, 20480.0, 22528.0,
24576.0, 26624.0, 28672.0, 30720.0,
32768.0, 36864.0, 40960.0, 45056.0,
49152.0, 53248.0, 57344.0, 61440.0,
65536.0, 73728.0, 81920.0, 90112.0,
98304.0, 106496.0, 114688.0, math.nan
]
@pytest.mark.parametrize("type, value_table", [
(ct.float4_e2m1fn, f4e2m1fn_values),
(ct.float8_e4m3fn, f8e4m3fn_values),
(ct.float8_e8m0fnu, f8e8m0fnu_values),
(ct.float8_e5m3fnu, f8e5m3fnu_values),
])
def test_low_precision_float_from_bits(type, value_table):
for bits, expected in enumerate(value_table):
val = decode_float(bits, type)
if math.isnan(expected):
assert math.isnan(val)
else:
assert val == expected
assert bits == encode_float(val, type)
@pytest.mark.parametrize("dtype", [ct.float8_e8m0fnu, ct.float8_e5m3fnu])
def test_encode_negative_nan_as_unsigned(dtype):
assert encode_float(-math.nan, dtype) == 0xff
def test_encode_not_representable():
with pytest.raises(ValueError, match="Cannot represent NaN as float4_e2m1fn"):
encode_float(math.nan, ct.float4_e2m1fn)
with pytest.raises(OverflowError,
match="Cannot represent negative value -1.0 as float8_e8m0fnu"):
encode_float(-1.0, ct.float8_e8m0fnu)
@pytest.mark.parametrize("bits", [2**64, 2**100, -1, -2**63, -2**100])
def test_decode_float64_bits_out_of_range(bits):
with pytest.raises(ValueError, match="do not fit in 64 bits"):
decode_float(bits, ct.float64)
def test_invalid_arguments():
with pytest.raises(TypeError, match="Expected a floating-point dtype"):
encode_float(1.0, ct.int32)
with pytest.raises(TypeError, match="Expected a floating-point dtype"):
decode_float(0, ct.int32)
with pytest.raises(TypeError, match="Expected an int"):
decode_float(1.0, ct.float16)
with pytest.raises(ValueError, match="do not fit in 16 bits"):
decode_float(0x10000, ct.float16)
with pytest.raises(ValueError, match="do not fit in 16 bits"):
decode_float(-1, ct.float16)
with pytest.raises(ValueError, match="do not fit in 19 bits"):
decode_float(1 << 19, ct.tfloat32)