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

Leaf Warning Fix #597

Closed
wants to merge 5 commits into from
Closed

Conversation

vivekmig
Copy link
Contributor

This removes the resetting of grad attribute to zero, which is causing warnings as mentioned in #491 and #421 . Based on torch documentation, resetting of grad is only needed when using torch.autograd.backward, which accumulates results into the grad attribute for leaf nodes. Since we only utilize torch.autograd.grad (with only_inputs always set to True), the gradients obtained in Captum are never actually accumulated into grad attributes, so resetting the attribute is not actually necessary.

This also adds a test to confirm that the grad attribute is not altered when gradients are utilized through Saliency.

inp.grad = torch.randn_like(inp)
grad = inp.grad.detach().clone()
self._saliency_base_assert(model, inp, grads, add_args)
assertTensorTuplesAlmostEqual(self, inp.grad, grad)
Copy link
Contributor

@NarineK NarineK Jan 25, 2021

Choose a reason for hiding this comment

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

nit: Can we, please, do exact equal here to be sure that the grads didn't change ?
It would be interesting to use backward and compare with the grads computed with the backward to contrast the difference.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, updated with delta=0.0 for exact equality. From the docs, I think backward should always compute the same gradients just also updating the grad attribute for leaf nodes, but can double-check this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you! I was thinking that if we use backward instead autograd.grad then we will see the difference, right ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, yup, if we used backward instead, this test should fail! Although backward doesn't take inputs as parameters and just requires accessing grad for the gradients, so would be a different structure from what we currently use and might fail for non-leaf inputs.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@vivekmig has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@vivekmig has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@vivekmig merged this pull request in 35f7af5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants