From f0de80dcb301dbb963f8020e3815eb77bc78347d Mon Sep 17 00:00:00 2001 From: Jian Weng Date: Mon, 10 Dec 2018 23:24:41 -0800 Subject: [PATCH 1/4] fix in-correct value index in hybrid script --- python/tvm/hybrid/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/hybrid/parser.py b/python/tvm/hybrid/parser.py index 539115d8b6f4..c05c8c879d1a 100644 --- a/python/tvm/hybrid/parser.py +++ b/python/tvm/hybrid/parser.py @@ -237,7 +237,7 @@ def visit_Subscript(self, node): if isinstance(node.value, ast.Name): array = node.value.id _buf = self._get_buffer_from_id(array) - return _make.Call(_buf.dtype, array, args, _expr.Call.Halide, _buf.op, 0) + return _make.Call(_buf.dtype, array, args, _expr.Call.Halide, _buf.op, _buf.value_index) _internal_assert(isinstance(node.value, ast.Attribute), \ "Only variable and attribute's subscript supported so far") From ced23bd256938ac80fd1b4ae002cb4a4010ff6ba Mon Sep 17 00:00:00 2001 From: Jian Weng Date: Wed, 12 Dec 2018 09:49:46 -0800 Subject: [PATCH 2/4] add a test case of value index --- tests/python/unittest/test_hybrid_script.py | 48 +++++++++++++++++++-- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/tests/python/unittest/test_hybrid_script.py b/tests/python/unittest/test_hybrid_script.py index c718fc66899a..e50c2c51e12e 100644 --- a/tests/python/unittest/test_hybrid_script.py +++ b/tests/python/unittest/test_hybrid_script.py @@ -1,4 +1,4 @@ -import tvm, inspect, sys, traceback, numpy, nose +import tvm, inspect, sys, traceback, numpy, nose, types from tvm.hybrid import script from tvm.hybrid.intrin import HYBRID_GLOBALS @@ -11,6 +11,10 @@ def tvm_val_2_py_val(val): return val.value ctx = tvm.context(target, 0) + op = None + + outs = func(*args) + op = outs[0].op if isinstance(outs, list) else outs.op emu_args = [] nd_args = [] @@ -24,8 +28,6 @@ def tvm_val_2_py_val(val): emu_args.append(tvm_val_2_py_val(i)) nd_args.append(emu_args[-1]) - outs = func(*args) - op = outs[0].op if isinstance(outs, list) else outs.op sch = tvm.create_schedule(op) module = tvm.build(sch, args + (outs if isinstance(outs, list) else [outs]), target=target) assert module @@ -425,10 +427,12 @@ def downstream(a): for i in range(20): b[i] = a[i] * i return b + a = tvm.placeholder((20, ), 'float32') b = downstream(a) c = tvm.compute((20, ), lambda x: b[x] + 1.0) + sch = tvm.create_schedule(c.op) module = tvm.build(sch, [a, c]) assert module @@ -469,6 +473,40 @@ def add_something(a, b): tvm.testing.assert_allclose(nd_c.asnumpy(), ref, 1e-5, 1e-5) +def test_value_index(): + @tvm.hybrid.script + def kernel_a(a): + b = output_tensor((16, ), 'int32') + c = output_tensor((4, 4), 'int32') + for i in range(16): + b[i] = a[i] + 1 + c[i / 4, i % 4] = a[i] + 1 + return b, c + + @tvm.hybrid.script + def kernel_b(b, a): + c = output_tensor((4, 4), 'int32') + for i in range(4): + for j in range(4): + c[i, j] = a[i * 4 + j] * b[i, j] + return c + + a = tvm.placeholder((16, ), 'int32') + b, c = kernel_a(a) + d = kernel_b(c, b) + sch = tvm.create_schedule(d.op) + module = tvm.build(sch, [a, d]) + assert module + + np_a = numpy.arange(16).astype('int32') + np_b, np_c = kernel_a(np_a) + ref = kernel_b(np_c, np_b) + + res = tvm.ndarray.array(numpy.zeros((4, 4)).astype('int32')) + module(tvm.ndarray.array(np_a), res) + tvm.testing.assert_allclose(res.asnumpy(), ref) + + if __name__ == "__main__": test_outer_product() @@ -479,9 +517,11 @@ def add_something(a, b): test_math_intrin() test_non_zero() test_allocate() - #test_inplace() test_upstream() test_downstream() test_const_param() + test_value_index() + # TODO: + # test_inplace() From e17f9c868beaa4d0a2406a9c467da98d6663d65e Mon Sep 17 00:00:00 2001 From: Jian Weng Date: Wed, 12 Dec 2018 11:46:22 -0800 Subject: [PATCH 3/4] fix python2/3 incompat --- tests/python/unittest/test_hybrid_script.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/python/unittest/test_hybrid_script.py b/tests/python/unittest/test_hybrid_script.py index e50c2c51e12e..efcb1368b05e 100644 --- a/tests/python/unittest/test_hybrid_script.py +++ b/tests/python/unittest/test_hybrid_script.py @@ -479,8 +479,8 @@ def kernel_a(a): b = output_tensor((16, ), 'int32') c = output_tensor((4, 4), 'int32') for i in range(16): - b[i] = a[i] + 1 - c[i / 4, i % 4] = a[i] + 1 + b[i] = a[i] + 2 + c[i // 4, i % 4] = a[i] + 1 return b, c @tvm.hybrid.script @@ -496,6 +496,8 @@ def kernel_b(b, a): d = kernel_b(c, b) sch = tvm.create_schedule(d.op) module = tvm.build(sch, [a, d]) + # A bug to fix? + print(tvm.lower(sch, [a, b], simple_mode=True)) assert module np_a = numpy.arange(16).astype('int32') From 1b4f3a81b15389aa98107fa5f5b2b884bc1550a1 Mon Sep 17 00:00:00 2001 From: Jian Weng Date: Wed, 12 Dec 2018 13:13:54 -0800 Subject: [PATCH 4/4] fix floordiv --- python/tvm/hybrid/parser.py | 29 +++++++++++---------- tests/python/unittest/test_hybrid_script.py | 2 -- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/python/tvm/hybrid/parser.py b/python/tvm/hybrid/parser.py index c05c8c879d1a..ba10dd8dde3c 100644 --- a/python/tvm/hybrid/parser.py +++ b/python/tvm/hybrid/parser.py @@ -35,20 +35,21 @@ class HybridParser(ast.NodeVisitor): _binop_maker = { - ast.Add : operator.add, - ast.Sub : operator.sub, - ast.Mult : operator.mul, - ast.Div : operator.div if sys.version_info[0] == 2 else operator.truediv, - ast.Mod : operator.mod, - ast.BitOr : operator.or_, - ast.BitAnd: operator.and_, - ast.BitXor: operator.xor, - ast.Gt : operator.gt, - ast.GtE : operator.ge, - ast.Lt : operator.lt, - ast.LtE : operator.le, - ast.Eq : operator.eq, - ast.NotEq : operator.ne, + ast.Add : operator.add, + ast.Sub : operator.sub, + ast.Mult : operator.mul, + ast.Div : operator.div if sys.version_info[0] == 2 else operator.truediv, + ast.FloorDiv: operator.div if sys.version_info[0] == 2 else operator.truediv, + ast.Mod : operator.mod, + ast.BitOr : operator.or_, + ast.BitAnd : operator.and_, + ast.BitXor : operator.xor, + ast.Gt : operator.gt, + ast.GtE : operator.ge, + ast.Lt : operator.lt, + ast.LtE : operator.le, + ast.Eq : operator.eq, + ast.NotEq : operator.ne, ast.And : _all, ast.Or : _any, } diff --git a/tests/python/unittest/test_hybrid_script.py b/tests/python/unittest/test_hybrid_script.py index efcb1368b05e..7efbbe43ee21 100644 --- a/tests/python/unittest/test_hybrid_script.py +++ b/tests/python/unittest/test_hybrid_script.py @@ -496,8 +496,6 @@ def kernel_b(b, a): d = kernel_b(c, b) sch = tvm.create_schedule(d.op) module = tvm.build(sch, [a, d]) - # A bug to fix? - print(tvm.lower(sch, [a, b], simple_mode=True)) assert module np_a = numpy.arange(16).astype('int32')