Lightning-AI / Lightning-AI/pytorch-lightning

Add stepwise scheduling for GradientAccumulationScheduler

Open
#21,534 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

callback: gradient accumulation feature
Dominant language
Python
Stars
31.4k
Forks
3.8k
Avg merge
6d 7h
Merged PRs (30d)
6

Description

Description & Motivation

Currently, the GradientAccumulationScheduler only supports scheduling on epoch intervals. However, during pretraining tasks, the model might only run for a single epoch. Therefore, it would be beneficial to be able to schedule the gradient accumulation according to trainer.global_step taken.

Proposal:

  • add a interval parameter to GradientAccumulationScheduler, which can be "epoch" or "step", defaulting to "epoch" for backwards compatibility
  • add a condition to the current on_train_epoch_start to only trigger if interval == "epoch"
  • add an on_train_batch_start/on_after_optimizer_step hook, triggering if interval == "step"

However, given the current warning of scheduling being incompatible with DeepSpeed, I am not sure if scheduling on steps would be unsupported by all/some strategies.

Pitch

I want to be able to scheduling gradient accumulation by trainer.global_step instead of trainer.current_epoch.

Alternatives
Additional context

Could depend on having an on_optimizer_step hook for callbacks. See https://github.com/Lightning-AI/pytorch-lightning/issues/11688#issuecomment-1812863621

cc @lantiga

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the GradientAccumulationScheduler implementation and its existing on_train_epoch_start behavior, then trace the callback hook options named in the issue and trainer.global_step. Review the linked discussion about an on_optimizer_step hook and the DeepSpeed compatibility warning; done means step-based scheduling is supported while epoch scheduling remains the default and backwards compatible.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.