forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_reduction_defaults.py
More file actions
363 lines (265 loc) · 11.7 KB
/
Copy pathtest_reduction_defaults.py
File metadata and controls
363 lines (265 loc) · 11.7 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
"""Regression tests for reduction-op constructor defaults and empty-dim semantics.
Pins two manifest-conformance invariants for the reduction op family:
1. For the ten ops whose manifest declares ``default: null`` on ``dim``
(Sum/Mean/Amax/Amin/Var/Std/VarMean/All/Any/CountNonzero), constructing
the op with only ``dtype=`` performs a full reduction (output shape
equals ``torch.<op>(x).shape``). ``ProdFwdOp`` keeps its documented
``dim=-1`` default.
2. ``AllFwdOp`` / ``AnyFwdOp`` honor the spec's ``dim=[]`` / ``dim=()``
no-op contract: output shape equals the input shape, output dtype is
``bool``, and values equal ``x.bool()``.
"""
from __future__ import annotations
from tileops.utils import get_backend_name, is_available
DEVICE = get_backend_name()
import pytest
import torch
pytestmark = pytest.mark.skipif(
not is_available(), reason=f"{DEVICE.upper()} required"
)
_FLOAT_SHAPE = (2, 4, 8)
_LOGICAL_SHAPE = (2, 4, 8)
def _make_float(shape: tuple, dtype: torch.dtype) -> torch.Tensor:
return torch.randn(*shape, dtype=dtype, device=DEVICE)
def _make_logical(shape: tuple, dtype: torch.dtype) -> torch.Tensor:
# values in {-1, 0, 1} so .bool() has both T and F.
return (torch.randint(-1, 2, shape, device=DEVICE)).to(dtype)
# ---------------------------------------------------------------------------
# AC-3: default dim=None for the ten ops -> full reduction on 3-D input
# ---------------------------------------------------------------------------
@pytest.mark.smoke
def test_sum_default_dim_full_reduction() -> None:
from tileops.ops.reduction.reduce import SumFwdOp
x = _make_float(_FLOAT_SHAPE, torch.float16)
op = SumFwdOp(dtype=torch.float16)
y = op(x)
assert y.shape == torch.sum(x).shape
@pytest.mark.smoke
def test_mean_default_dim_full_reduction() -> None:
from tileops.ops.reduction.reduce import MeanFwdOp
x = _make_float(_FLOAT_SHAPE, torch.float16)
op = MeanFwdOp(dtype=torch.float16)
y = op(x)
assert y.shape == torch.mean(x).shape
@pytest.mark.smoke
def test_amax_default_dim_full_reduction() -> None:
from tileops.ops.reduction.reduce import AmaxFwdOp
x = _make_float(_FLOAT_SHAPE, torch.float16)
op = AmaxFwdOp(dtype=torch.float16)
y = op(x)
assert y.shape == torch.amax(x).shape
@pytest.mark.smoke
def test_amin_default_dim_full_reduction() -> None:
from tileops.ops.reduction.reduce import AminFwdOp
x = _make_float(_FLOAT_SHAPE, torch.float16)
op = AminFwdOp(dtype=torch.float16)
y = op(x)
assert y.shape == torch.amin(x).shape
@pytest.mark.smoke
def test_var_default_dim_full_reduction() -> None:
from tileops.ops.reduction.reduce import VarFwdOp
x = _make_float(_FLOAT_SHAPE, torch.float16)
op = VarFwdOp(dtype=torch.float16)
y = op(x)
assert y.shape == torch.var(x).shape
@pytest.mark.smoke
def test_std_default_dim_full_reduction() -> None:
from tileops.ops.reduction.reduce import StdFwdOp
x = _make_float(_FLOAT_SHAPE, torch.float16)
op = StdFwdOp(dtype=torch.float16)
y = op(x)
assert y.shape == torch.std(x).shape
@pytest.mark.smoke
def test_var_mean_default_dim_full_reduction() -> None:
from tileops.ops.reduction.reduce import VarMeanFwdOp
x = _make_float(_FLOAT_SHAPE, torch.float16)
op = VarMeanFwdOp(dtype=torch.float16)
var_out, mean_out = op(x)
ref_var, ref_mean = torch.var_mean(x)
assert var_out.shape == ref_var.shape
assert mean_out.shape == ref_mean.shape
@pytest.mark.smoke
def test_all_default_dim_full_reduction() -> None:
from tileops.ops.reduction.all_op import AllFwdOp
x = _make_logical(_LOGICAL_SHAPE, torch.float16)
op = AllFwdOp(dtype=torch.float16)
y = op(x)
assert y.shape == torch.all(x.bool()).shape
assert y.dtype == torch.bool
@pytest.mark.smoke
def test_any_default_dim_full_reduction() -> None:
from tileops.ops.reduction.any_op import AnyFwdOp
x = _make_logical(_LOGICAL_SHAPE, torch.float16)
op = AnyFwdOp(dtype=torch.float16)
y = op(x)
assert y.shape == torch.any(x.bool()).shape
assert y.dtype == torch.bool
@pytest.mark.smoke
def test_count_nonzero_default_dim_full_reduction() -> None:
from tileops.ops.reduction.count_nonzero import CountNonzeroFwdOp
x = _make_logical(_LOGICAL_SHAPE, torch.float16)
op = CountNonzeroFwdOp(dtype=torch.float16)
y = op(x)
assert y.shape == torch.count_nonzero(x).shape
assert y.dtype == torch.int64
# ---------------------------------------------------------------------------
# AC-4: ProdFwdOp keeps documented dim=-1 default
# ---------------------------------------------------------------------------
@pytest.mark.smoke
def test_prod_default_dim_last_axis() -> None:
from tileops.ops.reduction.reduce import ProdFwdOp
# use a narrow value range so fp16 prod is numerically stable
x = torch.rand(*_FLOAT_SHAPE, dtype=torch.float16, device=DEVICE) * 0.01 + 0.99
op = ProdFwdOp(dtype=torch.float16)
y = op(x)
assert y.shape == torch.prod(x, dim=-1).shape
# ---------------------------------------------------------------------------
# AC-5: AllFwdOp/AnyFwdOp dim=[] / dim=() noop contract
# ---------------------------------------------------------------------------
@pytest.mark.smoke
@pytest.mark.parametrize("empty_dim", [[], ()])
def test_all_empty_dim_noop(empty_dim) -> None:
from tileops.ops.reduction.all_op import AllFwdOp
x = _make_logical(_LOGICAL_SHAPE, torch.float16)
op = AllFwdOp(dtype=torch.float16, dim=empty_dim)
y = op(x)
assert y.shape == x.shape
assert y.dtype == torch.bool
assert torch.equal(y, x.bool())
@pytest.mark.smoke
@pytest.mark.parametrize("empty_dim", [[], ()])
def test_any_empty_dim_noop(empty_dim) -> None:
from tileops.ops.reduction.any_op import AnyFwdOp
x = _make_logical(_LOGICAL_SHAPE, torch.float16)
op = AnyFwdOp(dtype=torch.float16, dim=empty_dim)
y = op(x)
assert y.shape == x.shape
assert y.dtype == torch.bool
assert torch.equal(y, x.bool())
# ---------------------------------------------------------------------------
# AC-6: normalize_dim noop policy returns []
# ---------------------------------------------------------------------------
@pytest.mark.smoke
def test_normalize_dim_noop_returns_empty() -> None:
from tileops.ops.reduction._multidim import normalize_dim
assert normalize_dim([], ndim=3, empty_dim_policy="noop") == []
assert normalize_dim((), ndim=3, empty_dim_policy="noop") == []
@pytest.mark.smoke
def test_normalize_dim_reject_raises_on_empty() -> None:
from tileops.ops.reduction._multidim import normalize_dim
with pytest.raises(ValueError):
normalize_dim([], ndim=3, empty_dim_policy="reject")
@pytest.mark.smoke
def test_normalize_dim_full_returns_all() -> None:
from tileops.ops.reduction._multidim import normalize_dim
assert normalize_dim([], ndim=3, empty_dim_policy="full") == [0, 1, 2]
@pytest.mark.smoke
def test_empty_dim_policy_class_attrs() -> None:
"""AC-6: per-op empty_dim_policy bindings."""
from tileops.ops.reduction.all_op import AllFwdOp
from tileops.ops.reduction.any_op import AnyFwdOp
from tileops.ops.reduction.count_nonzero import CountNonzeroFwdOp
from tileops.ops.reduction.reduce import (
AmaxFwdOp,
AminFwdOp,
MeanFwdOp,
ProdFwdOp,
StdFwdOp,
SumFwdOp,
VarFwdOp,
VarMeanFwdOp,
_ReduceOpBase,
)
assert _ReduceOpBase._empty_dim_policy == "reject"
assert AllFwdOp._empty_dim_policy == "noop"
assert AnyFwdOp._empty_dim_policy == "noop"
for cls in (
SumFwdOp, MeanFwdOp, AmaxFwdOp, AminFwdOp,
StdFwdOp, VarFwdOp, VarMeanFwdOp, CountNonzeroFwdOp,
):
assert cls._empty_dim_policy == "full", cls.__name__
# ProdFwdOp inherits default (reject); empty dim is not in its contract
assert ProdFwdOp._empty_dim_policy == "reject"
# ---------------------------------------------------------------------------
# Empty-dim noop must NOT bypass input validation or roofline binding
# ---------------------------------------------------------------------------
@pytest.mark.smoke
def test_all_empty_dim_noop_rejects_cpu_tensor() -> None:
"""dim=[] must still validate device; non-backend input must raise."""
from tileops.ops.reduction.all_op import AllFwdOp
x = (torch.randint(-1, 2, _LOGICAL_SHAPE)).to(torch.float16) # cpu
op = AllFwdOp(dtype=torch.float16, dim=[])
with pytest.raises(ValueError, match=f"{DEVICE.upper()} tensor"):
op(x)
@pytest.mark.smoke
def test_any_empty_dim_noop_rejects_cpu_tensor() -> None:
from tileops.ops.reduction.any_op import AnyFwdOp
x = (torch.randint(-1, 2, _LOGICAL_SHAPE)).to(torch.float16) # cpu
op = AnyFwdOp(dtype=torch.float16, dim=[])
with pytest.raises(ValueError, match=f"{DEVICE.upper()} tensor"):
op(x)
@pytest.mark.smoke
def test_all_empty_dim_noop_rejects_wrong_dtype() -> None:
"""dim=[] must still validate dtype against the op's declared dtype."""
from tileops.ops.reduction.all_op import AllFwdOp
x = _make_logical(_LOGICAL_SHAPE, torch.float32) # backend, fp32
op = AllFwdOp(dtype=torch.float16, dim=[])
with pytest.raises(ValueError, match="Expected x.dtype"):
op(x)
@pytest.mark.smoke
def test_any_empty_dim_noop_rejects_wrong_dtype() -> None:
from tileops.ops.reduction.any_op import AnyFwdOp
x = _make_logical(_LOGICAL_SHAPE, torch.float32)
op = AnyFwdOp(dtype=torch.float16, dim=[])
with pytest.raises(ValueError, match="Expected x.dtype"):
op(x)
@pytest.mark.smoke
def test_all_empty_dim_noop_binds_roofline() -> None:
"""eval_roofline() must succeed after a dim=[] noop forward and
report non-zero data-movement (the noop still reads the input and
writes an equal-shape cast result)."""
from tileops.ops.reduction.all_op import AllFwdOp
x = _make_logical(_LOGICAL_SHAPE, torch.float16)
op = AllFwdOp(dtype=torch.float16, dim=[])
op(x)
flops, mem_bytes = op.eval_roofline()
numel = x.numel()
elem_bytes = x.element_size()
# Noop binds (M=numel, N=1); for the "all" op_kind this gives
# mem_bytes = numel * elem_bytes + numel (input read + bool write).
expected_lower = numel * elem_bytes
expected_upper = 2 * numel * elem_bytes + numel
assert mem_bytes >= expected_lower, (
f"noop bandwidth {mem_bytes} under-counts input read "
f"({expected_lower} bytes)"
)
assert mem_bytes <= expected_upper
# flops are degenerate (one op per element); contract is non-negative.
assert flops >= 0
@pytest.mark.smoke
def test_any_empty_dim_noop_binds_roofline() -> None:
from tileops.ops.reduction.any_op import AnyFwdOp
x = _make_logical(_LOGICAL_SHAPE, torch.float16)
op = AnyFwdOp(dtype=torch.float16, dim=[])
op(x)
flops, mem_bytes = op.eval_roofline()
numel = x.numel()
elem_bytes = x.element_size()
expected_lower = numel * elem_bytes
expected_upper = 2 * numel * elem_bytes + numel
assert mem_bytes >= expected_lower
assert mem_bytes <= expected_upper
assert flops >= 0
@pytest.mark.smoke
def test_validate_dim_rejects_bool_scalar() -> None:
"""`bool` subclasses `int`, but a boolean dim is never a valid axis;
`_validate_dim` must reject it explicitly."""
from tileops.ops.reduction.reduce import SumFwdOp
with pytest.raises(TypeError, match="dim must not be bool"):
SumFwdOp(dtype=torch.float16, dim=True)
@pytest.mark.smoke
def test_validate_dim_rejects_bool_in_list() -> None:
"""Same guard applies element-wise to `list[int]` / `tuple[int, ...]`."""
from tileops.ops.reduction.reduce import SumFwdOp
with pytest.raises(TypeError, match="must be int .not bool"):
SumFwdOp(dtype=torch.float16, dim=[True, 0])