Replacing A Subgraph
Introduction
This example first generates a model consisting of a Min op followed by a
Max, and then uses the graph.layer() and graph.register() APIs seen in
example 07 to create a function
that can be used to replace this subgraph with a Clip op.
This can be useful, for example, to enable TensorRT plugins with ONNX.
Subgraph Replacement Basics
The process of replacing a subgraph involves 3 steps. For example, for a graph with the following structure:
Tensor0
|
Node0
|
Tensor1 Tensor2
\ /
Node1
|
Tensor3
|
Node2
In order to replace the subgraph consisting of [Node0, Node1], we need to:
-
Disconnect the outputs of the subgraph inputs:
Tensor0andTensor2That means we need to delete the edge between
Tensor0andNode0, and betweenTensor2andNode1. -
Disconnect the inputs of the subgraph outputs:
Tensor3That means we need to delete the edge between
Node1andTensor3.
This will leave us with a graph like this:
Tensor0 Tensor2
Tensor3
|
Node2
And the now disconnected subgraph:
Node0
|
Tensor1
\
Node1
- Lastly, we need to insert our node such that it has inputs: [
Tensor0,Tensor2] and outputs: [Tensor3, ].
After this step, we have our final graph (cleanup() will remove the dangling subgraph):
Tensor0 Tensor2
\ /
MyNewNode0
|
Tensor3
|
Node2

