flashinfer.token_sparse.token_sparse_kv_indices¶
- flashinfer.token_sparse.token_sparse_kv_indices(positions: Tensor, requests: Tensor, block_table: Tensor, *, block_tables: Tensor, seq_lens: Tensor, requests_per_token: int) None¶
Build the K/V slot lists of token-sparse attention from key positions.
Each token becomes
requests_per_tokenconsecutive attention requests, for example one per group of its query heads, each with the token’s K and V slot lists and count. These are theblock_tablesandseq_lensthattrtllm_batch_decode_with_kv_cache(..., sparse_top_k=K, uses_shared_paged_kv_idx=False)takes over a cache of one-token pages. It runs as one launch without host synchronization, so it can be captured in a CUDA graph (run it once before capture to compile it).- Parameters:
positions (torch.Tensor) – int32
[tokens, K],Ka multiple of 512: each token’s key positions, valid ones first, then -1.requests (torch.Tensor) – int32
[tokens]: each token’s request, a row ofblock_table.block_table (torch.Tensor) – int32
[requests, blocks]: each request’s pages of 64 keys. Keykis in slotblock_table[request, k // 64] * 64 + k % 64.block_tables (torch.Tensor) – int32
[rows, 2, K], output: requestrgets the slots in both rows (K, then V), -1 past its count. Rows pasttokens * requests_per_tokenare left as they are.seq_lens (torch.Tensor) – int32
[rows], output: each request’s count of valid keys.requests_per_token (int) – Attention requests per token.