2025-03-08 09:09:31 +00:00
|
|
|
import unittest
|
|
|
|
from extra.f16_decompress import u32_to_f16
|
|
|
|
from tinygrad.tensor import Tensor
|
2025-06-13 15:59:36 +09:00
|
|
|
from tinygrad.device import is_dtype_supported
|
2025-03-08 09:09:31 +00:00
|
|
|
from tinygrad import dtypes
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
class TestF16Decompression(unittest.TestCase):
|
2025-06-13 15:59:36 +09:00
|
|
|
@unittest.skipUnless(is_dtype_supported(dtypes.float16), "need float16")
|
2025-03-08 09:09:31 +00:00
|
|
|
def test_u32_to_f16(self):
|
2025-06-13 15:59:36 +09:00
|
|
|
a = Tensor.randn(50, dtype=dtypes.float16)
|
|
|
|
f16_as_u32 = a.bitcast(dtypes.uint32)
|
2025-03-08 09:09:31 +00:00
|
|
|
f16 = u32_to_f16(f16_as_u32)
|
|
|
|
ref = a.numpy()
|
|
|
|
out = f16.numpy().astype(np.float16)
|
|
|
|
np.testing.assert_allclose(out, ref)
|