Skip to content

[te] Don't fuse integer fmod or remainder#48700

Closed
bertmaher wants to merge 7 commits intogh/bertmaher/36/basefrom
gh/bertmaher/36/head
Closed

[te] Don't fuse integer fmod or remainder#48700
bertmaher wants to merge 7 commits intogh/bertmaher/36/basefrom
gh/bertmaher/36/head

Conversation

@bertmaher
Copy link
Copy Markdown
Contributor

@bertmaher bertmaher commented Dec 2, 2020

Stack from ghstack:

fmod and remainder on int tensors will raise ZeroDivisionError if their divisors are 0. I don't think we should try to generate code that raises exceptions. If at some point we really wanted to fuse these, I might lean towards calling a C++ helper function from the generated code.

Differential Revision: D25265792

fmod and remainder on int tensors will raise ZeroDivisionError if their divisors are 0.  I don't think we should try to generate code that raises exceptions.  If at some point we really wanted to fuse these, I might lean towards calling a C++ helper function from the generated code.

Differential Revision: [D25265792](https://our.internmc.facebook.com/intern/diff/D25265792/)

[ghstack-poisoned]
@facebook-github-bot facebook-github-bot added cla signed oncall: jit Add this issue/PR to JIT oncall triage queue labels Dec 2, 2020
bertmaher added a commit that referenced this pull request Dec 2, 2020
fmod and remainder on int tensors will raise ZeroDivisionError if their divisors are 0.  I don't think we should try to generate code that raises exceptions.  If at some point we really wanted to fuse these, I might lean towards calling a C++ helper function from the generated code.

Differential Revision: [D25265792](https://our.internmc.facebook.com/intern/diff/D25265792/)

ghstack-source-id: 117634182
Pull Request resolved: #48700
Copy link
Copy Markdown

@ZolotukhinM ZolotukhinM left a comment

Choose a reason for hiding this comment

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

Overall looks good, but I wonder if division by 0 was the only issue. There is another test called test_binary_div_ops that doesn't try to divide by 0 but still fails with int8. Only fusing floats would solve that too, but I'm curious what was the problem there - we might have some other bug.

if (!st || !isFloatingType(*st)) {
return false;
}
} else if (!v->type()->cast<FloatType>()) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This doesn't include Half and Double, does it?

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.

It might. We don't seem to differentiate statically halfs, doubles and single-points, @eellison ?

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.

this check is on a non-Tensor type (Scalar value), so Half/Double doesn't apply here

Comment thread test/test_jit_fuser_te.py
return lambda x, y: fn(x, y)

dtypes = [
# FIXME: Fails in IR Eval: torch.int8 and_ cpu
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This FIXME is for a different issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What's the issue? It looked like a vestigial FIXME.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The issue is that the combination op=and, dtype=int8, device=cpu fails on IR eval :) I added it in one of the recent commits

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ohhh, so what threw me off was this being in the block of dtypes. I read it as saying the combination of int8 and cpu fails :-p. Anyways I'll put it back

@bertmaher
Copy link
Copy Markdown
Contributor Author

Overall looks good, but I wonder if division by 0 was the only issue. There is another test called test_binary_div_ops that doesn't try to divide by 0 but still fails with int8. Only fusing floats would solve that too, but I'm curious what was the problem there - we might have some other bug.

Actually there are at least two other issues which end up being side-stepped by this PR. I should probably fix them too, really, although they become more annoying to test if ints are banned in fusion (which I still think is the right call for the ZeroDivisionError reason).

  1. We translate fmod(int, float) to (int)fmod(float, float), but eager mode translates it to (int)(int % int). Banning ints at the fuser level masks this bad translation.
  2. Even if we somehow did create TE for fmod(int, int) we throw unsupported_dtype from llvm codegen. Which isn't entirely wrong, since we arguably should translate it as normal mod(int, int).

@dr-ci
Copy link
Copy Markdown

dr-ci Bot commented Dec 2, 2020

💊 CI failures summary and remediations

As of commit 2d267c9 (more details on the Dr. CI page):


  • 1/1 failures possibly* introduced in this PR
    • 1/1 non-CircleCI failure(s)

Extra GitHub checks: 1 failed


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 or post in the (internal) Dr. CI Users group.

See how this bot performed.

This comment has been revised 33 times.

Copy link
Copy Markdown
Contributor

@eellison eellison left a comment

Choose a reason for hiding this comment

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

LGTM

if (!st || !isFloatingType(*st)) {
return false;
}
} else if (!v->type()->cast<FloatType>()) {
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.

this check is on a non-Tensor type (Scalar value), so Half/Double doesn't apply here

if (!st || !isFloatingType(*st)) {
return false;
}
} else if (!v->type()->cast<FloatType>()) {
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.

You could also allow constant non-zero ints:

(v->type()->cast<IntType>() && constant_as<int>(v).value_or(0) != 0)

}
}

// Value is only supported if operands are floats.
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.

Do we need to exclude non-floating point tensors if they are the first input ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's a good point, this could be relaxed.

fmod and remainder on int tensors will raise ZeroDivisionError if their divisors are 0.  I don't think we should try to generate code that raises exceptions.  If at some point we really wanted to fuse these, I might lean towards calling a C++ helper function from the generated code.

Differential Revision: [D25265792](https://our.internmc.facebook.com/intern/diff/D25265792/)

[ghstack-poisoned]
fmod and remainder on int tensors will raise ZeroDivisionError if their divisors are 0.  I don't think we should try to generate code that raises exceptions.  If at some point we really wanted to fuse these, I might lean towards calling a C++ helper function from the generated code.

Differential Revision: [D25265792](https://our.internmc.facebook.com/intern/diff/D25265792/)

[ghstack-poisoned]
fmod and remainder on int tensors will raise ZeroDivisionError if their divisors are 0.  I don't think we should try to generate code that raises exceptions.  If at some point we really wanted to fuse these, I might lean towards calling a C++ helper function from the generated code.

Differential Revision: [D25265792](https://our.internmc.facebook.com/intern/diff/D25265792/)

[ghstack-poisoned]
bertmaher added a commit that referenced this pull request Dec 3, 2020
Pull Request resolved: #48700

fmod and remainder on int tensors will raise ZeroDivisionError if their divisors are 0.  I don't think we should try to generate code that raises exceptions.  If at some point we really wanted to fuse these, I might lean towards calling a C++ helper function from the generated code.
ghstack-source-id: 117741914

Differential Revision: [D25265792](https://our.internmc.facebook.com/intern/diff/D25265792/)
…der"

fmod and remainder on int tensors will raise ZeroDivisionError if their divisors are 0.  I don't think we should try to generate code that raises exceptions.  If at some point we really wanted to fuse these, I might lean towards calling a C++ helper function from the generated code.

Differential Revision: [D25265792](https://our.internmc.facebook.com/intern/diff/D25265792/)

[ghstack-poisoned]
fmod and remainder on int tensors will raise ZeroDivisionError if their divisors are 0.  I don't think we should try to generate code that raises exceptions.  If at some point we really wanted to fuse these, I might lean towards calling a C++ helper function from the generated code.

Differential Revision: [D25265792](https://our.internmc.facebook.com/intern/diff/D25265792/)

[ghstack-poisoned]
bertmaher added a commit that referenced this pull request Dec 4, 2020
Pull Request resolved: #48700

fmod and remainder on int tensors will raise ZeroDivisionError if their divisors are 0.  I don't think we should try to generate code that raises exceptions.  If at some point we really wanted to fuse these, I might lean towards calling a C++ helper function from the generated code.
ghstack-source-id: 117821429

Differential Revision: [D25265792](https://our.internmc.facebook.com/intern/diff/D25265792/)
…mod or remainder"

fmod and remainder on int tensors will raise ZeroDivisionError if their divisors are 0.  I don't think we should try to generate code that raises exceptions.  If at some point we really wanted to fuse these, I might lean towards calling a C++ helper function from the generated code.

Differential Revision: [D25265792](https://our.internmc.facebook.com/intern/diff/D25265792/)

[ghstack-poisoned]
bertmaher added a commit that referenced this pull request Dec 4, 2020
Pull Request resolved: #48700

fmod and remainder on int tensors will raise ZeroDivisionError if their divisors are 0.  I don't think we should try to generate code that raises exceptions.  If at some point we really wanted to fuse these, I might lean towards calling a C++ helper function from the generated code.
ghstack-source-id: 117845642

Differential Revision: [D25265792](https://our.internmc.facebook.com/intern/diff/D25265792/)
@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 4, 2020

Codecov Report

Merging #48700 (2d267c9) into gh/bertmaher/36/base (90a3049) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@                  Coverage Diff                  @@
##           gh/bertmaher/36/base   #48700   +/-   ##
=====================================================
  Coverage                 80.83%   80.83%           
=====================================================
  Files                      1860     1860           
  Lines                    200742   200751    +9     
=====================================================
+ Hits                     162274   162285   +11     
+ Misses                    38468    38466    -2     

return true;
}

bool typesAreSupported(const Node* node) {
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.

While you’re at it, could you move the cuda_only fuse set ?

@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request has been merged in 2d07d5b.

@facebook-github-bot facebook-github-bot deleted the gh/bertmaher/36/head branch December 8, 2020 15:23
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
Pull Request resolved: pytorch#48700

fmod and remainder on int tensors will raise ZeroDivisionError if their divisors are 0.  I don't think we should try to generate code that raises exceptions.  If at some point we really wanted to fuse these, I might lean towards calling a C++ helper function from the generated code.
ghstack-source-id: 117845642

Test Plan: `buck test //caffe2/test:jit -- test_binary_ops`

Reviewed By: eellison

Differential Revision: D25265792

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

Labels

cla signed Merged oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants