-
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
[Fix] Fix IndexDataTypeNormalizer to avoid redundant casting #13449
Merged
junrushao
merged 1 commit into
apache:main
from
MasterJH5574:bugfix/2022-11-20-index-dtype-normalizer
Nov 21, 2022
Merged
[Fix] Fix IndexDataTypeNormalizer to avoid redundant casting #13449
junrushao
merged 1 commit into
apache:main
from
MasterJH5574:bugfix/2022-11-20-index-dtype-normalizer
Nov 21, 2022
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
junrushao
reviewed
Nov 21, 2022
CC: @vinx13 for confirmation |
MasterJH5574
force-pushed
the
bugfix/2022-11-20-index-dtype-normalizer
branch
from
November 21, 2022 00:40
48044ce
to
711fd3c
Compare
vinx13
approved these changes
Nov 21, 2022
xinetzone
pushed a commit
to daobook/tvm
that referenced
this pull request
Nov 25, 2022
…13449) This PR fixes the behavior of IndexDataTypeNormalizer on CastNode. ## Background Consider the following case, ```python A = te.placeholder((tir.IntImm("int64", 2), tir.IntImm("int64", 4)), name="A") B = topi.reshape(A, (4, 2)) func = te.create_prim_func([A, B], index_dtype_override=None) ``` the generated PrimFunc is ```python @T.prim_func def func(A: T.Buffer[(T.int64(2), T.int64(4)), "float32"], T_reshape: T.Buffer[(4, 2), "float32"]): for i0, i1 in T.grid(4, 2): with T.block("T_reshape"): ax0, ax1 = T.axis.remap("SS", [i0, i1]) T.reads(A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)]) T.writes(T_reshape[ax0, ax1]) T_reshape[ax0, ax1] = A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)] ``` Here loop variables `ax0` and `ax1` have dtype int32, since the shape of the output buffer is in int32. Other other hand, the input buffer has shape in int64. So as the script above shows, CreatePrimFunc will cast the int32 variables to int64 first, and access the input buffer afterwards. Now if we use the option `index_dtype_override` to specify an index dtype as below, ```python func = te.create_prim_func([A, B], index_dtype_override="int64") ``` the generated function will be ```python @T.prim_func def func(A: T.Buffer[(T.int64(2), T.int64(4)), "float32"], T_reshape: T.Buffer[(T.int64(4), T.int64(2)), "float32"]): for i0, i1 in T.grid(T.int64(4), T.int64(2)): with T.block("T_reshape"): ax0, ax1 = T.axis.remap("SS", [i0, i1]) T.reads(A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)]) T.writes(T_reshape[ax0, ax1]) T_reshape[ax0, ax1] = A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)] ``` Note that though all variables and the buffer shapes have dtype int64, there are still CastNodes such as `T.Cast("int64", ax0)` when `ax0` is already an int64 variable. We don’t want such redundant casting. ## Fix To fix the issue above, this PR overrides the `VisitExpr_(const CastNode* cast)` method in IndexDataTypeNormalizer. When the `value` field of a CastNode already has the target dtype, we no longer cast it.
masahi
added a commit
to masahi/tvm
that referenced
this pull request
Nov 25, 2022
…pache#13449)" This reverts commit d663207.
masahi
added a commit
to masahi/tvm
that referenced
this pull request
Nov 25, 2022
…pache#13449)" This reverts commit d663207.
masahi
added a commit
to masahi/tvm
that referenced
this pull request
Nov 29, 2022
…pache#13449)" This reverts commit d663207.
masahi
added a commit
to masahi/tvm
that referenced
this pull request
Nov 29, 2022
…casting (apache#13449)"" This reverts commit fc76ea1.
masahi
added a commit
to masahi/tvm
that referenced
this pull request
Dec 1, 2022
…pache#13449)" This reverts commit d663207.
masahi
added a commit
to masahi/tvm
that referenced
this pull request
Dec 1, 2022
…casting (apache#13449)"" This reverts commit fc76ea1.
masahi
added a commit
to masahi/tvm
that referenced
this pull request
Dec 2, 2022
…pache#13449)" This reverts commit d663207.
masahi
added a commit
to masahi/tvm
that referenced
this pull request
Dec 2, 2022
…casting (apache#13449)"" This reverts commit fc76ea1.
MasterJH5574
added a commit
to mlc-ai/relax
that referenced
this pull request
Dec 24, 2022
…tvm#13449) This PR fixes the behavior of IndexDataTypeNormalizer on CastNode. Consider the following case, ```python A = te.placeholder((tir.IntImm("int64", 2), tir.IntImm("int64", 4)), name="A") B = topi.reshape(A, (4, 2)) func = te.create_prim_func([A, B], index_dtype_override=None) ``` the generated PrimFunc is ```python @T.prim_func def func(A: T.Buffer[(T.int64(2), T.int64(4)), "float32"], T_reshape: T.Buffer[(4, 2), "float32"]): for i0, i1 in T.grid(4, 2): with T.block("T_reshape"): ax0, ax1 = T.axis.remap("SS", [i0, i1]) T.reads(A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)]) T.writes(T_reshape[ax0, ax1]) T_reshape[ax0, ax1] = A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)] ``` Here loop variables `ax0` and `ax1` have dtype int32, since the shape of the output buffer is in int32. Other other hand, the input buffer has shape in int64. So as the script above shows, CreatePrimFunc will cast the int32 variables to int64 first, and access the input buffer afterwards. Now if we use the option `index_dtype_override` to specify an index dtype as below, ```python func = te.create_prim_func([A, B], index_dtype_override="int64") ``` the generated function will be ```python @T.prim_func def func(A: T.Buffer[(T.int64(2), T.int64(4)), "float32"], T_reshape: T.Buffer[(T.int64(4), T.int64(2)), "float32"]): for i0, i1 in T.grid(T.int64(4), T.int64(2)): with T.block("T_reshape"): ax0, ax1 = T.axis.remap("SS", [i0, i1]) T.reads(A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)]) T.writes(T_reshape[ax0, ax1]) T_reshape[ax0, ax1] = A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)] ``` Note that though all variables and the buffer shapes have dtype int64, there are still CastNodes such as `T.Cast("int64", ax0)` when `ax0` is already an int64 variable. We don’t want such redundant casting. To fix the issue above, this PR overrides the `VisitExpr_(const CastNode* cast)` method in IndexDataTypeNormalizer. When the `value` field of a CastNode already has the target dtype, we no longer cast it.
tqchen
pushed a commit
to mlc-ai/relax
that referenced
this pull request
Dec 30, 2022
…tvm#13449) This PR fixes the behavior of IndexDataTypeNormalizer on CastNode. Consider the following case, ```python A = te.placeholder((tir.IntImm("int64", 2), tir.IntImm("int64", 4)), name="A") B = topi.reshape(A, (4, 2)) func = te.create_prim_func([A, B], index_dtype_override=None) ``` the generated PrimFunc is ```python @T.prim_func def func(A: T.Buffer[(T.int64(2), T.int64(4)), "float32"], T_reshape: T.Buffer[(4, 2), "float32"]): for i0, i1 in T.grid(4, 2): with T.block("T_reshape"): ax0, ax1 = T.axis.remap("SS", [i0, i1]) T.reads(A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)]) T.writes(T_reshape[ax0, ax1]) T_reshape[ax0, ax1] = A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)] ``` Here loop variables `ax0` and `ax1` have dtype int32, since the shape of the output buffer is in int32. Other other hand, the input buffer has shape in int64. So as the script above shows, CreatePrimFunc will cast the int32 variables to int64 first, and access the input buffer afterwards. Now if we use the option `index_dtype_override` to specify an index dtype as below, ```python func = te.create_prim_func([A, B], index_dtype_override="int64") ``` the generated function will be ```python @T.prim_func def func(A: T.Buffer[(T.int64(2), T.int64(4)), "float32"], T_reshape: T.Buffer[(T.int64(4), T.int64(2)), "float32"]): for i0, i1 in T.grid(T.int64(4), T.int64(2)): with T.block("T_reshape"): ax0, ax1 = T.axis.remap("SS", [i0, i1]) T.reads(A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)]) T.writes(T_reshape[ax0, ax1]) T_reshape[ax0, ax1] = A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)] ``` Note that though all variables and the buffer shapes have dtype int64, there are still CastNodes such as `T.Cast("int64", ax0)` when `ax0` is already an int64 variable. We don’t want such redundant casting. To fix the issue above, this PR overrides the `VisitExpr_(const CastNode* cast)` method in IndexDataTypeNormalizer. When the `value` field of a CastNode already has the target dtype, we no longer cast it.
MasterJH5574
added a commit
to mlc-ai/relax
that referenced
this pull request
Jan 9, 2023
…tvm#13449) This PR fixes the behavior of IndexDataTypeNormalizer on CastNode. Consider the following case, ```python A = te.placeholder((tir.IntImm("int64", 2), tir.IntImm("int64", 4)), name="A") B = topi.reshape(A, (4, 2)) func = te.create_prim_func([A, B], index_dtype_override=None) ``` the generated PrimFunc is ```python @T.prim_func def func(A: T.Buffer[(T.int64(2), T.int64(4)), "float32"], T_reshape: T.Buffer[(4, 2), "float32"]): for i0, i1 in T.grid(4, 2): with T.block("T_reshape"): ax0, ax1 = T.axis.remap("SS", [i0, i1]) T.reads(A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)]) T.writes(T_reshape[ax0, ax1]) T_reshape[ax0, ax1] = A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)] ``` Here loop variables `ax0` and `ax1` have dtype int32, since the shape of the output buffer is in int32. Other other hand, the input buffer has shape in int64. So as the script above shows, CreatePrimFunc will cast the int32 variables to int64 first, and access the input buffer afterwards. Now if we use the option `index_dtype_override` to specify an index dtype as below, ```python func = te.create_prim_func([A, B], index_dtype_override="int64") ``` the generated function will be ```python @T.prim_func def func(A: T.Buffer[(T.int64(2), T.int64(4)), "float32"], T_reshape: T.Buffer[(T.int64(4), T.int64(2)), "float32"]): for i0, i1 in T.grid(T.int64(4), T.int64(2)): with T.block("T_reshape"): ax0, ax1 = T.axis.remap("SS", [i0, i1]) T.reads(A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)]) T.writes(T_reshape[ax0, ax1]) T_reshape[ax0, ax1] = A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)] ``` Note that though all variables and the buffer shapes have dtype int64, there are still CastNodes such as `T.Cast("int64", ax0)` when `ax0` is already an int64 variable. We don’t want such redundant casting. To fix the issue above, this PR overrides the `VisitExpr_(const CastNode* cast)` method in IndexDataTypeNormalizer. When the `value` field of a CastNode already has the target dtype, we no longer cast it.
MasterJH5574
added a commit
to MasterJH5574/tlc-relax
that referenced
this pull request
Jan 11, 2023
…tvm#13449) This PR fixes the behavior of IndexDataTypeNormalizer on CastNode. Consider the following case, ```python A = te.placeholder((tir.IntImm("int64", 2), tir.IntImm("int64", 4)), name="A") B = topi.reshape(A, (4, 2)) func = te.create_prim_func([A, B], index_dtype_override=None) ``` the generated PrimFunc is ```python @T.prim_func def func(A: T.Buffer[(T.int64(2), T.int64(4)), "float32"], T_reshape: T.Buffer[(4, 2), "float32"]): for i0, i1 in T.grid(4, 2): with T.block("T_reshape"): ax0, ax1 = T.axis.remap("SS", [i0, i1]) T.reads(A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)]) T.writes(T_reshape[ax0, ax1]) T_reshape[ax0, ax1] = A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)] ``` Here loop variables `ax0` and `ax1` have dtype int32, since the shape of the output buffer is in int32. Other other hand, the input buffer has shape in int64. So as the script above shows, CreatePrimFunc will cast the int32 variables to int64 first, and access the input buffer afterwards. Now if we use the option `index_dtype_override` to specify an index dtype as below, ```python func = te.create_prim_func([A, B], index_dtype_override="int64") ``` the generated function will be ```python @T.prim_func def func(A: T.Buffer[(T.int64(2), T.int64(4)), "float32"], T_reshape: T.Buffer[(T.int64(4), T.int64(2)), "float32"]): for i0, i1 in T.grid(T.int64(4), T.int64(2)): with T.block("T_reshape"): ax0, ax1 = T.axis.remap("SS", [i0, i1]) T.reads(A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)]) T.writes(T_reshape[ax0, ax1]) T_reshape[ax0, ax1] = A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)] ``` Note that though all variables and the buffer shapes have dtype int64, there are still CastNodes such as `T.Cast("int64", ax0)` when `ax0` is already an int64 variable. We don’t want such redundant casting. To fix the issue above, this PR overrides the `VisitExpr_(const CastNode* cast)` method in IndexDataTypeNormalizer. When the `value` field of a CastNode already has the target dtype, we no longer cast it.
MasterJH5574
added a commit
to MasterJH5574/tlc-relax
that referenced
this pull request
Jan 11, 2023
This PR fixes the behavior of IndexDataTypeNormalizer on CastNode. Consider the following case, ```python A = te.placeholder((tir.IntImm("int64", 2), tir.IntImm("int64", 4)), name="A") B = topi.reshape(A, (4, 2)) func = te.create_prim_func([A, B], index_dtype_override=None) ``` the generated PrimFunc is ```python @T.prim_func def func(A: T.Buffer[(T.int64(2), T.int64(4)), "float32"], T_reshape: T.Buffer[(4, 2), "float32"]): for i0, i1 in T.grid(4, 2): with T.block("T_reshape"): ax0, ax1 = T.axis.remap("SS", [i0, i1]) T.reads(A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)]) T.writes(T_reshape[ax0, ax1]) T_reshape[ax0, ax1] = A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)] ``` Here loop variables `ax0` and `ax1` have dtype int32, since the shape of the output buffer is in int32. Other other hand, the input buffer has shape in int64. So as the script above shows, CreatePrimFunc will cast the int32 variables to int64 first, and access the input buffer afterwards. Now if we use the option `index_dtype_override` to specify an index dtype as below, ```python func = te.create_prim_func([A, B], index_dtype_override="int64") ``` the generated function will be ```python @T.prim_func def func(A: T.Buffer[(T.int64(2), T.int64(4)), "float32"], T_reshape: T.Buffer[(T.int64(4), T.int64(2)), "float32"]): for i0, i1 in T.grid(T.int64(4), T.int64(2)): with T.block("T_reshape"): ax0, ax1 = T.axis.remap("SS", [i0, i1]) T.reads(A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)]) T.writes(T_reshape[ax0, ax1]) T_reshape[ax0, ax1] = A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)] ``` Note that though all variables and the buffer shapes have dtype int64, there are still CastNodes such as `T.Cast("int64", ax0)` when `ax0` is already an int64 variable. We don’t want such redundant casting. To fix the issue above, this PR overrides the `VisitExpr_(const CastNode* cast)` method in IndexDataTypeNormalizer. When the `value` field of a CastNode already has the target dtype, we no longer cast it.
MasterJH5574
added a commit
to MasterJH5574/tlc-relax
that referenced
this pull request
Jan 11, 2023
This PR fixes the behavior of IndexDataTypeNormalizer on CastNode. Consider the following case, ```python A = te.placeholder((tir.IntImm("int64", 2), tir.IntImm("int64", 4)), name="A") B = topi.reshape(A, (4, 2)) func = te.create_prim_func([A, B], index_dtype_override=None) ``` the generated PrimFunc is ```python @T.prim_func def func(A: T.Buffer[(T.int64(2), T.int64(4)), "float32"], T_reshape: T.Buffer[(4, 2), "float32"]): for i0, i1 in T.grid(4, 2): with T.block("T_reshape"): ax0, ax1 = T.axis.remap("SS", [i0, i1]) T.reads(A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)]) T.writes(T_reshape[ax0, ax1]) T_reshape[ax0, ax1] = A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)] ``` Here loop variables `ax0` and `ax1` have dtype int32, since the shape of the output buffer is in int32. Other other hand, the input buffer has shape in int64. So as the script above shows, CreatePrimFunc will cast the int32 variables to int64 first, and access the input buffer afterwards. Now if we use the option `index_dtype_override` to specify an index dtype as below, ```python func = te.create_prim_func([A, B], index_dtype_override="int64") ``` the generated function will be ```python @T.prim_func def func(A: T.Buffer[(T.int64(2), T.int64(4)), "float32"], T_reshape: T.Buffer[(T.int64(4), T.int64(2)), "float32"]): for i0, i1 in T.grid(T.int64(4), T.int64(2)): with T.block("T_reshape"): ax0, ax1 = T.axis.remap("SS", [i0, i1]) T.reads(A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)]) T.writes(T_reshape[ax0, ax1]) T_reshape[ax0, ax1] = A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)] ``` Note that though all variables and the buffer shapes have dtype int64, there are still CastNodes such as `T.Cast("int64", ax0)` when `ax0` is already an int64 variable. We don’t want such redundant casting. To fix the issue above, this PR overrides the `VisitExpr_(const CastNode* cast)` method in IndexDataTypeNormalizer. When the `value` field of a CastNode already has the target dtype, we no longer cast it.
tqchen
pushed a commit
to tlc-pack/relax
that referenced
this pull request
Jan 11, 2023
This PR fixes the behavior of IndexDataTypeNormalizer on CastNode. Consider the following case, ```python A = te.placeholder((tir.IntImm("int64", 2), tir.IntImm("int64", 4)), name="A") B = topi.reshape(A, (4, 2)) func = te.create_prim_func([A, B], index_dtype_override=None) ``` the generated PrimFunc is ```python @T.prim_func def func(A: T.Buffer[(T.int64(2), T.int64(4)), "float32"], T_reshape: T.Buffer[(4, 2), "float32"]): for i0, i1 in T.grid(4, 2): with T.block("T_reshape"): ax0, ax1 = T.axis.remap("SS", [i0, i1]) T.reads(A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)]) T.writes(T_reshape[ax0, ax1]) T_reshape[ax0, ax1] = A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)] ``` Here loop variables `ax0` and `ax1` have dtype int32, since the shape of the output buffer is in int32. Other other hand, the input buffer has shape in int64. So as the script above shows, CreatePrimFunc will cast the int32 variables to int64 first, and access the input buffer afterwards. Now if we use the option `index_dtype_override` to specify an index dtype as below, ```python func = te.create_prim_func([A, B], index_dtype_override="int64") ``` the generated function will be ```python @T.prim_func def func(A: T.Buffer[(T.int64(2), T.int64(4)), "float32"], T_reshape: T.Buffer[(T.int64(4), T.int64(2)), "float32"]): for i0, i1 in T.grid(T.int64(4), T.int64(2)): with T.block("T_reshape"): ax0, ax1 = T.axis.remap("SS", [i0, i1]) T.reads(A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)]) T.writes(T_reshape[ax0, ax1]) T_reshape[ax0, ax1] = A[(T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(8) // T.int64(4), (T.Cast("int64", ax0) * T.int64(2) + T.Cast("int64", ax1)) % T.int64(4)] ``` Note that though all variables and the buffer shapes have dtype int64, there are still CastNodes such as `T.Cast("int64", ax0)` when `ax0` is already an int64 variable. We don’t want such redundant casting. To fix the issue above, this PR overrides the `VisitExpr_(const CastNode* cast)` method in IndexDataTypeNormalizer. When the `value` field of a CastNode already has the target dtype, we no longer cast it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes the behavior of IndexDataTypeNormalizer on CastNode.
Background
Consider the following case,
the generated PrimFunc is
Here loop variables
ax0
andax1
have dtype int32, since the shape of the output buffer is in int32. Other other hand, the input buffer has shape in int64. So as the script above shows, CreatePrimFunc will cast the int32 variables to int64 first, and access the input buffer afterwards.Now if we use the option
index_dtype_override
to specify an index dtype as below,the generated function will be
Note that though all variables and the buffer shapes have dtype int64, there are still CastNodes such as
T.Cast("int64", ax0)
whenax0
is already an int64 variable. We don’t want such redundant casting.Fix
To fix the issue above, this PR overrides the
VisitExpr_(const CastNode* cast)
method in IndexDataTypeNormalizer. When thevalue
field of a CastNode already has the target dtype, we no longer cast it.cc @vinx13 @junrushao