Skip to content

grad accumulation steps#1780

Merged
hnyu merged 1 commit into
pytorchfrom
PR_grad_acc_steps
Jun 14, 2025
Merged

grad accumulation steps#1780
hnyu merged 1 commit into
pytorchfrom
PR_grad_acc_steps

Conversation

@hnyu

@hnyu hnyu commented Jun 13, 2025

Copy link
Copy Markdown
Collaborator

This PR adds a flag for accumulating gradients for optimizer step. It can be used to simulate training with a large batch size with limited memory. The idea is to keep accumulating param grads before triggering a real optimizer step, by scaling the grad with 1/acc_steps.

@hnyu
hnyu requested a review from emailweixu June 13, 2025 17:25
@hnyu
hnyu force-pushed the PR_grad_acc_steps branch from 9b0f522 to 56d12e0 Compare June 13, 2025 17:31

@emailweixu emailweixu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it compatible with GradScaler? If not, need to raise error if both are used.

@hnyu

hnyu commented Jun 13, 2025

Copy link
Copy Markdown
Collaborator Author

Is it compatible with GradScaler? If not, need to raise error if both are used.

According to pytorch doc:

 # scaler.step() first unscales the gradients of the optimizer's assigned params.
 # If these gradients do not contain infs or NaNs, optimizer.step() is then called,
 # otherwise, optimizer.step() is skipped.
 scaler.step(optimizer)

So the optimizer already sees grad after unscaling.

@hnyu
hnyu merged commit e7eb81d into pytorch Jun 14, 2025
2 checks passed
@hnyu
hnyu deleted the PR_grad_acc_steps branch June 14, 2025 04:07
@hnyu hnyu mentioned this pull request Jun 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants