masahi commented on a change in pull request #5272: [BYOC] Add example of 
Composite + Annotate for DNNL fused op
URL: https://github.com/apache/incubator-tvm/pull/5272#discussion_r405850469
 
 

 ##########
 File path: src/relay/backend/utils.h
 ##########
 @@ -152,8 +152,35 @@ inline bool IsOp(const CallNode* call, const std::string& 
op_name) {
   Op op = GetRef<Op>(op_node);
   return op == Op::Get(op_name);
 }
+
+/*!
+ * \brief Retrieve the "root" conv2d op nested inside a fused call, such as 
conv2d + relu.
+ * \param call A Relay call node. Typically nn.relu when called the first time.
+ * \param depth The number of calls before conv2d call, counting from 
current_call.
+ * \param expected_op_names The names of ops in this fused call. Example: 
{"nn.conv2d", "add",
+ * "nn.relu"}
+ * \return conv2d op at the root
+ */
+
+inline const CallNode* GetRootConv2DCall(const CallNode* current_call, int 
depth,
+                                         const std::vector<std::string>& 
expected_op_names) {
+  CHECK(current_call && depth >= 0);
+
+  if (depth == 0) {
+    CHECK(IsOp(current_call, "nn.conv2d"));
+    return current_call;
+  }
+
+  CHECK(depth < expected_op_names.size() && IsOp(current_call, 
expected_op_names[depth]));
+  CHECK_GT(current_call->args.size(), 0);
+
+  const auto* next_call = current_call->args[0].as<CallNode>();
+  return GetRootConv2DCall(next_call, depth - 1, expected_op_names);
+}
 
 Review comment:
   ah thanks that is a good idea since nothing there is specific to conv2d. We 
could also make `expected_op_names` arg optional, so that this can be used to 
retrieve any ancestor node at height `depth` above from `current_call`. If this 
sounds useful outside of fused call use cases, I can make it more general like 
that. What do you think? I can certainly make it work with dense or other 
`root_op`. cc @zhiics 

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to