-
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] Handle LetStmt for var1 = var2
expressions
#14320
[Bugfix][TVMScript] Handle LetStmt for var1 = var2
expressions
#14320
Conversation
Usually, when using TVMScript to represent a `PrimFunc` variable definition `var_name = expr` defines `LetStmt` with a variable named `var_name` bound to the expression `expr`. However, prior to this commit, if `expr` is a `tir::Var`, the TVMScript parser would instead silently omit the `LetStmt`, and rename all instances of that variable to `var_name`. The root cause was in the `VarTable.exist` check, which erroneously returned False in all cases. This was due to a `value is v` check, which checked if the value was the same as the stack of maybe-shadowing values that share the same name. Replacing the 'value is v` check with a `value in v` check resolves this issue. This bug dates to the initial implementation of the new TVMScript parser in apache#12496.
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 |
1 similar 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 by @-ing them in a comment.
Generated by tvm-bot |
@cyx-6 would you love to take a look? I very vaguely remember that the behavior was due to some backward compatibility issue that we have to support |
(This had an interesting interaction with #14318, because the accidental constant-folding could result in |
Hi @Lunderberg, do you have a reproducible TVMScript code to illustrate the bug you are fixing? That will be helpful for us to get the point. |
In this case, perhaps we should instead do |
@junrushao Thank you, and I can work around the issue by using an explicit @cyx-6 Certainly. The example below shows the weird interaction between the dynamic parameter and the variable name, which is what led to my initial investigation. import tvm
from tvm.script import tir as T
@pytest.mark.parametrize("split_factor", [1, 2, 4])
def test_function(split_factor):
@T.prim_func
def explicit_tir_node(A: T.Buffer(16, "int32"), B: T.Buffer(16, "int32")):
for i in T.serial(T.FloorDiv(16, split_factor)):
chunk_start = T.Mul(i, split_factor)
for j in T.vectorized(split_factor):
B[chunk_start + j] = A[chunk_start + j]
@T.prim_func
def using_operator(A: T.Buffer(16, "int32"), B: T.Buffer(16, "int32")):
for i in T.serial(16 // split_factor):
chunk_start = i * split_factor
for j in T.vectorized(split_factor):
B[chunk_start + j] = A[chunk_start + j]
using_operator.show()
tvm.ir.assert_structural_equal(explicit_tir_node, using_operator) This passes when the dynamic parameter @T.prim_func
def main(A: T.Buffer((16,), "int32"), B: T.Buffer((16,), "int32")):
for chunk_start in range(16):
for j in T.vectorized(1):
B[chunk_start + j] = A[chunk_start + j] |
Fixed a couple of CI errors, re-running to see what occurs next.
(I also opened #14399, to improve an error message encountered while debugging (2).) |
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! Thanks for the patch!
…che#14320) * [Bugfix][TVMScript] Handle LetStmt for `var1 = var2` expressions Usually, when using TVMScript to represent a `PrimFunc` variable definition `var_name = expr` defines `LetStmt` with a variable named `var_name` bound to the expression `expr`. However, prior to this commit, if `expr` is a `tir::Var`, the TVMScript parser would instead silently omit the `LetStmt`, and rename all instances of that variable to `var_name`. The root cause was in the `VarTable.exist` check, which erroneously returned False in all cases. This was due to a `value is v` check, which checked if the value was the same as the stack of maybe-shadowing values that share the same name. Replacing the 'value is v` check with a `value in v` check resolves this issue. This bug dates to the initial implementation of the new TVMScript parser in apache#12496. * Avoid implicit `PrimExpr.__bool__` from `if value in value_stack` * Use T.meta_var where variable renaming is required.
…che#14320) * [Bugfix][TVMScript] Handle LetStmt for `var1 = var2` expressions Usually, when using TVMScript to represent a `PrimFunc` variable definition `var_name = expr` defines `LetStmt` with a variable named `var_name` bound to the expression `expr`. However, prior to this commit, if `expr` is a `tir::Var`, the TVMScript parser would instead silently omit the `LetStmt`, and rename all instances of that variable to `var_name`. The root cause was in the `VarTable.exist` check, which erroneously returned False in all cases. This was due to a `value is v` check, which checked if the value was the same as the stack of maybe-shadowing values that share the same name. Replacing the 'value is v` check with a `value in v` check resolves this issue. This bug dates to the initial implementation of the new TVMScript parser in apache#12496. * Avoid implicit `PrimExpr.__bool__` from `if value in value_stack` * Use T.meta_var where variable renaming is required.
Prior to this commit, the `bind_assign_value` implementation for TIR would treat assignment of constants (e.g. `j = 42`) as meta-variables to be inserted at their point of use. This commit updates the parsing to treat `j = 42` as a shorthand for the TIR `LetStmt`, similar to the update made in apache#14320. This behavior is more consistent with the other uses of `bind_assign_value` as assignment, with `j = T.meta_var(42)` used to represent meta-variables.
* [TVMScript] Preserve LetStmt of constants Prior to this commit, the `bind_assign_value` implementation for TIR would treat assignment of constants (e.g. `j = 42`) as meta-variables to be inserted at their point of use. This commit updates the parsing to treat `j = 42` as a shorthand for the TIR `LetStmt`, similar to the update made in #14320. This behavior is more consistent with the other uses of `bind_assign_value` as assignment, with `j = T.meta_var(42)` used to represent meta-variables. * Updated tests that relied on implicit meta_var
Usually, when using TVMScript to represent a
PrimFunc
variable definitionvar_name = expr
definesLetStmt
with a variable namedvar_name
bound to the expressionexpr
. However, prior to this commit, ifexpr
is atir::Var
, the TVMScript parser would instead silently omit theLetStmt
, and rename all instances of that variable tovar_name
.The root cause was in the
VarTable.exist
check, which erroneously returned False in all cases. This was due to avalue is v
check, which checked if the value was the same as the stack of maybe-shadowing values that share the same name. Replacing the 'value is vcheck with a
value in v` check resolves this issue.This bug dates to the initial implementation of the new TVMScript parser in #12496.