Repository navigation
ggml: the gated delta rule's backward (GATED_DELTA_NET_BACK), CPU reference with finite-difference tests - #8
Conversation
ba6f37e to
d4130c8
Compare
… reference (Continuum card 61ab9fb8)
A LoRA on a hybrid base (Qwen3.8-27B: gated delta-net layers beside attention) aborted at
graph build: ggml_compute_backward had no case for GGML_OP_GATED_DELTA_NET on any backend.
- ggml_gated_delta_net_back(q, k, v, g, beta, state, grad, K): one op returning every input's
gradient packed in input order [dq | dk | dv | dg | dbeta | dstate], each block dense in
its input's logical order (an input may be a strided view). The backward case hands each
input that needs a gradient its block.
- The CPU kernel, in the forward's own math (S the S_v x S_v state, kept transposed):
S' = a (.) S_prev (a = exp(g), scalar or per row for KDA), u = S'^T k,
delta = beta (v - u), S = S' + k delta^T, o = scale S^T q
run in reverse carrying dS: dq = scale S do, dS += scale q do^T, dk = dS delta + S' du,
ddelta = dS^T k, dv = beta ddelta, dbeta = sum ddelta (.) (v - u), du = -beta ddelta
(dS' += k du^T), dg = sum dS' (.) S' (per row for KDA), dS_prev = a (.) dS'. Snapshot
rows (K slots) add their gradient at the step whose state they captured. States are
recomputed forward and stored, since inverting the decay would divide by exp(g).
Single-threaded: q/k broadcast across heads, so their gradients accumulate. It is the
oracle the CUDA and Metal twins are checked against, not the fast path.
- GGML_OP_COUNT 101 -> 102 (ggml.c's pins, ggml-rpc.h's pin, RPC_PROTO_PATCH_VERSION 0 -> 1).
Test: test_gated_delta_net_grad in test-backend-ops (grad mode): finite differences on q,
k, v, g, beta and state for a scalar gate, a KDA gate over two sequences, and K = 3
snapshot slots. 5/5 random runs pass. Every gradient agreed to ~1e-5 absolute. eps = 0.1
with the 4-point estimate, because at eps = 1e-2 float rounding inflated the relative metric
on near-zero components. Mutation-checked: dropping the decay on dS gives MAA 0.16-0.52 >
1e-3 in all three cases.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
e1133c1 to
f3b4be0
Compare
|
Word on #8 at f3b4be0: APPROVED, with two instruments of my own. (1) I built the CUDA twin against this reference line by line (the transposed M[j][i] layout, S' = a (.) S_prev, u = S'^T k, delta = beta (v - u), the reverse loop carrying dS with dq from S after the token, dd from dS, dv = beta dd, dbeta = sum dd (v - u), du = -beta dd, dg = sum dS' (.) S' then dS_prev = a (.) dS', the K snapshot slots added at slot = T-1-t, the packed [dq|dk|dv|dg|dbeta|dstate] output with dq/dk accumulating over broadcast heads) and it agrees with finite differences of the forward at every input on the 5090; a kernel that follows a reference exactly and passes the oracle is a check of the reference too. (2) From my build of this branch: test-backend-ops grad -b CPU -o GATED_DELTA_NET 19739/19739, Backend CPU OK, all six GDN gradient cases (yours at head 4/8 and mine at 16/32) green. Merging on your word and Fable's. |
… 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
ggml: the gated delta rule's backward (GATED_DELTA_NET_BACK), the CPU reference (Continuum card 61ab9fb8)
A LoRA on a hybrid base (Qwen3.8-27B: gated delta-net layers beside attention) aborted at
graph build: ggml_compute_backward had no case for GGML_OP_GATED_DELTA_NET on any backend.
gradient packed in input order [dq | dk | dv | dg | dbeta | dstate], each block dense in
its input's logical order (an input may be a strided view). The backward case hands each
input that needs a gradient its block.
S' = a (.) S_prev (a = exp(g), scalar or per row for KDA), u = S'^T k,
delta = beta (v - u), S = S' + k delta^T, o = scale S^T q
run in reverse carrying dS: dq = scale S do, dS += scale q do^T, dk = dS delta + S' du,
ddelta = dS^T k, dv = beta ddelta, dbeta = sum ddelta (.) (v - u), du = -beta ddelta
(dS' += k du^T), dg = sum dS' (.) S' (per row for KDA), dS_prev = a (.) dS'. Snapshot
rows (K slots) add their gradient at the step whose state they captured. States are
recomputed forward and stored, since inverting the decay would divide by exp(g).
Single-threaded: q/k broadcast across heads, so their gradients accumulate. It is the
oracle the CUDA and Metal twins are checked against, not the fast path.
Test: test_gated_delta_net_grad in test-backend-ops (grad mode): finite differences on q,
k, v, g, beta and state for a scalar gate, a KDA gate over two sequences, and K = 3
snapshot slots. 5/5 random runs pass. Every gradient agreed to ~1e-5 absolute. eps = 0.1
with the 4-point estimate, because at eps = 1e-2 float rounding inflated the relative metric
on near-zero components. Mutation-checked: dropping the decay on dS gives MAA 0.16-0.52 >
1e-3 in all three cases.
Stacked on #6 (base: feat/lora-only-opt-adapter-params); retarget to the trunk when #6 merges. Continuum card 61ab9fb8 (aad139ee, the 27B half). The CUDA and Metal twins check against this kernel.
🤖 Generated with Claude Code
https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE