This document is relevant for: Trn2, Trn3

GDN CTE Backward Kernel API Reference#

Backward pass of the chunked gated delta-rule (schur) prefill kernel.

Reverse-mode VJP of gdn_cte_schur, in two passes over the sequence: a forward state pass that stashes each chunk’s incoming state, then a reverse-time scan that recomputes the chunk intermediates from it and emits every gradient. Requires S a multiple of CHUNK=128 and head dims at most MAX_HEAD_DIM=128. S is additionally bounded by the state stash, which is LINEAR in S: at Dv=128 the kernel admits S <= 49152, proportionally more at smaller Dv (see MAX_STATE_STASH_BYTES_PER_PARTITION) – a bound the forward does not have. Costs roughly 3.1-3.3x the forward’s device time at the same shape.

Background#

gdn_cte_bwd computes the reverse-mode gradient (VJP) of the chunked gated delta-rule (gdn_cte) prefill kernel, emitting the query, key, value, gate, beta, and initial-state gradients. It runs a forward state pass that stashes each chunk’s incoming state, then a reverse-time scan that recomputes the chunk intermediates and accumulates the gradients.

API Reference#

Source code for this kernel API can be found at: gdn_cte_bwd.py

gdn_cte_bwd#

nkilib.experimental.gdn.gdn_cte_bwd(query: nl.NkiTensor, key: nl.NkiTensor, value: nl.NkiTensor, g_log: nl.NkiTensor, beta: nl.NkiTensor, d_out: nl.NkiTensor, mask_pack: nl.NkiTensor, scale: float = 1.0, group_size: int = DEFAULT_GROUP_SIZE, recurrent_state: Optional[nl.NkiTensor] = None, d_state: Optional[nl.NkiTensor] = None) → tuple[nl.NkiTensor, nl.NkiTensor, nl.NkiTensor, nl.NkiTensor, nl.NkiTensor, nl.NkiTensor]#

Backward pass of the chunked gated delta-rule (schur) prefill kernel.

Parameters:
  • value (nl.NkiTensor) – [B, Hv, S, Dv], same dtype as query. g_log, beta (nl.NkiTensor): [B, Hv, S], fp32.

  • d_out (nl.NkiTensor) – [B, Hv, S, Dv], dL/d out, fp32 – it is consumed in fp32 and the gate/beta gradients it feeds are fp32 outputs, so it does NOT follow the compute dtype. A bf16 upstream gradient must be cast by the caller.

  • mask_pack (nl.NkiTensor) – [N_MASKS, CHUNK, CHUNK] fp32, from build_mask_pack().

  • scale (float) – query output scale.

  • group_size (int) – chunks per grouped inverse. The state-independent recompute (stage 1 + the schur merge) is emitted LEVEL-MAJOR across a group of this many chunks in both passes, so per-level dependency stalls amortize; the state / state-gradient recurrences stay serial. Scheduling only – output is bit-identical to group_size=1.

  • recurrent_state (Optional[nl.NkiTensor]) – [B, Hv, Dk, Dv] fp32 initial state, or None.

  • d_state (Optional[nl.NkiTensor]) – [B, Hv, Dk, Dv] fp32 dL/d final state, or None (zero).

Dimensions:

  • B: Batch size

  • Hk: Key/query head count

  • Hv: Value head count, a multiple of Hk (GQA ratio Hv // Hk)

  • Dk: Key/query/state head dimension (<= MAX_HEAD_DIM=128)

  • Dv: Value/state head dimension (<= MAX_HEAD_DIM=128, and independent of Dk)

  • S: Sequence length (must be a multiple of CHUNK)

  • CHUNK: Chunk length (128)

This document is relevant for: Trn2, Trn3