-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathconsts.py
More file actions
320 lines (268 loc) · 10.3 KB
/
Copy pathconsts.py
File metadata and controls
320 lines (268 loc) · 10.3 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
# Copyright 2026 FlagOS Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import itertools
from dataclasses import asdict, dataclass, fields
from enum import Enum
from typing import List, Optional, Tuple
import torch
import flag_gems
FLOAT_DTYPES = [torch.float16, torch.float32, torch.bfloat16]
INT_DTYPES = [torch.int16, torch.int32]
BOOL_DTYPES = [torch.bool]
COMPLEX_DTYPES = [torch.complex64]
EXTRA_INT_DTYPES = [torch.int8, torch.uint8, torch.int64]
def get_fp8_dtype():
if flag_gems.device != "cuda" or not torch.cuda.is_available():
return None
major, _ = torch.cuda.get_device_capability()
if major > 8 and hasattr(torch, "float8_e4m3fn"):
return torch.float8_e4m3fn
if major == 8 and hasattr(torch, "float8_e5m2"):
return torch.float8_e5m2
return None
FP8_DTYPES = [get_fp8_dtype()]
DEFAULT_WARMUP_TIME = 1000
DEFAULT_ITER_TIME = 100
# LEGACY_SHAPES are maintained for legacy benchmark SIZE settings and may be removed in the future.
# Do not reference this elsewhere.
LEGACY_SHAPES = [i * 64 for i in range(1, 22, 5)]
LEGACY_NON_BLAS_SHAPES = [(1024, shape) for shape in LEGACY_SHAPES]
LEGACY_BLAS_SHAPES = [(16, shape, shape, shape) for shape in LEGACY_SHAPES]
# Default shapes settings
DEFAULT_SHAPES = [
(1024 * 1024 * 1024,), # from perf
(64, 64),
(4096, 4096),
(64, 512, 512),
(1024, 1024, 1024), # from perf
]
def model_shapes():
# batch sizes * seq lengths
BS = [1, 2, 3, 4, 8, 98, 256, 8192]
# attn: wqkv, wo; ffn: w13, w2
NK = [
# extract from llama3-8b
(1024, 4096),
(128256, 4096),
(14336, 4096),
(4096, 14336),
(4096, 4096),
(6144, 4096),
(28672, 4096),
# extract from qwen2.5-7b
(3584, 3584),
(18944, 3584),
(3584, 18944),
(152064, 3584),
(37888, 3584),
(512, 3584),
(4608, 3584),
]
return [(4, bs, n, k) for bs, (n, k) in itertools.product(BS, NK)]
@dataclass
class BenchmarkMetrics:
# Legacy shape information for backward compatibility
# This field corresponds to the 'size' field in the previous version's benchmark.
legacy_shape: Optional[int] = None
# Detailed size info
shape_detail: Optional[Tuple[int, ...]] = None
# Latency base in ms
latency_base: Optional[float] = None
# Latency in ms
latency: Optional[float] = None
gbps_base: Optional[float] = None
gbps: Optional[float] = None
# Speedup over baseline
speedup: Optional[float] = None
# Accuracy over baseline (not implemented yet)
accuracy: Optional[float] = None
# TFLOPS (not implemented yet)
tflops: Optional[float] = None
# Utilization (not implemented yet)
utilization: Optional[float] = None
# Speedup compared to base data
compared_speedup: Optional[float] = None
# Error message
error_msg: Optional[str] = None
ALL_AVAILABLE_METRICS = set(map(lambda x: x.name, fields(BenchmarkMetrics))) - {
"legacy_shape",
"shape_detail",
}
DEFAULT_METRICS = [
metric
for metric in ["latency_base", "latency", "speedup"]
if metric in ALL_AVAILABLE_METRICS
]
def check_metric_dependencies(
requested_metrics: Optional[List[str]],
) -> Optional[List[str]]:
"""
Checks if the requested metrics satisfy their dependencies.
Returns True if the dependencies are satisfied, otherwise False.
"""
# Predefined dependencies between metrics
buildin_dependencies = {
"speedup": ["latency", "latency_base"],
"utilization": ["latency", "tflops"],
}
unsatisfied_metrics = []
if requested_metrics is None:
return unsatisfied_metrics
satisfied_metrics = set()
for metric in requested_metrics:
if metric not in buildin_dependencies:
# If the metric has no dependencies, it's automatically satisfied
satisfied_metrics.add(metric)
else:
required_metrics = buildin_dependencies[metric]
# Check if all dependencies are in the satisfied metrics list
if not all(req in satisfied_metrics for req in required_metrics):
unsatisfied_metrics.append(metric)
else:
satisfied_metrics.add(metric)
return unsatisfied_metrics
def get_recommended_shapes(
op_name: str, op_specified_shapes: Optional[List[Tuple[int, ...]]]
):
def _shapes_sort(shapes):
shapes = [shape if isinstance(shape, tuple) else (shape,) for shape in shapes]
return sorted(shapes, key=lambda x: torch.tensor(x).prod().item())
if op_specified_shapes:
# TODO: handle situation that list as the basic element in shape.
return _shapes_sort(op_specified_shapes)
return _shapes_sort(DEFAULT_SHAPES)
class BenchMode(Enum):
KERNEL = "kernel"
OPERATOR = "operator"
WRAPPER = "wrapper"
CUDAGRAPH = "cudagraph"
class BenchLevel(Enum):
COMPREHENSIVE = "comprehensive"
CORE = "core"
@dataclass
class OperationAttribute:
op_name: str
# Recommended core benchmark shapes for the given operation
recommended_core_shapes: List[Tuple[int, ...]]
shape_desc: str
def __str__(self) -> str:
return (
f"{'Operator name':<40} | {self.op_name}\n"
f"{'Recommended Core Shapes[' + self.shape_desc + ']':<40} | {self.recommended_core_shapes}\n"
)
def to_dict(self) -> dict:
return self.__dict__
def custom_json_encoder(obj):
if isinstance(obj, torch.dtype):
return str(obj)
raise TypeError(f"Object of type {obj.__class__.__name__} is not JSON serializable")
@dataclass
class BenchmarkResult:
"""Record the benchmark result for each operator."""
# Unique name of the operator
op_name: str
dtype: str
mode: str
level: str
# Benchmark results
result: List[BenchmarkMetrics]
def __str__(self) -> str:
header_title = (
f"\nOperator: {self.op_name} Performance Test (dtype={self.dtype}, mode={self.mode},"
f"level={self.level})\n"
)
native_baseline_skip_reason = getattr(self, "native_baseline_skip_reason", None)
if native_baseline_skip_reason:
header_title += f"Native baseline: N/A ({native_baseline_skip_reason})\n"
col_names = [
f"{'Status':<10}",
f"{'Torch Latency (ms)':>20}",
f"{'Gems Latency (ms)':>20}",
f"{'Gems Speedup':>20}",
]
if self.result[0].tflops and self.result[0].tflops != 0.0:
col_names.append(f"{'TFLOPS':>20}")
if self.result[0].gbps is not None:
col_names.append(f"{'Torch GBPS ':>20}")
col_names.append(f"{'Gems GBPS ':>20}")
col_names.append(f"{'Size Detail':>20}\n")
header_col_names = " ".join(col_names)
header_break = "-" * len(header_col_names) + "\n"
header = header_title + header_col_names + header_break
metrics_lines = "".join(self._format_metrics(ele) for ele in self.result)
return header + metrics_lines
def _format_metrics(self, metrics: BenchmarkMetrics) -> str:
# self.gen_legacy_shape(metrics)
# legacy_shape_str = (
# metrics.legacy_shape if metrics.legacy_shape is not None else "N/A"
# )
latency_base_str = (
f"{metrics.latency_base:.6f}" if metrics.latency_base is not None else "N/A"
)
latency_str = f"{metrics.latency:.6f}" if metrics.latency is not None else "N/A"
speedup_str = f"{metrics.speedup:.3f}" if metrics.speedup is not None else "N/A"
torch_gbps_str = (
f"{metrics.gbps_base:.3f}" if metrics.gbps_base is not None else "N/A"
)
gems_gbps_str = f"{metrics.gbps:.3f}" if metrics.gbps is not None else "N/A"
if metrics.tflops and metrics.tflops != 0.0:
tflops_str = (
f"{metrics.tflops:.3f}" if metrics.tflops is not None else "N/A"
)
shape_detail_str = (
metrics.shape_detail if metrics.shape_detail is not None else "N/A"
)
status = "SUCCESS" if metrics.error_msg is None else "FAILED"
data_line = (
f"{status:<10}"
f"{latency_base_str:>20}"
f"{latency_str:>20}"
f"{speedup_str:>20}"
)
if metrics.tflops and metrics.tflops != 0.0:
data_line += f"{tflops_str:>20}"
if metrics.gbps is not None:
data_line += f"{torch_gbps_str:>20}{gems_gbps_str:>20}"
data_line += " " * 10
data_line += f"{shape_detail_str}\n"
return data_line
def gen_legacy_shape(self, metrics: BenchmarkMetrics) -> Optional[int]:
first_shape = (
metrics.shape_detail[0] if isinstance(metrics.shape_detail, list) else None
)
to_record_shape = (
tuple(first_shape) if isinstance(first_shape, torch.Size) else None
)
if to_record_shape in LEGACY_NON_BLAS_SHAPES:
metrics.legacy_shape = to_record_shape[-1]
elif (
isinstance(to_record_shape, tuple)
and len(to_record_shape) == 2
and to_record_shape[0] == 1024
):
metrics.legacy_shape = to_record_shape[-1]
else:
metrics.legacy_shape = None
def to_json(self) -> str:
import json
# Convert to dict and handle tuple serialization for shape_detail
result_dict = asdict(self)
native_baseline_skip_reason = getattr(self, "native_baseline_skip_reason", None)
if native_baseline_skip_reason:
result_dict["native_baseline_skip_reason"] = native_baseline_skip_reason
return json.dumps(result_dict, default=custom_json_encoder)
def to_dict(self) -> dict:
return self.__dict__
# Subset dtypes for specific operators
FP16_BF16_DTYPES = [torch.float16, torch.bfloat16]