-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BugFix][TVMScript] Parser crash #13630
[BugFix][TVMScript] Parser crash #13630
Conversation
…he new value is not.
Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.
Generated by tvm-bot |
if __name__ == "__main__": | ||
a = numpy.zeros((10, 10), dtype="int8") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this line to line 49 (under the test_different_dtype_assignment_to_var
)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am very sorry that it can not in this case. If we do so, prim function can not capture the var 'a' because it is not a nonlocal variable of func test_case.
if self.name2value[var] and self.name2value[var][-1] is value: | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing out the issue! I believe either way might not be the most accurate, because it's possible that self.name2valuep[var]
is a python integer or so, which cannot be compared using is
. We might want to dispatch comparison according to different types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your suggestion. I have done some trials in the python terminal according to your concern. Let's have a look:
# python3
Python 3.8.13 (default, Apr 19 2022, 00:53:22)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 is 1
<stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
True
>>> a = 1
>>> a is 1
<stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
True
>>> a = 1
>>> b = 1
>>> a is b
True
>>> a = [1, 2, 3]
>>> b = 1
>>> a[0] is b
True
>>> b = [1, 2, 3]
>>> a [0] is b[1]
False
According to the above output, we can find that there will be a warning if we use literal directly, but here it is a variable/list/dict which in your concern contains a literal. It looks like that python differentiates literal from variables with literal
value. In our case, it belongs to the latter. So maybe no problem here with "is".
There might be some other scenarios I didn't cover, feel free to point out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the is
operator checks reference equality rather than value equality. For integers, it will just check equality, so @lightzhan-intellif is correct. Whether it's preferred style is another question
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not have particular opinion on which style we should go for, but just wanted to point out the implication of the switch:
is
checks referential/pointer equality, or in python's term, identity, where it returns True only whenid(lhs) == id(rhs)
. It could depend on certain underlying implementation of the system, for example:
>>> a = 257
>>> b = 257
>>> a is b
False
>>> a = 256
>>> b = 256
>>> a is b
True
==
checks equality that could be potentially overloaded, for TVM objects, it's using TVM's address comparison.same_as()
rather than python's builtinid()
method which is used inis
operator. However
The implication of switching from ==
to is
means that it bypasses TVM's .same_as()
method, which at the moment I am not quite certain is suitable for broad usecases.
Therefore, how about we do the following: if lhs
and rhs
are numpy arrays, then we use numpy-specific behavior (e.g. elementwise equality), but otherwise we still use ==
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, I have updated the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Please fix the lint and I'm happy to get it in
Done |
Thanks @lightzhan-intellif @slyubomirsky @Hzfengsy for the discussion! |
This PR tries to fix the crash of parser when the old value of a var is an array but the new value is not. For example: ```python from tvm.script import tir as T def func_wrapper(shape, dtype): @T.prim_func def test_case(): a = T.alloc_buffer(shape, dtype=dtype) return test_case if __name__ == "__main__": a = np.zeros((10, 10), dtype="int8") print(func_wrapper((256, 256), dtype="int8").script()) ``` In the above code, there are two assignment to var 'a'. In the global scope, its value is a numpy array. But it is a Buffer in the prim function. There is a table named 'name2value' to track the value of vars like 'a' here. When the parser wants to update its value, it will compare the value between the new and the old assignment. Here the problem comes. When we use '==' to compare an array with a value, the result is an array too, which can not be used as a condition of a if stmt directly. So, the code above will emit an error: ```shell error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() --> /workspace/code_newest/tvm/private_test/test_meta_programming.py:16:9 | 16 | a = T.alloc_buffer(shape, dtype=dtype) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` This PR fixes this by change "==" to "is". Co-authored-by: lightzhan-intellif <zhan.liang@intellif.com>
This PR tries to fix the crash of parser when the old value of a var is an array but the new value is not. For example: ```python from tvm.script import tir as T def func_wrapper(shape, dtype): @T.prim_func def test_case(): a = T.alloc_buffer(shape, dtype=dtype) return test_case if __name__ == "__main__": a = np.zeros((10, 10), dtype="int8") print(func_wrapper((256, 256), dtype="int8").script()) ``` In the above code, there are two assignment to var 'a'. In the global scope, its value is a numpy array. But it is a Buffer in the prim function. There is a table named 'name2value' to track the value of vars like 'a' here. When the parser wants to update its value, it will compare the value between the new and the old assignment. Here the problem comes. When we use '==' to compare an array with a value, the result is an array too, which can not be used as a condition of a if stmt directly. So, the code above will emit an error: ```shell error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() --> /workspace/code_newest/tvm/private_test/test_meta_programming.py:16:9 | 16 | a = T.alloc_buffer(shape, dtype=dtype) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` This PR fixes this by change "==" to "is". Co-authored-by: lightzhan-intellif <zhan.liang@intellif.com>
This PR tries to fix the crash of parser when the old value of a var is an array but the new value is not. For example:
In the above code, there are two assignment to var 'a'. In the global scope, its value is a numpy array. But it is a Buffer in the prim function. There is a table named 'name2value' to track the value of vars like 'a' here.
When the parser wants to update its value, it will compare the value between the new and the old assignment. Here the problem comes. When we use '==' to compare an array with a value, the result is an array too, which can not be used as a condition of a if stmt directly. So, the code above will emit an error:
This PR fixes this by change "==" to "is".