> This page is for version Latest (default).
> For other versions, use one of these documentation indexes:
> - Latest (default): https://docs.nvidia.com/cudnn/latest/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/cudnn/llms.txt.

# 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.

| Family | Differentiable op in `cudnn.linear_attention.jax_api` | Forward and backward in `cudnn.ops` | Summaries in `cudnn.ops` |
|---|---|---|---|
| Kimi Delta Attention | `kimi_delta_attention` | `kimi_delta_attention_forward_jax`, `kimi_delta_attention_backward_jax` | `kimi_delta_attention_summary_forward_jax`, `kimi_delta_attention_summary_backward_jax` |
| Gated DeltaNet | `gated_delta_net` | `gated_delta_net_forward_jax`, `gated_delta_net_backward_jax` | `gated_delta_net_summary_forward_jax`, `gated_delta_net_summary_backward_jax` |
| Gated DeltaNet v2 | `gated_delta_net_v2` | `gated_delta_net_v2_forward_jax`, `gated_delta_net_v2_backward_jax` | `gated_delta_net_v2_summary_forward_jax`, `gated_delta_net_v2_summary_backward_jax` |
| Gated DeltaProduct | `gated_delta_product` | `gated_delta_product_forward_jax`, `gated_delta_product_backward_jax` | `gated_delta_product_summary_forward_jax`, `gated_delta_product_summary_backward_jax` |
| Mamba-2 | `mamba2` | `mamba2_forward_jax`, `mamba2_backward_jax` | `mamba2_summary_forward_jax`, `mamba2_summary_backward_jax` |

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.

| Tensor | KDA | GDN | GDN2 | GDP |
|---|---|---|---|---|
| `q` | `[T, HQ, K]` | `[T, HQ, K]` | `[T, HQ, K]` | `[T, HQ, K]` |
| `k` | `[T, HK, K]` | `[T, HK, K]` | `[T, HK, K]` | `[T*n, HK, K]` |
| `v` | `[T, HV, V]` | `[T, HV, V]` | `[T, HV, V]` | `[T*n, HV, V]` |
| `g` | `[T, HO, K]` | `[T, HO]` | `[T, HO, K]` | `[T, HO]` |
| `beta` | `[T, HO]` | `[T, HO]` | `[T, HO, K]` | `[T*n, HO]` |
| `w` | n/a | n/a | `[T, HO, V]` | n/a |
| output | `[T, HO, V]` | `[T, HO, V]` | `[T, HO, V]` | `[T, HO, V]` |

- `q`, `k`, and `v` share BF16 or FP16.
- GDN2's `beta` and `w` share the I/O dtype; the scalar `beta` of KDA, GDN, and GDP accepts FP32 or the I/O dtype.
- `g`, `a_log`, and `dt_bias` accept FP32, BF16, or FP16.
- `initial_state` accepts 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:

- `scale` multiplies `q` and defaults to `1/sqrt(K)`.
- `use_qk_l2norm_in_kernel=True` normalizes `q` and `k` with epsilon `1e-6`; GDP takes another epsilon as `qk_l2norm_eps`.
- `use_beta_sigmoid_in_kernel=True` treats `beta` as logits; `allow_neg_eigval=True` doubles that sigmoid and requires it to be enabled.
- `gate_domain="log"` interprets `g` as log decay. `"linear"` accepts decay factors and returns their gradients. It cannot be combined with `safe_gate`.
- `safe_gate=True` accepts raw `g` logits. GDN and GDP use `-exp(a_log) * softplus(g + dt_bias)`; both parameters have shape `[HO]`. KDA and GDN2 use `gate_lower_bound * sigmoid(exp(a_log) * (g + dt_bias))`, with `a_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=0` saves 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.

```python
import jax
import jax.numpy as jnp
from cudnn.linear_attention.jax_api import gated_delta_net

q = jnp.full((35, 1, 64), 0.05, jnp.bfloat16)
k, v = q, jnp.ones_like(q)
g = jnp.full((35, 1), -0.1, jnp.float32)
beta = jnp.full((35, 1), 0.5, jnp.float32)
cu = jnp.array([0, 17, 17, 35], jnp.int32)
state = jnp.zeros((3, 1, 64, 64), jnp.float32)

def loss(q, k, v, g, beta, state):
    out, final = gated_delta_net(
        q, k, v, g, beta, cu, initial_state=state,
        output_final_state=True, batch_invariant=True,
    )
    return out.astype(jnp.float32).sum() + final.sum()

grads = jax.jit(jax.grad(loss, argnums=(0, 1, 2, 3, 4, 5)))(
    q, k, v, g, beta, state
)
```

Safe-gate parameters are ordinary differentiable arguments. KDA's gate has one value per key channel:

```python
from cudnn.linear_attention.jax_api import kimi_delta_attention

g_channels = jnp.full((35, 1, 64), -0.1, jnp.float32)
a_log = jnp.zeros((1,), jnp.float32)
dt_bias = jnp.zeros((1, 64), jnp.float32)

def gate_loss(a_log, dt_bias):
    out, _ = kimi_delta_attention(
        q, k, v, g_channels, beta, cu, safe_gate=True, a_log=a_log, dt_bias=dt_bias,
        use_beta_sigmoid_in_kernel=True,
    )
    return out.astype(jnp.float32).sum()

d_a_log, d_dt_bias = jax.jit(jax.grad(gate_loss, argnums=(0, 1)))(a_log, dt_bias)
```

## Mamba-2 Inputs

Mamba-2 follows the model-facing PyTorch signature:

```python
output, final = cudnn.linear_attention.jax_api.mamba2(
    x, dt, A, B, C, cu_seqlens,
    D=None, dt_bias=None, z=None, initial_state=None,
    output_final_state=True, checkpoint_every_n_tokens=0,
    dt_softplus=True, dt_min=0.0, dt_max=None, batch_invariant=True,
)
```

`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

```python
from cudnn.ops import gated_delta_net_forward_jax, gated_delta_net_backward_jax

out, final, residual = gated_delta_net_forward_jax(
    q, k, v, g, beta, cu, initial_state=state, output_final_state=True,
)
grads = gated_delta_net_backward_jax(
    residual, jnp.ones_like(out), d_final_state=jnp.ones_like(final),
)
```

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](/cudnn/operations/LinearAttention). Their arguments follow the PyTorch `<op>_summary` and `<op>_summary_bwd`:

```python
from cudnn.ops import gated_delta_net_summary_forward_jax, gated_delta_net_summary_backward_jax

final_state, transition = gated_delta_net_summary_forward_jax(
    k, v, g, beta, cu, initial_state=state, batch_invariant=True,
)
d_initial_state, transition = gated_delta_net_summary_backward_jax(
    q, k, g, beta, doutput, cu, d_final_state=d_final,
    output_transition=True, batch_invariant=True,
)
```

- The forward summary returns `(final_state, transition)`: `final_state [N, HO, V, K]` is each sequence's state after its last token under `initial_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 under `d_final_state`, the pure summary term when omitted; `output_transition=True` also 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_zero` and `d_initial = d_final @ transition + d_initial_from_zero`.
- For Mamba-2, `transition [N, H]` is the base-2 logarithm of the span's decay product `gamma`: `final = gamma * initial + final_from_zero`, and likewise for the gradient.
- Pass FP32 `initial_state` and `d_final_state` arrays 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](/cudnn/operations/determinism) and the [measurement record on GitHub](https://github.com/NVIDIA/cudnn-frontend/blob/develop/test/python/determinism_goldens/README.md#jax-linear-attention-shared-launcher) 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:

```bash
CUDA_VISIBLE_DEVICES=0 XLA_PYTHON_CLIENT_PREALLOCATE=false \
  python -m pytest core/jax/test_call_jax.py linear_attention/jax --confcutdir=linear_attention/jax
```

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.