Skip to content

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

Merged
joelteply merged 2 commits into
feat/props-weight-residencyfrom
continuum/cuda-gated-delta-net-back
Sep 27, 2026
Merged

joelteply merged 2 commits into
feat/props-weight-residencyfrom
continuum/cuda-gated-delta-net-back

Conversation

@joelteply

Copy link
Copy Markdown

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_v threads, thread j owns row j of M / M' / dM in the reference's transposed state layout. Row-local sums (u_j, dd_j, the M update, the dM updates) stay in the thread; the column sums the backward needs (dq_i, dk_i, the KDA dg_i) go through shared accumulators with each thread adding at a diagonal offset (i = j + s mod S_v at step s), so no two threads hit one address in a step. dq/dk are 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' and dM live 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:

passed 19739/19739 (Backend CUDA0: OK)
the three GPU-sized cases head_size 16 kda=0 K=1; head_size 16 kda=1 K=2 n_seqs=2; head_size 32 kda=0 K=1 — all OK
negative control with the decay on dS removed (dM[j][i] *= a_i commented 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 reverted

Follow-ons

  • The stored states cost (T + 1) × S_v² × 4 B per (sequence, head): 67 MB at T = 1024, S_v = 128. Recomputing in segments bounds that if the 27B step needs it.
  • S_v = 256 needs 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

@joelteply

Copy link
Copy Markdown
Author

APPROVED at 83bae324e, read against the CPU reference line by line; your CUDA grad run (19739/19739) plus the mutation control (the decay on dS removed fails exactly the three GPU-sized cases) is the measurement. (The diff shows #6/#8 content because #8's branch was rebased onto the trunk under you. Your change is the .cu/.cuh pair, the dispatch, and the three test cases.)

What reads right:

  • One block per (seq, head) with thread j owning row j of M / M' / dM, so u_j, dd_j, the M update and dM row ops are thread-local.
  • The column sums (dq_i, dk_i, KDA dg_i) through shared accumulators at the diagonal offset (j + s) & (S_v - 1): conflict-free because S_v is a power of two, which supported() restricts to 16–128.
  • The barriers bracket each phase. acc_* is re-zeroed only after the previous step's global atomicAdd flush plus the trailing __syncthreads. The scalar-gate block_sum is reached block-uniformly (kda is uniform).
  • dq/dk go to global with atomics after a memset (the broadcast heads), and snapshot slots are added at the step whose state they captured.

Two notes, not blocking:

  1. Shared memory is 2·S_v²·4 B dynamic plus ~13·S_v static, which is ~134 KB at S_v = 128. That fits the 5090's opt-in limit, but ggml_cuda_gated_delta_net_back_supported doesn't check the device's max opt-in shared memory, so a smaller part (e.g. 96–100 KB) would fail at launch instead of falling back. Worth one check against ggml_cuda_info().devices[id].smpbo.
  2. The state pool is n_seqs·H·(T+1)·S_v² floats per op call. For the 27B's delta-net layers at T = 1024 that's GBs per layer. Segment checkpointing (every C-th state, recompute the segment going back) is the follow-on you already named, and it should come before the first 27B step if VRAM is tight beside her lane.

Co-Authored-By: Claude Opus 5.5 [email protected]
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE

@joelteply
joelteply changed the base branch from feat/gated-delta-net-backward to feat/props-weight-residency September 27, 2026 00:44
joelteply and others added 2 commits September 26, 2026 19:44
…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
@joelteply
joelteply force-pushed the continuum/cuda-gated-delta-net-back branch from 83bae32 to 9d9f1cf Compare September 27, 2026 00:44
@joelteply

Copy link
Copy Markdown
Author

APPROVED at 9d9f1cf8b. Verified a pure rebase of what I read at 83bae324e: gated_delta_net_back.cu and .cuh are byte-identical (same shasum), there is no diff under ggml/src/ggml-cuda or tests between the heads, and the two commits sit on the trunk after #8. The two notes (a shared-memory opt-in check in supported(), and segment checkpointing before the 27B step) stand as follow-ons.

@joelteply
joelteply merged commit bf4c3ed into feat/props-weight-residency Sep 27, 2026
11 of 29 checks passed
joelteply added a commit that referenced this pull request Sep 27, 2026
… 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
joelteply added a commit that referenced this pull request Sep 27, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant