-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_ab_benchmark.py
More file actions
93 lines (68 loc) · 2.15 KB
/
Copy pathrun_ab_benchmark.py
File metadata and controls
93 lines (68 loc) · 2.15 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
import time
import torch
import torch.nn as nn
from renorm.layers import RenormTransformerLayer
# -----------------------------
# CONFIG
# -----------------------------
device = "cuda" if torch.cuda.is_available() else "cpu"
BATCH = 4
SEQ = 16
DIM = 512
STEPS = 50
# -----------------------------
# DATA
# -----------------------------
def make_input():
return torch.randn(BATCH, SEQ, DIM, device=device, requires_grad=True)
# -----------------------------
# MODEL A (BASELINE)
# -----------------------------
def build_baseline():
return nn.TransformerEncoderLayer(d_model=DIM, nhead=8, batch_first=True).to(device)
# -----------------------------
# MODEL B (RENORM)
# -----------------------------
def build_renorm():
return RenormTransformerLayer(dim=DIM, heads=8).to(device)
# -----------------------------
# BENCHMARK RUNNER
# -----------------------------
def benchmark(model, name):
model.train()
# warmup
for _ in range(5):
x = make_input()
y = model(x)
loss = y.mean()
loss.backward()
if device == "cuda":
torch.cuda.synchronize()
start = time.time()
mem_before = torch.cuda.memory_allocated() if device == "cuda" else 0
for _ in range(STEPS):
x = make_input()
y = model(x)
loss = y.mean()
loss.backward()
if hasattr(model, "zero_grad"):
model.zero_grad(set_to_none=True)
if device == "cuda":
torch.cuda.synchronize()
end = time.time()
mem_after = torch.cuda.memory_allocated() if device == "cuda" else 0
print("\n==============================")
print(f"MODEL: {name}")
print(f"Time total: {end - start:.4f}s")
print(f"Avg step: {(end - start) / STEPS * 1000:.3f} ms")
print(f"Memory delta: {(mem_after - mem_before) / 1e6:.2f} MB")
print("==============================\n")
# -----------------------------
# RUN A/B TEST
# -----------------------------
if __name__ == "__main__":
print(f"Running on device: {device}")
print("\n--- BASELINE ---")
benchmark(build_baseline(), "PyTorch Transformer")
print("\n--- RENORM ---")
benchmark(build_renorm(), "Renorm Transformer")