ZhennanQin commented on a change in pull request #12698: add a tutorial for the 
subgraph API.
URL: https://github.com/apache/incubator-mxnet/pull/12698#discussion_r221408201
 
 

 ##########
 File path: docs/faq/subgraphAPI.md
 ##########
 @@ -0,0 +1,95 @@
+##How to Integrate Backend Libraries to MXNet with the Subgraph API
+
+The subgraph API has been proposed and implemented as the default mechanism 
for integrating backend libraries to MXNet. The subgraph API is a very flexible 
interface. Although it was proposed as an integration mechanism, it has been 
used as a tool for manipulating NNVM graphs for graph-level optimizations, such 
as operator fusion.
+
+The subgraph API works as the following steps:
+
+* Search for particular patterns in a graph,
+* Group the operators/nodes with particular patterns into a subgraph and 
shrink the subgraph into a single node,
+* Put the subgraph node back to the original graph to replace the subgraph.
+
+The figure below illustrates the subgraph mechanism.
+
+![](/Users/dzzhen/Workspace/incubator-mxnet/docs/faq/subgraph.png)
+
+The subgraph API allows the backend developers to customize the subgraph 
mechanism in two places:
+
+* Subgraph searching: define a subgraph selector to search for particular 
patterns in a computation graph,
+
+* Subgraph node creation: attach an operator to run the computation in the 
subgraph. We can potentially manipulate the subgraph here.
+
+
+Below I will illustrate the subgraph API with a very simple task, as shown in 
the figure above. That is, replacing convolution and batch_norm with the 
conv_bn.
+
+The first step is to define a subgraph selector to find the required pattern.
+
+```C++
+class SgSelector : public SubgraphSelector {
+ public:
+  SgSelector() {
+      find_bn = false;
+  }
+  bool Select(const nnvm::Node &n) override {
+      return n.op() && n.op()->name == "Convolution";
+  }
+
+  bool SelectInput(const nnvm::Node &n, const nnvm::Node &new_node) override {
+      return false;
+  }
+
+  bool SelectOutput(const nnvm::Node &n, const nnvm::Node &new_node) override {
+      if (n.op() && n.op()->name == "BatchNorm" && !find_bn) {
+          find_bn = true;
+          return true;
+      } else {
+          return false;
+      }
+  }
+  std::vector<nnvm::Node *> Filter(const std::vector<nnvm::Node *> 
&candidates) {
+      return candidates;
+  }
+
+ private:
+  bool find_bn;
+};
+
+```
+
+The second step is to define a subgraph property to use the subgraph selector 
above to customize the subgraph searching. By defining this class, we can also 
customize subgraph node creation. When customizing node creation, we can 
specify what operator to run the subgraph on the node. In the example below, we 
use CachedOp to run the subgraph with convolution and batch_norm. In practice, 
it's most likely that we use a fused operator from a backend library to run the 
subgraph.
+
+```C++
+class SgProperty : public SubgraphProperty {
+ public:
+  static SubgraphPropertyPtr Create() {
+    return std::make_shared<SgProperty>();
+  }
+
+  bool NeedGraphAttrs() const override { return false; }
 
 Review comment:
   I can't find this function in current codebase, will we add this soon? Shall 
we move this into the PR introducing this API?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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