Repository: pytorch
Issue: vectorized convert_to_int_of_same_size <int64_t> can't handle nan
Labels: module: cpu,module: error checking,triaged,module: NaNs and Infs

The relevant code is at
https://github.com/pytorch/pytorch/blob/1071e92335d8a69107f4694f56bebbb9655371aa/aten/src/ATen/cpu/vec256/vec256.h#L115-L125

This can't handle nan, inf, or other large (small) values. We should error (e.g., trigger fpe?) in here. Triggering FPE is what `convert_to_int_of_same_size<int32_t>` does, according to https://scc.ustc.edu.cn/zlsc/sugon/intel/compiler_c/main_cls/intref_cls/common/intref_avx_cvttps_epi32.htm .

This manifests in the following script with `grid_sample`.

Original issue description:

## 🐛 Bug
CPU version of grid_sample got segmentation fault under some circumstances

## To Reproduce

Run this:
```
import torch
import torch.nn.functional as F

img = torch.empty([1, 3, 123, 456], dtype=torch.float32)
offset = torch.zeros([1, 123, 456, 2], dtype=torch.float32, requires_grad=True)
optimizer = torch.optim.SGD([offset], lr=0.0005, momentum=0.9)

for i in range(200):
    optimizer.zero_grad()
    warped = F.grid_sample(img, offset, mode='bilinear',padding_mode="border")
    loss = torch.mean(torch.sqrt(torch.abs(offset)))
    loss.backward()
    optimizer.step()
    print('{:04d} {:.6f}, '.format(i, loss.item()))
```
## Environment
PyTorch version: 1.0.0
Is debug build: No
CUDA used to build PyTorch: 10.0.130

