Repository navigation
cuda: the backward of the gated delta rule (GATED_DELTA_NET_BACK), the CUDA twin of #8's reference — 19739/19739 by finite differences, mutation fails - #10
Conversation
|
APPROVED at What reads right:
Two notes, not blocking:
Co-Authored-By: Claude Opus 5.5 [email protected] |
…he CUDA twin of the CPU reference
One block per (sequence, head), S_v threads, thread j owns row j of M / M' / dM in the
transposed state layout of the reference; row-local sums stay in the thread, column sums
(dq_i, dk_i, KDA dg_i) go through shared accumulators at diagonal offsets, dq/dk are
accumulated with atomics because broadcast heads share them, the forward states are
recomputed and stored in a pool buffer ((T + 1) x S_v^2 per block), M' and dM live in
dynamic shared memory. First cut: S_v in {16, 32, 64, 128}. Checked against the CPU
reference by the harness's finite-difference gradient test (grad -o GATED_DELTA_NET).
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Q4NU4VNiELPQfBpCacDZGc
…zes the GPU forwards dispatch The existing cases use head sizes 4 and 8, which the CUDA forward does not run, so a GPU GATED_DELTA_NET_BACK was never exercised by them. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01Q4NU4VNiELPQfBpCacDZGc
83bae32 to
9d9f1cf
Compare
|
APPROVED at |
bf4c3ed
into
feat/props-weight-residency
… hybrid (delta-net) training graph meets After the delta rule's backward (#8, #10), a LoRA on Qwen3.5/3.8-class hybrids still aborted at graph build on the ops around it: each delta-net layer runs a short causal conv over q/k/v (SSM_CONV), whose input is the cached conv state CONCATenated with this ubatch's TRANSPOSED qkv, and L2-normalises q and k (L2_NORM). All three rules compose from ops every backend already has, so no backend needs a new kernel. - CONCAT: each input takes its slice of the gradient along the concat dimension. - SSM_CONV: x[d,t,s] = sum_k sx[t+k,d,s] c[k,d], so dc[k] = sum_{t,s} dy^T (.) (sx shifted by k) and dsx = sum_k pad_ext(dy^T (.) c[k], left k, right d_conv-1-k). Built from transpose/view/MUL/SUM_ROWS/CONCAT/PAD_EXT/ADD. - L2_NORM: y = x / max(|x|, eps): dx = (dy - y (y . dy)) / |x| where |x| > eps, and dy/eps where clamped. The projection is selected per row by step(|x| - eps). Tests (test-backend-ops grad, CPU): - CONCAT: new test_concat_grad, dims 0 and 1 plus a transposed second input (the conv-state shape). Passes at the default 1e-4. - SSM_CONV: the existing cases now mark a/b as params. Passes at 1e-4; swapping the pad sides fails at MAA 4.7-12. - L2_NORM: new test_l2_norm_grad with one row under eps (the clamped branch). The clamped row matches 1/eps to 3e-5. At eps 1e-4 the unclamped rows carried ~7e-3 of float rounding, which fell tenfold at eps 1e-3 (rounding, not bias), so eps 1e-3 with the harness's usual 1e-3 bound. 3/3 runs pass; dropping the projection fails at MAA 0.22. - 19740/19740 green on CPU. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
…ead size 128 (M'/dM out of shared memory) Two changes to the CUDA twin (bf4c3ed): 1. HEAD SIZE 128 NEVER LAUNCHED. M' and dM lived in dynamic shared memory, 2 x S_v^2 floats = 128 KB at S_v = 128, above the ~99 KB per-block limit: supports_op claimed 128, cudaFuncSetAttribute rejected it (invalid argument) and CUDA_CHECK aborted the process. The gradient cases (head sizes 4-32) never reached it; the 27B's delta-net heads are 128. Each row of M' and dM is touched only by its own thread, so both now live in the block's global scratch (row-private, L1/L2-served) and no dynamic shared memory is used at all. 2. CHECKPOINTED RECOMPUTE (card f4fc77c2, Cormac on #10): storing every forward state cost (T + 1) x S_v^2 floats per (sequence, head), GBs per call on the 27B. The forward keeps a two-slot working state and checkpoints every C ~ sqrt(T) tokens; the backward walks the segments last to first, re-running each from its checkpoint into a (C + 1)-state scratch. ceil(T/C) + C + 3 states per block instead of T + 1 (68 vs 1025 at T 1024). Oracle: a new test case, GATED_DELTA_NET_BACK run directly on identical random inputs on every backend (CUDA compared output-for-output against the CPU reference, no finite differences): 5/5 at head sizes 16/32/64/128, sequences up to 64 tokens, KDA on and off, K 1-3, and at segment lengths default, 1, 3 and T (all 5/5). The finite-difference gradient suite is noisy on these small recurrent cases on BOTH backends (CPU reference 5 failing case-runs in 12 suite runs, CUDA 4 in 12, same cases): it is kept, but the direct case is the stable oracle. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Q4NU4VNiELPQfBpCacDZGc
What
The CUDA twin of
GATED_DELTA_NET_BACK(#8's CPU reference), so the 27B's training graph — every gated delta layer's backward — runs on the GPU instead of aborting at graph build or falling to the CPU.Shape: one block per (sequence, head),
S_vthreads, threadjowns rowjofM/M'/dMin the reference's transposed state layout. Row-local sums (u_j,dd_j, theMupdate, thedMupdates) stay in the thread; the column sums the backward needs (dq_i,dk_i, the KDAdg_i) go through shared accumulators with each thread adding at a diagonal offset (i = j + s mod S_vat steps), so no two threads hit one address in a step.dq/dkare accumulated with atomics because broadcast heads share them. The forward states are recomputed and stored in a pool buffer ((T + 1) × S_v²per block);M'anddMlive in dynamic shared memory. First cut:S_v ∈ {16, 32, 64, 128}.Also adds three gradient cases at head sizes 16 and 32 (kda on/off, K 1 and 2, two sequences): the existing cases use head sizes 4 and 8, which the CUDA forward does not dispatch, so a GPU backward was never exercised by them.
Measured (RTX 5090, CUDA 13.2, Windows)
test-backend-ops grad -b CUDA0 -o GATED_DELTA_NET— the harness's finite-difference check of the backward against the CUDA forward:Backend CUDA0: OK)dSremoved (dM[j][i] *= a_icommented out) the same run reads 19736/19739,Backend CUDA0: FAIL: exactly the three GPU-sized cases fail (MAA 0.293, 0.210, 0.092 against 0.001) and nothing else moves — the oracle sees the kernel, and the mutation is revertedFollow-ons
(T + 1) × S_v² × 4 Bper (sequence, head): 67 MB at T = 1024, S_v = 128. Recomputing in segments bounds that if the 27B step needs it.S_v = 256needs the state matrices out of shared memory (2 × 256 KB does not fit); not on the 27B's path.🤖 Generated with Claude Code
https://claude.ai/code/session_01Q4NU4VNiELPQfBpCacDZGc