yzhliu commented on a change in pull request #5618:
URL: https://github.com/apache/incubator-tvm/pull/5618#discussion_r445181151



##########
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:
       I think you're right. It is something wrong with the testing function 
and compilation flow.
   during the testing & compilation, simplification is applied and seems to 
mistakenly transform floordiv to truncdiv, here is one example,
   
   ```python
   >>> import tvm
   >>> from tvm import te
   >>> from tvm.tir import floordiv
   >>> ana = tvm.arith.Analyzer()
   >>> x0, x1 = te.var("x0"), te.var("x1")
   >>> expr = (((x0 - floordiv((0 - (x1*5)), 2)) - 1) + floordiv((37 + ((x1 + 
7)*-5)), 2))
   >>> expr_sim = ana.simplify(expr)
   >>> expr_sim
   ((floordiv((0 - (x1*5)), 2) + x0) - ((x1*-5)/2))
   >>> ana.simplify(tvm.tir.stmt_functor.substitute(expr, {x0: 1, x1: 3}))
   1
   >>> ana.simplify(tvm.tir.stmt_functor.substitute(expr_sim, {x0: 1, x1: 3}))
   0  # <- showing inconsistent results between expr and expr_sim
   ```
   
   I can confirm this is the part of logic in canonical simplification 
https://github.com/apache/incubator-tvm/blob/master/src/arith/canonical_simplify.cc#L757
   
   @tqchen do you have any idea about it?




----------------------------------------------------------------
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]


Reply via email to