|
| 1 | +"""Benchmark: SAGE3 HW kernels vs BF16 SDPA on [1, 40, 75600, 128].""" |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import sys |
| 6 | + |
| 7 | +import torch |
| 8 | +import torch.nn.functional as F |
| 9 | + |
| 10 | +# Resolve paths relative to this script to avoid hardcoded user-specific paths |
| 11 | +REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) |
| 12 | +sys.path.insert(0, os.path.join(REPO_ROOT, "src")) |
| 13 | +# Bypass top-level vllm dependency by importing the sage3 subpackage directly |
| 14 | +sys.path.insert(0, os.path.join(REPO_ROOT, "src", "vllm_qdq_plugin", "sage3_attn")) |
| 15 | +from sage3.api import sageattn3_standalone |
| 16 | + |
| 17 | +# ── Config ── |
| 18 | +B, H, N, D = 1, 40, 75600, 128 |
| 19 | +WARMUP = 3 |
| 20 | +ITERS = 10 |
| 21 | +DEVICE = "cuda" |
| 22 | +DTYPE = torch.bfloat16 |
| 23 | + |
| 24 | + |
| 25 | +def bench(fn, warmup=WARMUP, iters=ITERS): |
| 26 | + """Returns mean latency in ms.""" |
| 27 | + for _ in range(warmup): |
| 28 | + fn() |
| 29 | + torch.cuda.synchronize() |
| 30 | + start = torch.cuda.Event(enable_timing=True) |
| 31 | + end = torch.cuda.Event(enable_timing=True) |
| 32 | + start.record() |
| 33 | + for _ in range(iters): |
| 34 | + fn() |
| 35 | + end.record() |
| 36 | + torch.cuda.synchronize() |
| 37 | + return start.elapsed_time(end) / iters |
| 38 | + |
| 39 | + |
| 40 | +def compute_attn_flops(B, H, N, D): |
| 41 | + """FLOPs for standard attention: 2*B*H*N*N*D (QK) + 2*B*H*N*N*D (PV) = 4*B*H*N^2*D.""" |
| 42 | + return 4 * B * H * N * N * D |
| 43 | + |
| 44 | + |
| 45 | +def main(): |
| 46 | + print(f"Shape: [{B}, {H}, {N}, {D}] | dtype: {DTYPE} | warmup: {WARMUP} | iters: {ITERS}") |
| 47 | + print(f"Device: {torch.cuda.get_device_name()}") |
| 48 | + print("-" * 70) |
| 49 | + total_flops = compute_attn_flops(B, H, N, D) |
| 50 | + |
| 51 | + q = torch.randn(B, H, N, D, device=DEVICE, dtype=DTYPE) |
| 52 | + k = torch.randn(B, H, N, D, device=DEVICE, dtype=DTYPE) |
| 53 | + v = torch.randn(B, H, N, D, device=DEVICE, dtype=DTYPE) |
| 54 | + |
| 55 | + configs = [ |
| 56 | + ("bf16_sdpa", lambda: F.scaled_dot_product_attention(q, k, v)), |
| 57 | + ("mxfp8_hw", lambda: sageattn3_standalone(q, k, v, config="mxfp8_hw")), |
| 58 | + ("mxfp4_hw", lambda: sageattn3_standalone(q, k, v, config="mxfp4_hw")), |
| 59 | + ("mixed_mxfp8qk_mxfp4pv_hw", lambda: sageattn3_standalone(q, k, v, config="mixed_mxfp8qk_mxfp4pv_hw")), |
| 60 | + ("mixed_mxfp4qk_mxfp8pv_hw", lambda: sageattn3_standalone(q, k, v, config="mixed_mxfp4qk_mxfp8pv_hw")), |
| 61 | + ] |
| 62 | + |
| 63 | + results = {} |
| 64 | + baseline_ms = None |
| 65 | + |
| 66 | + for name, fn in configs: |
| 67 | + try: |
| 68 | + # Verify it runs |
| 69 | + out = fn() |
| 70 | + assert out.shape == (B, H, N, D), f"Bad shape: {out.shape}" |
| 71 | + del out |
| 72 | + torch.cuda.empty_cache() |
| 73 | + |
| 74 | + ms = bench(fn) |
| 75 | + results[name] = {"ms": ms, "status": "ok"} |
| 76 | + if baseline_ms is None: |
| 77 | + baseline_ms = ms |
| 78 | + except Exception as e: |
| 79 | + results[name] = {"ms": None, "status": f"FAIL: {e}"} |
| 80 | + print(f" {name}: FAILED - {e}") |
| 81 | + |
| 82 | + # Print table |
| 83 | + print(f"{'Config':<30} {'Latency (ms)':>12} {'TFLOPS':>8} {'Speedup':>8}") |
| 84 | + print("-" * 62) |
| 85 | + for name, r in results.items(): |
| 86 | + if r["ms"] is not None: |
| 87 | + speedup = baseline_ms / r["ms"] if baseline_ms else 0 |
| 88 | + tflops = total_flops / (r["ms"] * 1e-3) / 1e12 |
| 89 | + print(f"{name:<30} {r['ms']:>12.2f} {tflops:>7.1f} {speedup:>7.2f}x") |
| 90 | + r["speedup"] = round(speedup, 3) |
| 91 | + r["tflops"] = round(tflops, 2) |
| 92 | + else: |
| 93 | + print(f"{name:<30} {'FAIL':>12} {'N/A':>8}") |
| 94 | + |
| 95 | + # Save raw results |
| 96 | + out_path = os.path.join(REPO_ROOT, "bench_hw_vs_sdpa_results.json") |
| 97 | + meta = { |
| 98 | + "shape": [B, H, N, D], |
| 99 | + "dtype": str(DTYPE), |
| 100 | + "warmup": WARMUP, |
| 101 | + "iters": ITERS, |
| 102 | + "device": torch.cuda.get_device_name(), |
| 103 | + "total_flops": total_flops, |
| 104 | + } |
| 105 | + with open(out_path, "w") as f: |
| 106 | + json.dump({"meta": meta, "results": results}, f, indent=2) |
| 107 | + print(f"\nRaw results saved to: {out_path}") |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + main() |
0 commit comments