Skip to content

Unflatten segmentation faults if dim is negative integer #31184

@RobertCsordas

Description

@RobertCsordas

🐛 Bug

When using named tensors, and handling negative integer as a dim argument of unflatten, pytorch segfaults.

To Reproduce

import torch
a=torch.zeros(256)
a.unflatten(-1, [('x', 2), ('y',128)])

Expected behavior

[robert@asdf crash]$ python crash.py 
Segmentation fault (core dumped)

Environment

PyTorch version: 1.3.1
Is debug build: No
CUDA used to build PyTorch: 10.1.243

OS: Manjaro Linux
GCC version: (GCC) 9.2.0
CMake version: version 3.15.5

Python version: 3.7
Is CUDA available: Yes
CUDA runtime version: 10.1.243
GPU models and configuration:
GPU 0: GeForce RTX 2080 Ti
GPU 1: GeForce GTX TITAN X

Nvidia driver version: 435.21
cuDNN version: /usr/lib/libcudnn.so.7.6.4

Versions of relevant libraries:
[pip3] numpy==1.17.3
[pip3] torch==1.3.1
[pip3] torchfile==0.1.0
[pip3] torchvision==0.3.0a0+fe4d17f
[conda] Could not collect

Additional context

It works fine with positive integers and names. The size check also works for negative integers, as I get a proper error message if I specify negative dim and the size doesn't match.

The relevant part of GDB output:

Program received signal SIGSEGV, Segmentation fault.
0x00007fff95884bd8 in void std::vector<at::Dimname, std::allocator<at::Dimname> >::_M_range_insert<at::Dimname const*>(__gnu_cxx::__normal_iterator<at::Dimname*, std::vector<at::Dimname, std::allocator<at::Dimname> > >, at::Dimname const*, at::Dimname const*, std::forward_iterator_tag) () from /home/robert/.local/lib/python3.7/site-packages/torch/lib/libtorch.so
(gdb) bt
#0  0x00007fff95884bd8 in void std::vector<at::Dimname, std::allocator<at::Dimname> >::_M_range_insert<at::Dimname const*>(__gnu_cxx::__normal_iterator<at::Dimname*, std::vector<at::Dimname, std::allocator<at::Dimname> > >, at::Dimname const*, at::Dimname const*, std::forward_iterator_tag) () from /home/robert/.local/lib/python3.7/site-packages/torch/lib/libtorch.so
#1  0x00007fff958834a8 in at::native::unflatten(at::Tensor const&, long, c10::ArrayRef<long>, c10::ArrayRef<at::Dimname>) () from /home/robert/.local/lib/python3.7/site-packages/torch/lib/libtorch.so
#2  0x00007fff95ca783e in at::TypeDefault::unflatten(at::Tensor const&, long, c10::ArrayRef<long>, c10::ArrayRef<at::Dimname>) () from /home/robert/.local/lib/python3.7/site-packages/torch/lib/libtorch.so
#3  0x00007fff97839dba in torch::autograd::VariableType::(anonymous namespace)::unflatten(at::Tensor const&, long, c10::ArrayRef<long>, c10::ArrayRef<at::Dimname>) () from /home/robert/.local/lib/python3.7/site-packages/torch/lib/libtorch.so
#4  0x00007fffdba04f81 in torch::autograd::THPVariable_unflatten(_object*, _object*, _object*) () from /home/robert/.local/lib/python3.7/site-packages/torch/lib/libtorch_python.so

cc @ezyang @gchanan @zou3519

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions