-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtorch-compile-bench.py
More file actions
71 lines (64 loc) · 2.29 KB
/
Copy pathtorch-compile-bench.py
File metadata and controls
71 lines (64 loc) · 2.29 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
import time
import torch
from transformers import AutoModelForCausalLM
MODEL = "meta-llama/Llama-3.1-8B"
SEQ = 8192
# MODEL = "NousResearch/Llama-3.2-1B"
# SEQ = 512
BATCH = 1
WARMUP = 3
ITERS = 10
SEED = 42
DTYPE = torch.bfloat16
MODES = {
"eager": None,
"default": {},
"reduce-overhead": {"mode": "reduce-overhead"},
"max-autotune": {"mode": "max-autotune"},
}
WORKLOADS = ("forward", "forward+backward", "forward+backward+optim")
def main():
torch.set_float32_matmul_precision("high")
torch.manual_seed(SEED)
device = torch.device("cuda")
tokens = torch.randint(0, 30000, (BATCH, SEQ), device=device)
for workload in WORKLOADS:
is_training = workload != "forward"
is_optim = workload == "forward+backward+optim"
eager = None
for name, kwargs in MODES.items():
model = AutoModelForCausalLM.from_pretrained(MODEL, dtype=DTYPE, attn_implementation="sdpa").to(device)
model.train(is_training)
if is_training:
model.config.use_cache = False
model.gradient_checkpointing_enable()
opt = torch.optim.AdamW(model.parameters(), lr=1e-4) if is_optim else None
run = model if kwargs is None else torch.compile(model, **kwargs)
def step():
if not is_training:
with torch.no_grad():
run(input_ids=tokens)
return
run.zero_grad(set_to_none=True)
run(input_ids=tokens, labels=tokens).loss.backward()
if is_optim:
opt.step()
torch.cuda.synchronize()
t = time.perf_counter()
step()
torch.cuda.synchronize()
cold = time.perf_counter() - t
for _ in range(WARMUP):
step()
torch.cuda.synchronize()
t = time.perf_counter()
for _ in range(ITERS):
step()
torch.cuda.synchronize()
steady = (time.perf_counter() - t) / ITERS
if name == "eager":
eager = steady
print(f"{workload:22} {name:16} steady={steady * 1000:8.2f}ms cold={cold:7.2f}s speedup={eager / steady:.2f}x")
del model, run, opt
if __name__ == "__main__":
main()