From 6571ecf495c48a25385942e2267d115fa84ed294 Mon Sep 17 00:00:00 2001 From: lin-hitonami Date: Mon, 31 Oct 2022 11:21:45 +0800 Subject: [PATCH 1/4] [Error] Return NotImplemented for operations between field and Expr/Matrix/Struct --- python/taichi/lang/ops.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/python/taichi/lang/ops.py b/python/taichi/lang/ops.py index ecb4b1310003d..82d5df459faff 100644 --- a/python/taichi/lang/ops.py +++ b/python/taichi/lang/ops.py @@ -7,6 +7,7 @@ from taichi.lang import expr, impl from taichi.lang.exception import TaichiSyntaxError from taichi.lang.util import cook_dtype, is_taichi_class, taichi_scope +from taichi.lang.field import Field unary_ops = [] @@ -51,6 +52,8 @@ def rev_foo(x, y): @functools.wraps(foo) def wrapped(a, b): + if isinstance(a, Field) or isinstance(b, Field): + return NotImplemented if is_taichi_class(a): return a._element_wise_binary(imp_foo, b) if is_taichi_class(b): @@ -79,6 +82,8 @@ def cab_foo(c, a, b): @functools.wraps(foo) def wrapped(a, b, c): + if isinstance(a, Field) or isinstance(b, Field) or isinstance(c, Field): + return NotImplemented if is_taichi_class(a): return a._element_wise_ternary(abc_foo, b, c) if is_taichi_class(b): @@ -101,6 +106,8 @@ def imp_foo(x, y): @functools.wraps(foo) def wrapped(a, b): + if isinstance(a, Field) or isinstance(b, Field): + return NotImplemented if is_taichi_class(a): return a._element_wise_writeback_binary(imp_foo, b) if is_taichi_class(b): From 683be6f5b1c6db9bf9587af33805a38e579d378c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 31 Oct 2022 03:24:08 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- python/taichi/lang/ops.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/taichi/lang/ops.py b/python/taichi/lang/ops.py index 82d5df459faff..1265a815766c8 100644 --- a/python/taichi/lang/ops.py +++ b/python/taichi/lang/ops.py @@ -6,8 +6,8 @@ from taichi._lib import core as _ti_core from taichi.lang import expr, impl from taichi.lang.exception import TaichiSyntaxError -from taichi.lang.util import cook_dtype, is_taichi_class, taichi_scope from taichi.lang.field import Field +from taichi.lang.util import cook_dtype, is_taichi_class, taichi_scope unary_ops = [] @@ -82,7 +82,8 @@ def cab_foo(c, a, b): @functools.wraps(foo) def wrapped(a, b, c): - if isinstance(a, Field) or isinstance(b, Field) or isinstance(c, Field): + if isinstance(a, Field) or isinstance(b, Field) or isinstance( + c, Field): return NotImplemented if is_taichi_class(a): return a._element_wise_ternary(abc_foo, b, c) From 7183160b399767b16bffec10ef2ad88e2dff9def Mon Sep 17 00:00:00 2001 From: lin-hitonami Date: Mon, 31 Oct 2022 14:21:17 +0800 Subject: [PATCH 3/4] add test --- python/taichi/lang/ast/ast_transformer.py | 2 +- tests/python/test_field.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/python/taichi/lang/ast/ast_transformer.py b/python/taichi/lang/ast/ast_transformer.py index 681f28225c4d5..7b9418a02ea57 100644 --- a/python/taichi/lang/ast/ast_transformer.py +++ b/python/taichi/lang/ast/ast_transformer.py @@ -806,7 +806,7 @@ def build_BinOp(ctx, node): try: node.ptr = op(node.left.ptr, node.right.ptr) except TypeError as e: - raise TaichiTypeError(str(e)) + raise TaichiTypeError(str(e)) from None return node.ptr @staticmethod diff --git a/tests/python/test_field.py b/tests/python/test_field.py index d8e934329e686..44e787a1c7512 100644 --- a/tests/python/test_field.py +++ b/tests/python/test_field.py @@ -311,3 +311,14 @@ def test_python_for_in(): match="Struct for is only available in Taichi scope"): for i in x: pass + + +@test_utils.test() +def test_matrix_mult_field(): + x = ti.field(int, shape=()) + with pytest.raises(ti.TaichiTypeError, match="unsupported operand type"): + @ti.kernel + def foo(): + a = ti.Vector([1, 1, 1]) + b = a * x + foo() \ No newline at end of file From 49be1abd67e12b6bbd6e0e50cdcaa6c178258559 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 31 Oct 2022 06:22:29 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/python/test_field.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/python/test_field.py b/tests/python/test_field.py index 44e787a1c7512..d502c7621bb51 100644 --- a/tests/python/test_field.py +++ b/tests/python/test_field.py @@ -317,8 +317,10 @@ def test_python_for_in(): def test_matrix_mult_field(): x = ti.field(int, shape=()) with pytest.raises(ti.TaichiTypeError, match="unsupported operand type"): + @ti.kernel def foo(): a = ti.Vector([1, 1, 1]) b = a * x - foo() \ No newline at end of file + + foo()