flashinfer.gdn_paged.chunk_gated_delta_rule_paged¶
- flashinfer.gdn_paged.chunk_gated_delta_rule_paged(q: Tensor, k: Tensor, v: Tensor, g: Tensor | None = None, beta: Tensor | None = None, scale: float | None = None, *, cu_seqlens: Tensor, state_pool: Tensor, page_indices: Tensor, page_indptr: Tensor, page_size: int, last_page_len: Tensor, use_qk_l2norm_in_kernel: bool = False, output: Tensor | None = None, gate_domain: Literal['linear', 'log'] = 'linear', use_gate_in_kernel: bool = False, A_log: Tensor | None = None, dt_bias: Tensor | None = None, beta_is_logit: bool = False, slots: GDNPagedPrefillSlots | None = None, backend: Literal['flashinfer', 'cake_gdn', 'auto'] = 'flashinfer', max_seqlen: int | None = None, aligned_prefixes: bool = False) Tensor¶
Chunked GDN prefill that reads and writes a paged recurrent-state pool.
For every sequence
bthe call processes its new tokensq[cu_seqlens[b]:cu_seqlens[b + 1]], which are the last tokens of the sequence’s(num_pages_b - 1) * page_size + last_page_len[b]tokens (num_pages_b = page_indptr[b + 1] - page_indptr[b]). The tokens before them form its prefix. The callreads the initial state from the page that holds the last prefix token (the zero state when the prefix is empty),
writes the state at every page boundary it crosses to that page, and
writes the final state to the last page.
Pages that end at or before the prefix are only read, so a full-page prefix shared with other sequences is never written. A prefix that ends inside a page continues that (private) page in place. Prefix lengths need not be multiples of the 64-token chunk.
- Parameters:
beta – As in
flashinfer.gdn_prefill.chunk_gated_delta_rule().scale – As in
flashinfer.gdn_prefill.chunk_gated_delta_rule().use_qk_l2norm_in_kernel – As in
flashinfer.gdn_prefill.chunk_gated_delta_rule().output – As in
flashinfer.gdn_prefill.chunk_gated_delta_rule().gate_domain – As in
flashinfer.gdn_prefill.chunk_gated_delta_rule().use_gate_in_kernel – As in
flashinfer.gdn_prefill.chunk_gated_delta_rule().A_log – As in
flashinfer.gdn_prefill.chunk_gated_delta_rule().dt_bias – As in
flashinfer.gdn_prefill.chunk_gated_delta_rule().beta_is_logit – As in
flashinfer.gdn_prefill.chunk_gated_delta_rule().cu_seqlens (torch.Tensor) –
[num_seqs + 1]contiguous int32/int64 offsets of the new tokens of each sequence inq/k/v. Zero-length sequences are allowed and leave the pool unchanged.state_pool (torch.Tensor) –
[num_pages, num_sab_heads, head_size, head_size]state pool, updated in place. Each row must be one dense[H, V, K]block; the row stride may be padded. Its dtype is the state dtype.page_indices (torch.Tensor) –
[num_indices]contiguous int32/int64 page ids. The ids may be in any order and need not be consecutive.page_indptr (torch.Tensor) –
[num_seqs + 1]contiguous int32/int64 CSR offsets intopage_indices. Sequencebownspage_indices[page_indptr[b]:page_indptr[b + 1]]and must own at least one page.page_size (int) – Tokens per page, a positive multiple of 64.
last_page_len (torch.Tensor) –
[num_seqs]contiguous int32/int64 number of tokens in the last page of each sequence after this call, in[1, page_size].slots (GDNPagedPrefillSlots, optional) – Rows from
gdn_paged_prefill_slots()for the samecu_seqlensand page table, typically shared by every layer of a step. Without them the call computes them (one extra launch). They are not checked against the page table.backend (str) –
"flashinfer"(default) runs the serial chunked kernel."cake_gdn"runs the Cake four-stage context-parallel prefill, which splits each sequence across SMs and writes the page states from inside the split; it needs SM100, SM103 or SM107, a float32state_poolwhose row stride is a multiple of 4 elements, and every prefix length (the tokens beforecu_seqlens[b]) a multiple of 64. The caller must keep the prefixes 64-aligned; they are checked only withFLASHINFER_CAKE_GDN_VALIDATE_SLOTS=1, where a misaligned prefix fails a device assert. Otherwise, for a sequence whose prefix is not, every page row it would write (the page of each page boundary it crosses and its last page) is filled with NaN instead of a state; the other sequences are unaffected. Its results are not bitwise those of"flashinfer"(about 4e-3 relative on the states), and its checkpoint bits depend on the batch shape. It helps when few states run in parallel: on GB300 it is up to 4.3x faster than"flashinfer"at smallnum_seqs * num_sab_headswith long sequences, about even at 32 with 2048 new tokens per sequence, and slower (0.4-0.8x) at 64 or more."auto"(opt-in) picks"cake_gdn"where it measured faster than"flashinfer", and"flashinfer"everywhere else. It picks"cake_gdn"only when all of these hold:aligned_prefixes=True: the caller promises the prefix contract of"cake_gdn"above. Without it,"auto"always runs"flashinfer", so"auto"alone never risks NaN rows.The route accepts the call: the device, dtypes, state pool and page size above, a CUDA 12.8 or newer toolkit (13.4 on SM107), an
output, if given, that is contiguous and 16-byte aligned, and amax_seqlen, if given, of at leastceil(total_seq_len / num_seqs).The batch has few states in parallel and a long sequence. With
S = num_seqs * num_sab_headsandLthe new tokens of the longest sequence:S <= 12andL >= 2048on SM100, SM103 and SM107; on SM100 alsoS <= 24andL >= 4096, orS <= 32andL >= 6144; on SM103 alsoS <= 32andL >= 4096; on SM107 alsoS <= 32andL >= 4096, orS <= 48andL >= 6144.Lismax_seqlenwhen given, elseceil(total_seq_len / num_seqs).
The choice reads only host metadata (shapes, dtypes, the device, the layouts and addresses of
q,k,v,state_poolandoutput,page_size,max_seqlen,aligned_prefixesand the CUDA toolkit version), so it does not synchronize and is safe in a CUDA graph. When it picks"cake_gdn", the results are not bitwise those of"flashinfer".max_seqlen (int, optional) – With
backend="cake_gdn", an upper bound of the new tokens of any sequence; it sizes the split. Without it the bound is the total, and a larger value is clamped to the total. A value smaller than the new tokens of a sequence is undefined behavior; it is checked only withFLASHINFER_CAKE_GDN_VALIDATE_SLOTS=1, where it fails a device assert, and a value belowceil(total_seq_len / num_seqs)raises. Any integer type (e.g.numpy.int64) is accepted; aboolor a float raisesValueError. Withbackend="auto"it also sets the length the choice reads; a value belowceil(total_seq_len / num_seqs), aboolor a float runs"flashinfer". When"auto"may pick"cake_gdn"(aligned_prefixes=True), a given value must be a true upper bound, as for"cake_gdn"."flashinfer"ignores it.aligned_prefixes (bool) – The caller’s promise that every prefix length (the tokens before
cu_seqlens[b]) is a multiple of 64.backend="auto"picks"cake_gdn"only with this promise.backend="cake_gdn"carries the same contract whatever this value, and"flashinfer"ignores it. It is not checked; a wrong promise under"auto"gives the NaN rows described for"cake_gdn".
- Returns:
The output
[total_seq_len, num_o_heads, head_size].- Return type:
torch.Tensor
Notes
A page written by one sequence must not be read or written by another sequence of the same call. The page table and lengths are caller preconditions; they are not validated against each other on the device.