2023-03-11 08:56:07 +08:00
|
|
|
import unittest
|
|
|
|
import numpy as np
|
|
|
|
from tinygrad.helpers import getenv
|
|
|
|
from tinygrad.lazy import Device
|
|
|
|
from tinygrad.tensor import Tensor, dtypes
|
|
|
|
|
2023-03-20 14:43:49 +08:00
|
|
|
# for GPU, cl_khr_fp16 isn't supported (except now we don't need it!)
|
2023-03-11 08:56:07 +08:00
|
|
|
# for LLVM, it segfaults because it can't link to the casting function
|
2023-03-20 14:43:49 +08:00
|
|
|
@unittest.skipIf(getenv("CI", "") != "" and Device.DEFAULT in ["LLVM"], "float16 broken in some CI backends")
|
2023-03-11 08:56:07 +08:00
|
|
|
class TestDtype(unittest.TestCase):
|
2023-05-29 14:15:06 +08:00
|
|
|
def _test_to_np(self, a, np_dtype, target):
|
2023-03-11 08:56:07 +08:00
|
|
|
print(a)
|
|
|
|
na = a.numpy()
|
|
|
|
print(na, na.dtype, a.lazydata.realized)
|
2023-05-29 14:15:06 +08:00
|
|
|
assert na.dtype == np_dtype
|
|
|
|
np.testing.assert_allclose(na, target)
|
2023-03-11 08:56:07 +08:00
|
|
|
|
2023-05-29 14:15:06 +08:00
|
|
|
def test_half_to_np(self): self._test_to_np(Tensor([1,2,3,4], dtype=dtypes.float16), np.float16, [1,2,3,4])
|
|
|
|
def test_int8_to_np(self): self._test_to_np(Tensor([1,2,3,4], dtype=dtypes.int8), np.int8, [1,2,3,4])
|
|
|
|
def test_uint8_to_np(self): self._test_to_np(Tensor([1,2,3,4], dtype=dtypes.uint8), np.uint8, [1,2,3,4])
|
2023-05-31 07:04:46 +08:00
|
|
|
def test_int64_to_np(self): self._test_to_np(Tensor([1,2,3,4], dtype=dtypes.int64), np.int64, [1,2,3,4])
|
2023-05-29 14:15:06 +08:00
|
|
|
|
|
|
|
def _test_cast(self, a, target_dtype, target):
|
|
|
|
print(a)
|
|
|
|
b = a.cast(target_dtype)
|
|
|
|
print(b.numpy())
|
|
|
|
assert b.dtype == target_dtype
|
|
|
|
np.testing.assert_allclose(b.numpy(), target)
|
|
|
|
|
|
|
|
def test_float_to_half(self): self._test_cast(Tensor([1,2,3,4], dtype=dtypes.float32), dtypes.float16, [1,2,3,4])
|
|
|
|
def test_float_to_int8(self): self._test_cast(Tensor([1,2,3,4], dtype=dtypes.float32), dtypes.int8, [1,2,3,4])
|
|
|
|
def test_float_to_uint8(self): self._test_cast(Tensor([1,2,3,4], dtype=dtypes.float32), dtypes.uint8, [1,2,3,4])
|
2023-05-31 07:04:46 +08:00
|
|
|
def test_float_to_int64(self): self._test_cast(Tensor([1,2,3,4], dtype=dtypes.float32), dtypes.int64, [1,2,3,4])
|
2023-05-29 14:15:06 +08:00
|
|
|
|
|
|
|
def test_half_to_float(self): self._test_cast(Tensor([1,2,3,4], dtype=dtypes.float16), dtypes.float32, [1,2,3,4])
|
|
|
|
def test_half_to_int8(self): self._test_cast(Tensor([1,2,3,4], dtype=dtypes.float16), dtypes.int8, [1,2,3,4])
|
|
|
|
def test_half_to_uint8(self): self._test_cast(Tensor([1,2,3,4], dtype=dtypes.float16), dtypes.uint8, [1,2,3,4])
|
2023-05-31 07:04:46 +08:00
|
|
|
def test_half_to_int64(self): self._test_cast(Tensor([1,2,3,4], dtype=dtypes.float16), dtypes.int64, [1,2,3,4])
|
2023-05-29 14:15:06 +08:00
|
|
|
|
|
|
|
def test_int8_to_float(self): self._test_cast(Tensor([1,2,3,4], dtype=dtypes.int8), dtypes.float32, [1,2,3,4])
|
|
|
|
def test_int8_to_half(self): self._test_cast(Tensor([1,2,3,4], dtype=dtypes.int8), dtypes.float16, [1,2,3,4])
|
|
|
|
def test_int8_to_uint8(self): self._test_cast(Tensor([1,2,3,4], dtype=dtypes.int8), dtypes.uint8, [1,2,3,4])
|
2023-05-31 07:04:46 +08:00
|
|
|
def test_int8_to_int64(self): self._test_cast(Tensor([1,2,3,4], dtype=dtypes.int8), dtypes.int64, [1,2,3,4])
|
2023-05-29 14:15:06 +08:00
|
|
|
|
|
|
|
def test_uint8_to_float(self): self._test_cast(Tensor([1,2,3,4], dtype=dtypes.uint8), dtypes.float32, [1,2,3,4])
|
|
|
|
def test_uint8_to_half(self): self._test_cast(Tensor([1,2,3,4], dtype=dtypes.uint8), dtypes.float16, [1,2,3,4])
|
|
|
|
def test_uint8_to_int8(self): self._test_cast(Tensor([1,2,3,4], dtype=dtypes.uint8), dtypes.int8, [1,2,3,4])
|
2023-05-31 07:04:46 +08:00
|
|
|
def test_uint8_to_int64(self): self._test_cast(Tensor([1,2,3,4], dtype=dtypes.uint8), dtypes.int64, [1,2,3,4])
|
2023-05-29 14:15:06 +08:00
|
|
|
|
|
|
|
def _test_add(self, a, b, target_dtype, target):
|
2023-03-11 08:56:07 +08:00
|
|
|
c = a+b
|
|
|
|
print(c.numpy())
|
2023-05-29 14:15:06 +08:00
|
|
|
assert c.dtype == target_dtype
|
|
|
|
np.testing.assert_allclose(c.numpy(), target)
|
|
|
|
|
|
|
|
def test_half_add(self): self._test_add(Tensor([1,2,3,4], dtype=dtypes.float16), Tensor([1,2,3,4], dtype=dtypes.float16), dtypes.float16, [2,4,6,8])
|
|
|
|
def test_int8_add(self): self._test_add(Tensor([1,2,3,4], dtype=dtypes.int8), Tensor([1,2,3,4], dtype=dtypes.int8), dtypes.int8, [2,4,6,8])
|
2023-05-31 07:04:46 +08:00
|
|
|
def test_int64_add(self): self._test_add(Tensor([1,2,3,4], dtype=dtypes.int64),Tensor([1,2,3,4], dtype=dtypes.int64), dtypes.int64, [2,4,6,8])
|
|
|
|
|
2023-05-29 14:15:06 +08:00
|
|
|
def _test_mul(self, a, b, target_dtype, target):
|
2023-03-23 23:02:52 +08:00
|
|
|
c = a*b
|
|
|
|
print(c.numpy())
|
2023-05-29 14:15:06 +08:00
|
|
|
assert c.dtype == target_dtype
|
|
|
|
np.testing.assert_allclose(c.numpy(), target)
|
2023-03-23 23:02:52 +08:00
|
|
|
|
2023-05-29 14:15:06 +08:00
|
|
|
def test_half_mul(self): self._test_mul(Tensor([1,2,3,4], dtype=dtypes.float16), Tensor([1,2,3,4], dtype=dtypes.float16), dtypes.float16, [1,4,9,16])
|
|
|
|
def test_int8_mul(self): self._test_mul(Tensor([1,2,3,4], dtype=dtypes.int8), Tensor([1,2,3,4], dtype=dtypes.int8), dtypes.int8, [1,4,9,16])
|
2023-05-31 07:04:46 +08:00
|
|
|
def test_int64_mul(self): self._test_mul(Tensor([1,2,3,4], dtype=dtypes.int64), Tensor([1,2,3,4], dtype=dtypes.int64), dtypes.int64, [1,4,9,16])
|
2023-05-29 14:15:06 +08:00
|
|
|
|
|
|
|
def _test_matmul(self, a, b, target_dtype, target):
|
2023-03-23 23:02:52 +08:00
|
|
|
c = a@b
|
|
|
|
print(c.numpy())
|
2023-05-29 14:15:06 +08:00
|
|
|
assert c.dtype == target_dtype
|
|
|
|
np.testing.assert_allclose(c.numpy(), target)
|
2023-03-23 23:02:52 +08:00
|
|
|
|
2023-05-29 14:15:06 +08:00
|
|
|
def test_half_matmul(self): self._test_matmul(Tensor([[1,2],[3,4]], dtype=dtypes.float16), Tensor.eye(2, dtype=dtypes.float16), dtypes.float16, [[1,2],[3,4]])
|
|
|
|
def test_int8_matmul(self): self._test_matmul(Tensor([[1,2],[3,4]], dtype=dtypes.int8), Tensor.eye(2, dtype=dtypes.int8), dtypes.int8, [[1,2],[3,4]])
|
2023-05-31 07:04:46 +08:00
|
|
|
def test_int64_matmul(self): self._test_matmul(Tensor([[1,2],[3,4]], dtype=dtypes.int64), Tensor.eye(2, dtype=dtypes.int64), dtypes.int64, [[1,2],[3,4]])
|
2023-05-29 14:15:06 +08:00
|
|
|
|
|
|
|
def _test_add_upcast(self, a, b, target_dtype, target):
|
2023-03-11 08:56:07 +08:00
|
|
|
c = a+b
|
|
|
|
print(c.numpy())
|
2023-05-29 14:15:06 +08:00
|
|
|
assert c.dtype == target_dtype
|
|
|
|
np.testing.assert_allclose(c.numpy(), target)
|
|
|
|
|
|
|
|
def test_half_add_upcast_float(self): self._test_add_upcast(Tensor([1,2,3,4], dtype=dtypes.float16), Tensor([1,2,3,4], dtype=dtypes.float32), dtypes.float32, [2,4,6,8])
|
|
|
|
def test_int8_add_upcast_float(self): self._test_add_upcast(Tensor([1,2,3,4], dtype=dtypes.int8), Tensor([1,2,3,4], dtype=dtypes.float32), dtypes.float32, [2,4,6,8])
|
|
|
|
def test_int8_add_upcast_half(self): self._test_add_upcast(Tensor([1,2,3,4], dtype=dtypes.int8), Tensor([1,2,3,4], dtype=dtypes.float16), dtypes.float16, [2,4,6,8])
|
2023-05-31 07:04:46 +08:00
|
|
|
def test_int8_add_upcast_int64(self): self._test_add_upcast(Tensor([1,2,3,4], dtype=dtypes.int8), Tensor([1,2,3,4], dtype=dtypes.int64), dtypes.int64, [2,4,6,8])
|
2023-03-11 08:56:07 +08:00
|
|
|
|
2023-05-29 14:15:06 +08:00
|
|
|
def _test_mul_upcast(self, a, b, target_dtype, target):
|
2023-03-23 23:02:52 +08:00
|
|
|
c = a*b
|
|
|
|
print(c.numpy())
|
2023-05-29 14:15:06 +08:00
|
|
|
assert c.dtype == target_dtype
|
|
|
|
np.testing.assert_allclose(c.numpy(), target)
|
2023-03-23 23:02:52 +08:00
|
|
|
|
2023-05-29 14:15:06 +08:00
|
|
|
def test_half_mul_upcast_float(self): self._test_mul_upcast(Tensor([1,2,3,4], dtype=dtypes.float16), Tensor([1,2,3,4], dtype=dtypes.float32), dtypes.float32, [1,4,9,16])
|
|
|
|
def test_int8_mul_upcast_float(self): self._test_mul_upcast(Tensor([1,2,3,4], dtype=dtypes.int8), Tensor([1,2,3,4], dtype=dtypes.float32), dtypes.float32, [1,4,9,16])
|
|
|
|
def test_int8_mul_upcast_half(self): self._test_mul_upcast(Tensor([1,2,3,4], dtype=dtypes.int8), Tensor([1,2,3,4], dtype=dtypes.float16), dtypes.float16, [1,4,9,16])
|
2023-05-31 07:04:46 +08:00
|
|
|
def test_int8_mul_upcast_int64(self): self._test_mul_upcast(Tensor([1,2,3,4], dtype=dtypes.int8), Tensor([1,2,3,4], dtype=dtypes.int64), dtypes.int64, [1,4,9,16])
|
2023-05-29 14:15:06 +08:00
|
|
|
|
|
|
|
def _test_matmul_upcast(self, a, b, target_dtype, target):
|
2023-03-23 23:02:52 +08:00
|
|
|
c = a@b
|
|
|
|
print(c.numpy())
|
2023-05-29 14:15:06 +08:00
|
|
|
assert c.dtype == target_dtype
|
|
|
|
np.testing.assert_allclose(c.numpy(), target)
|
|
|
|
|
|
|
|
def test_half_matmul_upcast_float(self): self._test_matmul_upcast(Tensor([[1,2],[3,4]], dtype=dtypes.float16), Tensor.eye(2, dtype=dtypes.float32), dtypes.float32, [[1,2],[3,4]])
|
|
|
|
def test_int8_matmul_upcast_float(self): self._test_matmul_upcast(Tensor([[1,2],[3,4]], dtype=dtypes.int8), Tensor.eye(2, dtype=dtypes.float32), dtypes.float32, [[1,2],[3,4]])
|
|
|
|
def test_int8_matmul_upcast_half(self): self._test_matmul_upcast(Tensor([[1,2],[3,4]], dtype=dtypes.int8), Tensor.eye(2, dtype=dtypes.float16), dtypes.float16, [[1,2],[3,4]])
|
2023-05-31 07:04:46 +08:00
|
|
|
def test_int8_matmul_upcast_int64(self): self._test_matmul_upcast(Tensor([[1,2],[3,4]], dtype=dtypes.int8), Tensor.eye(2, dtype=dtypes.int64), dtypes.int64, [[1,2],[3,4]])
|
2023-05-29 14:15:06 +08:00
|
|
|
|
|
|
|
def test_int8_to_uint8_negative(self):
|
|
|
|
a = Tensor([-1, -2, -3, -4], dtype=dtypes.int8)
|
|
|
|
print(a)
|
|
|
|
b = a.cast(dtypes.uint8)
|
|
|
|
print(b.numpy())
|
|
|
|
np.testing.assert_allclose(b.numpy(), [255, 254, 253, 252])
|
|
|
|
|
|
|
|
def test_uint8_to_int8_overflow(self):
|
|
|
|
a = Tensor([255, 254, 253, 252], dtype=dtypes.uint8)
|
|
|
|
print(a)
|
|
|
|
b = a.cast(dtypes.int8)
|
|
|
|
print(b.numpy())
|
|
|
|
np.testing.assert_allclose(b.numpy(), [-1, -2, -3, -4])
|
2023-03-23 23:02:52 +08:00
|
|
|
|
2023-03-11 08:56:07 +08:00
|
|
|
if __name__ == '__main__':
|
2023-03-23 23:02:52 +08:00
|
|
|
unittest.main()
|