[FlexAttention] Avoid encoder block-mask compile explosion (#50339)

Signed-off-by: Andreas Karatzas <akaratza@amd.com>
This commit is contained in:
Andreas Karatzas
2026-07-30 11:24:13 -04:00
committed by GitHub
parent 30b4e7f479
commit 5b95890742
4 changed files with 66 additions and 13 deletions
+5 -3
View File
@@ -89,13 +89,15 @@ class AttentionConfig:
flex_attn_q_block_size: int | None = None
"""Logical Q block size for the flex attention block mask.
Must be a power of 2 and divisible by flex_attn_block_m.
If None, uses the default (16 on PyTorch >= 2.9, 128 otherwise)."""
If None, uses 16 for paged KV attention on PyTorch >= 2.9, and 128
for encoder-only attention or older PyTorch versions."""
flex_attn_kv_block_size: int | None = None
"""Logical KV block size for the flex attention block mask.
Must be a power of 2 and divisible by flex_attn_block_n.
If None, uses the default (kv_cache_block_size on PyTorch >= 2.9,
128 otherwise)."""
If None, uses the KV cache block size for paged KV attention on
PyTorch >= 2.9, and 128 for encoder-only attention or older PyTorch
versions."""
def compute_hash(self) -> str:
"""
+12 -2
View File
@@ -877,12 +877,14 @@ class FlexAttentionMetadataBuilder(AttentionMetadataBuilder[FlexAttentionMetadat
self.block_size = kv_cache_spec.block_size
self.kv_cache_spec = kv_cache_spec
supports_small_blocks = is_torch_equal_or_newer("2.9.0.dev0")
uses_paged_kv = not isinstance(kv_cache_spec, EncoderOnlyAttentionSpec)
self.direct_build: bool = supports_small_blocks
self.q_block_size, self.kv_block_size = self._get_block_sizes(
vllm_config.attention_config,
supports_small_blocks,
self.block_size,
uses_paged_kv,
)
if self.direct_build and self.kv_block_size != self.block_size:
@@ -945,9 +947,17 @@ class FlexAttentionMetadataBuilder(AttentionMetadataBuilder[FlexAttentionMetadat
attn_cfg,
supports_small_blocks: bool,
cache_block_size: int,
uses_paged_kv: bool,
) -> tuple[int, int]:
q_block_size = 16 if supports_small_blocks else 128
kv_block_size = cache_block_size if supports_small_blocks else 128
# Small blocks are efficient with the direct mask builder, which maps
# logical blocks to paged KV cache blocks without materializing and
# sorting a generic block mask. Encoder-only attention has no paged KV
# cache and therefore cannot use that path. Keeping its generic path at
# 16-token blocks creates very large mask-conversion graphs and makes
# Inductor compilation dominate the first request.
use_small_blocks = supports_small_blocks and uses_paged_kv
q_block_size = 16 if use_small_blocks else 128
kv_block_size = cache_block_size if use_small_blocks else 128
q_block_size = attn_cfg.flex_attn_q_block_size or q_block_size
if (q_block_size & (q_block_size - 1)) != 0 or (