OS: Ubuntu 18.04.2 LTS
GCC version: (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
CMake version: version 3.10.2

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: GeForce GTX 1080 Ti
Nvidia driver version: 410.93
cuDNN version: Could not collect

Versions of relevant libraries:
[pip3] numpy==1.16.1
[pip3] torch==1.0.0
[pip3] torchfile==0.1.0
[pip3] torchvision==0.2.1
[pip3] torchviz==0.0.1
[conda] Could not collect

## Additional context
GDB backtrace:
```
(gdb) run
Starting program: /usr/bin/python3.6 bug.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff39dc700 (LWP 21382)]
[New Thread 0x7ffff31db700 (LWP 21383)]
[New Thread 0x7ffff09da700 (LWP 21384)]
[New Thread 0x7fffec1d9700 (LWP 21385)]
[New Thread 0x7fffe99d8700 (LWP 21386)]
[New Thread 0x7fffe71d7700 (LWP 21387)]
[New Thread 0x7fffe49d6700 (LWP 21388)]
[Thread 0x7fffe49d6700 (LWP 21388) exited]
[Thread 0x7fffe71d7700 (LWP 21387) exited]
[Thread 0x7fffe99d8700 (LWP 21386) exited]
[Thread 0x7fffec1d9700 (LWP 21385) exited]
[Thread 0x7ffff09da700 (LWP 21384) exited]
[Thread 0x7ffff31db700 (LWP 21383) exited]
[Thread 0x7ffff39dc700 (LWP 21382) exited]
(gdb) [New Thread 0x7fffe49d6700 (LWP 21392)]
[New Thread 0x7fffe71d7700 (LWP 21393)]
[New Thread 0x7fffe99d8700 (LWP 21394)]
0000 0.000000, 

Thread 1 "python3.6" received signal SIGSEGV, Segmentation fault.
0x00007fff9823141f in std::enable_if<((((4l)==(1))||((4l)==(2)))||((4l)==(4)))||((4l)==(8)), at::vec256::(anonymous namespace)::Vec256<float> >::type at::vec256::(anonymous namespace)::mask_gather<4l, float>(at::vec256::(anonymous namespace)::Vec256<float> const&, float const*, at::vec256::(anonymous namespace)::Vec256<at::vec256::(anonymous namespace)::int_of_size<sizeof (float)>::type> const&, at::vec256::(anonymous namespace)::Vec256<float>&) [clone .constprop.166] () from /usr/local/lib/python3.6/dist-packages/torch/lib/libcaffe2.so
(gdb) backtrace
#0  0x00007fff9823141f in std::enable_if<((((4l)==(1))||((4l)==(2)))||((4l)==(4)))||((4l)==(8)), at::vec256::(anonymous namespace)::Vec256<float> >::type at::vec256::(anonymous namespace)::mask_gather<4l, float>(at::vec256::(anonymous namespace)::Vec256<float> const&, float const*, at::vec256::(anonymous namespace)::Vec256<at::vec256::(anonymous namespace)::int_of_size<sizeof (float)>::type> const&, at::vec256::(anonymous namespace)::Vec256<float>&) [clone .constprop.166] () from /usr/local/lib/python3.6/dist-packages/torch/lib/libcaffe2.so
#1  0x00007fff9824d7a1 in at::native::(anonymous namespace)::grid_sampler_2d_cpu_kernel_impl(at::Tensor const&, at::Tensor const&, long, long)::{lambda()#1}::operator()() const::{lambda()#2}::operator()() const ()
   from /usr/local/lib/python3.6/dist-packages/torch/lib/libcaffe2.so
#2  0x00007fff9824ee10 in at::native::(anonymous namespace)::grid_sampler_2d_cpu_kernel_impl(at::Tensor const&, at::Tensor const&, long, long) () from /usr/local/lib/python3.6/dist-packages/torch/lib/libcaffe2.so
#3  0x00007fff979d5c3d in at::native::grid_sampler_2d_cpu(at::Tensor const&, at::Tensor const&, long, long) () from /usr/local/lib/python3.6/dist-packages/torch/lib/libcaffe2.so
#4  0x00007fff97b97954 in at::CPUFloatType::grid_sampler_2d(at::Tensor const&, at::Tensor const&, long, long) const () from /usr/local/lib/python3.6/dist-packages/torch/lib/libcaffe2.so
#5  0x00007fff95b3b8cb in torch::autograd::VariableType::grid_sampler_2d(at::Tensor const&, at::Tensor const&, long, long) const () from /usr/local/lib/python3.6/dist-packages/torch/lib/libtorch.so.1
#6  0x00007fff979d6780 in at::native::grid_sampler(at::Tensor const&, at::Tensor const&, long, long) () from /usr/local/lib/python3.6/dist-packages/torch/lib/libcaffe2.so
#7  0x00007fff97c4f344 in at::TypeDefault::grid_sampler(at::Tensor const&, at::Tensor const&, long, long) const () from /usr/local/lib/python3.6/dist-packages/torch/lib/libcaffe2.so
#8  0x00007fff95b550e3 in torch::autograd::VariableType::grid_sampler(at::Tensor const&, at::Tensor const&, long, long) const () from /usr/local/lib/python3.6/dist-packages/torch/lib/libtorch.so.1
#9  0x00007ffff2d51e78 in torch::autograd::THPVariable_grid_sampler(_object*, _object*, _object*) () from /usr/local/lib/python3.6/dist-packages/torch/lib/libtorch_python.so
#10 0x00000000005030d5 in ?? ()
#11 0x0000000000506859 in _PyEval_EvalFrameDefault ()
#12 0x0000000000504c28 in ?? ()
#13 0x0000000000502540 in ?? ()
#14 0x0000000000502f3d in ?? ()
#15 0x0000000000507641 in _PyEval_EvalFrameDefault ()
#16 0x0000000000504c28 in ?? ()
#17 0x0000000000506393 in PyEval_EvalCode ()
#18 0x0000000000634d52 in ?? ()
#19 0x0000000000634e0a in PyRun_FileExFlags ()
#20 0x00000000006385c8 in PyRun_SimpleFileExFlags ()
#21 0x000000000063915a in Py_Main ()
#22 0x00000000004a6f10 in main ()
```