KEMBAR78
Reset grad attribute when called using del by bhushan23 · Pull Request #16525 · pytorch/pytorch · GitHub
Skip to content

Conversation

@bhushan23
Copy link
Contributor

del Tensor.grad set PyObject to nullptr
and Tensor.grad = None set PyObject to Py_None
Handling both the cases now
fixes ##16471

@bhushan23
Copy link
Contributor Author

@colesbury as we cannot control what PyObject gets, we need to add above check.
Is it correct? If so, Will add respective test cases

@colesbury
Copy link
Member

Yes. This same problem occurs in some other setters in THPVariable_properties and maybe other classes. Including:

  • THPVariable_set_grad_fn
  • THPVariable_set_data
  • THPVariable_set_requires_grad

(i.e. they crash when the someone tries to delete the property)

@vishwakftw
Copy link
Contributor

@colesbury Is grad_fn deletable? grad_fn is not writable, I believe.

{"grad_fn", (getter)THPVariable_get_grad_fn, nullptr, nullptr, nullptr},

Copy link
Contributor

Choose a reason for hiding this comment

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

Spaces before and after = would probably cause lint error, and so will no space after , in (5,5).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @vishwakftw for pointing out

@vishwakftw
Copy link
Contributor

Lint is failing:

./test/test_autograd.py:1123:32: W291 trailing whitespace

@bhushan23
Copy link
Contributor Author

bhushan23 commented Feb 1, 2019

@colesbury / @soumith please approve

Copy link
Collaborator

Choose a reason for hiding this comment

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

What does it mean for a Tensor .data field to be None ? What happens to x when you do del x.data ? Does it become unusable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

then it's an undefined tensor and can not longer be accessed.
Will lead to Runtime error if attempted to access.

del x.data is equivalent to del x but not fine approach.
Not allowing deletion or setting data to None would be much better approach.
@colesbury @albanD what do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

From my point of view, not allowing it would be better as this is a state you never want to be in (in python).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agreed. Doing the change.

@bhushan23
Copy link
Contributor Author

@colesbury

Copy link
Member

@fmassa fmassa left a comment

Choose a reason for hiding this comment

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

I think this looks good, but I'll let @colesbury have another look at it

@soumith
Copy link
Member

soumith commented Feb 9, 2019

just fyi to all, @colesbury is on vacation. If @albanD also approves, I'm happy to ship it.

Copy link
Collaborator

@albanD albanD left a comment

Choose a reason for hiding this comment

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

Could you do the same for THPVariable_set_grad_fn and forbid deleting it please (A user should never delete a grad_fn but detach() the Tensor) and THPVariable_set_backwards_hooks to make it have the same behavior as if None was passed (this shuold not be used by users, but I think it's better to remove the case where it segfaults)? Both these functions have the same problem as the one you fixed.

@bhushan23
Copy link
Contributor Author

@albanD sounds good to me.
Unfortunately, I do not have my laptop with me and can not setup pytorch on temporary laptop. Will send change in next couple of days.

@bhushan23
Copy link
Contributor Author

@albanD not allowing deletion for THPVariable_set_grad_fn
THPVariable_set_backwards_hooks's behavior for deletion and setting to None is same, hence skipping.

@albanD
Copy link
Collaborator

albanD commented Feb 18, 2019

But won't the check obj == Py_None segfault if obj is already a nullptr for THPVariable_set_backwards_hooks?

@bhushan23
Copy link
Contributor Author

bhushan23 commented Feb 18, 2019

But won't the check obj == Py_None segfault if obj is already a nullptr for THPVariable_set_backwards_hooks?

@albanD No. It won't as obj pointer is not same as one pointing to Py_None
But, if we don't want to allow deletion of backward hooks, we should definitely assert with useful error message.

@albanD
Copy link
Collaborator

albanD commented Feb 18, 2019

Ho my bad, for some reason I though it was the equality check that was causing issues. We are happy with deletion to behave like setting it to None !

@bhushan23
Copy link
Contributor Author

Ho my bad, for some reason I though it was the equality check that was causing issues. We are happy with deletion to behave like setting it to None !

FYI, I just pushed changes to assert if del is called on _backward_hooks
Fact that, deletion and setting to None behavior is same is good to have but do we want to allow it?
Not allowing will be good as we keep consistent behavior with other grad attribute

@albanD
Copy link
Collaborator

albanD commented Feb 18, 2019

This particular method is an internal one. It is just used internally at the moment.
Also you don't want the user to tinker with grad_fn (there are proper functions for that) but you're happy with him removing the hooks.

del Tensor.grad sets PyObject to nullptr and
Tensor.grad = None sets PyObject to Py_None
Handling both the cases now

Handling del Tensor.requires_grad to error out
requires_grad must be bool

Not allowing del or setting Tensor.data to None
deleting tensor data makes tensor undefined

Not allowing del Tensor._grad_fn

Not allowing del Tensor._backward_hooks

---Test cases added---
1. Deletion of grad leads to grad being None
2. Deletion of data leads to Runtime error and setting
   to None leads to Type error
3. Deletion of requires_grad leads to Runtime error
4. Deletion of _grad_fn leads to Runtime error
5. Deletion of _backward_hooks leads to Runetime error
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.

@soumith is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

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.

8 participants