forked from flagos-ai/FlagGems
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_unary_pointwise_perf.py
More file actions
369 lines (312 loc) · 11 KB
/
Copy pathtest_unary_pointwise_perf.py
File metadata and controls
369 lines (312 loc) · 11 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
from typing import Generator
import pytest
import torch
import flag_gems
from benchmark.attri_util import (
BOOL_DTYPES,
COMPLEX_DTYPES,
DEFAULT_METRICS,
FLOAT_DTYPES,
INT_DTYPES,
)
from benchmark.performance_utils import Benchmark, SkipVersion, generate_tensor_input
vendor_name = flag_gems.vendor_name
fp64_is_supported = flag_gems.runtime.device.support_fp64
class UnaryPointwiseBenchmark(Benchmark):
"""
Base class for benchmarking unary pointwise operations.
"""
DEFAULT_METRICS = DEFAULT_METRICS[:] + ["tflops"]
def set_more_shapes(self):
special_shapes_2d = [(1024, 2**i) for i in range(0, 20, 4)]
sp_shapes_3d = [(64, 64, 2**i) for i in range(0, 15, 4)]
return special_shapes_2d + sp_shapes_3d
def get_input_iter(self, cur_dtype) -> Generator:
for shape in self.shapes:
inp = generate_tensor_input(shape, cur_dtype, self.device)
yield inp,
def get_tflops(self, op, *args, **kwargs):
shape = list(args[0].shape)
return torch.tensor(shape).prod().item()
forward_operations = [
("abs", torch.abs, FLOAT_DTYPES),
("angle", torch.angle, COMPLEX_DTYPES + [torch.float32] + INT_DTYPES + BOOL_DTYPES),
("erf", torch.erf, FLOAT_DTYPES),
("exp", torch.exp, FLOAT_DTYPES),
("exp2", torch.exp2, FLOAT_DTYPES),
("neg", torch.neg, FLOAT_DTYPES),
("reciprocal", torch.reciprocal, FLOAT_DTYPES),
("sqrt", torch.sqrt, FLOAT_DTYPES),
("rsqrt", torch.rsqrt, FLOAT_DTYPES),
("logical_not", torch.logical_not, INT_DTYPES + BOOL_DTYPES),
("log", torch.log, FLOAT_DTYPES),
# ("triu", torch.triu, FLOAT_DTYPES), # do not support 1d shapes
# Dropout
("dropout", torch.nn.Dropout(p=0.5), FLOAT_DTYPES),
# Activation operations
("celu", torch.nn.functional.celu, FLOAT_DTYPES),
("elu", torch.nn.functional.elu, FLOAT_DTYPES),
("gelu", torch.nn.functional.gelu, FLOAT_DTYPES),
("relu", torch.nn.functional.relu, FLOAT_DTYPES),
("softplus", torch.nn.functional.softplus, FLOAT_DTYPES),
("sigmoid", torch.sigmoid, FLOAT_DTYPES),
("log_sigmoid", torch.nn.functional.logsigmoid, FLOAT_DTYPES),
("silu", torch.nn.functional.silu, FLOAT_DTYPES),
# Trigonometric operations
("cos", torch.cos, FLOAT_DTYPES),
("sin", torch.sin, FLOAT_DTYPES),
("tan", torch.tan, FLOAT_DTYPES),
("tanh", torch.tanh, FLOAT_DTYPES),
("atan", torch.atan, FLOAT_DTYPES),
# Bitwise operations
("bitwise_not", torch.bitwise_not, INT_DTYPES),
# Numerical Checks
("isinf", torch.isinf, FLOAT_DTYPES),
("isnan", torch.isnan, FLOAT_DTYPES),
("isfinite", torch.isfinite, FLOAT_DTYPES),
]
@pytest.mark.parametrize(
"op_name, torch_op, dtypes",
[
pytest.param(
name,
op,
dtype,
marks=getattr(pytest.mark, name, None),
)
for name, op, dtype in forward_operations
],
)
def test_general_unary_pointwise_perf(op_name, torch_op, dtypes):
if vendor_name == "kunlunxin":
if op_name in ["celu"] and SkipVersion("torch", "<2.5"):
pytest.skip(
"There is an error in kunlunxin torch 2.0 aten, please use torch 2.5 instead"
)
bench = UnaryPointwiseBenchmark(op_name=op_name, torch_op=torch_op, dtypes=dtypes)
bench.run()
forward_inplace_operations = [
("abs_", torch.abs_, FLOAT_DTYPES),
# ("angle", torch.angle, COMPLEX_DTYPES + [torch.float32] + INT_DTYPES + BOOL_DTYPES),
("erf_", torch.erf_, FLOAT_DTYPES),
("exp_", torch.exp_, FLOAT_DTYPES),
("exp2_", torch.exp2_, FLOAT_DTYPES),
("neg_", torch.neg_, FLOAT_DTYPES),
("reciprocal_", torch.reciprocal_, FLOAT_DTYPES),
("sqrt_", torch.sqrt_, FLOAT_DTYPES),
("rsqrt_", torch.rsqrt_, FLOAT_DTYPES),
# Activation operations
("celu_", torch.nn.functional.celu_, FLOAT_DTYPES),
("elu_", torch.nn.functional.elu_, FLOAT_DTYPES),
("gelu_", torch.ops.aten.gelu_.default, FLOAT_DTYPES),
("relu_", torch.relu_, FLOAT_DTYPES),
("sigmoid_", torch.sigmoid_, FLOAT_DTYPES),
("silu_", lambda a: torch.nn.functional.silu(a, inplace=True), FLOAT_DTYPES),
# Trigonometric operations
("cos_", torch.cos_, FLOAT_DTYPES),
("sin_", torch.sin_, FLOAT_DTYPES),
("tan_", torch.tan_, FLOAT_DTYPES),
("tanh_", torch.tanh_, FLOAT_DTYPES),
("atan_", torch.atan_, FLOAT_DTYPES),
# Bitwise operations
("bitwise_not_", lambda a: a.bitwise_not_(), INT_DTYPES),
]
@pytest.mark.parametrize(
"op_name, torch_op, dtypes",
[
pytest.param(
name,
op,
dtype,
marks=getattr(pytest.mark, name, None),
)
for name, op, dtype in forward_inplace_operations
],
)
def test_general_inplace_unary_pointwise_perf(op_name, torch_op, dtypes):
if vendor_name == "kunlunxin":
if op_name in ["celu_"] and SkipVersion("torch", "<2.5"):
pytest.skip(
"There is an error in kunlunxin torch 2.0 aten, please use torch 2.5 instead"
)
bench = UnaryPointwiseBenchmark(
op_name=op_name, torch_op=torch_op, dtypes=dtypes, is_inplace=True
)
bench.run()
backward_operations = [
("gelu", torch.nn.functional.gelu, FLOAT_DTYPES),
]
@pytest.mark.parametrize(
"op_name, torch_op, dtypes",
[
pytest.param(
name,
op,
dtype,
marks=getattr(pytest.mark, name + "_backward", None),
)
for name, op, dtype in backward_operations
],
)
def test_general_unary_pointwise_backward_perf(op_name, torch_op, dtypes):
bench = UnaryPointwiseBenchmark(
op_name=op_name,
torch_op=torch_op,
dtypes=dtypes,
is_backward=True,
)
bench.run()
class ToCopyBenchmark(UnaryPointwiseBenchmark):
def get_input_iter(self, cur_dtype) -> Generator:
for shape in self.shapes:
inp = torch.randn(shape, dtype=torch.float32, device=self.device)
yield inp, {"dtype": cur_dtype}
@pytest.mark.to_copy
def test_to_copy_perf():
bench = ToCopyBenchmark(
op_name="to_copy",
torch_op=torch.ops.aten._to_copy,
dtypes=[torch.float16, torch.bfloat16]
+ ([torch.float64] if fp64_is_supported else []),
)
bench.run()
class CopyInplaceBenchmark(Benchmark):
def get_input_iter(self, cur_dtype) -> Generator:
for shape in self.shapes:
dst = generate_tensor_input(shape, cur_dtype, self.device)
src = generate_tensor_input(shape, cur_dtype, self.device)
yield dst, src
@pytest.mark.copy_
@pytest.mark.skipif(
SkipVersion("torch", "<2.4"),
reason="The copy operator implement required for torch >= 2.4",
)
def test_copy_inplace_perf():
bench = CopyInplaceBenchmark(
op_name="copy_",
torch_op=torch.ops.aten.copy_,
dtypes=FLOAT_DTYPES + INT_DTYPES + BOOL_DTYPES,
is_inplace=True,
)
bench.run()
class EluBackwardBenchmark(UnaryPointwiseBenchmark):
def get_input_iter(self, cur_dtype: torch.dtype) -> Generator:
for shape in self.shapes:
inp = generate_tensor_input(shape, cur_dtype, self.device)
grad_out = torch.randn_like(inp)
alpha = 1.0
scale = 1.0
input_scale = 1.0
is_result = False
yield grad_out, alpha, scale, input_scale, is_result, inp
@pytest.mark.elu
def test_elu_backward_perf():
bench = EluBackwardBenchmark(
op_name="elu_backward",
torch_op=torch.ops.aten.elu_backward,
dtypes=FLOAT_DTYPES,
)
bench.run()
class GluBenchmark(UnaryPointwiseBenchmark):
# Glu test requires even numbers
def set_more_shapes(self):
special_shapes_2d = [(1024, 2**i) for i in range(1, 20, 4)]
sp_shapes_3d = [(64, 64, 2**i) for i in range(1, 15, 4)]
return special_shapes_2d + sp_shapes_3d
@pytest.mark.glu
def test_glu_perf():
bench = GluBenchmark(
op_name="glu",
torch_op=torch.nn.functional.glu,
dtypes=FLOAT_DTYPES,
)
bench.run()
@pytest.mark.glu
def test_glu_backward_perf():
bench = GluBenchmark(
op_name="glu",
torch_op=torch.nn.functional.glu,
dtypes=FLOAT_DTYPES,
is_backward=True,
)
bench.run()
class BinaryPointwiseBenchmark(Benchmark):
def set_more_shapes(self):
special_shapes_2d = [(1024, 2**i) for i in range(0, 20, 4)]
sp_shapes_3d = [(64, 64, 2**i) for i in range(0, 15, 4)]
return special_shapes_2d + sp_shapes_3d
def get_input_iter(self, cur_dtype) -> Generator:
for shape in self.shapes:
inp1 = generate_tensor_input(shape, cur_dtype, self.device)
shift_amount = torch.randint(0, 8, shape, dtype=cur_dtype, device="cpu").to(
self.device
)
yield inp1, shift_amount
@pytest.mark.bitwise_left_shift
def test_bitwise_left_shift_perf():
bench = BinaryPointwiseBenchmark(
op_name="bitwise_left_shift",
torch_op=torch.bitwise_left_shift,
dtypes=INT_DTYPES,
)
bench.run()
@pytest.mark.bitwise_right_shift
def test_bitwise_right_shift_perf():
bench = BinaryPointwiseBenchmark(
op_name="bitwise_right_shift",
torch_op=torch.bitwise_right_shift,
dtypes=INT_DTYPES,
)
bench.run()
class RepetitionPenaltyBenchmark(Benchmark):
def __init__(self, op_name, torch_op, dtypes):
super().__init__(op_name, torch_op, dtypes)
self.gems_op = None
def set_shapes(self, shape_file_path=None):
self.shapes = [
(1, 1024),
(1, 4096),
(1, 8192),
(8, 4096),
(16, 4096),
(32, 1024),
(8, 8192),
(64, 32000),
]
def get_input_iter(self, cur_dtype):
for shape in self.shapes:
num_seqs, vocab_size = shape
yield (
torch.randn(shape, dtype=cur_dtype, device=self.device),
torch.randint(0, 2, shape, dtype=torch.bool, device=self.device),
torch.randint(0, 2, shape, dtype=torch.bool, device=self.device),
torch.empty(num_seqs, dtype=cur_dtype, device=self.device).uniform_(
1.0, 2.0
),
)
def set_gems(self, gems_op):
self.gems_op = gems_op
UNSUPPORTED_VENDORS = {
"metax",
"kunlunxin",
"iluvatar",
"mthreads",
"hygon",
"cambricon",
}
@pytest.mark.skipif(SkipVersion("vllm", "<0.4"), reason="vLLM <0.4 not supported")
@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA required")
@pytest.mark.skipif(
flag_gems.vendor_name in UNSUPPORTED_VENDORS, reason="Vendor not supported"
)
@pytest.mark.apply_repetition_penalties
@pytest.mark.performance
def test_perf_repetition_penalty():
vllm_ops = pytest.importorskip("vllm._custom_ops")
bench = RepetitionPenaltyBenchmark(
op_name="apply_repetition_penalties",
torch_op=vllm_ops.apply_repetition_penalties,
dtypes=FLOAT_DTYPES,
)
bench.set_gems(flag_gems.apply_repetition_penalties)
bench.run()