forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cumulative.py
More file actions
311 lines (239 loc) · 10.4 KB
/
Copy pathtest_cumulative.py
File metadata and controls
311 lines (239 loc) · 10.4 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
from tileops.utils import get_backend_name
DEVICE = get_backend_name()
"""Correctness tests for cumulative ops (cumsum, cumprod).
Covers: CumsumFwdOp, CumprodFwdOp.
Each op computes an inclusive prefix scan along dim=-1 and supports 1D-4D input.
Output has the same shape as input.
"""
import pytest
import torch
from tests.test_base import FixtureBase, TestBase
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
class CumulativeBasicFixture(FixtureBase):
PARAMS = [
(
"m, n, dtype",
[
pytest.param(128, 512, torch.float32, marks=pytest.mark.smoke),
pytest.param(128, 512, torch.float16, marks=pytest.mark.smoke),
pytest.param(128, 512, torch.bfloat16, marks=pytest.mark.smoke),
pytest.param(256, 4096, torch.float16, marks=pytest.mark.full),
pytest.param(256, 4096, torch.bfloat16, marks=pytest.mark.full),
# Non-aligned N (non-pow2)
pytest.param(128, 300, torch.float16, marks=pytest.mark.full),
pytest.param(128, 300, torch.bfloat16, marks=pytest.mark.full),
# Tail-M: M not divisible by block_m
pytest.param(129, 512, torch.float16, marks=pytest.mark.full),
],
),
]
class CumulativeNonContigFixture(FixtureBase):
PARAMS = [
(
"m, n, dtype",
[
pytest.param(128, 512, torch.float16, marks=pytest.mark.smoke),
pytest.param(128, 512, torch.bfloat16, marks=pytest.mark.smoke),
],
),
]
class Cumulative3DFixture(FixtureBase):
PARAMS = [
(
"batch, seq, hidden, dtype",
[
pytest.param(2, 64, 512, torch.float16, marks=pytest.mark.smoke),
pytest.param(2, 64, 512, torch.bfloat16, marks=pytest.mark.smoke),
],
),
]
class Cumulative4DFixture(FixtureBase):
PARAMS = [
(
"b0, b1, b2, n, dtype",
[
pytest.param(2, 4, 8, 512, torch.float16, marks=pytest.mark.smoke),
pytest.param(2, 4, 8, 512, torch.bfloat16, marks=pytest.mark.smoke),
],
),
]
class Cumulative1DFixture(FixtureBase):
PARAMS = [
(
"n, dtype",
[
pytest.param(512, torch.float32, marks=pytest.mark.smoke),
pytest.param(512, torch.float16, marks=pytest.mark.smoke),
pytest.param(512, torch.bfloat16, marks=pytest.mark.smoke),
],
),
]
# ---------------------------------------------------------------------------
# TestBase helpers
# ---------------------------------------------------------------------------
class CumulativeTest(TestBase):
"""Parameterized test helper for cumulative ops."""
def __init__(
self, m: int, n: int, dtype: torch.dtype, op_kind: str, use_small_range: bool = False
):
self.m = m
self.n = n
self.dtype = dtype
self.op_kind = op_kind
self.use_small_range = use_small_range
def gen_inputs(self) -> tuple[torch.Tensor]:
if self.use_small_range:
# For cumprod, use small values to avoid overflow
x = torch.rand(self.m, self.n, dtype=self.dtype, device=DEVICE) * 0.01 + 0.99
else:
x = torch.randn(self.m, self.n, dtype=self.dtype, device=DEVICE)
return (x,)
def ref_program(self, x: torch.Tensor) -> torch.Tensor:
x_f32 = x.float()
if self.op_kind == "cumsum":
return x_f32.cumsum(dim=-1).to(x.dtype)
elif self.op_kind == "cumprod":
return x_f32.cumprod(dim=-1).to(x.dtype)
raise ValueError(f"Unknown op_kind: {self.op_kind}")
# ---------------------------------------------------------------------------
# Helper to get tolerances
# ---------------------------------------------------------------------------
def _tol(dtype: torch.dtype) -> dict:
if dtype == torch.float32:
return {"atol": 1e-4, "rtol": 1e-4}
return {"atol": 1e-2, "rtol": 1e-2}
def _cumprod_tol(dtype: torch.dtype) -> dict:
"""Tolerances for cumprod tests (more numerically sensitive)."""
if dtype == torch.float32:
return {"atol": 1e-3, "rtol": 1e-3}
return {"atol": 5e-2, "rtol": 5e-2}
# ---------------------------------------------------------------------------
# CumsumFwdOp tests
# ---------------------------------------------------------------------------
@CumulativeBasicFixture
def test_cumsum_op(m: int, n: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.cumsum import CumsumFwdOp
test = CumulativeTest(m, n, dtype, "cumsum")
op = CumsumFwdOp(N=n, dtype=dtype)
test.check(op, *test.gen_inputs(), **_tol(dtype))
@CumulativeNonContigFixture
def test_cumsum_non_contiguous(m: int, n: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.cumsum import CumsumFwdOp
x_full = torch.randn(m, n * 2, dtype=dtype, device=DEVICE)
x = x_full[:, :n]
op = CumsumFwdOp(N=n, dtype=dtype)
ref = x.contiguous().float().cumsum(dim=-1).to(dtype)
y = op(x)
tol = _tol(dtype)
assert torch.allclose(y, ref, **tol), f"max err: {(y - ref).abs().max()}"
@Cumulative3DFixture
def test_cumsum_3d(batch: int, seq: int, hidden: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.cumsum import CumsumFwdOp
x = torch.randn(batch, seq, hidden, dtype=dtype, device=DEVICE)
op = CumsumFwdOp(N=hidden, dtype=dtype)
ref = x.float().cumsum(dim=-1).to(dtype)
y = op(x)
tol = _tol(dtype)
assert torch.allclose(y, ref, **tol), f"3D max err: {(y - ref).abs().max()}"
@Cumulative4DFixture
def test_cumsum_4d(b0: int, b1: int, b2: int, n: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.cumsum import CumsumFwdOp
x = torch.randn(b0, b1, b2, n, dtype=dtype, device=DEVICE)
op = CumsumFwdOp(N=n, dtype=dtype)
ref = x.float().cumsum(dim=-1).to(dtype)
y = op(x)
tol = _tol(dtype)
assert torch.allclose(y, ref, **tol), f"4D max err: {(y - ref).abs().max()}"
@Cumulative1DFixture
def test_cumsum_1d(n: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.cumsum import CumsumFwdOp
x = torch.randn(n, dtype=dtype, device=DEVICE)
op = CumsumFwdOp(N=n, dtype=dtype)
ref = x.float().cumsum(dim=-1).to(dtype)
y = op(x)
tol = _tol(dtype)
assert torch.allclose(y, ref, **tol), f"1D cumsum max err: {(y - ref).abs().max()}"
# ---------------------------------------------------------------------------
# CumprodFwdOp tests
# ---------------------------------------------------------------------------
@CumulativeBasicFixture
def test_cumprod_op(m: int, n: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.cumprod import CumprodFwdOp
test = CumulativeTest(m, n, dtype, "cumprod", use_small_range=True)
op = CumprodFwdOp(N=n, dtype=dtype)
test.check(op, *test.gen_inputs(), **_cumprod_tol(dtype))
@CumulativeNonContigFixture
def test_cumprod_non_contiguous(m: int, n: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.cumprod import CumprodFwdOp
x_full = torch.rand(m, n * 2, dtype=dtype, device=DEVICE) * 0.01 + 0.99
x = x_full[:, :n]
op = CumprodFwdOp(N=n, dtype=dtype)
ref = x.contiguous().float().cumprod(dim=-1).to(dtype)
y = op(x)
tol = _cumprod_tol(dtype)
assert torch.allclose(y, ref, **tol), f"max err: {(y - ref).abs().max()}"
@Cumulative3DFixture
def test_cumprod_3d(batch: int, seq: int, hidden: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.cumprod import CumprodFwdOp
x = torch.rand(batch, seq, hidden, dtype=dtype, device=DEVICE) * 0.01 + 0.99
op = CumprodFwdOp(N=hidden, dtype=dtype)
ref = x.float().cumprod(dim=-1).to(dtype)
y = op(x)
tol = _cumprod_tol(dtype)
assert torch.allclose(y, ref, **tol), f"3D cumprod max err: {(y - ref).abs().max()}"
@Cumulative4DFixture
def test_cumprod_4d(b0: int, b1: int, b2: int, n: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.cumprod import CumprodFwdOp
x = torch.rand(b0, b1, b2, n, dtype=dtype, device=DEVICE) * 0.01 + 0.99
op = CumprodFwdOp(N=n, dtype=dtype)
ref = x.float().cumprod(dim=-1).to(dtype)
y = op(x)
tol = _cumprod_tol(dtype)
assert torch.allclose(y, ref, **tol), f"4D cumprod max err: {(y - ref).abs().max()}"
@Cumulative1DFixture
def test_cumprod_1d(n: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.cumprod import CumprodFwdOp
x = torch.rand(n, dtype=dtype, device=DEVICE) * 0.01 + 0.99
op = CumprodFwdOp(N=n, dtype=dtype)
ref = x.float().cumprod(dim=-1).to(dtype)
y = op(x)
tol = _cumprod_tol(dtype)
assert torch.allclose(y, ref, **tol), f"1D cumprod max err: {(y - ref).abs().max()}"
class CumulativeDimAxis1Fixture(FixtureBase):
PARAMS = [
("batch, hidden, seq, dtype", [
pytest.param(2, 512, 256, torch.float16, marks=pytest.mark.smoke),
pytest.param(2, 512, 256, torch.bfloat16, marks=pytest.mark.smoke),
]),
]
@CumulativeDimAxis1Fixture
def test_cumsum_dim_axis1(
batch: int, hidden: int, seq: int, dtype: torch.dtype
) -> None:
"""Cumsum along dim=1 (3D) — exercises movedim choreography in `_run`."""
from tileops.ops.reduction.cumsum import CumsumFwdOp
x = torch.randn(batch, hidden, seq, dtype=dtype, device=DEVICE)
op = CumsumFwdOp(N=hidden, dtype=dtype, dim=1)
ref = x.float().cumsum(dim=1).to(dtype)
y = op(x)
atol = 1e-2 if dtype == torch.float16 else 1.6e-2
assert torch.allclose(y, ref, atol=atol, rtol=atol), \
f"cumsum dim=1 max err: {(y - ref).abs().max()}"
@CumulativeDimAxis1Fixture
def test_cumprod_dim_axis1(
batch: int, hidden: int, seq: int, dtype: torch.dtype
) -> None:
"""Cumprod along dim=1 (3D) — exercises movedim choreography in `_run`."""
from tileops.ops.reduction.cumprod import CumprodFwdOp
# Values close to 1 to avoid over/underflow in cumprod over hidden dim.
x = torch.rand(batch, hidden, seq, dtype=dtype, device=DEVICE) * 0.01 + 0.99
op = CumprodFwdOp(N=hidden, dtype=dtype, dim=1)
ref = x.float().cumprod(dim=1).to(dtype)
y = op(x)
tol = _cumprod_tol(dtype)
assert torch.allclose(y, ref, **tol), \
f"cumprod dim=1 max err: {(y - ref).abs().max()}"
if __name__ == "__main__":
pytest.main([__file__, "-vvs"])