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
statetoflashinfer.gdn_decode.gated_delta_rule_decode()from then on (the call updates it in place, as any state). A T=1 call withnum_q_headsquery heads, thisscale, contiguous BF16 q/k/v/a/b and output, FP32A_loganddt_biasanduse_qk_l2norm=Trueunderbackend="auto"orbackend="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 anyrows0.rows0defaults to the byte cut: half the rows on each die, the odd row on die 0 (split_units(B, (1, 1))[0]).scaledefaults toK ** -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).stateitself is not freed: drop the last reference to it after this call so that the state is not held twice.Returns
stateitself, without a copy, when the call is under CUDA graph capture, or when it does not home the state:stateis not FP32 withK = V = 128on an SM107a device,stateis 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 coversH=16,HV=32fromB=48on, atscaleK ** -0.5or1.0, on 212-SM devices), the device has no SM table (flashinfer.cake_locality.sm_table()isNone: 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 thanstateis therefore a new homed copy, andstateitself as the result means only that no copy was made, not which kernel a decode call runs: a call on astatethat was never homed never runs the twin, but astatethat 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. RaisesValueErrorfor astatethat is not a contiguous 4-D CUDA tensor, a non-positivenum_q_headsor arows0outside[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 asstate[: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
Hof 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).