[SM100] Hdim 256 optimized (#52980)
Signed-off-by: Simon Veitner <sveitner@redhat.com>
This commit is contained in:
@@ -6,9 +6,13 @@ from typing import Any
|
||||
import vllm.envs as envs
|
||||
from vllm.logger import init_logger
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.utils.torch_utils import is_quantized_kv_cache
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
# The dedicated hd256 kernel requires one 128-token page per tile.
|
||||
FA4_HD256_PAGE_SIZE = 128
|
||||
|
||||
# Track whether upstream flash-attn is available on ROCm.
|
||||
# Set during module initialization and never modified afterwards.
|
||||
# This module-level flag avoids repeated import attempts and ensures
|
||||
@@ -72,6 +76,9 @@ def get_flash_attn_version(
|
||||
head_size: int | None = None,
|
||||
head_size_v: int | None = None,
|
||||
has_sinks: bool = False,
|
||||
requires_softcap: bool = False,
|
||||
kv_cache_block_size: int | None = None,
|
||||
supports_fa4_hd256: bool = False,
|
||||
) -> int | None:
|
||||
if current_platform.is_xpu():
|
||||
return 2
|
||||
@@ -168,26 +175,31 @@ def get_flash_attn_version(
|
||||
)
|
||||
fa_version = 2
|
||||
|
||||
# TODO: Restore the `requires_local_attention` restriction when FA4
|
||||
# head-dim 256 is re-enabled.
|
||||
if fa_version == 4 and device_capability.major >= 10 and head_size == 256:
|
||||
logger.warning_once(
|
||||
"FA4 on Blackwell is temporarily disabled for head_size=256, "
|
||||
"defaulting to FA version 2."
|
||||
)
|
||||
fa_version = 2
|
||||
if fa_version == 4 and uses_fa4_hd256_kernel(head_size, head_size_v):
|
||||
if not supports_fa4_hd256:
|
||||
fa_version = 2
|
||||
elif (
|
||||
reason := _fa4_hd256_fallback_reason(
|
||||
has_sinks, requires_softcap, kv_cache_block_size, vllm_config
|
||||
)
|
||||
) is not None:
|
||||
logger.warning_once(
|
||||
"FA4's Blackwell head_size=256 kernel does not support %s, "
|
||||
"defaulting to FA version 2.",
|
||||
reason,
|
||||
)
|
||||
fa_version = 2
|
||||
|
||||
# FA4 on SM100 (Blackwell) has TMEM capacity limits that restrict
|
||||
# supported head dimensions to ≤128. The 192/128 MLA prefill case is
|
||||
# supported; 256 is temporarily disabled until upstream supports the
|
||||
# required features. Development of symmetric 192, 384, and 512 support
|
||||
# is tracked in https://github.com/Dao-AILab/flash-attention/issues/2456
|
||||
# FA4 head dimensions on Blackwell are limited by TMEM capacity.
|
||||
if (
|
||||
fa_version == 4
|
||||
and device_capability.major >= 10
|
||||
and head_size is not None
|
||||
and head_size > 128
|
||||
and not (head_size == 192 and head_size_v == 128)
|
||||
and not (
|
||||
(head_size == 256 and head_size_v in (None, 256))
|
||||
or (head_size == 192 and head_size_v == 128)
|
||||
)
|
||||
):
|
||||
logger.warning_once(
|
||||
"FA4 on Blackwell does not support head_size=%d due to TMEM "
|
||||
@@ -209,6 +221,53 @@ def get_flash_attn_version(
|
||||
return None
|
||||
|
||||
|
||||
def uses_fa4_hd256_kernel(
|
||||
head_size: int | None, head_size_v: int | None = None
|
||||
) -> bool:
|
||||
"""Return whether FA4 uses its dedicated hd256 kernel."""
|
||||
if head_size != 256:
|
||||
return False
|
||||
if head_size_v is not None and head_size_v != 256:
|
||||
return False
|
||||
capability = current_platform.get_device_capability()
|
||||
return capability is not None and capability.major in (10, 11)
|
||||
|
||||
|
||||
def _fa4_hd256_fallback_reason(
|
||||
has_sinks: bool,
|
||||
requires_softcap: bool,
|
||||
kv_cache_block_size: int | None,
|
||||
vllm_config: Any,
|
||||
) -> str | None:
|
||||
model_config = vllm_config.model_config if vllm_config is not None else None
|
||||
cache_config = vllm_config.cache_config if vllm_config is not None else None
|
||||
if has_sinks:
|
||||
return "attention sinks"
|
||||
if requires_softcap or (
|
||||
# Keep model-level and per-layer version selection consistent.
|
||||
model_config is not None
|
||||
and getattr(model_config.hf_text_config, "attn_logit_softcapping", None)
|
||||
):
|
||||
return "logits soft capping"
|
||||
if cache_config is not None and is_quantized_kv_cache(cache_config.cache_dtype):
|
||||
return f"quantized KV cache dtype {cache_config.cache_dtype}"
|
||||
if kv_cache_block_size is not None and kv_cache_block_size % FA4_HD256_PAGE_SIZE:
|
||||
# Larger blocks are split into 128-token kernel pages.
|
||||
return f"a KV cache block size of {kv_cache_block_size}"
|
||||
if model_config is not None:
|
||||
if model_config.is_mm_prefix_lm:
|
||||
return "mm_prefix bidirectional attention"
|
||||
if model_config.rswa_window is not None:
|
||||
return "R-SWA"
|
||||
if (
|
||||
vllm_config is not None
|
||||
and vllm_config.parallel_config.decode_context_parallel_size > 1
|
||||
):
|
||||
# DCP reuses an unsliced block table with a non-page-aligned length.
|
||||
return "decode context parallelism"
|
||||
return None
|
||||
|
||||
|
||||
def is_fa_version_supported(fa_version: int) -> bool:
|
||||
try:
|
||||
from vllm.vllm_flash_attn.flash_attn_interface import (
|
||||
@@ -227,6 +286,9 @@ def flash_attn_supports_kv_cache_dtype(
|
||||
head_size: int | None = None,
|
||||
head_size_v: int | None = None,
|
||||
has_sinks: bool = False,
|
||||
requires_softcap: bool = False,
|
||||
kv_cache_block_size: int | None = None,
|
||||
supports_fa4_hd256: bool = False,
|
||||
) -> bool:
|
||||
if kv_cache_dtype == "fp8_e5m2":
|
||||
return False
|
||||
@@ -237,6 +299,9 @@ def flash_attn_supports_kv_cache_dtype(
|
||||
head_size=head_size,
|
||||
head_size_v=head_size_v,
|
||||
has_sinks=has_sinks,
|
||||
requires_softcap=requires_softcap,
|
||||
kv_cache_block_size=kv_cache_block_size,
|
||||
supports_fa4_hd256=supports_fa4_hd256,
|
||||
)
|
||||
return (fa_version == 3 and current_platform.is_device_capability_family(90)) or (
|
||||
fa_version == 4 and current_platform.is_device_capability_family(100)
|
||||
|
||||
@@ -24,11 +24,13 @@ from vllm.v1.attention.backend import (
|
||||
MultipleOf,
|
||||
)
|
||||
from vllm.v1.attention.backends.fa_utils import (
|
||||
FA4_HD256_PAGE_SIZE,
|
||||
flash_attn_supports_kv_cache_dtype,
|
||||
flash_attn_supports_quant_query_input,
|
||||
get_flash_attn_version,
|
||||
is_fa_version_supported,
|
||||
is_flash_attn_varlen_func_available,
|
||||
uses_fa4_hd256_kernel,
|
||||
)
|
||||
from vllm.v1.attention.backends.utils import (
|
||||
fill_mm_prefix_query_ranges,
|
||||
@@ -85,15 +87,40 @@ class FlashAttentionBackend(AttentionBackend):
|
||||
"fp8",
|
||||
"fp8_e4m3",
|
||||
]
|
||||
head_size_v: int | None = None
|
||||
|
||||
@staticmethod
|
||||
def get_supported_kernel_block_sizes() -> list[int | MultipleOf]:
|
||||
@classmethod
|
||||
def _get_fa4_hd256_block_size(cls) -> int | None:
|
||||
vllm_config = get_current_vllm_config_or_none()
|
||||
if vllm_config is None or vllm_config.model_config is None:
|
||||
return None
|
||||
|
||||
head_size = vllm_config.model_config.get_head_size()
|
||||
if (
|
||||
uses_fa4_hd256_kernel(head_size, cls.head_size_v)
|
||||
and get_flash_attn_version(
|
||||
head_size=head_size,
|
||||
head_size_v=cls.head_size_v,
|
||||
supports_fa4_hd256=True,
|
||||
)
|
||||
== 4
|
||||
):
|
||||
return FA4_HD256_PAGE_SIZE
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def get_supported_kernel_block_sizes(cls) -> list[int | MultipleOf]:
|
||||
if block_size := cls._get_fa4_hd256_block_size():
|
||||
# Sliding-window specs select the smallest advertised size.
|
||||
return [block_size]
|
||||
return [MultipleOf(16)]
|
||||
|
||||
forward_includes_kv_cache_update: bool = False
|
||||
|
||||
@classmethod
|
||||
def get_preferred_block_size(cls, default_block_size: int) -> int:
|
||||
if block_size := cls._get_fa4_hd256_block_size():
|
||||
return max(default_block_size, block_size)
|
||||
if current_platform.is_xpu():
|
||||
return max(default_block_size, 64)
|
||||
return super().get_preferred_block_size(default_block_size)
|
||||
@@ -194,12 +221,20 @@ class FlashAttentionBackend(AttentionBackend):
|
||||
head_size=head_size,
|
||||
head_size_v=head_size,
|
||||
has_sinks=has_sink,
|
||||
kv_cache_block_size=block_size,
|
||||
supports_fa4_hd256=True,
|
||||
)
|
||||
):
|
||||
return "FP8 KV cache requires FA3 on SM90 or FA4 on SM100"
|
||||
if (
|
||||
use_mm_prefix
|
||||
and get_flash_attn_version(head_size=head_size, has_sinks=has_sink) != 4
|
||||
and get_flash_attn_version(
|
||||
head_size=head_size,
|
||||
has_sinks=has_sink,
|
||||
kv_cache_block_size=block_size,
|
||||
supports_fa4_hd256=True,
|
||||
)
|
||||
!= 4
|
||||
):
|
||||
return (
|
||||
"mm_prefix (PrefixLM bidirectional attention) requires "
|
||||
@@ -409,6 +444,15 @@ class FlashAttentionMetadataBuilder(AttentionMetadataBuilder[FlashAttentionMetad
|
||||
self.max_num_splits = 0 # No upper bound on the number of splits.
|
||||
self.aot_schedule = get_flash_attn_version() == 3
|
||||
|
||||
self.fa4_hd256 = uses_fa4_hd256_kernel(self.headdim) and (
|
||||
get_flash_attn_version(
|
||||
head_size=self.headdim,
|
||||
kv_cache_block_size=self.block_size,
|
||||
supports_fa4_hd256=True,
|
||||
)
|
||||
== 4
|
||||
)
|
||||
|
||||
try:
|
||||
from vllm.distributed.parallel_state import get_dcp_group
|
||||
|
||||
@@ -780,6 +824,9 @@ class FlashAttentionMetadataBuilder(AttentionMetadataBuilder[FlashAttentionMetad
|
||||
metadata.scheduler_metadata = self._store_scheduler_metadata(scheduler_metadata)
|
||||
|
||||
def use_cascade_attention(self, *args, **kwargs) -> bool:
|
||||
if self.fa4_hd256:
|
||||
# Cascade may use a non-page-aligned prefix length.
|
||||
return False
|
||||
return use_cascade_attention(*args, **kwargs)
|
||||
|
||||
|
||||
@@ -823,11 +870,30 @@ class FlashAttentionImpl(AttentionImpl):
|
||||
self.num_queries_per_kv = self.num_heads // self.num_kv_heads
|
||||
|
||||
self.attn_type = attn_type
|
||||
vllm_config = get_current_vllm_config_or_none()
|
||||
uses_kv_cache = attn_type not in (
|
||||
AttentionType.ENCODER,
|
||||
AttentionType.ENCODER_ONLY,
|
||||
)
|
||||
# The final KV cache block size is unavailable during construction.
|
||||
self.vllm_flash_attn_version = get_flash_attn_version(
|
||||
requires_alibi=alibi_slopes is not None,
|
||||
head_size=head_size,
|
||||
has_sinks=sinks is not None,
|
||||
requires_softcap=bool(self.logits_soft_cap),
|
||||
supports_fa4_hd256=True,
|
||||
)
|
||||
self.fa4_hd256 = self.vllm_flash_attn_version == 4 and uses_fa4_hd256_kernel(
|
||||
head_size
|
||||
)
|
||||
if self.fa4_hd256 and not uses_kv_cache and sliding_window is not None:
|
||||
# The hd256 kernel requires seqused_k for local attention.
|
||||
logger.warning_once(
|
||||
"FA4's Blackwell head_size=256 kernel does not support local "
|
||||
"attention on encoder inputs, defaulting to FA version 2."
|
||||
)
|
||||
self.vllm_flash_attn_version = 2
|
||||
self.fa4_hd256 = False
|
||||
logger.info_once(
|
||||
"Using FlashAttention version %s",
|
||||
self.vllm_flash_attn_version,
|
||||
@@ -843,6 +909,8 @@ class FlashAttentionImpl(AttentionImpl):
|
||||
head_size=head_size,
|
||||
head_size_v=head_size,
|
||||
has_sinks=sinks is not None,
|
||||
requires_softcap=bool(self.logits_soft_cap),
|
||||
supports_fa4_hd256=True,
|
||||
):
|
||||
raise NotImplementedError(
|
||||
f"FlashAttention does not support {self.kv_cache_dtype}"
|
||||
@@ -861,7 +929,6 @@ class FlashAttentionImpl(AttentionImpl):
|
||||
|
||||
self.supports_quant_query_input = flash_attn_supports_quant_query_input()
|
||||
|
||||
vllm_config = get_current_vllm_config_or_none()
|
||||
dcp_a2a = (
|
||||
vllm_config is not None
|
||||
and vllm_config.parallel_config.decode_context_parallel_size > 1
|
||||
@@ -1083,6 +1150,15 @@ class FlashAttentionImpl(AttentionImpl):
|
||||
)
|
||||
causal = not has_window
|
||||
|
||||
num_splits = attn_metadata.max_num_splits
|
||||
if self.fa4_hd256:
|
||||
# hd256 requires page-aligned lengths, exact-width block
|
||||
# tables, and no SplitKV.
|
||||
num_pages = cdiv(max_seqlen_k, FA4_HD256_PAGE_SIZE)
|
||||
max_seqlen_k = num_pages * FA4_HD256_PAGE_SIZE
|
||||
block_table = block_table[:, :num_pages]
|
||||
num_splits = 1
|
||||
|
||||
flash_attn_varlen_func(
|
||||
q=query[:num_actual_tokens],
|
||||
k=key_cache,
|
||||
@@ -1104,7 +1180,7 @@ class FlashAttentionImpl(AttentionImpl):
|
||||
k_descale=k_descale,
|
||||
v_descale=v_descale,
|
||||
dynamic_causal=dynamic_causal,
|
||||
num_splits=attn_metadata.max_num_splits,
|
||||
num_splits=num_splits,
|
||||
s_aux=self.sinks,
|
||||
mask_mod=rswa_mask_mod_fn or mm_mask_mod,
|
||||
aux_tensors=rswa_aux or mm_aux,
|
||||
@@ -1416,7 +1492,8 @@ class FlashAttentionImpl(AttentionImpl):
|
||||
else None,
|
||||
k_descale=layer._k_scale.expand(descale_shape), # type: ignore[operator]
|
||||
v_descale=layer._v_scale.expand(descale_shape), # type: ignore[operator]
|
||||
num_splits=1 if self.batch_invariant_enabled else 0,
|
||||
# The hd256 kernel does not support SplitKV.
|
||||
num_splits=1 if self.batch_invariant_enabled or self.fa4_hd256 else 0,
|
||||
s_aux=self.sinks,
|
||||
)
|
||||
|
||||
|
||||
@@ -87,6 +87,7 @@ class FlashAttentionDiffKVImpl(FlashAttentionImpl):
|
||||
head_size_v=FlashAttentionDiffKVBackend.head_size_v,
|
||||
has_sinks=self.sinks is not None,
|
||||
)
|
||||
self.fa4_hd256 = False
|
||||
|
||||
def do_kv_cache_update(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user