-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathbenchmark.py
More file actions
executable file
·329 lines (271 loc) · 11 KB
/
benchmark.py
File metadata and controls
executable file
·329 lines (271 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
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
# Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
import argparse
import math
import os
import random
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
import triton
from examples.common.utils import JSONWriter, Timestamps, is_triton_interpret_set
from examples.common.validation import validate_gemm
import iris
from matmul_wrapper import matmul
from gemm_all_scatter_bulk_synchronous import persistent_all_scatter
torch.manual_seed(123)
random.seed(123)
def parse_args():
parser = argparse.ArgumentParser(
description="Parse matrix dimensions and configuration.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("-m", type=int, default=8192, help="Number of rows in matrix A")
parser.add_argument("-n", type=int, default=4608, help="Number of columns in matrix B")
parser.add_argument("-k", type=int, default=36864, help="Common dimension between matrices A and B")
parser.add_argument("-d", "--debug", action="store_true", help="Enable debug mode")
parser.add_argument("-v", "--validate", action="store_true", help="Enable validation mode")
parser.add_argument("-t", "--trace_tiles", action="store_true", help="Enable tile-tracing mode")
parser.add_argument("-b", "--benchmark", action="store_true", help="Enable benchmarking mode")
parser.add_argument(
"--datatype",
type=str,
default="fp16",
choices=["fp16", "fp32", "bf16"],
help="Datatype of computation",
)
parser.add_argument(
"--output_file",
type=str,
default="log.json",
help="Output file",
)
parser.add_argument("--BLK_M", type=int, default=256, help="Block size M")
parser.add_argument("--BLK_N", type=int, default=64, help="Block size N")
parser.add_argument("--BLK_K", type=int, default=64, help="Block size K")
parser.add_argument("--gsize_m", type=int, default=6, help="L2-cache locality swizzle parameter")
parser.add_argument("--num_stages", type=int, default=2, help="Number of stages")
parser.add_argument("--heap_size", type=int, default=1 << 33, help="Iris heap size")
parser.add_argument(
"--gemm_sms",
type=int,
default=None,
help="Number of SMs for workgroup-specialized GEMM algorithm (default: auto-detected)",
)
parser.add_argument(
"--comm_sms", type=int, default=None, help="Number of SMs for All-Scatter kernel (default: auto-detected)"
)
parser.add_argument("-r", "--num_ranks", type=int, default=2, help="Number of ranks/processes")
return vars(parser.parse_args())
def _worker(local_rank: int, world_size: int, init_url: str, args: dict):
"""Worker function for PyTorch distributed execution."""
backend = "nccl" if torch.cuda.is_available() else "gloo"
dist.init_process_group(
backend=backend,
init_method=init_url,
world_size=world_size,
rank=local_rank,
device_id=torch.device(f"cuda:{local_rank}"),
)
shmem = iris.iris(args["heap_size"])
rank = shmem.get_rank()
world_size = shmem.get_num_ranks()
# Set default SM values if not provided
cu_count = torch.cuda.get_device_properties(rank).multi_processor_count
next_pow2 = 2 ** int(math.log2(cu_count)) if cu_count > 0 else 1
if args["gemm_sms"] is None:
# For wg_specialized: use next smaller power of 2
args["gemm_sms"] = next_pow2
if args["comm_sms"] is None:
# For bulk synchronous, use same as gemm_sms
args["comm_sms"] = next_pow2
# GEMM
datatype = torch.float32
if args["datatype"] == "fp16":
datatype = torch.float16
elif args["datatype"] == "fp32":
datatype = torch.float32
elif args["datatype"] == "bf16":
datatype = torch.bfloat16
else:
print("Unknown datatype.")
exit(1)
assert args["n"] % world_size == 0, f"N ({args['n']}) must be divisible by world size ({world_size})."
assert args["k"] % world_size == 0, f"K ({args['k']}) must be divisible by world size ({world_size})."
A = shmem.randn(args["m"], args["k"], device="cuda", dtype=datatype)
B = shmem.randn(args["n"], args["k"], device="cuda", dtype=datatype).T
args["M"] = args["m"]
args["N"] = args["n"]
args["K"] = args["k"]
json_writer = JSONWriter(args["output_file"])
json_writer.add_field("world_size", world_size)
# Splitting
args["n"] = args["n"] // world_size
local_B = B[:, rank * args["n"] : (rank + 1) * args["n"]].clone()
local_A = A
for key, value in args.items():
json_writer.add_field(key, value)
C = shmem.zeros((args["M"], args["N"]), device="cuda", dtype=A.dtype)
total_blocks_M = triton.cdiv(args["m"], args["BLK_M"])
total_blocks_N = triton.cdiv(args["n"], args["BLK_N"])
total_tiles = total_blocks_M * total_blocks_N
bias = None
num_xcds = iris.hip.get_num_xcc()
# This is one after another.
main_stream = torch.cuda.Stream()
json_writer.add_field("gemm_sms", args["gemm_sms"])
json_writer.add_field("comm_sms", args["comm_sms"])
kernel_timing = {
"gemm": {
"start_event": torch.cuda.Event(enable_timing=True),
"end_event": torch.cuda.Event(enable_timing=True),
"ms": 0,
"experiments": 0,
},
"communication": {
"start_event": torch.cuda.Event(enable_timing=True),
"end_event": torch.cuda.Event(enable_timing=True),
"ms": 0,
"experiments": 0,
},
}
# Allocate Timestamps
timestamps = Timestamps(num_tiles=total_tiles)
def run_experiment():
nonlocal C
nonlocal kernel_timing
shmem.barrier()
if args["trace_tiles"]:
timestamps.reset()
shmem.barrier()
torch.cuda.nvtx.range_push("GEMM + Communication")
torch.cuda.nvtx.range_push("GEMM")
with torch.cuda.stream(main_stream):
kernel_timing["gemm"]["start_event"].record()
C = matmul.apply(
local_A,
local_B,
C,
bias,
rank,
world_size,
args["gemm_sms"],
args["BLK_M"],
args["BLK_N"],
args["BLK_K"],
args["gsize_m"],
args["num_stages"],
shmem.get_heap_bases(),
"gfx942",
args["trace_tiles"],
timestamps.mm_begin_timestamp,
timestamps.mm_end_timestamp,
)
kernel_timing["gemm"]["end_event"].record()
kernel_timing["gemm"]["experiments"] += 1
torch.cuda.nvtx.range_pop()
torch.cuda.nvtx.range_push("Communication")
with torch.cuda.stream(main_stream):
kernel_timing["communication"]["start_event"].record()
persistent_all_scatter[(args["comm_sms"],)](
C,
args["M"],
args["n"],
C.stride(0),
C.stride(1),
args["BLK_M"],
args["BLK_N"],
args["gsize_m"],
args["comm_sms"],
num_xcds,
shmem.get_heap_bases(),
rank,
world_size,
args["trace_tiles"],
timestamps.mm_begin_timestamp,
timestamps.mm_end_timestamp,
)
kernel_timing["communication"]["end_event"].record()
kernel_timing["communication"]["experiments"] += 1
torch.cuda.nvtx.range_pop()
shmem.barrier()
for k in ["gemm", "communication"]:
ms = kernel_timing[k]["start_event"].elapsed_time(kernel_timing[k]["end_event"])
kernel_timing[k]["ms"] += ms
torch.cuda.nvtx.range_pop()
# Synchronize across all GPUs
shmem.barrier()
# Warmup
run_experiment()
shmem.barrier()
for k in ["gemm", "communication"]:
kernel_timing[k]["ms"] = 0
kernel_timing[k]["experiments"] = 0
if args["validate"]:
shmem.info("Validating...")
matmul.set_debug(True)
# Validate global result
success = validate_gemm(A, B, C, shmem)
passed_str = "passed" if success else "failed"
shmem.info(f"Final C validation {passed_str}.")
# Wait for all to finish validation
shmem.barrier()
shmem.info("Validating local C...")
json_writer.add_field("success", success)
if not is_triton_interpret_set():
gemm_registers = matmul.get_matmul_registers()
gemm_spills = matmul.get_matmul_spills()
json_writer.add_field("gemm_registers", gemm_registers)
json_writer.add_field("gemm_spills", gemm_spills)
shmem.info("Validation completed")
if args["benchmark"]:
matmul.set_debug(False)
shmem.info("Benchmarking...")
perf = lambda ms: 2 * args["M"] * args["N"] * args["K"] * 1e-12 / (ms * 1e-3)
triton_ms = iris.do_bench(run_experiment, shmem.barrier)
triton_tflops = perf(triton_ms)
algo_string = "all_scatter"
shmem.info(
f"tile matmul + {algo_string} (total_tiles={total_tiles}): {triton_ms:.3f} ms {triton_tflops:.3f} tflops"
)
json_writer.add_field("tflops", triton_tflops)
json_writer.add_field("total_ms", triton_ms)
for k in ["gemm", "communication"]:
json_writer.add_field(k + "_ms", kernel_timing[k]["ms"] / kernel_timing[k]["experiments"])
json_writer.add_field(k + "_experiments", kernel_timing[k]["experiments"])
# Wait for all to finish benchmarking
shmem.barrier()
if rank == 0:
json_writer.flush()
json_writer.display()
if args["trace_tiles"] and rank == 0:
gpu_freq = iris.hip.get_wall_clock_rate(rank) * 1e-3
algo_string = "all_scatter"
filename = f"gemm_tiles_{algo_string}_trace_rank{rank}.json"
timestamps.to_json(filename, gpu_freq)
shmem.barrier()
dist.destroy_process_group()
def main():
print("Starting GEMM all_scatter bulk synchronous benchmark...")
args = parse_args()
# Check if running with torchrun (detected by environment variables)
if "RANK" in os.environ and "LOCAL_RANK" in os.environ:
# torchrun handles process spawning, so call _worker directly
print("Detected torchrun execution mode")
rank = int(os.environ.get("RANK", 0))
world_size = int(os.environ.get("WORLD_SIZE", 1))
init_url = os.environ.get("MASTER_ADDR", "127.0.0.1") + ":" + os.environ.get("MASTER_PORT", "29500")
_worker(rank, world_size, f"tcp://{init_url}", args)
else:
# Use multiprocessing spawn for backward compatibility
num_ranks = args["num_ranks"]
init_url = "tcp://127.0.0.1:29500"
mp.spawn(
fn=_worker,
args=(num_ranks, init_url, args),
nprocs=num_ranks,
join=True,
)
if __name__ == "__main__":
main()