[Mypy Fix] Mypy fix for "vllm/model_executor/models/[aA][bB]" (#48977)

Signed-off-by: yewentao256 <zhyanwentao@126.com>
This commit is contained in:
Wentao Ye
2026-08-06 19:30:15 +00:00
committed by GitHub
parent d8eabdbfbe
commit adc3e03517
23 changed files with 214 additions and 167 deletions
-2
View File
@@ -113,8 +113,6 @@ SEPARATE_GROUPS = [
# TODO(woosuk): Include the code from Megatron and HuggingFace. # TODO(woosuk): Include the code from Megatron and HuggingFace.
EXCLUDE = [ EXCLUDE = [
r"vllm/model_executor/models/[aA]",
r"vllm/model_executor/models/[bB]",
r"vllm/model_executor/models/[cC]", r"vllm/model_executor/models/[cC]",
r"vllm/model_executor/models/[dD]", r"vllm/model_executor/models/[dD]",
r"vllm/model_executor/models/[eE]", r"vllm/model_executor/models/[eE]",
@@ -144,7 +144,7 @@ class MambaStateShapeCalculator:
num_heads: int, num_heads: int,
tp_size: int, tp_size: int,
head_dim: int, head_dim: int,
) -> tuple[tuple[int, int, int], ...]: ) -> tuple[tuple[int, int, int]]:
state_shape = (num_heads // tp_size, head_dim, head_dim) state_shape = (num_heads // tp_size, head_dim, head_dim)
return (state_shape,) return (state_shape,)
+28 -10
View File
@@ -110,8 +110,12 @@ class AXK1MoE(nn.Module):
self.ep_group = get_ep_group().device_group self.ep_group = get_ep_group().device_group
self.ep_rank = get_ep_group().rank_in_group self.ep_rank = get_ep_group().rank_in_group
self.ep_size = self.ep_group.size() self.ep_size = self.ep_group.size()
assert config.n_routed_experts is not None
assert config.num_experts_per_tok is not None
assert config.scoring_func is not None
assert config.hidden_act is not None
self.n_routed_experts: int = config.n_routed_experts self.n_routed_experts: int = config.n_routed_experts
self.n_shared_experts: int = config.n_shared_experts self.n_shared_experts: int | None = config.n_shared_experts
self.is_sequence_parallel = parallel_config.use_sequence_parallel_moe self.is_sequence_parallel = parallel_config.use_sequence_parallel_moe
@@ -245,7 +249,7 @@ class AXK1Attention(nn.Module):
qk_nope_head_dim: int, qk_nope_head_dim: int,
qk_rope_head_dim: int, qk_rope_head_dim: int,
v_head_dim: int, v_head_dim: int,
q_lora_rank: int, q_lora_rank: int | None,
kv_lora_rank: int, kv_lora_rank: int,
max_position_embeddings: int = 8192, max_position_embeddings: int = 8192,
cache_config: CacheConfig | None = None, cache_config: CacheConfig | None = None,
@@ -282,7 +286,7 @@ class AXK1Attention(nn.Module):
) )
self.q_a_layernorm = RMSNorm(self.q_lora_rank, eps=config.rms_norm_eps) self.q_a_layernorm = RMSNorm(self.q_lora_rank, eps=config.rms_norm_eps)
self.q_b_proj = ColumnParallelLinear( self.q_b_proj = ColumnParallelLinear(
q_lora_rank, self.q_lora_rank,
self.num_heads * self.qk_head_dim, self.num_heads * self.qk_head_dim,
bias=False, bias=False,
quant_config=quant_config, quant_config=quant_config,
@@ -320,6 +324,7 @@ class AXK1Attention(nn.Module):
quant_config=quant_config, quant_config=quant_config,
prefix=f"{prefix}.o_proj", prefix=f"{prefix}.o_proj",
) )
assert config.rope_parameters is not None
if config.rope_parameters["rope_type"] != "default": if config.rope_parameters["rope_type"] != "default":
config.rope_parameters["rope_type"] = ( config.rope_parameters["rope_type"] = (
"deepseek_yarn" "deepseek_yarn"
@@ -492,6 +497,7 @@ class AXK1MLAAttention(nn.Module):
prefix=f"{prefix}.o_proj", prefix=f"{prefix}.o_proj",
) )
assert config.rope_parameters is not None
if config.rope_parameters["rope_type"] != "default": if config.rope_parameters["rope_type"] != "default":
config.rope_parameters["rope_type"] = ( config.rope_parameters["rope_type"] = (
"deepseek_yarn" "deepseek_yarn"
@@ -573,6 +579,13 @@ class AXK1DecoderLayer(nn.Module):
parallel_config = vllm_config.parallel_config parallel_config = vllm_config.parallel_config
self.config = config self.config = config
assert config.max_position_embeddings is not None
assert config.hidden_act is not None
assert config.routed_scaling_factor is not None
assert config.qk_nope_head_dim is not None
assert config.qk_rope_head_dim is not None
assert config.v_head_dim is not None
assert config.kv_lora_rank is not None
self.hidden_size = config.hidden_size self.hidden_size = config.hidden_size
max_position_embeddings = config.max_position_embeddings max_position_embeddings = config.max_position_embeddings
# DecoderLayers are created with `make_layers` which passes the prefix # DecoderLayers are created with `make_layers` which passes the prefix
@@ -588,6 +601,7 @@ class AXK1DecoderLayer(nn.Module):
use_mha = all(dim == 0 for dim in (qk_nope_head_dim, qk_rope_head_dim)) use_mha = all(dim == 0 for dim in (qk_nope_head_dim, qk_rope_head_dim))
self.use_mha = use_mha self.use_mha = use_mha
attn_cls: type[nn.Module]
if use_mha: if use_mha:
attn_cls = DeepseekAttention attn_cls = DeepseekAttention
elif model_config.use_mla: elif model_config.use_mla:
@@ -635,6 +649,7 @@ class AXK1DecoderLayer(nn.Module):
self.routed_scaling_factor = config.routed_scaling_factor self.routed_scaling_factor = config.routed_scaling_factor
def _is_layer_sparse(self) -> bool: def _is_layer_sparse(self) -> bool:
assert self.config.moe_layer_freq is not None
return ( return (
self.config.n_routed_experts is not None self.config.n_routed_experts is not None
and self.layer_idx >= self.config.first_k_dense_replace and self.layer_idx >= self.config.first_k_dense_replace
@@ -789,7 +804,7 @@ class AXK1Model(nn.Module):
rocm_aiter_moe_shared_expert_enabled = ( rocm_aiter_moe_shared_expert_enabled = (
rocm_aiter_ops.is_fusion_moe_shared_experts_enabled() rocm_aiter_ops.is_fusion_moe_shared_experts_enabled()
) )
stacked_params_mapping = [ stacked_params_mapping: list[tuple[str, str, int | str]] = [
# (param_name, shard_name, shard_id) # (param_name, shard_name, shard_id)
("gate_up_proj", "gate_proj", 0), ("gate_up_proj", "gate_proj", 0),
("gate_up_proj", "up_proj", 1), ("gate_up_proj", "up_proj", 1),
@@ -810,6 +825,7 @@ class AXK1Model(nn.Module):
# Params for weights, fp8 weight scales, fp8 activation scales # Params for weights, fp8 weight scales, fp8 activation scales
# (param_name, weight_name, expert_id, shard_id) # (param_name, weight_name, expert_id, shard_id)
assert self.config.n_routed_experts is not None
expert_params_mapping = fused_moe_make_expert_params_mapping( expert_params_mapping = fused_moe_make_expert_params_mapping(
self, self,
ckpt_gate_proj_name="gate_proj", ckpt_gate_proj_name="gate_proj",
@@ -817,7 +833,7 @@ class AXK1Model(nn.Module):
ckpt_up_proj_name="up_proj", ckpt_up_proj_name="up_proj",
num_experts=self.config.n_routed_experts num_experts=self.config.n_routed_experts
+ ( + (
self.config.n_shared_experts (self.config.n_shared_experts or 0)
if rocm_aiter_moe_shared_expert_enabled if rocm_aiter_moe_shared_expert_enabled
else 0 else 0
), ),
@@ -926,7 +942,7 @@ class AXK1Model(nn.Module):
# param and delegate to its expert-aware weight_loader # param and delegate to its expert-aware weight_loader
# with expert_id. # with expert_id.
for mapping in expert_params_mapping: for mapping in expert_params_mapping:
param_name, weight_name, expert_id, shard_id = mapping param_name, weight_name, expert_id, expert_shard_id = mapping
if weight_name not in chunk_name: if weight_name not in chunk_name:
continue continue
@@ -952,7 +968,7 @@ class AXK1Model(nn.Module):
param, param,
weight_to_load, weight_to_load,
name_mapped, name_mapped,
shard_id=shard_id, shard_id=expert_shard_id,
expert_id=expert_id, expert_id=expert_id,
return_success=True, return_success=True,
) )
@@ -974,9 +990,10 @@ class AXK1Model(nn.Module):
continue continue
# Remapping the name of FP8 kv-scale. # Remapping the name of FP8 kv-scale.
name = maybe_remap_kv_scale_name(name, params_dict) remapped_name = maybe_remap_kv_scale_name(name, params_dict)
if name is None: if remapped_name is None:
continue continue
name = remapped_name
if is_pp_missing_parameter(name, self): if is_pp_missing_parameter(name, self):
continue continue
@@ -1014,7 +1031,7 @@ class AXK1MixtureOfExperts(MixtureOfExperts):
self.num_physical_experts = example_moe.n_physical_experts self.num_physical_experts = example_moe.n_physical_experts
self.num_local_physical_experts = example_moe.n_local_physical_experts self.num_local_physical_experts = example_moe.n_local_physical_experts
self.num_routed_experts = example_moe.n_routed_experts self.num_routed_experts = example_moe.n_routed_experts
self.num_shared_experts = example_moe.n_shared_experts self.num_shared_experts = example_moe.n_shared_experts or 0
self.num_redundant_experts = example_moe.n_redundant_experts self.num_redundant_experts = example_moe.n_redundant_experts
def update_physical_experts_metadata( def update_physical_experts_metadata(
@@ -1132,6 +1149,7 @@ class AXK1ForCausalLM(
def get_expert_mapping(self) -> list[tuple[str, str, int, str]]: def get_expert_mapping(self) -> list[tuple[str, str, int, str]]:
# Params for weights, fp8 weight scales, fp8 activation scales # Params for weights, fp8 weight scales, fp8 activation scales
# (param_name, weight_name, expert_id, shard_id) # (param_name, weight_name, expert_id, shard_id)
assert self.config.n_routed_experts is not None
return fused_moe_make_expert_params_mapping( return fused_moe_make_expert_params_mapping(
self, self,
ckpt_gate_proj_name="gate_proj", ckpt_gate_proj_name="gate_proj",
+34 -34
View File
@@ -4,7 +4,7 @@
import itertools import itertools
from collections.abc import Iterable from collections.abc import Iterable
from contextlib import contextmanager from contextlib import contextmanager
from typing import TYPE_CHECKING, Any, TypeVar, cast from typing import TYPE_CHECKING, Any, TypeVar
import torch import torch
import torch.nn as nn import torch.nn as nn
@@ -25,7 +25,7 @@ if TYPE_CHECKING:
from vllm.config import ModelConfig, VllmConfig from vllm.config import ModelConfig, VllmConfig
from vllm.model_executor.layers.pooler import Pooler 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__) logger = init_logger(__name__)
@@ -45,7 +45,7 @@ def _load_st_projector(model_config: "ModelConfig") -> nn.Module | None:
) )
if dense_modules is None: if dense_modules is None:
return return None
try: try:
layers = [] layers = []
@@ -126,14 +126,14 @@ def _get_pooling_model_name(orig_model_name: str, pooling_suffix: str) -> str:
return model_name + pooling_suffix 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 # Lazy import
from vllm.model_executor.layers.logits_processor import LogitsProcessor from vllm.model_executor.layers.logits_processor import LogitsProcessor
from vllm.model_executor.layers.vocab_parallel_embedding import ParallelLMHead from vllm.model_executor.layers.vocab_parallel_embedding import ParallelLMHead
from .utils import AutoWeightsLoader, StageMissingLayer, no_init_weights 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 is_pooling_model = True
def __init__( def __init__(
@@ -148,16 +148,19 @@ def _create_pooling_model_cls(orig_cls: _T) -> _T:
lambda mod: StageMissingLayer("output", mod), lambda mod: StageMissingLayer("output", mod),
targets=(LogitsProcessor, ParallelLMHead), 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 # Used by SEQ_CLS_LOAD_METHODS
self.vllm_config = vllm_config self.vllm_config = vllm_config
# If the model already defines a pooler instance, don't overwrite it # If the model already defines a pooler instance, don't overwrite it
pooler = getattr(self, "pooler", None) 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 # 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"): if hasattr(language_model, "pooler"):
pooler = language_model.pooler pooler = language_model.pooler
@@ -173,7 +176,9 @@ def _create_pooling_model_cls(orig_cls: _T) -> _T:
) -> "Pooler": ) -> "Pooler":
raise NotImplementedError 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()) params_dict = dict(self.named_parameters())
# Use model-owned remapping only for the probe membership check # Use model-owned remapping only for the probe membership check
# (e.g. Gemma4 model.language_model.* -> language_model.model.*) # (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) load_weights = getattr(super(), "load_weights", default_load_weights)
return load_weights(mapped_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. Subclass an existing vLLM model to support embeddings.
@@ -257,7 +265,7 @@ def as_embedding_model(cls: _T) -> _T:
# Lazy import # Lazy import
from vllm.model_executor.layers.pooler import DispatchPooler 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( def _init_pooler(
self, self,
vllm_config: "VllmConfig", vllm_config: "VllmConfig",
@@ -270,7 +278,7 @@ def as_embedding_model(cls: _T) -> _T:
ModelForEmbedding.__name__ = _get_pooling_model_name(cls.__name__, "ForEmbedding") 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: 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 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. 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 from .utils import maybe_prefix
class ModelForSequenceClassification( class ModelForSequenceClassification(
_create_pooling_model_cls(cls), SupportsCrossEncoding _create_pooling_model_cls(cls), # type: ignore[misc]
SupportsCrossEncoding,
): ):
def _init_pooler( def _init_pooler(
self, self,
@@ -401,7 +410,7 @@ def as_seq_cls_model(cls: _T) -> _T:
cls.__name__, "ForSequenceClassification" cls.__name__, "ForSequenceClassification"
) )
return ModelForSequenceClassification # type: ignore return ModelForSequenceClassification
class SequenceClassificationConfig(VerifyAndUpdateConfig): class SequenceClassificationConfig(VerifyAndUpdateConfig):
@@ -435,14 +444,15 @@ class SequenceClassificationConfig(VerifyAndUpdateConfig):
text_config.use_sep_token = use_sep_token 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. Get the language model component for sequence classification conversion.
For VLMs, returns the inner language model. For standard LLMs, returns model itself. 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: try:
lm = model.get_language_model() lm = multimodal_model.get_language_model()
if lm is not model: if lm is not model:
return lm return lm
except Exception: except Exception:
@@ -516,12 +526,11 @@ def load_weights_using_from_2_way_softmax(
hf_config = model.config hf_config = model.config
text_config = hf_config.get_text_config() text_config = hf_config.get_text_config()
tokens = getattr( tokens: list[str] = getattr(
hf_config, hf_config,
"classifier_from_token", "classifier_from_token",
getattr(text_config, "classifier_from_token", []), getattr(text_config, "classifier_from_token", []),
) )
tokens = cast(list[int], tokens)
assert len(tokens) == 2 assert len(tokens) == 2
language_model = _get_language_model_for_seq_cls(model) 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) language_model.lm_head = language_model.lm_head.tie_weights(embed_tokens)
with _disable_seq_cls_loading_on_inner_model(language_model, is_vlm): with _disable_seq_cls_loading_on_inner_model(language_model, is_vlm):
# ModelForPooling is dynamically defined inside the _create_pooling_model_cls loaded_weights = model._load_pooling_model_weights(weights)
# 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)
from vllm.tokenizers import get_tokenizer 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 model_config = model.vllm_config.model_config
text_config = model.config.get_text_config() text_config = model.config.get_text_config()
tokens = getattr(text_config, "classifier_from_token", []) tokens: list[str] = getattr(text_config, "classifier_from_token", [])
tokens = cast(list[int], tokens)
assert len(tokens) > 0 assert len(tokens) > 0
language_model = _get_language_model_for_seq_cls(model) 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) language_model.lm_head = language_model.lm_head.tie_weights(embed_tokens)
with _disable_seq_cls_loading_on_inner_model(language_model, is_vlm): with _disable_seq_cls_loading_on_inner_model(language_model, is_vlm):
pooling_model_cls = next( # Bypass ModelForSequenceClassification to avoid infinite recursion
x for x in type(model).__mro__ if x.__name__ == "ModelForPooling" loaded_weights = model._load_pooling_model_weights(weights)
)
# Skip ModelForSequenceClassification in MRO to avoid infinite recursion
loaded_weights = pooling_model_cls.load_weights(model, weights)
from vllm.tokenizers import get_tokenizer from vllm.tokenizers import get_tokenizer
+1 -6
View File
@@ -47,7 +47,6 @@ from vllm.model_executor.models.utils import (
PPMissingLayer, PPMissingLayer,
WeightsMapper, WeightsMapper,
extract_layer_index, extract_layer_index,
make_empty_intermediate_tensors_factory,
make_layers, make_layers,
maybe_prefix, maybe_prefix,
) )
@@ -232,7 +231,7 @@ class AfmoeAttention(nn.Module):
# Only create rotary embeddings for local attention # Only create rotary embeddings for local attention
if self.is_local_attention: if self.is_local_attention:
self.rotary_emb = get_rope( self.rotary_emb: nn.Module | None = get_rope(
self.head_dim, self.head_dim,
max_position=max_position_embeddings, max_position=max_position_embeddings,
rope_parameters=config.rope_parameters, rope_parameters=config.rope_parameters,
@@ -409,10 +408,6 @@ class AfmoeModel(nn.Module, EagleModelMixin):
else: else:
self.norm = PPMissingLayer() self.norm = PPMissingLayer()
self.make_empty_intermediate_tensors = make_empty_intermediate_tensors_factory(
["hidden_states", "residual"], config.hidden_size
)
def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor: def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor:
return self.embed_tokens(input_ids) return self.embed_tokens(input_ids)
+3 -1
View File
@@ -180,7 +180,9 @@ class AIMv2Transformer(nn.Module):
] ]
) )
if require_post_norm: if require_post_norm:
self.post_trunk_norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) self.post_trunk_norm: RMSNorm | None = RMSNorm(
config.hidden_size, eps=config.rms_norm_eps
)
else: else:
self.post_trunk_norm = None self.post_trunk_norm = None
+4 -4
View File
@@ -501,7 +501,7 @@ class ArcticModel(nn.Module):
weight_loader(param, loaded_weight, shard_id) weight_loader(param, loaded_weight, shard_id)
break break
else: else:
for param_name, weight_name, shard_id in mlp_params_mapping: for param_name, weight_name, mlp_shard_id in mlp_params_mapping:
if weight_name not in name: if weight_name not in name:
continue continue
name = name.replace(weight_name, param_name) name = name.replace(weight_name, param_name)
@@ -509,10 +509,10 @@ class ArcticModel(nn.Module):
continue continue
param = params_dict[name] param = params_dict[name]
weight_loader = param.weight_loader weight_loader = param.weight_loader
weight_loader(param, loaded_weight, shard_id) weight_loader(param, loaded_weight, mlp_shard_id)
break break
else: else:
for param_name, weight_name, shard_id in expert_params_mapping: for param_name, weight_name, expert_id in expert_params_mapping:
if weight_name not in name: if weight_name not in name:
continue continue
name = name.replace(weight_name, param_name) name = name.replace(weight_name, param_name)
@@ -521,7 +521,7 @@ class ArcticModel(nn.Module):
param = params_dict[name] param = params_dict[name]
weight_loader = param.weight_loader weight_loader = param.weight_loader
weight_loader( weight_loader(
param, loaded_weight, weight_name, expert_id=shard_id param, loaded_weight, weight_name, expert_id=expert_id
) )
break break
else: else:
+4 -2
View File
@@ -10,7 +10,7 @@ from transformers.models.aria.modeling_aria import AriaCrossAttention
from transformers.models.aria.processing_aria import AriaProcessor from transformers.models.aria.processing_aria import AriaProcessor
from vllm.config import VllmConfig from vllm.config import VllmConfig
from vllm.config.multimodal import BaseDummyOptions from vllm.config.multimodal import BaseDummyOptions, ImageDummyOptions
from vllm.inputs import MultiModalDataDict from vllm.inputs import MultiModalDataDict
from vllm.model_executor.layers.activation import get_act_fn from vllm.model_executor.layers.activation import get_act_fn
from vllm.model_executor.layers.fused_moe import ( from vllm.model_executor.layers.fused_moe import (
@@ -319,7 +319,8 @@ class AriaDummyInputsBuilder(BaseDummyInputsBuilder[AriaProcessingInfo]):
num_images = mm_counts.get("image", 0) num_images = mm_counts.get("image", 0)
processor = self.info.get_hf_processor() processor = self.info.get_hf_processor()
image_token: str = processor.tokenizer.image_token # type: ignore image_token = getattr(processor.tokenizer, "image_token", None)
assert isinstance(image_token, str)
return image_token * num_images return image_token * num_images
@@ -335,6 +336,7 @@ class AriaDummyInputsBuilder(BaseDummyInputsBuilder[AriaProcessingInfo]):
num_images = mm_counts.get("image", 0) num_images = mm_counts.get("image", 0)
image_overrides = mm_options.get("image") image_overrides = mm_options.get("image")
assert image_overrides is None or isinstance(image_overrides, ImageDummyOptions)
return { return {
"image": self._get_dummy_images( "image": self._get_dummy_images(
+10 -7
View File
@@ -17,7 +17,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from collections.abc import Iterable, Mapping, Sequence from collections.abc import Iterable, Mapping, Sequence, Sized
from typing import Annotated, Any, Literal, TypeAlias from typing import Annotated, Any, Literal, TypeAlias
import torch import torch
@@ -30,7 +30,7 @@ from transformers.models.audioflamingo3 import (
from transformers.models.qwen2_audio import Qwen2AudioEncoder from transformers.models.qwen2_audio import Qwen2AudioEncoder
from vllm.config import VllmConfig from vllm.config import VllmConfig
from vllm.config.multimodal import BaseDummyOptions from vllm.config.multimodal import AudioDummyOptions, BaseDummyOptions
from vllm.inputs import ModalityData, MultiModalDataDict from vllm.inputs import ModalityData, MultiModalDataDict
from vllm.model_executor.layers.activation import get_act_fn from vllm.model_executor.layers.activation import get_act_fn
from vllm.model_executor.models.module_mapping import MultiModelKeys from vllm.model_executor.models.module_mapping import MultiModelKeys
@@ -226,6 +226,7 @@ class AudioFlamingo3DummyInputsBuilder(
audio_len = int(hf_processor.max_audio_len * sampling_rate) audio_len = int(hf_processor.max_audio_len * sampling_rate)
num_audios = mm_counts.get("audio", 0) num_audios = mm_counts.get("audio", 0)
audio_overrides = mm_options.get("audio") audio_overrides = mm_options.get("audio")
assert audio_overrides is None or isinstance(audio_overrides, AudioDummyOptions)
return { return {
"audio": self._get_dummy_audios( "audio": self._get_dummy_audios(
@@ -317,7 +318,7 @@ def _count_audio_tokens_from_mask(
if isinstance(chunk_counts, torch.Tensor): if isinstance(chunk_counts, torch.Tensor):
counts = chunk_counts.tolist() counts = chunk_counts.tolist()
elif chunk_counts and isinstance(chunk_counts[0], torch.Tensor): elif chunk_counts and isinstance(chunk_counts[0], torch.Tensor):
counts = [count.item() for count in chunk_counts] counts = [int(count) for count in chunk_counts]
else: else:
counts = chunk_counts counts = chunk_counts
@@ -375,7 +376,7 @@ class AudioFlamingo3MultiModalProcessor(
def _call_hf_processor( def _call_hf_processor(
self, self,
prompt: str, prompt: str,
mm_data: dict[str, object], mm_data: Mapping[str, object],
mm_kwargs: Mapping[str, Any], mm_kwargs: Mapping[str, Any],
tok_kwargs: Mapping[str, object], tok_kwargs: Mapping[str, object],
) -> BatchFeature: ) -> BatchFeature:
@@ -412,7 +413,8 @@ class AudioFlamingo3MultiModalProcessor(
chunk_counts = [] chunk_counts = []
for audio in audio_list: for audio in audio_list:
# audio is numpy array or list # audio is numpy array or list
n_samples = len(audio) if isinstance(audio, list) else audio.shape[0] assert isinstance(audio, Sized)
n_samples = len(audio)
n_win = max(1, (n_samples + window_size - 1) // window_size) n_win = max(1, (n_samples + window_size - 1) // window_size)
if n_win > max_windows: if n_win > max_windows:
@@ -457,6 +459,7 @@ class AudioFlamingo3MultiModalProcessor(
) )
else: else:
audio_embeds = out_mm_data["audio_embeds"][item_idx] audio_embeds = out_mm_data["audio_embeds"][item_idx]
assert isinstance(audio_embeds, torch.Tensor)
num_features = audio_embeds.shape[0] num_features = audio_embeds.shape[0]
if num_features == 0: if num_features == 0:
@@ -556,8 +559,8 @@ class AudioFlamingo3ForConditionalGeneration(
def _process_audio_input( def _process_audio_input(
self, audio_input: AudioFlamingo3Inputs self, audio_input: AudioFlamingo3Inputs
) -> torch.Tensor | tuple[torch.Tensor, ...]: ) -> torch.Tensor | tuple[torch.Tensor, ...]:
if audio_input["type"] == "audio_embeds": if audio_input.type == "audio_embeds":
audio_embeds = audio_input["audio_embeds"] audio_embeds = audio_input.audio_embeds
return tuple(audio_embeds) return tuple(audio_embeds)
( (
+2 -1
View File
@@ -14,7 +14,7 @@ import torch
import torch.nn as nn import torch.nn as nn
from vllm.config import VllmConfig from vllm.config import VllmConfig
from vllm.config.multimodal import BaseDummyOptions from vllm.config.multimodal import BaseDummyOptions, ImageDummyOptions
from vllm.inputs import MultiModalDataDict from vllm.inputs import MultiModalDataDict
from vllm.logger import init_logger from vllm.logger import init_logger
from vllm.model_executor.layers.activation import get_act_fn from vllm.model_executor.layers.activation import get_act_fn
@@ -258,6 +258,7 @@ class BagelDummyInputsBuilder(BaseDummyInputsBuilder[BagelProcessingInfo]):
# Use the configured image size # Use the configured image size
image_size = vit_config.image_size image_size = vit_config.image_size
image_overrides = mm_options.get("image") image_overrides = mm_options.get("image")
assert image_overrides is None or isinstance(image_overrides, ImageDummyOptions)
return { return {
"image": self._get_dummy_images( "image": self._get_dummy_images(
+2 -2
View File
@@ -182,7 +182,7 @@ class BailingMLP(nn.Module):
intermediate_size: int, intermediate_size: int,
config: PretrainedConfig, config: PretrainedConfig,
quant_config: QuantizationConfig | None = None, quant_config: QuantizationConfig | None = None,
reduce_results: bool | None = True, reduce_results: bool = True,
prefix: str = "", prefix: str = "",
) -> None: ) -> None:
super().__init__() super().__init__()
@@ -279,7 +279,7 @@ class BailingMoE(nn.Module):
else: else:
intermediate_size = config.moe_intermediate_size intermediate_size = config.moe_intermediate_size
intermediate_size *= config.num_shared_experts intermediate_size *= config.num_shared_experts
self.shared_experts = BailingMLP( self.shared_experts: BailingMLP | None = BailingMLP(
intermediate_size=intermediate_size, intermediate_size=intermediate_size,
config=config, config=config,
quant_config=quant_config, quant_config=quant_config,
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import copy import copy
from collections.abc import Iterable from collections.abc import Callable, Iterable
import torch import torch
import torch.nn as nn import torch.nn as nn
@@ -133,19 +133,21 @@ class BailingMoeV25MLAAttention(nn.Module):
if self.q_lora_rank is not None: if self.q_lora_rank is not None:
# Use fused_qkv_a_proj when q_lora_rank is set # Use fused_qkv_a_proj when q_lora_rank is set
self.fused_qkv_a_proj = MergedColumnParallelLinear( self.fused_qkv_a_proj: MergedColumnParallelLinear | None = (
self.hidden_size, MergedColumnParallelLinear(
[self.q_lora_rank, self.kv_lora_rank + self.qk_rope_head_dim], self.hidden_size,
bias=False, [self.q_lora_rank, self.kv_lora_rank + self.qk_rope_head_dim],
quant_config=quant_config, bias=False,
prefix=f"{prefix}.fused_qkv_a_proj", quant_config=quant_config,
disable_tp=True, prefix=f"{prefix}.fused_qkv_a_proj",
disable_tp=True,
)
) )
self.q_a_layernorm = RMSNorm( self.q_a_layernorm: RMSNorm | None = RMSNorm(
self.q_lora_rank, self.q_lora_rank,
eps=config.rms_norm_eps, eps=config.rms_norm_eps,
) )
self.q_b_proj = ColumnParallelLinear( self.q_b_proj: ColumnParallelLinear | None = ColumnParallelLinear(
self.q_lora_rank, self.q_lora_rank,
self.num_heads * self.qk_head_dim, self.num_heads * self.qk_head_dim,
bias=False, bias=False,
@@ -311,6 +313,8 @@ class BailingMoeV25(nn.Module):
"score_function and correction_bias should be " "score_function and correction_bias should be "
"(softmax, None) or (sigmoid, not None)" "(softmax, None) or (sigmoid, not None)"
) )
else:
self.score_function = "softmax"
# Shared experts (using BailingMLP) # Shared experts (using BailingMLP)
if self.num_shared_experts > 0: if self.num_shared_experts > 0:
@@ -319,7 +323,7 @@ class BailingMoeV25(nn.Module):
else: else:
intermediate_size = config.moe_intermediate_size intermediate_size = config.moe_intermediate_size
intermediate_size *= config.num_shared_experts intermediate_size *= config.num_shared_experts
self.shared_experts = BailingMLP( self.shared_experts: BailingMLP | None = BailingMLP(
intermediate_size=intermediate_size, intermediate_size=intermediate_size,
config=config, config=config,
quant_config=quant_config, quant_config=quant_config,
@@ -613,7 +617,9 @@ class BailingMoeV25Model(nn.Module):
return False return False
param = params_dict[name] param = params_dict[name]
weight_loader = getattr(param, "weight_loader", default_weight_loader) weight_loader: Callable[..., None] = getattr(
param, "weight_loader", default_weight_loader
)
if shard_id is None: if shard_id is None:
weight_loader(param, tensor) weight_loader(param, tensor)
@@ -691,11 +697,16 @@ class BailingMoeV25Model(nn.Module):
continue continue
# Routed experts # Routed experts
for param_name, weight_name, expert_id, shard_id in expert_mappings: for (
param_name,
weight_name,
expert_id,
expert_shard_id,
) in expert_mappings:
if weight_name not in norm_name: if weight_name not in norm_name:
continue continue
mapped = norm_name.replace(weight_name, param_name) mapped = norm_name.replace(weight_name, param_name)
if load_param(mapped, weight, (expert_id, shard_id)): if load_param(mapped, weight, (expert_id, expert_shard_id)):
break break
continue continue
@@ -783,7 +794,7 @@ class BailingMoeV25ForCausalLM(nn.Module, HasInnerState, IsHybrid, SupportsPP):
def get_mamba_state_shape_from_config( def get_mamba_state_shape_from_config(
cls, cls,
vllm_config: VllmConfig, vllm_config: VllmConfig,
) -> tuple[tuple[int, ...], ...]: ) -> tuple[tuple[int, int, int]]:
"""Calculate shape for linear attention cache.""" """Calculate shape for linear attention cache."""
config = vllm_config.model_config.hf_config config = vllm_config.model_config.hf_config
tp_size = vllm_config.parallel_config.tensor_parallel_size tp_size = vllm_config.parallel_config.tensor_parallel_size
@@ -2,7 +2,7 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Inference-only Bailing MoE v2.5 MTP model.""" """Inference-only Bailing MoE v2.5 MTP model."""
from collections.abc import Iterable from collections.abc import Callable, Iterable
import torch import torch
import torch.nn as nn import torch.nn as nn
@@ -268,14 +268,17 @@ class BailingMoeV25MTPModel(nn.Module):
loaded_weight: torch.Tensor, loaded_weight: torch.Tensor,
shard_id=None, shard_id=None,
) -> bool: ) -> bool:
name = maybe_remap_kv_scale_name(name, params_dict) remapped_name = maybe_remap_kv_scale_name(name, params_dict)
if name is None: if remapped_name is None:
return False return False
name = remapped_name
if name not in params_dict or is_pp_missing_parameter(name, self): if name not in params_dict or is_pp_missing_parameter(name, self):
return False return False
param = params_dict[name] param = params_dict[name]
weight_loader = getattr(param, "weight_loader", default_weight_loader) weight_loader: Callable[..., None] = getattr(
param, "weight_loader", default_weight_loader
)
if shard_id is None: if shard_id is None:
weight_loader(param, loaded_weight) weight_loader(param, loaded_weight)
elif isinstance(shard_id, int): elif isinstance(shard_id, int):
@@ -351,14 +354,14 @@ class BailingMoeV25MTPModel(nn.Module):
if "mlp.experts" in name: if "mlp.experts" in name:
for mapping in expert_params_mapping: for mapping in expert_params_mapping:
param_name, weight_name, expert_id, shard_id = mapping param_name, weight_name, expert_id, expert_shard_id = mapping
if weight_name not in name: if weight_name not in name:
continue continue
mapped_name = name.replace(weight_name, param_name) mapped_name = name.replace(weight_name, param_name)
if load_param( if load_param(
mapped_name, mapped_name,
loaded_weight, loaded_weight,
(expert_id, shard_id), (expert_id, expert_shard_id),
): ):
loaded = True loaded = True
break break
+21 -18
View File
@@ -9,7 +9,7 @@ vLLM's parallel linear layers, MLA kernel, KDA kernel and fused MoE loader.
""" """
import copy import copy
from collections.abc import Iterable from collections.abc import Callable, Iterable
import torch import torch
import torch.nn as nn import torch.nn as nn
@@ -75,7 +75,6 @@ from vllm.third_party.flash_linear_attention.ops.kda import (
fused_recurrent_kda_fwd, fused_recurrent_kda_fwd,
) )
from vllm.utils.torch_utils import direct_register_custom_op from vllm.utils.torch_utils import direct_register_custom_op
from vllm.v1.attention.backend import AttentionMetadata
from vllm.v1.attention.backends.gdn_attn import GDNAttentionMetadata from vllm.v1.attention.backends.gdn_attn import GDNAttentionMetadata
from vllm.v1.attention.backends.registry import MambaAttentionBackendEnum from vllm.v1.attention.backends.registry import MambaAttentionBackendEnum
@@ -136,7 +135,7 @@ def _is_kda_layer(
def _get_kda_state_shape_for_config( def _get_kda_state_shape_for_config(
vllm_config: VllmConfig, vllm_config: VllmConfig,
) -> tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...], tuple[int, ...]]: ) -> tuple[tuple[int, int], tuple[int, int, int]]:
config = vllm_config.model_config.hf_config config = vllm_config.model_config.hf_config
num_spec = ( num_spec = (
vllm_config.speculative_config.num_speculative_tokens vllm_config.speculative_config.num_speculative_tokens
@@ -205,7 +204,7 @@ class BailingMoeV3MLP(nn.Module):
intermediate_size: int, intermediate_size: int,
config: PretrainedConfig, config: PretrainedConfig,
quant_config: QuantizationConfig | None = None, quant_config: QuantizationConfig | None = None,
reduce_results: bool | None = True, reduce_results: bool = True,
prefix: str = "", prefix: str = "",
swiglu_limit: float | None = None, swiglu_limit: float | None = None,
) -> None: ) -> None:
@@ -226,9 +225,7 @@ class BailingMoeV3MLP(nn.Module):
prefix=f"{prefix}.down_proj", prefix=f"{prefix}.down_proj",
) )
self.act_fn = ( self.act_fn = (
SwigluStepAndMul(limit=swiglu_limit) SwigluStepAndMul(limit=swiglu_limit) if swiglu_limit else SiluAndMul()
if swiglu_limit not in (None, 0)
else SiluAndMul()
) )
def forward(self, x): def forward(self, x):
@@ -264,6 +261,8 @@ class BailingMoeV3MLAAttention(nn.Module):
self.num_local_heads = self.num_heads // tp_size self.num_local_heads = self.num_heads // tp_size
self.scaling = self.qk_head_dim**-0.5 self.scaling = self.qk_head_dim**-0.5
self.q_proj: ColumnParallelLinear | None
self.kv_a_proj_with_mqa: ReplicatedLinear | None
if self.q_lora_rank is None: if self.q_lora_rank is None:
self.q_proj = ColumnParallelLinear( self.q_proj = ColumnParallelLinear(
self.hidden_size, self.hidden_size,
@@ -400,7 +399,7 @@ class BailingMoeV3KimiDeltaAttention(PluggableLayer, MambaBase):
def get_state_dtype( def get_state_dtype(
self, self,
) -> tuple[torch.dtype, torch.dtype, torch.dtype, torch.dtype]: ) -> tuple[torch.dtype, torch.dtype]:
if self.model_config is None or self.cache_config is None: if self.model_config is None or self.cache_config is None:
raise ValueError("model_config and cache_config must be set") raise ValueError("model_config and cache_config must be set")
return MambaStateDtypeCalculator.kda_state_dtype( return MambaStateDtypeCalculator.kda_state_dtype(
@@ -409,7 +408,7 @@ class BailingMoeV3KimiDeltaAttention(PluggableLayer, MambaBase):
def get_state_shape( def get_state_shape(
self, self,
) -> tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...], tuple[int, ...]]: ) -> tuple[tuple[int, int], tuple[int, int, int]]:
return MambaStateShapeCalculator.kda_state_shape( return MambaStateShapeCalculator.kda_state_shape(
self.tp_size, self.tp_size,
self.num_heads, self.num_heads,
@@ -579,12 +578,12 @@ class BailingMoeV3KimiDeltaAttention(PluggableLayer, MambaBase):
core_attn_out: torch.Tensor, core_attn_out: torch.Tensor,
) -> None: ) -> None:
forward_context = get_forward_context() forward_context = get_forward_context()
attn_metadata: AttentionMetadata = forward_context.attn_metadata attn_metadata_map = forward_context.attn_metadata
if attn_metadata is None: if attn_metadata_map is None:
return return
assert isinstance(attn_metadata, dict) assert isinstance(attn_metadata_map, dict)
attn_metadata = attn_metadata[self.prefix] attn_metadata = attn_metadata_map[self.prefix]
assert isinstance(attn_metadata, GDNAttentionMetadata) assert isinstance(attn_metadata, GDNAttentionMetadata)
has_initial_state = attn_metadata.has_initial_state has_initial_state = attn_metadata.has_initial_state
spec_query_start_loc = attn_metadata.spec_query_start_loc spec_query_start_loc = attn_metadata.spec_query_start_loc
@@ -825,6 +824,7 @@ class BailingMoeV3KimiDeltaAttention(PluggableLayer, MambaBase):
assert q is not None and k is not None and v is not None assert q is not None and k is not None and v is not None
assert g1_non_spec is not None and beta_non_spec is not None assert g1_non_spec is not None and beta_non_spec is not None
assert state_indices is not None assert state_indices is not None
assert has_initial_state is not None
zero_idx = state_indices[~has_initial_state] zero_idx = state_indices[~has_initial_state]
recurrent_state[zero_idx] = 0 recurrent_state[zero_idx] = 0
initial_state = recurrent_state_active[state_indices].contiguous() initial_state = recurrent_state_active[state_indices].contiguous()
@@ -846,6 +846,7 @@ class BailingMoeV3KimiDeltaAttention(PluggableLayer, MambaBase):
assert q is not None and k is not None and v is not None assert q is not None and k is not None and v is not None
assert g1_non_spec is not None and beta_non_spec is not None assert g1_non_spec is not None and beta_non_spec is not None
assert state_indices is not None assert state_indices is not None
assert query_start_loc is not None
out, _ = fused_recurrent_kda( out, _ = fused_recurrent_kda(
q=q, q=q,
k=k, k=k,
@@ -1205,13 +1206,13 @@ class BailingMoeV3ForCausalLM(nn.Module, HasInnerState, IsHybrid, SupportsPP):
@classmethod @classmethod
def get_mamba_state_shape_from_config( def get_mamba_state_shape_from_config(
cls, vllm_config: VllmConfig cls, vllm_config: VllmConfig
) -> tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...], tuple[int, ...]]: ) -> tuple[tuple[int, int], tuple[int, int, int]]:
return _get_kda_state_shape_for_config(vllm_config) return _get_kda_state_shape_for_config(vllm_config)
@classmethod @classmethod
def get_mamba_state_dtype_from_config( def get_mamba_state_dtype_from_config(
cls, vllm_config: VllmConfig cls, vllm_config: VllmConfig
) -> tuple[torch.dtype, torch.dtype, torch.dtype, torch.dtype]: ) -> tuple[torch.dtype, torch.dtype]:
return MambaStateDtypeCalculator.kda_state_dtype( return MambaStateDtypeCalculator.kda_state_dtype(
vllm_config.model_config.dtype, vllm_config.model_config.dtype,
vllm_config.cache_config.mamba_cache_dtype, vllm_config.cache_config.mamba_cache_dtype,
@@ -1243,7 +1244,9 @@ class BailingMoeV3ForCausalLM(nn.Module, HasInnerState, IsHybrid, SupportsPP):
if name not in params_dict or is_pp_missing_parameter(name, self): if name not in params_dict or is_pp_missing_parameter(name, self):
return False return False
param = params_dict[name] param = params_dict[name]
weight_loader = getattr(param, "weight_loader", default_weight_loader) weight_loader: Callable[..., None] = getattr(
param, "weight_loader", default_weight_loader
)
if shard_id is None: if shard_id is None:
weight_loader(param, tensor) weight_loader(param, tensor)
elif isinstance(shard_id, int): elif isinstance(shard_id, int):
@@ -1268,10 +1271,10 @@ class BailingMoeV3ForCausalLM(nn.Module, HasInnerState, IsHybrid, SupportsPP):
if name is None: if name is None:
continue continue
loaded = False loaded = False
for param_suf, weight_suf, shard_id in stacked_mappings: for param_suf, weight_suf, stacked_shard_id in stacked_mappings:
if weight_suf in name: if weight_suf in name:
mapped = name.replace(weight_suf, param_suf) mapped = name.replace(weight_suf, param_suf)
if load_param(mapped, weight, shard_id): if load_param(mapped, weight, stacked_shard_id):
loaded = True loaded = True
break break
if loaded: if loaded:
@@ -2,7 +2,7 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Inference-only Bailing MoE v3 MTP model.""" """Inference-only Bailing MoE v3 MTP model."""
from collections.abc import Iterable from collections.abc import Callable, Iterable
import torch import torch
import torch.nn as nn import torch.nn as nn
@@ -233,11 +233,12 @@ class BailingMoeV3MTPModel(nn.Module, SupportsPP):
self, self,
input_ids: torch.Tensor | None, input_ids: torch.Tensor | None,
positions: torch.Tensor, positions: torch.Tensor,
hidden_states: torch.Tensor, hidden_states: torch.Tensor | None = None,
intermediate_tensors: IntermediateTensors | None = None, intermediate_tensors: IntermediateTensors | None = None,
inputs_embeds: torch.Tensor | None = None, inputs_embeds: torch.Tensor | None = None,
spec_step_idx: int = 0, spec_step_idx: int = 0,
) -> torch.Tensor: ) -> torch.Tensor:
assert hidden_states is not None
del intermediate_tensors del intermediate_tensors
return self.model( return self.model(
input_ids, input_ids,
@@ -280,14 +281,17 @@ class BailingMoeV3MTPModel(nn.Module, SupportsPP):
loaded_weight: torch.Tensor, loaded_weight: torch.Tensor,
shard_id=None, shard_id=None,
) -> bool: ) -> bool:
name = maybe_remap_kv_scale_name(name, params_dict) remapped_name = maybe_remap_kv_scale_name(name, params_dict)
if name is None: if remapped_name is None:
return False return False
name = remapped_name
if name not in params_dict or is_pp_missing_parameter(name, self): if name not in params_dict or is_pp_missing_parameter(name, self):
return False return False
param = params_dict[name] param = params_dict[name]
weight_loader = getattr(param, "weight_loader", default_weight_loader) weight_loader: Callable[..., None] = getattr(
param, "weight_loader", default_weight_loader
)
if shard_id is None: if shard_id is None:
weight_loader(param, loaded_weight) weight_loader(param, loaded_weight)
elif isinstance(shard_id, int): elif isinstance(shard_id, int):
@@ -347,13 +351,13 @@ class BailingMoeV3MTPModel(nn.Module, SupportsPP):
name = normalize_name(name) name = normalize_name(name)
loaded = False loaded = False
for param_name, weight_name, shard_id in stacked_params_mapping: for param_name, weight_name, stacked_shard_id in stacked_params_mapping:
if weight_name not in name: if weight_name not in name:
continue continue
if "mlp.experts." in name and name not in params_dict: if "mlp.experts." in name and name not in params_dict:
continue continue
mapped_name = name.replace(weight_name, param_name) mapped_name = name.replace(weight_name, param_name)
if load_param(mapped_name, loaded_weight, shard_id): if load_param(mapped_name, loaded_weight, stacked_shard_id):
loaded = True loaded = True
break break
if loaded: if loaded:
+2 -1
View File
@@ -8,7 +8,7 @@ import torch.nn as nn
from transformers.activations import GELUActivation from transformers.activations import GELUActivation
from vllm.config import VllmConfig from vllm.config import VllmConfig
from vllm.config.multimodal import BaseDummyOptions from vllm.config.multimodal import BaseDummyOptions, ImageDummyOptions
from vllm.inputs import MultiModalDataDict from vllm.inputs import MultiModalDataDict
from vllm.multimodal import MULTIMODAL_REGISTRY from vllm.multimodal import MULTIMODAL_REGISTRY
@@ -97,6 +97,7 @@ class BeeDummyInputsBuilder(LlavaDummyInputsBuilder[BeeProcessingInfo]):
target_width, target_height = self.info.get_image_size_with_most_features() target_width, target_height = self.info.get_image_size_with_most_features()
image_overrides = mm_options.get("image") image_overrides = mm_options.get("image")
assert image_overrides is None or isinstance(image_overrides, ImageDummyOptions)
return { return {
"image": self._get_dummy_images( "image": self._get_dummy_images(
+1
View File
@@ -97,6 +97,7 @@ class BertPooler(SequencePooler):
def __init__(self, model_config: ModelConfig): def __init__(self, model_config: ModelConfig):
pooler_config = model_config.pooler_config pooler_config = model_config.pooler_config
assert pooler_config is not None assert pooler_config is not None
assert pooler_config.seq_pooling_type is not None
config: BertConfig = model_config.hf_config config: BertConfig = model_config.hf_config
+18 -17
View File
@@ -57,8 +57,8 @@ class BertWithRopeEmbedding(nn.Module):
config.vocab_size, config.hidden_size config.vocab_size, config.hidden_size
) )
if config.type_vocab_size > 0: if config.type_vocab_size > 0:
self.token_type_embeddings = VocabParallelEmbedding( self.token_type_embeddings: VocabParallelEmbedding | None = (
config.type_vocab_size, config.hidden_size VocabParallelEmbedding(config.type_vocab_size, config.hidden_size)
) )
else: else:
self.token_type_embeddings = None self.token_type_embeddings = None
@@ -127,6 +127,7 @@ class BertWithRopeAttention(nn.Module):
prefix=f"{prefix}.qkv_proj", prefix=f"{prefix}.qkv_proj",
) )
assert rotary_kwargs is not None
self.rotary_emb = get_rope(**rotary_kwargs) self.rotary_emb = get_rope(**rotary_kwargs)
self.attn = EncoderOnlyAttention( self.attn = EncoderOnlyAttention(
@@ -466,7 +467,7 @@ class BertWithRope(nn.Module, SupportsQuant):
) )
if add_pooling_layer: if add_pooling_layer:
self.pooler = BertPooler(vllm_config.model_config) self.pooler: BertPooler | None = BertPooler(vllm_config.model_config)
else: else:
self.pooler = None self.pooler = None
@@ -632,7 +633,7 @@ class JinaRobertaModel(BertWithRope):
scaling = self.config.lora_alpha / self.config.lora_rank scaling = self.config.lora_alpha / self.config.lora_rank
device = self.vllm_config.device_config.device device = self.vllm_config.device_config.device
weights = {name: weight for name, weight in weights} weights_dict = {name: weight for name, weight in weights}
o = ".original" o = ".original"
a = ".0.lora_A" a = ".0.lora_A"
@@ -641,34 +642,34 @@ class JinaRobertaModel(BertWithRope):
# text-matching # text-matching
i = -1 i = -1
for name in list(weights.keys()): for name in list(weights_dict.keys()):
if o in name: if o in name:
dtype = weights[name].dtype dtype = weights_dict[name].dtype
shape = weights[name].shape shape = weights_dict[name].shape
weight_name = name[: -len(o)] weight_name = name[: -len(o)]
if "embeddings" in weight_name: if "embeddings" in weight_name:
B = weights[weight_name + a][i].to(device).float() B = weights_dict[weight_name + a][i].to(device).float()
A = weights[weight_name + b][i].to(device).float() A = weights_dict[weight_name + b][i].to(device).float()
else: else:
B = weights[weight_name + b][i].to(device).float() B = weights_dict[weight_name + b][i].to(device).float()
A = weights[weight_name + a][i].to(device).float() A = weights_dict[weight_name + a][i].to(device).float()
weight = ( weight = (
weights[weight_name + o].to(device) weights_dict[weight_name + o].to(device)
+ torch.matmul(B, A).view(shape) * scaling + torch.matmul(B, A).view(shape) * scaling
) )
weight = weight.cpu().to(dtype) weight = weight.cpu().to(dtype)
weights[weight_name.replace(".parametrizations", "")] = weight weights_dict[weight_name.replace(".parametrizations", "")] = weight
del ( del (
weights[weight_name + o], weights_dict[weight_name + o],
weights[weight_name + a], weights_dict[weight_name + a],
weights[weight_name + b], weights_dict[weight_name + b],
) )
return [(name, weight) for name, weight in weights.items()] return [(name, weight) for name, weight in weights_dict.items()]
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
weights = self.jina_merge_lora_weights(weights) weights = self.jina_merge_lora_weights(weights)
+4 -3
View File
@@ -14,7 +14,7 @@ from transformers import (
) )
from vllm.config import CacheConfig, VllmConfig from vllm.config import CacheConfig, VllmConfig
from vllm.config.multimodal import BaseDummyOptions from vllm.config.multimodal import BaseDummyOptions, ImageDummyOptions
from vllm.inputs import MultiModalDataDict from vllm.inputs import MultiModalDataDict
from vllm.model_executor.layers.activation import get_act_fn from vllm.model_executor.layers.activation import get_act_fn
from vllm.model_executor.layers.quantization import QuantizationConfig from vllm.model_executor.layers.quantization import QuantizationConfig
@@ -454,6 +454,7 @@ class Blip2DummyInputsBuilder(BaseDummyInputsBuilder[Blip2ProcessingInfo]):
num_images = mm_counts.get("image", 0) num_images = mm_counts.get("image", 0)
image_overrides = mm_options.get("image") image_overrides = mm_options.get("image")
assert image_overrides is None or isinstance(image_overrides, ImageDummyOptions)
return { return {
"image": self._get_dummy_images( "image": self._get_dummy_images(
@@ -620,8 +621,8 @@ class Blip2ForConditionalGeneration(
return self._image_pixels_to_features(self.vision_model, pixel_values) return self._image_pixels_to_features(self.vision_model, pixel_values)
def _process_image_input(self, image_input: Blip2ImageInputs) -> torch.Tensor: def _process_image_input(self, image_input: Blip2ImageInputs) -> torch.Tensor:
if image_input["type"] == "image_embeds": if image_input.type == "image_embeds":
return image_input["data"] return image_input.data
image_features = self._process_image_pixels(image_input) image_features = self._process_image_pixels(image_input)
@@ -19,6 +19,7 @@
"""PyTorch Idefics2 model.""" """PyTorch Idefics2 model."""
from collections.abc import Iterable from collections.abc import Iterable
from typing import ClassVar
import torch import torch
from torch import nn from torch import nn
@@ -352,7 +353,7 @@ class Idefics2Encoder(nn.Module):
class Idefics2VisionTransformer(nn.Module): class Idefics2VisionTransformer(nn.Module):
hf_to_vllm_mapper = WeightsMapper( hf_to_vllm_mapper: ClassVar[WeightsMapper] = WeightsMapper(
orig_to_new_stacked={ orig_to_new_stacked={
".q_proj": (".qkv_proj", "q"), ".q_proj": (".qkv_proj", "q"),
".k_proj": (".qkv_proj", "k"), ".k_proj": (".qkv_proj", "k"),
+28 -25
View File
@@ -71,6 +71,13 @@ The output embeddings must be one of the following formats:
- A single 3D tensor, with the batch dimension grouping the 2D tensors. - A single 3D tensor, with the batch dimension grouping the 2D tensors.
""" """
MambaStateShapes: TypeAlias = (
tuple[tuple[int, int]]
| tuple[tuple[int, int, int]]
| tuple[tuple[int, int], tuple[int, int]]
| tuple[tuple[int, int], tuple[int, int, int]]
)
@dataclass(frozen=True, slots=True) @dataclass(frozen=True, slots=True)
class DiarizedTranscriptionSegment: class DiarizedTranscriptionSegment:
@@ -616,7 +623,7 @@ class SupportsLoRA(Protocol):
# The `embedding_module` and `embedding_padding_modules` # The `embedding_module` and `embedding_padding_modules`
# are empty by default. # are empty by default.
embedding_modules: ClassVar[dict[str, str]] = {} embedding_modules: ClassVar[dict[str, str]] = {}
packed_modules_mapping: dict[str, list[str]] = {} packed_modules_mapping: ClassVar[dict[str, list[str]]] = {}
# Module prefixes to skip during LoRA loading (e.g., ["mtp."] for MTP layers) # Module prefixes to skip during LoRA loading (e.g., ["mtp."] for MTP layers)
lora_skip_prefixes: ClassVar[list[str]] = [] lora_skip_prefixes: ClassVar[list[str]] = []
lora_manager: "LoRAModelManager | None" lora_manager: "LoRAModelManager | None"
@@ -678,6 +685,15 @@ def _supports_lora(model: type[object] | object) -> bool:
return isinstance(model, SupportsLoRA) return isinstance(model, SupportsLoRA)
class _MakeEmptyIntermediateTensors(Protocol):
def __call__(
self,
batch_size: int,
dtype: torch.dtype,
device: torch.device,
) -> "IntermediateTensors": ...
@runtime_checkable @runtime_checkable
class SupportsPP(Protocol): class SupportsPP(Protocol):
"""The interface required for all models that support pipeline parallel.""" """The interface required for all models that support pipeline parallel."""
@@ -691,14 +707,8 @@ class SupportsPP(Protocol):
MRO of your model class. MRO of your model class.
""" """
def make_empty_intermediate_tensors( make_empty_intermediate_tensors: _MakeEmptyIntermediateTensors
self, """Called when PP rank > 0 for profiling purposes."""
batch_size: int,
dtype: torch.dtype,
device: torch.device,
) -> "IntermediateTensors":
"""Called when PP rank > 0 for profiling purposes."""
...
def forward( def forward(
self, self,
@@ -706,7 +716,7 @@ class SupportsPP(Protocol):
positions: Tensor, positions: Tensor,
*, *,
intermediate_tensors: "IntermediateTensors | None", intermediate_tensors: "IntermediateTensors | None",
) -> "IntermediateTensors | None": ) -> "Tensor | IntermediateTensors | tuple[Tensor, list[Tensor]]":
""" """
Accept [`IntermediateTensors`][vllm.sequence.IntermediateTensors] when Accept [`IntermediateTensors`][vllm.sequence.IntermediateTensors] when
PP rank > 0. PP rank > 0.
@@ -723,12 +733,7 @@ class SupportsPP(Protocol):
class _SupportsPPType(Protocol): class _SupportsPPType(Protocol):
supports_pp: Literal[True] supports_pp: Literal[True]
def make_empty_intermediate_tensors( make_empty_intermediate_tensors: _MakeEmptyIntermediateTensors
self,
batch_size: int,
dtype: torch.dtype,
device: torch.device,
) -> "IntermediateTensors": ...
def forward( def forward(
self, self,
@@ -736,7 +741,7 @@ class _SupportsPPType(Protocol):
positions: Tensor, positions: Tensor,
*, *,
intermediate_tensors: "IntermediateTensors | None", intermediate_tensors: "IntermediateTensors | None",
) -> "Tensor | IntermediateTensors": ... ) -> "Tensor | IntermediateTensors | tuple[Tensor, list[Tensor]]": ...
@overload @overload
@@ -785,7 +790,7 @@ def supports_pp(
def _supports_pp_attributes(model: type[object] | object) -> bool: def _supports_pp_attributes(model: type[object] | object) -> bool:
if isinstance(model, type): if isinstance(model, type):
return isinstance(model, _SupportsPPType) return SupportsPP in model.__mro__ or isinstance(model, _SupportsPPType)
return isinstance(model, SupportsPP) return isinstance(model, SupportsPP)
@@ -867,16 +872,14 @@ class IsHybrid(Protocol):
def get_mamba_state_shape_from_config( def get_mamba_state_shape_from_config(
cls, cls,
vllm_config: "VllmConfig", vllm_config: "VllmConfig",
) -> tuple[tuple[int, int], tuple[int, int, int]]: ) -> MambaStateShapes:
"""Calculate shapes for Mamba's convolutional and state caches. """Calculate shapes for Mamba's convolutional and state caches.
Args: Args:
vllm_config: vLLM config vllm_config: vLLM config
Returns: Returns:
Tuple containing: Shapes for each state cache used by the model.
- conv_state_shape: Shape for convolutional state cache
- temporal_state_shape: Shape for state space model cache
""" """
... ...
@@ -1090,7 +1093,7 @@ class SupportsQuant:
"""The interface required for all models that support quantization.""" """The interface required for all models that support quantization."""
hf_to_vllm_mapper: ClassVar["WeightsMapper | None"] = None hf_to_vllm_mapper: ClassVar["WeightsMapper | None"] = None
packed_modules_mapping: ClassVar[dict[str, list[str]] | None] = None packed_modules_mapping: ClassVar[dict[str, list[str]]]
quant_config: QuantizationConfig | None = None quant_config: QuantizationConfig | None = None
def __new__(cls, *args, **kwargs) -> Self: def __new__(cls, *args, **kwargs) -> Self:
@@ -1125,8 +1128,8 @@ class SupportsQuant:
if (hf_to_vllm_mapper := self.hf_to_vllm_mapper) is not None: if (hf_to_vllm_mapper := self.hf_to_vllm_mapper) is not None:
unstacked_mapper = hf_to_vllm_mapper.get_unstacked_mapper() unstacked_mapper = hf_to_vllm_mapper.get_unstacked_mapper()
self.quant_config.apply_vllm_mapper(unstacked_mapper) self.quant_config.apply_vllm_mapper(unstacked_mapper)
if self.packed_modules_mapping is not None: if packed_modules_mapping := getattr(self, "packed_modules_mapping", None):
self.quant_config.packed_modules_mapping.update(self.packed_modules_mapping) self.quant_config.packed_modules_mapping.update(packed_modules_mapping)
@runtime_checkable @runtime_checkable
+2 -1
View File
@@ -26,6 +26,7 @@
from collections.abc import Iterable from collections.abc import Iterable
from itertools import islice from itertools import islice
from typing import ClassVar
import torch import torch
from torch import nn from torch import nn
@@ -342,7 +343,7 @@ class LlamaDecoderLayer(nn.Module):
}, },
) )
class LlamaModel(nn.Module, EagleModelMixin): class LlamaModel(nn.Module, EagleModelMixin):
hf_to_vllm_mapper = WeightsMapper( hf_to_vllm_mapper: ClassVar[WeightsMapper] = WeightsMapper(
orig_to_new_stacked={ orig_to_new_stacked={
# weight_name: (param_name, shard_id) # weight_name: (param_name, shard_id)
".q_proj": (".qkv_proj", "q"), ".q_proj": (".qkv_proj", "q"),
+1 -3
View File
@@ -204,9 +204,7 @@ class BaseLlavaNextMultiModalProcessor(BaseLlavaMultiModalProcessor[_I]):
raise NotImplementedError raise NotImplementedError
class LlavaNextMultiModalProcessor( class LlavaNextMultiModalProcessor(BaseLlavaNextMultiModalProcessor[_I]):
BaseLlavaNextMultiModalProcessor[LlavaNextProcessingInfo]
):
def _get_mm_fields_config( def _get_mm_fields_config(
self, self,
hf_inputs: BatchFeature, hf_inputs: BatchFeature,