[bugfix] sync template.padding_free with args after prepare_model for…#9031
Merged
Jintao-Huang merged 1 commit intomodelscope:mainfrom Apr 8, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a synchronization step in the MegatronTrainer base class to ensure that template.padding_free is consistent with args.padding_free after the model is prepared. This change accounts for potential overrides of the padding configuration during model-specific initialization, such as for DSA attention models. I have no feedback to provide.
Collaborator
|
thanks! |
Jintao-Huang
approved these changes
Apr 8, 2026
Collaborator
|
please run: |
Jintao-Huang
pushed a commit
that referenced
this pull request
Apr 13, 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.
PR type
PR information
Problem
When running Megatron DPO/KTO training with DSA-based models (e.g. DeepSeek-V3.2),
training produces NaN loss on every step, regardless of
--packingsettings.Root Cause
In the Megatron training path,
MegatronArgumentsdefinespadding_free: bool = Trueas the default.The
templateobject is created before model initialization, sotemplate.padding_free = True.During
BaseMegatronTrainer.prepare_model(),_check_padding_free()inswift/megatron/model/utils.pydetects DSA attention and forcesargs.padding_free = False.However,
template.padding_freeis never updated — it remainsTrue.This mismatch causes:
template.padding_free=True): packs chosen+rejected into a single row→
labels.shape = [1, N]_prepare_batch(args.padding_free=False): does not createpacked_seq_params→
packed_seq_params = Noneloss_func: computesnum_samples = labels.shape[0] // 2 = 1 // 2 = 0→ empty
chosen_logps→ loss = NaNThis bug affects all DSA models by default (since Megatron defaults
padding_free=True).Non-DSA models (e.g. Qwen) are not affected because
_check_padding_freedoes not overrideargs.padding_freefor them.Fix
Sync
template.padding_freetoargs.padding_freeright afterself.prepare_model()in
BaseMegatronTrainer.__init__(), before the data collator is created.Scope
padding_free(True)--padding_free true--packing true--padding_free falseVerification
Tested with DeepSeek-V3.2 Megatron DPO training:
chosen_logps.shape=[0]via logging)