Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-dnn/blob/main/evidence/STACK7-GPU-20261026/gpu-bench/torch_arm.py # omega-dnn aien-dev/omega-dnn Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-dnn / evidence / STACK7-GPU-20261026 / gpu-bench / torch_arm.py` 112 lines ยท 6337 bytes Raw Blame History 1 # THROWAWAY measurement driver (lane OmegaDnn.GpuDnn, 2026-10-26), not product code. 2 # Baseline arm: PyTorch CUDA (+cuDNN where the op has a cuDNN path) on the same inputs the Omega driver wrote. 3 # Usage: python torch_arm.py INDIR OUTDIR [--warm 10] [--reps 50] 4 # Per case and backend: `warm` untimed calls, then `reps` timed calls; each timed call records 5 # dev_ns = torch.cuda.Event elapsed between records placed right before and after the op (device-resident inputs) 6 # wall_ns = perf_counter_ns around host->device copy of the per-call inputs, the op, device->host copy (synchronous) 7 # First output of each backend is saved as OUTDIR/..out.npy. One JSON line per (case, backend). 8 import json, sys, time 9 import numpy as np 10 import torch 11 import torch.nn.functional as F 12 from torch.nn.attention import sdpa_kernel, SDPBackend 13 14 indir, outdir = sys.argv[1], sys.argv[2] 15 warm = int(sys.argv[sys.argv.index("--warm") + 1]) if "--warm" in sys.argv else 10 16 reps = int(sys.argv[sys.argv.index("--reps") + 1]) if "--reps" in sys.argv else 50 17 dev = torch.device("cuda") 18 19 def f32(case, what, shape): 20 return torch.from_numpy(np.fromfile(f"{indir}/{case}.{what}.bin", dtype=np.float32).reshape(shape)) 21 22 def bf16_pool(case): 23 raw = np.fromfile(f"{indir}/{case}.pool.bin", dtype=np.uint16) 24 return torch.from_numpy(raw.astype(np.int32)).to(torch.int32).mul(65536).view(torch.float32) 25 26 def bench(case, backend, host_inputs, fn): 27 """host_inputs: CPU tensors copied per call for wall_ns; fn(dev_inputs) -> device output.""" 28 dins = [t.to(dev) for t in host_inputs] 29 try: 30 out = fn(dins); torch.cuda.synchronize() 31 except Exception as e: # backend refused this dtype/shape: record, do not substitute 32 print(json.dumps({"case": case, "backend": backend, "error": f"{type(e).__name__}: {str(e)[:300]}"}), flush=True) 33 return 34 np.save(f"{outdir}/{case}.{backend}.out.npy", out.float().cpu().numpy()) 35 for _ in range(warm): 36 fn(dins) 37 torch.cuda.synchronize() 38 s, e = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True) 39 dev_ns, wall_ns = [], [] 40 for _ in range(reps): 41 s.record(); fn(dins); e.record(); e.synchronize() 42 dev_ns.append(int(round(s.elapsed_time(e) * 1e6))) 43 for _ in range(reps): 44 t0 = time.perf_counter_ns() 45 d2 = [t.to(dev) for t in host_inputs] 46 o = fn(d2).cpu() 47 wall_ns.append(time.perf_counter_ns() - t0) 48 print(json.dumps({"case": case, "backend": backend, "warm": warm, "reps": reps, "dev_ns": dev_ns, "wall_ns": wall_ns}), flush=True) 49 50 def sdpa(backend, q, k, v, causal=False): 51 with sdpa_kernel([backend]): 52 return F.scaled_dot_product_attention(q, k, v, is_causal=causal, enable_gqa=True) 53 54 BACKENDS = [("cudnn", SDPBackend.CUDNN_ATTENTION), ("flash", SDPBackend.FLASH_ATTENTION), 55 ("efficient", SDPBackend.EFFICIENT_ATTENTION), ("math", SDPBackend.MATH)] 56 57 for rows, dim in ((1, 2048), (64, 2048)): 58 c = f"rmsnorm_{rows}x{dim}" 59 x, w = f32(c, "x", (rows, dim)), f32(c, "w", (dim,)) 60 wd = w.to(dev) 61 bench(c, "torch_rms_norm", [x], lambda d: F.rms_norm(d[0], (dim,), wd, 1e-5)) 62 63 c = "rope_32x128" 64 x, cs, sn = f32(c, "x", (32, 128)), f32(c, "cos", (64,)), f32(c, "sin", (64,)) 65 csd, snd = cs.to(dev), sn.to(dev) 66 def rope(d): 67 x0, x1 = d[0][:, :64], d[0][:, 64:] 68 return torch.cat((x0 * csd - x1 * snd, x0 * snd + x1 * csd), dim=1) 69 bench(c, "torch_ops", [x], rope) 70 71 for n in (5632, 131072): 72 c = f"swiglu_{n}" 73 g, u = f32(c, "gate", (n,)), f32(c, "up", (n,)) 74 bench(c, "torch_silu_mul", [g, u], lambda d: F.silu(d[0]) * d[1]) 75 76 for c, nq, nkv, hd, ctx in (("gqa_f32_32q8kv_hd128_ctx2048", 32, 8, 128, 2048), ("gqa_f32_32q4kv_hd64_ctx2048", 32, 4, 64, 2048)): 77 q = f32(c, "q", (1, nq, 1, hd)) 78 k = f32(c, "k", (ctx, nkv, hd)).permute(1, 0, 2).unsqueeze(0).contiguous() 79 v = f32(c, "v", (ctx, nkv, hd)).permute(1, 0, 2).unsqueeze(0).contiguous() 80 for name, be in BACKENDS: 81 bench(c, f"sdpa_{name}_f32", [q, k, v], lambda d, be=be: sdpa(be, d[0], d[1], d[2]).reshape(nq, hd)) 82 for name, be in BACKENDS[:2]: # reference-only: the cuDNN/flash paths need half precision 83 bench(c, f"sdpa_{name}_bf16_REFONLY", [q, k, v], lambda d, be=be: sdpa(be, d[0].bfloat16(), d[1].bfloat16(), d[2].bfloat16()).reshape(nq, hd)) 84 85 c, nq, nkv, hd, m = "gqa_rows_f32_32q8kv_hd128_m256", 32, 8, 128, 256 86 q = f32(c, "q", (m, nq, hd)).permute(1, 0, 2).unsqueeze(0).contiguous() 87 k = f32(c, "k", (m, nkv, hd)).permute(1, 0, 2).unsqueeze(0).contiguous() 88 v = f32(c, "v", (m, nkv, hd)).permute(1, 0, 2).unsqueeze(0).contiguous() 89 for name, be in BACKENDS: 90 bench(c, f"sdpa_{name}_f32", [q, k, v], lambda d, be=be: sdpa(be, d[0], d[1], d[2], True)[0].permute(1, 0, 2).reshape(m, nq * hd)) 91 for name, be in BACKENDS[:2]: 92 bench(c, f"sdpa_{name}_bf16_REFONLY", [q, k, v], lambda d, be=be: sdpa(be, d[0].bfloat16(), d[1].bfloat16(), d[2].bfloat16(), True)[0].permute(1, 0, 2).reshape(m, nq * hd)) 93 94 # paged bf16 KV: pool [blk][plane k/v][slot 16][kv 8][hd 128] (one layer), ids[i] = physical block of logical block i 95 c, nq, nkv, hd, ctx, bs = "paged_bf16_32q8kv_hd128_ctx2048_bs16", 32, 8, 128, 2048, 16 96 nblk = ctx // bs 97 pool = bf16_pool(c).reshape(nblk, 2, bs, nkv, hd).to(torch.bfloat16) # exact: values are bf16 bit patterns 98 ids = torch.from_numpy(np.fromfile(f"{indir}/{c}.ids.bin", dtype=np.uint32).astype(np.int64)) 99 q = f32(c, "q", (1, nq, 1, hd)) 100 poold, idsd = pool.to(dev), ids.to(dev) 101 def gather(): 102 g = poold.index_select(0, idsd) # [nblk][2][bs][nkv][hd] in logical order 103 kk = g[:, 0].reshape(ctx, nkv, hd).permute(1, 0, 2).unsqueeze(0) 104 vv = g[:, 1].reshape(ctx, nkv, hd).permute(1, 0, 2).unsqueeze(0) 105 return kk.contiguous(), vv.contiguous() 106 kc, vc = gather() 107 kcf, vcf = kc.float(), vc.float() 108 for name, be in BACKENDS: 109 # KV pre-gathered contiguous (gather not timed: favours the baseline); q rounded to bf16 for the bf16 kernels 110 bench(c, f"sdpa_{name}_bf16_pregathered", [q], lambda d, be=be: sdpa(be, d[0].bfloat16(), kc, vc).reshape(nq, hd)) 111 bench(c, f"gather+sdpa_{name}_bf16", [q], lambda d, be=be: sdpa(be, d[0].bfloat16(), *gather()).reshape(nq, hd)) 112 bench(c, "sdpa_efficient_f32q_f32kv_pregathered", [q], lambda d: sdpa(SDPBackend.EFFICIENT_ATTENTION, d[0], kcf, vcf).reshape(nq, hd))