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

Fix require grad warning for non-leaf tensor in noise tunnel #426

Closed
wants to merge 4 commits into from

Conversation

NarineK
Copy link
Contributor

@NarineK NarineK commented Jul 9, 2020

This will fix the warning error specifically related to NoiseTunnel in #421.
In addition to that I moved almost everything under no_grad in the attribute method. This will hopefully also help with runtime performance.
In the _forward_layer_eval I had to add grad_enabled flag in order to allow to enable the gradients externally. As it is also needed in test_neuron_gradient.py test case.

)
grad_enabled = True if gradient_neuron_index is not None or grad_enabled else False

with torch.autograd.set_grad_enabled(grad_enabled):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another option is to leave here set_grad_enabled(True)

Copy link
Contributor

Choose a reason for hiding this comment

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

This looks good! We don't need to enable grad if unnecessary.

@NarineK NarineK requested a review from vivekmig July 10, 2020 18:28
Copy link
Contributor

@vivekmig vivekmig left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for fixing this :) !

One question related to #421 , for NoiseTunnel with Saliency (or other methods that compute gradients directly wrt the provided input), the user will now always get this warning, since the input to the method will not require grad?

UserWarning: Input Tensor 0 did not already require gradients, required_grads has been set automatically.
  warnings.warn(

To avoid this, we can internally require grad on inputs_with_noise for a gradient based method. What do you think?

)
grad_enabled = True if gradient_neuron_index is not None or grad_enabled else False

with torch.autograd.set_grad_enabled(grad_enabled):
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks good! We don't need to enable grad if unnecessary.

@NarineK
Copy link
Contributor Author

NarineK commented Jul 13, 2020

Looks good, thanks for fixing this :) !

One question related to #421 , for NoiseTunnel with Saliency (or other methods that compute gradients directly wrt the provided input), the user will now always get this warning, since the input to the method will not require grad?

UserWarning: Input Tensor 0 did not already require gradients, required_grads has been set automatically.
  warnings.warn(

To avoid this, we can internally require grad on inputs_with_noise for a gradient based method. What do you think?

Yeah, that's a good point! Added require_grads for GradientAttribution methods.

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.

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

@facebook-github-bot
Copy link
Contributor

@NarineK merged this pull request in ee41c68.

NarineK added a commit to NarineK/captum-1 that referenced this pull request Nov 19, 2020
…#426)

Summary:
This will fix the warning error specifically related to NoiseTunnel in pytorch#421.
In addition to that I moved almost everything under no_grad in the attribute method. This will hopefully also help with runtime performance.
In the `_forward_layer_eval ` I had to add `grad_enabled ` flag in order to allow to enable the gradients externally. As it is also needed in `test_neuron_gradient.py` test case.

Pull Request resolved: pytorch#426

Reviewed By: vivekmig

Differential Revision: D22500566

Pulled By: NarineK

fbshipit-source-id: d3170e1711012593ff421b964a02e54532a95b13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants