Upload benchmark_throughput.py with huggingface_hub
Browse files- benchmark_throughput.py +65 -0
benchmark_throughput.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import argparse
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
import torch.nn.functional as F
|
| 8 |
+
|
| 9 |
+
from audio_dit import AudioDiT
|
| 10 |
+
|
| 11 |
+
def main():
|
| 12 |
+
ap = argparse.ArgumentParser()
|
| 13 |
+
ap.add_argument("--batch-sizes", type=int, nargs="+", default=[64, 128, 256])
|
| 14 |
+
ap.add_argument("--x-res", type=int, default=384)
|
| 15 |
+
ap.add_argument("--y-res", type=int, default=256)
|
| 16 |
+
ap.add_argument("--warmup", type=int, default=10)
|
| 17 |
+
ap.add_argument("--iters", type=int, default=40)
|
| 18 |
+
args = ap.parse_args()
|
| 19 |
+
dev = "cuda"
|
| 20 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
| 21 |
+
torch.backends.cudnn.allow_tf32 = True
|
| 22 |
+
|
| 23 |
+
model = AudioDiT(x_res=args.x_res, y_res=args.y_res).to(dev)
|
| 24 |
+
opt = torch.optim.AdamW(model.parameters(), lr=2e-4)
|
| 25 |
+
print(f"[bench] {model.num_params()/1e6:.2f}M params, grid {model.grid_h}x{model.grid_w} "
|
| 26 |
+
f"({model.grid_h*model.grid_w} tokens) at x_res={args.x_res} y_res={args.y_res}", flush=True)
|
| 27 |
+
|
| 28 |
+
for B in args.batch_sizes:
|
| 29 |
+
try:
|
| 30 |
+
torch.cuda.reset_peak_memory_stats()
|
| 31 |
+
x1 = torch.randn(B, 1, args.y_res, args.x_res, device=dev)
|
| 32 |
+
seq = torch.randn(B, 32, 768, device=dev)
|
| 33 |
+
pool = torch.randn(B, 512, device=dev)
|
| 34 |
+
|
| 35 |
+
def step():
|
| 36 |
+
x0 = torch.randn_like(x1)
|
| 37 |
+
t = torch.sigmoid(torch.randn(B, device=dev))
|
| 38 |
+
tb = t.view(-1, 1, 1, 1)
|
| 39 |
+
xt = (1 - tb) * x0 + tb * x1
|
| 40 |
+
target = x1 - x0
|
| 41 |
+
with torch.autocast("cuda", dtype=torch.bfloat16):
|
| 42 |
+
v = model(xt, t, seq, pool)
|
| 43 |
+
loss = F.mse_loss(v.float(), target)
|
| 44 |
+
opt.zero_grad(set_to_none=True)
|
| 45 |
+
loss.backward()
|
| 46 |
+
opt.step()
|
| 47 |
+
|
| 48 |
+
for _ in range(args.warmup):
|
| 49 |
+
step()
|
| 50 |
+
torch.cuda.synchronize()
|
| 51 |
+
t0 = time.time()
|
| 52 |
+
for _ in range(args.iters):
|
| 53 |
+
step()
|
| 54 |
+
torch.cuda.synchronize()
|
| 55 |
+
el = time.time() - t0
|
| 56 |
+
sps = args.iters / el
|
| 57 |
+
peak = torch.cuda.max_memory_allocated() / 2**30
|
| 58 |
+
print(f"[bench] batch={B:<5} {sps:6.2f} steps/s {sps*B:8.1f} samples/s "
|
| 59 |
+
f"peak {peak:5.1f} GiB 90k-step ETA {90000/sps/3600:5.2f} h", flush=True)
|
| 60 |
+
except torch.cuda.OutOfMemoryError:
|
| 61 |
+
print(f"[bench] batch={B:<5} OOM", flush=True)
|
| 62 |
+
torch.cuda.empty_cache()
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
main()
|