Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible issue with gradient accumulation #57

Closed
rosinality opened this issue Jul 3, 2023 · 3 comments
Closed

Possible issue with gradient accumulation #57

rosinality opened this issue Jul 3, 2023 · 3 comments

Comments

@rosinality
Copy link

rosinality commented Jul 3, 2023

Hello, thank you for a great work!

During studying the implementation, I suspected this line https://github.com/tatsu-lab/alpaca_farm/blob/main/src/alpaca_farm/rl/rl_trainer.py#L150 for zero the gradients during gradient accumulation could cause zero out all gradients except the gradients from the final gradient accumulation steps (accelerator.sync_gradients), as policy.zero_grad is used instead of optimizer.zero_grad.

I think this could cause ignore all gradients from the gradient accumulation steps except step with sync_gradients=True. Could you let me know about this possible problems? Thank you!

@lxuechen
Copy link
Collaborator

lxuechen commented Jul 4, 2023

Oh, thanks for pointing this out. You're absolutely correct. The AcceleratedOptimizer object has the right wraps that disable zero_grad on non-sync steps, and should be used for clearing gradients, as opposed to the wrapped model.

Looking at the history of our internal codebase, we were actually calling self.optimizer.zero_grad(set_to_none=True) until a commit on May 7 2023 changed this to the model. Note we have trained similarly performing PPO models both before and after that commit, so I think it's safe to say that this bug hasn't affected the results in noticeable ways.

Note the quark trainer doesn't have this problem, since I'm not using the accelerator ctx manager there.

@lxuechen
Copy link
Collaborator

lxuechen commented Jul 4, 2023

Resolved in #58

@lxuechen lxuechen closed this as completed Jul 4, 2023
@shunzh
Copy link

shunzh commented Oct 19, 2023

Thanks for fixing this! However, I find that removing self.policy.zero_grad(set_to_none=True) immediately causes a cuda out-of-memory error. I'm running PPO training on 8 A100 GPUs. Have you encountered this issue?

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

No branches or pull requests

3 participants