tvm-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-tvm] sergei-grechanik commented on a change in pull request #5171: [Arith] linear system and equation solver
Date Wed, 08 Apr 2020 01:21:20 GMT
sergei-grechanik commented on a change in pull request #5171: [Arith] linear system and equation
solver
URL: https://github.com/apache/incubator-tvm/pull/5171#discussion_r405204662
 
 

 ##########
 File path: tests/python/unittest/test_arith_solve_linear_system.py
 ##########
 @@ -14,9 +14,130 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
+import random
+import numpy as np
 import tvm
-from tvm import te, arith
-from tvm.tir import ir_pass
+from tvm import te, arith, ir, tir
+
+
+def run_expr(expr, vranges):
+    def _compute_body(*us):
+        vmap = {v: u + r.min for (v, r), u in zip(vranges.items(), us)}
+        return tir.ir_pass.Substitute(expr, vmap)
+
+    A = te.compute([r.extent.value for v, r in vranges.items()], _compute_body)
+    args = [tvm.nd.empty(A.shape, A.dtype)]
+    sch = te.create_schedule(A.op)
+    mod = tvm.build(sch, [A])
+    mod(*args)
+    return args[0].asnumpy()
+
+
+def check_bruteforce(bool_expr, vranges, cond=None):
+    if cond is not None:
+        bool_expr = te.any(tir.Not(cond), bool_expr)
+
+    res = run_expr(bool_expr, vranges)
+    if not np.all(res):
+        indices = list(np.argwhere(res == 0)[0])
+        counterex = [(str(v), i + r.min) for (v, r), i in zip(vranges.items(), indices)]
+        counterex = sorted(counterex, key=lambda x: x[0])
+        counterex = ", ".join([v + " = " + str(i) for v, i in counterex])
+        raise AssertionError("Expression {}\nis not true on {}\n"
+                             "Counterexample: {}"
+                             .format(tir.ir_pass.CanonicalSimplify(bool_expr), vranges, counterex))
+
+
+def check_solution(solution, vranges={}):
+    def _check_forward(formula1, formula2, varmap, backvarmap):
+        all_vranges = vranges.copy()
+        all_vranges.update({v: r for v, r in formula1.ranges.items()})
+
+        # Check that the transformation is injective
+        cond_on_vars = tir.const(1, 'bool')
+        for v in formula1.variables:
+            # variable mapping is consistent
+            v_back = tir.ir_pass.Simplify(tir.ir_pass.Substitute(varmap[v], backvarmap))
+            cond_on_vars = te.all(cond_on_vars, v == v_back)
+        # Also we have to check that the new relations are true when old relations are true
+        cond_subst = tir.ir_pass.Substitute(
+            te.all(tir.const(1, 'bool'), *formula2.relations), backvarmap)
+        # We have to include relations from vranges too
+        for v in formula2.variables:
+            if v in formula2.ranges:
+                r = formula2.ranges[v]
+                range_cond = te.all(v >= r.min, v < r.min + r.extent)
+                range_cond = tir.ir_pass.Substitute(range_cond, backvarmap)
+                cond_subst = te.all(cond_subst, range_cond)
+        cond_subst = tir.ir_pass.Simplify(cond_subst)
+        check_bruteforce(te.all(cond_subst, cond_on_vars), all_vranges,
+                         cond=te.all(tir.const(1, 'bool'), *formula1.relations))
+
+    rels = solution.dst.relations
+    if len(rels) == 1 and ir.structural_equal(rels[0], False):
+        # not solvable, skip
+        return
+    _check_forward(solution.src, solution.dst,
+                   solution.src_to_dst, solution.dst_to_src)
+    _check_forward(solution.dst, solution.src,
+                   solution.dst_to_src, solution.src_to_dst)
+
+
+def test_solution_consistency():
+    random.seed(0)
 
 Review comment:
   Personally, I think it is a good idea. I'm just worried that people won't know what to
do with such failure and will just complain about flaky tests instead of reporting the unlucky
seed. Maybe print some message like "this test is intentionally nondeterministic, if it fails
please report it together with this seed"?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message