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_r221407963
##########
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.
+
+
+
+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;
Review comment:
Here we need to check find_bn. Otherwise we will return single convolution
if find_bn == false.
----------------------------------------------------------------
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