mirror of https://github.com/commaai/tinygrad.git
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)`
This commit is contained in:
parent
4e5add4d01
commit
cc2be9064f
|
@ -112,11 +112,7 @@ class TestDType(unittest.TestCase):
|
||||||
dtypes = list(map(np.dtype, ["bool", "uint8", "int8", "int16", "int32", "int64", "float32", "float64"]))
|
dtypes = list(map(np.dtype, ["bool", "uint8", "int8", "int16", "int32", "int64", "float32", "float64"]))
|
||||||
data = [1., 2., 0., 0.5, -1.5, 5.25]
|
data = [1., 2., 0., 0.5, -1.5, 5.25]
|
||||||
for dt in dtypes:
|
for dt in dtypes:
|
||||||
try:
|
arr = np.asarray(data).astype(dt)
|
||||||
arr = np.asarray(data, dtype=dt)
|
|
||||||
except OverflowError:
|
|
||||||
# TODO: this happens with numpy 2.0, update with proper behavior
|
|
||||||
continue
|
|
||||||
tin = Tensor(arr).numpy()
|
tin = Tensor(arr).numpy()
|
||||||
tor = torch.as_tensor(arr).detach().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}"
|
assert dt == tin.dtype == tor.dtype, f"dtype mismatch: expected={dt} | tinygrad={tin.dtype} | torch={tor.dtype}"
|
||||||
|
|
|
@ -307,18 +307,17 @@ class TestTinygrad(unittest.TestCase):
|
||||||
if is_dtype_supported(dtypes.float16):
|
if is_dtype_supported(dtypes.float16):
|
||||||
data = [math.nan, -math.inf, 65504, 65519, 65519.999, 65520, 65520.1]
|
data = [math.nan, -math.inf, 65504, 65519, 65519.999, 65520, 65520.1]
|
||||||
data = data + [-x for x in data]
|
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
|
||||||
# # uint32
|
data = [1 << 33, 1 << 32, 1 << 32 - 1, 1]
|
||||||
# data = [1 << 33, 1 << 32, 1 << 32 - 1, 1]
|
data = data + [-x for x in data]
|
||||||
# data = data + [-x for x in data]
|
np.testing.assert_allclose(Tensor(data, dtype=dtypes.uint32).numpy(), np.array(data).astype(np.uint32))
|
||||||
# np.testing.assert_allclose(Tensor(data, dtype=dtypes.uint32).numpy(), np.array(data, dtype=np.uint32))
|
|
||||||
|
|
||||||
# # int32
|
# int32
|
||||||
# data = [1 << 33, 1 << 32, 1 << 32 - 1, 1]
|
data = [1 << 33, 1 << 32, 1 << 32 - 1, 1]
|
||||||
# data = data + [-x for x in data]
|
data = data + [-x for x in data]
|
||||||
# np.testing.assert_allclose(Tensor(data, dtype=dtypes.int32).numpy(), np.array(data, dtype=np.int32))
|
np.testing.assert_allclose(Tensor(data, dtype=dtypes.int32).numpy(), np.array(data).astype(np.int32))
|
||||||
|
|
||||||
def test_tensor_list_ndarray(self):
|
def test_tensor_list_ndarray(self):
|
||||||
data = [np.array([1, 2, 3]), np.array([1, 2, 3]), np.array([1, 2, 3])]
|
data = [np.array([1, 2, 3]), np.array([1, 2, 3]), np.array([1, 2, 3])]
|
||||||
|
|
|
@ -128,7 +128,7 @@ class Tensor:
|
||||||
if (d := fully_flatten(data)) and all(isinstance(s, bool) for s in d): dtype = dtypes.bool
|
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
|
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
|
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 data is None: data = _loadop(LoadOps.EMPTY, (0,), dtype or dtypes.default_float, device)
|
||||||
elif isinstance(data, np.ndarray):
|
elif isinstance(data, np.ndarray):
|
||||||
if data.shape == (): data = _loadop(LoadOps.CONST, tuple(), dtype or _from_np_dtype(data.dtype), device, data.item())
|
if data.shape == (): data = _loadop(LoadOps.CONST, tuple(), dtype or _from_np_dtype(data.dtype), device, data.item())
|
||||||
|
|
Loading…
Reference in New Issue