Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions aten/src/ATen/native/TensorCompare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ Tensor where(const Tensor& condition, const Tensor& self, const Tensor& other) {
return at::_s_where(b_condition, b_self, b_other);
}

std::vector<Tensor> where(const Tensor& condition) {
return condition.nonzero_numpy();
}

Tensor _s_where_cpu(const Tensor& condition, const Tensor& self, const Tensor& other) {
Tensor ret = at::empty(self.sizes(), self.options());
AT_DISPATCH_ALL_TYPES(ret.scalar_type(), "where_cpu", [&] {
Expand Down
3 changes: 3 additions & 0 deletions aten/src/ATen/native/native_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2231,6 +2231,9 @@
- func: where(Tensor condition, Tensor self, Tensor other) -> Tensor
variants: function, method

- func: where(Tensor condition) -> Tensor[]
variants: function

- func: _s_where(Tensor condition, Tensor self, Tensor other) -> Tensor
variants: function
dispatch:
Expand Down
3 changes: 2 additions & 1 deletion test/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9322,8 +9322,9 @@ def gen_nontrivial_input(num_src, dtype, device):
if TEST_NUMPY:
tup1 = torch.nonzero(tensor, as_tuple=True)
tup2 = tensor.nonzero(as_tuple=True)
tup3 = torch.where(tensor)
np1 = tensor.cpu().numpy().nonzero()
for t in (tup1, tup2):
for t in (tup1, tup2, tup3):
self.assertEqual(len(t), len(np1))
for i in range(len(t)):
self.assertEqual(t[i].cpu().numpy(), np1[i])
Expand Down
10 changes: 9 additions & 1 deletion torch/_torch_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5848,7 +5848,7 @@ def merge_dicts(*dicts):

add_docstr(torch.where,
r"""
where(condition, x, y) -> Tensor
.. function:: where(condition, x, y) -> Tensor
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't this either a different function or the optional x, y need to be explained in some way?

Copy link
Contributor

Choose a reason for hiding this comment

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

whoops, didn't see the line below, my mistake.


Return a tensor of elements selected from either :attr:`x` or :attr:`y`, depending on :attr:`condition`.

Expand Down Expand Up @@ -5883,6 +5883,14 @@ def merge_dicts(*dicts):
tensor([[ 1.0000, 0.3139],
[ 0.3898, 1.0000],
[ 0.0478, 1.0000]])

.. function:: where(condition) -> tuple of LongTensor

``torch.where(condition)`` is identical to
``torch.nonzero(condition, as_tuple=True)``.

.. note::
See also :func:`torch.nonzero`.
""")

add_docstr(torch.logdet,
Expand Down