[Kernel] Gemma-4 FA4 FP8 Kernel (#48666)
Signed-off-by: Qixiang Lin <qixiangl@nvidia.com> Signed-off-by: Jhao-Ting Chen <jhaotingc@nvidia.com> Co-authored-by: Qixiang Lin <qixiangl@nvidia.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Yongye Zhu <zyy1102000@gmail.com>
This commit is contained in:
co-authored by
Qixiang Lin
Claude Opus 4.8
Yongye Zhu
parent
38f097fab8
commit
827a2af806
@@ -69,6 +69,13 @@ class AttentionBackend(ABC):
|
||||
def get_supported_kernel_block_sizes() -> list[int | MultipleOf]:
|
||||
return [MultipleOf(1)]
|
||||
|
||||
@classmethod
|
||||
def get_supported_kernel_block_sizes_for_config(
|
||||
cls, vllm_config: "VllmConfig"
|
||||
) -> list[int | MultipleOf]:
|
||||
"""Return kernel block sizes for a concrete engine configuration."""
|
||||
return cls.get_supported_kernel_block_sizes()
|
||||
|
||||
@staticmethod
|
||||
@abstractmethod
|
||||
def get_name() -> str:
|
||||
@@ -201,6 +208,13 @@ class AttentionBackend(ABC):
|
||||
|
||||
return min(s.base if isinstance(s, MultipleOf) else s for s in supported_sizes)
|
||||
|
||||
@classmethod
|
||||
def get_preferred_block_size_for_config(
|
||||
cls, default_block_size: int, vllm_config: "VllmConfig"
|
||||
) -> int:
|
||||
"""Return the preferred block size for a concrete engine config."""
|
||||
return cls.get_preferred_block_size(default_block_size)
|
||||
|
||||
@classmethod
|
||||
def indexes_kv_by_block_stride(cls) -> bool:
|
||||
"""Whether the backend reads KV pages by the runtime block stride.
|
||||
|
||||
@@ -241,9 +241,9 @@ def flash_attn_supports_kv_cache_dtype(
|
||||
head_size_v=head_size_v,
|
||||
has_sinks=has_sinks,
|
||||
)
|
||||
return (fa_version == 3 and current_platform.is_device_capability_family(90)) or (
|
||||
fa_version == 4 and current_platform.is_device_capability_family(100)
|
||||
)
|
||||
return (
|
||||
fa_version in (3, 4) and current_platform.is_device_capability_family(90)
|
||||
) or (fa_version == 4 and current_platform.is_device_capability_family(100))
|
||||
|
||||
|
||||
def flash_attn_supports_quant_query_input() -> bool:
|
||||
|
||||
@@ -85,13 +85,58 @@ class FlashAttentionBackend(AttentionBackend):
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def get_supported_kernel_block_sizes() -> list[int | MultipleOf]:
|
||||
def _get_sm90_fa4_fp8_kv_block_size(
|
||||
vllm_config: VllmConfig | None = None,
|
||||
) -> int | None:
|
||||
if vllm_config is 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 (
|
||||
current_platform.is_device_capability_family(90)
|
||||
and vllm_config.cache_config.cache_dtype in ("fp8", "fp8_e4m3")
|
||||
and head_size == 512
|
||||
and get_flash_attn_version(head_size=head_size) == 4
|
||||
):
|
||||
# The SM90 FP8-KV-dequant kernel uses a 64-token TMA tile/page.
|
||||
return 64
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def get_supported_kernel_block_sizes(cls) -> list[int | MultipleOf]:
|
||||
if block_size := cls._get_sm90_fa4_fp8_kv_block_size():
|
||||
# Sliding-window cache specs select the smallest advertised size.
|
||||
# Report the kernel's exact page-size contract instead of the
|
||||
# generic FlashAttention multiple-of-16 capability.
|
||||
return [block_size]
|
||||
return [MultipleOf(16)]
|
||||
|
||||
@classmethod
|
||||
def get_supported_kernel_block_sizes_for_config(
|
||||
cls, vllm_config: VllmConfig
|
||||
) -> list[int | MultipleOf]:
|
||||
if block_size := cls._get_sm90_fa4_fp8_kv_block_size(vllm_config):
|
||||
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_sm90_fa4_fp8_kv_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)
|
||||
|
||||
@classmethod
|
||||
def get_preferred_block_size_for_config(
|
||||
cls, default_block_size: int, vllm_config: VllmConfig
|
||||
) -> int:
|
||||
if block_size := cls._get_sm90_fa4_fp8_kv_block_size(vllm_config):
|
||||
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)
|
||||
@@ -895,7 +940,17 @@ class FlashAttentionImpl(AttentionImpl):
|
||||
"heads in the layer"
|
||||
)
|
||||
|
||||
self.supports_quant_query_input = flash_attn_supports_quant_query_input()
|
||||
# FA4's SM90 FP8-KV path consumes native FP16/BF16 Q and dequantizes
|
||||
# FP8 K/V in-kernel. Other FA4 paths (notably SM100) still require Q,
|
||||
# K, and V to have the same FP8 dtype.
|
||||
uses_sm90_fa4_fp8_kv_dequant = (
|
||||
self.vllm_flash_attn_version == 4
|
||||
and current_platform.is_device_capability_family(90)
|
||||
and self.kv_cache_dtype in ("fp8", "fp8_e4m3")
|
||||
)
|
||||
self.supports_quant_query_input = flash_attn_supports_quant_query_input() and (
|
||||
not uses_sm90_fa4_fp8_kv_dequant
|
||||
)
|
||||
|
||||
vllm_config = get_current_vllm_config_or_none()
|
||||
dcp_a2a = (
|
||||
|
||||
Reference in New Issue
Block a user