|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <unordered_set>
|
|
|
|
|
|
#include "pattern_matching.hpp"
|
|
|
|
|
|
namespace {
|
|
|
using Graph = cv::gimpl::GModel::Graph;
|
|
|
using Metadata = typename Graph::CMetadataT;
|
|
|
using VisitedMatchings = std::list<std::pair<ade::NodeHandle, ade::NodeHandle>>;
|
|
|
|
|
|
using LabeledNodes = std::unordered_map
|
|
|
<
|
|
|
ade::NodeHandle
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
, std::vector<std::size_t>
|
|
|
, ade::HandleHasher<ade::Node>
|
|
|
>;
|
|
|
|
|
|
using MultipleMatchings = std::unordered_map
|
|
|
|
|
|
< ade::NodeHandle
|
|
|
|
|
|
, std::vector<ade::NodeHandle>
|
|
|
, ade::HandleHasher<ade::Node>
|
|
|
>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool compareDataNodes(const ade::NodeHandle& first, const std::vector<std::size_t>& firstPorts,
|
|
|
const Metadata& firstMeta,
|
|
|
const ade::NodeHandle& second, const std::vector<std::size_t>& secondPorts,
|
|
|
const Metadata& secondMeta) {
|
|
|
if (secondMeta.get<cv::gimpl::NodeType>().t != cv::gimpl::NodeType::DATA) {
|
|
|
throw std::logic_error("NodeType of passed node as second argument"
|
|
|
"shall be NodeType::DATA!");
|
|
|
}
|
|
|
|
|
|
if (firstMeta.get<cv::gimpl::Data>().shape != secondMeta.get<cv::gimpl::Data>().shape) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if (*firstPorts.begin() != *secondPorts.begin()) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
const auto& firstOutputEdges = first->outEdges();
|
|
|
const auto& secondOutputEdges = second->outEdges();
|
|
|
|
|
|
if (firstOutputEdges.size() != secondOutputEdges.size()) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool compareOpNodes(const VisitedMatchings& matchedVisitedNodes,
|
|
|
const ade::NodeHandle& first, std::vector<std::size_t> firstPorts,
|
|
|
const Metadata& firstMeta,
|
|
|
const ade::NodeHandle& second, std::vector<std::size_t> secondPorts,
|
|
|
const Metadata& secondMeta,
|
|
|
bool& isAlreadyVisited) {
|
|
|
if (secondMeta.get<cv::gimpl::NodeType>().t != cv::gimpl::NodeType::OP) {
|
|
|
throw std::logic_error("NodeType of passed node as second argument shall be NodeType::OP!");
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (firstMeta.get<cv::gimpl::Op>().k.name != secondMeta.get<cv::gimpl::Op>().k.name) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
std::sort(firstPorts.begin(), firstPorts.end());
|
|
|
std::sort(secondPorts.begin(), secondPorts.end());
|
|
|
if (firstPorts != secondPorts) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auto foundIt = std::find_if(matchedVisitedNodes.begin(), matchedVisitedNodes.end(),
|
|
|
[&first, &second](const std::pair<ade::NodeHandle,
|
|
|
ade::NodeHandle>& match)
|
|
|
{return first == match.first || second == match.second; });
|
|
|
if (foundIt != matchedVisitedNodes.end()) {
|
|
|
if (first != foundIt->first || second != foundIt->second) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
isAlreadyVisited = true;
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
VisitedMatchings sampleFromProduct(std::size_t sampleIdx,
|
|
|
const MultipleMatchings& candidatesSets)
|
|
|
|
|
|
{
|
|
|
VisitedMatchings matchingsSample;
|
|
|
|
|
|
std::size_t quo = sampleIdx;
|
|
|
for (const auto& setForNode : candidatesSets) {
|
|
|
|
|
|
|
|
|
auto size = setForNode.second.size();
|
|
|
|
|
|
|
|
|
|
|
|
std::size_t index = quo % size;
|
|
|
quo = quo / size;
|
|
|
const auto& candidate = setForNode.second[index];
|
|
|
matchingsSample.push_back({ setForNode.first, candidate });
|
|
|
}
|
|
|
|
|
|
return matchingsSample;
|
|
|
}
|
|
|
|
|
|
|
|
|
std::size_t labelOf (const ade::NodeHandle& node,
|
|
|
const ade::EdgeHandle& edge,
|
|
|
const Graph& graph)
|
|
|
{
|
|
|
|
|
|
if (graph.metadata(node).get<cv::gimpl::NodeType>().t == cv::gimpl::NodeType::OP) {
|
|
|
return graph.metadata(edge).get<cv::gimpl::Input>().port;
|
|
|
}
|
|
|
else {
|
|
|
return graph.metadata(edge).get<cv::gimpl::Output>().port;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
inline bool IS_STARTPOINT(const ade::NodeHandle& nh){
|
|
|
return nh->inEdges().empty();
|
|
|
}
|
|
|
|
|
|
inline bool IS_ENDPOINT(const ade::NodeHandle& nh){
|
|
|
|
|
|
|
|
|
return nh->outEdges().empty();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
cv::gimpl::SubgraphMatch
|
|
|
cv::gimpl::findMatches(const cv::gimpl::GModel::Graph& patternGraph,
|
|
|
const cv::gimpl::GModel::Graph& testGraph) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SubgraphMatch::S patternStartOpNodes, patternEndOpNodes;
|
|
|
|
|
|
const auto& patternInputDataNodes = patternGraph.metadata().get<cv::gimpl::Protocol>().in_nhs;
|
|
|
const auto& patternOutputDataNodes = patternGraph.metadata().get<cv::gimpl::Protocol>().out_nhs;
|
|
|
|
|
|
for (const auto& node : patternInputDataNodes) {
|
|
|
auto opNodes = node->outNodes();
|
|
|
patternStartOpNodes.insert(opNodes.begin(), opNodes.end());
|
|
|
}
|
|
|
|
|
|
for (const auto& node : patternOutputDataNodes) {
|
|
|
auto opNodes = node->inNodes();
|
|
|
|
|
|
patternEndOpNodes.insert(opNodes.begin(), opNodes.end());
|
|
|
}
|
|
|
|
|
|
std::unordered_map<ade::NodeHandle, // pattern OP node
|
|
|
std::vector<ade::NodeHandle>, // nodes in the test graph which match
|
|
|
// to the pattern OP node
|
|
|
ade::HandleHasher<ade::Node>> allMatchingsForStartOpNodes;
|
|
|
|
|
|
|
|
|
std::size_t possibleStartPointsCount = 1;
|
|
|
|
|
|
|
|
|
|
|
|
auto testOpNodes = ade::util::filter(testGraph.nodes(),
|
|
|
[&](const ade::NodeHandle& node) {
|
|
|
return testGraph.metadata(node).
|
|
|
get<cv::gimpl::NodeType>().t
|
|
|
== cv::gimpl::NodeType::OP;
|
|
|
});
|
|
|
for (const auto& patternStartOpNode : patternStartOpNodes) {
|
|
|
const auto& patternOpMeta = patternGraph.metadata(patternStartOpNode);
|
|
|
|
|
|
auto& possibleMatchings = allMatchingsForStartOpNodes[patternStartOpNode];
|
|
|
std::copy_if(testOpNodes.begin(), testOpNodes.end(), std::back_inserter(possibleMatchings),
|
|
|
[&](const ade::NodeHandle& testOpNode) {
|
|
|
const auto& testOpMeta = testGraph.metadata(testOpNode);
|
|
|
|
|
|
bool stub = false;
|
|
|
return compareOpNodes({ },
|
|
|
patternStartOpNode, { }, patternOpMeta,
|
|
|
testOpNode, { }, testOpMeta,
|
|
|
stub);
|
|
|
});
|
|
|
|
|
|
if (possibleMatchings.size() == 0) {
|
|
|
|
|
|
return SubgraphMatch { };
|
|
|
}
|
|
|
|
|
|
possibleStartPointsCount *= possibleMatchings.size();
|
|
|
}
|
|
|
|
|
|
SubgraphMatch::M subgraphStartOps;
|
|
|
SubgraphMatch::M subgraphEndOps;
|
|
|
|
|
|
std::list<ade::NodeHandle> subgraphInternals;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool patternFound = false;
|
|
|
std::size_t i = 0;
|
|
|
while (!patternFound && (i < possibleStartPointsCount)) {
|
|
|
subgraphStartOps.clear();
|
|
|
subgraphEndOps.clear();
|
|
|
subgraphInternals.clear();
|
|
|
|
|
|
|
|
|
VisitedMatchings matchedVisitedNodes;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
matchedVisitedNodes = sampleFromProduct(i, allMatchingsForStartOpNodes);
|
|
|
|
|
|
bool stop = false;
|
|
|
|
|
|
|
|
|
auto matchIt = matchedVisitedNodes.begin();
|
|
|
std::size_t size = matchedVisitedNodes.size();
|
|
|
|
|
|
while (!stop) {
|
|
|
|
|
|
|
|
|
for (std::size_t index = 0u; index < size && !stop; ++index, ++matchIt) {
|
|
|
|
|
|
|
|
|
|
|
|
bool cond1 = std::find(patternEndOpNodes.begin(),
|
|
|
patternEndOpNodes.end(),
|
|
|
matchIt->first)
|
|
|
!= patternEndOpNodes.end();
|
|
|
if (cond1) {
|
|
|
subgraphEndOps[matchIt->first] = matchIt->second;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool cond2 = std::find(patternStartOpNodes.begin(),
|
|
|
patternStartOpNodes.end(),
|
|
|
matchIt->first)
|
|
|
!= patternStartOpNodes.end();
|
|
|
if (cond2) {
|
|
|
subgraphStartOps[matchIt->first] = matchIt->second;
|
|
|
}
|
|
|
|
|
|
|
|
|
if (!cond1 && !cond2) {
|
|
|
subgraphInternals.push_back(matchIt->second);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LabeledNodes patternOutputNodesLabeled;
|
|
|
LabeledNodes testOutputNodesLabeled;
|
|
|
|
|
|
auto patternOutputEdges = matchIt->first->outEdges();
|
|
|
auto testOutputEdges = matchIt->second->outEdges();
|
|
|
|
|
|
for (const auto& patternOutputEdge : patternOutputEdges) {
|
|
|
const auto& dstNh = patternOutputEdge->dstNode();
|
|
|
if (!IS_ENDPOINT(dstNh)) {
|
|
|
|
|
|
patternOutputNodesLabeled[dstNh].
|
|
|
push_back(labelOf(dstNh, patternOutputEdge, patternGraph));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for (const auto& testOutputEdge : testOutputEdges) {
|
|
|
const auto& dstNh = testOutputEdge->dstNode();
|
|
|
testOutputNodesLabeled[dstNh].
|
|
|
push_back(labelOf(dstNh, testOutputEdge, testGraph));
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const auto& patternNode : patternOutputNodesLabeled) {
|
|
|
bool isAlreadyVisited = false;
|
|
|
const auto& patternNodeMeta = patternGraph.metadata(patternNode.first);
|
|
|
|
|
|
auto testIt = std::find_if(testOutputNodesLabeled.begin(),
|
|
|
testOutputNodesLabeled.end(),
|
|
|
[&](const std::pair<const ade::NodeHandle,
|
|
|
std::vector<std::size_t>>& testNode) {
|
|
|
const auto& testNodeMeta = testGraph.metadata(testNode.first);
|
|
|
|
|
|
auto patternNodeType = patternNodeMeta.get<cv::gimpl::NodeType>().t;
|
|
|
|
|
|
switch(patternNodeType) {
|
|
|
case cv::gimpl::NodeType::DATA:
|
|
|
return compareDataNodes(patternNode.first, patternNode.second,
|
|
|
patternNodeMeta,
|
|
|
testNode.first, testNode.second,
|
|
|
testNodeMeta);
|
|
|
case cv::gimpl::NodeType::OP:
|
|
|
return compareOpNodes(matchedVisitedNodes,
|
|
|
patternNode.first, patternNode.second,
|
|
|
patternNodeMeta,
|
|
|
testNode.first, testNode.second,
|
|
|
testNodeMeta,
|
|
|
isAlreadyVisited);
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
GAPI_Error("Unsupported Node type!");
|
|
|
});
|
|
|
|
|
|
if (testIt == testOutputNodesLabeled.end()) {
|
|
|
stop = true;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!isAlreadyVisited) {
|
|
|
matchedVisitedNodes.push_back({ patternNode.first, testIt->first });
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!stop) {
|
|
|
|
|
|
if (matchIt == matchedVisitedNodes.end()) {
|
|
|
|
|
|
stop = true;
|
|
|
patternFound = true;
|
|
|
}
|
|
|
|
|
|
|
|
|
size = static_cast<std::size_t>(std::distance(matchIt, matchedVisitedNodes.end()));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (!patternFound){
|
|
|
|
|
|
|
|
|
++i;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
SubgraphMatch::M inputApiMatch;
|
|
|
SubgraphMatch::M outputApiMatch;
|
|
|
|
|
|
|
|
|
for (auto it = subgraphStartOps.begin();
|
|
|
it != subgraphStartOps.end() && patternFound; ++it) {
|
|
|
const auto& match = *it;
|
|
|
auto patternInputEdges = match.first->inEdges();
|
|
|
auto testInputEdges = match.second->inEdges();
|
|
|
|
|
|
SubgraphMatch::S patternUniqInNodes(match.first->inNodes().begin(),
|
|
|
match.first->inNodes().end());
|
|
|
SubgraphMatch::S testUniqInNodes(match.second->inNodes().begin(),
|
|
|
match.second->inNodes().end());
|
|
|
|
|
|
if (patternUniqInNodes.size() < testUniqInNodes.size()) {
|
|
|
inputApiMatch.clear();
|
|
|
patternFound = false;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (const auto& patternInEdge : patternInputEdges) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!IS_STARTPOINT(patternInEdge->srcNode())) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
auto patternInputPort =
|
|
|
patternGraph.metadata(patternInEdge).get<cv::gimpl::Input>().port;
|
|
|
|
|
|
auto matchedIt = std::find_if(testInputEdges.begin(), testInputEdges.end(),
|
|
|
[&](const ade::EdgeHandle& testInEdge) -> bool {
|
|
|
auto testInputPort =
|
|
|
testGraph.metadata(testInEdge).get<cv::gimpl::Input>().port;
|
|
|
|
|
|
if (patternInputPort != testInputPort) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
auto foundIt = inputApiMatch.find(patternInEdge->srcNode());
|
|
|
if (foundIt != inputApiMatch.end()) {
|
|
|
if (testInEdge->srcNode() != foundIt->second) {
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
inputApiMatch[patternInEdge->srcNode()] = testInEdge->srcNode();
|
|
|
return true;
|
|
|
});
|
|
|
|
|
|
if (matchedIt == testInputEdges.end()) {
|
|
|
inputApiMatch.clear();
|
|
|
patternFound = false;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (!patternFound) {
|
|
|
|
|
|
|
|
|
++i;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
|
|
|
std::vector<ade::NodeHandle> inputTestDataNodes;
|
|
|
for (const auto& patternInNode : patternInputDataNodes) {
|
|
|
inputTestDataNodes.push_back(inputApiMatch.at(patternInNode));
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const auto& match : subgraphEndOps) {
|
|
|
auto patternOutputEdges = match.first->outEdges();
|
|
|
auto testOutputEdges = match.second->outEdges();
|
|
|
|
|
|
GAPI_Assert(patternOutputEdges.size() == testOutputEdges.size()
|
|
|
&&
|
|
|
"Ending OP nodes are matched, so OPs' outputs count shall be the same!");
|
|
|
|
|
|
|
|
|
for (const auto& patternOutEdge : patternOutputEdges) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!IS_ENDPOINT(patternOutEdge->dstNode())) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
auto patternOutputPort =
|
|
|
patternGraph.metadata(patternOutEdge).get<cv::gimpl::Output>().port;
|
|
|
|
|
|
auto matchedIt = std::find_if(testOutputEdges.begin(), testOutputEdges.end(),
|
|
|
[&](const ade::EdgeHandle& testOutEdge) -> bool {
|
|
|
auto testOutputPort =
|
|
|
testGraph.metadata(testOutEdge).get<cv::gimpl::Output>().port;
|
|
|
|
|
|
if (patternOutputPort != testOutputPort) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
outputApiMatch[patternOutEdge->dstNode()] = testOutEdge->dstNode();
|
|
|
return true;
|
|
|
});
|
|
|
|
|
|
GAPI_Assert(matchedIt != testOutputEdges.end()
|
|
|
&&
|
|
|
"There shall be a match for every OUT data node from ending OP node,"
|
|
|
"if ending OP node matches");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
std::vector<ade::NodeHandle> outputTestDataNodes;
|
|
|
for (const auto& patternOutNode : patternOutputDataNodes) {
|
|
|
outputTestDataNodes.push_back(outputApiMatch.at(patternOutNode));
|
|
|
}
|
|
|
|
|
|
SubgraphMatch subgraph;
|
|
|
|
|
|
subgraph.inputDataNodes = std::move(inputApiMatch);
|
|
|
subgraph.startOpNodes = std::move(subgraphStartOps);
|
|
|
subgraph.internalLayers = std::move(subgraphInternals);
|
|
|
subgraph.finishOpNodes = std::move(subgraphEndOps);
|
|
|
subgraph.outputDataNodes = std::move(outputApiMatch);
|
|
|
|
|
|
subgraph.inputTestDataNodes = std::move(inputTestDataNodes);
|
|
|
subgraph.outputTestDataNodes = std::move(outputTestDataNodes);
|
|
|
|
|
|
return subgraph;
|
|
|
|
|
|
}
|
|
|
|
|
|
return SubgraphMatch { };
|
|
|
}
|
|
|
|