flashinfer.cake_gdp.cake_gated_delta_product_mtp¶
- flashinfer.cake_gdp.cake_gated_delta_product_mtp(q: Tensor, k: Tensor, v: Tensor, initial_state: Tensor, initial_state_indices: Tensor, A_log: Tensor, a: Tensor, dt_bias: Tensor, b: Tensor, scale: float | None = None, output: Tensor | None = None, ssm_state_indices: Tensor | None = None, disable_state_update: bool | None = None, use_qk_l2norm: bool = True, output_state_indices: Tensor | None = None, rand_seed: Tensor | None = None, philox_rounds: int = 10) tuple[Tensor, Tensor]¶
Gated DeltaProduct decode / MTP verification over an FP32 or FP16 state pool.
One fused kernel per call: for every batch row and head the state is read from its pool slot, decayed once per token, updated by the token’s
num_householderHouseholder sub-steps, read out at the real token and, after the last token, written back to the row’s write slot. The gate is fused:alpha_t = exp(-exp(A_log) * softplus(a_t + dt_bias))per token (softplus threshold 20) andbeta_tj = sigmoid(b_tj)per sub-step. The parameters are those offlashinfer.gated_delta_rule_mtp(), withk,vandbcarrying the Householder axis.- Parameters:
q (torch.Tensor) – Queries
[B, T, H, 128], bfloat16 or float16, one per real token.qandkviews are read in place when their last dimension is dense and the view is 8-byte aligned (base address and outer strides multiples of four elements); other layouts are refused. float16 inputs are converted to bfloat16 for the kernel.k (torch.Tensor) – Keys
[B, T, num_householder, H, 128]inq.dtype: thenum_householder(1to4) sub-steps of tokentarek[:, t, 0 .. n - 1].v (torch.Tensor) – Values
[B, T, num_householder, HV, 64]inq.dtype.HVmust equalH(no GQA / GVA).initial_state (torch.Tensor) – State pool
[pool_size, HV, 64, 128](K-last), float32 or (SM100 / SM103 / SM107 only) float16, read and written in place. Its rows must be dense, its strides multiples of four elements and its base address 16-byte (float32) or 8-byte (float16) aligned; a non-contiguous pool with these properties (padding between heads or slots) is used as is. The recurrence runs in FP32 either way: a float16 pool is widened exactly on load, and every store (the writeback and eachssm_state_indicessnapshot) rounds the FP32 state to float16 – to nearest even withoutrand_seed, stochastically with it. A value that rounds past the largest float16 (65504) is stored as an infinity.initial_state_indices (torch.Tensor) – Read slots
[B](int32 or int64). A negative entry marks a padding row: the kernel skips it entirely – no state read, no recurrence, no output store, no writeback and no scatter – so its output row keeps the value of the caller’soutput(zero whenoutputisNone).A_log (torch.Tensor) – Log decay parameter
[HV], float32 (read in place).a (torch.Tensor) – Input-dependent decay
[B, T, HV]inq.dtype, one per real token.dt_bias (torch.Tensor) – Decay bias
[HV]. A float32 vector is read in place; a bfloat16 or float16 vector is converted once per call, which adds one small launch – keep the parameter in float32 for the single-launch path.b (torch.Tensor) – Update-gate inputs
[B, T, num_householder, HV]inq.dtype, one per sub-step.scale (float, optional) – Query scale;
1 / sqrt(128)whenNone. Any other value,0.0included, is applied as given.output (torch.Tensor, optional) – Pre-allocated output
[B, T, HV, 64], bfloat16 or float16. A bfloat16 buffer whose last dimension is dense and whose base address and strides are 8-byte aligned is written directly; any other buffer receives a copy after the launch (its values in skipped rows are preserved through a bfloat16 round trip). Allocated as zeros inq.dtypewhenNone.ssm_state_indices (torch.Tensor, optional) – Per-token scatter slots
[B, T], int32. When given, the state after every real token (itsnum_householdersub-steps completed) is written toinitial_state[ssm_state_indices[b, t]](a negative entry skips that write) and the end-of-sequence writeback to the write slot is skipped, as inflashinfer.gated_delta_rule_mtp(). Requiresdisable_state_update=False;T == 1is accepted.disable_state_update (bool, optional) –
Trueleaves the pool unchanged (verification without commit).None(the default) meansFalse: the state is written back.use_qk_l2norm (bool) – Apply the L2 normalization (
x * rsqrt(sum(x^2) + 1e-6)) toqandkinside the kernel. DefaultTrue.output_state_indices (torch.Tensor, optional) – Write slots
[B](int32 or int64);initial_state_indiceswhenNone. A negative entry skips the writeback of that row only (its output is still computed and stored). The write slot of a row that writes back must not be another row’s read or write slot (see Notes).rand_seed (torch.Tensor, optional) – Stochastic rounding of a float16 pool: a one-element int64 tensor on the pool’s device, read by the kernel on the device (a captured CUDA graph sees in-place updates). It must not overlap
initial_state. Each stored element adds 13 random bits below its float16 precision and truncates. The bits come from Philox-4x32 ofphilox_roundsrounds keyed by the seed’s 64 bits. One draw serves the columnsk .. k + 3andk + 64 .. k + 67(ka multiple of 4 below 64) of one state row; its counter is the element offset, from the base address of theinitial_stateview, of columnkof that state row in the batch row’s read slot, whichever slot the store writes. So a fixed seed gives bitwise reproducible stores. The counter holds no step or layer index: advance the seed every decode step and use a different one for every layer. It holds no token or batch-row index either: the snapshots of one call, and the rows that read the same slot, share their random bits.None(the default) rounds to nearest even. A float32 pool takes no seed (ValueError).philox_rounds (int) – Philox rounds of the stochastic rounding, an
intfrom 5 to 16 (default 10); read only withrand_seed. Fewer than 5 rounds bias the low bits of neighbouring counters and are refused.
- Returns:
(output, initial_state): the output[B, T, HV, 64]in the requested dtype and the pool with the writebacks / scatters applied (unchanged whendisable_state_update=True).- Return type:
tuple[torch.Tensor, torch.Tensor]
Notes
Requires an SM90, SM100, SM103 or SM107 device (
sm_90a/sm_100a/sm_103a/sm_107aprograms); a float16 pool requires SM100, SM103 or SM107 (the hardwarecvt.rsconversion) and is refused withValueErroron SM90.Tis a runtime value up to the exported maximum (16);Tof 1 to 8 and 16 withnum_householder == 3run programs compiled for that token count, selected byB x HV.The call reads no tensor contents on the host and synchronizes nothing. With bfloat16 activations, float32
A_log/dt_bias, int32 indices and a bfloat16outputit launches the kernel alone; it allocates only the output (whenoutputisNone), whenssm_state_indicesisNonean uninitialized[B, T]int32 scatter table the kernel never reads, and, for a float16 pool withoutrand_seedwhose storage offset is not a multiple of four elements, a one-element int64 seed placeholder the kernel never reads. It is CUDA-graph safe.Batch rows run concurrently in no fixed order. A row writes its write slot (
output_state_indices[b], which defaults toinitial_state_indices[b]) or, withssm_state_indices, its scatter slots; no other row of the same call may read or write those slots, otherwise the stored states and outputs of the rows involved are undefined. So when rows write back withoutoutput_state_indices, the live read slots must be unique; with distinct write slots, withssm_state_indicesor withdisable_state_update=True, read slots may repeat (those rows start from the same state and share their random bits). This is a caller precondition, not checked on the host (no synchronization). Given it, a row’s output and stores do not depend on the other rows, and repeated launches on the same inputs (the same seed included) are bitwise identical. A float16 pool runs the twin of the program a float32 pool would run, with the same arithmetic: the output equals the float32 pool’s bit for bit when both pools hold the same values.