|
7 | 7 |
|
8 | 8 | from math import ceil |
9 | 9 | import cuda.tile as ct |
10 | | -from util import assert_equal |
| 10 | +from cuda.tile import TileTypeError |
| 11 | +from util import assert_equal, assert_close |
11 | 12 | from conftest import int_dtypes, float_dtypes, dtype_id |
12 | 13 |
|
13 | 14 |
|
14 | 15 | @ct.kernel |
15 | | -def arange(x, TILE: ct.Constant[int]): |
| 16 | +def arange_dynamic_start_step(x, step, TILE: ct.Constant[int]): |
16 | 17 | bid = ct.bid(0) |
17 | | - start = ct.astype(bid * TILE, x.dtype) |
18 | | - tx = start + ct.arange(TILE, dtype=x.dtype) |
| 18 | + tx = ct.arange(TILE, start=bid * TILE, step=step, dtype=x.dtype) |
19 | 19 | ct.store(x, index=(bid,), tile=tx) |
20 | 20 |
|
21 | 21 |
|
22 | 22 | @pytest.mark.parametrize("shape", [(128,)]) |
23 | 23 | @pytest.mark.parametrize("tile", [64]) |
24 | 24 | @pytest.mark.parametrize("dtype", int_dtypes + float_dtypes, ids=dtype_id) |
25 | | -def test_arange(shape, dtype, tile): |
| 25 | +def test_arange_dynamic_start_step(shape, dtype, tile): |
26 | 26 | x = torch.zeros(shape, dtype=dtype, device='cuda') |
27 | 27 | grid = (ceil(shape[0] / tile), 1, 1) |
28 | | - ct.launch(torch.cuda.current_stream(), grid, arange, (x, tile)) |
| 28 | + ct.launch(torch.cuda.current_stream(), grid, arange_dynamic_start_step, (x, 1, tile)) |
29 | 29 | ref = torch.arange(len(x), dtype=dtype, device=x.device) |
30 | 30 | assert_equal(x, ref) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize("size,start,step", [ |
| 34 | + (128, None, None), # arange(size) |
| 35 | + (64, 8, None), (64, -16, None), # arange(size, start) |
| 36 | + (64, 0, 2), (64, 64, -1), (16, -8, -3), (4, 8.5, -1.1), # arange(size, start, step) |
| 37 | + (8, 10, 0) # step=0 |
| 38 | +]) |
| 39 | +@pytest.mark.parametrize("dtype", int_dtypes + float_dtypes, ids=dtype_id) |
| 40 | +def test_arange(size, start, step, dtype): |
| 41 | + @ct.kernel |
| 42 | + def arange_kernel(x): |
| 43 | + if start is None: |
| 44 | + tx = ct.arange(size, dtype=x.dtype) |
| 45 | + elif step is None: |
| 46 | + tx = ct.arange(size, start=start, dtype=x.dtype) |
| 47 | + else: |
| 48 | + tx = ct.arange(size, start=start, step=step, dtype=x.dtype) |
| 49 | + ct.store(x, index=(0,), tile=tx) |
| 50 | + |
| 51 | + x = torch.zeros(size, dtype=dtype, device='cuda') |
| 52 | + ct.launch(torch.cuda.current_stream(), (1, 1, 1), arange_kernel, (x,)) |
| 53 | + if step == 0: |
| 54 | + ref = torch.full((size,), start, dtype=dtype, device=x.device) |
| 55 | + else: |
| 56 | + start = 0 if start is None else start |
| 57 | + step = 1 if step is None else step |
| 58 | + ref = torch.arange(start, start + size * step, step, dtype=dtype, device=x.device) |
| 59 | + assert_close(x, ref) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize("size,start,step,error_message", [ |
| 63 | + (3, None, None, "Result tile shape must be power of 2"), |
| 64 | + (5, 0, 2, "Result tile shape must be power of 2"), |
| 65 | + (0.1, None, None, 'Expected an integer constant') |
| 66 | +]) |
| 67 | +def test_arange_invalid_size(size, start, step, error_message): |
| 68 | + @ct.kernel |
| 69 | + def arange_kernel(x): |
| 70 | + if start is None: |
| 71 | + tx = ct.arange(size, dtype=x.dtype) |
| 72 | + elif step is None: |
| 73 | + tx = ct.arange(size, start=start, dtype=x.dtype) |
| 74 | + else: |
| 75 | + tx = ct.arange(size, start=start, step=step, dtype=x.dtype) |
| 76 | + ct.store(x, index=(0,), tile=tx) |
| 77 | + |
| 78 | + with pytest.raises(TileTypeError, match=error_message): |
| 79 | + x = torch.zeros(1, dtype=torch.int32, device='cuda') |
| 80 | + ct.launch(torch.cuda.current_stream(), (1, 1, 1), arange_kernel, (x,)) |
| 81 | + |
| 82 | + |
| 83 | +def test_arange_reject_dynamic_size(): |
| 84 | + @ct.kernel |
| 85 | + def arange_dynamic_size(x): |
| 86 | + tx = ct.arange(ct.bid(0), dtype=x.dtype) |
| 87 | + ct.store(x, index=(0,), tile=tx) |
| 88 | + |
| 89 | + with pytest.raises(TileTypeError, match="Expected an integer constant"): |
| 90 | + x = torch.zeros(1, dtype=torch.int32, device='cuda') |
| 91 | + ct.launch(torch.cuda.current_stream(), (1, 1, 1), arange_dynamic_size, (x,)) |
0 commit comments