|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) <2025> NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import pytest |
| 6 | +import torch |
| 7 | +from torch.testing import make_tensor |
| 8 | + |
| 9 | +import cuda.tile as ct |
| 10 | +from cuda.tile._bytecode.version import BytecodeVersion |
| 11 | +from util import assert_equal |
| 12 | +from cuda.tile._exception import TileTypeError |
| 13 | +from conftest import float_dtypes, int_dtypes, requires_tileiras, uint_dtypes, dtype_id |
| 14 | + |
| 15 | +# TODO: remove when feature is out of development only |
| 16 | +from cuda.tile._stub import pack_to_bytes, unpack_from_bytes |
| 17 | +ct.pack_to_bytes = pack_to_bytes |
| 18 | +ct.unpack_from_bytes = unpack_from_bytes |
| 19 | + |
| 20 | +pytestmark = requires_tileiras(BytecodeVersion.V_13_3) |
| 21 | + |
| 22 | +test_dtypes = float_dtypes + int_dtypes + uint_dtypes + [torch.float64] |
| 23 | + |
| 24 | + |
| 25 | +@ct.kernel |
| 26 | +def pack_unpack_1d(x, y, TILE: ct.Constant[int]): |
| 27 | + tx = ct.load(x, index=(0,), shape=(TILE,)) |
| 28 | + packed = ct.pack_to_bytes(tx) |
| 29 | + ty = ct.unpack_from_bytes(packed, y.dtype) |
| 30 | + ct.store(y, index=(0,), tile=ty) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize("dtype", test_dtypes, ids=dtype_id) |
| 34 | +def test_pack_to_bytes(dtype): |
| 35 | + @ct.kernel |
| 36 | + def kernel(x, y, TILE: ct.Constant[int]): |
| 37 | + tx = ct.load(x, index=(0,), shape=(TILE,)) |
| 38 | + ty = ct.pack_to_bytes(tx) |
| 39 | + ct.store(y, index=(0,), tile=ty) |
| 40 | + |
| 41 | + tile = 128 |
| 42 | + x = make_tensor((tile,), dtype=dtype, device='cuda') |
| 43 | + nbytes = tile * x.element_size() |
| 44 | + y = torch.zeros(nbytes, dtype=torch.uint8, device='cuda') |
| 45 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y, tile)) |
| 46 | + ref = x.view(torch.uint8) |
| 47 | + assert_equal(y, ref) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("dtype", test_dtypes, ids=dtype_id) |
| 51 | +def test_unpack_from_bytes(dtype): |
| 52 | + @ct.kernel |
| 53 | + def kernel(x, y, TILE: ct.Constant[int]): |
| 54 | + tx = ct.load(x, index=(0,), shape=(TILE,)) |
| 55 | + ty = ct.unpack_from_bytes(tx, y.dtype) |
| 56 | + ct.store(y, index=(0,), tile=ty) |
| 57 | + |
| 58 | + ref = make_tensor((32,), dtype=dtype, device='cuda') |
| 59 | + x = ref.view(torch.uint8) |
| 60 | + y = torch.zeros_like(ref) |
| 61 | + tile = x.shape[0] |
| 62 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y, tile)) |
| 63 | + assert_equal(y, ref) |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize("dtype", test_dtypes, ids=dtype_id) |
| 67 | +def test_pack_unpack_roundtrip(dtype): |
| 68 | + tile = 128 |
| 69 | + x = make_tensor((tile,), dtype=dtype, device='cuda') |
| 70 | + y = torch.zeros_like(x) |
| 71 | + ct.launch(torch.cuda.current_stream(), (1,), pack_unpack_1d, (x, y, tile)) |
| 72 | + assert_equal(y, x) |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize("dtype", test_dtypes, ids=dtype_id) |
| 76 | +def test_pack_unpack_roundtrip_0d(dtype): |
| 77 | + @ct.kernel |
| 78 | + def kernel(x, y): |
| 79 | + tx = ct.gather(x, ()) |
| 80 | + packed = ct.pack_to_bytes(tx) |
| 81 | + ty = ct.unpack_from_bytes(packed, x.dtype) |
| 82 | + ty = ty.reshape(()) |
| 83 | + ct.scatter(y, (), ty) |
| 84 | + |
| 85 | + x = make_tensor((), dtype=dtype, device='cuda') |
| 86 | + y = torch.zeros_like(x) |
| 87 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y)) |
| 88 | + assert_equal(y, x) |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize("dtype", test_dtypes, ids=dtype_id) |
| 92 | +def test_pack_unpack_roundtrip_2d(dtype): |
| 93 | + @ct.kernel |
| 94 | + def kernel(x, y, TILE_M: ct.Constant[int], TILE_N: ct.Constant[int]): |
| 95 | + bidm = ct.bid(0) |
| 96 | + bidn = ct.bid(1) |
| 97 | + tx = ct.load(x, index=(bidm, bidn), shape=(TILE_M, TILE_N)) |
| 98 | + packed = ct.pack_to_bytes(tx) |
| 99 | + ty = ct.unpack_from_bytes(packed, x.dtype) |
| 100 | + ty = ct.reshape(ty, (TILE_M, TILE_N)) |
| 101 | + ct.store(y, index=(bidm, bidn), tile=ty) |
| 102 | + |
| 103 | + shape = (64, 128) |
| 104 | + tiles = (32, 64) |
| 105 | + x = make_tensor(shape, dtype=dtype, device='cuda') |
| 106 | + y = torch.zeros_like(x) |
| 107 | + grid = (ct.cdiv(shape[0], tiles[0]), ct.cdiv(shape[1], tiles[1])) |
| 108 | + ct.launch(torch.cuda.current_stream(), grid, |
| 109 | + kernel, (x, y, tiles[0], tiles[1])) |
| 110 | + assert_equal(y, x) |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.parametrize("dtype_x", test_dtypes, ids=dtype_id) |
| 114 | +@pytest.mark.parametrize("dtype_y", test_dtypes, ids=dtype_id) |
| 115 | +def test_cross_type_pack_unpack(dtype_x, dtype_y): |
| 116 | + tile = 128 |
| 117 | + x = make_tensor((tile,), dtype=dtype_x, device='cuda') |
| 118 | + ref = x.view(torch.uint8).view(dtype_y) |
| 119 | + y = torch.zeros_like(ref) |
| 120 | + ct.launch(torch.cuda.current_stream(), (1,), pack_unpack_1d, (x, y, tile)) |
| 121 | + assert_equal(y, ref) |
| 122 | + |
| 123 | + |
| 124 | +def test_unpack_from_bytes_not_divisible(): |
| 125 | + @ct.kernel |
| 126 | + def kernel(x, y): |
| 127 | + tx = ct.load(x, index=(0,), shape=(2,)) |
| 128 | + ct.unpack_from_bytes(tx, y.dtype) |
| 129 | + |
| 130 | + x = torch.ones(2, dtype=torch.uint8, device='cuda') |
| 131 | + y = torch.zeros(1, dtype=torch.int32, device='cuda') |
| 132 | + with pytest.raises(TileTypeError, match="not divisible by 32"): |
| 133 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y)) |
| 134 | + |
| 135 | + |
| 136 | +def test_unpack_from_bytes_wrong_input_dtype(): |
| 137 | + @ct.kernel |
| 138 | + def kernel(x, y): |
| 139 | + tx = ct.load(x, index=(0,), shape=(4,)) |
| 140 | + ct.unpack_from_bytes(tx, y.dtype) |
| 141 | + |
| 142 | + x = torch.ones(4, dtype=torch.int32, device='cuda') |
| 143 | + y = torch.zeros(4, dtype=torch.int32, device='cuda') |
| 144 | + with pytest.raises(TileTypeError, match="unpack_from_bytes requires uint8 tile"): |
| 145 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y)) |
| 146 | + |
| 147 | + |
| 148 | +def test_unpack_from_bytes_not_1d(): |
| 149 | + @ct.kernel |
| 150 | + def kernel(x, y): |
| 151 | + tx = ct.load(x, index=(0, 0), shape=(4, 4)) |
| 152 | + ct.unpack_from_bytes(tx, y.dtype) |
| 153 | + |
| 154 | + x = torch.ones((4, 4), dtype=torch.uint8, device='cuda') |
| 155 | + y = torch.zeros(4, dtype=torch.int32, device='cuda') |
| 156 | + with pytest.raises(TileTypeError, match="unpack_from_bytes requires a 1D tile"): |
| 157 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y)) |
| 158 | + |
| 159 | + |
| 160 | +def test_pack_to_bytes_bool(): |
| 161 | + @ct.kernel |
| 162 | + def kernel(x, y, TILE: ct.Constant[int]): |
| 163 | + tx = ct.load(x, index=(0,), shape=(TILE,)) |
| 164 | + ct.pack_to_bytes(tx) |
| 165 | + |
| 166 | + x = torch.ones(4, dtype=torch.bool, device='cuda') |
| 167 | + y = torch.zeros(4, dtype=torch.uint8, device='cuda') |
| 168 | + with pytest.raises(TileTypeError, match="pack_to_bytes from a bool_ tile"): |
| 169 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y, 4)) |
| 170 | + |
| 171 | + |
| 172 | +def test_unpack_from_bytes_bool(): |
| 173 | + @ct.kernel |
| 174 | + def kernel(x, y): |
| 175 | + tx = ct.load(x, index=(0,), shape=(4,)) |
| 176 | + ct.unpack_from_bytes(tx, y.dtype) |
| 177 | + |
| 178 | + x = torch.ones(4, dtype=torch.uint8, device='cuda') |
| 179 | + y = torch.zeros(4, dtype=torch.bool, device='cuda') |
| 180 | + with pytest.raises(TileTypeError, match="unpack_from_bytes to a bool_ tile"): |
| 181 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, y)) |
0 commit comments