diff --git a/pytorchsim/frontend/attention.py b/pytorchsim/frontend/attention.py index 3014779b9..f3d92d196 100644 --- a/pytorchsim/frontend/attention.py +++ b/pytorchsim/frontend/attention.py @@ -155,13 +155,12 @@ def _decode_tiles(GP, BKV, D, esize): return _prefill_tiles(GP, BKV, D, esize, causal=False) -def _fits(tiles, copies=1): - """Whether `copies` sets of `tiles` fit one lane's scratchpad budget, and each is a tensor - Triton builds.""" +def _fits(tiles): + """Whether `tiles` fit one lane's scratchpad budget, and each is a tensor Triton builds.""" from pytorchsim.codegen import tiling big = tiling.triton_max_numel() return (all(r * c <= big for r, c, _ in tiles) - and copies * _lane_bytes(tiles) <= int(config.CONFIG_MACHINE.spad_budget)) + and _lane_bytes(tiles) <= int(config.CONFIG_MACHINE.spad_budget)) def fused_plan(query, key, value, is_causal): @@ -169,8 +168,9 @@ def fused_plan(query, key, value, is_causal): SDPA's math. The one predicate: _fused_sdp_choice only says which op carries the call. prefill L == S. One program per (batch, query head); BQ = BKV is splash's block, min(S, - SPLASH_BLOCK), halved while the static query loop's S / BQ blocks of scratch, all - live at once, overrun the budget, down to one row per lane, below which it declines. + SPLASH_BLOCK), halved while one query block's scratch overruns the budget (the compiler + gives the static query loop's blocks one set), down to one row per lane, below which + it declines. decode L == 1, not causal (a causal L == 1 attends key 0 alone, SDPA's top-left mask). One program per (batch, KV head), its G query heads padded to GP rows; one KV step of the largest power of two that fits, since every further step adds its own DMAs. @@ -192,11 +192,9 @@ def fused_plan(query, key, value, is_causal): if L == S: floor = min(S, int(config.CONFIG_MACHINE.vector_lanes)) block = min(S, SPLASH_BLOCK) - while block > floor and not _fits(_prefill_tiles(block, block, D, esize, is_causal), - copies=S // block): + while block > floor and not _fits(_prefill_tiles(block, block, D, esize, is_causal)): block //= 2 - if block % rows or not _fits(_prefill_tiles(block, block, D, esize, is_causal), - copies=S // block): + if block % rows or not _fits(_prefill_tiles(block, block, D, esize, is_causal)): return None return {"kernel": "prefill", "BQ": block, "BKV": block} if L == 1 and not is_causal: diff --git a/tests/ops/attention/test_fused_sdpa.py b/tests/ops/attention/test_fused_sdpa.py index d0102b8c6..54420e925 100644 --- a/tests/ops/attention/test_fused_sdpa.py +++ b/tests/ops/attention/test_fused_sdpa.py @@ -14,10 +14,9 @@ eager a prefill-shaped call outside torch.compile runs math (no Triton launch outside a compiled graph) and says so in ROUTED; under inference_mode too, which skips the autograd key the op's npu kernel is registered at, eagerly and compiled - spad doubling a fused prefill's S, attention.fused_plan starts at splash's block, halves it - as the scratchpad bound binds, and declines only once one row per lane overruns the - bound (checked on the plan: a math run of that S on Spike takes far longer than this - file) + spad doubling a fused prefill's S keeps splash's block (one query block's scratch is live + at a time); doubling its D halves the block as the scratchpad bound binds, and + fused_plan declines only once one row per lane overruns the bound (checked on the plan) counts a compiled prefill called twice counts two launches, not its traces; under backend="eager" the call runs math, and only math is counted @@ -143,28 +142,29 @@ def run_eager(name): def run_spad(name): - """Double prefill_causal_gqa's S, on meta tensors, until fused_plan declines it: the block - starts at min(S, SPLASH_BLOCK), never grows as S does once the bound binds, and is one row per - lane when the scratchpad bound declines the next S.""" + """On meta tensors: doubling prefill_causal_gqa's S keeps splash's block, min(S, SPLASH_BLOCK), + since one query block's scratch is live at a time; doubling its D halves the block as the + scratchpad bound binds, until one row per lane overruns it and fused_plan declines.""" from pytorchsim import config from pytorchsim.frontend import attention - _, B, Hq, H, _, S, D, causal = CASES["prefill_causal_gqa"] - plans = [] - while S <= 1 << 20: - q, k, v = (torch.empty(B, h, S, D, dtype=torch.bfloat16, device="meta") - for h in (Hq, H, H)) - plans.append((S, attention.fused_plan(q, k, v, causal))) - if plans[-1][1] is None: - break - S *= 2 - declined = plans[-1][1] is None and len(plans) > 1 - fused = [(S, p) for S, p in plans[:-1]] - blocks = [p["BQ"] for _, p in fused] - ok = (declined and all(p["BQ"] == p["BKV"] for _, p in fused) - and blocks[0] == min(fused[0][0], attention.SPLASH_BLOCK) - and all(b <= min(S, attention.SPLASH_BLOCK) for (S, _), b in zip(fused, blocks)) + _, B, Hq, H, _, S0, D0, causal = CASES["prefill_causal_gqa"] + + def plan(S, D): + q, k, v = (torch.empty(B, h, S, D, dtype=torch.bfloat16, device="meta") for h in (Hq, H, H)) + return attention.fused_plan(q, k, v, causal) + + by_s = [(S, plan(S, D0)) for S in (S0 << i for i in range(13))] + by_d = [] + D = D0 + while not by_d or by_d[-1][1] is not None: + by_d.append((D, plan(attention.SPLASH_BLOCK, D))) + D *= 2 + blocks = [p["BQ"] for _, p in by_d[:-1]] + ok = (all(p is not None and p["BQ"] == p["BKV"] == min(S, attention.SPLASH_BLOCK) for S, p in by_s) + and len(by_d) > 1 and blocks[0] == attention.SPLASH_BLOCK + and all(a >= b for a, b in zip(blocks, blocks[1:])) and blocks[-1] == int(config.CONFIG_MACHINE.vector_lanes)) - print(f"{name}: S={plans[0][0]} {plans[0][1]}, S={plans[-1][0]} {plans[-1][1]} " + print(f"{name}: S={by_s[-1][0]} {by_s[-1][1]}, D={by_d[-2][0]} {by_d[-2][1]}, D={by_d[-1][0]} declined " f"{'PASS' if ok else 'FAIL'}", flush=True) return ok