[Bugfix] Resolve seq-cls num_labels from the top-level config for multimodal checkpoints (#50950)

Signed-off-by: Lazurite <43494437+Rapisurazurite@users.noreply.github.com>
This commit is contained in:
Lazurite
2026-08-04 11:39:39 +00:00
committed by GitHub
parent a1657a0235
commit 6a9fdf0dc7
2 changed files with 71 additions and 2 deletions
+24 -1
View File
@@ -261,6 +261,29 @@ def as_embedding_model(cls: _T) -> _T:
return ModelForEmbedding # type: ignore
def _resolve_num_labels(hf_config: Any, text_config: Any) -> int:
"""Resolve the label count for a sequence classification head.
``PretrainedConfig.num_labels`` is derived from ``id2label``, which always
carries a default of two entries. Composite configs (such as multimodal
checkpoints) declare their label space on the top-level config, so reading
``num_labels`` from ``get_text_config()`` silently returns that default and
builds a score head of the wrong size.
Prefer the top-level config whenever it declares a label space of its own,
mirroring the ``classifier_from_token`` / ``method`` lookups in
``as_seq_cls_model``.
"""
if text_config is hf_config:
return hf_config.num_labels
from transformers import PretrainedConfig
if hf_config.num_labels != PretrainedConfig().num_labels:
return hf_config.num_labels
return text_config.num_labels
def as_seq_cls_model(cls: _T) -> _T:
"""
Subclass an existing vLLM model to support classify and score tasks.
@@ -320,7 +343,7 @@ def as_seq_cls_model(cls: _T) -> _T:
self.score = ReplicatedLinear(
model_config.get_hidden_size(),
text_config.num_labels,
_resolve_num_labels(hf_config, text_config),
bias=False,
params_dtype=model_config.head_dtype,
quant_config=quant_config,