File size: 22,739 Bytes
dc2106c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# Copyright (c) ONNX Project Contributors

# SPDX-License-Identifier: Apache-2.0

import unittest
from typing import Any

import numpy as np
import parameterized

import onnx
from onnx import helper, numpy_helper


def bfloat16_to_float32(ival: int) -> Any:
    if ival == 0x7FC0:
        return np.float32(np.nan)

    expo = ival >> 7
    prec = ival - (expo << 7)
    sign = expo & 256
    powe = expo & 255
    fval = float(prec * 2 ** (-7) + 1) * 2.0 ** (powe - 127)
    if sign:
        fval = -fval
    return np.float32(fval)


def float8e4m3_to_float32(ival: int) -> Any:
    if ival < 0 or ival > 255:
        raise ValueError(f"{ival} is not a float8.")
    if ival == 255:
        return np.float32(-np.nan)
    if ival == 127:
        return np.float32(np.nan)
    if (ival & 0x7F) == 0:
        return np.float32(0)

    sign = ival & 0x80
    ival &= 0x7F
    expo = ival >> 3
    mant = ival & 0x07
    powe = expo & 0x0F
    if expo == 0:
        powe -= 6
        fraction = 0
    else:
        powe -= 7
        fraction = 1
    fval = float(mant / 8 + fraction) * 2.0**powe
    if sign:
        fval = -fval
    return np.float32(fval)


def float8e5m2_to_float32(ival: int) -> Any:
    if ival < 0 or ival > 255:
        raise ValueError(f"{ival} is not a float8.")
    if ival in (255, 254, 253):
        return np.float32(-np.nan)
    if ival in (127, 126, 125):
        return np.float32(np.nan)
    if ival == 252:
        return -np.float32(np.inf)
    if ival == 124:
        return np.float32(np.inf)
    if (ival & 0x7F) == 0:
        return np.float32(0)

    sign = ival & 0x80
    ival &= 0x7F
    expo = ival >> 2
    mant = ival & 0x03
    powe = expo & 0x1F
    if expo == 0:
        powe -= 14
        fraction = 0
    else:
        powe -= 15
        fraction = 1
    fval = float(mant / 4 + fraction) * 2.0**powe
    if sign:
        fval = -fval
    return np.float32(fval)


class TestNumpyHelper(unittest.TestCase):
    def _test_numpy_helper_float_type(self, dtype: np.number) -> None:
        a = np.random.rand(13, 37).astype(dtype)
        tensor_def = numpy_helper.from_array(a, "test")
        self.assertEqual(tensor_def.name, "test")
        a_recover = numpy_helper.to_array(tensor_def)
        np.testing.assert_equal(a, a_recover)

    def _test_numpy_helper_int_type(self, dtype: np.number) -> None:
        a = np.random.randint(
            np.iinfo(dtype).min, np.iinfo(dtype).max, dtype=dtype, size=(13, 37)
        )
        tensor_def = numpy_helper.from_array(a, "test")
        self.assertEqual(tensor_def.name, "test")
        a_recover = numpy_helper.to_array(tensor_def)
        np.testing.assert_equal(a, a_recover)

    def test_float(self) -> None:
        self._test_numpy_helper_float_type(np.float32)

    def test_uint8(self) -> None:
        self._test_numpy_helper_int_type(np.uint8)

    def test_int8(self) -> None:
        self._test_numpy_helper_int_type(np.int8)

    def test_uint16(self) -> None:
        self._test_numpy_helper_int_type(np.uint16)

    def test_int16(self) -> None:
        self._test_numpy_helper_int_type(np.int16)

    def test_int32(self) -> None:
        self._test_numpy_helper_int_type(np.int32)

    def test_int64(self) -> None:
        self._test_numpy_helper_int_type(np.int64)

    def test_string(self) -> None:
        a = np.array(["Amy", "Billy", "Cindy", "David"]).astype(object)
        tensor_def = numpy_helper.from_array(a, "test")
        self.assertEqual(tensor_def.name, "test")
        a_recover = numpy_helper.to_array(tensor_def)
        np.testing.assert_equal(a, a_recover)

    def test_bool(self) -> None:
        a = np.random.randint(2, size=(13, 37)).astype(bool)
        tensor_def = numpy_helper.from_array(a, "test")
        self.assertEqual(tensor_def.name, "test")
        a_recover = numpy_helper.to_array(tensor_def)
        np.testing.assert_equal(a, a_recover)

    def test_float16(self) -> None:
        self._test_numpy_helper_float_type(np.float32)

    def test_complex64(self) -> None:
        self._test_numpy_helper_float_type(np.complex64)

    def test_complex128(self) -> None:
        self._test_numpy_helper_float_type(np.complex128)

    @parameterized.parameterized.expand(

        [

            (1,),

            (0.100097656,),

            (130048,),

            (1.2993813e-5,),

            (np.nan,),

            (np.inf,),

        ]

    )
    def test_bfloat16_to_float32(self, f):
        f32 = np.float32(f)
        bf16 = helper.float32_to_bfloat16(f32)
        assert isinstance(bf16, int)
        f32_1 = numpy_helper.bfloat16_to_float32(np.array([bf16]))[0]
        f32_2 = bfloat16_to_float32(bf16)
        if np.isnan(f32):
            assert np.isnan(f32_1)
            assert np.isnan(f32_2)
        else:
            self.assertEqual(f32, f32_1)
            self.assertEqual(f32, f32_2)

    def test_float8e4m3_to_float32(self):
        self.assertEqual(numpy_helper.float8e4m3_to_float32(int("1111110", 2)), 448)
        self.assertEqual(numpy_helper.float8e4m3_to_float32(int("1000", 2)), 2 ** (-6))
        self.assertEqual(numpy_helper.float8e4m3_to_float32(int("1", 2)), 2 ** (-9))
        self.assertEqual(
            numpy_helper.float8e4m3_to_float32(int("111", 2)), 0.875 * 2 ** (-6)
        )
        for f in [
            0,
            1,
            -1,
            0.5,
            -0.5,
            0.1015625,
            -0.1015625,
            2,
            3,
            -2,
            -3,
            448,
            2 ** (-6),
            2 ** (-9),
            0.875 * 2 ** (-6),
            np.nan,
        ]:
            with self.subTest(f=f):
                f32 = np.float32(f)
                f8 = helper.float32_to_float8e4m3(f32)
                assert isinstance(f8, int)
                f32_1 = numpy_helper.float8e4m3_to_float32(np.array([f8]))[0]
                f32_2 = float8e4m3_to_float32(f8)
                if np.isnan(f32):
                    assert np.isnan(f32_1)
                    assert np.isnan(f32_2)
                else:
                    self.assertEqual(f32, f32_1)
                    self.assertEqual(f32, f32_2)

    @parameterized.parameterized.expand(

        [

            (0.00439453125, 0.00390625),

            (0.005859375, 0.005859375),

            (0.005759375, 0.005859375),

            (0.0046875, 0.00390625),

            (0.001953125, 0.001953125),

            (0.0029296875, 0.00390625),

            (0.002053125, 0.001953125),

            (0.00234375, 0.001953125),

            (0.0087890625, 0.0078125),

            (0.001171875, 0.001953125),

            (1.8131605, 1.875),

        ]

    )
    def test_float8e4m3_to_float32_round(self, val, expected):
        f8 = helper.float32_to_float8e4m3(val)
        f32 = numpy_helper.float8e4m3_to_float32(f8)
        self.assertEqual(f32, expected)

    def test_float8e5m2_to_float32(self):
        self.assertEqual(numpy_helper.float8e5m2_to_float32(int("1111011", 2)), 57344)
        self.assertEqual(numpy_helper.float8e5m2_to_float32(int("100", 2)), 2 ** (-14))
        self.assertEqual(
            numpy_helper.float8e5m2_to_float32(int("11", 2)), 0.75 * 2 ** (-14)
        )
        self.assertEqual(numpy_helper.float8e5m2_to_float32(int("1", 2)), 2 ** (-16))
        self.assertTrue(np.isnan(numpy_helper.float8e5m2_to_float32(int("1111101", 2))))
        self.assertTrue(np.isnan(numpy_helper.float8e5m2_to_float32(int("1111110", 2))))
        self.assertTrue(np.isnan(numpy_helper.float8e5m2_to_float32(int("1111111", 2))))
        self.assertTrue(
            np.isnan(numpy_helper.float8e5m2_to_float32(int("11111101", 2)))
        )
        self.assertTrue(
            np.isnan(numpy_helper.float8e5m2_to_float32(int("11111110", 2)))
        )
        self.assertTrue(
            np.isnan(numpy_helper.float8e5m2_to_float32(int("11111111", 2)))
        )
        self.assertEqual(numpy_helper.float8e5m2_to_float32(int("1111100", 2)), np.inf)
        self.assertEqual(
            numpy_helper.float8e5m2_to_float32(int("11111100", 2)), -np.inf
        )
        for f in [
            0,
            0.0017089844,
            20480,
            14,
            -3584,
            np.nan,
        ]:
            with self.subTest(f=f):
                f32 = np.float32(f)
                f8 = helper.float32_to_float8e5m2(f32)
                assert isinstance(f8, int)
                f32_1 = numpy_helper.float8e5m2_to_float32(np.array([f8]))[0]
                f32_2 = float8e5m2_to_float32(f8)
                if np.isnan(f32):
                    assert np.isnan(f32_1)
                    assert np.isnan(f32_2)
                else:
                    self.assertEqual(f32, f32_1)
                    self.assertEqual(f32, f32_2)

    def test_float8_e4m3fn_inf(self):
        x = np.float32(np.inf)
        to = helper.float32_to_float8e4m3(x)
        back = numpy_helper.float8e4m3_to_float32(to)
        self.assertEqual(back, 448)

        x = np.float32(np.inf)
        to = helper.float32_to_float8e4m3(x, saturate=False)
        back = numpy_helper.float8e4m3_to_float32(to)
        self.assertTrue(np.isnan(back))

        x = np.float32(-np.inf)
        to = helper.float32_to_float8e4m3(x)
        self.assertEqual(to & 0x80, 0x80)
        back = numpy_helper.float8e4m3_to_float32(to)
        self.assertEqual(back, -448)

        x = np.float32(-np.inf)
        to = helper.float32_to_float8e4m3(x, saturate=False)
        self.assertEqual(to & 0x80, 0x80)
        back = numpy_helper.float8e4m3_to_float32(to)
        self.assertTrue(np.isnan(back))

    def test_float8_e4m3fnuz_inf(self):
        x = np.float32(np.inf)
        to = helper.float32_to_float8e4m3(x, uz=True)
        back = numpy_helper.float8e4m3_to_float32(to, uz=True)
        self.assertEqual(back, 240)

        x = np.float32(np.inf)
        to = helper.float32_to_float8e4m3(x, uz=True, saturate=False)
        back = numpy_helper.float8e4m3_to_float32(to, uz=True)
        self.assertTrue(np.isnan(back))

        x = np.float32(-np.inf)
        to = helper.float32_to_float8e4m3(x, uz=True)
        back = numpy_helper.float8e4m3_to_float32(to, uz=True)
        self.assertEqual(back, -240)

        x = np.float32(-np.inf)
        to = helper.float32_to_float8e4m3(x, uz=True, saturate=False)
        back = numpy_helper.float8e4m3_to_float32(to, uz=True)
        self.assertTrue(np.isnan(back))

    def test_float8_e5m2_inf(self):
        x = np.float32(np.inf)
        to = helper.float32_to_float8e5m2(x)
        back = numpy_helper.float8e5m2_to_float32(to)
        self.assertEqual(back, 57344)

        x = np.float32(np.inf)
        to = helper.float32_to_float8e5m2(x, saturate=False)
        back = numpy_helper.float8e5m2_to_float32(to)
        self.assertTrue(np.isinf(back))

        x = np.float32(-np.inf)
        to = helper.float32_to_float8e5m2(x)
        self.assertEqual(to & 0x80, 0x80)
        back = numpy_helper.float8e5m2_to_float32(to)
        self.assertEqual(back, -57344)

        x = np.float32(-np.inf)
        to = helper.float32_to_float8e5m2(x, saturate=False)
        self.assertEqual(to & 0x80, 0x80)
        back = numpy_helper.float8e5m2_to_float32(to)
        self.assertTrue(np.isinf(back))
        self.assertLess(back, 0)

    def test_float8_e5m2fnuz_inf(self):
        x = np.float32(np.inf)
        to = helper.float32_to_float8e5m2(x, fn=True, uz=True)
        back = numpy_helper.float8e5m2_to_float32(to, fn=True, uz=True)
        self.assertEqual(back, 57344)

        x = np.float32(np.inf)
        to = helper.float32_to_float8e5m2(x, fn=True, uz=True, saturate=False)
        back = numpy_helper.float8e5m2_to_float32(to, fn=True, uz=True)
        self.assertTrue(np.isnan(back))

        x = np.float32(-np.inf)
        to = helper.float32_to_float8e5m2(x, fn=True, uz=True)
        back = numpy_helper.float8e5m2_to_float32(to, fn=True, uz=True)
        self.assertEqual(back, -57344)

        x = np.float32(-np.inf)
        to = helper.float32_to_float8e5m2(x, fn=True, uz=True, saturate=False)
        back = numpy_helper.float8e5m2_to_float32(to, fn=True, uz=True)
        self.assertTrue(np.isnan(back))

    def test_float8_e4m3fn_out_of_range(self):
        x = np.float32(1000000)
        to = helper.float32_to_float8e4m3(x)
        back = numpy_helper.float8e4m3_to_float32(to)
        self.assertEqual(back, 448)

        x = np.float32(1000000)
        to = helper.float32_to_float8e4m3(x, saturate=False)
        back = numpy_helper.float8e4m3_to_float32(to)
        self.assertTrue(np.isnan(back))

        x = np.float32(-1000000)
        to = helper.float32_to_float8e4m3(x)
        back = numpy_helper.float8e4m3_to_float32(to)
        self.assertEqual(back, -448)

        x = np.float32(-1000000)
        to = helper.float32_to_float8e4m3(x, saturate=False)
        back = numpy_helper.float8e4m3_to_float32(to)
        self.assertTrue(np.isnan(back))

    def test_float8_e4m3fnuz_out_of_range(self):
        x = np.float32(1000000)
        to = helper.float32_to_float8e4m3(x, uz=True)
        back = numpy_helper.float8e4m3_to_float32(to, uz=True)
        self.assertEqual(back, 240)

        x = np.float32(1000000)
        to = helper.float32_to_float8e4m3(x, uz=True, saturate=False)
        back = numpy_helper.float8e4m3_to_float32(to, uz=True)
        self.assertTrue(np.isnan(back))

        x = np.float32(-1000000)
        to = helper.float32_to_float8e4m3(x, uz=True)
        back = numpy_helper.float8e4m3_to_float32(to, uz=True)
        self.assertEqual(back, -240)

        x = np.float32(-1000000)
        to = helper.float32_to_float8e4m3(x, uz=True, saturate=False)
        back = numpy_helper.float8e4m3_to_float32(to, uz=True)
        self.assertTrue(np.isnan(back))

    def test_float8_e5m2_out_of_range(self):
        x = np.float32(1000000)
        to = helper.float32_to_float8e5m2(x)
        back = numpy_helper.float8e5m2_to_float32(to)
        self.assertEqual(back, 57344)

        x = np.float32(1000000)
        to = helper.float32_to_float8e5m2(x, saturate=False)
        back = numpy_helper.float8e5m2_to_float32(to)
        self.assertTrue(np.isinf(back))

        x = np.float32(-1000000)
        to = helper.float32_to_float8e5m2(x)
        back = numpy_helper.float8e5m2_to_float32(to)
        self.assertEqual(back, -57344)

        x = np.float32(-1000000)
        to = helper.float32_to_float8e5m2(x, saturate=False)
        back = numpy_helper.float8e5m2_to_float32(to)
        self.assertTrue(np.isinf(back))

    def test_float8_e5m2fnuz_out_of_range(self):
        x = np.float32(1000000)
        to = helper.float32_to_float8e5m2(x, fn=True, uz=True)
        back = numpy_helper.float8e5m2_to_float32(to, fn=True, uz=True)
        self.assertEqual(back, 57344)

        x = np.float32(1000000)
        to = helper.float32_to_float8e5m2(x, fn=True, uz=True, saturate=False)
        back = numpy_helper.float8e5m2_to_float32(to, fn=True, uz=True)
        self.assertTrue(np.isnan(back))

        x = np.float32(-1000000)
        to = helper.float32_to_float8e5m2(x, fn=True, uz=True)
        back = numpy_helper.float8e5m2_to_float32(to, fn=True, uz=True)
        self.assertEqual(back, -57344)

        x = np.float32(-1000000)
        to = helper.float32_to_float8e5m2(x, fn=True, uz=True, saturate=False)
        back = numpy_helper.float8e5m2_to_float32(to, fn=True, uz=True)
        self.assertTrue(np.isnan(back))

    def test_float8_e4m3fn_negative_zero(self):
        x = numpy_helper.float8e5m2_to_float32(0x80)  # -0
        to = helper.float32_to_float8e4m3(x)
        self.assertEqual(to, 0x80)
        back = numpy_helper.float8e4m3_to_float32(to)
        self.assertEqual(back, 0)

        x = numpy_helper.float8e5m2_to_float32(0x80)  # -0
        to = helper.float32_to_float8e4m3(x, saturate=False)
        self.assertEqual(to, 0x80)
        back = numpy_helper.float8e4m3_to_float32(to)
        self.assertEqual(back, 0)

    def test_float8_e4m3fnuz_negative_zero(self):
        x = numpy_helper.float8e5m2_to_float32(0x80)  # -0
        to = helper.float32_to_float8e4m3(x, uz=True)
        self.assertEqual(to, 0)
        back = numpy_helper.float8e4m3_to_float32(to, uz=True)
        self.assertEqual(back, 0)

        x = numpy_helper.float8e5m2_to_float32(0x80)  # -0
        to = helper.float32_to_float8e4m3(x, uz=True, saturate=False)
        back = numpy_helper.float8e4m3_to_float32(to, uz=True)
        self.assertEqual(back, 0)
        self.assertEqual(to, 0)

    def test_float8_e5m2_negative_zero(self):
        x = numpy_helper.float8e5m2_to_float32(0x80)  # -0
        to = helper.float32_to_float8e5m2(x)
        self.assertEqual(to, 0x80)
        back = numpy_helper.float8e4m3_to_float32(to)
        self.assertEqual(back, 0)

        x = numpy_helper.float8e5m2_to_float32(0x80)  # -0
        to = helper.float32_to_float8e5m2(x, saturate=False)
        self.assertEqual(to, 0x80)
        back = numpy_helper.float8e4m3_to_float32(to)
        self.assertEqual(back, 0)

    def test_float8_e5m2fnuz_negative_zero(self):
        x = numpy_helper.float8e5m2_to_float32(0x80)  # -0
        to = helper.float32_to_float8e5m2(x, fn=True, uz=True)
        self.assertEqual(to, 0)
        back = numpy_helper.float8e4m3_to_float32(to, fn=True, uz=True)
        self.assertEqual(back, 0)

        x = numpy_helper.float8e5m2_to_float32(0x80)  # -0
        to = helper.float32_to_float8e5m2(x, fn=True, uz=True, saturate=False)
        self.assertEqual(to, 0)
        back = numpy_helper.float8e4m3_to_float32(to, fn=True, uz=True)
        self.assertEqual(back, 0)

    def test_float8_e4m3fn_negative_nan(self):
        x = numpy_helper.float8e5m2_to_float32(255)  # -nan
        to = helper.float32_to_float8e4m3(x)
        self.assertEqual(to, 255)
        back = numpy_helper.float8e4m3_to_float32(to)
        self.assertTrue(np.isnan(back))

        x = numpy_helper.float8e5m2_to_float32(255)  # -nan
        to = helper.float32_to_float8e4m3(x, saturate=False)
        self.assertEqual(to, 255)
        back = numpy_helper.float8e4m3_to_float32(to)
        self.assertTrue(np.isnan(back))

    def test_float8_e4m3fnuz_negative_nan(self):
        x = numpy_helper.float8e5m2_to_float32(255)  # -nan
        to = helper.float32_to_float8e4m3(x, uz=True)
        self.assertEqual(to, 0x80)
        back = numpy_helper.float8e4m3_to_float32(to, uz=True)
        self.assertTrue(np.isnan(back))

        x = numpy_helper.float8e5m2_to_float32(255)  # -nan
        to = helper.float32_to_float8e4m3(x, uz=True, saturate=False)
        self.assertEqual(to, 0x80)
        back = numpy_helper.float8e4m3_to_float32(to, uz=True)
        self.assertTrue(np.isnan(back))

    def test_float8_e5m2_negative_nan(self):
        x = numpy_helper.float8e5m2_to_float32(255)  # -nan
        to = helper.float32_to_float8e5m2(x)
        self.assertEqual(to, 255)
        back = numpy_helper.float8e4m3_to_float32(to)
        self.assertTrue(np.isnan(back))

        x = numpy_helper.float8e5m2_to_float32(255)  # -nan
        to = helper.float32_to_float8e5m2(x, saturate=False)
        self.assertEqual(to, 255)
        back = numpy_helper.float8e4m3_to_float32(to)
        self.assertTrue(np.isnan(back))

    def test_float8_e5m2fnuz_negative_nan(self):
        x = numpy_helper.float8e5m2_to_float32(255)  # -nan
        to = helper.float32_to_float8e5m2(x, fn=True, uz=True)
        self.assertEqual(to, 0x80)
        back = numpy_helper.float8e4m3_to_float32(to, fn=True, uz=True)
        self.assertTrue(np.isnan(back))

        x = numpy_helper.float8e5m2_to_float32(255)  # -nan
        to = helper.float32_to_float8e5m2(x, fn=True, uz=True, saturate=False)
        self.assertEqual(to, 0x80)
        back = numpy_helper.float8e4m3_to_float32(to, fn=True, uz=True)
        self.assertTrue(np.isnan(back))

    def test_from_dict_values_are_np_arrays_of_float(self):
        map_proto = numpy_helper.from_dict({0: np.array(0.1), 1: np.array(0.9)})
        self.assertIsInstance(map_proto, onnx.MapProto)
        self.assertEqual(
            numpy_helper.to_array(map_proto.values.tensor_values[0]), np.array(0.1)
        )
        self.assertEqual(
            numpy_helper.to_array(map_proto.values.tensor_values[1]), np.array(0.9)
        )

    def test_from_dict_values_are_np_arrays_of_int(self):
        map_proto = numpy_helper.from_dict({0: np.array(1), 1: np.array(9)})
        self.assertIsInstance(map_proto, onnx.MapProto)
        self.assertEqual(
            numpy_helper.to_array(map_proto.values.tensor_values[0]), np.array(1)
        )
        self.assertEqual(
            numpy_helper.to_array(map_proto.values.tensor_values[1]), np.array(9)
        )

    def test_from_dict_values_are_np_arrays_of_ints(self):
        zero_array = np.array([1, 2])
        one_array = np.array([9, 10])
        map_proto = numpy_helper.from_dict({0: zero_array, 1: one_array})
        self.assertIsInstance(map_proto, onnx.MapProto)

        out_tensor = numpy_helper.to_array(map_proto.values.tensor_values[0])
        self.assertEqual(out_tensor[0], zero_array[0])
        self.assertEqual(out_tensor[1], zero_array[1])

        out_tensor = numpy_helper.to_array(map_proto.values.tensor_values[1])
        self.assertEqual(out_tensor[0], one_array[0])
        self.assertEqual(out_tensor[1], one_array[1])

    def test_from_dict_raises_type_error_when_values_are_not_np_arrays(self):
        with self.assertRaises(TypeError):
            # from_dict/from_array expects tensors to be numpy array's or similar.
            numpy_helper.from_dict({0: 0.1, 1: 0.9})

    def test_from_dict_differing_key_types(self):
        with self.assertRaises(TypeError):
            # Differing key types should raise a TypeError
            numpy_helper.from_dict({0: np.array(0.1), 1.1: np.array(0.9)})

    def test_from_dict_differing_value_types(self):
        with self.assertRaises(TypeError):
            # Differing value types should raise a TypeError
            numpy_helper.from_dict({0: np.array(1), 1: np.array(0.9)})


if __name__ == "__main__":
    unittest.main()