MXNet can integrate with many different kinds of acceleratorsbackend libraries, including TVM, MKLDNN, TensorRT, Intel nGraph and more. These accelerators These backend in general support a limited number of operators, and thus running computation in a model usually involves in interaction between accelerator operators and MXNet operators.
These accelerators share some common requirements:
...
Integration with these accelerators should happen in the granularity of subgraphs instead of in the granularity of operators. To fuse operators, it's obvious that we need to divide a graph into subgraphs so that the operators in a subgraph can be fused into a single operator. To handle customized data formats, we should partition a computation graph into subgraphs as well. Each subgraph contains only TVM, MKLDNN or ngraph operators. In this way, MXNet converts data formats only when entering such a subgraph and the operators inside a subgraph handle format conversion themselves if necessary. This makes interaction of TVM and MKLDNN with MXNet much easier. Neither the MXNet executor nor the MXNet operators need to deal with customized data formats. Integration with these accelerators may result in two levels of graph partitioning. In the first level, a subgraph only contains the operators supported by the accelerator. In the second level, a subgraph only contains the operators that can be fused.
- TVM and MKLDNN require two levels of partitioning. We want to isolate MKLDNN/TVM operators from MXNet operators so that we know where to insert MKLDNN/TVM format conversion operators. They also fuses operators to achieve the optimal performance and operator fusion is done in NNVM.
- TensorRT and nGraph only need one level of partitioning. Once we get a TensorRT and nGraph subgraph, the libraries perform a graph transformation internally for better performance, which is hidden from the library users.
Even though invoking these libraries from MXNet requires similar steps, the partitioning rule and the subgraph execution of these accelerators can be different. As such, we define the following interface for accelerators to customize graph partitioning and subgraph execution inside an operator.
...
Given these assumptions, our graph partitioning algorithm traverses from every node in a graph with breadth-first search and explore their neighbor nodes with rules provided by users. The interface below allows users to define the selection rules. Given this interface, users can determine which edges to follow to generate a subgraph. Starting from every node, the graph partitioning algorithm creates a new subgraph selector and tries to grow a subgraph. Two criteria need to meet before a node is selected as a starting node:
...
When a subgraph is found, the partitioning algorithm invokes SubgraphProperty::CreateSubgraphNode to create a new node for the subgraph and connects the new node back to the original graph to replace the subgraph. The subgraph passed to CreateSubgraphNode contains shape/dtype/storage information of the input nodes. This is important because accelerators, such as TVM and TensorRT, need these information to compile and select the best kernel. CreateSubgraphNode allows any customization on the subgraph and the node, which gives users many opportunities to optimize the subgraph. For example, TensorRT can optimize a computation graph based on its input shapes and data types in CreateSubgraphNode; some of the accelerators, such as TVM and MKLDNN, can perform another level of graph partitioning in CreateSubgraphNode for operator fusion.
To perform graph partitioning, we attach a graph property (a class that implements SubgraphProperty) and invoke PartitionGraph.
...