The cluster smoke at 9170d24fe showed ~88 s/epoch projected for the
full 8000-step epoch (vs the 17 s baseline) — a 5× regression that
fails the spec §5 wall-time gate. Diagnosis: the inverted-attention
kernel's hot loops recomputed `mean_k_X_inv[j] = (1/K) Σ_k X_inv[j, k]`
per-thread, per-j, every pass:
- fwd pool: 128 × 32 reads per thread = 4096 extra ops × 128 threads
= ~0.5 M wasted ops per fwd
- bwd phase 1: 128 × 32 × 2 passes per thread = ~1.0 M wasted ops
per bwd
At 1000 steps/epoch this alone adds ~1.5 s of pointless compute, and
the cumulative effect across fwd + bwd + DRAM round-trips for the
score tensor was the main contributor to the 5× regression.
REWRITE:
1. `mean_k_X_inv[H]` (0.5 KB) cached ONCE in shared memory at kernel
entry. Each thread h does its OWN k-trajectory load + sum in
parallel during the x_inv staging, so no extra cost vs the prior
x_inv-only stage.
2. Forward now does:
- Pass 1: compute max(score) only — no DRAM writes.
- Pass 2: compute exp(score - max) → write to attn_out (scratch),
accumulate sum locally.
- Pass 3: single sweep over j — divide attn_out by sum (in place),
accumulate pool += attn · mean_k[j]. ← uses cached mean_k.
Eliminates the post-softmax recompute of mean_k that the prior
version did 128× per thread.
3. Backward `d_scores` computation now uses cached mean_k (saves 4096
ops/thread). Also: `dot = pooled[my_h]` is now computed once at
the start of phase 1 from attn × mean_k (one pass over j) instead
of being implicit in the per-j d_attn computation.
CORRECTNESS:
- inverted_attention_pool numgrad PASSES 6 random-position checks
within 5e-2 rel / 5e-3 abs.
- perception_overfit 9/9 tests PASS — including
stacked_trainer_loss_shrinks_on_constant_signal (loss 0.67 → -0.99
over 250 steps).
SMEM FOOTPRINT:
- fwd: x_inv[H · K] + mean_k[H] = 16 KB + 0.5 KB
- bwd: x_inv[H · K] + mean_k[H] + dp[H] = 16 KB + 1 KB
Both well under the 48 KB sm_86 dynamic-shared default; no
cuFuncSetAttribute opt-in needed.
Next: re-run cluster smoke to measure the new wall-time. Expected to
land ≤ 30 s/epoch per spec §5 wall-time gate.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>