Path: blob/master/modules/gapi/src/compiler/passes/meta.cpp
16345 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.3//4// Copyright (C) 2018 Intel Corporation567#include "precomp.hpp"89#include <ade/util/zip_range.hpp> // util::indexed10#include <ade/graph.hpp>11#include <ade/passes/check_cycles.hpp>1213#include "compiler/gmodel.hpp"14#include "compiler/passes/passes.hpp"15#include "logger.hpp" // GAPI_LOG161718// Iterate over all nodes and initialize meta of objects taken from the19// outside (i.e., computation input/output arguments)20void cv::gimpl::passes::initMeta(ade::passes::PassContext &ctx, const GMetaArgs &metas)21{22GModel::Graph gr(ctx.graph);2324const auto &proto = gr.metadata().get<Protocol>();2526for (const auto& it : ade::util::indexed(proto.in_nhs))27{28auto& data = gr.metadata(ade::util::value(it)).get<Data>();29data.meta = metas.at(ade::util::index(it));30}31}3233// Iterate over all operations in the topological order, trigger kernels34// validate() function, update output objects metadata.35void cv::gimpl::passes::inferMeta(ade::passes::PassContext &ctx)36{37// FIXME: ADE pass dependency on topo_sort?38// FIXME: ADE pass dependency on initMeta?39GModel::Graph gr(ctx.graph);4041const auto sorted = gr.metadata().get<ade::passes::TopologicalSortData>() ;42for (const auto &nh : sorted.nodes())43{44if (gr.metadata(nh).get<NodeType>().t == NodeType::OP)45{46const auto& op = gr.metadata(nh).get<Op>();47GAPI_Assert(op.k.outMeta != nullptr);4849// Prepare operation's input metadata vector50// Note that it's size is usually different from nh.inEdges.size(),51// and its element count is equal to operation's arguments count.52GMetaArgs input_meta_args(op.args.size());5354// Iterate through input edges, update input_meta_args's slots55// appropriately. Not all of them will be updated due to (see above).56GAPI_Assert(nh->inEdges().size() > 0);57for (const auto &in_eh : nh->inEdges())58{59const auto& input_port = gr.metadata(in_eh).get<Input>().port;60const auto& input_nh = in_eh->srcNode();61GAPI_Assert(gr.metadata(input_nh).get<NodeType>().t == NodeType::DATA);6263const auto& input_meta = gr.metadata(input_nh).get<Data>().meta;64if (util::holds_alternative<util::monostate>(input_meta))65{66// No meta in an input argument - a fatal error67// (note graph is traversed here in topoligcal order)68util::throw_error(std::logic_error("Fatal: input object's metadata "69"not found!"));70// FIXME: Add more details!!!71}72input_meta_args.at(input_port) = input_meta;73}74// Now ask kernel for it's output meta.75// Resulting out_args may have a larger size than op.outs, since some76// outputs could stay unused (unconnected)77const GMetaArgs out_metas = op.k.outMeta(input_meta_args, op.args);7879// Walk through operation's outputs, update meta of output objects80// appropriately81GAPI_Assert(nh->outEdges().size() > 0);82for (const auto &out_eh : nh->outEdges())83{84const auto &output_port = gr.metadata(out_eh).get<Output>().port;85const auto &output_nh = out_eh->dstNode();86GAPI_Assert(gr.metadata(output_nh).get<NodeType>().t == NodeType::DATA);8788auto &output_meta = gr.metadata(output_nh).get<Data>().meta;89if (!util::holds_alternative<util::monostate>(output_meta))90{91GAPI_LOG_INFO(NULL,92"!!! Output object has an initialized meta - "93"how it is possible today?" << std::endl; );94if (output_meta != out_metas.at(output_port))95{96util::throw_error(std::logic_error("Fatal: meta mismatch"));97// FIXME: New exception type?98// FIXME: More details!99}100}101// Store meta in graph102output_meta = out_metas.at(output_port);103}104} // if(OP)105} // for(sorted)106}107108// After all metadata in graph is infered, store a vector of inferred metas109// for computation output values.110void cv::gimpl::passes::storeResultingMeta(ade::passes::PassContext &ctx)111{112GModel::Graph gr(ctx.graph);113114const auto &proto = gr.metadata().get<Protocol>();115GMetaArgs output_metas(proto.out_nhs.size());116117for (const auto& it : ade::util::indexed(proto.out_nhs))118{119auto& data = gr.metadata(ade::util::value(it)).get<Data>();120output_metas[ade::util::index(it)] = data.meta;121}122123gr.metadata().set(OutputMeta{output_metas});124}125126127