Fix preserving output_router_logits in multimodal template forward hooks - #10190
Merged
tastelikefeet merged 2 commits intoSep 22, 2026
Merged
Conversation
Collaborator
|
Conflicting |
tastelikefeet
approved these changes
Sep 22, 2026
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
Preserve
output_router_logitswhenTemplate.pre_forward_hookrestores model-forward kwargs after multimodal_post_encode.Fixes #10189.
Background
Native Swift DPO/SFT can enable the MoE router auxiliary loss with:
The Qwen3.5 multimodal template reuses a Qwen2VL post-encoding path that returns a new kwargs dictionary. The generic template hook restores only fields in a manually maintained whitelist. Because
output_router_logitswas missing from that whitelist, the flag was silently removed before the Hugging Face model forward.With
router_aux_loss_coef > 0, Qwen3.5 MoE then returnsaux_loss=None; native DPO fails when it evaluates:Changes
output_router_logitsto the forward-kwargs restoration whitelist inTemplate.pre_forward_hook.output_router_logits=True.Validation
router_aux_loss_coef=1e-3.aux_loss=None, not NCCL initialization.aux_lossand allows the training step to continue.conda run -n llm python -m pytest tests/general/test_template_forward_hook.py -q—1 passed.python -m py_compile swift/template/base.py tests/general/test_template_forward_hook.py.git diff --check.Compatibility and risk
This change only preserves a caller-provided forward kwarg. It does not change the default model behavior when the kwarg is absent, and it does not alter the auxiliary-loss coefficient or loss formula.
The field is already used by the native DPO/SFT trainers for supported MoE models. The regression test targets the parameter-loss mechanism directly and does not require model weights or private data.
Checklist