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 keyj(cache slotblock_table[requests[r], key // P] * P + key % P,Pthe page size) becomes FP8 rowr * top_k + jofout(see Notes); a negative key gives a zero row. Each of the row’srequests_per_rowattention requests (rowsr * requests_per_row + iofblock_tablesandseq_lens) gets index listsr * top_k + jand the row’s count of nonnegative keys: theblock_tablesandseq_lensthattrtllm_batch_decode_with_kv_cache(..., sparse_top_k=top_k, uses_shared_paged_kv_idx=False)takes overoutas 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_sizeis positive, and a multiple of four for swizzled V scales;head_dimis 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 followv_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_sizemust then be a multiple of four. Pass False for linear V scales, as written byflashinfer.page.nvfp4_quantize_append_paged_kv_cache().positions (torch.Tensor) – int32
[rows, top_k]key positions, valid ones first, then -1;top_ka power of two (512, 2048), a multiple oftokens_per_program.requests (torch.Tensor) – int32
[rows]: each row’s row ofblock_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 rowsr * requests_per_rowto(r + 1) * requests_per_row - 1ofblock_tablesandseq_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.