You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Segmentation fault when dividing by zero with integer tensors #327
high prioritymodule: NaNs and InfsProblems related to NaN and Inf handling in floating pointmodule: crashProblem manifests as a hard crash, as opposed to a RuntimeErrortriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module
<repr(<torch.FloatTensor at 0x7f8a012eee18>) failed: RuntimeError: inconsistent tensor size at /tmp/pip-zh0a53-build/torch/lib/TH/generic/THTensorMath.c:90>
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.
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.
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?
(..) 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.
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.
[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.
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.
high prioritymodule: NaNs and InfsProblems related to NaN and Inf handling in floating pointmodule: crashProblem manifests as a hard crash, as opposed to a RuntimeErrortriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module
Activity
soumith commentedon Dec 19, 2016
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.
apaszke commentedon Dec 19, 2016
The first one is expected, second one is a bug in printing, when there are no non NaN and inf values in the tensor.
soumith commentedon Dec 20, 2016
the printing is fixed now in master via #332
fmassa commentedon Sep 11, 2019
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.
17 remaining items
Baranowski commentedon Jan 24, 2020
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 arestaticwithinReduceOps.cppso I think it would require defining a new reduction op (something liketensor.contains(x)) and that seems way beyond the scope of this issue.So I'm going to follow fmassa's original suggestion:
However, I don't understand why fmassa suggested that
ZeroDivisionErrorshould be raised outside the loop, as opposed to throwing from within thecpu_kernel. @andreaskoepf and @fmassa, any ideas about this sentence?andreaskoepf commentedon Jan 24, 2020
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_foris usedTensorIterator::for_each()code which is called fromcpu_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 aTORCH_CHECK(b != 0, "ZeroDivisionError")indiv_kerneland 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.div_kernel: throw when dividing by integer zero (pytorch#32629)
div_kernel: throw when dividing by integer zero (pytorch#32629)
OOXXXXOO commentedon Sep 11, 2020
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.
[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
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.
OOXXXXOO commentedon Sep 11, 2020
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)
Detect computeAt causing mismatched TensorDomain (pytorch#327)
Detect computeAt causing mismatched TensorDomain (pytorch#327)
Adding GPT-2-LH-Model model (pytorch#327)
Merge pull request pytorch#327 from pjh5/smoke_cuver
div_kernel: throw when dividing by integer zero (pytorch#32629)