-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcudagraph.py
More file actions
144 lines (123 loc) · 4.02 KB
/
cudagraph.py
File metadata and controls
144 lines (123 loc) · 4.02 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
import argparse
import contextlib
import time
from typing import NamedTuple
import torch
import triton
import triton.language as tl
import triton.profiler as proton
import cupti_min_profiler
def metadata_fn(grid: tuple, metadata: NamedTuple, args: dict):
vec = torch.arange(256, device="cuda")
return {"name": "dot", "flops": 512 * 512 * 2, "vec": vec}
@triton.jit(launch_metadata=metadata_fn)
def triton_dot(a_ptr, b_ptr, c_ptr, BLOCK_SIZE: tl.constexpr):
offs = tl.arange(0, BLOCK_SIZE)
a = tl.load(a_ptr + offs[:, None] * BLOCK_SIZE + offs[None, :])
b = tl.load(b_ptr + offs[:, None] * BLOCK_SIZE + offs[None, :])
c = tl.dot(a, b)
tl.store(c_ptr + offs[:, None] * BLOCK_SIZE + offs[None, :], c)
def fn(
num_scopes: int,
workload_size: int = 1024,
dot_every: int = 1,
use_proton_scopes: bool = True,
) -> None:
device = "cuda"
for i in range(num_scopes):
with (
proton.scope(f"scope_{i}")
if use_proton_scopes
else contextlib.nullcontext()
):
x = torch.randn(workload_size, workload_size, device=device)
y = torch.randn(workload_size, workload_size, device=device)
z = torch.relu(x @ y)
if i % dot_every == 0:
triton_dot[(1,)](x, y, z, 64)
def run(
*,
mode: str,
num_iters: int = 100,
num_scopes: int = 5000,
workload_size: int = 1024,
advance_every: int = 10,
dot_every: int = 1,
) -> None:
enable_profiling = mode != "none"
enable_triton_hooks = mode == "profile_triton"
use_cupti_min = mode == "cupti_min"
use_proton_scopes = not use_cupti_min
# warmup (avoid capturing/profiling compilation)
fn(
num_scopes,
workload_size=workload_size,
use_proton_scopes=use_proton_scopes,
)
session = None
if enable_profiling:
if use_cupti_min:
session = cupti_min_profiler.start("test")
else:
start_mode = "periodic_flushing:format=hatchet_msgpack"
if enable_triton_hooks:
session = proton.start("test", mode=start_mode, hook="triton")
else:
session = proton.start("test", mode=start_mode)
stream = torch.cuda.Stream()
torch.cuda.set_stream(stream)
g = torch.cuda.CUDAGraph()
with torch.cuda.graph(g):
fn(
num_scopes,
workload_size=workload_size,
dot_every=dot_every,
use_proton_scopes=use_proton_scopes,
)
start_time = time.time()
for i in range(num_iters):
g.replay()
if (
enable_profiling
and not use_cupti_min
and i != 0
and i % advance_every == 0
):
proton.data.advance_phase(session)
torch.cuda.synchronize()
if enable_profiling:
if use_cupti_min:
cupti_min_profiler.finalize()
else:
proton.finalize()
end_time = time.time()
print(f"cpu time: {end_time - start_time:.4f}")
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"--mode",
choices=["none", "profile", "profile_triton", "cupti_min"],
default="none",
help=(
"1) none: no profile. "
"2) profile: periodic_flushing. "
"3) profile_triton: periodic_flushing + Triton hooks. "
"4) cupti_min: kernel-only CUPTI activity consumer."
),
)
parser.add_argument("--iters", type=int, default=100)
parser.add_argument("--scopes", type=int, default=5000)
parser.add_argument("--workload-size", "--ws", type=int, default=1024)
parser.add_argument("--advance-every", type=int, default=10)
parser.add_argument("--dot-every", type=int, default=1)
args = parser.parse_args()
run(
mode=args.mode,
num_iters=args.iters,
num_scopes=args.scopes,
workload_size=args.workload_size,
advance_every=args.advance_every,
dot_every=args.dot_every,
)
if __name__ == "__main__":
main()