You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/12/16 04:48:13 UTC

[GitHub] [tvm] lightzhan-intellif opened a new pull request, #13630: [BugFix][TVMScript] Parser crash

lightzhan-intellif opened a new pull request, #13630:
URL: https://github.com/apache/tvm/pull/13630

   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".


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] junrushao commented on pull request #13630: [BugFix][TVMScript] Parser crash

Posted by GitBox <gi...@apache.org>.
junrushao commented on PR #13630:
URL: https://github.com/apache/tvm/pull/13630#issuecomment-1356623111

   Thanks @lightzhan-intellif @slyubomirsky @Hzfengsy for the discussion!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] lightzhan-intellif commented on a diff in pull request #13630: [BugFix][TVMScript] Parser crash

Posted by GitBox <gi...@apache.org>.
lightzhan-intellif commented on code in PR #13630:
URL: https://github.com/apache/tvm/pull/13630#discussion_r1051304245


##########
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

Review Comment:
   Yea, I have updated the code.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] lightzhan-intellif commented on a diff in pull request #13630: [BugFix][TVMScript] Parser crash

Posted by GitBox <gi...@apache.org>.
lightzhan-intellif commented on code in PR #13630:
URL: https://github.com/apache/tvm/pull/13630#discussion_r1050386527


##########
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")

Review Comment:
   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.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] Hzfengsy commented on a diff in pull request #13630: [BugFix][TVMScript] Parser crash

Posted by GitBox <gi...@apache.org>.
Hzfengsy commented on code in PR #13630:
URL: https://github.com/apache/tvm/pull/13630#discussion_r1050371682


##########
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")

Review Comment:
   Can you move this line to line 49 (under the `test_different_dtype_assignment_to_var`)?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] tvm-bot commented on pull request #13630: [BugFix][TVMScript] Parser crash

Posted by GitBox <gi...@apache.org>.
tvm-bot commented on PR #13630:
URL: https://github.com/apache/tvm/pull/13630#issuecomment-1354201379

   <!---bot-comment-->
   
   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](https://github.com/apache/incubator-tvm/blob/master/CONTRIBUTORS.md#reviewers) by @-ing them in a comment.
   
   <!--bot-comment-ccs-start-->
    * No users to tag found in teams: `bugfix`, `tvmscript` <sub>See [#10317](https://github.com/apache/tvm/issues/10317) for details</sub><!--bot-comment-ccs-end-->
   
   <sub>Generated by [tvm-bot](https://github.com/apache/tvm/blob/main/ci/README.md#github-actions)</sub>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] lightzhan-intellif commented on a diff in pull request #13630: [BugFix][TVMScript] Parser crash

Posted by GitBox <gi...@apache.org>.
lightzhan-intellif commented on code in PR #13630:
URL: https://github.com/apache/tvm/pull/13630#discussion_r1050552367


##########
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

Review Comment:
   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.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] lightzhan-intellif commented on a diff in pull request #13630: [BugFix][TVMScript] Parser crash

Posted by GitBox <gi...@apache.org>.
lightzhan-intellif commented on code in PR #13630:
URL: https://github.com/apache/tvm/pull/13630#discussion_r1050552367


##########
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

Review Comment:
   Thanks for your suggestion. I have done some trials in the python terminal according to your concern. Let's have a look:
   ```shell
   # 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.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] slyubomirsky commented on a diff in pull request #13630: [BugFix][TVMScript] Parser crash

Posted by GitBox <gi...@apache.org>.
slyubomirsky commented on code in PR #13630:
URL: https://github.com/apache/tvm/pull/13630#discussion_r1051152722


##########
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

Review Comment:
   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



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] junrushao merged pull request #13630: [BugFix][TVMScript] Parser crash

Posted by GitBox <gi...@apache.org>.
junrushao merged PR #13630:
URL: https://github.com/apache/tvm/pull/13630


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] junrushao commented on a diff in pull request #13630: [BugFix][TVMScript] Parser crash

Posted by GitBox <gi...@apache.org>.
junrushao commented on code in PR #13630:
URL: https://github.com/apache/tvm/pull/13630#discussion_r1050377117


##########
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

Review Comment:
   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



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] junrushao commented on a diff in pull request #13630: [BugFix][TVMScript] Parser crash

Posted by GitBox <gi...@apache.org>.
junrushao commented on code in PR #13630:
URL: https://github.com/apache/tvm/pull/13630#discussion_r1051176207


##########
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

Review Comment:
   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 when `id(lhs) == id(rhs)`. It could depend on certain underlying implementation of the system, for example:
   
   ```python
   >>> 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 builtin `id()` method which is used in `is` 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 `==`.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] lightzhan-intellif commented on pull request #13630: [BugFix][TVMScript] Parser crash

Posted by GitBox <gi...@apache.org>.
lightzhan-intellif commented on PR #13630:
URL: https://github.com/apache/tvm/pull/13630#issuecomment-1356222805

   > LGTM! Please fix the lint and I'm happy to get it in
   
   Done


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org