don't use numpy to create Tensor(None) (#2909)

* don't use numpy to create Tensor(None)

empty suffices

* parentheses
This commit is contained in:
chenyu
2023-12-22 01:07:44 -05:00
committed by GitHub
parent 50cfb1fb3a
commit 3855432265
2 changed files with 2 additions and 1 deletions
+1
View File
@@ -252,6 +252,7 @@ class TestTypeSpec(unittest.TestCase):
def test_creation(self, default_int, default_float):
dtypes.default_int, dtypes.default_float = default_int, default_float
assert Tensor(True).dtype == dtypes.bool
assert Tensor(None).dtype == dtypes.default_float
assert Tensor(2).dtype == dtypes.default_int
assert Tensor(2.34).dtype == dtypes.default_float
assert Tensor([]).dtype == dtypes.default_float
+1 -1
View File
@@ -62,7 +62,7 @@ class Tensor:
if isinstance(data, LazyBuffer): assert dtype is None or dtype == data.dtype, "dtype doesn't match, and casting isn't supported"
elif isinstance(data, (bool, int, float)): data = LazyBuffer.loadop(LoadOps.CONST, tuple(), dtype or dtypes.from_py(data), device, data)
elif isinstance(data, bytes): data = LazyBuffer.fromCPU(np.frombuffer(data, np.uint8))
elif data is None: data = LazyBuffer.fromCPU(np.array([], dtype=(dtype or dtypes.default_float).np))
elif data is None: data = LazyBuffer.loadop(LoadOps.EMPTY, (0,), dtype or dtypes.default_float, device)
elif isinstance(data, list):
if (d := fully_flatten(data)) and all(isinstance(s, bool) for s in d): dtype = dtype or dtypes.bool
elif d and all_int(d): dtype = dtype or dtypes.default_int