forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_argreduce.py
More file actions
551 lines (428 loc) · 20.7 KB
/
Copy pathtest_argreduce.py
File metadata and controls
551 lines (428 loc) · 20.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
from tileops.utils import get_backend_name
DEVICE = get_backend_name()
"""Correctness tests for argreduce ops (argmax, argmin).
Covers: ArgmaxFwdOp, ArgminFwdOp.
Each op reduces along a configurable dim and returns int64 indices.
Uses exact match (torch.equal) instead of allclose.
"""
from typing import cast
import pytest
import torch
from tests.test_base import FixtureBase, TestBase
from workloads.argreduce import ArgmaxTest as _ArgmaxWorkload
def _call(op, x: torch.Tensor) -> torch.Tensor:
"""Invoke a single-output argreduce op and narrow the return to ``Tensor``.
The shared ``OpBase.__call__`` is typed as ``Union[Tensor, tuple]`` to
accommodate ops with multiple outputs. Argreduce ops always return a
single ``Tensor``; this helper keeps the call sites well-typed without
sprinkling ``cast(...)`` everywhere.
"""
return cast(torch.Tensor, op(x))
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
class ArgreduceBasicFixture(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 last dim)
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 ArgreduceNonContigFixture(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 Argreduce3DFixture(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 Argreduce3DDim0Fixture(FixtureBase):
"""dim=0 reduction on 3D tensors — small outermost dim triggers
the TileLang layout constraint (N << N_padded)."""
PARAMS = [
(
"batch, seq, hidden, dtype",
[
pytest.param(4, 8, 256, torch.float16, marks=pytest.mark.smoke),
pytest.param(4, 8, 256, torch.bfloat16, marks=pytest.mark.smoke),
pytest.param(4, 8, 256, torch.float32, marks=pytest.mark.smoke),
],
),
]
class Argreduce4DFixture(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 Argreduce4DDim0Fixture(FixtureBase):
"""dim=0 reduction on 4D tensors — regression coverage for 3D+ contract."""
PARAMS = [
(
"b0, b1, b2, n, dtype",
[
pytest.param(2, 4, 8, 256, torch.float16, marks=pytest.mark.smoke),
pytest.param(2, 4, 8, 256, torch.bfloat16, marks=pytest.mark.smoke),
],
),
]
class Argreduce1DFixture(FixtureBase):
PARAMS = [
(
"n, dtype",
[
pytest.param(512, torch.float16, marks=pytest.mark.smoke),
pytest.param(512, torch.float32, marks=pytest.mark.smoke),
pytest.param(512, torch.bfloat16, marks=pytest.mark.smoke),
],
),
]
class SpecArgreduceFixture(FixtureBase):
PARAMS = [
(
"shape, dim, keepdim, dtype",
[
pytest.param((128, 512), -1, False, torch.float16, marks=pytest.mark.smoke),
pytest.param((4, 32, 512), -1, False, torch.bfloat16, marks=pytest.mark.smoke),
pytest.param((128, 512), -1, True, torch.float16, marks=pytest.mark.full),
pytest.param((512, 4, 32), 0, False, torch.float16, marks=pytest.mark.full),
pytest.param((4, 32, 512), 1, False, torch.float16, marks=pytest.mark.full),
pytest.param((4, 32, 512), -1, True, torch.bfloat16, marks=pytest.mark.full),
],
),
]
# ---------------------------------------------------------------------------
# TestBase helpers — inherit gen_inputs() from workload classes
# ---------------------------------------------------------------------------
class ArgreduceTest(_ArgmaxWorkload, TestBase):
"""Parameterized test helper for argreduce ops."""
def __init__(self, m: int, n: int, dtype: torch.dtype, op_kind: str):
super().__init__((m, n), dtype)
self.op_kind = op_kind
def ref_program(self, *inputs: torch.Tensor) -> torch.Tensor:
(x,) = inputs
if self.op_kind == "argmax":
return x.argmax(dim=-1)
elif self.op_kind == "argmin":
return x.argmin(dim=-1)
raise ValueError(f"Unknown op_kind: {self.op_kind}")
def _exact_compare(output: torch.Tensor, output_ref: torch.Tensor) -> None:
"""Exact match comparison using torch.equal."""
assert output.dtype == torch.int64, f"Expected int64, got {output.dtype}"
assert output_ref.dtype == torch.int64, f"Expected ref int64, got {output_ref.dtype}"
assert torch.equal(output, output_ref), (
f"Indices mismatch.\n"
f" output: {output[:10]}...\n"
f" output_ref: {output_ref[:10]}...\n"
f" mismatches: {(output != output_ref).sum().item()} / {output.numel()}"
)
# ---------------------------------------------------------------------------
# ArgmaxFwdOp tests
# ---------------------------------------------------------------------------
@ArgreduceBasicFixture
def test_argmax_op(m: int, n: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.argmax import ArgmaxFwdOp
test = ArgreduceTest(m, n, dtype, "argmax")
op = ArgmaxFwdOp(dtype=dtype, dim=-1)
test.check(op, *test.gen_inputs(), compare=_exact_compare)
@ArgreduceNonContigFixture
def test_argmax_non_contiguous(m: int, n: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.argmax import ArgmaxFwdOp
x_full = torch.randn(m, n * 2, dtype=dtype, device=DEVICE)
x = x_full[:, :n]
op = ArgmaxFwdOp(dtype=dtype, dim=-1)
ref = x.contiguous().argmax(dim=-1)
y = _call(op, x)
assert y.dtype == torch.int64
assert torch.equal(y, ref), f"non-contig argmax mismatch: {(y != ref).sum().item()}"
@Argreduce3DFixture
def test_argmax_3d(batch: int, seq: int, hidden: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.argmax import ArgmaxFwdOp
x = torch.randn(batch, seq, hidden, dtype=dtype, device=DEVICE)
op = ArgmaxFwdOp(dtype=dtype, dim=-1)
ref = x.argmax(dim=-1)
y = _call(op, x)
assert y.dtype == torch.int64
assert torch.equal(y, ref), f"3D argmax mismatch: {(y != ref).sum().item()}"
@Argreduce4DFixture
def test_argmax_4d(b0: int, b1: int, b2: int, n: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.argmax import ArgmaxFwdOp
x = torch.randn(b0, b1, b2, n, dtype=dtype, device=DEVICE)
op = ArgmaxFwdOp(dtype=dtype, dim=-1)
ref = x.argmax(dim=-1)
y = _call(op, x)
assert y.dtype == torch.int64
assert torch.equal(y, ref), f"4D argmax mismatch: {(y != ref).sum().item()}"
@Argreduce1DFixture
def test_argmax_1d(n: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.argmax import ArgmaxFwdOp
x = torch.randn(n, dtype=dtype, device=DEVICE)
op = ArgmaxFwdOp(dtype=dtype, dim=-1)
ref = x.argmax(dim=-1)
y = _call(op, x)
assert y.dtype == torch.int64
assert torch.equal(y.view_as(ref), ref), "1D argmax mismatch"
@Argreduce3DDim0Fixture
def test_argmax_3d_dim0(batch: int, seq: int, hidden: int, dtype: torch.dtype) -> None:
"""Argmax along dim=0 on 3D tensors (outermost-dim reduction)."""
from tileops.ops.reduction.argmax import ArgmaxFwdOp
x = torch.randn(batch, seq, hidden, dtype=dtype, device=DEVICE)
op = ArgmaxFwdOp(dtype=dtype, dim=0)
ref = x.argmax(dim=0)
y = _call(op, x)
assert y.shape == ref.shape, f"shape mismatch: {y.shape} vs {ref.shape}"
assert y.dtype == torch.int64
assert torch.equal(y, ref), f"3D dim=0 argmax mismatch: {(y != ref).sum().item()}"
@Argreduce3DDim0Fixture
def test_argmax_3d_dim0_keepdim(batch: int, seq: int, hidden: int, dtype: torch.dtype) -> None:
"""Argmax along dim=0 with keepdim=True on 3D tensors."""
from tileops.ops.reduction.argmax import ArgmaxFwdOp
x = torch.randn(batch, seq, hidden, dtype=dtype, device=DEVICE)
op = ArgmaxFwdOp(dtype=dtype, dim=0, keepdim=True)
ref = x.argmax(dim=0, keepdim=True)
y = _call(op, x)
assert y.shape == ref.shape, f"shape mismatch: {y.shape} vs {ref.shape}"
assert y.dtype == torch.int64
assert torch.equal(y, ref), f"3D dim=0 keepdim argmax mismatch: {(y != ref).sum().item()}"
@Argreduce4DDim0Fixture
def test_argmax_4d_dim0(b0: int, b1: int, b2: int, n: int, dtype: torch.dtype) -> None:
"""Argmax along dim=0 on 4D tensors (outermost-dim reduction, 3D+ regression)."""
from tileops.ops.reduction.argmax import ArgmaxFwdOp
x = torch.randn(b0, b1, b2, n, dtype=dtype, device=DEVICE)
op = ArgmaxFwdOp(dtype=dtype, dim=0)
ref = x.argmax(dim=0)
y = _call(op, x)
assert y.shape == ref.shape, f"shape mismatch: {y.shape} vs {ref.shape}"
assert y.dtype == torch.int64
assert torch.equal(y, ref), f"4D dim=0 argmax mismatch: {(y != ref).sum().item()}"
@Argreduce4DDim0Fixture
def test_argmax_4d_dim0_keepdim(b0: int, b1: int, b2: int, n: int, dtype: torch.dtype) -> None:
"""Argmax along dim=0 with keepdim=True on 4D tensors."""
from tileops.ops.reduction.argmax import ArgmaxFwdOp
x = torch.randn(b0, b1, b2, n, dtype=dtype, device=DEVICE)
op = ArgmaxFwdOp(dtype=dtype, dim=0, keepdim=True)
ref = x.argmax(dim=0, keepdim=True)
y = _call(op, x)
assert y.shape == ref.shape, f"shape mismatch: {y.shape} vs {ref.shape}"
assert y.dtype == torch.int64
assert torch.equal(y, ref), f"4D dim=0 keepdim argmax mismatch: {(y != ref).sum().item()}"
@SpecArgreduceFixture
def test_argmax_spec_dim(shape: tuple, dim: int, keepdim: bool, dtype: torch.dtype) -> None:
"""Spec interface: ArgmaxFwdOp with dim + keepdim."""
from tileops.ops.reduction.argmax import ArgmaxFwdOp
x = torch.randn(*shape, dtype=dtype, device=DEVICE)
op = ArgmaxFwdOp(dtype=dtype, dim=dim, keepdim=keepdim)
ref = x.argmax(dim=dim, keepdim=keepdim)
y = _call(op, x)
assert y.shape == ref.shape, f"shape mismatch: {y.shape} vs {ref.shape}"
assert y.dtype == torch.int64
assert torch.equal(y, ref), f"spec dim={dim} argmax mismatch: {(y != ref).sum().item()}"
# ---------------------------------------------------------------------------
# ArgminFwdOp tests
# ---------------------------------------------------------------------------
@ArgreduceBasicFixture
def test_argmin_op(m: int, n: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.argmin import ArgminFwdOp
test = ArgreduceTest(m, n, dtype, "argmin")
op = ArgminFwdOp(dtype=dtype, dim=-1)
test.check(op, *test.gen_inputs(), compare=_exact_compare)
@ArgreduceNonContigFixture
def test_argmin_non_contiguous(m: int, n: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.argmin import ArgminFwdOp
x_full = torch.randn(m, n * 2, dtype=dtype, device=DEVICE)
x = x_full[:, :n]
op = ArgminFwdOp(dtype=dtype, dim=-1)
ref = x.contiguous().argmin(dim=-1)
y = _call(op, x)
assert y.dtype == torch.int64
assert torch.equal(y, ref), f"non-contig argmin mismatch: {(y != ref).sum().item()}"
@Argreduce3DFixture
def test_argmin_3d(batch: int, seq: int, hidden: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.argmin import ArgminFwdOp
x = torch.randn(batch, seq, hidden, dtype=dtype, device=DEVICE)
op = ArgminFwdOp(dtype=dtype, dim=-1)
ref = x.argmin(dim=-1)
y = _call(op, x)
assert y.dtype == torch.int64
assert torch.equal(y, ref), f"3D argmin mismatch: {(y != ref).sum().item()}"
@Argreduce4DFixture
def test_argmin_4d(b0: int, b1: int, b2: int, n: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.argmin import ArgminFwdOp
x = torch.randn(b0, b1, b2, n, dtype=dtype, device=DEVICE)
op = ArgminFwdOp(dtype=dtype, dim=-1)
ref = x.argmin(dim=-1)
y = _call(op, x)
assert y.dtype == torch.int64
assert torch.equal(y, ref), f"4D argmin mismatch: {(y != ref).sum().item()}"
@Argreduce1DFixture
def test_argmin_1d(n: int, dtype: torch.dtype) -> None:
from tileops.ops.reduction.argmin import ArgminFwdOp
x = torch.randn(n, dtype=dtype, device=DEVICE)
op = ArgminFwdOp(dtype=dtype, dim=-1)
ref = x.argmin(dim=-1)
y = _call(op, x)
assert y.dtype == torch.int64
assert torch.equal(y.view_as(ref), ref), "1D argmin mismatch"
@Argreduce3DDim0Fixture
def test_argmin_3d_dim0(batch: int, seq: int, hidden: int, dtype: torch.dtype) -> None:
"""Argmin along dim=0 on 3D tensors (outermost-dim reduction)."""
from tileops.ops.reduction.argmin import ArgminFwdOp
x = torch.randn(batch, seq, hidden, dtype=dtype, device=DEVICE)
op = ArgminFwdOp(dtype=dtype, dim=0)
ref = x.argmin(dim=0)
y = _call(op, x)
assert y.shape == ref.shape, f"shape mismatch: {y.shape} vs {ref.shape}"
assert y.dtype == torch.int64
assert torch.equal(y, ref), f"3D dim=0 argmin mismatch: {(y != ref).sum().item()}"
@Argreduce3DDim0Fixture
def test_argmin_3d_dim0_keepdim(batch: int, seq: int, hidden: int, dtype: torch.dtype) -> None:
"""Argmin along dim=0 with keepdim=True on 3D tensors."""
from tileops.ops.reduction.argmin import ArgminFwdOp
x = torch.randn(batch, seq, hidden, dtype=dtype, device=DEVICE)
op = ArgminFwdOp(dtype=dtype, dim=0, keepdim=True)
ref = x.argmin(dim=0, keepdim=True)
y = _call(op, x)
assert y.shape == ref.shape, f"shape mismatch: {y.shape} vs {ref.shape}"
assert y.dtype == torch.int64
assert torch.equal(y, ref), f"3D dim=0 keepdim argmin mismatch: {(y != ref).sum().item()}"
@Argreduce4DDim0Fixture
def test_argmin_4d_dim0(b0: int, b1: int, b2: int, n: int, dtype: torch.dtype) -> None:
"""Argmin along dim=0 on 4D tensors (outermost-dim reduction, 3D+ regression)."""
from tileops.ops.reduction.argmin import ArgminFwdOp
x = torch.randn(b0, b1, b2, n, dtype=dtype, device=DEVICE)
op = ArgminFwdOp(dtype=dtype, dim=0)
ref = x.argmin(dim=0)
y = _call(op, x)
assert y.shape == ref.shape, f"shape mismatch: {y.shape} vs {ref.shape}"
assert y.dtype == torch.int64
assert torch.equal(y, ref), f"4D dim=0 argmin mismatch: {(y != ref).sum().item()}"
@Argreduce4DDim0Fixture
def test_argmin_4d_dim0_keepdim(b0: int, b1: int, b2: int, n: int, dtype: torch.dtype) -> None:
"""Argmin along dim=0 with keepdim=True on 4D tensors."""
from tileops.ops.reduction.argmin import ArgminFwdOp
x = torch.randn(b0, b1, b2, n, dtype=dtype, device=DEVICE)
op = ArgminFwdOp(dtype=dtype, dim=0, keepdim=True)
ref = x.argmin(dim=0, keepdim=True)
y = _call(op, x)
assert y.shape == ref.shape, f"shape mismatch: {y.shape} vs {ref.shape}"
assert y.dtype == torch.int64
assert torch.equal(y, ref), f"4D dim=0 keepdim argmin mismatch: {(y != ref).sum().item()}"
@SpecArgreduceFixture
def test_argmin_spec_dim(shape: tuple, dim: int, keepdim: bool, dtype: torch.dtype) -> None:
"""Spec interface: ArgminFwdOp with dim + keepdim."""
from tileops.ops.reduction.argmin import ArgminFwdOp
x = torch.randn(*shape, dtype=dtype, device=DEVICE)
op = ArgminFwdOp(dtype=dtype, dim=dim, keepdim=keepdim)
ref = x.argmin(dim=dim, keepdim=keepdim)
y = _call(op, x)
assert y.shape == ref.shape, f"shape mismatch: {y.shape} vs {ref.shape}"
assert y.dtype == torch.int64
assert torch.equal(y, ref), f"spec dim={dim} argmin mismatch: {(y != ref).sum().item()}"
# ---------------------------------------------------------------------------
# Regression: multidim dim must be rejected for argreduce ops
# ---------------------------------------------------------------------------
@pytest.mark.smoke
@pytest.mark.parametrize("op_cls_path, dim", [
("tileops.ops.reduction.argmax.ArgmaxFwdOp", [0, 1]),
("tileops.ops.reduction.argmin.ArgminFwdOp", [0, 1]),
("tileops.ops.reduction.argmax.ArgmaxFwdOp", (0, 1)),
("tileops.ops.reduction.argmin.ArgminFwdOp", (0, 1)),
])
def test_argreduce_rejects_multidim(op_cls_path: str, dim) -> None:
"""Argreduce ops only support scalar dim or None; list/tuple must raise."""
import importlib
module_path, cls_name = op_cls_path.rsplit(".", 1)
mod = importlib.import_module(module_path)
op_cls = getattr(mod, cls_name)
with pytest.raises((TypeError, ValueError)):
op_cls(dtype=torch.float16, dim=dim)
# ---------------------------------------------------------------------------
# dim=None (full-tensor reduction) tests
# ---------------------------------------------------------------------------
class ArgreduceDimNoneFixture(FixtureBase):
"""Full-tensor reduction (dim=None).
Coverage rationale (testing.md §Test case policy):
- dtype dispatch: one 2D shape across {fp16, bf16, fp32}.
- ndim shape branches (flatten path): 1D / 3D / 4D in fp16 only;
ndim and dtype are not crossed since the flatten code path is
dtype-independent.
- One non-aligned flat size as full coverage (tail handling).
"""
PARAMS = [
(
"shape, dtype",
[
# dtype dispatch on a single 2D shape
pytest.param((16, 64), torch.float16, marks=pytest.mark.smoke),
pytest.param((16, 64), torch.bfloat16, marks=pytest.mark.smoke),
pytest.param((16, 64), torch.float32, marks=pytest.mark.smoke),
# ndim shape coverage (flatten path is dtype-agnostic)
pytest.param((512,), torch.float16, marks=pytest.mark.smoke),
pytest.param((4, 8, 32), torch.float16, marks=pytest.mark.smoke),
pytest.param((2, 4, 8, 16), torch.float16, marks=pytest.mark.smoke),
# Non-aligned flat size (tail handling)
pytest.param((10, 30), torch.float16, marks=pytest.mark.full),
],
),
]
@ArgreduceDimNoneFixture
def test_argmax_dim_none(shape: tuple, dtype: torch.dtype) -> None:
"""ArgmaxFwdOp(dim=None) matches torch.argmax(x); covers keepdim={False, True}."""
from tileops.ops.reduction.argmax import ArgmaxFwdOp
x = torch.randn(*shape, dtype=dtype, device=DEVICE)
ref_flat = torch.argmax(x)
y = _call(ArgmaxFwdOp(dtype=dtype, dim=None), x)
assert y.dtype == torch.int64
assert y.shape == ref_flat.shape, f"shape mismatch: {y.shape} vs {ref_flat.shape}"
assert torch.equal(y, ref_flat), f"dim=None argmax mismatch on shape={shape} dtype={dtype}"
y_keep = _call(ArgmaxFwdOp(dtype=dtype, dim=None, keepdim=True), x)
expected_shape = tuple(1 for _ in shape)
assert y_keep.shape == expected_shape, f"keepdim shape mismatch: {y_keep.shape} vs {expected_shape}"
assert torch.equal(y_keep.reshape(()), ref_flat), (
f"dim=None keepdim argmax value mismatch on shape={shape} dtype={dtype}"
)
@ArgreduceDimNoneFixture
def test_argmin_dim_none(shape: tuple, dtype: torch.dtype) -> None:
"""ArgminFwdOp(dim=None) matches torch.argmin(x); covers keepdim={False, True}."""
from tileops.ops.reduction.argmin import ArgminFwdOp
x = torch.randn(*shape, dtype=dtype, device=DEVICE)
ref_flat = torch.argmin(x)
y = _call(ArgminFwdOp(dtype=dtype, dim=None), x)
assert y.dtype == torch.int64
assert y.shape == ref_flat.shape, f"shape mismatch: {y.shape} vs {ref_flat.shape}"
assert torch.equal(y, ref_flat), f"dim=None argmin mismatch on shape={shape} dtype={dtype}"
y_keep = _call(ArgminFwdOp(dtype=dtype, dim=None, keepdim=True), x)
expected_shape = tuple(1 for _ in shape)
assert y_keep.shape == expected_shape, f"keepdim shape mismatch: {y_keep.shape} vs {expected_shape}"
assert torch.equal(y_keep.reshape(()), ref_flat), (
f"dim=None keepdim argmin value mismatch on shape={shape} dtype={dtype}"
)
if __name__ == "__main__":
pytest.main([__file__, "-vvs"])