graph_simplifier.cpp
6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
// Copyright (C) 2020, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
#include "precomp.hpp"
#include "graph_simplifier.hpp"
#include <queue>
namespace cv { namespace dnn {
Subgraph::~Subgraph() {}
int Subgraph::addNodeToMatch(const std::string& op, int input_0, int input_1,
int input_2, int input_3)
{
int nodeInputs[] = {input_0, input_1, input_2, input_3};
int numInputs = 0;
for (int i = 0; i < 4; ++i)
{
numInputs += (int)(nodeInputs[i] != -1);
}
return addNodeToMatch(op, std::vector<int>(&nodeInputs[0], &nodeInputs[0] + numInputs));
}
int Subgraph::addNodeToMatch(const std::string& op, const std::vector<int>& inputs_)
{
for (int i = 0; i < inputs_.size(); ++i)
{
CV_Assert(inputs_[i] < (int)nodes.size());
}
nodes.push_back(op);
inputs.push_back(inputs_);
return nodes.size() - 1;
}
void Subgraph::setFusedNode(const std::string& op, int input_0, int input_1,
int input_2, int input_3, int input_4, int input_5)
{
int nodeInputs[] = {input_0, input_1, input_2, input_3, input_4, input_5};
int numInputs = 0;
for (int i = 0; i < 6; ++i)
{
CV_Assert(nodeInputs[i] < (int)nodes.size());
numInputs += (int)(nodeInputs[i] != -1);
}
setFusedNode(op, std::vector<int>(&nodeInputs[0], &nodeInputs[0] + numInputs));
}
void Subgraph::setFusedNode(const std::string& op, const std::vector<int>& inputs_)
{
fusedNodeInputs = inputs_;
fusedNodeOp = op;
}
int Subgraph::getInputNodeId(const Ptr<ImportGraphWrapper>& net,
const Ptr<ImportNodeWrapper>& node,
int inpId)
{
CV_Assert(inpId < node->getNumInputs());
std::string name = node->getInputName(inpId);
const int numNodes = net->getNumNodes();
for (int i = 0; i < numNodes; ++i)
{
const int numOutputs = net->getNumOutputs(i);
for (int j = 0; j < numOutputs; j++)
{
if (net->getOutputName(i, j) == name)
return i;
}
}
CV_Error(Error::StsParseError, "Input node with name " + name + " not found");
}
bool Subgraph::match(const Ptr<ImportGraphWrapper>& net, int nodeId,
std::vector<int>& matchedNodesIds,
std::vector<int>& targetNodesIds)
{
matchedNodesIds.clear();
targetNodesIds.clear();
std::queue<int> nodesToMatch;
std::queue<int> targetNodes;
nodesToMatch.push(nodeId);
targetNodes.push(nodes.size() - 1);
while (!nodesToMatch.empty())
{
int nodeToMatch = nodesToMatch.front();
int targetNodeId = targetNodes.front();
nodesToMatch.pop();
targetNodes.pop();
if (std::find(matchedNodesIds.begin(), matchedNodesIds.end(), nodeToMatch) !=
matchedNodesIds.end())
continue;
const Ptr<ImportNodeWrapper> node = net->getNode(nodeToMatch);
if (node->getType() != nodes[targetNodeId])
return false;
std::vector<int>& inputNodes = inputs[targetNodeId];
if (inputNodes.size() != node->getNumInputs())
return false;
for (int j = 0; j < inputNodes.size(); ++j)
{
if (nodes[inputNodes[j]].empty()) // Unknown input node type.
continue;
nodeId = getInputNodeId(net, node, j);
const Ptr<ImportNodeWrapper> inpNode = net->getNode(nodeId);
if (inpNode->getType() != "Const" && inpNode->getType() != "Constant")
{
nodesToMatch.push(nodeId);
targetNodes.push(inputNodes[j]);
}
else if (nodes[inputNodes[j]] != "Const" && nodes[inputNodes[j]] != "Constant")
return false;
}
matchedNodesIds.push_back(nodeToMatch);
targetNodesIds.push_back(targetNodeId);
}
const int n = matchedNodesIds.size();
std::vector<std::pair<int, int> > elements(n);
for (int i = 0; i < n; ++i)
elements[i] = std::make_pair(matchedNodesIds[i], targetNodesIds[i]);
std::sort(elements.begin(), elements.end());
for (int i = 0; i < n; ++i)
{
matchedNodesIds[i] = elements[i].first;
targetNodesIds[i] = elements[i].second;
}
return true;
}
void Subgraph::replace(const Ptr<ImportGraphWrapper>& net, const std::vector<int>& matchedNodesIds,
const std::vector<int>& targetNodesIds)
{
// Extract names of input nodes.
std::vector<std::string> inputsNames(fusedNodeInputs.size());
for (int i = 0; i < fusedNodeInputs.size(); ++i)
{
std::string inpName;
// Find input node name looking at inputs of fused nodes.
for (int j = 0; j < matchedNodesIds.size() && inpName.empty(); ++j)
{
Ptr<ImportNodeWrapper> node = net->getNode(matchedNodesIds[j]);
std::vector<int>& inpIndices = inputs[targetNodesIds[j]];
CV_Assert(node->getNumInputs() == inpIndices.size());
for (int k = 0; k < inpIndices.size(); ++k)
{
if (inpIndices[k] == fusedNodeInputs[i])
{
inpName = node->getInputName(k);
break;
}
}
}
CV_Assert(!inpName.empty());
inputsNames[i] = inpName;
}
// Remove matched nodes except the last one. Indices in ascending order are expected.
Ptr<ImportNodeWrapper> node = net->getNode(matchedNodesIds.back());
for (int i = matchedNodesIds.size() - 2; i >= 0; --i)
net->removeNode(matchedNodesIds[i]);
// Modify the last node to be a fused one.
node->setType(fusedNodeOp);
node->setInputNames(inputsNames);
std::vector<Ptr<ImportNodeWrapper> > inputNodes(inputsNames.size());
for (int i = 0; i < inputsNames.size(); ++i)
{
inputNodes[i] = net->getNode(getInputNodeId(net, node, i));
}
finalize(net, node, inputNodes);
}
void Subgraph::finalize(const Ptr<ImportGraphWrapper>& net,
const Ptr<ImportNodeWrapper>& fusedNode,
std::vector<Ptr<ImportNodeWrapper> >& inputs) {}
void simplifySubgraphs(const Ptr<ImportGraphWrapper>& net,
const std::vector<Ptr<Subgraph> >& patterns)
{
int numNodes = net->getNumNodes();
std::vector<int> matchedNodesIds, targetNodesIds;
for (int j = 0; j < patterns.size(); ++j)
{
for (int i = 0; i < numNodes; ++i)
{
if (patterns[j]->match(net, i, matchedNodesIds, targetNodesIds))
{
patterns[j]->replace(net, matchedNodesIds, targetNodesIds);
numNodes -= matchedNodesIds.size() - 1; // #matchedNodes removed and one added.
}
}
}
}
}} // namespace cv::dnn