flashinfer.sdsa.token_sparse.gather_nvfp4_kv_cache_by_position

flashinfer.sdsa.token_sparse.gather_nvfp4_kv_cache_by_position(paged_kv_cache: tuple[Tensor, Tensor], kv_cache_sf: tuple[Tensor, Tensor], positions: Tensor, requests: Tensor, block_table: Tensor, *, out: Tensor, block_tables: Tensor, seq_lens: Tensor, 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 the NVFP4 K/V tokens at the given key positions into FP8 attention workspace and write their K/V index lists, in one launch.

Row r’s key j (cache slot block_table[requests[r], key // P] * P + key % P, P the page size) becomes FP8 row r * top_k + j of out (see Notes); a negative key gives a zero row. Each of the row’s requests_per_row attention requests (rows r * requests_per_row + i of block_tables and seq_lens) gets index lists r * top_k + j and the row’s count of nonnegative keys: the block_tables and seq_lens that trtllm_batch_decode_with_kv_cache(..., sparse_top_k=top_k, uses_shared_paged_kv_idx=False) takes over out as a cache of one-token pages.

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 (512, 2048), a multiple of tokens_per_program.

  • requests (torch.Tensor) – int32 [rows]: each row’s row of block_table.

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

  • out (torch.Tensor) – FP8 E4M3 [rows * top_k or more, 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).

  • seq_lens (torch.Tensor) – int32 outputs [rows * requests_per_row, 2, top_k] and [rows * requests_per_row] (or more rows).

  • lengths (torch.Tensor, optional) – int32 [rows]: each row’s count of valid keys, when entries past it may hold stale values; those then give zero rows. Entries within the count must be valid keys: a negative one gives a zero row that the count still includes, so attention weights a zero token. None: count the nonnegative keys.

  • requests_per_row (int) – Attention requests per row: row r’s lists and count go to rows r * requests_per_row to (r + 1) * requests_per_row - 1 of block_tables and seq_lens.

  • tokens_per_program (int) – Launch tuning (powers of two): the tokens each program converts, and its warps.

  • num_warps (int) – Launch tuning (powers of two): the tokens each program converts, 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.