Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/gapi/src/compiler/passes/meta.cpp
16345 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html.
4
//
5
// Copyright (C) 2018 Intel Corporation
6
7
8
#include "precomp.hpp"
9
10
#include <ade/util/zip_range.hpp> // util::indexed
11
#include <ade/graph.hpp>
12
#include <ade/passes/check_cycles.hpp>
13
14
#include "compiler/gmodel.hpp"
15
#include "compiler/passes/passes.hpp"
16
#include "logger.hpp" // GAPI_LOG
17
18
19
// Iterate over all nodes and initialize meta of objects taken from the
20
// outside (i.e., computation input/output arguments)
21
void cv::gimpl::passes::initMeta(ade::passes::PassContext &ctx, const GMetaArgs &metas)
22
{
23
GModel::Graph gr(ctx.graph);
24
25
const auto &proto = gr.metadata().get<Protocol>();
26
27
for (const auto& it : ade::util::indexed(proto.in_nhs))
28
{
29
auto& data = gr.metadata(ade::util::value(it)).get<Data>();
30
data.meta = metas.at(ade::util::index(it));
31
}
32
}
33
34
// Iterate over all operations in the topological order, trigger kernels
35
// validate() function, update output objects metadata.
36
void cv::gimpl::passes::inferMeta(ade::passes::PassContext &ctx)
37
{
38
// FIXME: ADE pass dependency on topo_sort?
39
// FIXME: ADE pass dependency on initMeta?
40
GModel::Graph gr(ctx.graph);
41
42
const auto sorted = gr.metadata().get<ade::passes::TopologicalSortData>() ;
43
for (const auto &nh : sorted.nodes())
44
{
45
if (gr.metadata(nh).get<NodeType>().t == NodeType::OP)
46
{
47
const auto& op = gr.metadata(nh).get<Op>();
48
GAPI_Assert(op.k.outMeta != nullptr);
49
50
// Prepare operation's input metadata vector
51
// Note that it's size is usually different from nh.inEdges.size(),
52
// and its element count is equal to operation's arguments count.
53
GMetaArgs input_meta_args(op.args.size());
54
55
// Iterate through input edges, update input_meta_args's slots
56
// appropriately. Not all of them will be updated due to (see above).
57
GAPI_Assert(nh->inEdges().size() > 0);
58
for (const auto &in_eh : nh->inEdges())
59
{
60
const auto& input_port = gr.metadata(in_eh).get<Input>().port;
61
const auto& input_nh = in_eh->srcNode();
62
GAPI_Assert(gr.metadata(input_nh).get<NodeType>().t == NodeType::DATA);
63
64
const auto& input_meta = gr.metadata(input_nh).get<Data>().meta;
65
if (util::holds_alternative<util::monostate>(input_meta))
66
{
67
// No meta in an input argument - a fatal error
68
// (note graph is traversed here in topoligcal order)
69
util::throw_error(std::logic_error("Fatal: input object's metadata "
70
"not found!"));
71
// FIXME: Add more details!!!
72
}
73
input_meta_args.at(input_port) = input_meta;
74
}
75
// Now ask kernel for it's output meta.
76
// Resulting out_args may have a larger size than op.outs, since some
77
// outputs could stay unused (unconnected)
78
const GMetaArgs out_metas = op.k.outMeta(input_meta_args, op.args);
79
80
// Walk through operation's outputs, update meta of output objects
81
// appropriately
82
GAPI_Assert(nh->outEdges().size() > 0);
83
for (const auto &out_eh : nh->outEdges())
84
{
85
const auto &output_port = gr.metadata(out_eh).get<Output>().port;
86
const auto &output_nh = out_eh->dstNode();
87
GAPI_Assert(gr.metadata(output_nh).get<NodeType>().t == NodeType::DATA);
88
89
auto &output_meta = gr.metadata(output_nh).get<Data>().meta;
90
if (!util::holds_alternative<util::monostate>(output_meta))
91
{
92
GAPI_LOG_INFO(NULL,
93
"!!! Output object has an initialized meta - "
94
"how it is possible today?" << std::endl; );
95
if (output_meta != out_metas.at(output_port))
96
{
97
util::throw_error(std::logic_error("Fatal: meta mismatch"));
98
// FIXME: New exception type?
99
// FIXME: More details!
100
}
101
}
102
// Store meta in graph
103
output_meta = out_metas.at(output_port);
104
}
105
} // if(OP)
106
} // for(sorted)
107
}
108
109
// After all metadata in graph is infered, store a vector of inferred metas
110
// for computation output values.
111
void cv::gimpl::passes::storeResultingMeta(ade::passes::PassContext &ctx)
112
{
113
GModel::Graph gr(ctx.graph);
114
115
const auto &proto = gr.metadata().get<Protocol>();
116
GMetaArgs output_metas(proto.out_nhs.size());
117
118
for (const auto& it : ade::util::indexed(proto.out_nhs))
119
{
120
auto& data = gr.metadata(ade::util::value(it)).get<Data>();
121
output_metas[ade::util::index(it)] = data.meta;
122
}
123
124
gr.metadata().set(OutputMeta{output_metas});
125
}
126
127