Repository navigation
[RLlib] Fix PyTorch grad cliping usage - #25584
Conversation
|
Since it was not using this utility function, QMix went under the radar here. This PR now also deals with unification of our grad clipping algorithm parameters (grad_clip vs grad_clip_norm) in case of QMix. Offline Estimators do not obey this atm. |
| self.optim_alpha = 0.99 | ||
| self.optim_eps = 0.00001 | ||
| self.grad_norm_clipping = 10 | ||
| self.grad_clip = 10 |
There was a problem hiding this comment.
Nice to have this unified!
| # Pass kwargs onto super's `training()` method. | ||
| super().training(**kwargs) | ||
|
|
||
| if grad_norm_clipping is not None: |
| from torch.optim import RMSprop | ||
|
|
||
| self.optimiser = RMSprop( | ||
| self.rmsprop_optimizer = RMSprop( |
There was a problem hiding this comment.
I got stuck on this actually, because the policy object will have an optimizer attribute, which is different from the optimiser attribute. That's why I chose to rename this to gain some distance and prevent this from happening again.
There was a problem hiding this comment.
Yeah, it's terrible. There is also an optimizer() method for TFPolicies :D
We will fix all that for the new Policy API for 2.0 and going forward.
But this is correct here: self.optimizer should be a property, if anything.
sven1977
left a comment
There was a problem hiding this comment.
Looks good. Just the question on making sure QMix already uses the grad clipping utility. Seems weird that it wouldn't throw an error due to the missing grad_clip config key.
| if isinstance(grad_gnorm, torch.Tensor): | ||
| grad_gnorm = grad_gnorm.cpu().numpy() | ||
| info["grad_gnorm"] = grad_gnorm | ||
| # PyTorch clips gradients inplace and returns the norm before clipping |
There was a problem hiding this comment.
Sorry, stupid question: w/o checking. Does QMix Policy actually use apply_grad_clipping?
There was a problem hiding this comment.
It did not use apply_grad_clipping before this PR.
But it had the same bug, resulting in bad metrics.
Additionally, I wanted it to use apply_grad_clipping to avoid duplication of this functionality and make this easier to maintain.
gjoliver
left a comment
There was a problem hiding this comment.
thanks for fixing this!! sorry I dropped the ball, I was looking at this a bit yesterday.
| global_norm = nn.utils.clip_grad_norm_(params, clip_value) | ||
|
|
||
| if isinstance(global_norm, torch.Tensor): | ||
| global_norm = global_norm.cpu().numpy() |
There was a problem hiding this comment.
I am curious, can we do this clipping on whatever device it is, like torch.min(), without unloading it from GPU?
There was a problem hiding this comment.
Do you mean that as in "does this really work what we are doing here?".
Because we are clipping on whatever device params are and we unload afterward. And the metrics look very much like it is working.
| if isinstance(global_norm, torch.Tensor): | ||
| global_norm = global_norm.cpu().numpy() | ||
|
|
||
| info["grad_gnorm"] = min(global_norm, clip_value) |
There was a problem hiding this comment.
I am clueless, will there be negative global_norm?
There was a problem hiding this comment.
global norm is an l2 norm so it can not become negative!
| self.params, self.config["grad_norm_clipping"] | ||
| ) | ||
| self.optimiser.step() | ||
| self.rmsprop_optimizer.step() |
There was a problem hiding this comment.
@ArturNiederfahrenhorst where is the actual gradient clipping happening? It's not just enough to do it in stats report, right? or Am I missing something?
There was a problem hiding this comment.
This is what I think is happening: Since you are already calling self.optimizer.step() you have used the unclipped gradients to update the parameters already before calling apply_grad_clipping.
Why are these changes needed?
PyTorch's grad clipping clips in place and returns the global norm before clipping (instead of after clipping).
This throws our gnorm metric off.
Related issue number
Closes #4965
Checks
scripts/format.shto lint the changes in this PR.