[Kernel] Support GLM-5 dimensions for TRT-LLM ragged MLA prefill (#43525)
Signed-off-by: Mohammad Miadh Angkad <176301910+mmangkad@users.noreply.github.com> Signed-off-by: Matthew Bonanni <mbonanni@redhat.com> Co-authored-by: Matthew Bonanni <mbonanni@redhat.com>
This commit is contained in:
co-authored by
Matthew Bonanni
parent
b9684d99e9
commit
9d4dc4ca2f
@@ -3,6 +3,7 @@
|
||||
"""Abstract base class for MLA prefill backends."""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING, ClassVar
|
||||
|
||||
import torch
|
||||
@@ -19,6 +20,20 @@ if TYPE_CHECKING:
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class MLADimensions:
|
||||
qk_nope_head_dim: int
|
||||
qk_rope_head_dim: int
|
||||
v_head_dim: int
|
||||
|
||||
def __str__(self) -> str:
|
||||
return (
|
||||
f"(qk_nope_head_dim={self.qk_nope_head_dim}, "
|
||||
f"qk_rope_head_dim={self.qk_rope_head_dim}, "
|
||||
f"v_head_dim={self.v_head_dim})"
|
||||
)
|
||||
|
||||
|
||||
class MLAPrefillBackend(ABC):
|
||||
"""Abstract base class for MLA prefill backends."""
|
||||
|
||||
@@ -26,7 +41,7 @@ class MLAPrefillBackend(ABC):
|
||||
torch.float16,
|
||||
torch.bfloat16,
|
||||
]
|
||||
requires_r1_mla_dimensions: ClassVar[bool] = False
|
||||
supported_mla_dimensions: ClassVar[list[MLADimensions]] = []
|
||||
|
||||
@staticmethod
|
||||
@abstractmethod
|
||||
@@ -71,10 +86,14 @@ class MLAPrefillBackend(ABC):
|
||||
if not cls.is_available():
|
||||
invalid_reasons.append("required dependencies not available")
|
||||
|
||||
if cls.requires_r1_mla_dimensions and not selector_config.is_r1_compatible:
|
||||
if (
|
||||
cls.supported_mla_dimensions
|
||||
and selector_config.mla_dimensions not in cls.supported_mla_dimensions
|
||||
):
|
||||
supported = ", ".join(str(dims) for dims in cls.supported_mla_dimensions)
|
||||
invalid_reasons.append(
|
||||
"model does not have DeepSeek R1 MLA dimensions "
|
||||
"(qk_nope_head_dim=128, qk_rope_head_dim=64, v_head_dim=128)"
|
||||
"Model does not have supported MLA dimensions "
|
||||
f"(got {selector_config.mla_dimensions}; supported: {supported})"
|
||||
)
|
||||
|
||||
return invalid_reasons
|
||||
|
||||
@@ -2,12 +2,15 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""FlashInfer backend for MLA prefill."""
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, ClassVar
|
||||
|
||||
import torch
|
||||
|
||||
import vllm.envs as envs
|
||||
from vllm.v1.attention.backends.mla.prefill.base import MLAPrefillBackend
|
||||
from vllm.v1.attention.backends.mla.prefill.base import (
|
||||
MLADimensions,
|
||||
MLAPrefillBackend,
|
||||
)
|
||||
from vllm.v1.attention.backends.utils import (
|
||||
PerLayerParameters,
|
||||
get_per_layer_parameters,
|
||||
@@ -33,7 +36,13 @@ _DEFAULT_NUM_CHUNKS = 32
|
||||
class FlashInferPrefillBackend(MLAPrefillBackend):
|
||||
"""FlashInfer backend for MLA prefill."""
|
||||
|
||||
requires_r1_mla_dimensions = True
|
||||
supported_mla_dimensions: ClassVar[list[MLADimensions]] = [
|
||||
MLADimensions(
|
||||
qk_nope_head_dim=128,
|
||||
qk_rope_head_dim=64,
|
||||
v_head_dim=128,
|
||||
),
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def get_name() -> str:
|
||||
|
||||
@@ -13,6 +13,7 @@ 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:
|
||||
@@ -31,24 +32,17 @@ class MLAPrefillSelectorConfig(NamedTuple):
|
||||
"""
|
||||
|
||||
dtype: torch.dtype
|
||||
is_r1_compatible: bool
|
||||
mla_dimensions: MLADimensions = MLADimensions(
|
||||
qk_nope_head_dim=0,
|
||||
qk_rope_head_dim=0,
|
||||
v_head_dim=0,
|
||||
)
|
||||
|
||||
|
||||
def is_deepseek_r1_mla_compatible(vllm_config: "VllmConfig") -> bool:
|
||||
"""Check if model has DeepSeek R1 compatible MLA dimensions.
|
||||
|
||||
DeepSeek R1 MLA dimensions are:
|
||||
- qk_nope_head_dim = 128
|
||||
- qk_rope_head_dim = 64
|
||||
- v_head_dim = 128
|
||||
"""
|
||||
if vllm_config.model_config is None:
|
||||
return False
|
||||
hf_text_config = vllm_config.model_config.hf_text_config
|
||||
qk_nope_head_dim = getattr(hf_text_config, "qk_nope_head_dim", 1)
|
||||
qk_rope_head_dim = getattr(hf_text_config, "qk_rope_head_dim", 1)
|
||||
v_head_dim = getattr(hf_text_config, "v_head_dim", 1)
|
||||
return qk_nope_head_dim == 128 and qk_rope_head_dim == 64 and v_head_dim == 128
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"MLAPrefillSelectorConfig(dtype={self.dtype}, "
|
||||
f"mla_dimensions={self.mla_dimensions})"
|
||||
)
|
||||
|
||||
|
||||
def _get_mla_prefill_backend_priorities(
|
||||
@@ -101,10 +95,19 @@ def get_mla_prefill_backend(
|
||||
|
||||
attention_config = vllm_config.attention_config
|
||||
|
||||
selector_config = MLAPrefillSelectorConfig(
|
||||
dtype=vllm_config.model_config.dtype,
|
||||
is_r1_compatible=is_deepseek_r1_mla_compatible(vllm_config),
|
||||
)
|
||||
model_config = vllm_config.model_config
|
||||
if model_config is None:
|
||||
selector_config = MLAPrefillSelectorConfig(dtype=torch.get_default_dtype())
|
||||
else:
|
||||
hf_text_config = model_config.hf_text_config
|
||||
selector_config = MLAPrefillSelectorConfig(
|
||||
dtype=model_config.dtype,
|
||||
mla_dimensions=MLADimensions(
|
||||
qk_nope_head_dim=getattr(hf_text_config, "qk_nope_head_dim", 0),
|
||||
qk_rope_head_dim=getattr(hf_text_config, "qk_rope_head_dim", 0),
|
||||
v_head_dim=getattr(hf_text_config, "v_head_dim", 0),
|
||||
),
|
||||
)
|
||||
|
||||
if attention_config.mla_prefill_backend is not None:
|
||||
selected_backend = attention_config.mla_prefill_backend
|
||||
|
||||
@@ -2,11 +2,14 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""TokenSpeed CuTe DSL backend for MLA prefill."""
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, ClassVar
|
||||
|
||||
import torch
|
||||
|
||||
from vllm.v1.attention.backends.mla.prefill.base import MLAPrefillBackend
|
||||
from vllm.v1.attention.backends.mla.prefill.base import (
|
||||
MLADimensions,
|
||||
MLAPrefillBackend,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.config import VllmConfig
|
||||
@@ -19,7 +22,13 @@ if TYPE_CHECKING:
|
||||
class TokenspeedMLAPrefillBackend(MLAPrefillBackend):
|
||||
"""TokenSpeed CuTe DSL backend for MLA prefill."""
|
||||
|
||||
requires_r1_mla_dimensions = True
|
||||
supported_mla_dimensions: ClassVar[list[MLADimensions]] = [
|
||||
MLADimensions(
|
||||
qk_nope_head_dim=128,
|
||||
qk_rope_head_dim=64,
|
||||
v_head_dim=128,
|
||||
),
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def get_name() -> str:
|
||||
|
||||
@@ -2,12 +2,15 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""TRT-LLM Ragged backend for MLA prefill."""
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, ClassVar
|
||||
|
||||
import torch
|
||||
|
||||
import vllm.envs as envs
|
||||
from vllm.v1.attention.backends.mla.prefill.base import MLAPrefillBackend
|
||||
from vllm.v1.attention.backends.mla.prefill.base import (
|
||||
MLADimensions,
|
||||
MLAPrefillBackend,
|
||||
)
|
||||
from vllm.v1.worker.workspace import current_workspace_manager
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -21,7 +24,18 @@ if TYPE_CHECKING:
|
||||
class TrtllmRaggedPrefillBackend(MLAPrefillBackend):
|
||||
"""TRT-LLM Ragged backend for MLA prefill."""
|
||||
|
||||
requires_r1_mla_dimensions = True
|
||||
supported_mla_dimensions: ClassVar[list[MLADimensions]] = [
|
||||
MLADimensions(
|
||||
qk_nope_head_dim=128,
|
||||
qk_rope_head_dim=64,
|
||||
v_head_dim=128,
|
||||
),
|
||||
MLADimensions(
|
||||
qk_nope_head_dim=192,
|
||||
qk_rope_head_dim=64,
|
||||
v_head_dim=256,
|
||||
),
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def get_name() -> str:
|
||||
|
||||
Reference in New Issue
Block a user