flashinfer.sdsa.token_sparse.compact_nvfp4_kv_cache

flashinfer.sdsa.token_sparse.compact_nvfp4_kv_cache(paged_kv_cache: tuple[Tensor, Tensor], kv_cache_sf: tuple[Tensor, Tensor], positions: Tensor, row_groups: Tensor, group_requests: Tensor, block_table: Tensor, group_offsets: Tensor, *, out: Tensor, block_tables: Tensor, seq_lens: Tensor, scratch: Tensor, bitmap: Tensor, max_keys: int, max_capacity: int, zero_row: int, group_base: int = 0, group_dense: Tensor | None = None, lengths: Tensor | None = None, requests_per_row: int = 1, v_sf_swizzled: bool = True, tokens_per_program: int = 32, num_warps: int = 4) → None

Convert each selected NVFP4 token once per group of rows, and point the rows’ K/V index lists at the converted tokens.

Rows that share a request (its prefill tokens, or its 1 + k MTP verify tokens) select mostly the same keys. This converts each group’s union of selected keys once, into the group’s region of out (rows group_offsets[g] on), and writes each row’s lists as indices into that region. A dense group (group_dense[g] != 0) instead converts every key below its last selected key, key p to region row p: no bitmap, worth it when its rows select more keys in all than its context holds.

Every FP8 row holds its cache token’s converted values (see Notes), the same bits as gather_nvfp4_kv_cache_by_position() writes for that token, and entry j of a row’s lists holds the same key as entry j of its positions, so token-sparse attention over either result computes on the same values in the same order. Entries that are not live (negative keys, entries past the row’s count, keys at or past max_keys) point at zero_row, which is zeroed. Each row’s count is its number of nonnegative keys.

Parameters:
  • paged_kv_cache (tuple[torch.Tensor, torch.Tensor]) – K/V uint8 HND pages, each shaped [pages, 1, page_size, head_dim // 2] (two E2M1 values per byte), with matching strides and contiguous token data. page_size is positive, and a multiple of four for swizzled V scales; head_dim is a power of two, 64 or more.

  • kv_cache_sf (tuple[torch.Tensor, torch.Tensor]) – K/V FP8 E4M3 block-scale pages, each shaped [pages, 1, page_size, head_dim // 16], with matching strides. K scales are linear. V scales follow v_sf_swizzled.

  • v_sf_swizzled (bool) – Whether V scales use TRT-LLM’s HND four-token swizzle, as in caches written for trtllm-gen attention; page_size must then be a multiple of four. Pass False for linear V scales, as written by flashinfer.page.nvfp4_quantize_append_paged_kv_cache().

  • positions (torch.Tensor) – int32 [rows, top_k] key positions, valid ones first, then -1; top_k a power of two.

  • row_groups (torch.Tensor) – int32 [rows]: each row’s group plus group_base. A negative value marks a padding row: its lists point at zero_row. Rows of groups outside [0, groups) (other than padding) are skipped, their lists and counts left as they are, so launches over different group ranges (group_base) can share positions and the output tables.

  • group_requests (torch.Tensor) – int32 [groups]: each group’s row of block_table.

  • block_table (torch.Tensor) – int32 [requests, pages].

  • group_offsets (torch.Tensor) – int64 [groups]: each group’s first row of out. A union group’s region must hold its union (at most its rows times top_k, at most its keys); a dense group’s must hold its keys up to the last selected. Regions must not overlap each other or zero_row.

  • out (torch.Tensor) – FP8 E4M3 [N, 1, 1, 2 * head_dim], K then V.

  • block_tables (torch.Tensor) – int32 outputs [rows * requests_per_row, 2, top_k] and [rows * requests_per_row] (or more rows); row r’s lists and count go to its requests_per_row consecutive rows.

  • seq_lens (torch.Tensor) – int32 outputs [rows * requests_per_row, 2, top_k] and [rows * requests_per_row] (or more rows); row r’s lists and count go to its requests_per_row consecutive rows.

  • scratch (torch.Tensor) – int32 workspace of compact_nvfp4_scratch_numel() (groups, max_keys, N) elements or more; its contents do not matter.

  • bitmap (torch.Tensor) – int32 workspace of compact_nvfp4_bitmap_numel() (groups, max_keys) elements or more, all zero; every call leaves it all zero (zero it once, and do not share it with other uses).

  • max_keys (int) – Keys at or past it are not live (they point at zero_row, and the row’s count still includes them); sizes the bitmap.

  • max_capacity (int) – Sizes the conversion grid: at least every group’s region size (a union group’s union of live keys, a dense group’s last live key plus one). Region rows at or past it are not converted.

  • zero_row (int) – A row of out outside every region.

  • group_dense (torch.Tensor, optional) – int32 [groups], nonzero for dense groups; None: all union groups.

  • lengths (torch.Tensor, optional) – int32 [rows]: each row’s count of valid keys, when entries past it may hold stale values (they are then not live). Entries within the count must be valid keys: a negative one points at zero_row and is still counted, so attention weights a zero token. None: count the nonnegative keys.

  • group_base (int) – The row_groups value of this launch’s group 0 (see row_groups).

  • requests_per_row (int) – Attention requests per row (see block_tables).

  • tokens_per_program (int) – Launch tuning (powers of two): the region rows each conversion program converts at a time, and its warps.

  • num_warps (int) – Launch tuning (powers of two): the region rows each conversion program converts at a time, and its warps.

  • safe (Graph)

Notes

All tensors must be on the same CUDA device. Outputs are written in place. An FP8 row holds the token’s decoded NVFP4 values (each E2M1 value times its E4M3 block scale) divided by six, excluding the global scales: multiply BMM1’s K scale and BMM2’s V scale by six. This covers NVFP4’s full normalized range without FP8 saturation. The result is rounded to E4M3: products of E2M1 values and E4M3 scales can need more mantissa bits, and small scales can become subnormal or zero. Requires a Blackwell GPU (compute capability 10.x, 11.x or 12.x); validated on SM103.