[Bugfix] Make Attention Backend Auto-Selection Batch-Invariance-Aware (#40193)
Signed-off-by: Srreyansh Sethi <srreyansh.sethi@gmail.com> Signed-off-by: Matthew Bonanni <mbonanni@redhat.com> Co-authored-by: Matthew Bonanni <mbonanni@redhat.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Matthew Bonanni
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent
d0009ddb0b
commit
b7a2605020
@@ -236,6 +236,10 @@ class AttentionBackend(ABC):
|
||||
"""
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def supports_batch_invariance(cls) -> bool:
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def supports_attn_type(cls, attn_type: str) -> bool:
|
||||
"""Check if backend supports a given attention type.
|
||||
@@ -278,6 +282,7 @@ class AttentionBackend(ABC):
|
||||
device_capability: "DeviceCapability",
|
||||
attn_type: str,
|
||||
use_non_causal: bool = False,
|
||||
use_batch_invariant: bool = False,
|
||||
) -> list[str]:
|
||||
invalid_reasons = []
|
||||
if not cls.supports_head_size(head_size):
|
||||
@@ -312,6 +317,8 @@ class AttentionBackend(ABC):
|
||||
invalid_reasons.append(f"attention type {attn_type} not supported")
|
||||
if use_non_causal and not cls.supports_non_causal():
|
||||
invalid_reasons.append("non-causal attention not supported")
|
||||
if use_batch_invariant and not cls.supports_batch_invariance():
|
||||
invalid_reasons.append("batch invariance not supported")
|
||||
combination_reason = cls.supports_combination(
|
||||
head_size,
|
||||
dtype,
|
||||
|
||||
@@ -103,6 +103,10 @@ class FlashAttentionBackend(AttentionBackend):
|
||||
def get_name() -> str:
|
||||
return "FLASH_ATTN"
|
||||
|
||||
@classmethod
|
||||
def supports_batch_invariance(cls) -> bool:
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def supports_non_causal(cls) -> bool:
|
||||
return True
|
||||
|
||||
@@ -56,6 +56,10 @@ class FlashAttnMLABackend(MLACommonBackend):
|
||||
def get_name() -> str:
|
||||
return "FLASH_ATTN_MLA"
|
||||
|
||||
@classmethod
|
||||
def supports_batch_invariance(cls) -> bool:
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def get_builder_cls() -> type["FlashAttnMLAMetadataBuilder"]:
|
||||
return FlashAttnMLAMetadataBuilder
|
||||
|
||||
@@ -55,6 +55,10 @@ class TritonMLABackend(MLACommonBackend):
|
||||
def get_name() -> str:
|
||||
return "TRITON_MLA"
|
||||
|
||||
@classmethod
|
||||
def supports_batch_invariance(cls) -> bool:
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def get_impl_cls() -> type["TritonMLAImpl"]:
|
||||
return TritonMLAImpl
|
||||
|
||||
@@ -296,6 +296,10 @@ class TritonAttentionBackend(AttentionBackend):
|
||||
def get_name() -> str:
|
||||
return "TRITON_ATTN"
|
||||
|
||||
@classmethod
|
||||
def supports_batch_invariance(cls) -> bool:
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def get_impl_cls() -> type["TritonAttentionImpl"]:
|
||||
return TritonAttentionImpl
|
||||
|
||||
@@ -6,6 +6,7 @@ from typing import NamedTuple, cast, get_args
|
||||
|
||||
import torch
|
||||
|
||||
import vllm.envs as envs
|
||||
from vllm.config.cache import CacheDType
|
||||
from vllm.logger import init_logger
|
||||
from vllm.utils.import_utils import resolve_obj_by_qualname
|
||||
@@ -30,6 +31,7 @@ class AttentionSelectorConfig(NamedTuple):
|
||||
use_per_head_quant_scales: bool = False
|
||||
attn_type: str = AttentionType.DECODER
|
||||
use_non_causal: bool = False
|
||||
use_batch_invariant: bool = False
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
@@ -43,7 +45,8 @@ class AttentionSelectorConfig(NamedTuple):
|
||||
f"use_mm_prefix={self.use_mm_prefix}, "
|
||||
f"use_per_head_quant_scales={self.use_per_head_quant_scales}, "
|
||||
f"attn_type={self.attn_type}, "
|
||||
f"use_non_causal={self.use_non_causal})"
|
||||
f"use_non_causal={self.use_non_causal}, "
|
||||
f"use_batch_invariant={self.use_batch_invariant})"
|
||||
)
|
||||
|
||||
|
||||
@@ -95,6 +98,7 @@ def get_attn_backend(
|
||||
use_per_head_quant_scales=use_per_head_quant_scales,
|
||||
attn_type=attn_type or AttentionType.DECODER,
|
||||
use_non_causal=use_non_causal,
|
||||
use_batch_invariant=envs.VLLM_BATCH_INVARIANT,
|
||||
)
|
||||
|
||||
return _cached_get_attn_backend(
|
||||
@@ -162,4 +166,9 @@ def _cached_get_mamba_attn_backend(
|
||||
) from e
|
||||
|
||||
mamba_attn_backend = selected_backend.get_class()
|
||||
if envs.VLLM_BATCH_INVARIANT and not mamba_attn_backend.supports_batch_invariance():
|
||||
raise RuntimeError(
|
||||
"VLLM batch_invariant mode is not supported for "
|
||||
f"{mamba_attn_backend.get_name()}."
|
||||
)
|
||||
return mamba_attn_backend
|
||||
|
||||
@@ -1027,11 +1027,10 @@ def init_worker_distributed_environment(
|
||||
backend: str = "nccl",
|
||||
) -> None:
|
||||
"""Initialize the distributed environment."""
|
||||
attention_config = vllm_config.attention_config
|
||||
parallel_config = vllm_config.parallel_config
|
||||
from vllm.model_executor.layers.batch_invariant import init_batch_invariance
|
||||
|
||||
init_batch_invariance(attention_config.backend)
|
||||
init_batch_invariance()
|
||||
override_envs_for_eplb(parallel_config)
|
||||
set_custom_all_reduce(not parallel_config.disable_custom_all_reduce)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user