-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathtest_enum.py
More file actions
189 lines (142 loc) · 4.78 KB
/
Copy pathtest_enum.py
File metadata and controls
189 lines (142 loc) · 4.78 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
# SPDX-FileCopyrightText: Copyright (c) <2026> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
from enum import Enum, IntEnum
import pytest
import torch
import cuda.tile as ct
from cuda.tile import TileTypeError, TileValueError
class Color(Enum):
RED = 0
GREEN = 1
BLUE = 2
class Status(Enum):
OK = "ok"
ERROR = "error"
class Weight(Enum):
LIGHT = 0.5
HEAVY = 2.0
class Priority(IntEnum):
LOW = 0
MEDIUM = 1
HIGH = 2
def test_comparison_eq():
@ct.kernel
def kernel(out):
x = Color.RED
if x == Color.RED:
ct.scatter(out, (), 1)
else:
ct.scatter(out, (), -1)
out = torch.zeros((), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,))
assert out.item() == 1
def test_comparison_not_equal():
@ct.kernel
def kernel(out):
x = Color.RED
if x != Color.GREEN:
ct.scatter(out, (), 1)
else:
ct.scatter(out, (), -1)
out = torch.zeros((), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,))
assert out.item() == 1
def test_construction_from_known_int():
@ct.kernel
def kernel(out):
i = 0
x = Color(i)
if x == Color.RED:
ct.scatter(out, (), 10)
elif x == Color.GREEN:
ct.scatter(out, (), 20)
else:
ct.scatter(out, (), 30)
out = torch.zeros((), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,))
assert out.item() == 10
def test_construction_from_string_value():
@ct.kernel
def kernel(out):
x = Status("ok")
if x == Status.OK:
ct.scatter(out, (), 1)
else:
ct.scatter(out, (), 0)
out = torch.zeros((), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,))
assert out.item() == 1
def test_construction_from_float_value():
@ct.kernel
def kernel(out):
x = Weight(0.5)
if x == Weight.LIGHT:
ct.scatter(out, (), 1)
else:
ct.scatter(out, (), 0)
out = torch.zeros((), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,))
assert out.item() == 1
def test_intenum_ordering():
@ct.kernel
def kernel(out):
if Priority.LOW < Priority.HIGH:
ct.scatter(out, (), 1)
else:
ct.scatter(out, (), -1)
out = torch.zeros((), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,))
assert out.item() == 1
# ===========================================================================
# Error cases
# ===========================================================================
def test_construction_from_runtime_value_raises():
@ct.kernel
def kernel(x, out):
bid = ct.bid(0)
_ = Color(bid)
ct.scatter(out, (), 0)
x = torch.zeros(1, device="cuda")
out = torch.zeros((), dtype=torch.int32, device="cuda")
with pytest.raises(TileTypeError):
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x, out))
@pytest.mark.parametrize("invalid_value", ["foo", 99])
def test_construction_from_invalid_type_or_value_raises(invalid_value):
@ct.kernel
def kernel(out):
_ = Color(invalid_value)
ct.scatter(out, (), 0)
out = torch.zeros((), dtype=torch.int32, device="cuda")
with pytest.raises(TileValueError):
ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,))
@pytest.mark.parametrize("enum_value", [Color.BLUE, Status.ERROR, Weight.HEAVY])
def test_name_attribute(enum_value):
name = enum_value.name
@ct.kernel
def kernel(out):
if enum_value.name == name:
ct.scatter(out, (), 1)
out = torch.zeros((), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,))
assert out.item() == 1
@pytest.mark.parametrize("enum_value", [Color.BLUE, Status.ERROR, Weight.HEAVY])
def test_value_attribute(enum_value):
value = enum_value.value
@ct.kernel
def kernel(out):
if enum_value.value == value:
ct.scatter(out, (), 1)
out = torch.zeros((), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,))
assert out.item() == 1
def test_enum_ordering_raises():
@ct.kernel
def kernel(out):
if Color.RED < Color.GREEN:
ct.scatter(out, (), 1)
else:
ct.scatter(out, (), -1)
out = torch.zeros((), dtype=torch.int32, device="cuda")
with pytest.raises(TileTypeError):
ct.launch(torch.cuda.current_stream(), (1,), kernel, (out,))