Skip to content

vllm.v1.attention.backends.flashinfer

Attention layer with FlashInfer.

Classes:

Functions:

  • fast_plan_decode –

    A faster version of BatchDecodeWithPagedKVCacheWrapper::plan used for

BatchDCPPrefillWrapper

Methods:

  • plan –

    Plan the prefill operation with given parameters.

plan(qo_indptr_cpu, paged_kv_indptr_cpu, paged_kv_indices, paged_kv_last_page_len_cpu, kv_lens_cpu, page_size, num_qo_heads, dcp_world_size, num_kv_heads, head_dim, sm_scale, window_left, logits_soft_cap, q_data_type, kv_cache_dtype, prefill_fixed_split_size, disable_split_kv, block_tables)

Plan the prefill operation with given parameters.

FIDecode dataclass

Metadata for the native FlashInfer decode pathway (non-TRTLLM).

FIPrefill dataclass

Metadata for the native FlashInfer prefill pathway (non-TRTLLM).

FlashInferBackend

Bases: AttentionBackend

Methods:

  • customize_spec –

    NVFP4 stores K and V as separate per-head slots of packed fp4 data

  • supports_sink –

    Whether FlashInfer can serve attention sinks on this platform.

customize_spec(spec) classmethod

NVFP4 stores K and V as separate per-head slots of packed fp4 data plus fp8 block scales.

supports_sink() classmethod

Whether FlashInfer can serve attention sinks on this platform.

FlashInferDecodeKernel

Bases: Enum

Decode kernels selected inside the FlashInfer backend.

FlashInferImpl

Bases: AttentionImpl

Methods:

  • forward –

    Forward pass with FlashInfer.

forward(layer, query, key, value, kv_cache, attn_metadata, output, output_scale=None, output_block_scale=None)

Forward pass with FlashInfer.

Parameters:

  • layer

    (Module) –

    The attention layer, providing the q/k/v quantization scales.

  • query

    (Tensor) –

    shape = [num_tokens, num_heads, head_size]

  • key

    (Tensor | None) –

    shape = [num_tokens, num_kv_heads, head_size], or None for a KV-sharing decoder layer.

  • value

    (Tensor | None) –

    shape = [num_tokens, num_kv_heads, head_size], or None for a KV-sharing decoder layer.

  • kv_cache

    (Tensor) –

    [num_blocks, num_kv_heads, block_size, 2*head_size]

  • attn_metadata

    (FlashInferMetadata) –

    Metadata for attention.

  • output

    (Tensor) –

    Tensor that the attention result is written into.

  • output_scale

    (Tensor | None, default: None ) –

    Scale for fused output quantization. Enables the attention+quantization fusion path when provided.

  • output_block_scale

    (Tensor | None, default: None ) –

    Block scale for fused output quantization, required for nvfp4 output and rejected for fp8 output.

Returns:

  • Tensor –

    shape = [num_tokens, num_heads * head_size]

FlashInferMetadata dataclass

Attributes:

decode instance-attribute

Holds the metadata for the decode portion of the batch. Will be None if num_decode_tokens == 0.

num_actual_tokens instance-attribute

Total number of tokens in the batch (excluding padding).

prefill instance-attribute

Holds the metadata for the prefill portion of the batch. Will be None if num_prefill_tokens == 0.

slot_mapping instance-attribute

Tensor for writing K/V to the cache. Shape: [num_actual_tokens]

FlashInferMetadataBuilder

Bases: AttentionMetadataBuilder[FlashInferMetadata]

Methods:

_compute_decode_query_lens(qo_indptr, qo_indptr_cpu, num_decodes, num_decode_tokens)

Return the query width, ragged offsets, and effective query lengths.

_compute_flashinfer_kv_metadata(num_blocks_np, seq_lens_np, block_table_tensor, num_reqs, page_size)

Compute paged_kv_indptr, paged_kv_indices and paged_kv_last_page_len.

Results are stored in self.paged_kv_indptr, self.paged_kv_indices, self.paged_kv_last_page_len buffers.

Returns paged_kv_indices, a GPU tensor with shape [num_actual_pages].

_get_decode_mask(q_len_per_req, ragged_q_lens, num_decodes, causal)

Return the packed XQA draft mask for speculative decode.

_uses_trtllm_gen_varlen_decode(vllm_config, decode_kernel, use_dcp) staticmethod

Whether decode reads per-request query lengths from the device qo_indptr. Adaptive verification trims drafts on device, and only trtllm-gen supports that (the selector already rejects the other configurations).

get_cudagraph_support(vllm_config, kv_cache_spec) classmethod

Get the cudagraph support level for FlashInfer attention.

