sergei-grechanik commented on a change in pull request #5618: URL: https://github.com/apache/incubator-tvm/pull/5618#discussion_r443945936
########## File path: tests/python/unittest/test_arith_solve_linear_inequality.py ########## @@ -0,0 +1,166 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +import random +import sys +import pytest +import tvm +from tvm import te, arith, ir, tir, testing + + +def test_solve_system_of_inequalities(): + seed = random.randrange(sys.maxsize) + print("\nThis test is intentionally non-deterministic, " + "if it fails please report it in github issue together with this seed {}\n".format(seed)) + random.seed(seed) + + def _check(variables, formulas, coef=(-5, 5), bounds=(-20, 20)): + vs = [te.var("x" + str(i)) for i in range(variables)] + + fs = [] + for i in range(formulas): + s1 = sum([v*random.randint(coef[0], coef[1]) for v in vs]) + s1 += random.randint(coef[0], coef[1]) + s2 = sum([v*random.randint(coef[0], coef[1]) for v in vs]) + s2 += random.randint(coef[0], coef[1]) + op = random.choice([tir.expr.EQ, tir.expr.LE, tir.expr.LT, tir.expr.GE, tir.expr.GT]) + fs.append(op(s1, s2)) + + vranges = {v: tvm.ir.expr.Range(bounds[0], bounds[1] + 1) for v in vs} + before = te.all(tir.const(1, 'bool'), *fs) + after = arith._ffi_api.SolveInequalitiesAsCondition(vs, vranges, fs) + after = te.all(tir.const(1, 'bool'), *after) + testing.check_bool_expr_is_true(before == after, vranges) Review comment: `2x == 1` should be transformed to `x >= ceildiv(1, 2) = 1` and `x <= floordiv(1, 2) = 0` which gives the empty set, so I don't think that equations should be problematic. I checked out your branch and the bug I mentioned does seem to be fixed indeed. However your example results in the following failure (when the workaround for equations is off): ``` AssertionError: Expression ((((((x0: int32 + 1) - (x1: int32*3)) <= (((x1*2) + x0) + 3)) || ((((x0*3) + 1) - (x1*5)) < ((x0*2) - 5))) || ((((x1*4) + x0) + 1) < ((2 - x0) - x1))) || ((((1 <= ((x1*5) + (x0*2))) && (x0 <= 20)) && ((1 <= (x0 - ((x1*-5) / 2))) && ((x0 - ((x1*-5) / 2)) < 19))) && ((-7 <= x1) && (x1 < 0)))) is not true on {x1: range(min=-20, ext=41), x0: range(min=-20, ext=41)} Counterexample: x0: int32 = 18, x1: int32 = -7 ``` But the expression is true for the given values if I'm not mistaken. Could be a bug in the testing function or a miscompile. (BTW, I see that trunc division appeared during simplification which probably shouldn't happen. Although it might not be the cause of the bug, it still seems bad because truncated division is harder to deal with and may cause poor performance of some transformations) ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
