Files
foxhunt/docs
jgrusewski e09757419c fix(tlob): align backward dW_Q/K/V layout with forward W_Q/K/V (Fix 20)
Forward W_Q SGEMM stored col-major [K, M] (lda=K) while backward dW_Q
wrote col-major [M, K] (ldc=M). When M ≠ K (TLOB: M=16, K=32), Adam's
element-wise update applied gradients computed at position (m, k) to
weights stored at position (k, m) — silent learning corruption at
every flat index ≠ 0 (511 of 512 W_Q slots updated using wrong-position
gradients, matched in W_K/V; W_O is square so unaffected).

Standardised backward dW_Q/K/V SGEMM output to col-major [K, M] (ldc=K)
matching the forward layout (Strategy A from the audit brainstorm — the
forward layout is the definitive weight storage; Adam's flat layout
follows forward's allocation). The fix flips the cuBLAS strided-batched
operands: backward now computes `dW^T = ofi @ d_proj^T` instead of
`dW = d_proj @ ofi^T`. Same gradient values, just re-laid-out so flat
indexing matches `params`. No new kernel; no kernel-internal layout
change (the SDP forward/backward kernels still read `proj_qkv_buf` /
`d_proj_qkv_buf` as [M, B] col-major — those buffers are untouched).
The QKV-fusion `cublasSgemmStridedBatched(batch=3)` semantics are
preserved: ofi is the new shared operand (strideA=0), d_proj is the
per-batch operand (strideB=M·B), strideC=M·K=512 unchanged.

Phase-1 reproduction (`tlob_dw_layout_alignment_repro`,
#[ignore = "requires GPU"]) ran the broken and fixed cuBLAS dispatches
side-by-side on identical sentinel inputs (`d_proj[m=0,b=0]=1`,
`ofi[k=1,b=0]=1`, all else 0); broken `[M, K]` placed the `1.0`
gradient at flat 16, fixed `[K, M]` placed it at flat 1 — O(1)
cross-layout delta exactly matching the audit prediction. Pre-fix Adam
would have updated `W_Q[m=0, k=16]` (the forward layout's flat-16 slot)
using the gradient computed for `W_Q[m=0, k=1]` — the silent
corruption.

Phase-3 regression (`tlob_dw_layout_alignment_regression_full_chain`,
#[ignore = "requires GPU"]) exercises the full forward → backward →
Adam → forward chain with random Xavier-init weights (W_O seeded to
break the production-zero-init that would collapse the gradient chain
to all-zero in a synthetic test). Asserts (1) GPU dW_Q matches a CPU
reference computed in the post-fix [K, M] layout within TF32 tolerance,
and (2) the second forward Q matches the analytical [K, M]
interpretation of the post-Adam W_Q — locks in cross-step layout
agreement and would fail if any future refactor accidentally
re-permutes `params` between Adam and the next forward.

Existing inline `tlob_sgemm_parity_with_cpu_reference` still passes
(its CPU dW_Q/K/V reference was updated in lockstep to the [K, M]
layout per `feedback_no_partial_refactor`; pre-fix the GPU produced
[M, K] and the new CPU reference would diverge element-wise — a clean
no-skip parity check that locks the layout convention end-to-end).
`tlob_qkv_fusion_equivalence` unchanged (the fix only touches the
backward call, forward QKV fusion is bit-identical pre/post).

Local verification (RTX 3050 Ti, batch=256 for fusion test):
  tlob_dw_layout_alignment_repro:                      PASS
  tlob_dw_layout_alignment_regression_full_chain:      PASS
  tlob_qkv_fusion_equivalence:    PASS (3.79× speedup retained)
  tlob_sgemm_parity_with_cpu_reference:                PASS

Fix 20 in docs/dqn-gpu-hot-path-audit.md updated FIXED with verdict
+ strategy + test list. Forward SGEMM call site got an inline comment
block documenting the [K, M] convention and pointing at the
`tlob_dw_layout_alignment_*` regression coverage.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 12:15:34 +02:00
..