fix(trainer): normalize partial gradient accumulation windows - #10084
Open
MrCapricornLiu wants to merge 1 commit into
Open
fix(trainer): normalize partial gradient accumulation windows#10084MrCapricornLiu wants to merge 1 commit into
MrCapricornLiu wants to merge 1 commit into
Conversation
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
When the last accumulation window of an epoch has fewer micro-batches than
gradient_accumulation_steps,Trainer.compute_lossandRerankerTrainer.compute_lossstill divide their mean loss by the configuredwindow size. For models accepting loss kwargs, HF's
training_stepdoes notdivide that loss again. With a configured window of four and an actual window
of two, this produces half the full-batch reference gradient.
Use HF Trainer's
current_gradient_accumulation_stepsfor that division, withthe configured value as a fallback for versions/callers without this attribute.
Full windows and the existing normalization branch conditions are unchanged.
The regression runs the actual HF
training_step/Accelerate backward pathwith Swift's Trainer and RerankerTrainer loss implementations, then compares
parameter gradients and accumulated loss with an independent concatenated-batch
reference. It uses a tiny CPU model, no model downloads. The test initializes
HF's training machinery directly and supplies Swift's required loss context;
it does not run the complete Swift initialization or epoch loop.
Experiment results
CUDA_VISIBLE_DEVICES='' PYTHONPATH=. python -m pytest -q \ tests/train/test_trainer_partial_accumulation.pymicro-batches in Trainer and RerankerTrainer.
9 passed with Transformers 4.57.1 / Accelerate 1.11.0.
git diff --checkpass.These are CPU backward tests with equal-size micro-batches, not distributed,
AMP, full fine-tuning or variable-batch weighting tests. The new pytest module
must be invoked explicitly; the repository's existing unittest CI does not
automatically collect it.
Related: #9510 refactors the training loop but retains the configured-size
loss division in these implementations; this fix is independent of that work.