-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
259 lines (198 loc) · 6.3 KB
/
test.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# pytest me
import pytest
import logging
from mcl.machine_types import i32, i64, intp, memref
from mcl.vm import Type
from mcl.ndarray import Array, DType, Int32
def test_i32():
a = i32(321)
assert not isinstance(a, Type)
assert not issubclass(type(a), Type) # metaclass not subclass
assert type(a).__mcl_type_descriptor__.machine_repr == "i32"
b = i32(123)
c = a + b
out = c == i32(444)
assert isinstance(out, bool)
assert out
def test_i64():
a = i32(123)
b = i64(321)
c = b + i64(a)
assert c == i64(444)
def test_final():
with pytest.raises(TypeError, match="final type cannot be subclassed"):
class sub_i32(i32):
pass
def test_array():
shape = (intp(3), intp(4))
i32_dtype = DType(Int32)
print(i32_dtype)
data = memref.alloc(shape, i32)
print(data)
ary = Array(dtype=i32_dtype, data=data)
print(ary)
print(ary.shape)
print(ary.dtype)
ary[0, 0] = i32(0xcafe)
res = ary[intp(0), intp(0)]
assert res == i32(0xcafe)
# write loop
c = i32(0)
for i in range(shape[0]):
for j in range(shape[1]):
ary[i, j] = c
c += i32(1)
# read loop
c = i32(0)
for i in range(shape[0]):
for j in range(shape[1]):
got = ary[i, j]
print(f"ary[{i}, {j}] = {got}")
assert got == c
c += i32(1)
def test_broadcast_array():
shape = (intp(3), intp(1), intp(4))
i32_dtype = DType(Int32)
data = memref.alloc(shape, i32)
ary = Array(dtype=i32_dtype, data=data)
# write loop
c = i32(0)
for i in range(shape[0]):
for j in range(shape[2]):
ary[i, intp(0), j] = c
c += i32(1)
# Broadcast to new shape
broad_shape = (intp(5), intp(3), intp(4), intp(4))
ary.broadcast_to(broad_shape)
assert ary.shape == broad_shape
# read loop
for i in range(broad_shape[0]):
for k in range(broad_shape[2]):
# Array has been 'replicated' along the first and third axis
# as a view. (i, k)
# Underlying data has not been modified
c = i32(0)
for j in range(broad_shape[1]):
for l in range(broad_shape[3]):
got = ary[i, j, k, l]
# print(f"ary[{i}, {j}, {k}, {l}] = {got}")
assert got == c
c += i32(1)
def test_broadcast_shapes():
shape_1 = (intp(3), intp(4))
shape_2 = (intp(3), intp(1))
shape_3 = (intp(4), intp(3), intp(1))
out = Array.broadcast_shapes(shape_1, shape_2, shape_3)
assert out == (intp(4), intp(3), intp(4))
def test_broadcast_shapes_2():
# Test non broadcastable shape throws error
shape_1 = (intp(3), intp(4))
shape_2 = (intp(3), intp(1))
shape_3 = (intp(4), intp(3), intp(2))
with pytest.raises(ValueError, match="Shapes are not broadcastable"):
out = Array.broadcast_shapes(shape_1, shape_2, shape_3)
def test_array_slice_getitem():
shape = (intp(3), intp(4), intp(5))
i32_dtype = DType(Int32)
data = memref.alloc(shape, i32)
ary = Array(dtype=i32_dtype, data=data)
# write loop
c = i32(0)
for i in range(shape[0]):
for j in range(shape[1]):
for k in range(shape[2]):
ary[i, j, k] = c
c += i32(1)
# Slice
slice_1 = ary[0]
slice_2 = ary[0, slice(None), 0]
slice_3 = ary[0, slice(1, 3)]
assert slice_1.shape == (intp(4), intp(5))
assert slice_2.shape == (intp(4),)
assert slice_3.shape == (intp(2), intp(5))
# Check contents
for i in range(shape[0]):
for j in range(shape[1]):
for k in range(shape[2]):
c += i32(1)
assert ary[0, j, k] == slice_1[j, k]
assert ary[0, j, 0] == slice_2[j]
if 1 <= j < 3:
assert ary[0, j, k] == slice_3[j - 1, k]
def test_array_slice_setitem():
shape = (intp(3), intp(4))
i32_dtype = DType(Int32)
data = memref.alloc(shape, i32)
ary = Array(dtype=i32_dtype, data=data)
# write loop
c = i32(0)
for i in range(shape[0]):
for j in range(shape[1]):
ary[i, j] = c
c += i32(1)
# Declare a larger array
shape_2 = (intp(3), intp(3), intp(4))
data_2 = memref.alloc(shape_2, i32)
ary_2 = Array(dtype=i32_dtype, data=data_2)
# Setitem slice
ary_2[0] = ary
ary_2[1] = i32(100)
# Check contents
for i in range(shape[0]):
for j in range(shape[1]):
assert ary[i, j] == ary_2[0, i, j]
assert ary_2[1, i, j] == i32(100)
def test_array_copy():
shape = (intp(3), intp(4))
i32_dtype = DType(Int32)
data = memref.alloc(shape, i32)
ary = Array(dtype=i32_dtype, data=data)
# write loop
c = i32(0)
for i in range(shape[0]):
for j in range(shape[1]):
ary[i, j] = c
c += i32(1)
# Declare a copy
ary_copy = ary.copy()
# Check contents
for i in range(shape[0]):
for j in range(shape[1]):
assert ary[i, j] == ary_copy[i, j]
# Modify the copy
ary_copy[0, 0] = i32(10)
assert ary[0, 0] != ary_copy[0, 0]
def test_array_fancy_getitem():
shape = (intp(4), intp(5), intp(6))
i32_dtype = DType(Int32)
data = memref.alloc(shape, i32)
ary = Array(dtype=i32_dtype, data=data)
# write loop
c = i32(0)
for i in range(shape[0]):
for j in range(shape[1]):
for k in range(shape[2]):
ary[i, j, k] = c
c += i32(1)
# Index array
idx_shape = (intp(2), intp(3))
idx_data = memref.alloc(idx_shape, i32)
idx_ary = Array(dtype=i32_dtype, data=idx_data)
idx_ary[0, 0] = i32(0)
idx_ary[0, 1] = i32(1)
idx_ary[0, 2] = i32(2)
idx_ary[1, 0] = i32(2)
idx_ary[1, 1] = i32(1)
idx_ary[1, 2] = i32(0)
# Fancy getitem
fancy_slice = ary[idx_ary]
# Fancy getitem with slice
fancy_slice_2 = ary[slice(1, 3), idx_ary]
# Fancy getitem with int
fancy_slice_3 = ary[0, idx_ary]
# Check shapes
assert fancy_slice.shape == (intp(2), intp(3), intp(5), intp(6))
assert fancy_slice_2.shape == (intp(2), intp(2), intp(3), intp(6))
assert fancy_slice_3.shape == (intp(2), intp(3), intp(6))
# Check contents
pass