From 8f3d85bdce37558b2a19359add7cf849702688da Mon Sep 17 00:00:00 2001 From: archibate <1931127624@qq.com> Date: Mon, 29 Jun 2020 10:10:39 +0800 Subject: [PATCH 1/4] Revert "[skip ci] revert ti.chain_compare" This reverts commit d1a356e7126720c1a357bfc30ff16c6844b9462b. --- python/taichi/lang/impl.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/python/taichi/lang/impl.py b/python/taichi/lang/impl.py index a8884e9bdbff1..39babe2f9a74e 100644 --- a/python/taichi/lang/impl.py +++ b/python/taichi/lang/impl.py @@ -102,13 +102,10 @@ def subscript(value, *indices): def chain_compare(comparators, ops): assert len(comparators) == len(ops) + 1, \ f'Chain comparison invoked with {len(comparators)} comparators but {len(ops)} operators' - evaluated_comparators = [] - for i in range(len(comparators)): - evaluated_comparators += [expr_init(comparators[i])] - ret = expr_init(True) + ret = True for i in range(len(ops)): - lhs = evaluated_comparators[i] - rhs = evaluated_comparators[i + 1] + lhs = comparators[i] + rhs = comparators[i + 1] if ops[i] == 'Lt': now = lhs < rhs elif ops[i] == 'LtE': @@ -123,7 +120,7 @@ def chain_compare(comparators, ops): now = lhs != rhs else: assert False, f'Unknown operator {ops[i]}' - ret = ret.logical_and(now) + ret = logical_and(ret, now) return ret From 5fbcf165022fd0d1631406ef2e478063ccb3325a Mon Sep 17 00:00:00 2001 From: archibate <1931127624@qq.com> Date: Thu, 2 Jul 2020 13:10:49 +0800 Subject: [PATCH 2/4] add func eval twice test according to @yuanming-hu --- tests/python/test_compare.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/python/test_compare.py b/tests/python/test_compare.py index 61726291e5b5b..616d1fdea8395 100644 --- a/tests/python/test_compare.py +++ b/tests/python/test_compare.py @@ -97,6 +97,27 @@ def func(): assert a[2] # ti.append returns 0 +@ti.all_archs +def test_no_duplicate_eval_func(): + a = ti.var(ti.i32, ()) + b = ti.var(ti.i32, ()) + + @ti.func + def why_this_foo_fail(n): + return ti.atomic_add(b[None], n) + + def foo(n): + return ti.atomic_add(ti.subscript(b, None), n) + + @ti.kernel + def func(): + a[None] = 0 <= foo(2) < 1 + + func() + assert a[None] == 1 + assert b[None] == 2 + + @ti.require(ti.extension.sparse) @ti.all_archs def test_chain_compare(): From 65ad3c5e2194f1295fb0079d9b0da54e1ae941d6 Mon Sep 17 00:00:00 2001 From: archibate <1931127624@qq.com> Date: Thu, 2 Jul 2020 13:14:59 +0800 Subject: [PATCH 3/4] fix https://github.com/taichi-dev/taichi/pull/1355#discussion_r448752644 --- taichi/ir/expr.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/taichi/ir/expr.cpp b/taichi/ir/expr.cpp index 20ea0cd594fbe..62138066fa98f 100644 --- a/taichi/ir/expr.cpp +++ b/taichi/ir/expr.cpp @@ -181,7 +181,7 @@ Expr ptr_if_global(const Expr &var) { // singleton global variable TI_ASSERT_INFO( var.snode()->num_active_indices == 0, - "Please always use 'x[None]' (instead of simply 'x') to access any 0-D tensor." + "Please always use 'x[None]' (instead of simply 'x') to access any 0-D tensor."); return var[ExprGroup()]; } else { // may be any local or global expr From 1e233e9097cf788d73a96dd2cc1afdc542676adb Mon Sep 17 00:00:00 2001 From: Taichi Gardener Date: Thu, 2 Jul 2020 01:15:31 -0400 Subject: [PATCH 4/4] [skip ci] enforce code format --- taichi/ir/expr.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/taichi/ir/expr.cpp b/taichi/ir/expr.cpp index 62138066fa98f..552e029bcf6d4 100644 --- a/taichi/ir/expr.cpp +++ b/taichi/ir/expr.cpp @@ -179,9 +179,9 @@ Expr load(const Expr &ptr) { Expr ptr_if_global(const Expr &var) { if (var.is()) { // singleton global variable - TI_ASSERT_INFO( - var.snode()->num_active_indices == 0, - "Please always use 'x[None]' (instead of simply 'x') to access any 0-D tensor."); + TI_ASSERT_INFO(var.snode()->num_active_indices == 0, + "Please always use 'x[None]' (instead of simply 'x') to " + "access any 0-D tensor."); return var[ExprGroup()]; } else { // may be any local or global expr