Unverified Commit 8020a604 authored by RickyChen / 陳昭儒's avatar RickyChen / 陳昭儒 Committed by GitHub
Browse files

[Bugfix] Fix Qwen3-VL-Reranker model loading for sequence classification (#32089)


Signed-off-by: default avatarrickychen-infinirc <ricky.chen@infinirc.com>
Signed-off-by: default avatarIsotr0py <mozf@mail2.sysu.edu.cn>
Co-authored-by: default avatarIsotr0py <mozf@mail2.sysu.edu.cn>
parent e15a5ff0
...@@ -401,24 +401,23 @@ def load_weights_using_from_2_way_softmax( ...@@ -401,24 +401,23 @@ def load_weights_using_from_2_way_softmax(
tokens = cast(list[int], tokens) tokens = cast(list[int], tokens)
assert len(tokens) == 2 assert len(tokens) == 2
model.lm_head = ParallelLMHead( language_model = (
model.get_language_model() if hasattr(model, "get_language_model") else model
)
language_model.lm_head = ParallelLMHead(
text_config.vocab_size, text_config.hidden_size, quant_config=quant_config text_config.vocab_size, text_config.hidden_size, quant_config=quant_config
) )
if text_config.tie_word_embeddings: if text_config.tie_word_embeddings:
# embed_tokens is the assumed name for input embeddings. If the model does not # embed_tokens is the assumed name for input embeddings. If the model does not
# have this attribute, we fall back to get_input_embeddings(), which is used by # have this attribute, we fall back to get_input_embeddings(), which is used by
# the Transformers modeling backend. # the Transformers modeling backend.
text_backbone = ( text_backbone = language_model.model
model.get_language_model().model
if hasattr(model, "get_language_model")
else model.model
)
embed_tokens = ( embed_tokens = (
text_backbone.embed_tokens text_backbone.embed_tokens
if hasattr(text_backbone, "embed_tokens") if hasattr(text_backbone, "embed_tokens")
else text_backbone.get_input_embeddings() else text_backbone.get_input_embeddings()
) )
model.lm_head = model.lm_head.tie_weights(embed_tokens) language_model.lm_head = language_model.lm_head.tie_weights(embed_tokens)
# ModelForPooling is dynamically defined inside the _create_pooling_model_cls # ModelForPooling is dynamically defined inside the _create_pooling_model_cls
# function, so we need use this hacky method to obtain it. # function, so we need use this hacky method to obtain it.
...@@ -438,17 +437,22 @@ def load_weights_using_from_2_way_softmax( ...@@ -438,17 +437,22 @@ def load_weights_using_from_2_way_softmax(
false_id = tokenizer.convert_tokens_to_ids(tokens[0]) false_id = tokenizer.convert_tokens_to_ids(tokens[0])
true_id = tokenizer.convert_tokens_to_ids(tokens[1]) true_id = tokenizer.convert_tokens_to_ids(tokens[1])
score_weight = model.lm_head.weight.data[[true_id]].to( lm_head_weight = language_model.lm_head.weight
score_weight = lm_head_weight.data[[true_id]].to(
torch.float32 torch.float32
) - model.lm_head.weight.data[[false_id]].to(torch.float32) ) - lm_head_weight.data[[false_id]].to(torch.float32)
param = model.score.weight param = model.score.weight
weight_loader = getattr(param, "weight_loader", default_weight_loader) weight_loader = getattr(param, "weight_loader", default_weight_loader)
weight_loader(param, score_weight) weight_loader(param, score_weight)
del model.lm_head del language_model.lm_head
loaded_weights.add("score.weight") loaded_weights.add("score.weight")
loaded_weights.discard("lm_head.weight")
lm_head_name = "lm_head.weight"
if hf_to_vllm_mapper := getattr(model, "hf_to_vllm_mapper", None):
lm_head_name = hf_to_vllm_mapper._map_name(lm_head_name)
loaded_weights.discard(lm_head_name)
return loaded_weights return loaded_weights
......
...@@ -254,7 +254,9 @@ class Qwen3ForSequenceClassificationConfig(VerifyAndUpdateConfig): ...@@ -254,7 +254,9 @@ class Qwen3ForSequenceClassificationConfig(VerifyAndUpdateConfig):
"Try loading the original Qwen3 Reranker?, see: " "Try loading the original Qwen3 Reranker?, see: "
"https://github.com/vllm-project/vllm/tree/main/examples/pooling/score/qwen3_reranker_offline.py" "https://github.com/vllm-project/vllm/tree/main/examples/pooling/score/qwen3_reranker_offline.py"
) )
model_config.hf_config.method = "from_2_way_softmax" text_config = config.get_text_config()
text_config.method = "from_2_way_softmax"
text_config.classifier_from_token = tokens
class Qwen3VLForSequenceClassificationConfig(Qwen3ForSequenceClassificationConfig): class Qwen3VLForSequenceClassificationConfig(Qwen3ForSequenceClassificationConfig):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment