Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ARITH] Enhance Canonical Simplify for LE #15471

Merged
merged 3 commits into from
Aug 8, 2023
Merged

Conversation

Hzfengsy
Copy link
Member

@Hzfengsy Hzfengsy commented Aug 3, 2023

This PR enhances the canonical simplifier to support the following patterns:

x0 * s0 + x1 * s1 + ... + xn + c < 0, let d = gcd(s0, s1, ..., s{n-1}, c)

  1. if can prove -d < xn < d, then we can simplify the expression to x0 * (s0/d) + x1 * (s1/d) + ... + x{n-1} * (s{n-1}/d) < c/d, e.g. x * 8 + y < 16 where y \in [0, 8), we can simplify it to x < 2
  2. if xn is in pattern of yn % m, where m % d == 0, convert it to yn // d % (m/d) e.g. x1 * 64 + (x2 * 8 + x3) % 64 < 120, x3 \in [0, 8), we can simplify it to x1 * 8 + (x2 * 8 + x3) // 8 % 8 < 15 ==> x1 * 8 + x2 % 8 < 15

cc @tqchen

This PR enhances the canonical simplifier to support the following patterns:

x0 * s0 + x1 * s1 + ... + xn + c < 0, let d = gcd(s0, s1, ..., s{n-1}, c)

1. if can prove -d < xn < d, then we can simplify
   the expression to x0 * (s0/d) + x1 * (s1/d) + ... + x{n-1} * (s{n-1}/d) < c/d,
   e.g. `x * 8 + y < 16` where `y` \in [0, 8), we can simplify it to `x < 2`
2. if xn is in pattern of yn % m, where m % d == 0, convert it to yn // d % (m/d)
   e.g. `x1 * 64 + (x2 * 8 + x3) % 64 < 120`, `x3` \in [0, 8), we can simplify it to
   `x1 * 8 + (x2 * 8 + x3) // 8 % 8 < 15` ==> `x1 * 8 + x2 % 8 < 15`
@tvm-bot
Copy link
Collaborator

tvm-bot commented Aug 3, 2023

Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.

Generated by tvm-bot

@github-actions github-actions bot requested a review from tqchen August 3, 2023 16:59
@@ -64,7 +64,8 @@ def test_add_pipeline():
s[C].pragma(xo1, "parallel_launch_point")
s[C].pragma(xo2, "parallel_stride_pattern")
s[C].pragma(xo2, "parallel_barrier_when_finish")
s[C].vectorize(xi)
# FIXME(tvm-team): vector operators are not supported for codegen to C yet
Copy link
Member Author

Choose a reason for hiding this comment

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

This case is because of the poor support of vectorization for c_codegen.
Before this PR, TVM failed to simplify the branch predicate and skip the vectorization. This PR enhances the arith, and makes it TRUE vectoring, but failed on codegen stage.

In short:

  1. It's not related to this PR, as it's codegen issue
  2. It's not a regression, vectorized step is skipped before this PR.

@tqchen tqchen merged commit 8cadd1f into apache:main Aug 8, 2023
@Hzfengsy Hzfengsy deleted the simplify_arith branch November 5, 2023 09:39
MasterJH5574 added a commit to MasterJH5574/tvm that referenced this pull request Mar 12, 2024
PR apache#15471 enhances the simplification for LE, while missed a case
where the upper bound `kPosInf` is divisible by a factor. Therefore,
prior to this PR, when simplifying `x * 1024 + y < z * 7168`, it will
fails with the error message
```
InternalError: Check failed: value < 1LL << (dtype.bits() - 1)
(8589934591 vs. 2147483648) : ValueError: Literal value 8589934591
exceeds maximum of int32
```

This is just because the upper bound 7 here divides `kPosInf` the
maximum value of int64, which passes an "if" condition in apache#15471
unexpectedly.

This PR fixes the issue.
MasterJH5574 added a commit to MasterJH5574/tvm that referenced this pull request Mar 12, 2024
PR apache#15471 enhances the simplification for LE, while missed a case
where the upper bound `kPosInf` is divisible by a factor. Therefore,
prior to this PR, when simplifying `x * 1024 + y < z * 7168`, it will
fails with the error message
```
InternalError: Check failed: value < 1LL << (dtype.bits() - 1)
(8589934591 vs. 2147483648) : ValueError: Literal value 8589934591
exceeds maximum of int32
```

This is just because the upper bound 7 here divides `kPosInf` the
maximum value of int64, which passes an "if" condition in apache#15471
unexpectedly.

This PR fixes the issue.
MasterJH5574 added a commit to MasterJH5574/tvm that referenced this pull request Mar 12, 2024
PR apache#15471 enhances the simplification for LE, while missed a case
where the upper bound `kPosInf` is divisible by a factor. Therefore,
prior to this PR, when simplifying `x * 1024 + y < z * 7168`, it will
fails with the error message
```
InternalError: Check failed: value < 1LL << (dtype.bits() - 1)
(8589934591 vs. 2147483648) : ValueError: Literal value 8589934591
exceeds maximum of int32
```

This is just because the upper bound 7 here divides `kPosInf` the
maximum value of int64, which passes an "if" condition in apache#15471
unexpectedly.

This PR fixes the issue.
tqchen pushed a commit that referenced this pull request Mar 12, 2024
PR #15471 enhances the simplification for LE, while missed a case
where the upper bound `kPosInf` is divisible by a factor. Therefore,
prior to this PR, when simplifying `x * 1024 + y < z * 7168`, it will
fails with the error message
```
InternalError: Check failed: value < 1LL << (dtype.bits() - 1)
(8589934591 vs. 2147483648) : ValueError: Literal value 8589934591
exceeds maximum of int32
```

This is just because the upper bound 7 here divides `kPosInf` the
maximum value of int64, which passes an "if" condition in #15471
unexpectedly.

This PR fixes the issue.
thaisacs pushed a commit to thaisacs/tvm that referenced this pull request Apr 3, 2024
PR apache#15471 enhances the simplification for LE, while missed a case
where the upper bound `kPosInf` is divisible by a factor. Therefore,
prior to this PR, when simplifying `x * 1024 + y < z * 7168`, it will
fails with the error message
```
InternalError: Check failed: value < 1LL << (dtype.bits() - 1)
(8589934591 vs. 2147483648) : ValueError: Literal value 8589934591
exceeds maximum of int32
```

This is just because the upper bound 7 here divides `kPosInf` the
maximum value of int64, which passes an "if" condition in apache#15471
unexpectedly.

This PR fixes the issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants