Skip to content

Commit d3176fe

Browse files
committed
test(§TB1): lean gb10 B2 A/B probe (skips slow prod gold) — v1/§27/batched timing + batched-vs-v1 self-consistency
1 parent 8b24e38 commit d3176fe

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""LEAN gb10 A/B: B2 v1-threaded vs §27 single-tile GEMM vs batched-large-tile.
2+
3+
Skips the slow prod-shape autograd gold (the full parity gate is the separate
4+
probe). This isolates the B2 TIMING + the batched-vs-{v1,§27} grad self-consistency
5+
(batched vs v1 max|abs| on the GEMM-able outputs). prod cfg, bs1 or bs4.
6+
RULE #1: build/run errors propagate.
7+
"""
8+
import os
9+
import sys
10+
import time
11+
12+
import numpy as np
13+
import torch
14+
15+
import tilelang as tl
16+
from cppmega_mlx.nn._tilelang.mamba3_chunked_backward_core import (
17+
chunk_scan_combine_bwd_cuda_prim,
18+
chunk_scan_combine_bwd_cuda_prim_gemm,
19+
chunk_scan_combine_bwd_cuda_prim_gemm_batched,
20+
_b2_batched_heads_per_cta,
21+
)
22+
from cppmega_mlx.nn._tilelang.mamba3_chunked_scan_core import (
23+
_resolve_chunked_compile_target as _rct,
24+
)
25+
26+
DEV = "cuda"
27+
BS = 4 if "--bs4" in sys.argv else 1
28+
HPC_REQ = int(os.environ.get("CPPMEGA_PATH_C_B2_HEADS_PER_CTA", "2"))
29+
b, S, chunk, G, H, P, N = BS, 4096, 64, 8, 112, 64, 64
30+
nchunks = S // chunk
31+
HPC = _b2_batched_heads_per_cta(H, H // G, HPC_REQ)
32+
print(f"[B2-AB] dev={torch.cuda.get_device_name(0)} bs={b} S={S} c={chunk} G={G} "
33+
f"H={H} P={P} N={N} HPC={HPC}")
34+
35+
rng = np.random.RandomState(0)
36+
def f16(*sh):
37+
return torch.tensor((rng.randn(*sh) * 0.1).astype(np.float32), device=DEV, dtype=torch.float16).contiguous()
38+
def f32(*sh):
39+
return torch.tensor((rng.randn(*sh) * 0.1).astype(np.float32), device=DEV, dtype=torch.float32).contiguous()
40+
41+
dout = f16(b, S, H, P); cb = f16(b, nchunks, G, chunk, chunk); x = f16(b, S, H, P)
42+
z = f16(b, S, H, P); dt = f16(b, H, nchunks, chunk); dA = f16(b, H, nchunks, chunk)
43+
C = f16(b, S, G, N); Bm = f16(b, S, G, N); prev = f32(b, nchunks, H, P, N)
44+
D = f16(H); y = f16(b, S, H, P)
45+
INP = (dout, cb, x, z, dt, dA, C, Bm, prev, D, y)
46+
47+
def outs():
48+
return [torch.zeros(b, S, H, N, device=DEV, dtype=torch.float32),
49+
torch.zeros(b, S, H, P, device=DEV, dtype=torch.float32),
50+
torch.zeros(b, S, H, P, device=DEV, dtype=torch.float32),
51+
torch.zeros(b, nchunks, H, P, N, device=DEV, dtype=torch.float32),
52+
torch.zeros(b, S, H, P, N, device=DEV, dtype=torch.float32),
53+
torch.zeros(b, H, nchunks, chunk, device=DEV, dtype=torch.float32),
54+
torch.zeros(H, device=DEV, dtype=torch.float32)]
55+
56+
_tgt = _rct("cuda")
57+
_pc = {"tl.disable_tma_lower": True, "tl.disable_warp_specialized": True}
58+
OUT = [11, 12, 13, 14, 15, 16, 17]
59+
60+
def build(prim):
61+
return tl.compile(prim, out_idx=OUT, target=_tgt, pass_configs=_pc)
62+
63+
def run(k, o):
64+
for t in o:
65+
t.zero_()
66+
k(*INP, *o)
67+
68+
def timeit(k, n=20):
69+
o = outs()
70+
run(k, o); torch.cuda.synchronize()
71+
t0 = time.perf_counter()
72+
for _ in range(n):
73+
run(k, o)
74+
torch.cuda.synchronize()
75+
return (time.perf_counter() - t0) / n * 1e3, o
76+
77+
print("[B2-AB] building v1-threaded ..."); k1 = build(chunk_scan_combine_bwd_cuda_prim(b, S, chunk, G, H, P, N))
78+
print("[B2-AB] building §27 single-tile gemm ..."); kg = build(chunk_scan_combine_bwd_cuda_prim_gemm(b, S, chunk, G, H, P, N))
79+
print(f"[B2-AB] building batched HPC={HPC} ..."); kb = build(chunk_scan_combine_bwd_cuda_prim_gemm_batched(b, S, chunk, G, H, P, N, heads_per_cta=HPC))
80+
81+
t1, o1 = timeit(k1)
82+
tg, og = timeit(kg)
83+
tb, ob = timeit(kb)
84+
85+
names = ["dC", "dx", "dz", "dchunk", "dinp", "dA_y", "dD"]
86+
eq_b_v1 = {nm: float((a - c).abs().max().cpu()) for nm, a, c in zip(names, ob, o1)}
87+
worst = max(eq_b_v1.values())
88+
89+
print(f"\n[B2-AB] MEASURED v1_threaded={t1:.3f}ms §27_single_tile_gemm={tg:.3f}ms "
90+
f"batched={tb:.3f}ms HPC={HPC} bs={b}")
91+
print(f"[B2-AB] batched_vs_v1={t1/tb:.3f}x batched_vs_§27={tg/tb:.3f}x "
92+
f"§27_vs_v1={t1/tg:.3f}x")
93+
print(f"[B2-AB] verdict_vs_v1={'GO' if tb < t1 else 'NO-GO'} "
94+
f"verdict_vs_§27={'GO' if tb < tg else 'NO-GO'}")
95+
print(f"[B2-AB] batched-vs-v1 max|abs| worst={worst:.2e} "
96+
+ " ".join(f"{k}={v:.2e}" for k, v in eq_b_v1.items()))
97+
print("B2_AB_JSON " + str({
98+
"bs": b, "HPC": HPC, "v1_ms": round(t1, 4), "s27_gemm_ms": round(tg, 4),
99+
"batched_ms": round(tb, 4), "batched_vs_v1": round(t1/tb, 4),
100+
"batched_vs_s27": round(tg/tb, 4), "s27_vs_v1": round(t1/tg, 4),
101+
"worst_vs_v1": f"{worst:.2e}", "maxabs": {k: f"{v:.2e}" for k, v in eq_b_v1.items()},
102+
}))

0 commit comments

Comments
 (0)