pytorch / pytorch

Tensors and Dynamic neural networks in Python with strong GPU acceleration
https://pytorch.org
Other
83.39k stars 22.49k forks source link

Whether 'targetSize' in inferExpandGeometryImpl needs to be checked when it is less than 0 #76362

Open lfw0103 opened 2 years ago

lfw0103 commented 2 years ago

🐛 Describe the bug

when I run below code

import torch
a = torch.randn(1,3)
a.expand([-2, 2, 1, 3])

I got error message as below:

Traceback (most recent call last): File "", line 1, in File "/home/pytorch/torch/_tensor.py", line 341, in repr return torch._tensor_str._str(self) File "/home/pytorch/torch/_tensor_str.py", line 439, in _str return _str_intern(self) File "/home/pytorch/torch/_tensor_str.py", line 414, in _str_intern tensor_str = _tensor_str(self, indent) File "/home/pytorch/torch/_tensor_str.py", line 264, in _tensor_str formatter = _Formatter(get_summarized_data(self) if summarize else self) File "/home/pytorch/torch/_tensor_str.py", line 92, in init tensor_view = tensor.reshape(-1) RuntimeError: Trying to create tensor with negative dimension -2: [-2, 2, 1, 3]

It seems to report 'reshape' has invalid parameter, actually expand op does. It causes confuse sometime. Maybe torch should check 'targerSize' in inferExpandGeometryImpl to make sure it is greater than or equal to 0.

Versions

'1.12.0a0+git0aa3c39'

zou3519 commented 2 years ago

Agree that the error message could be clearer