[Core] Keep attention backends eligible for text-only serving of prefix-LM models (#48796)

Signed-off-by: qtris123 <voquangtri2021@gmail.com>
Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk>
This commit is contained in:
Tri Vo
2026-07-25 03:06:14 -07:00
committed by GitHub
co-authored by Cyrus Leung
parent dbcc1cdd0a
commit fe5145765f
4 changed files with 165 additions and 7 deletions
+43 -1
View File
@@ -758,6 +758,10 @@ class ModelConfig:
"disable the cache with --mm-processor-cache-gb 0."
)
# Rebuild after multimodal_config exists so text-only mm_prefix
# clearing is applied (and cached for later with_hf_config calls).
self.model_arch_config = self.get_model_arch_config()
if self.disable_sliding_window:
# Set after get_and_verify_max_len to ensure that max_model_len
# can be correctly capped to sliding window size
@@ -770,6 +774,42 @@ class ModelConfig:
self._verify_cuda_graph()
self._verify_bnb_config()
def _supports_multimodal_for_mm_prefix(self) -> bool:
"""Whether multimodal inputs can still appear for this deployment.
This runs more than once per config: once early in ``__post_init__``
(before ``multimodal_config`` exists), again after it is created, and
then for every ``get_model_arch_config`` regeneration -- notably
``with_hf_config``, which deep-copies this ``ModelConfig`` and swaps
``hf_config`` for a text-only submodule (e.g. ``Gemma4ForCausalLM``).
The result is cached for correctness, not just to save work: on the
``with_hf_config`` copy the submodule architecture has no registered
multimodal processor, so re-querying the registry would raise and be
treated as text-only, wrongly clearing ``is_mm_prefix_lm`` even when a
vision modality is still enabled (e.g. ``image=0`` but video allowed).
The deep-copied cache preserves the top-level decision instead.
"""
cached = getattr(self, "_supports_multimodal_inputs_cached", None)
if cached is not None:
return cached
if self.multimodal_config is None:
# Early call before multimodal init — do not clear mm_prefix yet.
return True
from vllm.multimodal import MULTIMODAL_REGISTRY
supports_mm = MULTIMODAL_REGISTRY.supports_multimodal_inputs(self)
self._supports_multimodal_inputs_cached = supports_mm
if not supports_mm:
logger.info_once(
"Disabled mm_prefix attention mode because multimodal inputs "
"are configuration-disabled. Attention backends without "
"mm_prefix support may now be selected."
)
return supports_mm
def get_model_arch_config(
self,
) -> ModelArchitectureConfig:
@@ -777,7 +817,9 @@ class ModelConfig:
self.hf_config.model_type, ModelArchConfigConvertorBase
)
convertor = convertor_cls(self.hf_config, self.hf_text_config)
return convertor.convert()
return convertor.convert(
supports_multimodal=self._supports_multimodal_for_mm_prefix()
)
@field_validator("tokenizer", "max_model_len", mode="wrap")
@classmethod
@@ -300,8 +300,16 @@ class ModelArchConfigConvertorBase:
)
return False
def is_mm_prefix_lm(self) -> bool:
"""Whether to use bidirectional attention for mm positions."""
def is_mm_prefix_lm(self, supports_multimodal: bool = True) -> bool:
"""Whether to use bidirectional attention for mm positions.
``supports_multimodal`` is False when the deployment is configuration-
disabled for multimodal inputs (text-only serving). In that case
mm_prefix is unnecessary and must stay off so attention backends
without ``supports_mm_prefix()`` remain eligible.
"""
if not supports_multimodal:
return False
if hasattr(self.hf_config, "is_mm_prefix_lm"):
return bool(self.hf_config.is_mm_prefix_lm)
# fallback to list of known models
@@ -358,7 +366,7 @@ class ModelArchConfigConvertorBase:
derived_max_model_len = tmp_max_len
return derived_max_model_len, max_len_key
def convert(self) -> ModelArchitectureConfig:
def convert(self, supports_multimodal: bool = True) -> ModelArchitectureConfig:
model_arch_config = ModelArchitectureConfig(
architectures=self.get_architectures(),
model_type=self.hf_config.model_type,
@@ -372,7 +380,7 @@ class ModelArchConfigConvertorBase:
num_experts=self.get_num_experts(),
quantization_config=self.get_quantization_config(),
is_deepseek_mla=self.is_deepseek_mla(),
is_mm_prefix_lm=self.is_mm_prefix_lm(),
is_mm_prefix_lm=self.is_mm_prefix_lm(supports_multimodal),
rswa_window=self.rswa_window(),
derived_max_model_len_and_key=self.derive_max_model_len_and_key(),
)
@@ -401,7 +409,7 @@ class CohereAsrModelArchConfigConvertor(ModelArchConfigConvertorBase):
)
return enc_num_kv_heads
def is_mm_prefix_lm(self) -> bool:
def is_mm_prefix_lm(self, supports_multimodal: bool = True) -> bool:
return False
@@ -584,7 +592,9 @@ class Gemma4MTPModelArchConfigConvertor(ModelArchConfigConvertorBase):
class Gemma4ModelArchConfigConvertor(ModelArchConfigConvertorBase):
def is_mm_prefix_lm(self) -> bool:
def is_mm_prefix_lm(self, supports_multimodal: bool = True) -> bool:
if not supports_multimodal:
return False
return (
getattr(self.hf_text_config, "use_bidirectional_attention", None)
== "vision"