Create 03_simple_mlp.py
Browse files- 03_simple_mlp.py +81 -0
03_simple_mlp.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import os
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
from torch.nn import functional as F
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class SimpleGeGLUMLP(nn.Module):
|
| 9 |
+
def __init__(self, dim, hidden):
|
| 10 |
+
super().__init__()
|
| 11 |
+
self.gate_proj = nn.Linear(dim, hidden, bias=False)
|
| 12 |
+
self.up_proj = nn.Linear(dim, hidden, bias=False)
|
| 13 |
+
self.down_proj = nn.Linear(hidden, dim, bias=False)
|
| 14 |
+
|
| 15 |
+
def forward(self, x):
|
| 16 |
+
g = self.gate_proj(x)
|
| 17 |
+
u = self.up_proj(x)
|
| 18 |
+
h = F.gelu(g, approximate="tanh")
|
| 19 |
+
m = h * u
|
| 20 |
+
y = self.down_proj(m)
|
| 21 |
+
return y
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def main():
|
| 25 |
+
p = argparse.ArgumentParser()
|
| 26 |
+
p.add_argument("--batch", type=int, default=64)
|
| 27 |
+
p.add_argument("--seq", type=int, default=128)
|
| 28 |
+
p.add_argument("--dim", type=int, default=768)
|
| 29 |
+
p.add_argument("--hidden", type=int, default=3072)
|
| 30 |
+
p.add_argument("--compile", action="store_true")
|
| 31 |
+
p.add_argument("--trace_dir", default="./traces/03_simple_mlp")
|
| 32 |
+
args = p.parse_args()
|
| 33 |
+
|
| 34 |
+
device = "cuda"
|
| 35 |
+
x = torch.randn(args.batch, args.seq, args.dim, device=device, dtype=torch.bfloat16)
|
| 36 |
+
|
| 37 |
+
mlp = SimpleGeGLUMLP(args.dim, args.hidden).to(device, dtype=torch.bfloat16)
|
| 38 |
+
mlp.eval()
|
| 39 |
+
|
| 40 |
+
fwd = torch.compile(mlp) if args.compile else mlp
|
| 41 |
+
|
| 42 |
+
def step():
|
| 43 |
+
with torch.profiler.record_function("mlp_fwd"), torch.no_grad():
|
| 44 |
+
return fwd(x)
|
| 45 |
+
|
| 46 |
+
for _ in range(3):
|
| 47 |
+
step()
|
| 48 |
+
torch.cuda.synchronize()
|
| 49 |
+
|
| 50 |
+
os.makedirs(args.trace_dir, exist_ok=True)
|
| 51 |
+
compile_tag = "compile" if args.compile else "eager"
|
| 52 |
+
tag = f"{args.batch}_{args.seq}_{args.dim}_{args.hidden}_{compile_tag}"
|
| 53 |
+
|
| 54 |
+
table_path = os.path.join(args.trace_dir, f"{tag}.txt")
|
| 55 |
+
trace_path = os.path.join(args.trace_dir, f"{tag}.json")
|
| 56 |
+
|
| 57 |
+
schedule = torch.profiler.schedule(wait=1, warmup=1, active=3, repeat=1)
|
| 58 |
+
with torch.profiler.profile(
|
| 59 |
+
activities=[
|
| 60 |
+
torch.profiler.ProfilerActivity.CPU,
|
| 61 |
+
torch.profiler.ProfilerActivity.CUDA,
|
| 62 |
+
],
|
| 63 |
+
schedule=schedule,
|
| 64 |
+
record_shapes=False, # adds CPU overhead
|
| 65 |
+
profile_memory=False, # adds CPU overhead
|
| 66 |
+
with_stack=False,
|
| 67 |
+
) as prof:
|
| 68 |
+
for _ in range(5):
|
| 69 |
+
step()
|
| 70 |
+
prof.step()
|
| 71 |
+
torch.cuda.synchronize()
|
| 72 |
+
|
| 73 |
+
print(f"saving traces ... {trace_path}")
|
| 74 |
+
prof.export_chrome_trace(trace_path)
|
| 75 |
+
|
| 76 |
+
with open(table_path, "w") as f:
|
| 77 |
+
f.write(prof.key_averages().table(sort_by="cuda_time_total", row_limit=15))
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
main()
|