Skip to content

div_kernel: throw when dividing by integer zero - #32629

Closed
Baranowski wants to merge 2 commits into
pytorch:masterfrom
Quansight:wbaranowski-div_zero-327
Closed

Baranowski wants to merge 2 commits into
pytorch:masterfrom
Quansight:wbaranowski-div_zero-327

Conversation

@Baranowski

Copy link
Copy Markdown
Contributor

Fixes #327

@Baranowski

Copy link
Copy Markdown
Contributor Author

Much to my surprise, the additional check does not seem to cause noticeable slowdown. To be fair, the measurements are not stable in my dev environment between different runs (and thus I'm not posting them) but both timeit and ASV suggest that there is nothing outrageous happening (i.e., if there is a slowdown, it's no greater than about 5%).

For the record, here is my ASV benchmark:

class TimeDiv:
    params = [100, 500, 1000, 2000, 3000]

    def setup(self, n):
        self.a = torch.rand(n, n).mul(10000).ceil().int()
        self.b = torch.rand(n, n).mul(10000).ceil().int().add(1)

    def time_div(self, n):
        self.a.div(self.b)

And timeit:

a = None
b = None

if __name__ == "__main__":
    for size in (100, 500, 1000, 2000, 3000):
        a = torch.rand(size, size).mul(10000).ceil().int()
        b = torch.rand(size, size).mul(10000).ceil().int().add(1)
        a.div(b)
        print("{size}x{size}: {duration}".format(
            size=size,
            duration=timeit.timeit("a.div(b)", setup="from __main__ import a, b", number=10000)
        ))

As for the error handling, division by zero results in the following:

$ python div_by_zero.py 
Traceback (most recent call last):
  File "div_by_zero.py", line 4, in <module>
    a.div(a)
RuntimeError: ZeroDivisionError

$ cat div_by_zero.py 
import torch

a = torch.IntTensor([1, 0])
a.div(a)

@kostmo

kostmo commented Jan 26, 2020 •

Copy link
Copy Markdown
Member

💊 CircleCI build failures summary and remediations

As of commit ff61297:

  • 1/1 failures introduced in this PR

Detailed failure analysis

One may explore the probable reasons each build failed interactively on the Dr. CI website.

🕵️ 1 new failure recognized by patterns

The following build failures do not appear to be due to upstream breakage:

See CircleCI build pytorch_macos_10_13_py3_test (1/1)

Step: "Test" (full log | pattern match details)

Jan 27 08:39:58 RuntimeError: test_autograd failed!
Jan 27 08:39:57 Ran 1047 tests in 765.239s 
Jan 27 08:39:57  
Jan 27 08:39:57 FAILED (errors=1, skipped=13, expected failures=1) 
Jan 27 08:39:57  
Jan 27 08:39:57 Generating XML reports... 
Jan 27 08:39:58 Traceback (most recent call last): 
Jan 27 08:39:58   File "test/run_test.py", line 456, in <module> 
Jan 27 08:39:58     main() 
Jan 27 08:39:58   File "test/run_test.py", line 449, in main 
Jan 27 08:39:58     raise RuntimeError(message) 
Jan 27 08:39:58 RuntimeError: test_autograd failed! 
Jan 27 08:39:59 + cleanup 
Jan 27 08:39:59 + retcode=1 
Jan 27 08:39:59 + set +x 

This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker.

This comment has been revised 5 times.

@Baranowski
Baranowski force-pushed the wbaranowski-div_zero-327 branch from bda13c1 to 04772aa Compare January 26, 2020 16:53
@rgommers
rgommers requested a review from ezyang January 26, 2020 19:18

@andreaskoepf andreaskoepf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me (see my comment for test_torch.py).

Comment thread test/test_torch.py Resolved

@ezyang ezyang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need some testing changes.

@ezyang

ezyang commented Jan 27, 2020

Copy link
Copy Markdown
Contributor

@VitalyFedyunin or @ngimel could you help us check if there are any perf implications; if the benchmarking above seems sufficient?

@VitalyFedyunin

Copy link
Copy Markdown
Contributor

As per https://www.agner.org/optimize/instruction_tables.pdf conditional short jump is about 20-40 times faster than division op. So slow down seems to be insignificant and benchmarks are correct.

@VitalyFedyunin

Copy link
Copy Markdown
Contributor

I think we can give it go, but prefer to have second opinion CC @ngimel

@Baranowski

Copy link
Copy Markdown
Contributor Author

As for the CI failure, I cannot reproduce it and it doesn't seem related to my change, so I'm tempted to claim it spurious.

@facebook-github-bot facebook-github-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ezyang is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@ngimel ngimel left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@ezyang merged this pull request in 8e41615.

1 similar comment
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@ezyang merged this pull request in 8e41615.

wuhuikx pushed a commit to wuhuikx/pytorch that referenced this pull request Jan 30, 2020
Summary:
Fixes pytorch#327
Pull Request resolved: pytorch#32629

Differential Revision: D19595782

Pulled By: ezyang

fbshipit-source-id: f5bbb298f150efe63a698e8a0b53a84871d16560
ttumiel pushed a commit to ttumiel/pytorch that referenced this pull request Mar 4, 2020
Summary:
Fixes pytorch#327
Pull Request resolved: pytorch#32629

Differential Revision: D19595782

Pulled By: ezyang

fbshipit-source-id: f5bbb298f150efe63a698e8a0b53a84871d16560
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segmentation fault when dividing by zero with integer tensors

9 participants