From cc2be9064f7dde3e2607054bc3bf4a21af9168a5 Mon Sep 17 00:00:00 2001 From: chenyu Date: Tue, 18 Jun 2024 18:05:21 -0400 Subject: [PATCH] fix out of bound python list into numpy array (#5043) numpy 2.0 does not allow oob python const and recommends writing as `np.array(value).astype(dtype)` --- test/test_dtype.py | 6 +----- test/test_tensor.py | 19 +++++++++---------- tinygrad/tensor.py | 2 +- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/test/test_dtype.py b/test/test_dtype.py index ebd64449..57175d0c 100644 --- a/test/test_dtype.py +++ b/test/test_dtype.py @@ -112,11 +112,7 @@ class TestDType(unittest.TestCase): dtypes = list(map(np.dtype, ["bool", "uint8", "int8", "int16", "int32", "int64", "float32", "float64"])) data = [1., 2., 0., 0.5, -1.5, 5.25] for dt in dtypes: - try: - arr = np.asarray(data, dtype=dt) - except OverflowError: - # TODO: this happens with numpy 2.0, update with proper behavior - continue + arr = np.asarray(data).astype(dt) tin = Tensor(arr).numpy() tor = torch.as_tensor(arr).detach().numpy() assert dt == tin.dtype == tor.dtype, f"dtype mismatch: expected={dt} | tinygrad={tin.dtype} | torch={tor.dtype}" diff --git a/test/test_tensor.py b/test/test_tensor.py index c791aa2c..f1ce7ea4 100644 --- a/test/test_tensor.py +++ b/test/test_tensor.py @@ -307,18 +307,17 @@ class TestTinygrad(unittest.TestCase): if is_dtype_supported(dtypes.float16): data = [math.nan, -math.inf, 65504, 65519, 65519.999, 65520, 65520.1] data = data + [-x for x in data] - np.testing.assert_allclose(Tensor(data, dtype=dtypes.float16).numpy(), np.array(data, dtype=np.float16)) + np.testing.assert_allclose(Tensor(data, dtype=dtypes.float16).numpy(), np.array(data).astype(np.float16)) - # TODO: numpy changed this behavior in 2.0 - # # uint32 - # data = [1 << 33, 1 << 32, 1 << 32 - 1, 1] - # data = data + [-x for x in data] - # np.testing.assert_allclose(Tensor(data, dtype=dtypes.uint32).numpy(), np.array(data, dtype=np.uint32)) + # uint32 + data = [1 << 33, 1 << 32, 1 << 32 - 1, 1] + data = data + [-x for x in data] + np.testing.assert_allclose(Tensor(data, dtype=dtypes.uint32).numpy(), np.array(data).astype(np.uint32)) - # # int32 - # data = [1 << 33, 1 << 32, 1 << 32 - 1, 1] - # data = data + [-x for x in data] - # np.testing.assert_allclose(Tensor(data, dtype=dtypes.int32).numpy(), np.array(data, dtype=np.int32)) + # int32 + data = [1 << 33, 1 << 32, 1 << 32 - 1, 1] + data = data + [-x for x in data] + np.testing.assert_allclose(Tensor(data, dtype=dtypes.int32).numpy(), np.array(data).astype(np.int32)) def test_tensor_list_ndarray(self): data = [np.array([1, 2, 3]), np.array([1, 2, 3]), np.array([1, 2, 3])] diff --git a/tinygrad/tensor.py b/tinygrad/tensor.py index 8b6a2d4c..562eb8e3 100644 --- a/tinygrad/tensor.py +++ b/tinygrad/tensor.py @@ -128,7 +128,7 @@ class Tensor: if (d := fully_flatten(data)) and all(isinstance(s, bool) for s in d): dtype = dtypes.bool else: dtype = dtypes.default_int if d and all_int(d) else dtypes.default_float if dtype == dtypes.bfloat16: data = Tensor(_fromnp(np.array(data, np.float32)), device=device).cast(dtypes.bfloat16).lazydata - else: data = _fromnp(np.array(data, _to_np_dtype(dtype))) + else: data = _fromnp(np.array(data).astype(_to_np_dtype(dtype))) elif data is None: data = _loadop(LoadOps.EMPTY, (0,), dtype or dtypes.default_float, device) elif isinstance(data, np.ndarray): if data.shape == (): data = _loadop(LoadOps.CONST, tuple(), dtype or _from_np_dtype(data.dtype), device, data.item())