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



##########
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:
       Thanks for the suggestion. It does find some bugs in the implementation, 
most related to round division, which might also exist in your branch, here's 
one example, 
   ```python
   [(((x0 + (x1*2)) + 3) < ((x0 + (x1*-3)) + 1)),
   ((((x0*3) + (x1*-5)) + 1) >= ((x0*2) + -5)),
   ((((x0*-1) + (x1*-1)) + 2) <= ((x0 + (x1*4)) + 1))]
   ```
   if we allow non-divisible results, it is solved to be
   ```
   IntConstraintsTransform(
        IntConstraints([x0, x1], {x2: range(min=-20, ext=41), x1: 
range(min=-20, ext=41), x3: range(min=-20, ext=41), x0: range(min=-20, 
ext=41)}, [(((x0 + (x1*2)) + 3) < ((x0 + (x1*-3)) + 1)), ((((x0*3) + (x1*-5)) + 
1) >= ((x0*2) + -5)), ((((x0*-1) + (x1*-1)) + 2) <= ((x0 + (x1*4)) + 1))])
        IntConstraints([x0.shifted, x1.shifted], {x0.shifted: range(min=0, 
ext=18), x1.shifted: range(min=0, ext=7)}, [(36 <= (((x1.shifted*5) + 
(floordiv((37 - (x1.shifted*5)), 2)*2)) + (x0.shifted*2))), ((floordiv((37 - 
(x1.shifted*5)), 2) + x0.shifted) <= 20)])
        {x0: (x0.shifted + floordiv((37 + (x1.shifted*-5)), 2)), x1: 
(x1.shifted + -7)}
        {x0.shifted: ((x0 - floordiv((0 - (x1*5)), 2)) - 1), x1.shifted: (x1 + 
7)}
   )
   ```
   I modified the `FindBestRange` to skip non-divisible cases,
   ```cpp
   if (analyzer.CanProve(floormod(low, coef) == 0)) {
         // we need to be very careful with rounding
         // as it could be wrong when we have equations.
         // equations can come from
         // 1. when it is a single point, i.e., extent == 1.
         // 2. when var is substituted by another var in deskew range.
         best_lower = low_divided;
         best_diff_over = diff_over;
   }
   ```




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