d282144339
Land the new GQA fused-attention kernels (ADR-0060) for prefill/decode
across long and short context, the TL discipline primitives they depend
on (ADR-0062 lazy load, ADR-0063 scratch_scope + copy_to), and the
per-op-type CPU issue cost model (ADR-0064). Remove the pre-ADR-0060
mesh-attention baseline now that the unified kernels supersede it.
ADR-0060 (long context)
- _gqa_decode.py: M-fold + 2-level chain reduce-to-root (Level-2
intra-CUBE row-then-col + Level-1 inter-CUBE) — root-only output.
- _gqa_prefill.py: head-parallel + Ring KV rotation around C CUBEs,
online-softmax merge per ring step, per-CUBE distributed output.
- Each merge stage wraps in scratch_scope() and persists running
(m, l, O) via copy_to() to lift the 1 MiB scratch ceiling.
ADR-0060 §B.split.2 (short context, kv_per_cube in {1,2,4,8})
- _gqa_decode_short.py / _gqa_prefill_short.py: no cube-SP; each CUBE
owns whole KV heads; PE-parallel heads with intra-group chain
reduce. Prefill has no Ring KV (each head fully resident).
ADR-0062 (lazy tl.load): future-bearing TensorHandle, auto-wait at
first consuming op (dot/MATH/store/send/copy_to/composite).
ADR-0063 (tl.scratch_scope + tl.copy_to): scoped per-tile arena with
copy_to writeback primitive for persistent running state.
ADR-0064 (CPU issue cost model)
- common/cpu_issue_cost.py: per-op-type table (composite=40 ns,
primitives=5 ns); ratios are load-bearing per D1.
- TLContext: issue_cost_table param; _emit_dispatch_overhead(kind)
consults table with dispatch_cycles fallback (ADR-0046 §D6
back-compat).
- Live PE_CPU paths (greenlet + legacy) construct TLContext with
DEFAULT_CPU_ISSUE_COST so saturation lever (ADR-0060 §1) is
measurable end-to-end.
P7 headline bench: milestone-gqa-headline writes per-panel
op_log_summary to 1H_milestone_output/gqa_headline/sweep.json. No
figure renderers yet (deferred).
Removals (pre-ADR-0060 baseline now superseded):
- benches: _attention_mesh_kv.py, _attention_mesh_mlo.py,
_attention_mesh_mlo_2d.py, milestone_gqa_llama70b.py
- tests: test_attention_*, test_mesh_*, test_milestone_gqa_llama70b
- topology: llama70b_4sip.yaml (only consumer was the deleted diag)
- artifacts: 1H_milestone_output/gqa/ (sweep.json + 5 PNGs)
- tests/gqa/ plot helper + test (broken on Windows Tcl/Tkinter)
- ADR-0060/0061 references to deleted file paths cleaned up
(EN + KO kept in sync).
Tests: 124/124 focused regression green (attention + Phase E + TL
discipline + triton_emu + pe_components). Full regression: 764 pass,
2 pre-existing test_bench_registry failures (stale EXPECTED_NAMES
across multiple benches, not introduced here).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
119 lines
4.3 KiB
Python
119 lines
4.3 KiB
Python
"""Phase 1 spec test for P6a GQA prefill kernel (head-parallel, C=1 baseline).
|
|
|
|
P6a introduces ``_gqa_prefill.py`` with the head-parallel structure (one
|
|
Q head per CUBE, per-CUBE distributed output, no reduce). C=1 is the
|
|
degenerate case — no Ring KV, no IPCQ traffic. Validates kernel
|
|
structure and T_q > 1 attention.
|
|
|
|
P6b (deferred) adds the Ring KV rotation for C > 1, which needs either
|
|
a new SFR install function (intra_* + wrapped E/W at CUBE level) or a
|
|
topology-specific config — separate design call.
|
|
|
|
The prefill kernel differs from decode (P1a/P2a/P2b) in three ways
|
|
(ADR-0060 §5.5 / TL;DR):
|
|
1. Q has T_q > 1 rows (not just decode's single timestep).
|
|
2. Head-parallel placement: each CUBE owns ONE Q head — no M-fold.
|
|
3. Each CUBE writes its own head's output — NO reduce.
|
|
|
|
Phase 1 (this commit): tests only — production code lands in Phase 2.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from kernbench.benches._gqa_prefill import gqa_prefill_kernel # noqa: F401
|
|
from kernbench.policy.placement.dp import DPPolicy
|
|
from kernbench.runtime_api.bench_runner import run_bench
|
|
from kernbench.runtime_api.types import resolve_device
|
|
from kernbench.sim_engine.engine import GraphEngine
|
|
from kernbench.topology.builder import resolve_topology
|
|
|
|
TOPOLOGY_DEFAULT = Path(__file__).resolve().parents[2] / "topology.yaml"
|
|
|
|
D_HEAD = 64
|
|
DTYPE = "f16"
|
|
|
|
|
|
def _engine_factory(t, d):
|
|
return GraphEngine(getattr(t, "topology_obj", t), enable_data=True)
|
|
|
|
|
|
def _run_prefill(*, T_q: int, S_kv: int, C: int = 1):
|
|
"""C=1 head-parallel prefill: single CUBE owns the one head + full KV."""
|
|
topo = resolve_topology(str(TOPOLOGY_DEFAULT))
|
|
|
|
def _bench_fn(ctx):
|
|
dp = DPPolicy(cube="replicate", pe="replicate",
|
|
num_cubes=C, num_pes=1)
|
|
# Q: (T_q, d_head) — one head per CUBE (head-parallel; for C=1
|
|
# only one head total). 2D layout matches what the kernel loads.
|
|
# P6b will use a 3D (h_q, T_q, d_head) Q with cube_row_wise
|
|
# sharding so each CUBE owns its head.
|
|
q = ctx.zeros((T_q, D_HEAD), dtype=DTYPE, dp=dp,
|
|
name=f"q_t{T_q}_c{C}")
|
|
# K, V: full local for C=1 (no ring). Kernel loads K as
|
|
# (d_head, S_kv) via byte-conserving reshape.
|
|
k = ctx.zeros((S_kv, D_HEAD), dtype=DTYPE, dp=dp,
|
|
name=f"k_t{T_q}_c{C}")
|
|
v = ctx.zeros((S_kv, D_HEAD), dtype=DTYPE, dp=dp,
|
|
name=f"v_t{T_q}_c{C}")
|
|
# O: (T_q, d_head) — per-CUBE distributed output.
|
|
o = ctx.empty((T_q, D_HEAD), dtype=DTYPE, dp=dp,
|
|
name=f"o_t{T_q}_c{C}")
|
|
ctx.launch(
|
|
f"gqa_prefill_p6a_t{T_q}_s{S_kv}_c{C}",
|
|
gqa_prefill_kernel,
|
|
q, k, v, o,
|
|
T_q, S_kv, D_HEAD, C,
|
|
_auto_dim_remap=False,
|
|
)
|
|
|
|
return run_bench(
|
|
topology=topo, bench_fn=_bench_fn,
|
|
device=resolve_device(None),
|
|
engine_factory=_engine_factory,
|
|
)
|
|
|
|
|
|
def _count(op_log, name: str) -> int:
|
|
return sum(1 for r in op_log if r.op_name == name)
|
|
|
|
|
|
def test_prefill_c_one_t_q_one_completes():
|
|
"""C=1, T_q=1: smallest workload (decode-like)."""
|
|
result = _run_prefill(T_q=1, S_kv=16, C=1)
|
|
assert result.completion.ok, (
|
|
f"prefill C=1 T_q=1 failed: {result.completion}"
|
|
)
|
|
|
|
|
|
def test_prefill_c_one_t_q_four_completes():
|
|
"""C=1, T_q=4: real prefill (Q has multiple rows) — distinguishes
|
|
prefill from decode (T_q=1)."""
|
|
result = _run_prefill(T_q=4, S_kv=16, C=1)
|
|
assert result.completion.ok, (
|
|
f"prefill C=1 T_q=4 failed: {result.completion}"
|
|
)
|
|
|
|
|
|
def test_prefill_c_one_no_ipcq_traffic():
|
|
"""C=1: no ring step, no IPCQ traffic."""
|
|
result = _run_prefill(T_q=4, S_kv=16, C=1)
|
|
assert result.completion.ok
|
|
n_copy = _count(result.engine.op_log, "ipcq_copy")
|
|
assert n_copy == 0, (
|
|
f"C=1 must have no IPCQ traffic (no ring); got {n_copy}"
|
|
)
|
|
|
|
|
|
def test_prefill_c_one_one_dma_write():
|
|
"""C=1, one head: exactly one dma_write (per-CUBE distributed output;
|
|
no reduce). For C > 1 in P6b this becomes dma_write_count == C."""
|
|
result = _run_prefill(T_q=4, S_kv=16, C=1)
|
|
assert result.completion.ok
|
|
n_writes = _count(result.engine.op_log, "dma_write")
|
|
assert n_writes == 1, (
|
|
f"C=1 prefill: expected 1 dma_write (one head per cube); "
|
|
f"got {n_writes}"
|
|
)
|