Skip to content

Conversation

@syang-ng
Copy link

@syang-ng syang-ng commented Sep 8, 2021

Bug Description

I found there will be a segfault when we try to use rewrite_simplify to simplify div expr whose dividend and divisor are Ramp and Broadcast respectively.

Here is the code to trigger the segmentation fault error.

import tvm

analyzer = tvm.arith.Analyzer()
analyzer.rewrite_simplify(tvm.tir.Div(tvm.tir.Ramp(1,1,2), tvm.tir.Broadcast(0, 2)))
(tvm-build) ➜  ~ python test_div_zero.py
[1]    44116 floating point exception (core dumped)  python test_div_zero.py

Bug Analysis

The root cause of this bug is that two numbers are divided directly in src/arith/rewrite_simplify.cc, Line 477.

image

Thus, I add a zero check in src/arith/rewrite_simplify.cc to prevent division by 0 in this commit. I also add a test case in tests/python/unittest/test_arith_rewrite_simplify.py to cover this problem.

# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import py
Copy link
Contributor

Choose a reason for hiding this comment

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

Not used?

Comment on lines 477 to 478
// If divisor is equal to zero
ICHECK(c2val != 0) << "division by zero";
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: The check itself is clear enough so we probably don't need the comment.

Suggested change
// If divisor is equal to zero
ICHECK(c2val != 0) << "division by zero";
ICHECK(c2val != 0) << "division by zero";

@masahi masahi merged commit e47fc6a into apache:main Sep 9, 2021
ylc pushed a commit to ylc/tvm that referenced this pull request Sep 29, 2021
* fix div zero error in rewrite_simplify

* update the style to fix ci error

* remove useless code and comment
ylc pushed a commit to ylc/tvm that referenced this pull request Jan 13, 2022
* fix div zero error in rewrite_simplify

* update the style to fix ci error

* remove useless code and comment
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.

4 participants