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/11/21 00:38:42 UTC

[GitHub] [tvm] MasterJH5574 opened a new pull request, #13449: [Fix] Fix IndexDataTypeNormalizer to avoid redundant casting

MasterJH5574 opened a new pull request, #13449:
URL: https://github.com/apache/tvm/pull/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=None)
   ```
   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.
   
   ---
   
   cc @vinx13 @junrushao 


-- 
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 #13449: [Fix] Fix IndexDataTypeNormalizer to avoid redundant casting

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

   <!---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: `fix` <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] junrushao commented on pull request #13449: [Fix] Fix IndexDataTypeNormalizer to avoid redundant casting

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

   CC: @vinx13 for confirmation


-- 
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] MasterJH5574 commented on a diff in pull request #13449: [Fix] Fix IndexDataTypeNormalizer to avoid redundant casting

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


##########
src/te/operation/create_primfunc.cc:
##########
@@ -496,7 +496,7 @@ PrimFunc GenerateAndCompletePrimFunc(const Array<te::Tensor>& arg_list,
 PrimFunc CreatePrimFuncWithConstants(const Array<te::Tensor>& arg_list,
                                      const Array<runtime::NDArray>& constants,
                                      std::optional<DataType> index_dtype_override) {
-  // Infomations used in CreatePrimFunc and its sub-functions.
+  // Informations used in CreatePrimFunc and its sub-functions.

Review Comment:
   You’re right. Fixed :-)



-- 
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 #13449: [Fix] Fix IndexDataTypeNormalizer to avoid redundant casting

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


##########
src/te/operation/create_primfunc.cc:
##########
@@ -496,7 +496,7 @@ PrimFunc GenerateAndCompletePrimFunc(const Array<te::Tensor>& arg_list,
 PrimFunc CreatePrimFuncWithConstants(const Array<te::Tensor>& arg_list,
                                      const Array<runtime::NDArray>& constants,
                                      std::optional<DataType> index_dtype_override) {
-  // Infomations used in CreatePrimFunc and its sub-functions.
+  // Informations used in CreatePrimFunc and its sub-functions.

Review Comment:
   Information is not countable (i guess)



-- 
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] vinx13 commented on pull request #13449: [Fix] Fix IndexDataTypeNormalizer to avoid redundant casting

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

   Any ideas why the loop i0, i1 wasn't rewritten to int64 previously? The rewriting of loop is independent from the dtype of buffer shapes. 
   https://github.com/apache/tvm/blob/main/src/tir/ir/data_type_rewriter.cc#L461


-- 
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 #13449: [Fix] Fix IndexDataTypeNormalizer to avoid redundant casting

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


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