Add gradient accumulation for training (single-GPU/DDP/FSDP2)#1
Closed
eldarkurtic wants to merge 1 commit into
Closed
Add gradient accumulation for training (single-GPU/DDP/FSDP2)#1eldarkurtic wants to merge 1 commit into
eldarkurtic wants to merge 1 commit into
Conversation
Introduce a `--gradient-accumulation-steps` / `TrainerConfig.gradient_accumulation_steps` knob (default 1) that accumulates gradients over N microbatches before each optimizer step, growing the effective batch size without extra per-microbatch memory. - Loss is scaled by 1/N; the optimizer, gradient clip, and LR scheduler run once per accumulation window (macro-step counting for global_step, log_freq, checkpoint cadence, and scheduler total steps). - Trailing microbatches that don't fill a full window are dropped each epoch; `_optimizer_steps_per_epoch` is the single source of truth for this policy. - `_maybe_no_sync` skips the DDP all-reduce on non-boundary microbatches; the single-GPU and FSDP2 paths fall back to a no-op context and stay correct because gradients accumulate additively into `.grad`. - Trainer raises early when N exceeds the batches-per-epoch (would never step). - accum=1 is behavior-identical to the previous one-step-per-batch loop. Verified numerically equivalent to the mean of per-microbatch gradients on single-GPU, 8-GPU DDP, and 4-GPU FSDP2 (fully_shard), plus unit tests for the scheduler math, no_sync behavior, cadence, and CLI wiring. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: Eldar Kurtic <8884008+eldarkurtic@users.noreply.github.com>
Owner
Author
|
Superseded by the upstream PR vllm-project#859. |
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
Adds a
--gradient-accumulation-steps/TrainerConfig.gradient_accumulation_stepsknob (default1) that accumulates gradients over N microbatches before each optimizer step, growing the effective batch size (per-step batch × N) without extra per-microbatch memory. Matches the convention used by peer speculative-decoding frameworks (DeepSpec, TorchSpec, SpecForge): loss scaled by1/N, optimizer + LR scheduler stepped once per accumulation window, counting in optimizer (macro) steps.Behavior
global_step,--log-freq,--checkpoint-freq(when< 1), and the LR scheduler total all advance once per accumulation window (an accum=N run does1/Nthe scheduler steps per epoch)._optimizer_steps_per_epochis the single source of truth for this policy (used by the scheduler math, the__init__guard, and the loop boundary)._maybe_no_sync: on DDP, skips the gradient all-reduce on non-boundary microbatches and syncs the accumulated gradient on the boundary. Single-GPU (raw module) and FSDP2 (fully_shard, DTensor params) fall back to a no-op context and stay correct because gradients accumulate additively into.grad.Trainerraises early if N exceeds the batches-per-epoch (which would otherwise run zero optimizer steps).accum=1is behavior-identical to the previous one-step-per-batch loop (regression-safe).Verification
Numerically confirmed equivalent to the mean of per-microbatch gradients (accumulated grad vs. reference) on real hardware:
pythontorchrun×8fully_shard)torchrun×4All three exercise the real
Trainer.train_epoch → _accumulate_and_step → _maybe_no_syncpath. DDP/FSDP2 use different data per rank, so cross-rank averaging composes with accumulation.Unit tests added: scheduler-step math (accum, remainder, accum=1 regression), numerical grad-equivalence on a toy model,
_maybe_no_syncbehavior (single-process DDP), the accum-too-large guard, and CLI parsing/rejection.Test plan
python -m pytest tests/unit/train/test_gradient_accumulation.py tests/unit/train/test_trainer_scheduler.py tests/unit/train/test_cli_args.py— passing.make quality(ruff + format + mypy) clean on changed files.🤖 Generated with Claude Code