Skip to content

Conversation

@zou3519
Copy link
Contributor

@zou3519 zou3519 commented May 7, 2018

Fixes #7156

Here is some behavior before this PR:

In[1]:
x = torch.arange(9).view(3, 3).contiguous()
x

Out[1]:
tensor([[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]])

In[2]:
x[[0], [-1]]  # Should be equivalent to x[0, -1]

Out[2]:
tensor([ 8])

However, I would expect the output to be tensor([2]).

The bug is that negative indices are added to the computed linear index
directly. In the above example, the linear index computed is "-1", which
wraps around to "8", giving the last element of a flattened view of x.

Instead, we should wrap negative indices around before adding them to
the linear index.

cc @colesbury

Fixes pytorch#7156

Here is some behavior before this PR:
```
In[1]:
x = torch.arange(9).view(3, 3).contiguous()
x[[0], [-1]]  # Should be equivalent to x[0, -1]

Out[1]:
tensor([ 8])
```

The bug is that negative indices are added to the computed linear index
directly. In the above example, the linear index computed is "-1", which
wraps around to "8", giving the last element of a flattened view of `x`.

Instead, we should wrap negative indices around before adding them to
the linear index.
@ezyang ezyang merged commit eaa3f2e into pytorch:master May 13, 2018
onnxbot added a commit to onnxbot/onnx-fb-universe that referenced this pull request May 13, 2018
@colesbury
Copy link
Member

@zou3519, can you fix the error messages on this PR? They're missing spaces around the max_idx, dim, etc.

@colesbury
Copy link
Member

Also please remove the throw std::runtime_error("foo");

@zou3519
Copy link
Contributor Author

zou3519 commented May 29, 2018

@colesbury my bad, will fix them soon, thank you for the catch

zou3519 added a commit to zou3519/pytorch that referenced this pull request May 29, 2018
zou3519 added a commit that referenced this pull request May 29, 2018
weiyangfb pushed a commit to weiyangfb/pytorch that referenced this pull request Jun 11, 2018
* Fix advanced indexing with negative indices

Fixes pytorch#7156

Here is some behavior before this PR:
```
In[1]:
x = torch.arange(9).view(3, 3).contiguous()
x[[0], [-1]]  # Should be equivalent to x[0, -1]

Out[1]:
tensor([ 8])
```

The bug is that negative indices are added to the computed linear index
directly. In the above example, the linear index computed is "-1", which
wraps around to "8", giving the last element of a flattened view of `x`.

Instead, we should wrap negative indices around before adding them to
the linear index.

* Use toCLong()
weiyangfb pushed a commit to weiyangfb/pytorch that referenced this pull request Jun 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants