fix(model): rewrite seq_cls architectures to *ForSequenceClassification - #10063
Open
RerankerGuo wants to merge 1 commit into
Open
fix(model): rewrite seq_cls architectures to *ForSequenceClassification#10063RerankerGuo wants to merge 1 commit into
RerankerGuo wants to merge 1 commit into
Conversation
…on (modelscope#9704) When fine-tuning a VLM or LM with task_type=seq_cls (or reranker), the seq_cls patcher monkey-patches a 'score' head onto the generation model class without swapping the class itself. transformers' PreTrainedModel.save_pretrained writes 'model.__class__.__name__' into config.json['architectures'], so the on-disk checkpoint advertises the generation architecture (e.g. Qwen3VLForConditionalGeneration) while shipping a score head, num_labels, id2label, and problem_type. Downstream vLLM deployment reads architectures to pick the model class, finds the generation class, and rejects the checkpoint — even though inference through PtEngine works correctly. The fix rewrites model.config.architectures in-place to the matching *ForSequenceClassification class at the same place the score head is attached, so every save path (trainer, save_checkpoint, export, peft merge) writes the right value. A new helper _seq_cls_architectures() handles the suffix rewrite idempotently and leaves unknown/custom architectures untouched. Unit tests cover the rewrite, idempotence, multi-arch lists, and empty/None inputs. Refs: modelscope#9704
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #9704.
When fine-tuning a VLM or LM with
task_type=seq_cls(orreranker), the seq_cls patcher in_patch_sequence_classificationmonkey-patches ascorehead onto the generation model class without swapping the class itself.transformers.PreTrainedModel.save_pretrainedthen writesmodel.__class__.__name__intoconfig.json['architectures'], so the on-disk checkpoint advertises e.g.Qwen3VLForConditionalGenerationwhile shipping ascorehead,num_labels,id2label, andproblem_type=multi_label_classification.Downstream vLLM deployment reads
architecturesto pick the model class, finds the generation class, and rejects the checkpoint — even thoughPtEngineinference works correctly. The reporter in #9704 confirmed all four ofscore.weight: [20, 4096],id2label,label2id, andproblem_typeare written correctly; onlyarchitecturesis wrong.Fix
Rewrite
model.config.architecturesin-place to the matching*ForSequenceClassificationclass at the same place thescorehead is attached, so every save path (trainer_save_model,save_checkpoint, export-quant, peft merge-and-unload) writes the right value automatically. The existingvllm_engine.py:347-353arch_mappingoverride is left in place — it remains useful for users who already have bad checkpoints on disk.The new helper
_seq_cls_architectureshandles the suffix rewrite idempotently and leaves unknown / custom architectures untouched (so we never advertise a class that does not exist in transformers).Mapping coverage
*ForConditionalGeneration(Qwen3-VL, Qwen2-VL, InternVL, LLaVA, GLM4V, …)*ForSequenceClassification*ForCausalLM(Qwen2, Llama, Mistral, Mixtral, Yi, …)*ForSequenceClassification*ForSequenceClassification[]/NoneTests
Added
TestSeqClsArchitecturesRewriteintests/general/test_model.pycovering:ForCausalLMandForConditionalGenerationsuffixesNoneinputsLocal sanity checks (10/10 PASS) and syntax checks on both files PASS. Full ms-swift unit / smoke CI is needed to validate end-to-end on real seq_cls training runs; that will run on the upstream CI after this PR is opened.
Out of scope
architecturesrewrite — Megatron has its own seq_cls head code path (swift/megatron/utils/convert_utils.py:283,swift/megatron/model/utils.py:47); happy to follow up with a separate PR if maintainers want it.vllm_engine.py:347-353arch_mappingcleanup — kept for backward-compat with existing bad checkpoints.🤖 Generated with Claude Code