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 2021/04/12 16:41:09 UTC

[GitHub] [tvm] comaniac commented on a change in pull request #7827: [Relay][Pass] SimplifyCastLike/Cast and ConcretizeFullLikeRewrite rewrites for SimplifyExpr

comaniac commented on a change in pull request #7827:
URL: https://github.com/apache/tvm/pull/7827#discussion_r611784815



##########
File path: src/relay/transforms/simplify_expr.cc
##########
@@ -75,6 +75,59 @@ class SimplifyReshape : public DFPatternRewrite {
   DFPattern x_;
 };
 
+/*!
+ * \brief SimplifyCastLike matches the pattern of cast data to the same dtype.
+ */
+class SimplifyCastLike : public DFPatternRewrite {
+ public:
+  SimplifyCastLike() {
+    data_pat_ = IsWildcard();
+    like_pat_ = IsWildcard();
+    pattern_ = IsOp("cast_like")({data_pat_, like_pat_});
+  }
+
+  Expr Callback(const Expr& pre, const Expr& post,
+                const Map<DFPattern, Array<Expr>>& node_map) const override {
+    auto data = node_map[data_pat_][0];
+    const TensorTypeNode* data_ty = data->checked_type().as<TensorTypeNode>();
+    const TensorTypeNode* like_ty = pre->checked_type().as<TensorTypeNode>();
+    if (like_ty->dtype == data_ty->dtype) {
+      return data;
+    }
+    return post;
+  }
+
+ protected:
+  DFPattern data_pat_;
+  DFPattern like_pat_;
+};
+
+/*!
+ * \brief SimplifyCast matches the pattern of cast data to the same dtype.
+ */
+class SimplifyCast : public DFPatternRewrite {

Review comment:
       Seems like these two patterns be merged to one?
   ```
   pattern_ = IsOp("cast_like")({data_pat_, like_pat_}) || IsOp("cast")({data_pat_});
   ```
   And both patterns can use `like_ty = pre->checked_type().as<TensorTypeNode>();`.




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

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