-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
181 lines (143 loc) · 4.93 KB
/
utils.py
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
import os
import re
from contextlib import nullcontext
from typing import Callable, List, Optional
import torch
import torch.distributed as dist
def benchmark_with_profiler(
target_fn: Callable[[None], None],
event_key_regex: str,
warmup_iters: int = 200,
benchmark_iters: int = 25,
profile_ranks: Optional[List[int]] = None,
flush_l2: bool = False,
) -> float:
"""
Benchmark the target function with PyTorch profiler.
Args:
target_fn: The target function to benchmark.
event_key_regex: The regex pattern to identify the profiler event
associated with the target function.
profile_ranks: The ranks to profile.
warmup_iters: The number of warmup iterations.
benchmark_iters: The number of benchmark iterations.
flush_l2: Whether to flush the L2 cache before each invocation of the
target function.
Returns:
The measured median latency in microseconds.
"""
if "BENCHMARK_ITERS" in os.environ:
benchmark_iters = int(os.environ["BENCHMARK_ITERS"])
rank = dist.get_rank() if dist.is_initialized() else 0
profile_ranks = profile_ranks or [0]
if flush_l2:
cache = torch.empty(int(256e6 // 4), dtype=torch.int, device="cuda")
if rank in profile_ranks:
try:
from trace_handler import trace_handler
except ImportError:
trace_handler = None
if "NO_TRACE" in os.environ:
trace_handler = None
prof = torch.profiler.profile(
activities=[
torch.profiler.ProfilerActivity.CPU,
torch.profiler.ProfilerActivity.CUDA,
],
on_trace_ready=trace_handler,
)
else:
prof = nullcontext()
for _ in range(warmup_iters):
target_fn()
if dist.is_initialized():
dist.barrier(device_ids=[torch.cuda.current_device()])
torch.cuda.synchronize()
with prof:
torch.cuda._sleep(int(2e7))
for i in range(benchmark_iters):
if flush_l2:
cache.zero_()
target_fn()
torch.cuda.synchronize()
if rank not in profile_ranks:
return 0
latencies_us = []
for event in prof.events():
if re.match(event_key_regex, event.key):
latencies_us.append(event.device_time)
if len(latencies_us) == 0:
return 0
return torch.tensor(latencies_us).median().item()
def benchmark_with_event(
target_fn: Callable[[None], None],
warmup_iters: int = 200,
benchmark_iters: int = 25,
profile_ranks: Optional[List[int]] = None,
flush_l2: bool = False,
cuda_graph: bool = False,
) -> float:
if cuda_graph:
target_fn()
g = torch.cuda.CUDAGraph()
with torch.cuda.graph(g):
target_fn()
target_fn = lambda: g.replay()
if "BENCHMARK_ITERS" in os.environ:
benchmark_iters = int(os.environ["BENCHMARK_ITERS"])
rank = dist.get_rank() if dist.is_initialized() else 0
profile_ranks = profile_ranks or [0]
if flush_l2:
cache = torch.empty(int(256e6 // 4), dtype=torch.int, device="cuda")
for _ in range(warmup_iters):
target_fn()
if dist.is_initialized():
dist.barrier(device_ids=[torch.cuda.current_device()])
torch.cuda.synchronize()
begin_events = [
torch.cuda.Event(enable_timing=True) for _ in range(benchmark_iters)
]
end_events = [torch.cuda.Event(enable_timing=True) for _ in range(benchmark_iters)]
if rank in profile_ranks:
try:
from trace_handler import trace_handler
except ImportError:
trace_handler = None
if "NO_TRACE" in os.environ:
trace_handler = None
prof = torch.profiler.profile(
activities=[
torch.profiler.ProfilerActivity.CPU,
torch.profiler.ProfilerActivity.CUDA,
],
on_trace_ready=trace_handler,
)
else:
prof = nullcontext()
with prof:
torch.cuda._sleep(int(2e7))
for i in range(benchmark_iters):
if flush_l2:
cache.zero_()
begin_events[i].record()
target_fn()
end_events[i].record()
torch.cuda.synchronize()
latencies = [b.elapsed_time(e) for b, e in zip(begin_events, end_events)]
return torch.tensor(latencies).median().item() * 1000
triton_kernels = {}
def log_triton_kernel(kernel):
import atexit
import tempfile
if dist.is_initialized() and dist.get_rank() != 0:
return
def on_exit():
print("PTX files:")
for kernel in triton_kernels:
f = tempfile.NamedTemporaryFile(dir="/tmp", delete=False)
f.write(kernel.asm["ptx"].encode("utf-8"))
print(f"+- {kernel.name}: {f.name}")
if len(triton_kernels) == 0:
atexit.register(on_exit)
if kernel not in triton_kernels:
triton_kernels[kernel] = None