-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathtest_tuple.py
More file actions
267 lines (202 loc) · 7.16 KB
/
test_tuple.py
File metadata and controls
267 lines (202 loc) · 7.16 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
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
260
261
262
263
264
265
266
267
# SPDX-FileCopyrightText: Copyright (c) <2026> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
import re
import pytest
import cuda.tile as ct
import torch
from cuda.tile import TileTypeError
from util import assert_equal
def test_tuple_concatenation():
@ct.kernel
def kernel(x, y, z):
a = ct.load(x, (0,), (16,))
b = ct.load(x, (1,), (16,))
c = ct.load(x, (2,), (16,))
t = (a,) + (b, c)
ct.store(y, (0,), t[0])
ct.store(y, (1,), t[1])
ct.store(y, (2,), t[2])
ct.scatter(z, (), len(t))
x = torch.arange(48, dtype=torch.int32, device="cuda")
y = torch.zeros((48,), dtype=torch.int32, device="cuda")
z = torch.zeros((), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y, z))
assert_equal(y, x)
assert z.item() == 3
def test_tuple_getitem_noninteger():
@ct.kernel
def kernel():
t = (1, 2, 3)
t[1.0]
with pytest.raises(TileTypeError, match="Tuple indices must be integers or slices"):
ct.launch(torch.cuda.current_stream(), (1,), kernel, ())
def test_tuple_getitem_nonscalar():
@ct.kernel
def kernel():
t = (1, 2, 3)
i = ct.ones((2,), dtype=ct.int32)
t[i]
with pytest.raises(TileTypeError, match="Tuple indices must be integers or slices"):
ct.launch(torch.cuda.current_stream(), (1,), kernel, ())
def test_tuple_getitem_nonconstant():
@ct.kernel
def kernel():
t = (1, 2, 3)
t[ct.bid(0)]
with pytest.raises(TileTypeError, match="Tuple indices must be constant"):
ct.launch(torch.cuda.current_stream(), (1,), kernel, ())
def test_tuple_getitem_nontile():
@ct.kernel
def kernel():
t = (1, 2, 3)
t[(4, 5)]
with pytest.raises(TileTypeError, match="Tuple indices must be integers or slices"):
ct.launch(torch.cuda.current_stream(), (1,), kernel, ())
def test_tuple_setitem():
@ct.kernel
def kernel():
t = (1, 2, 3)
t[0] = 7
with pytest.raises(TileTypeError,
match="Tuples are immutable: item assignment is not supported"):
ct.launch(torch.cuda.current_stream(), (1,), kernel, ())
def test_build_tuple_starred():
@ct.kernel
def kernel(x):
a = (10, 20, 30)
b = ()
c = (40, 50)
t = (7, *a, 8, *b, 9, *c, 10)
for i, v in ct.static_iter(enumerate(t)):
ct.scatter(x, i, v)
x = torch.zeros(9, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x,))
assert x.tolist() == [7, 10, 20, 30, 8, 9, 40, 50, 10]
def test_pass_tuple_starred_to_user_defined_helper():
def helper(arr, *items):
for i, v in ct.static_iter(enumerate(items)):
ct.scatter(arr, i, v)
@ct.kernel
def kernel(x):
helper(x, 123, *(10, 20), 456, *(30,))
x = torch.zeros(5, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x,))
assert x.tolist() == [123, 10, 20, 456, 30]
def test_pass_tuple_starred_to_builtin():
@ct.kernel
def kernel(x):
args = (x, (), 1234)
ct.scatter(*args)
x = torch.zeros((), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x,))
assert x.item() == 1234
def test_pass_non_tuple_starred():
def helper(*items):
pass
@ct.kernel
def kernel():
tile = ct.ones((4,), dtype=ct.int32)
helper(*tile)
with pytest.raises(TileTypeError, match=re.escape("Expected a tuple after *")):
ct.launch(torch.cuda.current_stream(), (1,), kernel, ())
def test_tuple_compare_empty_eq():
@ct.kernel
def kernel(x):
if () == ():
ct.scatter(x, (), 1)
else:
ct.scatter(x, (), 0)
x = torch.zeros((), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x,))
assert x.item() == 1
def test_tuple_compare_constants_eq():
@ct.kernel
def kernel(x):
if (1, 2, 3) == (1, 2, 3):
ct.scatter(x, (), 1)
else:
ct.scatter(x, (), 0)
x = torch.zeros((), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x,))
assert x.item() == 1
def test_tuple_compare_constants_ne():
@ct.kernel
def kernel(x):
if (1, 2) != (1, 3):
ct.scatter(x, (), 1)
else:
ct.scatter(x, (), 0)
x = torch.zeros((), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x,))
assert x.item() == 1
def test_tuple_compare_different_lengths():
@ct.kernel
def kernel(x):
a = ct.bid(0)
if (a, 1) != (a, 1, 2):
ct.scatter(x, (), 1)
else:
ct.scatter(x, (), 0)
x = torch.zeros((), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x,))
assert x.item() == 1
def test_tuple_compare_0d_tiles_eq():
@ct.kernel
def kernel(x):
a = ct.bid(0)
b = ct.bid(1)
if (a, b) == (0, 0):
ct.scatter(x, (a, b), 1)
else:
ct.scatter(x, (a, b), -1)
x = torch.zeros((2, 2), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (2, 2), kernel, (x,))
assert x.tolist() == [[1, -1], [-1, -1]]
def test_tuple_compare_nd_tile_error():
@ct.kernel
def kernel():
t = ct.ones((4,), dtype=ct.int32)
if (t,) == (t,):
pass
with pytest.raises(TileTypeError, match="not supported for N-D tile elements"):
ct.launch(torch.cuda.current_stream(), (1,), kernel, ())
def test_tuple_compare_unsupported_op():
@ct.kernel
def kernel():
if (1, 2) < (3, 4):
pass
with pytest.raises(TileTypeError, match="not supported for tuples"):
ct.launch(torch.cuda.current_stream(), (1,), kernel, ())
def test_tuple_compare_nested():
@ct.kernel
def kernel(x):
a = ct.bid(0)
if ((a, 1), 2) == ((0, 1), 2):
ct.scatter(x, (a, ), 1)
else:
ct.scatter(x, (a, ), -1)
x = torch.zeros((2, ), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (2, ), kernel, (x,))
assert x.tolist() == [1, -1]
def test_tuple_compare_array_element_error():
@ct.kernel
def kernel(x, y):
if (x,) == (y,):
pass
with pytest.raises(TileTypeError, match="not supported for elements of type"):
ct.launch(torch.cuda.current_stream(), (1,), kernel,
(torch.zeros(4, dtype=torch.int32, device="cuda"),
torch.zeros(4, dtype=torch.int32, device="cuda")))
def test_tuple_compare_constant_args():
@ct.kernel
def kernel(x, M: ct.Constant[int], N: ct.Constant[int]):
if (M, N) == (4, 8):
ct.scatter(x, (), 1)
else:
ct.scatter(x, (), -1)
x = torch.zeros((), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, 4, 8))
assert x.item() == 1
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, 4, 9))
assert x.item() == -1