Skip to content

Python interpreter died without Traceback when CPU Tensor divided by zero. #8079

Closed

Description

@woxuankai

There's no Traceback when a cpu tensor is divided by zero.

i.e.

$ python3
Python 3.6.5 (default, May 11 2018, 13:30:17) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> a = torch.LongTensor([1,2]).cuda()
>>> a/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: invalid argument 3: divide by zero at /pytorch/aten/src/THC/generic/THCTensorMathPairwise.cu:88
>>> a = torch.LongTensor([1,2])
>>> a/0
Floating point exception  #######No Traceback, just exit without any indication.
$ echo $?
136

If there is no Traceback, it will be difficult to locate the bugs.

What about raising an exception instead of just aborting ?

Environment:
Python 3.6 provided by Debian Testing, PyTorch 0.4.0 installed with pip3 (pytorch compiled from source has the same behavior), CUDA 9.1, cudnn 7.1.

Thanks

Activity

  1. soumith commented on Jun 4, 2018

    @soumith
    Collaborator

    dividing an Integer Tensor by an integer 0 (as opposed to Float Tensor divided by 0.0) will lead to this exception. This is not easily catchable (last I looked) at a global level.

    See:

    https://stackoverflow.com/a/3105604
    https://stackoverflow.com/a/6121690

    Closing this as wontfix, because the amount of effort is really large to justify (full audit of all core libraries and replacing all integer division with a macro that branch-handles divide by zero).

  2. samuela commented on Jul 18, 2018

    @samuela
    Contributor

    As tricky as this may be to fix, it's still a bug in a very real sense. Allowing C errors to creep into Python code is the result of a leaky abstraction.

  3. soumith commented on Jul 18, 2018

    @soumith
    Collaborator

    @samuela i completely agree. Unfortunately there's no easy fix. We cant set a signal trap, or catch an exception in C land to catch a Floating point exception. The only solution is to actually never do an integer divide by zero, so the entire set of C kernels have to guard against this.

  4. samuela commented on Jul 18, 2018

    @samuela
    Contributor

    @soumith Understood, and I don't think this bug is the worst thing in the world to be completely honest.

    FWIW I tried this in TF and they do catch these errors. I'm not sure of their implementation details though.

    import tensorflow as tf
    tf.enable_eager_execution()
    tf.constant(3) / 0

    produces

    ---------------------------------------------------------------------------
    InvalidArgumentError                      Traceback (most recent call last)
    <ipython-input-1-3d0865101110> in <module>()
          1 import tensorflow as tf
          2 tf.enable_eager_execution()
    ----> 3 tf.constant(3) / 0
    
    google3/third_party/tensorflow/python/ops/math_ops.py in binary_op_wrapper(x, y)
        861           else:
        862             raise
    --> 863       return func(x, y, name=name)
        864 
        865   def binary_op_wrapper_sparse(sp_x, y):
    
    google3/third_party/tensorflow/python/ops/math_ops.py in _div_python2(x, y, name)
        982       return gen_math_ops.real_div(x, y, name=name)
        983     else:
    --> 984       return gen_math_ops.floor_div(x, y, name=name)
        985 
        986 
    
    google3/third_party/tensorflow/python/ops/gen_math_ops.py in floor_div(x, y, name)
       3011       else:
       3012         message = e.message
    -> 3013       _six.raise_from(_core._status_to_exception(e.code, message), None)
       3014 
       3015 
    
    google3/third_party/py/six/__init__.py in raise_from(value, from_value)
        735 else:
        736     def raise_from(value, from_value):
    --> 737         raise value
        738 
        739 
    
    InvalidArgumentError: Integer division by zero [Op:FloorDiv] name: div/
    
  5. fmassa commented on Jul 18, 2018

    @fmassa
    Member

    @samuela can you try to see what happens if the divisor is a tf.constant as well, potentially with more than one element?

  6. samuela commented on Jul 18, 2018

    @samuela
    Contributor

    @fmassa

    import tensorflow as tf
    tf.enable_eager_execution()
    tf.constant(3) / tf.constant(0)

    is mostly the same:

    ---------------------------------------------------------------------------
    InvalidArgumentError                      Traceback (most recent call last)
    <ipython-input-1-07abdcaac50d> in <module>()
          1 import tensorflow as tf
          2 tf.enable_eager_execution()
    ----> 3 tf.constant(3) / tf.constant(0)
    
    google3/third_party/tensorflow/python/ops/math_ops.py in binary_op_wrapper(x, y)
        849     with ops.name_scope(None, op_name, [x, y]) as name:
        850       if isinstance(x, ops.Tensor) and isinstance(y, ops.Tensor):
    --> 851         return func(x, y, name=name)
        852       elif not isinstance(y, sparse_tensor.SparseTensor):
        853         try:
    
    google3/third_party/tensorflow/python/ops/math_ops.py in _div_python2(x, y, name)
        982       return gen_math_ops.real_div(x, y, name=name)
        983     else:
    --> 984       return gen_math_ops.floor_div(x, y, name=name)
        985 
        986 
    
    google3/third_party/tensorflow/python/ops/gen_math_ops.py in floor_div(x, y, name)
       3011       else:
       3012         message = e.message
    -> 3013       _six.raise_from(_core._status_to_exception(e.code, message), None)
       3014 
       3015 
    
    google3/third_party/py/six/__init__.py in raise_from(value, from_value)
        735 else:
        736     def raise_from(value, from_value):
    --> 737         raise value
        738 
        739 
    
    InvalidArgumentError: Integer division by zero [Op:FloorDiv] name: div/
    

    and

    import tensorflow as tf
    tf.enable_eager_execution()
    tf.constant(3) / tf.constant([0, 1])

    is also basically the same:

    ---------------------------------------------------------------------------
    InvalidArgumentError                      Traceback (most recent call last)
    <ipython-input-1-5f9dc24174f2> in <module>()
          1 import tensorflow as tf
          2 tf.enable_eager_execution()
    ----> 3 tf.constant(3) / tf.constant([0, 1])
    
    google3/third_party/tensorflow/python/ops/math_ops.py in binary_op_wrapper(x, y)
        849     with ops.name_scope(None, op_name, [x, y]) as name:
        850       if isinstance(x, ops.Tensor) and isinstance(y, ops.Tensor):
    --> 851         return func(x, y, name=name)
        852       elif not isinstance(y, sparse_tensor.SparseTensor):
        853         try:
    
    google3/third_party/tensorflow/python/ops/math_ops.py in _div_python2(x, y, name)
        982       return gen_math_ops.real_div(x, y, name=name)
        983     else:
    --> 984       return gen_math_ops.floor_div(x, y, name=name)
        985 
        986 
    
    google3/third_party/tensorflow/python/ops/gen_math_ops.py in floor_div(x, y, name)
       3011       else:
       3012         message = e.message
    -> 3013       _six.raise_from(_core._status_to_exception(e.code, message), None)
       3014 
       3015 
    
    google3/third_party/py/six/__init__.py in raise_from(value, from_value)
        735 else:
        736     def raise_from(value, from_value):
    --> 737         raise value
        738 
        739 
    
    InvalidArgumentError: Integer division by zero [Op:FloorDiv] name: div/
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions