Description
🐛 Describe the bug
Hi PyTorch dev team, I am using a list of lists for indexing a single-dimensional Tensor. When the lengths of the list drops below 32, an IndexError is raised. The same behavior has been reported at https://discuss.pytorch.org/t/solved-bug-inconsistent-behavior-for-tensor-list-indexing/137225 in Nov '21, but it appears that the inconsistent behaviour still remains.
import torch
# Create a single dimensional time-series tensor with N elements
N = 1000
t = torch.randn(N)
# Set window size
W = 10
# Generate a list of lists, containing overlapping window indices
indices = [range(i, i + W) for i in range(0, N - W)]
# Extract windows from time-series tensor
# While the number of windows is >= 32, no erros occur,
# But when the number is < 32, an IndexError is raised.
for i in [len(indices), 100, 32, 31]:
print('Num of windows:', i)
windowed_data = t[indices[:i]]
print('Windowed data shape:', windowed_data.shape)
The code prints out:
Num of windows: 990
Windowed data shape: torch.Size([990, 10])
Num of windows: 100
Windowed data shape: torch.Size([100, 10])
Num of windows: 32
Windowed data shape: torch.Size([32, 10])
Num of windows: 31
And then the following IndexError is raised:
Traceback (most recent call last):
File "/Users/pacfan/Desktop/Torch_Tensor_List_Indexing_Bug.py", line 18, in <module>
windowed_data = t[indices[:i]]
IndexError: too many indices for tensor of dimension 1
The problem can be bypassed by changing indices
from a list of lists to a Tensor with indices = torch.tensor(indices)
.
Versions
PyTorch version: 2.3.0.dev20240208
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A
OS: macOS 13.1 (arm64)
GCC version: Could not collect
Clang version: 14.0.3 (clang-1403.0.22.14.1)
CMake version: Could not collect
Libc version: N/A
Python version: 3.9.18 (main, Sep 11 2023, 08:25:10) [Clang 14.0.6 ] (64-bit runtime)
Python platform: macOS-13.1-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
CPU:
Apple M1 Pro
Versions of relevant libraries:
[pip3] numpy==1.26.0
[pip3] torch==2.3.0.dev20240208
[pip3] torchaudio==2.2.0.dev20240208
[pip3] torchvision==0.15.2a0
[conda] numpy 1.26.0 py39h3b2db8e_0 anaconda
[conda] numpy-base 1.26.0 py39ha9811e2_0 anaconda
[conda] pytorch 2.3.0.dev20240208 py3.9_0 pytorch-nightly
[conda] torchaudio 2.2.0.dev20240208 py39_cpu pytorch-nightly
[conda] torchvision 0.15.2 cpu_py39h31aa045_0 anaconda