vllm.v1.attention.backends.flash_attn
¶
Attention layer with FlashAttention.
Classes:
-
FA4DenseAttentionKernel–Warm paged, causal FA4 on Blackwell and Hopper.
-
FlashAttentionBackend– -
FlashAttentionImpl– -
FlashAttentionMetadataBuilder–
FA4DenseAttentionKernel
¶
Bases: VllmJitKernel['FA4DenseAttentionKernel.CompileKey']
Warm paged, causal FA4 on Blackwell and Hopper.
FlashAttentionBackend
¶
Bases: AttentionBackend
Methods:
-
supports_attn_type–FlashAttention supports all attention types.
supports_attn_type(attn_type)
classmethod
¶
FlashAttention supports all attention types.
FlashAttentionImpl
¶
Bases: AttentionImpl
Methods:
-
forward–Forward pass with FlashAttention.
_forward_encoder_attention(query, key, value, output, attn_metadata, layer)
¶
Forward pass for encoder attention without KV cache.
Parameters:
-
(query¶Tensor) –shape = [num_encoder_tokens, num_heads, head_size]
-
(key¶Tensor) –shape = [num_encoder_tokens, num_kv_heads, head_size]
-
(value¶Tensor) –shape = [num_encoder_tokens, num_kv_heads, head_size]
-
(output¶Tensor) –shape = [num_encoder_tokens, num_heads, head_size]
-
(attn_metadata¶FlashAttentionMetadata) –Encoder attention metadata
-
(layer¶Module) –The attention layer
forward(layer, query, key, value, kv_cache, attn_metadata, output, output_scale=None, output_block_scale=None)
¶
Forward pass with FlashAttention.
Parameters:
-
(layer¶Module) –The attention layer, providing the q/k/v quantization scales.
-
(query¶Tensor) –shape = [num_tokens, num_heads, head_size]
-
(key¶Tensor) –shape = [num_tokens, num_kv_heads, head_size]
-
(value¶Tensor) –shape = [num_tokens, num_kv_heads, head_size]
-
(kv_cache¶Tensor) –shape = [num_blocks, num_kv_heads, block_size, 2 * head_size]
-
(attn_metadata¶FlashAttentionMetadata) –Metadata for attention.
-
(output¶Tensor) –Tensor that the attention result is written into.
-
(output_scale¶Tensor | None, default:None) –Scale for fused output quantization; not supported by this backend.
-
(output_block_scale¶Tensor | None, default:None) –Block scale for fused output quantization; not supported by this backend.
Returns:
-
Tensor–shape = [num_tokens, num_heads * head_size]
NOTE: FP8 quantization, flash-attn expect the size of {q,k,v}_descale to be (num_sequences, num_kv_heads). We use torch's .expand() to avoid duplicating values
FlashAttentionMetadataBuilder
¶
Bases: AttentionMetadataBuilder[FlashAttentionMetadata]
Methods:
-
build–fast_build disables AOT scheduling, used when there will be few
build(common_prefix_len, common_attn_metadata, fast_build=False)
¶
fast_build disables AOT scheduling, used when there will be few iterations i.e. spec-decode
_get_sliding_window_configs(vllm_config)
¶
Get the set of all sliding window configs used in the model.
Only inspects FlashAttentionImpl layers, including implementations wrapped by a composite backend. Other backends (e.g. TurboQuant, MLA) use their own metadata builders and are skipped.
_make_mm_prefix_mask_mod(sliding_window=0, sliding_window_left=None)
cached
¶
Build a CuTE-DSL mask_mod implementing
(causal AND sliding_window) OR mm_prefix.
Cached so identical (sliding_window, sliding_window_left) reuse the
same function object. FA4's hash_callable mixes repr() of closure
cells into the compile key; the nested _load_q_range would otherwise
get a new address each call and force a full JIT recompile every forward.
The FA4 kernel passes local q_idx (0-based within the current
prefill chunk) while kv_idx is absolute (0-based over the full
KV cache). We recover the absolute Q position via
q_abs = q_idx + seqlen_k - seqlen_q (the context-length offset)
so that causal, sliding-window, and mm_prefix range comparisons all
use consistent absolute positions. This matches the Triton
reference path (compute_kv_seq_mask).
aux_tensors[0] holds the absolute [start, end] bounds of the
mm_prefix range containing each scheduled query token ((-1, -1) when
none), and aux_tensors[1] is cu_seqlens_q, used to turn the local
q_idx into a packed row index. Because mm_prefix ranges never overlap,
r_start <= kv_idx <= r_end is exactly "query and key share a range", so
no key-side lookup is needed and kv_idx is never used as an index.
The (-1, -1) sentinel falls out for free: kv_idx <= -1 is false for
every valid key.
sliding_window_left enforces the sliding window on the causal
term (None = full causal, no window). sliding_window clamps the
bidirectional block to the window (0 = unclamped; >0 = Gemma4 local
layers via mm_prefix_clamp_sliding_window).
_make_rswa_mask_mod()
¶
Build a CuTE-DSL mask_mod for Reference Sliding Window Attention (R-SWA).
FA4 varlen + paged-KV convention (verified from cute/mask.py apply_mask): q_idx = LOCAL query-token offset (0 .. seqlen_q - 1) within this sequence. kv_idx = LOCAL KV-token position (0 .. seqlen_k - 1) within this sequence.
To recover the ABSOLUTE token position (needed for causal and the sliding window distance), use the standard offset: abs_q = q_idx + (seqlen_k - seqlen_q)
R-SWA keep condition
abs_q >= kv_idx (causal: KV at or before the query) AND (kv_idx < prefix_len (global prefix is always visible) OR abs_q - kv_idx < window) (generated tokens: sliding window)
aux_tensors[0]: prefix_lens [num_reqs] int32 — per-request prefill length. aux_tensors[1]: rswa_window [1] int32 — decode sliding window size.
use_fast_sampling=True lets FA4 skip fully-masked KV blocks (gap blocks) without loading their data.
_maybe_symmetrize_window(window, causal)
¶
Make a causal sliding window (w, 0) symmetric (w, w) when attention
is non-causal, so bidirectional queries attend in both directions. Leaves
full-attention (-1, -1) and already-symmetric windows untouched.