-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathtest_helper_function.py
More file actions
366 lines (280 loc) · 11.4 KB
/
test_helper_function.py
File metadata and controls
366 lines (280 loc) · 11.4 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# SPDX-FileCopyrightText: Copyright (c) <2025> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
import inspect
import pytest
import torch
from math import ceil
import cuda.tile as ct
from util import assert_close
from cuda.tile._exception import TileTypeError, TileSyntaxError, TileRecursionError
@pytest.fixture
def shape():
return (512, 128)
@pytest.fixture
def tile():
return 16
def helper_function(input):
return input + 1
def main_kernel_calling_helper(x, y, output, B: ct.Constant[int], N: ct.Constant[int]):
px = ct.bid(0)
tile_x = ct.load(x, index=(px, 0), shape=(B, N))
tile_y = ct.load(y, index=(px, 0), shape=(B, 1))
x1 = helper_function(tile_x)
# This should be a deduped call as tile_y and y1 have the same type.
y1 = helper_function(tile_y)
y2 = helper_function(y1)
out = x1 + y2
ct.store(output, index=(px, 0), tile=out)
def test_helper_function_multiple_calls(shape, tile):
x = torch.rand(shape, dtype=torch.float32, device="cuda")
y = torch.rand((shape[0], 1), dtype=torch.float32, device="cuda")
z = torch.zeros_like(x)
kernel = ct.kernel(main_kernel_calling_helper)
grid = (ceil(shape[0] / tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, kernel, (x, y, z, tile, shape[1]))
ref_result = x + y + 3
assert_close(z, ref_result, atol=1e-4, rtol=1e-5)
def helper_function_no_return():
# Do nothing for now.
pass
def helper_function_multiple_returns(input, input1):
return input + 1, input1 + 1
@ct.kernel
def main_kernel_multiple_returns(x, y, output, B: ct.Constant[int], N: ct.Constant[int]):
px = ct.bid(0)
tile_x = ct.load(x, index=(px, 0), shape=(B, N))
tile_y = ct.load(y, index=(px, 0), shape=(B, 1))
helper_function_no_return()
x1, y1 = helper_function_multiple_returns(tile_x, tile_y)
out = x1 + y1
ct.store(output, index=(px, 0), tile=out)
def test_helper_function_multiple_returns(shape, tile):
x = torch.rand(shape, dtype=torch.float32, device="cuda")
y = torch.rand((shape[0], 1), dtype=torch.float32, device="cuda")
z = torch.zeros_like(x)
grid = (ceil(shape[0] / tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, main_kernel_multiple_returns,
(x, y, z, tile, shape[1]))
ref_result = x + y + 2
assert_close(z, ref_result, atol=1e-4, rtol=1e-5)
def helper_function_keyword_args(input, input1, arg=False):
offset = 1 if arg else -1
return input + offset, input1 + offset
@ct.kernel
def main_kernel_keyword_args(x, y, output, B: ct.Constant[int], N: ct.Constant[int], arg):
px = ct.bid(0)
tile_x = ct.load(x, index=(px, 0), shape=(B, N))
tile_y = ct.load(y, index=(px, 0), shape=(B, 1))
x1, y1 = helper_function_keyword_args(tile_x, tile_y, arg=arg)
out = x1 + y1
ct.store(output, index=(px, 0), tile=out)
@ct.kernel
def main_kernel_keyword_args_default(x, y, output, B: ct.Constant[int], N: ct.Constant[int]):
px = ct.bid(0)
tile_x = ct.load(x, index=(px, 0), shape=(B, N))
tile_y = ct.load(y, index=(px, 0), shape=(B, 1))
x1, y1 = helper_function_keyword_args(tile_x, tile_y)
out = x1 + y1
ct.store(output, index=(px, 0), tile=out)
@pytest.mark.parametrize("func", [main_kernel_keyword_args,
main_kernel_keyword_args_default])
def test_helper_function_keyword_args(shape, tile, func):
x = torch.rand(shape, dtype=torch.float32, device="cuda")
y = torch.rand((shape[0], 1), dtype=torch.float32, device="cuda")
z = torch.zeros_like(x)
grid = (ceil(shape[0] / tile), 1, 1)
if func is main_kernel_keyword_args:
ct.launch(torch.cuda.current_stream(), grid, main_kernel_keyword_args,
(x, y, z, tile, shape[1], False))
else:
ct.launch(torch.cuda.current_stream(), grid, main_kernel_keyword_args_default,
(x, y, z, tile, shape[1]))
ref_result = x + y - 2
assert_close(z, ref_result, atol=1e-4, rtol=1e-5)
def helper_function_recursive_calls(input, N):
if N > 0:
x = helper_function_recursive_calls(input, N - 1) + 1
else:
x = input
return x
@ct.kernel
def main_kernel_recursive_calls(x, output, N: ct.Constant[int]):
tile_x = ct.gather(x, ())
x1 = helper_function_recursive_calls(tile_x, N)
ct.scatter(output, (), x1)
def test_reject_runaway_recursion():
x = torch.tensor(100.0, dtype=torch.float32, device="cuda")
y = torch.zeros_like(x)
with pytest.raises(TileRecursionError):
ct.launch(torch.cuda.current_stream(), (1,), main_kernel_recursive_calls,
(x, y, 100000))
def test_accept_reasonable_recursion():
x = torch.tensor(100.0, dtype=torch.float32, device="cuda")
y = torch.zeros_like(x)
ct.launch(torch.cuda.current_stream(), (1,), main_kernel_recursive_calls,
(x, y, 109))
assert y.item() == 209.0
def helper_function_array_arguments(x, y, output, B: ct.Constant[int], N: ct.Constant[int]):
px = ct.bid(0)
tile_x = ct.load(x, index=(px, 0), shape=(B, N))
tile_y = ct.load(y, index=(px, 0), shape=(B, 1))
out = tile_x + tile_y
ct.store(output, index=(px, 0), tile=out)
@ct.kernel
def main_kernel_array_arguments_in_helper(x, y, output, B: ct.Constant[int], N: ct.Constant[int]):
helper_function_array_arguments(x, y, output, B, N)
def test_helper_function_array_arguments(shape, tile):
x = torch.rand(shape, dtype=torch.float32, device="cuda")
y = torch.rand((shape[0], 1), dtype=torch.float32, device="cuda")
z = torch.zeros_like(x)
grid = (ceil(shape[0] / tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, main_kernel_array_arguments_in_helper,
(x, y, z, tile, shape[1]))
ref_result = x + y
assert_close(z, ref_result, atol=1e-4, rtol=1e-5)
def helper_function_early_return(tile_x, tile_y, early_return):
if early_return:
return tile_x
return tile_x + tile_y
@ct.kernel
def helper_function_early_return_kernel(x, y, output,
B: ct.Constant[int],
N: ct.Constant[int],
early_return: bool):
px = ct.bid(0)
tile_x = ct.load(x, index=(px, 0), shape=(B, N))
tile_y = ct.load(y, index=(px, 0), shape=(B, 1))
out = helper_function_early_return(tile_x, tile_y, early_return)
ct.store(output, index=(px, 0), tile=out)
@pytest.mark.parametrize("early_return", [True, False])
def test_helper_function_early_return(early_return):
shape = (512, 128)
tile = 16
x = torch.rand(shape, dtype=torch.float32, device="cuda")
y = torch.rand((shape[0], 1), dtype=torch.float32, device="cuda")
z = torch.zeros_like(x)
grid = (ceil(shape[0] / tile), 1, 1)
ct.launch(torch.cuda.current_stream(), grid, helper_function_early_return_kernel,
(x, y, z, tile, shape[1], early_return))
ref = x if early_return else x + y
assert_close(z, ref)
def early_return_inside_while_loop(n):
a, b = 1, 1
while True:
a, b = b, a + b
if b > n:
return b
def early_return_inside_for_loop(n):
a, b = 1, 1
for i in range(n):
a, b = b, a + b
if b > n:
return b
def early_return_inside_loop(helper_func):
@ct.kernel
def early_return_inside_loop_kernel(n, y):
n = ct.load(n, index=(0,), shape=(1,))
res = ct.full((1,), helper_func(n.item()), dtype=ct.int32)
ct.store(y, (0,), res)
return early_return_inside_loop_kernel
def test_early_return_inside_while_loop():
n = torch.tensor([15], dtype=torch.int32, device="cuda")
out = torch.zeros_like(n)
kernel = early_return_inside_loop(early_return_inside_while_loop)
ct.launch(torch.cuda.current_stream(), (1,), kernel, (n, out))
assert out.cpu().item() == 21
def test_early_return_inside_for_loop():
n = torch.tensor([15], dtype=torch.int32, device="cuda")
out = torch.zeros_like(n)
kernel = early_return_inside_loop(early_return_inside_for_loop)
with pytest.raises(TileSyntaxError, match="Returning from a for loop is not supported"):
ct.launch(torch.cuda.current_stream(), (1,), kernel, (n, out))
def return_after_while_loop(n):
while n > 0:
n = n - 1
return n
def test_return_after_while_loop():
n = torch.tensor([3], dtype=torch.int32, device="cuda")
out = torch.zeros_like(n)
kernel = early_return_inside_loop(return_after_while_loop)
ct.launch(torch.cuda.current_stream(), (1,), kernel, (n, out))
assert out.cpu().item() == 0
def loops(n):
a = 0
for i in range(n):
a += 1
for j in range(n):
a += 1
return a
@ct.kernel
def loops_kernel(n, y):
n = ct.load(n, index=(0,), shape=(1,))
res = ct.full((1,), loops(n.item()), dtype=ct.int32)
ct.store(y, (0,), res)
def test_loops_in_helper_function():
n = torch.tensor([5], dtype=torch.int32, device="cuda")
out = torch.zeros_like(n)
ct.launch(torch.cuda.current_stream(), (1,), loops_kernel, (n, out))
assert out.cpu().item() == 30
def helper_reassign_param(bid):
bid = bid + 10
return bid + 5
@ct.kernel
def call_helper_reassign_param(x):
val = helper_reassign_param(ct.bid(0))
t = ct.full((1,), val, ct.int32)
ct.store(x, (0,), t)
def test_helper_function_reassign_param():
x = torch.zeros((1,), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), call_helper_reassign_param, (x,))
assert x.cpu().item() == 15
@ct.function
def helper_function_using_ct_api(x, output, B: ct.Constant[int], N: ct.Constant[int]):
px = ct.bid(0)
tile_x = ct.load(x, index=(px, 0), shape=(B, N))
ct.store(output, index=(px, 0), tile=tile_x + 1)
def test_calling_function_from_host(shape, tile):
x = torch.rand(shape, dtype=torch.float32, device="cuda")
y = torch.zeros_like(x)
with pytest.raises(RuntimeError, match="Tile functions can only be called from tile code."):
helper_function_using_ct_api(x, y, tile, shape[1])
@ct.kernel
def kernel_calling_function_using_ct_api(x, output, B: ct.Constant[int], N: ct.Constant[int]):
helper_function_using_ct_api(x, output, B, N)
def test_helper_function_using_ct_api(shape, tile):
x = torch.rand(shape, dtype=torch.float32, device="cuda")
y = torch.zeros_like(x)
grid = (ceil(shape[0] / tile), 1, 1)
ct.launch(
torch.cuda.current_stream(),
grid,
kernel_calling_function_using_ct_api,
(x, y, tile, shape[1])
)
ref_result = x + 1
assert_close(y, ref_result, atol=1e-4, rtol=1e-5)
def test_error_message_stack_trace():
def bar(x): # Line +1
ct.abracadabra(x)
def foo(x): # Line + 4
bar(x)
@ct.kernel
def kernel(x): # Line +8
foo(x)
x = torch.zeros((), device="cuda")
_, first_line = inspect.getsourcelines(test_error_message_stack_trace)
msg_regex = (
"No such attribute 'abracadabra'.*\n"
f".*test_helper_function.py\", line {first_line + 9}.*, in kernel:\n"
f" *foo\\(x\\)\n"
f" *\\^\\^\\^\\^\\^\\^\n"
f".*test_helper_function.py\", line {first_line + 5}.*, in foo:\n"
f" *bar\\(x\\)\n"
f" *\\^\\^\\^\\^\\^\\^\n"
f".*test_helper_function.py\", line {first_line + 2}.*, in bar:\n"
f" ct.abracadabra\\(x\\)\n"
f" \\^\\^\\^\\^\\^\\^\\^\\^\\^\\^\\^\\^\\^\\^\n"
)
with pytest.raises(TileTypeError, match=msg_regex):
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x,))