|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) <2026> NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +from enum import Enum, IntEnum |
| 6 | + |
| 7 | +import pytest |
| 8 | +import torch |
| 9 | + |
| 10 | +import cuda.tile as ct |
| 11 | +from cuda.tile import TileTypeError, TileValueError |
| 12 | + |
| 13 | + |
| 14 | +class Color(Enum): |
| 15 | + RED = 0 |
| 16 | + GREEN = 1 |
| 17 | + BLUE = 2 |
| 18 | + |
| 19 | + |
| 20 | +class Status(Enum): |
| 21 | + OK = "ok" |
| 22 | + ERROR = "error" |
| 23 | + |
| 24 | + |
| 25 | +class Weight(Enum): |
| 26 | + LIGHT = 0.5 |
| 27 | + HEAVY = 2.0 |
| 28 | + |
| 29 | + |
| 30 | +class Priority(IntEnum): |
| 31 | + LOW = 0 |
| 32 | + MEDIUM = 1 |
| 33 | + HIGH = 2 |
| 34 | + |
| 35 | + |
| 36 | +def test_comparison_eq(): |
| 37 | + @ct.kernel |
| 38 | + def kernel(out): |
| 39 | + x = Color.RED |
| 40 | + if x == Color.RED: |
| 41 | + ct.scatter(out, (), 1) |
| 42 | + else: |
| 43 | + ct.scatter(out, (), -1) |
| 44 | + |
| 45 | + out = torch.zeros((), dtype=torch.int32, device="cuda") |
| 46 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,)) |
| 47 | + assert out.item() == 1 |
| 48 | + |
| 49 | + |
| 50 | +def test_comparison_not_equal(): |
| 51 | + @ct.kernel |
| 52 | + def kernel(out): |
| 53 | + x = Color.RED |
| 54 | + if x != Color.GREEN: |
| 55 | + ct.scatter(out, (), 1) |
| 56 | + else: |
| 57 | + ct.scatter(out, (), -1) |
| 58 | + |
| 59 | + out = torch.zeros((), dtype=torch.int32, device="cuda") |
| 60 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,)) |
| 61 | + assert out.item() == 1 |
| 62 | + |
| 63 | + |
| 64 | +def test_construction_from_known_int(): |
| 65 | + @ct.kernel |
| 66 | + def kernel(out): |
| 67 | + i = 0 |
| 68 | + x = Color(i) |
| 69 | + if x == Color.RED: |
| 70 | + ct.scatter(out, (), 10) |
| 71 | + elif x == Color.GREEN: |
| 72 | + ct.scatter(out, (), 20) |
| 73 | + else: |
| 74 | + ct.scatter(out, (), 30) |
| 75 | + |
| 76 | + out = torch.zeros((), dtype=torch.int32, device="cuda") |
| 77 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,)) |
| 78 | + assert out.item() == 10 |
| 79 | + |
| 80 | + |
| 81 | +def test_construction_from_string_value(): |
| 82 | + @ct.kernel |
| 83 | + def kernel(out): |
| 84 | + x = Status("ok") |
| 85 | + if x == Status.OK: |
| 86 | + ct.scatter(out, (), 1) |
| 87 | + else: |
| 88 | + ct.scatter(out, (), 0) |
| 89 | + |
| 90 | + out = torch.zeros((), dtype=torch.int32, device="cuda") |
| 91 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,)) |
| 92 | + assert out.item() == 1 |
| 93 | + |
| 94 | + |
| 95 | +def test_construction_from_float_value(): |
| 96 | + @ct.kernel |
| 97 | + def kernel(out): |
| 98 | + x = Weight(0.5) |
| 99 | + if x == Weight.LIGHT: |
| 100 | + ct.scatter(out, (), 1) |
| 101 | + else: |
| 102 | + ct.scatter(out, (), 0) |
| 103 | + |
| 104 | + out = torch.zeros((), dtype=torch.int32, device="cuda") |
| 105 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,)) |
| 106 | + assert out.item() == 1 |
| 107 | + |
| 108 | + |
| 109 | +def test_intenum_ordering(): |
| 110 | + @ct.kernel |
| 111 | + def kernel(out): |
| 112 | + if Priority.LOW < Priority.HIGH: |
| 113 | + ct.scatter(out, (), 1) |
| 114 | + else: |
| 115 | + ct.scatter(out, (), -1) |
| 116 | + |
| 117 | + out = torch.zeros((), dtype=torch.int32, device="cuda") |
| 118 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,)) |
| 119 | + assert out.item() == 1 |
| 120 | + |
| 121 | + |
| 122 | +# =========================================================================== |
| 123 | +# Error cases |
| 124 | +# =========================================================================== |
| 125 | + |
| 126 | +def test_construction_from_runtime_value_raises(): |
| 127 | + @ct.kernel |
| 128 | + def kernel(x, out): |
| 129 | + bid = ct.bid(0) |
| 130 | + _ = Color(bid) |
| 131 | + ct.scatter(out, (), 0) |
| 132 | + |
| 133 | + x = torch.zeros(1, device="cuda") |
| 134 | + out = torch.zeros((), dtype=torch.int32, device="cuda") |
| 135 | + with pytest.raises(TileTypeError): |
| 136 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, out)) |
| 137 | + |
| 138 | + |
| 139 | +@pytest.mark.parametrize("invalid_value", ["foo", 99]) |
| 140 | +def test_construction_from_invalid_type_or_value_raises(invalid_value): |
| 141 | + @ct.kernel |
| 142 | + def kernel(out): |
| 143 | + _ = Color(invalid_value) |
| 144 | + ct.scatter(out, (), 0) |
| 145 | + |
| 146 | + out = torch.zeros((), dtype=torch.int32, device="cuda") |
| 147 | + with pytest.raises(TileValueError): |
| 148 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,)) |
| 149 | + |
| 150 | + |
| 151 | +@pytest.mark.parametrize("enum_value", [Color.BLUE, Status.ERROR, Weight.HEAVY]) |
| 152 | +def test_name_attribute(enum_value): |
| 153 | + name = enum_value.name |
| 154 | + |
| 155 | + @ct.kernel |
| 156 | + def kernel(out): |
| 157 | + if enum_value.name == name: |
| 158 | + ct.scatter(out, (), 1) |
| 159 | + |
| 160 | + out = torch.zeros((), dtype=torch.int32, device="cuda") |
| 161 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,)) |
| 162 | + assert out.item() == 1 |
| 163 | + |
| 164 | + |
| 165 | +@pytest.mark.parametrize("enum_value", [Color.BLUE, Status.ERROR, Weight.HEAVY]) |
| 166 | +def test_value_attribute(enum_value): |
| 167 | + value = enum_value.value |
| 168 | + |
| 169 | + @ct.kernel |
| 170 | + def kernel(out): |
| 171 | + if enum_value.value == value: |
| 172 | + ct.scatter(out, (), 1) |
| 173 | + |
| 174 | + out = torch.zeros((), dtype=torch.int32, device="cuda") |
| 175 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,)) |
| 176 | + assert out.item() == 1 |
| 177 | + |
| 178 | + |
| 179 | +def test_enum_ordering_raises(): |
| 180 | + @ct.kernel |
| 181 | + def kernel(out): |
| 182 | + if Color.RED < Color.GREEN: |
| 183 | + ct.scatter(out, (), 1) |
| 184 | + else: |
| 185 | + ct.scatter(out, (), -1) |
| 186 | + |
| 187 | + out = torch.zeros((), dtype=torch.int32, device="cuda") |
| 188 | + with pytest.raises(TileTypeError): |
| 189 | + ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,)) |
0 commit comments