-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathtest_contextlib_contextmanager.py
More file actions
447 lines (351 loc) · 11.3 KB
/
Copy pathtest_contextlib_contextmanager.py
File metadata and controls
447 lines (351 loc) · 11.3 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# SPDX-FileCopyrightText: Copyright (c) <2026> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
import re
from contextlib import contextmanager
from dataclasses import dataclass
import pytest
import torch
import cuda.tile as ct
from cuda.tile import static_assert
from cuda.tile._exception import TileSyntaxError, TileTypeError
def append(x, val):
i = ct.gather(x, 0) + 1
ct.scatter(x, 0, i)
ct.scatter(x, i, val)
@contextmanager
def foo(x, start_val, end_val):
append(x, start_val)
yield start_val
append(x, end_val)
def test_user_defined_context_manager():
@ct.kernel
def kern(x):
append(x, 10)
with foo(x, 20, 30) as val:
append(x, 40)
static_assert(val == 20)
append(x, 50)
x = torch.zeros(10, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
assert x.tolist() == [5, 10, 20, 40, 30, 50, 0, 0, 0, 0]
def test_nested_with():
@ct.kernel
def kern(x):
append(x, 10)
with foo(x, 20, 30) as val:
static_assert(val == 20)
append(x, 40)
with foo(x, 50, 60) as val2:
static_assert(val2 == 50)
append(x, 70)
append(x, 80)
append(x, 90)
x = torch.zeros(10, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
assert x.tolist() == [9, 10, 20, 40, 50, 70, 60, 80, 30, 90]
def test_break_in_with():
@ct.kernel
def kern(x):
i = 0
while True:
with foo(x, 100 + i, 200 + i):
append(x, i)
if i == 2:
break
i += 1
x = torch.zeros(10, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
assert x.tolist() == [9, 100, 0, 200, 101, 1, 201, 102, 2, 202]
@contextmanager
def wrap_foo(x, a, b, c, d, e, f):
append(x, a)
with foo(x, b, e) as res:
append(x, c)
yield res + 1
append(x, d)
append(x, f)
def test_enter_context_inside_context_manager():
@ct.kernel
def kern(x):
append(x, 10)
with wrap_foo(x, 20, 30, 40, 60, 70, 80) as r:
append(x, 50)
static_assert(r == 31)
append(x, 90)
x = torch.zeros(10, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
assert x.tolist() == [9, 10, 20, 30, 40, 50, 60, 70, 80, 90]
def test_break_in_with_nested_context():
@ct.kernel
def kern(x):
i = 0
while True:
with wrap_foo(x, 10 + i, 20 + i, 30 + i, 40 + i, 50 + i, 60 + i):
append(x, i)
if i == 1:
break
i += 1
x = torch.zeros(20, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
assert x.tolist() == [14,
10, 20, 30, 0, 40, 50, 60, # i=0
11, 21, 31, 1, 41, 51, 61, # i=1
0, 0, 0, 0, 0]
@contextmanager
def enter_context_in_cleanup(x, a, b, c, d, e):
append(x, a)
yield
append(x, b)
with foo(x, c, e):
append(x, d)
def test_enter_context_in_cleanup():
@ct.kernel
def kern(x):
append(x, 10)
i = 0
while True:
with enter_context_in_cleanup(x, 20 + i, 40 + i, 50 + i, 60 + i, 70 + i):
append(x, 30 + i)
if i == 1:
break
i += 1
append(x, 80)
x = torch.zeros(16, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
assert x.tolist() == [14, 10,
20, 30, 40, 50, 60, 70, # i=0
21, 31, 41, 51, 61, 71, # i=1
80, 0]
@contextmanager
def yield_closure(x, a, b, c):
def func():
append(x, a)
append(x, b)
yield func
a += 1
append(x, c)
def test_yield_closure():
@ct.kernel
def kern(x):
with yield_closure(x, 10, 20, 30) as func:
func()
func()
x = torch.zeros(8, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
assert x.tolist() == [4, 20, 10, 30, 11, 0, 0, 0]
def test_yield_closure_break():
@ct.kernel
def kern(x):
i = 0
while True:
with yield_closure(x, 10 + i * 100, 20 + i * 100, 30 + i * 100) as func:
func()
if i == 2:
break
func()
i += 1
x = torch.zeros(15, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
assert x.tolist() == [11,
20, 10, 30, 11, # i=0
120, 110, 130, 111, # i=1
220, 210, 230, # i=2
0, 0, 0]
def test_nested_break_cleanup_order():
@ct.kernel
def kern(x):
i = 0
while True:
with foo(x, 1, 2):
with foo(x, 3, 4):
append(x, 9)
if ct.bid(0) == 0:
break
i += 1
x = torch.zeros(8, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
assert x.tolist() == [5, 1, 3, 9, 4, 2, 0, 0]
def test_nested_continue_cleanup_order():
@ct.kernel
def kern(x):
i = 0
while i < 1:
with foo(x, 1, 2):
with foo(x, 3, 4):
append(x, 9)
i += 1
continue
x = torch.zeros(8, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
assert x.tolist() == [5, 1, 3, 9, 4, 2, 0, 0]
def test_nested_return_cleanup_order():
@ct.kernel
def kern(x):
with foo(x, 1, 2):
with foo(x, 3, 4):
append(x, 9)
return
x = torch.zeros(8, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
assert x.tolist() == [5, 1, 3, 9, 4, 2, 0, 0]
def test_break_with_outer_context_outside_loop():
@ct.kernel
def kern(x):
with foo(x, 1, 2):
while True:
with foo(x, 3, 4):
with foo(x, 5, 6):
append(x, 9)
break
append(x, 7)
x = torch.zeros(10, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
assert x.tolist() == [8, 1, 3, 5, 9, 6, 4, 7, 2, 0]
@pytest.mark.parametrize("return_early", [False, True])
def test_yield_closure_helper_function(return_early):
def helper(x, return_early):
with yield_closure(x, 10, 20, 30) as func:
func()
if return_early:
return func
return func
@ct.kernel
def kern(x, return_early):
func = helper(x, return_early)
func()
x = torch.zeros(8, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kern, (x, return_early))
assert x.tolist() == [4, 20, 10, 30, 11, 0, 0, 0]
@contextmanager
def two_yields(x):
append(x, 1)
yield
append(x, 2)
yield
append(x, 3)
def test_multiple_yields_diagnostic():
@ct.kernel
def kern(x):
with two_yields(x):
append(x, 99)
x = torch.zeros(8, dtype=torch.int32, device="cuda")
with pytest.raises(TileTypeError, match="must have one `yield` statement"):
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
@contextmanager
def no_yield(x):
append(x, 1)
def test_no_yield_diagnostic():
@ct.kernel
def kern(x):
with no_yield(x):
append(x, 99)
x = torch.zeros(8, dtype=torch.int32, device="cuda")
with pytest.raises(TileTypeError, match="no reachable `yield` statement"):
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
@contextmanager
def yield_in_if(x, flag):
append(x, 1)
if flag:
yield
append(x, 2)
def test_yield_in_control_flow_diagnostic():
@ct.kernel
def kern(x):
with yield_in_if(x, True):
append(x, 99)
x = torch.zeros(8, dtype=torch.int32, device="cuda")
with pytest.raises(TileTypeError, match="outside of loops and conditional"):
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
@contextmanager
def dead_yield_after_break(x):
append(x, 1)
yield
while True:
append(x, 3)
break
yield # dead: unreachable after break, must not count as a second yield
def test_dead_yield_after_break():
@ct.kernel
def kern(x):
with dead_yield_after_break(x):
append(x, 9)
x = torch.zeros(8, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
assert x.tolist() == [3, 1, 9, 3, 0, 0, 0, 0]
@contextmanager
def dead_yield_after_continue(x):
append(x, 1)
yield
i = 0
while i < 2:
append(x, 10 + i)
i += 1
continue
yield
def test_dead_yield_after_continue():
@ct.kernel
def kern(x):
with dead_yield_after_continue(x):
append(x, 9)
x = torch.zeros(8, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
assert x.tolist() == [4, 1, 9, 10, 11, 0, 0, 0]
@contextmanager
def only_dead_yield(x):
append(x, 1)
while True:
append(x, 2)
break
yield
def test_only_dead_yield():
@ct.kernel
def kern(x):
with only_dead_yield(x):
append(x, 9)
x = torch.zeros(8, dtype=torch.int32, device="cuda")
with pytest.raises(TileTypeError, match="no reachable `yield` statement"):
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
@contextmanager
def with_return():
if ct.bid(0) == 1:
return
yield
def test_disallow_return():
@ct.kernel
def kern():
with with_return():
pass
with pytest.raises(TileSyntaxError, match="Returning from a generator-based context manager"):
ct.launch(torch.cuda.current_stream(), (1,), kern, ())
@dataclass(frozen=True)
class DataclassWithCtxMethod:
x: ct.Array
@contextmanager
def foo(self, start_val, end_val):
append(self.x, start_val)
yield start_val
append(self.x, end_val)
def test_dataclass_method_context_manager():
@ct.kernel
def kern(x):
append(x, 10)
obj = DataclassWithCtxMethod(x)
with obj.foo(20, 30) as val:
append(x, 40)
static_assert(val == 20)
append(x, 50)
x = torch.zeros(10, dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kern, (x,))
assert x.tolist() == [5, 10, 20, 40, 30, 50, 0, 0, 0, 0]
def test_generator_context_manager_type_str():
@ct.kernel
def kern():
cm = foo(None, 10, end_val=20)
cm + 1 # trigger a type error
msg = (re.escape("Unsupported operand types for +: GeneratorContextManager[<function foo at 0x")
+ "[0-9a-fA-F]+"
+ re.escape(">, args=[None, Tile[int32,()]],"
" kwargs={end_val: Tile[int32,()]}] and Tile[int32,()]"))
with pytest.raises(TileTypeError, match=msg):
ct.launch(torch.cuda.current_stream(), (1,), kern, ())