Skip to content

Segmentation fault when dividing by zero with integer tensors #327

Closed

Description

@fmassa

This seems to be an issue with TH and not pytorch, the same thing happens in lua torch.

import torch
a = torch.IntTensor([0,1])
b = torch.IntTensor([0,1])
print(a.div(b)) # Floating point exception (core dumped)

Another unrelated weirdness happens with float tensors when the answer is inf

import torch
a = torch.FloatTensor([1,1])
b = torch.FloatTensor([0,0])
a.div(b)

which works on lua torch, but gives

<repr(<torch.FloatTensor at 0x7f8a012eee18>) failed: RuntimeError: inconsistent tensor size at /tmp/pip-zh0a53-build/torch/lib/TH/generic/THTensorMath.c:90>

in pytorch.

cc @ezyang @gchanan @zou3519

Activity

  1. soumith commented on Dec 19, 2016

    @soumith
    Collaborator

    integer division by zero getting a FP exception is something we cannot avoid, it's a hardware exception. Numpy wraps it's integer division code to literally check the denominators with conditionals and instead generates fpectl exceptions. I dont think this can be a hi-pri issue for us just looking at the amount of work involved in refactoring TH.

    Your second issue looks more like a bug.

  2. apaszke commented on Dec 19, 2016

    @apaszke
    Contributor

    The first one is expected, second one is a bug in printing, when there are no non NaN and inf values in the tensor.

  3. self-assigned this
    on Dec 19, 2016
  4. soumith commented on Dec 20, 2016

    @soumith
    Collaborator

    the printing is fixed now in master via #332

  5. fmassa commented on Sep 11, 2019

    @fmassa
    MemberAuthor

    I'm re-opening this issue because it has appeared plenty of times in the past (see linked issues, plus many others that haven't been linked).

    From discussion in #8079, both TensorFlow and numpy handles the 0 / 0 division gracefully, so the performance hit of adding an extra check (for integer tensors) wouldn't be that bad in comparison.

    With the current implementation, adding those checks only amounts to a few extra lines, so I think it should be reconsidered.

  6. reopened this on Sep 11, 2019
  7. added
    module: crash
    module: NaNs and Infs
    on Sep 11, 2019
  8. 17 remaining items

  9. Baranowski commented on Jan 24, 2020

    @Baranowski
    Contributor

    I was too ambitious with point 3. I still believe that doing a reduction pass over the entire divisor to find a zero before the actual division loop would be superior for performance. However, after digging a bit more into ReduceOps.cpp, I don't see a reasonably easy way to do it. The necessary helper functions to do that are static within ReduceOps.cpp so I think it would require defining a new reduction op (something like tensor.contains(x)) and that seems way beyond the scope of this issue.

    So I'm going to follow fmassa's original suggestion:

    modify [the div_kernel] to also check if b is zero, in which case we set a flag in the loop indicating that a ZeroDivisionError should be raised

    However, I don't understand why fmassa suggested that ZeroDivisionError should be raised outside the loop, as opposed to throwing from within the cpu_kernel. @andreaskoepf and @fmassa, any ideas about this sentence?

  10. andreaskoepf commented on Jan 24, 2020

    @andreaskoepf
    Collaborator

    (..) ZeroDivisionError should be raised outside the loop, as opposed to throwing from within the cpu_kernel.

    AFAIK you can now throw from inside parallel cpu kernels, e.g. at::{parallel_for, parallel_reduce} loops store the first caught exception and rethrow it on the calling thread after the threads-join. parallel_for is used TensorIterator::for_each() code which is called from cpu_kernel & cpu_kernel_vec . Some time ago (e.g. in TH code) this exception-forwarding was not done automatically. First thing you might test is to add a TORCH_CHECK(b != 0, "ZeroDivisionError") in div_kernel and run a benchmark with and without this line to see the perf impact and also to test the integral-type error handling for a zero denominator.

  11. OOXXXXOO commented on Sep 11, 2020

    @OOXXXXOO

    Pytorch 1.3-1.5
    Python 3.7.0 (default, Oct 9 2018, 10:31:47)
    [GCC 7.3.0] :: Anaconda, Inc. on linux
    Type "help", "copyright", "credits" or "license" for more information.

    import torch
    torch.ones(3, dtype=torch.int8) / torch.zeros(3, dtype=torch.int8)

    [1] 624290 floating point exception (core dumped) python

    ❯ source activate nightly
    ❯ python
    Python 3.7.9 (default, Aug 31 2020, 12:42:55)
    [GCC 7.3.0] :: Anaconda, Inc. on linux
    Type "help", "copyright", "credits" or "license" for more information.

    pytoch 1.6

    import torch
    torch.ones(3, dtype=torch.int8) / torch.zeros(3, dtype=torch.int8)

    Traceback (most recent call last):
    File "", line 1, in
    RuntimeError: Integer division of tensors using div or / is no longer supported, and in a future release div will perform true division as in Python 3. Use true_divide or floor_divide (// in Python) instead.

  12. OOXXXXOO commented on Sep 11, 2020

    @OOXXXXOO

    When i train a EfficientNet in step 5000+ step that cause floatpoint exception error int pyotch 1.5:
    when i train on pytorch 1.6 cause :

    terminate called after throwing an instance of 'c10::Error'
    what(): invalid device pointer: 0x7ff443ffdc00
    Exception raised from free at /opt/conda/conda-bld/pytorch_1595629427478/work/c10/cuda/CUDACachingAllocator.cpp:844 (most recent call first):
    frame #0: c10::Error::Error(c10::SourceLocation, std::string) + 0x4d (0x7ff789d4377d in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libc10.so)
    frame #1: c10::cuda::CUDACachingAllocator::raw_delete(void*) + 0x529 (0x7ff789f93769 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libc10_cuda.so)
    frame #2: c10::TensorImpl::release_resources() + 0x4d (0x7ff789d2fb1d in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libc10.so)
    frame #3: + 0xc8bf69 (0x7ff78ae34f69 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so)
    frame #4: + 0xc9d394 (0x7ff78ae46394 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so)
    frame #5: + 0xc9f0b5 (0x7ff78ae480b5 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so)
    frame #6: + 0xc9a30e (0x7ff78ae4330e in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so)
    frame #7: + 0xc9befb (0x7ff78ae44efb in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so)
    frame #8: at::native::cudnn_convolution_backward_input(c10::ArrayRef, at::Tensor const&, at::Tensor const&, c10::ArrayRef, c10::ArrayRef, c10::ArrayRef, long, bool, bool) + 0xb2 (0x7ff78ae45452 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so)
    frame #9: + 0xd0165b (0x7ff78aeaa65b in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so)
    frame #10: + 0xd31478 (0x7ff78aeda478 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so)
    frame #11: at::cudnn_convolution_backward_input(c10::ArrayRef, at::Tensor const&, at::Tensor const&, c10::ArrayRef, c10::ArrayRef, c10::ArrayRef, long, bool, bool) + 0x1ad (0x7ff7c13f0ced in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
    frame #12: at::native::cudnn_convolution_backward(at::Tensor const&, at::Tensor const&, at::Tensor const&, c10::ArrayRef, c10::ArrayRef, c10::ArrayRef, long, bool, bool, std::array<bool, 2ul>) + 0x223 (0x7ff78ae43b23 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so)
    frame #13: + 0xd01745 (0x7ff78aeaa745 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so)
    frame #14: + 0xd314d4 (0x7ff78aeda4d4 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cuda.so)
    frame #15: at::cudnn_convolution_backward(at::Tensor const&, at::Tensor const&, at::Tensor const&, c10::ArrayRef, c10::ArrayRef, c10::ArrayRef, long, bool, bool, std::array<bool, 2ul>) + 0x1e2 (0x7ff7c13ff6a2 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
    frame #16: + 0x2c250c2 (0x7ff7c30c30c2 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
    frame #17: + 0x2c39684 (0x7ff7c30d7684 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
    frame #18: at::cudnn_convolution_backward(at::Tensor const&, at::Tensor const&, at::Tensor const&, c10::ArrayRef, c10::ArrayRef, c10::ArrayRef, long, bool, bool, std::array<bool, 2ul>) + 0x1e2 (0x7ff7c13ff6a2 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
    frame #19: torch::autograd::generated::CudnnConvolutionBackward::apply(std::vector<at::Tensor, std::allocatorat::Tensor >&&) + 0x258 (0x7ff7c2f4a098 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
    frame #20: + 0x30d1017 (0x7ff7c356f017 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
    frame #21: torch::autograd::Engine::evaluate_function(std::shared_ptrtorch::autograd::GraphTask&, torch::autograd::Node*, torch::autograd::InputBuffer&, std::shared_ptrtorch::autograd::ReadyQueue const&) + 0x1400 (0x7ff7c356a860 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
    frame #22: torch::autograd::Engine::thread_main(std::shared_ptrtorch::autograd::GraphTask const&) + 0x451 (0x7ff7c356b401 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
    frame #23: torch::autograd::Engine::thread_init(int, std::shared_ptrtorch::autograd::ReadyQueue const&, bool) + 0x89 (0x7ff7c3563579 in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_cpu.so)
    frame #24: torch::autograd::python::PythonEngine::thread_init(int, std::shared_ptrtorch::autograd::ReadyQueue const&, bool) + 0x4a (0x7ff7c789299a in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
    frame #25: + 0xc819d (0x7ff7ca3cc19d in /home/winshare/anaconda3/envs/nightly/lib/python3.7/site-packages/torch/lib/../../../.././libstdc++.so.6)
    frame #26: + 0x9609 (0x7ff7e43bc609 in /lib/x86_64-linux-gnu/libpthread.so.0)
    frame #27: clone + 0x43 (0x7ff7e42e3103 in /lib/x86_64-linux-gnu/libc.so.6)

  13. added a commit that references this issue on Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

high prioritymodule: NaNs and Infsmodule: crashtriaged

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions