[doc] correct docs for torch.nonzero#43434
Conversation
As discussed at pytorch#43425 - `as_tuple` is a required argument since pytorch-1.5. If not passed a warning will be issued. - adjust the docs - add the missing 3nd arg in Args
torch/_torch_docs.py
Outdated
| gives all nonzero values of tensor ``x``. Of the returned tuple, each index tensor | ||
| contains nonzero indices for a certain dimension. | ||
|
|
||
| `as_tuple` is a required argument since pytorch-1.5. If not passed a warning |
There was a problem hiding this comment.
Is the default in the signature actually False? Isn't it None by any chance? Otherwise, how do we detect that the user did not provide it? :D
There was a problem hiding this comment.
It is False, since it wasn't required until now.
That's why it's confusing, that it has a default, yet a user must pass the argument explicitly - perhaps it's a bug in the code?
There was a problem hiding this comment.
Hmm, weird it has no default in the signature
torch/__init__.pyi:def nonzero(self: Tensor, *, out: Optional[Tensor]=None) -> Tensor: ..
There was a problem hiding this comment.
Yep that's what I expected :D
If the default is actually False, there is no way the function can know if the user provided it or not.
I think you should change the signature in the doc to reflect that.
There was a problem hiding this comment.
print(torch.nonzero(torch.tensor([1, 1, 1, 0, 1])))
print(torch.nonzero(torch.tensor([1, 1, 1, 0, 1]), as_tuple=False))
print(torch.nonzero(torch.tensor([1, 1, 1, 0, 1]), as_tuple=True))
the 1st and 2nd ones produce the same output (except the first one prints the warning)
There was a problem hiding this comment.
Hmm, but as_tuple's default is not None.
I found where the ambiguity is flagged:
https://github.com/pytorch/pytorch/blob/master/tools/autograd/templates/python_torch_functions.cpp#L577
and when it's called on an object:
https://github.com/pytorch/pytorch/blob/master/tools/autograd/templates/python_variable_methods.cpp#L450
There was a problem hiding this comment.
I don't seem to find the actual C++ function - too many unrelated nonzero strings. Do you by chance know where it is?
Without seeing the source code from everything I have seen so far, and if there is no better way to resolve it - the doc should say:
as_tupleis a required argument and that it has no default.
But should it still show the old signature? Separately? And if not, should out be removed from the doc?
There was a problem hiding this comment.
see the further updates I made in subsequent commits.
There was a problem hiding this comment.
I think I'll let @ssnl give his opinion here as he already did a doc update on this param in the past.
There was a problem hiding this comment.
Thank you for your feedback so far, @albanD
💊 CI failures summary and remediationsAs of commit 9159a04 (more details on the Dr. CI page):
🕵️ 2 new failures recognized by patternsThe following CI failures do not appear to be due to upstream breakages:
|
|
The CI failures appear unrelated to this PR |
|
I think this is a bug of signature parser. To avoid ambiguity, maybe we should only deprecate nonzero(Tensor input, *, Tensor out), not the one with out=None.
|
|
what needs to happen so that a decision is made to move towards a resolution? Thank you! |
|
Related issue: #44284 |
|
Looks like another PR fixed the warning instead: #45413 |
As discussed at #43425
as_tupleis a required argument since pytorch-1.5. If not passed, a warning will be issued.outout,as_tuplein)Fixes #32994, #43425