From 121760476a8f914eefa296bc6bba02d1887f4239 Mon Sep 17 00:00:00 2001 From: Vladislav Volkov Date: Fri, 5 Mar 2021 16:28:41 +0300 Subject: [PATCH] Fix for MKLDNN constant layers execution (#4632) * Single mkldnn::engine for all MKLDNN graphs * Fix for MKLDNN constant layers execution --- .../src/mkldnn_plugin/mkldnn_edge.cpp | 4 +-- .../src/mkldnn_plugin/mkldnn_graph.cpp | 26 +++++++++++++------ .../src/mkldnn_plugin/mkldnn_graph.h | 6 ++--- .../nodes/mkldnn_tensoriterator_node.cpp | 3 +-- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/inference-engine/src/mkldnn_plugin/mkldnn_edge.cpp b/inference-engine/src/mkldnn_plugin/mkldnn_edge.cpp index bfda87ff812..b4c9434cd9f 100644 --- a/inference-engine/src/mkldnn_plugin/mkldnn_edge.cpp +++ b/inference-engine/src/mkldnn_plugin/mkldnn_edge.cpp @@ -169,9 +169,9 @@ void MKLDNNEdge::allocate(const void* mem_ptr) { } std::string MKLDNNEdge::name() const { - auto childPtr = getChild(); auto parentPtr = getParent(); - return childPtr->getName() + "<->" + parentPtr->getName(); + auto childPtr = getChild(); + return parentPtr->getName() + std::to_string(parent_port) + "<->" + childPtr->getName() + std::to_string(child_port); } void MKLDNNEdge::externalAllocate(MKLDNNWeightsSharing::Ptr weightsCache) { diff --git a/inference-engine/src/mkldnn_plugin/mkldnn_graph.cpp b/inference-engine/src/mkldnn_plugin/mkldnn_graph.cpp index 8087ba7b627..df071706ce4 100644 --- a/inference-engine/src/mkldnn_plugin/mkldnn_graph.cpp +++ b/inference-engine/src/mkldnn_plugin/mkldnn_graph.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -67,6 +68,8 @@ using namespace InferenceEngine::details; typedef std::unordered_set edge_cluster_t; typedef std::vector edge_clusters_t; +mkldnn::engine MKLDNNGraph::eng(mkldnn::engine::kind::cpu, 0); + template void MKLDNNGraph::ApplyUnrollPasses(NET &net) { OV_ITT_SCOPED_TASK(itt::domains::MKLDNNPlugin, "MKLDNNGraph::ApplyUnrollPasses"); @@ -453,15 +456,24 @@ void MKLDNNGraph::ExecuteConstantNodesOnly() { auto acquireSharedOutputs = [this](MKLDNNNodePtr & graphNode) { std::vector outputs; + bool hasLocalAllocatedEdges = false; + bool hasExternalInvalidEdges = false; for (size_t i = 0; i < graphNode->getChildEdges().size(); ++i) { auto edgePtr = graphNode->getChildEdgeAt(i); - if (edgePtr && edgePtr->isUseExternalMemory()) { - outputs.emplace_back(weightsCache->get(edgePtr->name())); + if (edgePtr) { + if (edgePtr->isUseExternalMemory()) { + auto ptr = weightsCache->get(edgePtr->name()); + outputs.emplace_back(ptr); + if (!ptr->isValid()) + hasExternalInvalidEdges = true; + } else { + hasLocalAllocatedEdges = true; + } } } - return outputs; + return std::make_tuple(hasExternalInvalidEdges, hasLocalAllocatedEdges, outputs); }; for (auto &graphNode : graphNodes) { @@ -471,12 +483,10 @@ void MKLDNNGraph::ExecuteConstantNodesOnly() { if (weightsCache) { auto sharedOutputs = acquireSharedOutputs(graphNode); - if (std::find_if(sharedOutputs.begin(), sharedOutputs.end(), - [](const shared_memory_ptr & ptr) { - return !ptr->isValid(); - }) != sharedOutputs.end()) { + if (std::get<0>(sharedOutputs) || std::get<1>(sharedOutputs)) { graphNode->execute(stream); - for (auto & output : sharedOutputs) + + for (auto & output : std::get<2>(sharedOutputs)) output->valid(true); } } else { diff --git a/inference-engine/src/mkldnn_plugin/mkldnn_graph.h b/inference-engine/src/mkldnn_plugin/mkldnn_graph.h index 0df95cbe86b..7834d08a7d3 100644 --- a/inference-engine/src/mkldnn_plugin/mkldnn_graph.h +++ b/inference-engine/src/mkldnn_plugin/mkldnn_graph.h @@ -30,7 +30,7 @@ public: Ready = 1, }; - MKLDNNGraph(mkldnn::engine eng = mkldnn::engine(mkldnn::engine::kind::cpu, 0)) : status(NotReady), eng(eng) {} + MKLDNNGraph() = default; Status GetStatus() { return status; @@ -172,7 +172,7 @@ protected: graphEdges.clear(); _meanImages.clear(); } - Status status; + Status status { NotReady }; Config config; // For dumping purposes. -1 - no counting, all other positive @@ -191,7 +191,7 @@ protected: std::map _meanImages; std::string _name; - mkldnn::engine eng; + static mkldnn::engine eng; void Replicate(const InferenceEngine::CNNNetwork &network, const MKLDNNExtensionManager::Ptr& extMgr); void Replicate(const InferenceEngine::TensorIterator::Body &subgraph, const MKLDNNExtensionManager::Ptr& extMgr); diff --git a/inference-engine/src/mkldnn_plugin/nodes/mkldnn_tensoriterator_node.cpp b/inference-engine/src/mkldnn_plugin/nodes/mkldnn_tensoriterator_node.cpp index 173718e122e..b7898dfb992 100644 --- a/inference-engine/src/mkldnn_plugin/nodes/mkldnn_tensoriterator_node.cpp +++ b/inference-engine/src/mkldnn_plugin/nodes/mkldnn_tensoriterator_node.cpp @@ -187,8 +187,7 @@ private: } // namespace MKLDNNPlugin MKLDNNTensorIteratorNode::MKLDNNTensorIteratorNode(InferenceEngine::CNNLayerPtr layer, const mkldnn::engine& eng, MKLDNNWeightsSharing::Ptr &cache) : - MKLDNNNode(layer, eng, cache), - sub_graph(eng) {} + MKLDNNNode(layer, eng, cache) {} void MKLDNNTensorIteratorNode::getSupportedDescriptors() { auto *ti = dynamic_cast(getCnnLayer().get());