Skip to content

[RLlib] Fix PyTorch grad cliping usage - #25584

Merged
sven1977 merged 4 commits into
ray-project:masterfrom
ArturNiederfahrenhorst:fixclippingmetric
Jun 8, 2022
Merged

sven1977 merged 4 commits into
ray-project:masterfrom
ArturNiederfahrenhorst:fixclippingmetric

Conversation

@ArturNiederfahrenhorst

Copy link
Copy Markdown
Contributor

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

@ArturNiederfahrenhorst

Copy link
Copy Markdown
Contributor Author

@ArturNiederfahrenhorst

Copy link
Copy Markdown
Contributor Author

@ArturNiederfahrenhorst

Copy link
Copy Markdown
Contributor Author

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.
QMix before: https://tensorboard.dev/experiment/W34kbWQLTDOrhkAvgM863A/#scalars&tagFilter=grad&_smoothingWeight=0
QMix after: https://tensorboard.dev/experiment/yHo7dcrPQgm0r3R6WdF7Sg/#scalars&tagFilter=grad&_smoothingWeight=0

self.optim_alpha = 0.99
self.optim_eps = 0.00001
self.grad_norm_clipping = 10
self.grad_clip = 10

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.

Nice to have this unified!

# Pass kwargs onto super's `training()` method.
super().training(**kwargs)

if grad_norm_clipping is not None:

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.

Cool.

from torch.optim import RMSprop

self.optimiser = RMSprop(
self.rmsprop_optimizer = RMSprop(

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.

:) "optimiser"

@ArturNiederfahrenhorst ArturNiederfahrenhorst Jun 8, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

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.

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 sven1977 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.

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

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.

Sorry, stupid question: w/o checking. Does QMix Policy actually use apply_grad_clipping?

@ArturNiederfahrenhorst ArturNiederfahrenhorst Jun 8, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@sven1977
sven1977 merged commit 9226643 into ray-project:master Jun 8, 2022

@gjoliver gjoliver left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am curious, can we do this clipping on whatever device it is, like torch.min(), without unloading it from GPU?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am clueless, will there be negative global_norm?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

global norm is an l2 norm so it can not become negative!

sumanthratna pushed a commit to sumanthratna/ray that referenced this pull request Jun 8, 2022
self.params, self.config["grad_norm_clipping"]
)
self.optimiser.step()
self.rmsprop_optimizer.step()

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.

@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?

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.

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.

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.

[RLlib] PyTorch grad_clip happens in place - Extra Grad Info Reports Unclipped Total Norm

4 participants