|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
|
|
#include <ade/util/zip_range.hpp>
|
|
|
#include <ade/graph.hpp>
|
|
|
|
|
|
#include "api/gcomputation_priv.hpp"
|
|
|
|
|
|
#include "compiler/gmodel.hpp"
|
|
|
#include "compiler/gmodelbuilder.hpp"
|
|
|
#include "compiler/passes/passes.hpp"
|
|
|
#include "compiler/passes/pattern_matching.hpp"
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
namespace cv { namespace gimpl { namespace passes {
|
|
|
namespace
|
|
|
{
|
|
|
using Graph = GModel::Graph;
|
|
|
using Metadata = typename Graph::CMetadataT;
|
|
|
|
|
|
|
|
|
void checkDataNodes(const Graph& pattern,
|
|
|
const Graph& substitute,
|
|
|
const std::vector<ade::NodeHandle>& patternNodes,
|
|
|
const std::vector<ade::NodeHandle>& substituteNodes)
|
|
|
{
|
|
|
for (auto it : ade::util::zip(patternNodes, substituteNodes)) {
|
|
|
auto pNodeMeta = pattern.metadata(std::get<0>(it));
|
|
|
auto sNodeMeta = substitute.metadata(std::get<1>(it));
|
|
|
GAPI_Assert(pNodeMeta.get<NodeType>().t == NodeType::DATA);
|
|
|
GAPI_Assert(pNodeMeta.get<NodeType>().t == sNodeMeta.get<NodeType>().t);
|
|
|
GAPI_Assert(pNodeMeta.get<Data>().shape == sNodeMeta.get<Data>().shape);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
void checkCompatibility(const Graph& pattern,
|
|
|
const Graph& substitute,
|
|
|
const Protocol& patternP,
|
|
|
const Protocol& substituteP)
|
|
|
{
|
|
|
const auto& patternDataInputs = patternP.in_nhs;
|
|
|
const auto& patternDataOutputs = patternP.out_nhs;
|
|
|
|
|
|
const auto& substituteDataInputs = substituteP.in_nhs;
|
|
|
const auto& substituteDataOutputs = substituteP.out_nhs;
|
|
|
|
|
|
|
|
|
GAPI_Assert(patternDataInputs.size() == substituteDataInputs.size());
|
|
|
GAPI_Assert(patternDataOutputs.size() == substituteDataOutputs.size());
|
|
|
|
|
|
|
|
|
checkDataNodes(pattern, substitute, patternDataInputs, substituteDataInputs);
|
|
|
|
|
|
|
|
|
checkDataNodes(pattern, substitute, patternDataOutputs, substituteDataOutputs);
|
|
|
}
|
|
|
|
|
|
|
|
|
bool tryToSubstitute(ade::Graph& main,
|
|
|
const std::unique_ptr<ade::Graph>& patternG,
|
|
|
const cv::GComputation& substitute)
|
|
|
{
|
|
|
GModel::Graph gm(main);
|
|
|
|
|
|
|
|
|
auto match1 = findMatches(*patternG, gm);
|
|
|
if (!match1.ok()) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
|
|
|
cv::gimpl::GModelBuilder builder(main);
|
|
|
auto expr = cv::util::get<cv::GComputation::Priv::Expr>(substitute.priv().m_shape);
|
|
|
const auto& proto_slots = builder.put(expr.m_ins, expr.m_outs);
|
|
|
Protocol substituteP;
|
|
|
std::tie(substituteP.inputs, substituteP.outputs, substituteP.in_nhs, substituteP.out_nhs) =
|
|
|
proto_slots;
|
|
|
|
|
|
const Protocol& patternP = GModel::Graph(*patternG).metadata().get<Protocol>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
checkCompatibility(*patternG, gm, patternP, substituteP);
|
|
|
|
|
|
|
|
|
performSubstitution(gm, patternP, substituteP, match1);
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void applyTransformations(ade::passes::PassContext& ctx,
|
|
|
const GKernelPackage& pkg,
|
|
|
const std::vector<std::unique_ptr<ade::Graph>>& patterns)
|
|
|
{
|
|
|
const auto& transforms = pkg.get_transformations();
|
|
|
const auto size = transforms.size();
|
|
|
if (0u == size) return;
|
|
|
|
|
|
GAPI_Assert(patterns.size() == transforms.size());
|
|
|
|
|
|
|
|
|
bool canTransform = true;
|
|
|
while (canTransform)
|
|
|
{
|
|
|
canTransform = false;
|
|
|
|
|
|
|
|
|
for (auto it : ade::util::zip(ade::util::toRange(transforms), ade::util::toRange(patterns)))
|
|
|
{
|
|
|
const auto& t = std::get<0>(it);
|
|
|
auto& pattern = std::get<1>(it);
|
|
|
GAPI_Assert(nullptr != pattern);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
canTransform = tryToSubstitute(ctx.graph, pattern, t.substitute());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (canTransform) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|