forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_bitwise.py
More file actions
244 lines (183 loc) · 8.13 KB
/
Copy pathtest_bitwise.py
File metadata and controls
244 lines (183 loc) · 8.13 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
from tileops.utils import get_backend_name
DEVICE = get_backend_name()
"""Tests for bitwise elementwise ops (bitwise_and, bitwise_or, bitwise_xor, bitwise_not).
Bitwise ops operate on integer inputs. We use int32 tensors for testing
binary bitwise ops, and all bool/integer dtypes for bitwise_not.
Covers L1 smoke correctness.
"""
import pytest
import torch
from tests.test_base import FixtureBase, TestBase, exact_compare
from tileops.ops.elementwise import (
BitwiseAndFwdOp,
BitwiseNotFwdOp,
BitwiseOrFwdOp,
BitwiseXorFwdOp,
)
# ---------------------------------------------------------------------------
# Shared helpers
# ---------------------------------------------------------------------------
def _exact_compare(output: torch.Tensor, output_ref: torch.Tensor) -> None:
"""Exact comparison for integer outputs."""
assert torch.equal(output, output_ref), (
f"Mismatch: {(output != output_ref).sum().item()} elements differ"
)
class BitwiseTest(TestBase):
"""Reusable test body for bitwise ops."""
def __init__(self, n_total: int, ref_fn):
self.n_total = n_total
self.dtype = torch.int32
self.ref_fn = ref_fn
def gen_inputs(self) -> tuple[torch.Tensor, torch.Tensor]:
a = torch.randint(-1000, 1000, (self.n_total,), dtype=torch.int32, device=DEVICE)
b = torch.randint(-1000, 1000, (self.n_total,), dtype=torch.int32, device=DEVICE)
return a, b
def ref_program(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
return self.ref_fn(a, b)
# ---------------------------------------------------------------------------
# BitwiseAnd op
# ---------------------------------------------------------------------------
class BitwiseAndFixture(FixtureBase):
PARAMS = [
("n_total", [
pytest.param(4_096, marks=pytest.mark.smoke),
pytest.param(16_384, marks=pytest.mark.full),
]),
]
@BitwiseAndFixture
def test_bitwise_and_op(n_total: int) -> None:
test = BitwiseTest(n_total, torch.bitwise_and)
shape = (n_total,)
op = BitwiseAndFwdOp(a_shape=shape, b_shape=shape, dtype=torch.int32)
test.check(op, *test.gen_inputs(), compare=_exact_compare)
# ---------------------------------------------------------------------------
# BitwiseOr op
# ---------------------------------------------------------------------------
class BitwiseOrFixture(FixtureBase):
PARAMS = [
("n_total", [
pytest.param(4_096, marks=pytest.mark.smoke),
pytest.param(16_384, marks=pytest.mark.full),
]),
]
@BitwiseOrFixture
def test_bitwise_or_op(n_total: int) -> None:
test = BitwiseTest(n_total, torch.bitwise_or)
shape = (n_total,)
op = BitwiseOrFwdOp(a_shape=shape, b_shape=shape, dtype=torch.int32)
test.check(op, *test.gen_inputs(), compare=_exact_compare)
# ---------------------------------------------------------------------------
# BitwiseXor op
# ---------------------------------------------------------------------------
class BitwiseXorFixture(FixtureBase):
PARAMS = [
("n_total", [
pytest.param(4_096, marks=pytest.mark.smoke),
pytest.param(16_384, marks=pytest.mark.full),
]),
]
@BitwiseXorFixture
def test_bitwise_xor_op(n_total: int) -> None:
test = BitwiseTest(n_total, torch.bitwise_xor)
shape = (n_total,)
op = BitwiseXorFwdOp(a_shape=shape, b_shape=shape, dtype=torch.int32)
test.check(op, *test.gen_inputs(), compare=_exact_compare)
# ---------------------------------------------------------------------------
# Broadcast pattern tests for binary bitwise ops (L3)
# ---------------------------------------------------------------------------
_BROADCAST_PATTERNS = [
((2, 64, 128), (1, 1, 128)), # bias-add
((2, 64, 128), (2, 64, 1)), # row broadcast
((64, 128), (1, 1)), # scalar broadcast
]
_BITWISE_OPS = [
("bitwise_and", BitwiseAndFwdOp, torch.bitwise_and),
("bitwise_or", BitwiseOrFwdOp, torch.bitwise_or),
("bitwise_xor", BitwiseXorFwdOp, torch.bitwise_xor),
]
class BitwiseBroadcastFixture(FixtureBase):
PARAMS = [
("op_name, op_cls, ref_fn, a_shape, b_shape", [
pytest.param(name, cls, ref, a_s, b_s,
marks=pytest.mark.smoke if i == 0 and j == 0
else pytest.mark.full)
for j, (name, cls, ref) in enumerate(_BITWISE_OPS)
for i, (a_s, b_s) in enumerate(_BROADCAST_PATTERNS)
]),
]
@BitwiseBroadcastFixture
def test_bitwise_broadcast(
op_name, op_cls, ref_fn, a_shape, b_shape,
) -> None:
a = torch.randint(-1000, 1000, a_shape, dtype=torch.int32, device=DEVICE)
b = torch.randint(-1000, 1000, b_shape, dtype=torch.int32, device=DEVICE)
op = op_cls(a_shape=a_shape, b_shape=b_shape, dtype=torch.int32)
ref = ref_fn(a, b)
with torch.no_grad():
out = op(a, b)
_exact_compare(out, ref)
# ---------------------------------------------------------------------------
# BitwiseNot op
# ---------------------------------------------------------------------------
class BitwiseFixture(FixtureBase):
"""Parametrize over torch-supported bitwise_not dtypes."""
PARAMS = [
("n_total, dtype", [
pytest.param(1_048_576, torch.bool, marks=pytest.mark.smoke),
pytest.param(1_048_576, torch.uint8, marks=pytest.mark.smoke),
pytest.param(1_048_576, torch.int8, marks=pytest.mark.smoke),
pytest.param(1_048_576, torch.int16, marks=pytest.mark.smoke),
pytest.param(1_048_576, torch.int32, marks=pytest.mark.smoke),
pytest.param(1_048_576, torch.int64, marks=pytest.mark.smoke),
]),
]
class BitwiseNotTest(TestBase):
"""Test harness for bitwise_not."""
def __init__(self, n_total: int, dtype: torch.dtype):
self.n_total = n_total
self.dtype = dtype
def gen_inputs(self) -> tuple[torch.Tensor]:
if self.dtype == torch.bool:
x = torch.rand(self.n_total, device=DEVICE) > 0.5
elif self.dtype == torch.uint8:
x = torch.randint(0, 256, (self.n_total,), device=DEVICE, dtype=self.dtype)
else:
x = torch.randint(-128, 128, (self.n_total,), device=DEVICE, dtype=self.dtype)
return (x,)
def ref_program(self, x: torch.Tensor) -> torch.Tensor:
return torch.bitwise_not(x)
@BitwiseFixture
def test_bitwise_not(n_total: int, dtype: torch.dtype) -> None:
test = BitwiseNotTest(n_total, dtype)
op = BitwiseNotFwdOp(N_total=n_total, dtype=dtype)
test.check(op, *test.gen_inputs(), compare=exact_compare)
@pytest.mark.parametrize("dtype", [
pytest.param(torch.float16, marks=pytest.mark.smoke),
pytest.param(torch.bfloat16, marks=pytest.mark.smoke),
pytest.param(torch.float32, marks=pytest.mark.smoke),
])
def test_bitwise_not_rejects_float_dtype(dtype: torch.dtype) -> None:
from tileops.kernels.elementwise import BitwiseNotFwdKernel
with pytest.raises(ValueError, match="only supports dtypes"):
BitwiseNotFwdKernel(N_total=16, dtype=dtype)
# ---------------------------------------------------------------------------
# Dtype rejection tests for binary bitwise ops
# ---------------------------------------------------------------------------
class BitwiseBinaryRejectFixture(FixtureBase):
PARAMS = [
("op_cls, dtype", [
pytest.param(BitwiseAndFwdOp, torch.float16, marks=pytest.mark.smoke),
pytest.param(BitwiseAndFwdOp, torch.bfloat16, marks=pytest.mark.smoke),
pytest.param(BitwiseAndFwdOp, torch.float32, marks=pytest.mark.smoke),
pytest.param(BitwiseOrFwdOp, torch.float16, marks=pytest.mark.full),
pytest.param(BitwiseXorFwdOp, torch.float16, marks=pytest.mark.full),
]),
]
@BitwiseBinaryRejectFixture
def test_bitwise_binary_rejects_float_dtype(op_cls, dtype: torch.dtype) -> None:
"""Binary bitwise ops only support integer dtypes; floats must be rejected."""
shape = (16,)
with pytest.raises(ValueError, match="does not support dtype"):
op_cls(a_shape=shape, b_shape=shape, dtype=dtype)
if __name__ == "__main__":
pytest.main([__file__, "-vvs"])