[Mypy Fix] Mypy fix for "vllm/model_executor/models/[aA][bB]" (#48977)
Signed-off-by: yewentao256 <zhyanwentao@126.com>
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
import itertools
|
||||
from collections.abc import Iterable
|
||||
from contextlib import contextmanager
|
||||
from typing import TYPE_CHECKING, Any, TypeVar, cast
|
||||
from typing import TYPE_CHECKING, Any, TypeVar
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
@@ -25,7 +25,7 @@ if TYPE_CHECKING:
|
||||
from vllm.config import ModelConfig, VllmConfig
|
||||
from vllm.model_executor.layers.pooler import Pooler
|
||||
|
||||
_T = TypeVar("_T", bound=type[nn.Module])
|
||||
_T = TypeVar("_T", bound=nn.Module)
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -45,7 +45,7 @@ def _load_st_projector(model_config: "ModelConfig") -> nn.Module | None:
|
||||
)
|
||||
|
||||
if dense_modules is None:
|
||||
return
|
||||
return None
|
||||
|
||||
try:
|
||||
layers = []
|
||||
@@ -126,14 +126,14 @@ def _get_pooling_model_name(orig_model_name: str, pooling_suffix: str) -> str:
|
||||
return model_name + pooling_suffix
|
||||
|
||||
|
||||
def _create_pooling_model_cls(orig_cls: _T) -> _T:
|
||||
def _create_pooling_model_cls(orig_cls: type[_T]) -> type[_T]:
|
||||
# Lazy import
|
||||
from vllm.model_executor.layers.logits_processor import LogitsProcessor
|
||||
from vllm.model_executor.layers.vocab_parallel_embedding import ParallelLMHead
|
||||
|
||||
from .utils import AutoWeightsLoader, StageMissingLayer, no_init_weights
|
||||
|
||||
class ModelForPooling(orig_cls, VllmModelForPooling):
|
||||
class ModelForPooling(orig_cls, VllmModelForPooling): # type: ignore[valid-type,misc]
|
||||
is_pooling_model = True
|
||||
|
||||
def __init__(
|
||||
@@ -148,16 +148,19 @@ def _create_pooling_model_cls(orig_cls: _T) -> _T:
|
||||
lambda mod: StageMissingLayer("output", mod),
|
||||
targets=(LogitsProcessor, ParallelLMHead),
|
||||
):
|
||||
super().__init__(vllm_config=vllm_config, prefix=prefix, **kwargs)
|
||||
super().__init__( # type: ignore[safe-super]
|
||||
vllm_config=vllm_config, prefix=prefix, **kwargs
|
||||
)
|
||||
|
||||
# Used by SEQ_CLS_LOAD_METHODS
|
||||
self.vllm_config = vllm_config
|
||||
|
||||
# If the model already defines a pooler instance, don't overwrite it
|
||||
pooler = getattr(self, "pooler", None)
|
||||
if not pooler and supports_multimodal(self):
|
||||
multimodal_model: object = self
|
||||
if not pooler and supports_multimodal(multimodal_model):
|
||||
# Try to get the pooler from the LM backbone
|
||||
language_model = self.get_language_model()
|
||||
language_model = multimodal_model.get_language_model()
|
||||
if hasattr(language_model, "pooler"):
|
||||
pooler = language_model.pooler
|
||||
|
||||
@@ -173,7 +176,9 @@ def _create_pooling_model_cls(orig_cls: _T) -> _T:
|
||||
) -> "Pooler":
|
||||
raise NotImplementedError
|
||||
|
||||
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]):
|
||||
def _load_pooling_model_weights(
|
||||
self, weights: Iterable[tuple[str, torch.Tensor]]
|
||||
):
|
||||
params_dict = dict(self.named_parameters())
|
||||
# Use model-owned remapping only for the probe membership check
|
||||
# (e.g. Gemma4 model.language_model.* -> language_model.model.*)
|
||||
@@ -236,10 +241,13 @@ def _create_pooling_model_cls(orig_cls: _T) -> _T:
|
||||
load_weights = getattr(super(), "load_weights", default_load_weights)
|
||||
return load_weights(mapped_weights)
|
||||
|
||||
return ModelForPooling # type: ignore
|
||||
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]):
|
||||
return self._load_pooling_model_weights(weights)
|
||||
|
||||
return ModelForPooling
|
||||
|
||||
|
||||
def as_embedding_model(cls: _T) -> _T:
|
||||
def as_embedding_model(cls: type[_T]) -> type[_T]:
|
||||
"""
|
||||
Subclass an existing vLLM model to support embeddings.
|
||||
|
||||
@@ -257,7 +265,7 @@ def as_embedding_model(cls: _T) -> _T:
|
||||
# Lazy import
|
||||
from vllm.model_executor.layers.pooler import DispatchPooler
|
||||
|
||||
class ModelForEmbedding(_create_pooling_model_cls(cls)):
|
||||
class ModelForEmbedding(_create_pooling_model_cls(cls)): # type: ignore[misc]
|
||||
def _init_pooler(
|
||||
self,
|
||||
vllm_config: "VllmConfig",
|
||||
@@ -270,7 +278,7 @@ def as_embedding_model(cls: _T) -> _T:
|
||||
|
||||
ModelForEmbedding.__name__ = _get_pooling_model_name(cls.__name__, "ForEmbedding")
|
||||
|
||||
return ModelForEmbedding # type: ignore
|
||||
return ModelForEmbedding
|
||||
|
||||
|
||||
def _resolve_num_labels(hf_config: Any, text_config: Any) -> int:
|
||||
@@ -296,7 +304,7 @@ def _resolve_num_labels(hf_config: Any, text_config: Any) -> int:
|
||||
return text_config.num_labels
|
||||
|
||||
|
||||
def as_seq_cls_model(cls: _T) -> _T:
|
||||
def as_seq_cls_model(cls: type[_T]) -> type[_T]:
|
||||
"""
|
||||
Subclass an existing vLLM model to support classify and score tasks.
|
||||
|
||||
@@ -320,7 +328,8 @@ def as_seq_cls_model(cls: _T) -> _T:
|
||||
from .utils import maybe_prefix
|
||||
|
||||
class ModelForSequenceClassification(
|
||||
_create_pooling_model_cls(cls), SupportsCrossEncoding
|
||||
_create_pooling_model_cls(cls), # type: ignore[misc]
|
||||
SupportsCrossEncoding,
|
||||
):
|
||||
def _init_pooler(
|
||||
self,
|
||||
@@ -401,7 +410,7 @@ def as_seq_cls_model(cls: _T) -> _T:
|
||||
cls.__name__, "ForSequenceClassification"
|
||||
)
|
||||
|
||||
return ModelForSequenceClassification # type: ignore
|
||||
return ModelForSequenceClassification
|
||||
|
||||
|
||||
class SequenceClassificationConfig(VerifyAndUpdateConfig):
|
||||
@@ -435,14 +444,15 @@ class SequenceClassificationConfig(VerifyAndUpdateConfig):
|
||||
text_config.use_sep_token = use_sep_token
|
||||
|
||||
|
||||
def _get_language_model_for_seq_cls(model) -> nn.Module:
|
||||
def _get_language_model_for_seq_cls(model: nn.Module) -> nn.Module:
|
||||
"""
|
||||
Get the language model component for sequence classification conversion.
|
||||
For VLMs, returns the inner language model. For standard LLMs, returns model itself.
|
||||
"""
|
||||
if supports_multimodal(model):
|
||||
multimodal_model: object = model
|
||||
if supports_multimodal(multimodal_model):
|
||||
try:
|
||||
lm = model.get_language_model()
|
||||
lm = multimodal_model.get_language_model()
|
||||
if lm is not model:
|
||||
return lm
|
||||
except Exception:
|
||||
@@ -516,12 +526,11 @@ def load_weights_using_from_2_way_softmax(
|
||||
hf_config = model.config
|
||||
text_config = hf_config.get_text_config()
|
||||
|
||||
tokens = getattr(
|
||||
tokens: list[str] = getattr(
|
||||
hf_config,
|
||||
"classifier_from_token",
|
||||
getattr(text_config, "classifier_from_token", []),
|
||||
)
|
||||
tokens = cast(list[int], tokens)
|
||||
assert len(tokens) == 2
|
||||
|
||||
language_model = _get_language_model_for_seq_cls(model)
|
||||
@@ -545,12 +554,7 @@ def load_weights_using_from_2_way_softmax(
|
||||
language_model.lm_head = language_model.lm_head.tie_weights(embed_tokens)
|
||||
|
||||
with _disable_seq_cls_loading_on_inner_model(language_model, is_vlm):
|
||||
# ModelForPooling is dynamically defined inside the _create_pooling_model_cls
|
||||
# function, so we need use this hacky method to obtain it.
|
||||
pooling_model_cls = next(
|
||||
x for x in type(model).__mro__ if x.__name__ == "ModelForPooling"
|
||||
)
|
||||
loaded_weights = pooling_model_cls.load_weights(model, weights)
|
||||
loaded_weights = model._load_pooling_model_weights(weights)
|
||||
|
||||
from vllm.tokenizers import get_tokenizer
|
||||
|
||||
@@ -594,8 +598,7 @@ def load_weights_no_post_processing(model, weights: Iterable[tuple[str, torch.Te
|
||||
model_config = model.vllm_config.model_config
|
||||
text_config = model.config.get_text_config()
|
||||
|
||||
tokens = getattr(text_config, "classifier_from_token", [])
|
||||
tokens = cast(list[int], tokens)
|
||||
tokens: list[str] = getattr(text_config, "classifier_from_token", [])
|
||||
assert len(tokens) > 0
|
||||
|
||||
language_model = _get_language_model_for_seq_cls(model)
|
||||
@@ -619,11 +622,8 @@ def load_weights_no_post_processing(model, weights: Iterable[tuple[str, torch.Te
|
||||
language_model.lm_head = language_model.lm_head.tie_weights(embed_tokens)
|
||||
|
||||
with _disable_seq_cls_loading_on_inner_model(language_model, is_vlm):
|
||||
pooling_model_cls = next(
|
||||
x for x in type(model).__mro__ if x.__name__ == "ModelForPooling"
|
||||
)
|
||||
# Skip ModelForSequenceClassification in MRO to avoid infinite recursion
|
||||
loaded_weights = pooling_model_cls.load_weights(model, weights)
|
||||
# Bypass ModelForSequenceClassification to avoid infinite recursion
|
||||
loaded_weights = model._load_pooling_model_weights(weights)
|
||||
|
||||
from vllm.tokenizers import get_tokenizer
|
||||
|
||||
|
||||
Reference in New Issue
Block a user