1 // Copyright (C) 2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
5 #include "vpu/utils/runtime_graph.hpp"
7 #include <cnn_network_impl.hpp>
8 #include <exec_graph_info.hpp>
16 using namespace InferenceEngine;
20 InferenceEngine::ICNNNetwork::Ptr buildRuntimeGraph(GraphMetaInfo &graphMetaInfo, const std::vector<float>& perfInfo) {
21 auto net = std::make_shared<InferenceEngine::details::CNNNetworkImpl>();
22 net->setName(graphMetaInfo.graphName);
24 std::map<size_t, CNNLayerPtr> stageMetaIndexToLayer;
26 auto createLayerFromMeta = [&](const StageMetaInfo &stageMetaInfo) -> CNNLayer::Ptr {
27 auto layer = std::make_shared<CNNLayer>(LayerParams{stageMetaInfo.stageName,
28 stageMetaInfo.layerType,
31 layer->params[ExecGraphInfoSerialization::ORIGINAL_NAMES] = stageMetaInfo.layerName;
32 layer->params[ExecGraphInfoSerialization::IMPL_TYPE] = stageMetaInfo.stageType;
33 layer->params[ExecGraphInfoSerialization::EXECUTION_ORDER] = std::to_string(stageMetaInfo.execOrder);
35 std::stringstream layoutStream;
37 for (auto &outLayout : stageMetaInfo.outLayouts) {
39 layoutStream << outLayout;
43 layoutStream << ',' << outLayout;
45 layer->params[ExecGraphInfoSerialization::OUTPUT_LAYOUTS] = layoutStream.str();
47 std::string outPrecisionsStr;
49 for (auto &outPrecision : stageMetaInfo.outPrecisions) {
51 outPrecisionsStr += outPrecision.name();
55 outPrecisionsStr += ',' + std::string(outPrecision.name());
57 layer->params[ExecGraphInfoSerialization::OUTPUT_PRECISIONS] = outPrecisionsStr;
59 if (stageMetaInfo.execOrder < 0) {
60 layer->params[ExecGraphInfoSerialization::PERF_COUNTER] = "not_executed";
62 layer->params[ExecGraphInfoSerialization::PERF_COUNTER] = std::to_string(stageMetaInfo.execTime);
69 // Write performance counts
72 const auto deviceTimings = perfInfo.data();
73 auto deviceTimingsCount = perfInfo.size();
75 if (deviceTimingsCount > 0) {
76 std::size_t timeIndex = 0;
78 for (auto &stageMeta : graphMetaInfo.stagesMeta) {
79 if (stageMeta.status == ie::InferenceEngineProfileInfo::EXECUTED &&
80 timeIndex < deviceTimingsCount) {
81 stageMeta.execTime += deviceTimings[timeIndex];
88 // Add all stages to network
91 for (std::size_t i = 0; i < graphMetaInfo.stagesMeta.size(); i++) {
92 const auto stageMetaData = graphMetaInfo.stagesMeta[i];
94 if (stageMetaData.status == ie::InferenceEngineProfileInfo::LayerStatus::OPTIMIZED_OUT ||
95 stageMetaData.stageName == "<Receive-Tensor>" ||
96 stageMetaData.stageName == "<none>") {
100 auto layer = createLayerFromMeta(stageMetaData);
101 stageMetaIndexToLayer.insert(std::make_pair(i, layer));
102 net->addLayer(layer);
106 // Add all edges to network
109 for (const auto &dataMetaData : graphMetaInfo.datasMeta) {
110 ::InferenceEngine::DataPtr data;
112 auto parent = stageMetaIndexToLayer[dataMetaData.parentIndex];
113 data = std::make_shared<::InferenceEngine::Data>(dataMetaData.name, dataMetaData.desc);
114 parent->outData.push_back(data);
115 data->getCreatorLayer() = parent;
117 for (auto &childMetaIndex : dataMetaData.childrenIndices) {
118 auto child = stageMetaIndexToLayer[childMetaIndex];
119 data->getInputTo()[child->name] = child;
120 child->insData.push_back(data);
125 // Specify inputs data
128 for (std::size_t i = 0; i < graphMetaInfo.stagesMeta.size(); i++) {
129 const auto stageMetaData = graphMetaInfo.stagesMeta[i];
131 if (stageMetaData.inputsNum != 0 ||
132 stageMetaData.stageName == "<Receive-Tensor>" ||
133 stageMetaData.stageName == "<none>") {
137 auto input = stageMetaIndexToLayer[i];
138 auto inputInfo = std::make_shared<InputInfo>();
139 inputInfo->setInputData(input->outData[0]);
140 net->setInputInfo(inputInfo);