Repository: pytorch
Issue: Inconsistent numpy indexing
Labels: triaged,module: numpy,module: advanced indexing

### 🐛 Describe the bug

The exact same code using torch or numpy to index tensors is not consistent. Check the code below:

```python
import torch
import numpy as np

powers_of_two = list(2**p for p in range(1, 9))
print(powers_of_two)

print('-------- Using torch')
for M in powers_of_two:
  for N in powers_of_two:
    a = torch.rand(M, N)
    m, n = a.shape
    xx, yy = torch.meshgrid(torch.arange(m), torch.arange(m), indexing='xy')
    result = a[xx]   # WORKS 
    print(M, N, result.shape)

print('-------- Using numpy')
for M in powers_of_two:
  for N in powers_of_two:
    a = torch.rand(M, N)
    m, n = a.shape
    xx, yy = np.meshgrid(np.arange(m), np.arange(m))
    result = a[xx]   # FAILS 
    print(M, N, result.shape)
``` 

in the first half, using tensors the outputs are consistent, but with numpy it does not work and it even crashes. This is the console output:

```
[2, 4, 8, 16, 32, 64, 128, 256]
-------- Using torch
2 2 torch.Size([2, 2, 2])
2 4 torch.Size([2, 2, 4])
2 8 torch.Size([2, 2, 8])
2 16 torch.Size([2, 2, 16])
2 32 torch.Size([2, 2, 32])
2 64 torch.Size([2, 2, 64])
2 128 torch.Size([2, 2, 128])
2 256 torch.Size([2, 2, 256])
4 2 torch.Size([4, 4, 2])
4 4 torch.Size([4, 4, 4])
4 8 torch.Size([4, 4, 8])
4 16 torch.Size([4, 4, 16])
4 32 torch.Size([4, 4, 32])
4 64 torch.Size([4, 4, 64])
4 128 torch.Size([4, 4, 128])
4 256 torch.Size([4, 4, 256])
8 2 torch.Size([8, 8, 2])
8 4 torch.Size([8, 8, 4])
8 8 torch.Size([8, 8, 8])
8 16 torch.Size([8, 8, 16])
8 32 torch.Size([8, 8, 32])
8 64 torch.Size([8, 8, 64])
8 128 torch.Size([8, 8, 128])
8 256 torch.Size([8, 8, 256])
16 2 torch.Size([16, 16, 2])
16 4 torch.Size([16, 16, 4])
16 8 torch.Size([16, 16, 8])
16 16 torch.Size([16, 16, 16])
16 32 torch.Size([16, 16, 32])
16 64 torch.Size([16, 16, 64])
16 128 torch.Size([16, 16, 128])
16 256 torch.Size([16, 16, 256])
32 2 torch.Size([32, 32, 2])
32 4 torch.Size([32, 32, 4])
32 8 torch.Size([32, 32, 8])
32 16 torch.Size([32, 32, 16])
32 32 torch.Size([32, 32, 32])
32 64 torch.Size([32, 32, 64])
32 128 torch.Size([32, 32, 128])
32 256 torch.Size([32, 32, 256])
64 2 torch.Size([64, 64, 2])
64 4 torch.Size([64, 64, 4])
64 8 torch.Size([64, 64, 8])
64 16 torch.Size([64, 64, 16])
64 32 torch.Size([64, 64, 32])
64 64 torch.Size([64, 64, 64])
64 128 torch.Size([64, 64, 128])
64 256 torch.Size([64, 64, 256])
128 2 torch.Size([128, 128, 2])
128 4 torch.Size([128, 128, 4])
128 8 torch.Size([128, 128, 8])
128 16 torch.Size([128, 128, 16])
128 32 torch.Size([128, 128, 32])
128 64 torch.Size([128, 128, 64])
128 128 torch.Size([128, 128, 128])
128 256 torch.Size([128, 128, 256])
256 2 torch.Size([256, 256, 2])
256 4 torch.Size([256, 256, 4])
256 8 torch.Size([256, 256, 8])
256 16 torch.Size([256, 256, 16])
256 32 torch.Size([256, 256, 32])
256 64 torch.Size([256, 256, 64])
256 128 torch.Size([256, 256, 128])
256 256 torch.Size([256, 256, 256])
-------- Using numpy
2 2 torch.Size([2])
2 4 torch.Size([2])
2 8 torch.Size([2])
2 16 torch.Size([2])
2 32 torch.Size([2])
2 64 torch.Size([2])
2 128 torch.Size([2])
2 256 torch.Size([2])
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
[<ipython-input-7-4d96860c9e66>](https://localhost:8080/#) in <module>()
     20     m, n = a.shape
     21     xx, yy = np.meshgrid(np.arange(m), np.arange(m))
---> 22     result = a[xx]   # FAILS 
     23     print(M, N, result.shape)

IndexError: too many indices for tensor of dimension 2
```

### Versions

Tested in google colab:

```
Collecting environment information...
PyTorch version: 1.10.0+cu111
Is debug build: False
CUDA used to build PyTorch: 11.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
CMake version: version 3.12.0
Libc version: glibc-2.26

Python version: 3.7.12 (default, Jan 15 2022, 18:48:18)  [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.4.144+-x86_64-with-Ubuntu-18.04-bionic
Is CUDA available: False
CUDA runtime version: 11.1.105
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.0.5
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.21.5
[pip3] torch==1.10.0+cu111
[pip3] torchaudio==0.10.0+cu111
[pip3] torchsummary==1.5.1
[pip3] torchtext==0.11.0
[pip3] torchvision==0.11.1+cu111
[conda] Could not collect
```

cc @mruberry @rgommers