flashinfer.gdn_decode.localize_gdn_decode_state

flashinfer.gdn_decode.localize_gdn_decode_state(state: Tensor, num_q_heads: int, scale: float | None = None, rows0: int | None = None) → Tensor

A copy of the K-major FP32 GDN decode state state ([B, HV, K, V], contiguous) homed by batch rows on the two dies of a dual-die SM107a GPU: rows [0, rows0) in die 0’s memory, the rest in die 1’s, in one virtual address range.

Pass the returned tensor as state to flashinfer.gdn_decode.gated_delta_rule_decode() from then on (the call updates it in place, as any state). A T=1 call with num_q_heads query heads, this scale, contiguous BF16 q/k/v/a/b and output, FP32 A_log and dt_bias and use_qk_l2norm=True under backend="auto" or backend="cake_gdn" then runs the locality-domain twin of the Cake nontranspose FP32 decode body: one launch over both dies whose CTAs take the (batch row, value head) items of their own die’s rows first, so most state bytes stay on the die that reads and writes them. The output and the updated state are the whole-GPU body’s bit for bit, for any rows0.

rows0 defaults to the byte cut: half the rows on each die, the odd row on die 0 (split_units(B, (1, 1))[0]). scale defaults to K ** -0.5, the default of the decode call.

The returned tensor is a new allocation of the size of state, made by the CUDA driver outside PyTorch’s caching allocator (torch.cuda.memory_allocated() does not count it). state itself is not freed: drop the last reference to it after this call so that the state is not held twice.

Returns state itself, without a copy, when the call is under CUDA graph capture, or when it does not home the state: state is not FP32 with K = V = 128 on an SM107a device, state is a __torch_dispatch__ wrapper subclass, the call row (B, num_q_heads, HV, scale) has no twin listed for the device’s SM count in any die class (the twin covers H=16, HV=32 from B=48 on, at scale K ** -0.5 or 1.0, on 212-SM devices), the device has no SM table (flashinfer.cake_locality.sm_table() is None: a CUDA 13.5 toolkit and driver are needed), the row is not listed for the die class of the device, or the cut is not a multiple of the allocation granularity of locality-domain memory. The checks that need no device work come first: a refused row is returned before the SM table is probed or anything is allocated. A result other than state is therefore a new homed copy, and state itself as the result means only that no copy was made, not which kernel a decode call runs: a call on a state that was never homed never runs the twin, but a state that was already homed (for example by an earlier call of this helper) stays homed, and a call of a listed row on it can still run the twin. Raises ValueError for a state that is not a contiguous 4-D CUDA tensor, a non-positive num_q_heads or a rows0 outside [0, B].

Only the whole returned allocation is homed: the tensor itself and every alias of it with the same shape (a detach(), a view of the same shape). A view of part of it, such as state[:B'], and a copy run the whole-GPU body, which on homed memory may be slower than on ordinary memory. A state of another batch needs its own homed allocation.

Homing a state also fills the device’s reserve of pre-zeroed claim counter pairs for captured calls (flashinfer.cake_locality.reserve_launch_counters()); a failure there does not fail this call. Make one eager decode call before a CUDA graph capture (it builds the kernel and probes the SM table). The memory is freed with the returned tensor, and freeing it synchronizes the device; a CUDA graph that reads the tensor after it is freed faults on replay, so keep it alive as long as such a graph. One graph must not replay on two streams at once.

Parameters:
  • state – K-major FP32 decode state [B, HV, K, V], contiguous, on CUDA.

  • num_q_heads – Number of query (and key) heads H of the decode calls.

  • scale – Query scale of the decode calls (default K ** -0.5).

  • rows0 – Batch rows homed on die 0 (default: the byte cut).