From 5fec0d06959d89a8b7509bbd8ebf0c44938426c8 Mon Sep 17 00:00:00 2001 From: lightzhan-intellif Date: Fri, 16 Dec 2022 04:46:30 +0000 Subject: [PATCH 1/3] Fix the crash of parser when the old value of a var is an array but the new value is not. --- python/tvm/script/parser/core/parser.py | 2 +- .../python/unittest/test_tvmscript_regression.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/python/tvm/script/parser/core/parser.py b/python/tvm/script/parser/core/parser.py index c6d43f11cbf5..e628a46e3f86 100644 --- a/python/tvm/script/parser/core/parser.py +++ b/python/tvm/script/parser/core/parser.py @@ -150,7 +150,7 @@ def add(self, var: str, value: Any, allow_shadowing: bool = False): The options of whether variable shadowing allwed for this variable. """ # Skip if the key and value are equal to those in the var_table - if self.name2value[var] and self.name2value[var][-1] == value: + if self.name2value[var] and self.name2value[var][-1] is value: return if allow_shadowing and var in self.frames[-1].vars: # Shadowing diff --git a/tests/python/unittest/test_tvmscript_regression.py b/tests/python/unittest/test_tvmscript_regression.py index 3ad8090893eb..05c1665ea2a1 100644 --- a/tests/python/unittest/test_tvmscript_regression.py +++ b/tests/python/unittest/test_tvmscript_regression.py @@ -45,5 +45,20 @@ def test_multi_element_array_in_outmost_namespace(): tvm.ir.assert_structural_equal(func, rt_func) +def test_different_dtype_assignment_to_var(): + @T.prim_func + def test_case(): + a = T.alloc_buffer((10, 10), dtype="int8") + + @T.prim_func + def func_ref(): + a = T.alloc_buffer([10, 10], dtype="int8") + T.evaluate(0) + + tvm.ir.assert_structural_equal(test_case, func_ref) + + if __name__ == "__main__": + a = numpy.zeros((10, 10), dtype="int8") test_multi_element_array_in_outmost_namespace() + test_different_dtype_assignment_to_var() From 2de1380cfe29e608919d8a7197d5214d6c0749bd Mon Sep 17 00:00:00 2001 From: lightzhan-intellif Date: Sat, 17 Dec 2022 03:35:03 +0000 Subject: [PATCH 2/3] dispatch according to different types. --- python/tvm/script/parser/core/parser.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/python/tvm/script/parser/core/parser.py b/python/tvm/script/parser/core/parser.py index e628a46e3f86..5cdc858d70fb 100644 --- a/python/tvm/script/parser/core/parser.py +++ b/python/tvm/script/parser/core/parser.py @@ -17,6 +17,7 @@ """The core parser""" from collections import defaultdict +import numpy as np from contextlib import contextmanager from typing import Any, Callable, Dict, List, Optional, Set, Union from tvm._ffi.base import TVMError @@ -150,8 +151,11 @@ def add(self, var: str, value: Any, allow_shadowing: bool = False): The options of whether variable shadowing allwed for this variable. """ # Skip if the key and value are equal to those in the var_table - if self.name2value[var] and self.name2value[var][-1] is value: - return + if self.name2value[var] and type(self.name2value[var][-1]) == type(value): + if isinstance(value, np.ndarray) and (self.name2value[var][-1] == value).all(): + return + elif self.name2value[var][-1] == value: + return if allow_shadowing and var in self.frames[-1].vars: # Shadowing self.name2value[var][-1] = value From 57d900d52812a9104806fbefbc7bbaa78acb3261 Mon Sep 17 00:00:00 2001 From: lightzhan-intellif Date: Sat, 17 Dec 2022 07:45:57 +0000 Subject: [PATCH 3/3] fix the lint. --- python/tvm/script/parser/core/parser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/tvm/script/parser/core/parser.py b/python/tvm/script/parser/core/parser.py index 5cdc858d70fb..7c699c42aecb 100644 --- a/python/tvm/script/parser/core/parser.py +++ b/python/tvm/script/parser/core/parser.py @@ -17,9 +17,9 @@ """The core parser""" from collections import defaultdict -import numpy as np from contextlib import contextmanager from typing import Any, Callable, Dict, List, Optional, Set, Union +import numpy as np from tvm._ffi.base import TVMError from tvm.error import DiagnosticError @@ -151,7 +151,7 @@ def add(self, var: str, value: Any, allow_shadowing: bool = False): The options of whether variable shadowing allwed for this variable. """ # Skip if the key and value are equal to those in the var_table - if self.name2value[var] and type(self.name2value[var][-1]) == type(value): + if self.name2value[var] and isinstance(self.name2value[var][-1], type(value)): if isinstance(value, np.ndarray) and (self.name2value[var][-1] == value).all(): return elif self.name2value[var][-1] == value: