forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_softmax.py
More file actions
175 lines (143 loc) · 5.5 KB
/
Copy pathbench_softmax.py
File metadata and controls
175 lines (143 loc) · 5.5 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
"""Benchmarks for softmax-family ops (softmax, log_softmax, logsumexp).
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
import torch.nn.functional as F
from benchmarks.benchmark_base import BenchmarkReport, ManifestBenchmark, workloads_to_params
from tileops.ops.reduction.log_softmax import LogSoftmaxFwdOp
from tileops.ops.reduction.logsumexp import LogSumExpFwdOp
from tileops.ops.reduction.softmax import SoftmaxFwdOp
from workloads.softmax import (
LogSoftmaxTest,
LogSumExpTest,
SoftmaxTest,
)
# ===================================================================
# Op name constants
# ===================================================================
_SOFTMAX_OP = "SoftmaxFwdOp"
_LOG_SOFTMAX_OP = "LogSoftmaxFwdOp"
_LOGSUMEXP_OP = "LogSumExpFwdOp"
# ===================================================================
# Softmax benchmarks
# ===================================================================
@pytest.mark.parametrize("shape, dtype", workloads_to_params(_SOFTMAX_OP))
def test_softmax_bench(shape: tuple, dtype: torch.dtype) -> None:
test = SoftmaxTest(shape, dtype)
inputs = test.gen_inputs()
op = SoftmaxFwdOp(N=shape[-1], dtype=dtype, dim=-1, tune=True)
bm = ManifestBenchmark(_SOFTMAX_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 F.softmax(x, dim=-1)
result_bl = bm.profile(baseline_fn, *inputs)
BenchmarkReport.record(op, locals(), result_bl, tag="torch")
# ===================================================================
# LogSoftmax benchmarks
# ===================================================================
@pytest.mark.parametrize("shape, dtype", workloads_to_params(_LOG_SOFTMAX_OP))
def test_log_softmax_bench(shape: tuple, dtype: torch.dtype) -> None:
test = LogSoftmaxTest(shape, dtype)
inputs = test.gen_inputs()
op = LogSoftmaxFwdOp(N=shape[-1], dtype=dtype, dim=-1, tune=True)
bm = ManifestBenchmark(_LOG_SOFTMAX_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 F.log_softmax(x, dim=-1)
result_bl = bm.profile(baseline_fn, *inputs)
BenchmarkReport.record(op, locals(), result_bl, tag="torch")
# ===================================================================
# LogSumExp benchmarks
# ===================================================================
@pytest.mark.parametrize("shape, dtype", workloads_to_params(_LOGSUMEXP_OP))
def test_logsumexp_bench(shape: tuple, dtype: torch.dtype) -> None:
test = LogSumExpTest(shape, dtype)
inputs = test.gen_inputs()
op = LogSumExpFwdOp(dtype=dtype, dim=-1, tune=False)
bm = ManifestBenchmark(_LOGSUMEXP_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.logsumexp(x, dim=-1)
result_bl = bm.profile(baseline_fn, *inputs)
BenchmarkReport.record(op, locals(), result_bl, tag="torch")
@pytest.mark.parametrize(
"op_name, op_cls, test_cls, baseline_fn, shape, dtype",
[
pytest.param(
_SOFTMAX_OP,
SoftmaxFwdOp,
SoftmaxTest,
lambda x: F.softmax(x, dim=-1),
(256, 1024),
torch.float16,
id="softmax-musa-smoke-fp16",
),
pytest.param(
_SOFTMAX_OP,
SoftmaxFwdOp,
SoftmaxTest,
lambda x: F.softmax(x, dim=-1),
(256, 1024),
torch.bfloat16,
id="softmax-musa-smoke-bf16",
),
pytest.param(
_LOG_SOFTMAX_OP,
LogSoftmaxFwdOp,
LogSoftmaxTest,
lambda x: F.log_softmax(x, dim=-1),
(256, 1024),
torch.float16,
id="log-softmax-musa-smoke-fp16",
),
pytest.param(
_LOG_SOFTMAX_OP,
LogSoftmaxFwdOp,
LogSoftmaxTest,
lambda x: F.log_softmax(x, dim=-1),
(256, 1024),
torch.bfloat16,
id="log-softmax-musa-smoke-bf16",
),
],
)
@pytest.mark.smoke
def test_softmax_family_bench_musa_smoke(
op_name: str,
op_cls,
test_cls,
baseline_fn,
shape: tuple[int, ...],
dtype: torch.dtype,
) -> None:
"""Small benchmark proof cases for the softmax/log_softmax MUSA path."""
test = test_cls(shape, dtype)
inputs = test.gen_inputs()
op = op_cls(dtype=dtype, dim=-1, tune=False)
bm = ManifestBenchmark(op_name, op, test)
result = bm.profile(op, *inputs)
BenchmarkReport.record(op, locals(), result, tag="tileops")
result_bl = bm.profile(baseline_fn, *inputs)
BenchmarkReport.record(op, locals(), result_bl, tag="torch")
if __name__ == "__main__":
pytest.main([__file__, "-vvs"])