FlashInferTrtllmAPIDecode dataclass

Metadata for XQA and trtllm-gen decode.

Attributes:

  • block_tables (Tensor) –

    The slice of the block table tensor corresponding only to decode requests.

  • dcp_query_start_loc (Tensor | None) –

    Decode query boundaries for masking empty DCP shards during reduction.

  • mask (Tensor | None) –

    Packed XQA draft mask.

  • max_seq_len (int) –

    The maximum sequence length for KV Cache.

  • q_cu_seq_lens (Tensor | None) –

    Cumulative query lengths for ragged XQA decode.

  • q_len_per_req (int) –

    Query tokens per request for uniform speculative decode.

  • seq_lens (Tensor) –

    The slice of the sequence lengths tensor corresponding only to decode requests.

block_tables instance-attribute

The slice of the block table tensor corresponding only to decode requests. Shape: [num_decodes, max_num_blocks_per_seq]

dcp_query_start_loc = None class-attribute instance-attribute

Decode query boundaries for masking empty DCP shards during reduction.

mask = None class-attribute instance-attribute

Packed XQA draft mask.

max_seq_len instance-attribute

The maximum sequence length for KV Cache.

q_cu_seq_lens = None class-attribute instance-attribute

Cumulative query lengths for ragged XQA decode.

q_len_per_req = 1 class-attribute instance-attribute

Query tokens per request for uniform speculative decode.

seq_lens instance-attribute

The slice of the sequence lengths tensor corresponding only to decode requests. Shape: [num_decodes]

TRTLLMPrefill dataclass

Metadata for the TRTLLM prefill pathway.

Attributes:

block_tables instance-attribute

The slice of the block table tensor corresponding only to prefill requests. Shape: [num_prefills, max_num_blocks_per_seq]

max_q_len instance-attribute

The maximum query length among prefill requests.

max_seq_len instance-attribute

The maximum sequence length for KV Cache.

seq_lens instance-attribute

The slice of the sequence lengths tensor corresponding only to prefill requests. Shape: [num_prefills]

token_causal = None class-attribute instance-attribute

Per-token causal flags (mixed-causal only).

variable_window_bounds = None class-attribute instance-attribute

Per-token KV bounds (rows: starts, ends) by window_left; mixed-causal only.

TrtllmDCPContextWrapper

trtllm-gen stand-in for the native wrapper in the DCP context pass.

_fill_mixed_causal_metadata(query_start_loc, seq_lens, causal, num_tokens, window_bounds, token_causal)

Fill per-token mixed-causal metadata in place without a host sync.

window_bounds[w] rows get KV starts and ends: [pos - w, pos] if causal, else [pos - w, pos + w]. token_causal gets the causal flags. Padding tokens get [0, 0] and False.

_get_multi_ctas_kv_counter_buffer(min_bytes, device)

Shared multi-CTA-KV counter buffer; the kernels re-zero it after each launch.

_make_xqa_draft_block_mask(q_len, causal, device)

Build a packed XQA draft mask for a uniform request.

_make_xqa_ragged_draft_block_mask(q_lens, max_q_len, causal, device)

Build a packed XQA draft mask for a ragged batch from host q_lens.

_native_mm_prefix(vllm_config)

Whether FlashInfer serves this model's mm-prefix ranges itself; on SM100 only when FLASHINFER is selected explicitly.

_nvfp4_kv_views(kv_cache, num_kv_heads)

Views of NVFP4 pages laid out as [K data | K scale | V data | V scale].

_pack_draft_block_bool_mask(bool_mask, num_packed)

Pack a boolean draft mask into XQA's uint16 layout.

fast_plan_decode(self, indptr_cpu, indices, last_page_len_cpu, seq_lens_cpu, num_qo_heads, num_kv_heads, head_dim, page_size, pos_encoding_mode='NONE', window_left=-1, logits_soft_cap=None, q_data_type='float16', kv_data_type=None, o_data_type=None, data_type=None, sm_scale=None, rope_scale=None, rope_theta=None, non_blocking=True, fixed_split_size=-1, disable_split_kv=False, q_len_per_req=1)

A faster version of BatchDecodeWithPagedKVCacheWrapper::plan used for cudagraph capture/replay, while the no cudagraph version turns back to the original plan. using original plan after passing host-side buffers: - only host-to-device copy of indptr and last_page_len buffers Modifications for cudagraph: - only host-to-device copy of indptr and last_page_len buffers. - avoid device-to-device copy of indices buffer.

Part of the code get inspiration from the original plan from FlashInfer repo and the implementation of fast_decode_plan for FlashInfer in SGlang repo.