Linear Attention in JAX
The JAX entries run the existing CuTeDSL FROST kernels of the five linear-attention families on SM100, SM103 and SM107, without importing PyTorch. They support eager calls, jax.jit, and first-order reverse-mode differentiation through the output, the initial and final state, and the gate parameters.
The differentiable op returns (output, final_state_or_None) and supports jax.grad. The forward in cudnn.ops takes the same arguments and returns (output, final_state_or_None, residual); pass that residual unchanged to the family’s backward. XLA owns outputs and scratch; no workspace, output buffer, or stream argument is needed.
Packed Sequences and State
Inputs use packed token-major tensors. cu_seqlens [N+1] is an INT32 or INT64 device array containing sequence boundaries: it starts at zero, is nondecreasing, and ends at the packed token count T. Enable JAX x64 before creating INT64 offsets; otherwise JAX may downcast them. Offsets remain runtime data, and changing their values with the same shapes does not specialize the call. The caller must provide valid boundary values; the entries do not read them on the host. Token and workspace indexing must fit signed INT32.
An empty sequence preserves its initial state, or zero when no state is supplied. State is V-major [N, HO, V, K]. It is an ordinary differentiable array, not a mutable cache: pass the returned state into the next call to continue a sequence. output_final_state=False returns None for the state output.
Delta-Rule Inputs
KDA, GDN, and GDP use (q, k, v, g, beta, cu_seqlens, **options); GDN2 adds w between beta and cu_seqlens. GDP requires num_householder=n as a keyword. HO = max(HQ, HV). Each Q, K, and V head count must divide HO, and HK must equal HQ or HV. The FROST delta-rule kernels support K and V of 64 or 128.
q,k, andvshare BF16 or FP16.- GDN2’s
betaandwshare the I/O dtype; the scalarbetaof KDA, GDN, and GDP accepts FP32 or the I/O dtype. g,a_log, anddt_biasaccept FP32, BF16, or FP16.initial_stateaccepts FP32, BF16, or FP16; the final state follows its dtype, or uses FP32 when no initial state is supplied.- Gradients have their input’s dtype.
GDP boundaries, q, g, and the output use real tokens. Each token’s n Householder updates occupy consecutive expanded rows of k, v, and beta. The decay acts before those updates, and the output is read after the last update.
All scalar options are static:
scalemultipliesqand defaults to1/sqrt(K).use_qk_l2norm_in_kernel=Truenormalizesqandkwith epsilon1e-6; GDP takes another epsilon asqk_l2norm_eps.use_beta_sigmoid_in_kernel=Truetreatsbetaas logits;allow_neg_eigval=Truedoubles that sigmoid and requires it to be enabled.gate_domain="log"interpretsgas log decay."linear"accepts decay factors and returns their gradients. It cannot be combined withsafe_gate.safe_gate=Trueaccepts rawglogits. GDN and GDP use-exp(a_log) * softplus(g + dt_bias); both parameters have shape[HO]. KDA and GDN2 usegate_lower_bound * sigmoid(exp(a_log) * (g + dt_bias)), witha_log [HO],dt_bias [HO, K], and a lower bound in[-5, 0)that defaults to-5. Omitted parameters mean unit amplitude and zero bias. Supplying parameters requires safe-gate mode.- GDN2 additionally exposes
beta_guard=True, requiring in-kernel Q/K normalization, with the same straight-through gradient as the PyTorch op. checkpoint_every_n_tokens=0saves no checkpoints. Positive cadences must be multiples of 64 for GDN and GDP and of 16 for KDA and GDN2. GDP counts expanded sub-tokens. Checkpoints are retained in the residual for backward reuse.
Safe-gate parameters are ordinary differentiable arguments. KDA’s gate has one value per key channel:
Mamba-2 Inputs
Mamba-2 follows the model-facing PyTorch signature:
x [T, H, 64], B [T, G, 128], and C [T, G, 128] share BF16 or FP16; G must divide H. dt [T, H] holds raw step logits and A [H] holds negative decay rates. Optional D [H] or D [H, 64] supplies the skip weight, dt_bias [H] supplies the step bias, and z [T, H, 64] supplies the SiLU output gate. dt, A, D, and the step bias accept FP32, BF16, or FP16; z matches x. State is [N, H, 64, 128].
The step is clamp(softplus(dt + dt_bias), dt_min, dt_max); setting dt_softplus=False omits softplus and dt_max=None removes the upper bound. The gradient is zero where the clamp is active. Mamba-2 has no Q scaling option. Checkpoint cadence must be zero or a positive multiple of 128 tokens.
Explicit Backward
The backward returns a named tuple with one gradient per differentiable input, in argument order. GDN, GDP, and KDA return (dq, dk, dv, dg, dbeta, d_initial_state, d_a_log, d_dt_bias); GDN2 adds dw after dbeta. Mamba-2 returns (dx, ddt, da, db, dc, dd, d_dt_bias, dz, d_initial_state). Missing optional inputs have None gradients. An omitted d_final_state contributes zero; supplying it requires a forward call with output_final_state=True. Cotangents must match the output and state shape and dtype.
The residual holds the primals, the static configuration, and the saved checkpoints; it owns no graph handle, workspace, or executable. Pass it unchanged: the checkpoints belong to their saved primals and configuration. A coarser cadence saves fewer states and the backward recomputes the ones in between, which reduces the residual’s storage but not the backward’s workspace.
The explicit entries do not themselves provide autodiff rules; the differentiable op installs custom_vjp over the same kernels. Offsets and static options are not differentiable. Forward-mode AD, higher derivatives, vmap, and distributed array partitioning are outside this API.
Summaries
The summaries are the per-span passes of context parallelism. Their arguments follow the PyTorch <op>_summary and <op>_summary_bwd:
- The forward summary returns
(final_state, transition):final_state [N, HO, V, K]is each sequence’s state after its last token underinitial_state, zero when omitted. - The backward summary returns
(d_initial_state, transition_or_None):d_initial_state [N, HO, V, K]is each sequence’s incoming state gradient underd_final_state, the pure summary term when omitted;output_transition=Truealso returns the transition. - For the delta-rule families and KDA,
transition [N, HO, K, K]is the span’s transition in the stored orientation of each direction:final = initial @ transition + final_from_zeroandd_initial = d_final @ transition + d_initial_from_zero. - For Mamba-2,
transition [N, H]is the base-2 logarithm of the span’s decay productgamma:final = gamma * initial + final_from_zero, and likewise for the gradient. - Pass FP32
initial_stateandd_final_statearrays when supplying boundary states. All returned states, state gradients, and transitions are FP32. The summaries have no autodiff rule.
GDN2’s forward summary takes w between beta and cu_seqlens. With safe_gate=True, pass the same gate_lower_bound to both GDN2 summaries and the full operation. GDP’s summaries take num_householder=n as a keyword. Mamba-2 uses mamba2_summary_forward_jax(x, dt, A, B, cu_seqlens, ...) and mamba2_summary_backward_jax(doutput, dt, A, C, cu_seqlens, ...).
The PyTorch ops fold the gathered summaries of the other ranks inside the op (preceding_state_update, preceding_transition, following_state_grad, following_transition). The JAX entries do not take those ports: compose the boundary state with the identities above and pass it as initial_state or d_final_state.
Placement and Qualification
All operands of a call must reside on one supported GPU. For traced calls (including closures that capture every operand), set jax.default_device to the GPU that JAX will execute on. The entries use it for CuTeDSL compilation and tuning. Without an explicit default, tracing uses JAX’s first local device and requires that at most one of SM100, SM103, and SM107 be visible. Separate local calls do not require a single-process job.
batch_invariant=True requests the existing per-sequence scheduling mode for outputs, recurrent states, and token and state gradients. Shared parameter gradients are whole-batch reductions: a_log and dt_bias of GDN, GDP, and GDN2, and A, D, and dt_bias of Mamba-2. Their fixed-shape replay is deterministic, but changing batch shape or order can change the association of FP32 sums and their result bytes, even when only one sequence has nonzero cotangents.
See the determinism support page and the measurement record on GitHub for the measured scope, numerical limitations, and unmeasured configurations.
Paged and in-place state, the context-parallel fold ports and contexts, and the dedicated decode and verify entry points are not exposed here. A call needs a positive packed token count. JAX passes compact row-major operands, so a transposed input is materialized first. Install the frontend’s JAX dependency group, a CUDA-enabled JAX build, and CuTeDSL 4.8.1 or later (KDA runs from the library’s minimum version). The entries use cutlass.jax and do not fall back to a PyTorch or cuTile implementation.
Tests
Run the JAX suite from test/python; --confcutdir keeps the fixtures of the PyTorch suites above it out:
The suite needs JAX 0.9.1 or later, one supported GPU, and PyTorch for the reference results. It covers forward and backward parity with the PyTorch ops, state and gate gradients, the summaries, repeated calls, recurrent scans (KDA), device placement, and execution without importing PyTorch.