forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_logical_reduce.py
More file actions
107 lines (79 loc) · 3.6 KB
/
Copy pathbench_logical_reduce.py
File metadata and controls
107 lines (79 loc) · 3.6 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
"""Benchmarks for logical reduce ops (any, all, count_nonzero).
Measures latency, TFLOPS, and DRAM bandwidth against PyTorch baselines.
Workload shapes and roofline formulas are loaded from the ops manifest (tileops/manifest/).
"""
import pytest
import torch
from benchmarks.benchmark_base import BenchmarkReport, ManifestBenchmark, workloads_to_params
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 workloads.logical_reduce import AllTest, AnyTest, CountNonzeroTest
# ===================================================================
# Op name constants
# ===================================================================
_ANY_OP = "AnyFwdOp"
_ALL_OP = "AllFwdOp"
_COUNT_NONZERO_OP = "CountNonzeroFwdOp"
# ===================================================================
# Any benchmarks
# ===================================================================
@pytest.mark.parametrize("shape, dtype", workloads_to_params(_ANY_OP))
def test_any_bench(shape: tuple, dtype: torch.dtype) -> None:
test = AnyTest(shape, dtype)
inputs = test.gen_inputs()
op = AnyFwdOp(dtype=dtype, dim=-1)
bm = ManifestBenchmark(_ANY_OP, op, test)
try:
result = bm.profile(op, *inputs)
except ValueError as exc:
if "No configurations to tune" in str(exc):
pytest.skip(f"Kernel does not support this shape: {exc}")
raise
BenchmarkReport.record(op, locals(), result, tag="tileops")
def baseline_fn(x):
return x.bool().any(dim=-1)
result_bl = bm.profile(baseline_fn, *inputs)
BenchmarkReport.record(op, locals(), result_bl, tag="torch")
# ===================================================================
# All benchmarks
# ===================================================================
@pytest.mark.parametrize("shape, dtype", workloads_to_params(_ALL_OP))
def test_all_bench(shape: tuple, dtype: torch.dtype) -> None:
test = AllTest(shape, dtype)
inputs = test.gen_inputs()
op = AllFwdOp(dtype=dtype, dim=-1)
bm = ManifestBenchmark(_ALL_OP, op, test)
try:
result = bm.profile(op, *inputs)
except ValueError as exc:
if "No configurations to tune" in str(exc):
pytest.skip(f"Kernel does not support this shape: {exc}")
raise
BenchmarkReport.record(op, locals(), result, tag="tileops")
def baseline_fn(x):
return x.bool().all(dim=-1)
result_bl = bm.profile(baseline_fn, *inputs)
BenchmarkReport.record(op, locals(), result_bl, tag="torch")
# ===================================================================
# CountNonzero benchmarks
# ===================================================================
@pytest.mark.parametrize("shape, dtype", workloads_to_params(_COUNT_NONZERO_OP))
def test_count_nonzero_bench(shape: tuple, dtype: torch.dtype) -> None:
test = CountNonzeroTest(shape, dtype)
inputs = test.gen_inputs()
op = CountNonzeroFwdOp(dtype=dtype, dim=-1)
bm = ManifestBenchmark(_COUNT_NONZERO_OP, op, test)
try:
result = bm.profile(op, *inputs)
except ValueError as exc:
if "No configurations to tune" in str(exc):
pytest.skip(f"Kernel does not support this shape: {exc}")
raise
BenchmarkReport.record(op, locals(), result, tag="tileops")
def baseline_fn(x):
return torch.count_nonzero(x, dim=-1).to(torch.int64)
result_bl = bm.profile(baseline_fn, *inputs)
BenchmarkReport.record(op, locals(), result_bl, tag="torch")
if __name__ == "__main__":
pytest.main([__file__, "-vvs"])