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/12/07 08:51:35 UTC

[GitHub] [tvm] crazydemo opened a new pull request #9671: add simplify consetuitive add pattern in simplify expr pass

crazydemo opened a new pull request #9671:
URL: https://github.com/apache/tvm/pull/9671


   To make `FoldConstant` able to fuse pattern `conv add add relu` (comes from `conv bias_add bn relu`) into `conv add relu`.
   A `simplifyConsecuitiveAdd` pattern are added in `simplify_expr` pass.
   [Related RFC](https://discuss.tvm.apache.org/t/rfc-byoc-intel-r-onednn-integration/11582/10)
   Test case is also added in "test_pass_simplify_expr.py"


-- 
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] comaniac commented on pull request #9671: [SimplifyExpr] Simplify consecutive adds with constants

Posted by GitBox <gi...@apache.org>.
comaniac commented on pull request #9671:
URL: https://github.com/apache/tvm/pull/9671#issuecomment-991256068


   Thanks @crazydemo 


-- 
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] comaniac merged pull request #9671: [SimplifyExpr] Simplify consecutive adds with constants

Posted by GitBox <gi...@apache.org>.
comaniac merged pull request #9671:
URL: https://github.com/apache/tvm/pull/9671


   


-- 
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] comaniac commented on a change in pull request #9671: add simplify consetuitive add pattern in simplify expr pass

Posted by GitBox <gi...@apache.org>.
comaniac commented on a change in pull request #9671:
URL: https://github.com/apache/tvm/pull/9671#discussion_r766051215



##########
File path: src/relay/transforms/simplify_expr.cc
##########
@@ -585,6 +585,47 @@ class EliminateIdentityRewrite : public DFPatternRewrite {
   DFPattern const_;
 };
 
+/*! \brief Make two consecutive add able to be constant_folded. */
+class SimplifyConsecutiveAdd : public DFPatternRewrite {
+ public:
+  SimplifyConsecutiveAdd() {
+    x_ = IsWildcard();
+    const1_ = IsConstant();
+    const2_ = IsConstant();
+    DFPattern add_op1_ = IsOp("add");
+    DFPattern add_op2_ = IsOp("add");
+    pattern_ = add_op2_({add_op1_({x_, const1_}), const2_});
+  }
+
+  Expr Callback(const Expr& pre, const Expr& post,
+                const Map<DFPattern, Array<Expr>>& node_map) const override {
+    const CallNode* call = pre.as<CallNode>();
+    auto x = node_map[x_][0];
+    auto c1 = node_map[const1_][0];
+    auto c2 = node_map[const2_][0];
+
+    auto pre_call = call;
+    if (pre_call->args[1].as<ConstantNode>()) {
+      pre_call = pre_call->args[0].as<CallNode>();
+    } else {
+      pre_call = pre_call->args[1].as<CallNode>();
+    }
+    // check the first two input has one non-constant

Review comment:
       ```suggestion
       // Do nothing if both inputs are not constants as they will be constant folded already.
   ```

##########
File path: src/relay/transforms/simplify_expr.cc
##########
@@ -585,6 +585,47 @@ class EliminateIdentityRewrite : public DFPatternRewrite {
   DFPattern const_;
 };
 
+/*! \brief Make two consecutive add able to be constant_folded. */
+class SimplifyConsecutiveAdd : public DFPatternRewrite {
+ public:
+  SimplifyConsecutiveAdd() {
+    x_ = IsWildcard();
+    const1_ = IsConstant();
+    const2_ = IsConstant();
+    DFPattern add_op1_ = IsOp("add");
+    DFPattern add_op2_ = IsOp("add");
+    pattern_ = add_op2_({add_op1_({x_, const1_}), const2_});
+  }
+
+  Expr Callback(const Expr& pre, const Expr& post,
+                const Map<DFPattern, Array<Expr>>& node_map) const override {
+    const CallNode* call = pre.as<CallNode>();
+    auto x = node_map[x_][0];
+    auto c1 = node_map[const1_][0];
+    auto c2 = node_map[const2_][0];
+
+    auto pre_call = call;
+    if (pre_call->args[1].as<ConstantNode>()) {

Review comment:
       ```suggestion
       // Find the next add call.
       if (pre_call->args[1].as<ConstantNode>()) {
   ```

##########
File path: src/relay/transforms/simplify_expr.cc
##########
@@ -585,6 +585,47 @@ class EliminateIdentityRewrite : public DFPatternRewrite {
   DFPattern const_;
 };
 
+/*! \brief Make two consecutive add able to be constant_folded. */
+class SimplifyConsecutiveAdd : public DFPatternRewrite {
+ public:
+  SimplifyConsecutiveAdd() {
+    x_ = IsWildcard();
+    const1_ = IsConstant();
+    const2_ = IsConstant();
+    DFPattern add_op1_ = IsOp("add");
+    DFPattern add_op2_ = IsOp("add");
+    pattern_ = add_op2_({add_op1_({x_, const1_}), const2_});

Review comment:
       1. Only private class members need the underline suffix.
   2. You actually don't need two `add_op`s because all ops are the same.
   3. Might be better to add a comment saying pattern matching supports commutative property for addition so this pattern already covers all possible cases.
   ```suggestion
       DFPattern add_op = IsOp("add");
       pattern_ = add_op({add_op({x_, const1_}), const2_});
   ```




-- 
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] comaniac commented on pull request #9671: [SimplifyExpr] Simplify consecutive adds with constants

Posted by GitBox <gi...@apache.org>.
comaniac commented on pull request #9671:
URL: https://github.com/apache/tvm/pull/9671#issuecomment-991256068


   Thanks @crazydemo 


-- 
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] comaniac merged pull request #9671: [SimplifyExpr] Simplify consecutive adds with constants

Posted by GitBox <gi...@apache.org>.
comaniac merged pull request #9671:
URL: https://github.com/apache/tvm/pull/9671


   


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