[Platform] Support platform-specific MLA prefill backend selection (#56959)
Signed-off-by: wangxiyuan <wangxiyuan1007@gmail.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent
fd928e5f3c
commit
072d49bb6e
@@ -18,6 +18,7 @@ from vllm.utils.cpu_resource_utils import (
|
||||
get_visible_memory_node,
|
||||
)
|
||||
from vllm.utils.mem_constants import GiB_bytes
|
||||
from vllm.v1.attention.backends.mla.prefill.registry import MLAPrefillBackendEnum
|
||||
from vllm.v1.attention.backends.registry import AttentionBackendEnum
|
||||
|
||||
from .interface import CpuArchEnum, Platform, PlatformEnum
|
||||
@@ -26,6 +27,10 @@ logger = init_logger(__name__)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.v1.attention.backends.mla.prefill.base import (
|
||||
MLAPrefillBackend,
|
||||
)
|
||||
from vllm.v1.attention.backends.mla.prefill.selector import MLAPrefillSelectorConfig
|
||||
from vllm.v1.attention.selector import AttentionSelectorConfig
|
||||
else:
|
||||
VllmConfig = None
|
||||
@@ -173,6 +178,21 @@ class CpuPlatform(Platform):
|
||||
logger.info("Cannot use %s backend on CPU.", selected_backend)
|
||||
return AttentionBackendEnum.CPU_ATTN.get_path()
|
||||
|
||||
@classmethod
|
||||
def get_mla_prefill_backend_cls(
|
||||
cls,
|
||||
mla_selector_config: "MLAPrefillSelectorConfig",
|
||||
) -> "type[MLAPrefillBackend]":
|
||||
"""Get the MLA prefill backend class of a device."""
|
||||
for backend_enum in (MLAPrefillBackendEnum.ZEN_CPU, MLAPrefillBackendEnum.CPU):
|
||||
try:
|
||||
cpu_backend_cls = backend_enum.get_class()
|
||||
except ImportError:
|
||||
continue
|
||||
if cpu_backend_cls.is_available():
|
||||
return cpu_backend_cls
|
||||
raise ValueError("No valid CPU MLA prefill backend found.")
|
||||
|
||||
@classmethod
|
||||
def get_device_total_memory(cls, device_id: int = 0) -> int:
|
||||
meminfo = get_memory_node_info(device_id)
|
||||
|
||||
@@ -26,6 +26,8 @@ if TYPE_CHECKING:
|
||||
from vllm.sampling_params import SamplingParams
|
||||
from vllm.utils.argparse_utils import FlexibleArgumentParser
|
||||
from vllm.v1.attention.backend import AttentionBackend
|
||||
from vllm.v1.attention.backends.mla.prefill.base import MLAPrefillBackend
|
||||
from vllm.v1.attention.backends.mla.prefill.selector import MLAPrefillSelectorConfig
|
||||
from vllm.v1.attention.selector import AttentionSelectorConfig
|
||||
else:
|
||||
FlexibleArgumentParser = object
|
||||
@@ -429,6 +431,88 @@ class Platform:
|
||||
)
|
||||
return AttentionBackendEnum.TORCH_SDPA
|
||||
|
||||
@classmethod
|
||||
def get_mla_prefill_backend_cls(
|
||||
cls,
|
||||
mla_selector_config: "MLAPrefillSelectorConfig",
|
||||
) -> "type[MLAPrefillBackend]":
|
||||
"""Get the MLA prefill backend class of a device.
|
||||
|
||||
Platforms that have a single, fixed MLA prefill backend can override
|
||||
this to return it directly. The default implementation selects the
|
||||
best backend from a device-capability-based priority list.
|
||||
|
||||
Returns:
|
||||
The selected backend class.
|
||||
|
||||
Raises:
|
||||
ValueError: If no MLA prefill backend is valid for the
|
||||
configuration.
|
||||
|
||||
"""
|
||||
from vllm.v1.attention.backends.mla.prefill.base import MLADimensions
|
||||
from vllm.v1.attention.backends.mla.prefill.registry import (
|
||||
MLAPrefillBackendEnum,
|
||||
)
|
||||
|
||||
device_capability = cls.get_device_capability()
|
||||
if device_capability is None:
|
||||
return MLAPrefillBackendEnum.FLASH_ATTN.get_class()
|
||||
|
||||
if device_capability.major == 10: # Blackwell
|
||||
if mla_selector_config.mla_dimensions == MLADimensions(
|
||||
qk_nope_head_dim=192,
|
||||
qk_rope_head_dim=64,
|
||||
v_head_dim=256,
|
||||
):
|
||||
priorities = [
|
||||
MLAPrefillBackendEnum.TRTLLM_RAGGED,
|
||||
MLAPrefillBackendEnum.FLASH_ATTN,
|
||||
MLAPrefillBackendEnum.FLASHINFER,
|
||||
MLAPrefillBackendEnum.TOKENSPEED_MLA,
|
||||
]
|
||||
else:
|
||||
priorities = [
|
||||
MLAPrefillBackendEnum.FLASH_ATTN,
|
||||
MLAPrefillBackendEnum.TRTLLM_RAGGED,
|
||||
MLAPrefillBackendEnum.FLASHINFER,
|
||||
MLAPrefillBackendEnum.TOKENSPEED_MLA,
|
||||
]
|
||||
else: # Hopper (SM90) and older
|
||||
priorities = [MLAPrefillBackendEnum.FLASH_ATTN]
|
||||
|
||||
all_invalid_reasons: dict[str, list[str]] = {}
|
||||
|
||||
for backend_enum in priorities:
|
||||
try:
|
||||
backend_cls = backend_enum.get_class()
|
||||
invalid_reasons = backend_cls.validate_configuration(
|
||||
device_capability, mla_selector_config
|
||||
)
|
||||
except ImportError:
|
||||
invalid_reasons = ["ImportError"]
|
||||
if not invalid_reasons:
|
||||
return backend_cls
|
||||
all_invalid_reasons[backend_enum.name] = invalid_reasons
|
||||
|
||||
reasons_str = (
|
||||
"{"
|
||||
+ ", ".join(
|
||||
f"{name}: [{', '.join(reasons)}]"
|
||||
for name, reasons in all_invalid_reasons.items()
|
||||
)
|
||||
+ "}"
|
||||
)
|
||||
logger.debug_once(
|
||||
"Some MLA prefill backends are not valid with %s. Reasons: %s.",
|
||||
repr(mla_selector_config),
|
||||
reasons_str,
|
||||
)
|
||||
raise ValueError(
|
||||
f"No valid MLA prefill backend found with {mla_selector_config!r}. "
|
||||
f"Reasons: {reasons_str}."
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_device_capability(
|
||||
cls,
|
||||
|
||||
@@ -24,6 +24,10 @@ if TYPE_CHECKING:
|
||||
from vllm.config.kernel import IrOpPriorityConfig
|
||||
from vllm.utils.argparse_utils import FlexibleArgumentParser
|
||||
from vllm.v1.attention.backend import AttentionBackend
|
||||
from vllm.v1.attention.backends.mla.prefill.base import MLAPrefillBackend
|
||||
from vllm.v1.attention.backends.mla.prefill.selector import (
|
||||
MLAPrefillSelectorConfig,
|
||||
)
|
||||
from vllm.v1.attention.selector import AttentionSelectorConfig
|
||||
|
||||
logger = init_logger(__name__)
|
||||
@@ -904,6 +908,57 @@ class RocmPlatform(Platform):
|
||||
logger.info_once("Using Torch SDPA backend for ViT model.")
|
||||
return AttentionBackendEnum.TORCH_SDPA
|
||||
|
||||
@classmethod
|
||||
def get_mla_prefill_backend_cls(
|
||||
cls,
|
||||
mla_selector_config: "MLAPrefillSelectorConfig",
|
||||
) -> "type[MLAPrefillBackend]":
|
||||
"""On ROCm, prefer the AITER FlashAttention backend, falling back to
|
||||
FlashAttention.
|
||||
|
||||
Raises:
|
||||
ValueError: If neither backend is valid for the configuration.
|
||||
|
||||
"""
|
||||
from vllm.v1.attention.backends.mla.prefill.registry import (
|
||||
MLAPrefillBackendEnum,
|
||||
)
|
||||
|
||||
device_capability = cls.get_device_capability()
|
||||
all_invalid_reasons: dict[str, list[str]] = {}
|
||||
for backend_enum in (
|
||||
MLAPrefillBackendEnum.ROCM_AITER_FA,
|
||||
MLAPrefillBackendEnum.FLASH_ATTN,
|
||||
):
|
||||
try:
|
||||
backend_cls = backend_enum.get_class()
|
||||
invalid_reasons = backend_cls.validate_configuration(
|
||||
device_capability, mla_selector_config
|
||||
)
|
||||
except ImportError:
|
||||
invalid_reasons = ["ImportError"]
|
||||
if not invalid_reasons:
|
||||
return backend_cls
|
||||
all_invalid_reasons[backend_enum.name] = invalid_reasons
|
||||
|
||||
reasons_str = (
|
||||
"{"
|
||||
+ ", ".join(
|
||||
f"{name}: [{', '.join(reasons)}]"
|
||||
for name, reasons in all_invalid_reasons.items()
|
||||
)
|
||||
+ "}"
|
||||
)
|
||||
logger.debug_once(
|
||||
"Some MLA prefill backends are not valid with %s. Reasons: %s.",
|
||||
repr(mla_selector_config),
|
||||
reasons_str,
|
||||
)
|
||||
raise ValueError(
|
||||
f"No valid MLA prefill backend found with {mla_selector_config!r}. "
|
||||
f"Reasons: {reasons_str}."
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def set_device(cls, device: torch.device) -> None:
|
||||
"""Set the device for the current platform."""
|
||||
|
||||
@@ -31,7 +31,10 @@ class AiterFlashAttnPrefillBackend(MLAPrefillBackend):
|
||||
return "ROCM_AITER_FA"
|
||||
|
||||
@classmethod
|
||||
def supports_compute_capability(cls, device_capability: "DeviceCapability") -> bool:
|
||||
def supports_compute_capability(
|
||||
cls,
|
||||
device_capability: "DeviceCapability | None",
|
||||
) -> bool:
|
||||
if not current_platform.is_rocm():
|
||||
return False
|
||||
from vllm.platforms.rocm import on_mi3xx
|
||||
|
||||
@@ -49,7 +49,10 @@ class MLAPrefillBackend(ABC):
|
||||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def supports_compute_capability(cls, device_capability: "DeviceCapability") -> bool:
|
||||
def supports_compute_capability(
|
||||
cls,
|
||||
device_capability: "DeviceCapability | None",
|
||||
) -> bool:
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
@@ -90,16 +93,19 @@ class MLAPrefillBackend(ABC):
|
||||
@classmethod
|
||||
def validate_configuration(
|
||||
cls,
|
||||
device_capability: "DeviceCapability",
|
||||
device_capability: "DeviceCapability | None",
|
||||
selector_config: "MLAPrefillSelectorConfig",
|
||||
) -> list[str]:
|
||||
invalid_reasons: list[str] = []
|
||||
|
||||
if not cls.supports_compute_capability(device_capability):
|
||||
invalid_reasons.append(
|
||||
f"compute capability {device_capability.major}."
|
||||
f"{device_capability.minor} not supported"
|
||||
)
|
||||
if device_capability is None:
|
||||
invalid_reasons.append("device capability not available")
|
||||
else:
|
||||
invalid_reasons.append(
|
||||
f"compute capability {device_capability.major}."
|
||||
f"{device_capability.minor} not supported"
|
||||
)
|
||||
|
||||
if not cls.supports_dtype(selector_config.dtype):
|
||||
invalid_reasons.append(f"dtype {selector_config.dtype} not supported")
|
||||
|
||||
@@ -51,8 +51,11 @@ class FlashInferPrefillBackend(MLAPrefillBackend):
|
||||
return "FLASHINFER"
|
||||
|
||||
@classmethod
|
||||
def supports_compute_capability(cls, device_capability: "DeviceCapability") -> bool:
|
||||
return device_capability.major == 10
|
||||
def supports_compute_capability(
|
||||
cls,
|
||||
device_capability: "DeviceCapability | None",
|
||||
) -> bool:
|
||||
return device_capability is not None and device_capability.major == 10
|
||||
|
||||
@classmethod
|
||||
def is_available(cls) -> bool:
|
||||
|
||||
@@ -12,9 +12,7 @@ from typing import TYPE_CHECKING, NamedTuple
|
||||
import torch
|
||||
|
||||
from vllm.logger import init_logger
|
||||
from vllm.platforms.interface import DeviceCapability
|
||||
from vllm.v1.attention.backends.mla.prefill.base import MLADimensions
|
||||
from vllm.v1.attention.backends.mla.prefill.registry import MLAPrefillBackendEnum
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.config import VllmConfig
|
||||
@@ -45,54 +43,8 @@ class MLAPrefillSelectorConfig(NamedTuple):
|
||||
)
|
||||
|
||||
|
||||
def _get_mla_prefill_backend_priorities(
|
||||
device_capability: DeviceCapability,
|
||||
mla_dimensions: MLADimensions,
|
||||
) -> list[MLAPrefillBackendEnum]:
|
||||
"""Get MLA prefill backend priorities based on device capability.
|
||||
|
||||
Args:
|
||||
device_capability: The device's compute capability.
|
||||
mla_dimensions: The model's MLA head dimensions.
|
||||
|
||||
Returns:
|
||||
List of backends in priority order (highest priority first).
|
||||
|
||||
"""
|
||||
from vllm.platforms import current_platform
|
||||
|
||||
if current_platform.is_rocm():
|
||||
return [
|
||||
MLAPrefillBackendEnum.ROCM_AITER_FA,
|
||||
MLAPrefillBackendEnum.FLASH_ATTN,
|
||||
]
|
||||
|
||||
if device_capability.major == 10: # Blackwell
|
||||
if mla_dimensions == MLADimensions(
|
||||
qk_nope_head_dim=192,
|
||||
qk_rope_head_dim=64,
|
||||
v_head_dim=256,
|
||||
):
|
||||
return [
|
||||
MLAPrefillBackendEnum.TRTLLM_RAGGED,
|
||||
MLAPrefillBackendEnum.FLASH_ATTN,
|
||||
MLAPrefillBackendEnum.FLASHINFER,
|
||||
MLAPrefillBackendEnum.TOKENSPEED_MLA,
|
||||
]
|
||||
return [
|
||||
MLAPrefillBackendEnum.FLASH_ATTN,
|
||||
MLAPrefillBackendEnum.TRTLLM_RAGGED,
|
||||
MLAPrefillBackendEnum.FLASHINFER,
|
||||
MLAPrefillBackendEnum.TOKENSPEED_MLA,
|
||||
]
|
||||
else: # Hopper (SM90) and older
|
||||
return [
|
||||
MLAPrefillBackendEnum.FLASH_ATTN,
|
||||
]
|
||||
|
||||
|
||||
def get_mla_prefill_backend(
|
||||
vllm_config: "VllmConfig",
|
||||
vllm_config: "VllmConfig | None",
|
||||
) -> "type[MLAPrefillBackend]":
|
||||
"""Select the MLA prefill backend based on configuration and device.
|
||||
|
||||
@@ -101,7 +53,8 @@ def get_mla_prefill_backend(
|
||||
priority-based selection.
|
||||
|
||||
Args:
|
||||
vllm_config: The vLLM configuration.
|
||||
vllm_config: The vLLM configuration. May be None before model
|
||||
resolution, in which case defaults are used.
|
||||
|
||||
Returns:
|
||||
The selected prefill backend class.
|
||||
@@ -109,30 +62,7 @@ def get_mla_prefill_backend(
|
||||
"""
|
||||
from vllm.platforms import current_platform
|
||||
|
||||
if current_platform.is_cpu():
|
||||
# CPUs have no compute capability, so the capability-driven priority
|
||||
# path below does not apply. Prefer an accelerator-specific CPU backend
|
||||
# when its kernels are present, else the generic SDPA one.
|
||||
for backend_enum in (MLAPrefillBackendEnum.ZEN_CPU, MLAPrefillBackendEnum.CPU):
|
||||
try:
|
||||
cpu_backend_cls = backend_enum.get_class()
|
||||
except ImportError:
|
||||
continue
|
||||
if cpu_backend_cls.is_available():
|
||||
logger.info_once("Using %s MLA prefill backend.", backend_enum.name)
|
||||
return cpu_backend_cls
|
||||
raise ValueError("No valid CPU MLA prefill backend found.")
|
||||
|
||||
device_capability = current_platform.get_device_capability()
|
||||
if device_capability is None:
|
||||
logger.info_once(
|
||||
"Device capability not available, using FlashAttention MLA prefill backend."
|
||||
)
|
||||
return MLAPrefillBackendEnum.FLASH_ATTN.get_class()
|
||||
|
||||
attention_config = vllm_config.attention_config
|
||||
|
||||
model_config = vllm_config.model_config
|
||||
model_config = vllm_config.model_config if vllm_config is not None else None
|
||||
if model_config is None:
|
||||
selector_config = MLAPrefillSelectorConfig(dtype=torch.get_default_dtype())
|
||||
else:
|
||||
@@ -146,11 +76,21 @@ def get_mla_prefill_backend(
|
||||
),
|
||||
)
|
||||
|
||||
if attention_config.mla_prefill_backend is not None:
|
||||
if current_platform.is_cpu():
|
||||
# CPUs have a single fixed MLA prefill backend, so explicit backend
|
||||
# configuration does not apply; always auto-select via the platform.
|
||||
return _auto_select_mla_prefill_backend(selector_config)
|
||||
|
||||
attention_config = vllm_config.attention_config if vllm_config is not None else None
|
||||
if (
|
||||
attention_config is not None
|
||||
and attention_config.mla_prefill_backend is not None
|
||||
):
|
||||
selected_backend = attention_config.mla_prefill_backend
|
||||
backend_cls: type[MLAPrefillBackend] | None = None
|
||||
try:
|
||||
backend_cls = selected_backend.get_class()
|
||||
device_capability = current_platform.get_device_capability()
|
||||
invalid_reasons = backend_cls.validate_configuration(
|
||||
device_capability, selector_config
|
||||
)
|
||||
@@ -166,63 +106,27 @@ def get_mla_prefill_backend(
|
||||
logger.info_once("Using %s MLA prefill backend.", selected_backend.name)
|
||||
return backend_cls
|
||||
|
||||
return _auto_select_mla_prefill_backend(
|
||||
device_capability,
|
||||
selector_config,
|
||||
)
|
||||
return _auto_select_mla_prefill_backend(selector_config)
|
||||
|
||||
|
||||
@cache
|
||||
def _auto_select_mla_prefill_backend(
|
||||
device_capability: DeviceCapability,
|
||||
selector_config: MLAPrefillSelectorConfig,
|
||||
) -> "type[MLAPrefillBackend]":
|
||||
"""Auto-select the best available MLA prefill backend.
|
||||
|
||||
Args:
|
||||
device_capability: The device's compute capability.
|
||||
selector_config: Hashable configuration for backend selection.
|
||||
|
||||
Returns:
|
||||
The selected prefill backend class.
|
||||
|
||||
Raises:
|
||||
ValueError: If the platform has no valid MLA prefill backend.
|
||||
|
||||
"""
|
||||
priorities = _get_mla_prefill_backend_priorities(
|
||||
device_capability,
|
||||
selector_config.mla_dimensions,
|
||||
)
|
||||
all_invalid_reasons: dict[str, list[str]] = {}
|
||||
from vllm.platforms import current_platform
|
||||
|
||||
for backend_enum in priorities:
|
||||
backend_cls: type[MLAPrefillBackend] | None = None
|
||||
try:
|
||||
backend_cls = backend_enum.get_class()
|
||||
invalid_reasons = backend_cls.validate_configuration(
|
||||
device_capability, selector_config
|
||||
)
|
||||
except ImportError:
|
||||
invalid_reasons = ["ImportError"]
|
||||
if not invalid_reasons:
|
||||
assert backend_cls is not None
|
||||
logger.info_once("Using %s MLA prefill backend.", backend_enum.name)
|
||||
return backend_cls
|
||||
all_invalid_reasons[backend_enum.name] = invalid_reasons
|
||||
|
||||
reasons_str = (
|
||||
"{"
|
||||
+ ", ".join(
|
||||
f"{name}: [{', '.join(reasons)}]"
|
||||
for name, reasons in all_invalid_reasons.items()
|
||||
)
|
||||
+ "}"
|
||||
)
|
||||
config_str = repr(selector_config)
|
||||
logger.debug_once(
|
||||
"Some MLA prefill backends are not valid with %s. Reasons: %s.",
|
||||
config_str,
|
||||
reasons_str,
|
||||
)
|
||||
|
||||
raise ValueError(
|
||||
f"No valid MLA prefill backend found with {config_str}. Reasons: {reasons_str}."
|
||||
)
|
||||
selected_backend = current_platform.get_mla_prefill_backend_cls(selector_config)
|
||||
logger.info_once("Using %s MLA prefill backend.", selected_backend.get_name())
|
||||
return selected_backend
|
||||
|
||||
@@ -35,8 +35,11 @@ class TokenspeedMLAPrefillBackend(MLAPrefillBackend):
|
||||
return "TOKENSPEED_MLA"
|
||||
|
||||
@classmethod
|
||||
def supports_compute_capability(cls, device_capability: "DeviceCapability") -> bool:
|
||||
return device_capability.major == 10
|
||||
def supports_compute_capability(
|
||||
cls,
|
||||
device_capability: "DeviceCapability | None",
|
||||
) -> bool:
|
||||
return device_capability is not None and device_capability.major == 10
|
||||
|
||||
_INSTALL_HINT = (
|
||||
"tokenspeed_mla package is not installed. "
|
||||
|
||||
@@ -43,8 +43,11 @@ class TrtllmRaggedPrefillBackend(MLAPrefillBackend):
|
||||
return "TRTLLM_RAGGED"
|
||||
|
||||
@classmethod
|
||||
def supports_compute_capability(cls, device_capability: "DeviceCapability") -> bool:
|
||||
return device_capability.major == 10
|
||||
def supports_compute_capability(
|
||||
cls,
|
||||
device_capability: "DeviceCapability | None",
|
||||
) -> bool:
|
||||
return device_capability is not None and device_capability.major == 10
|
||||
|
||||
@classmethod
|
||||
def is_available(cls) -> bool:
|
||||
|
||||
Reference in New Issue
Block a user