[BugFix][Pooling] Skip weight-prefix probe when model has WeightsMapper (#50890)

Signed-off-by: Juqi Li <2223621784@qq.com>
Co-authored-by: Cyrus Leung <tlleungac@connect.ust.hk>
This commit is contained in:
Li Juqi
2026-08-05 21:12:33 -07:00
committed by GitHub
co-authored by Cyrus Leung
parent f84df12c6a
commit 821717118f
2 changed files with 108 additions and 2 deletions
+95 -1
View File
@@ -10,7 +10,11 @@ from vllm.model_executor.models.adapters import (
_create_pooling_model_cls,
_resolve_num_labels,
)
from vllm.model_executor.models.utils import AutoWeightsLoader, StageMissingLayer
from vllm.model_executor.models.utils import (
AutoWeightsLoader,
StageMissingLayer,
WeightsMapper,
)
pytestmark = pytest.mark.cpu_test
@@ -152,6 +156,96 @@ def test_pooling_load_weights_clones_probed_weights():
_load_and_compare(_make_pooling_model(PackedWeightModel), ref, expected)
class _LanguageModelInner(torch.nn.Module):
def __init__(self):
super().__init__()
self.embed = torch.nn.Linear(4, 8, bias=False)
self.layer0 = torch.nn.Linear(8, 8, bias=False)
class _LanguageModelWrapper(torch.nn.Module):
def __init__(self):
super().__init__()
self.model = _LanguageModelInner()
class ModelWithWeightsMapper(torch.nn.Module):
"""Stand-in for models whose keys only align after hf_to_vllm_mapper.
Checkpoint keys like ``model.language_model.*`` never match
``""`` / ``model.`` + raw name against ``language_model.model.*`` params.
The pooling prefix probe must consult the mapper so it can early-exit.
"""
hf_to_vllm_mapper = WeightsMapper(
orig_to_new_prefix={
"model.language_model.": "language_model.model.",
}
)
def __init__(self):
super().__init__()
self.language_model = _LanguageModelWrapper()
self.lm_head = torch.nn.Linear(8, 16, bias=False)
def load_weights(self, weights):
params = dict(self.named_parameters())
loaded = set()
self.seen_names = []
for name, tensor in weights:
self.seen_names.append(name)
mapped = self.hf_to_vllm_mapper._map_name(name)
if mapped is not None and mapped in params:
params[mapped].data.copy_(tensor)
loaded.add(mapped)
return loaded
def test_pooling_prefix_probe_uses_hf_to_vllm_mapper(monkeypatch):
"""Mapper lets the prefix probe early-exit without cloning the full ckpt.
Checkpoint keys only align after ``hf_to_vllm_mapper``. The probe must use
the mapped name for membership, keep forwarding original names to the
parent loader, and stop after the first hit instead of cloning everything.
"""
ref = {
"model.language_model.embed.weight": torch.randn(8, 4),
"model.language_model.layer0.weight": torch.randn(8, 8),
"model.language_model.extra0.weight": torch.randn(8, 8),
"model.language_model.extra1.weight": torch.randn(8, 8),
"model.language_model.extra2.weight": torch.randn(8, 8),
}
# Sanity: none of these keys match the generic "" / "model." probe.
model = _make_pooling_model(ModelWithWeightsMapper)
params = dict(model.named_parameters())
for name in ref:
assert name not in params
assert f"model.{name}" not in params
clone_count = 0
original_clone = torch.Tensor.clone
def counting_clone(self, *args, **kwargs):
nonlocal clone_count
clone_count += 1
return original_clone(self, *args, **kwargs)
monkeypatch.setattr(torch.Tensor, "clone", counting_clone)
loaded = model.load_weights(iter(ref.items()))
# First key maps onto a param, so the probe clones once and breaks.
assert clone_count == 1
assert clone_count < len(ref)
# Parent still receives original (unmapped, unprefixed) checkpoint names.
assert model.seen_names == list(ref.keys())
assert loaded == {
"language_model.model.embed.weight",
"language_model.model.layer0.weight",
}
def _composite_config(outer_labels=None, inner_labels=None):
"""Build a multimodal-style config whose text_config is a separate object."""
config = Gemma3Config(text_config={"num_hidden_layers": 1})
+13 -1
View File
@@ -175,6 +175,11 @@ def _create_pooling_model_cls(orig_cls: _T) -> _T:
def load_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.*)
# so the "" / "model." scan can early-exit. Forwarded names stay
# unmapped to avoid double-applying the mapper in the parent loader.
hf_to_vllm_mapper = getattr(self, "hf_to_vllm_mapper", None)
# We support loading from both `*ForCausalLM` and `*Model`
candidate_prefixes = ["", "model."]
@@ -185,11 +190,18 @@ def _create_pooling_model_cls(orig_cls: _T) -> _T:
# Clone because the iterator may reuse the tensor buffer
seen_weights.append((name, loaded_weight.clone()))
lookup_name = name
if hf_to_vllm_mapper is not None:
mapped = hf_to_vllm_mapper._map_name(name)
if mapped is None:
continue
lookup_name = mapped
try:
target_prefix = next(
prefix
for prefix in candidate_prefixes
if prefix + name in params_dict
if prefix + lookup_name in params_dict
)
break
except StopIteration: