forked from tinygrad/tinygrad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tensor_data.py
More file actions
49 lines (42 loc) · 1.29 KB
/
test_tensor_data.py
File metadata and controls
49 lines (42 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import unittest
from tinygrad import Tensor, dtypes
# format types: https://docs.python.org/3/library/struct.html
class TestTensorData(unittest.TestCase):
def test_data(self):
a = Tensor([1,2,3,4], dtype=dtypes.int32)
dat = a.data()
assert dat.itemsize == 4
assert list(dat) == [1,2,3,4]
assert dat.shape == (4,)
assert dat[0] == 1
assert dat[1] == 2
def test_data_uint8(self):
a = Tensor([1,2,3,4], dtype=dtypes.uint8)
dat = a.data()
assert dat.format == "B"
assert dat.itemsize == 1
assert dat[0] == 1
assert dat[1] == 2
def test_data_nested(self):
a = Tensor([[1,2],[3,4]], dtype=dtypes.int32)
dat = a.data()
assert dat.format == "i"
assert dat.itemsize == 4
assert dat.tolist() == [[1, 2], [3, 4]]
assert dat.shape == (2,2)
assert dat[0, 0] == 1
assert dat[1, 1] == 4
def test_data_float32(self):
a = Tensor([[1,2.5],[3,4]], dtype=dtypes.float32)
dat = a.data()
assert dat.format == "f"
assert dat[0, 1] == 2.5
@unittest.skip("requires python 3.12")
def test_data_float16(self):
a = Tensor([[1,2.5],[3,4]], dtype=dtypes.float16)
dat = a.data()
assert dat.format == "e"
assert dat.shape == (2,2)
# NOTE: python can't deref float16
if __name__ == '__main__':
unittest.main()