forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_reduction_scalar_input.py
More file actions
286 lines (221 loc) · 10.2 KB
/
Copy pathtest_reduction_scalar_input.py
File metadata and controls
286 lines (221 loc) · 10.2 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
"""Scalar-input conformance tests for status-implemented reduction ops.
Asserts that each of ``Sum/Mean/Amax/Amin/Var/Std/VarMean/All/Any/CountNonzero``
accepts a 0-D input tensor paired with each ``dim`` form PyTorch accepts
(``None``, ``0``, ``-1``, ``()``, ``[]``) and returns a result that matches
the corresponding ``torch.<op>`` reference in both value and shape.
For the Welford family (``VarFwdOp``, ``StdFwdOp``, ``VarMeanFwdOp``) the
scalar input with default ``correction=1`` produces ``nan`` and emits a
``UserWarning`` matching PyTorch's behavior.
"""
from __future__ import annotations
from tileops.utils import get_backend_name, is_available
DEVICE = get_backend_name()
import warnings
import pytest
import torch
pytestmark = pytest.mark.skipif(
not is_available(), reason=f"{DEVICE.upper()} required"
)
_DIM_FORMS = [None, 0, -1, (), []]
def _ids(prefix: str):
return [f"{prefix}-dim={d!r}" for d in _DIM_FORMS]
# ---------------------------------------------------------------------------
# Arithmetic + logical reductions on scalar input
# ---------------------------------------------------------------------------
@pytest.mark.smoke
@pytest.mark.parametrize("dim", _DIM_FORMS, ids=_ids("sum"))
def test_sum_scalar_input(dim) -> None:
from tileops.ops.reduction.reduce import SumFwdOp
x = torch.tensor(3.5, dtype=torch.float32, device=DEVICE)
op = SumFwdOp(dtype=torch.float32, dim=dim)
y = op(x)
ref = torch.sum(x, dim=dim) if dim is not None else torch.sum(x)
assert y.shape == ref.shape
torch.testing.assert_close(y, ref)
@pytest.mark.smoke
@pytest.mark.parametrize("dim", _DIM_FORMS, ids=_ids("mean"))
def test_mean_scalar_input(dim) -> None:
from tileops.ops.reduction.reduce import MeanFwdOp
x = torch.tensor(2.0, dtype=torch.float32, device=DEVICE)
op = MeanFwdOp(dtype=torch.float32, dim=dim)
y = op(x)
ref = torch.mean(x, dim=dim) if dim is not None else torch.mean(x)
assert y.shape == ref.shape
torch.testing.assert_close(y, ref)
@pytest.mark.smoke
@pytest.mark.parametrize("dim", _DIM_FORMS, ids=_ids("amax"))
def test_amax_scalar_input(dim) -> None:
from tileops.ops.reduction.reduce import AmaxFwdOp
x = torch.tensor(-1.5, dtype=torch.float32, device=DEVICE)
op = AmaxFwdOp(dtype=torch.float32, dim=dim)
y = op(x)
ref = torch.amax(x, dim=dim) if dim is not None else torch.amax(x)
assert y.shape == ref.shape
torch.testing.assert_close(y, ref)
@pytest.mark.smoke
@pytest.mark.parametrize("dim", _DIM_FORMS, ids=_ids("amin"))
def test_amin_scalar_input(dim) -> None:
from tileops.ops.reduction.reduce import AminFwdOp
x = torch.tensor(4.25, dtype=torch.float32, device=DEVICE)
op = AminFwdOp(dtype=torch.float32, dim=dim)
y = op(x)
ref = torch.amin(x, dim=dim) if dim is not None else torch.amin(x)
assert y.shape == ref.shape
torch.testing.assert_close(y, ref)
@pytest.mark.smoke
@pytest.mark.parametrize("dim", [0, -1], ids=["prod-dim=0", "prod-dim=-1"])
def test_prod_scalar_input(dim) -> None:
from tileops.ops.reduction.reduce import ProdFwdOp
x = torch.tensor(3.0, dtype=torch.float32, device=DEVICE)
op = ProdFwdOp(dtype=torch.float32, dim=dim)
y = op(x)
ref = torch.prod(x, dim=dim)
assert y.shape == ref.shape
torch.testing.assert_close(y, ref)
@pytest.mark.smoke
@pytest.mark.parametrize("dim", _DIM_FORMS, ids=_ids("all"))
def test_all_scalar_input(dim) -> None:
from tileops.ops.reduction.all_op import AllFwdOp
x = torch.tensor(1.0, dtype=torch.float32, device=DEVICE)
op = AllFwdOp(dtype=torch.float32, dim=dim)
y = op(x)
ref = torch.all(x, dim=dim) if dim is not None else torch.all(x)
assert y.shape == ref.shape
assert y.dtype == torch.bool
assert torch.equal(y, ref)
@pytest.mark.smoke
@pytest.mark.parametrize("dim", _DIM_FORMS, ids=_ids("any"))
def test_any_scalar_input(dim) -> None:
from tileops.ops.reduction.any_op import AnyFwdOp
x = torch.tensor(0.0, dtype=torch.float32, device=DEVICE)
op = AnyFwdOp(dtype=torch.float32, dim=dim)
y = op(x)
ref = torch.any(x, dim=dim) if dim is not None else torch.any(x)
assert y.shape == ref.shape
assert y.dtype == torch.bool
assert torch.equal(y, ref)
@pytest.mark.smoke
@pytest.mark.parametrize("dim", _DIM_FORMS, ids=_ids("count_nonzero"))
def test_count_nonzero_scalar_input(dim) -> None:
from tileops.ops.reduction.count_nonzero import CountNonzeroFwdOp
x = torch.tensor(2.5, dtype=torch.float32, device=DEVICE)
op = CountNonzeroFwdOp(dtype=torch.float32, dim=dim)
y = op(x)
ref = torch.count_nonzero(x, dim=dim) if dim is not None else torch.count_nonzero(x)
assert y.shape == ref.shape
assert y.dtype == ref.dtype
assert torch.equal(y, ref)
# ---------------------------------------------------------------------------
# Welford-family reductions on scalar input -> nan + UserWarning
# ---------------------------------------------------------------------------
def _expect_var_warning() -> bool:
"""Return whether PyTorch emits a UserWarning for var on scalar input.
PyTorch raises a "degrees of freedom is <= 0" UserWarning when
``correction >= N``. We probe the reference path so the test stays
aligned with the local PyTorch build's exact warning emission.
"""
x = torch.tensor(1.0, dtype=torch.float32)
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
torch.var(x)
return any(issubclass(w.category, UserWarning) for w in caught)
@pytest.mark.smoke
@pytest.mark.parametrize("dim", _DIM_FORMS, ids=_ids("var"))
def test_var_scalar_input(dim) -> None:
from tileops.ops.reduction.reduce import VarFwdOp
x = torch.tensor(1.5, dtype=torch.float32, device=DEVICE)
op = VarFwdOp(dtype=torch.float32, dim=dim)
expect_warn = _expect_var_warning()
with warnings.catch_warnings(record=True) as op_caught:
warnings.simplefilter("always")
y = op(x)
ref = torch.var(x, dim=dim) if dim is not None else torch.var(x)
assert y.shape == ref.shape == ()
assert torch.isnan(y).item()
assert torch.isnan(ref).item()
if expect_warn:
assert any(issubclass(w.category, UserWarning) for w in op_caught)
@pytest.mark.smoke
@pytest.mark.parametrize("dim", _DIM_FORMS, ids=_ids("std"))
def test_std_scalar_input(dim) -> None:
from tileops.ops.reduction.reduce import StdFwdOp
x = torch.tensor(-0.75, dtype=torch.float32, device=DEVICE)
op = StdFwdOp(dtype=torch.float32, dim=dim)
expect_warn = _expect_var_warning()
with warnings.catch_warnings(record=True) as op_caught:
warnings.simplefilter("always")
y = op(x)
ref = torch.std(x, dim=dim) if dim is not None else torch.std(x)
assert y.shape == ref.shape == ()
assert torch.isnan(y).item()
assert torch.isnan(ref).item()
if expect_warn:
assert any(issubclass(w.category, UserWarning) for w in op_caught)
@pytest.mark.smoke
@pytest.mark.parametrize("dim", _DIM_FORMS, ids=_ids("var_mean"))
def test_var_mean_scalar_input(dim) -> None:
from tileops.ops.reduction.reduce import VarMeanFwdOp
x = torch.tensor(2.25, dtype=torch.float32, device=DEVICE)
op = VarMeanFwdOp(dtype=torch.float32, dim=dim)
expect_warn = _expect_var_warning()
with warnings.catch_warnings(record=True) as op_caught:
warnings.simplefilter("always")
var_out, mean_out = op(x)
var_ref, mean_ref = (
torch.var_mean(x, dim=dim) if dim is not None else torch.var_mean(x)
)
assert var_out.shape == var_ref.shape == ()
assert mean_out.shape == mean_ref.shape == ()
assert torch.isnan(var_out).item()
assert torch.isnan(var_ref).item()
torch.testing.assert_close(mean_out, mean_ref)
if expect_warn:
assert any(issubclass(w.category, UserWarning) for w in op_caught)
# ---------------------------------------------------------------------------
# Duplicate-dim regression — aliasing sequences on a 0-D tensor must raise
# the same RuntimeError PyTorch raises ("dim 0 appears multiple times in
# the list of dims") instead of silently returning a scalar.
# ---------------------------------------------------------------------------
@pytest.mark.smoke
@pytest.mark.parametrize(
"dim",
[[0, 0], [0, -1], [-1, -1], [-1, 0], (0, -1)],
ids=["dim=[0,0]", "dim=[0,-1]", "dim=[-1,-1]", "dim=[-1,0]", "dim=(0,-1)"],
)
def test_sum_scalar_duplicate_dim_matches_torch(dim) -> None:
from tileops.ops.reduction.reduce import SumFwdOp
x = torch.tensor(1.5, dtype=torch.float32, device=DEVICE)
with pytest.raises(RuntimeError, match="appears multiple times"):
torch.sum(x, dim=list(dim))
op = SumFwdOp(dtype=torch.float32, dim=list(dim))
with pytest.raises(RuntimeError, match="appears multiple times"):
op(x)
# ---------------------------------------------------------------------------
# Welford requires_grad regression — the invalid-DOF (NaN) fast path must
# return a tensor with autograd history matching PyTorch so backward works.
# ---------------------------------------------------------------------------
@pytest.mark.smoke
def test_var_scalar_requires_grad_preserves_grad_fn() -> None:
from tileops.ops.reduction.reduce import VarFwdOp
x = torch.tensor(0.5, dtype=torch.float32, device=DEVICE, requires_grad=True)
op = VarFwdOp(dtype=torch.float32, dim=None)
with warnings.catch_warnings():
warnings.simplefilter("ignore", UserWarning)
y = op(x)
ref = torch.var(x)
assert y.shape == ref.shape == ()
assert torch.isnan(y).item()
assert y.requires_grad and ref.requires_grad
assert y.grad_fn is not None and ref.grad_fn is not None
@pytest.mark.smoke
def test_var_mean_scalar_requires_grad_preserves_grad_fn() -> None:
from tileops.ops.reduction.reduce import VarMeanFwdOp
x = torch.tensor(1.25, dtype=torch.float32, device=DEVICE, requires_grad=True)
op = VarMeanFwdOp(dtype=torch.float32, dim=None)
with warnings.catch_warnings():
warnings.simplefilter("ignore", UserWarning)
var_out, mean_out = op(x)
var_ref, mean_ref = torch.var_mean(x)
assert var_out.requires_grad and var_ref.requires_grad
assert mean_out.requires_grad and mean_ref.requires_grad
assert var_out.grad_fn is not None and mean_out.grad_fn is not None