From 921cf68424f4a07f9db173165213f42ba48356c7 Mon Sep 17 00:00:00 2001 From: archibate <1931127624@qq.com> Date: Fri, 28 Aug 2020 16:39:34 +0800 Subject: [PATCH 1/6] Revert "[skip ci] revert" This reverts commit 10d85dbc99610c2200bccb4e88014a2bedcc1aa8. --- python/taichi/lang/ops.py | 1 + taichi/python/export_lang.cpp | 2 -- taichi/transforms/type_check.cpp | 4 +++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/python/taichi/lang/ops.py b/python/taichi/lang/ops.py index d4d36f9acda73..00e25bc1fcd01 100644 --- a/python/taichi/lang/ops.py +++ b/python/taichi/lang/ops.py @@ -300,6 +300,7 @@ def mul(a, b): @binary def mod(a, b): def expr_python_mod(a, b): + # a % b = (a // b) * b - a quotient = Expr(ti_core.expr_floordiv(a, b)) multiply = Expr(ti_core.expr_mul(b, quotient.ptr)) return ti_core.expr_sub(a, multiply.ptr) diff --git a/taichi/python/export_lang.cpp b/taichi/python/export_lang.cpp index 20deadcc5a7a7..e853e72bee8cc 100644 --- a/taichi/python/export_lang.cpp +++ b/taichi/python/export_lang.cpp @@ -575,11 +575,9 @@ void export_lang(py::module &m) { m.def("host_arch", host_arch); m.def("set_lib_dir", [&](const std::string &dir) { - TI_INFO("set_lib_dir: [{}]", dir); compiled_lib_dir = dir; }); m.def("set_tmp_dir", [&](const std::string &dir) { - TI_INFO("set_tmp_dir: [{}]", dir); runtime_tmp_dir = dir; }); m.def("get_runtime_dir", get_runtime_dir); diff --git a/taichi/transforms/type_check.cpp b/taichi/transforms/type_check.cpp index 3450a78f6d508..d29140c8d9cc8 100644 --- a/taichi/transforms/type_check.cpp +++ b/taichi/transforms/type_check.cpp @@ -40,7 +40,9 @@ class TypeCheck : public IRVisitor { void visit(IfStmt *if_stmt) { // TODO: use DataType::u1 when it's supported - TI_ASSERT(if_stmt->cond->ret_type.data_type == DataType::i32); + TI_ASSERT_INFO(if_stmt->cond->ret_type.data_type == DataType::i32, + "`if` conditions must be of type int32, consider using `if x != 0:` " + "instead of `if x:` for float values."); if (if_stmt->true_statements) if_stmt->true_statements->accept(this); if (if_stmt->false_statements) { From e54d773f8d2e0bc136e8bbb98bb2d20e92f3bbd7 Mon Sep 17 00:00:00 2001 From: archibate <1931127624@qq.com> Date: Fri, 28 Aug 2020 16:58:12 +0800 Subject: [PATCH 2/6] [Lang] Support printing list in Taichi-scope --- python/taichi/lang/impl.py | 18 +++++++++++++++--- tests/python/test_print.py | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/python/taichi/lang/impl.py b/python/taichi/lang/impl.py index 4e8528e265ddd..5e6b98e2193c9 100644 --- a/python/taichi/lang/impl.py +++ b/python/taichi/lang/impl.py @@ -407,14 +407,26 @@ def entry2content(var): else: return Expr(var).ptr + def list_ti_repr(var): + yield '[' # distinguishing tuple & list will increase maintainance cost + for i, v in enumerate(var): + if i: + yield ', ' + yield v + yield ']' + def vars2entries(vars): for var in vars: if hasattr(var, '__ti_repr__'): - repr = var.__ti_repr__() - for v in vars2entries(repr): - yield v + res = var.__ti_repr__() + elif isinstance(var, (list, tuple)): + res = list_ti_repr(var) else: yield var + continue + + for v in vars2entries(res): + yield v def add_separators(vars): for i, var in enumerate(vars): diff --git a/tests/python/test_print.py b/tests/python/test_print.py index 14e92808c92ab..d81d4d9462647 100644 --- a/tests/python/test_print.py +++ b/tests/python/test_print.py @@ -95,6 +95,26 @@ def func(k: ti.f32): ti.sync() +@ti.test() +def test_print_list(): + x = ti.Matrix.field(2, 3, dtype=ti.f32, shape=(2, 3)) + y = ti.Vector.field(3, dtype=ti.f32, shape=()) + + @ti.kernel + def func(k: ti.f32): + w = [k, x.shape] + print(w + [y.n]) # [233.3, 3] + print(x.shape) # [2, 3] + print(y.shape) # [] + z = (1, ) + print([1, k**2, k + 1]) # [1, 233.3, 234.3] + print(z) # [1] + print([]) # [] + + func(233.3) + ti.sync() + + @ti.test(arch=ti.cpu) def test_python_scope_print_field(): x = ti.Matrix.field(2, 3, dtype=ti.f32, shape=()) From d3993b081619c7536e5ae7579b251fe0dc4127ae Mon Sep 17 00:00:00 2001 From: archibate <1931127624@qq.com> Date: Fri, 28 Aug 2020 17:02:04 +0800 Subject: [PATCH 3/6] enhance test with Matrix nested list --- tests/python/test_print.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/python/test_print.py b/tests/python/test_print.py index d81d4d9462647..93d00874c6acf 100644 --- a/tests/python/test_print.py +++ b/tests/python/test_print.py @@ -109,6 +109,7 @@ def func(k: ti.f32): z = (1, ) print([1, k**2, k + 1]) # [1, 233.3, 234.3] print(z) # [1] + print([y[0], z]) # [[0, 0, 0], [1]] print([]) # [] func(233.3) From 162ec9e49cbcbd74b130284ffaba29727e9dfb66 Mon Sep 17 00:00:00 2001 From: Taichi Gardener Date: Fri, 28 Aug 2020 05:04:56 -0400 Subject: [PATCH 4/6] [skip ci] enforce code format --- taichi/python/export_lang.cpp | 8 ++------ taichi/transforms/type_check.cpp | 3 ++- tests/python/test_print.py | 2 +- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/taichi/python/export_lang.cpp b/taichi/python/export_lang.cpp index e853e72bee8cc..5a883ad56baa0 100644 --- a/taichi/python/export_lang.cpp +++ b/taichi/python/export_lang.cpp @@ -574,12 +574,8 @@ void export_lang(py::module &m) { m.def("host_arch", host_arch); - m.def("set_lib_dir", [&](const std::string &dir) { - compiled_lib_dir = dir; - }); - m.def("set_tmp_dir", [&](const std::string &dir) { - runtime_tmp_dir = dir; - }); + m.def("set_lib_dir", [&](const std::string &dir) { compiled_lib_dir = dir; }); + m.def("set_tmp_dir", [&](const std::string &dir) { runtime_tmp_dir = dir; }); m.def("get_runtime_dir", get_runtime_dir); m.def("get_commit_hash", get_commit_hash); diff --git a/taichi/transforms/type_check.cpp b/taichi/transforms/type_check.cpp index d29140c8d9cc8..9ae253c4ac685 100644 --- a/taichi/transforms/type_check.cpp +++ b/taichi/transforms/type_check.cpp @@ -40,7 +40,8 @@ class TypeCheck : public IRVisitor { void visit(IfStmt *if_stmt) { // TODO: use DataType::u1 when it's supported - TI_ASSERT_INFO(if_stmt->cond->ret_type.data_type == DataType::i32, + TI_ASSERT_INFO( + if_stmt->cond->ret_type.data_type == DataType::i32, "`if` conditions must be of type int32, consider using `if x != 0:` " "instead of `if x:` for float values."); if (if_stmt->true_statements) diff --git a/tests/python/test_print.py b/tests/python/test_print.py index 93d00874c6acf..42e1dced3977a 100644 --- a/tests/python/test_print.py +++ b/tests/python/test_print.py @@ -110,7 +110,7 @@ def func(k: ti.f32): print([1, k**2, k + 1]) # [1, 233.3, 234.3] print(z) # [1] print([y[0], z]) # [[0, 0, 0], [1]] - print([]) # [] + print([]) # [] func(233.3) ti.sync() From 773937f9967fc3d34c4c76ea32532a53f26d2edd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E4=BA=8E=E6=96=8C?= <1931127624@qq.com> Date: Fri, 28 Aug 2020 21:54:18 +0800 Subject: [PATCH 5/6] [skip ci] Update tests/python/test_print.py --- tests/python/test_print.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/test_print.py b/tests/python/test_print.py index 42e1dced3977a..96581e06d59b7 100644 --- a/tests/python/test_print.py +++ b/tests/python/test_print.py @@ -103,7 +103,7 @@ def test_print_list(): @ti.kernel def func(k: ti.f32): w = [k, x.shape] - print(w + [y.n]) # [233.3, 3] + print(w + [y.n]) # [233.3, [2, 3], 3] print(x.shape) # [2, 3] print(y.shape) # [] z = (1, ) From a176644bf5ea8ee4fe117d1c7c6e12b97430b9ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E4=BA=8E=E6=96=8C?= <1931127624@qq.com> Date: Fri, 28 Aug 2020 22:15:43 +0800 Subject: [PATCH 6/6] fix test --- tests/python/test_print.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/test_print.py b/tests/python/test_print.py index 96581e06d59b7..3a7d6061e606f 100644 --- a/tests/python/test_print.py +++ b/tests/python/test_print.py @@ -109,7 +109,7 @@ def func(k: ti.f32): z = (1, ) print([1, k**2, k + 1]) # [1, 233.3, 234.3] print(z) # [1] - print([y[0], z]) # [[0, 0, 0], [1]] + print([y[None], z]) # [[0, 0, 0], [1]] print([]) # [] func(233.3)