Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[CPU] Disable parallel UpdateShapes/PrepairParams processing if there are many sync nodes (#27280)

Maksim Kutakovfc5f897442bd
diff --git a/src/plugins/intel_cpu/src/graph.cpp b/src/plugins/intel_cpu/src/graph.cpp
index 45118763a3e..f9bfa9334ea 100644
--- a/src/plugins/intel_cpu/src/graph.cpp
+++ b/src/plugins/intel_cpu/src/graph.cpp
@@ -371,9 +371,20 @@ void Graph::Activate(const std::vector<MemoryPtr>& externalInputMemory,
std::tie(m_executableGraphNodes, m_executableSyncNodesInds) = ExtractExecutableNodesAndSyncPoints(syncNodesInds, graphNodes);
- status = hasDynNodes ? (parallel_get_max_threads() > 1 ? Status::ReadyDynamic : Status::ReadyDynamicSeq)
- : Status::ReadyStatic;
-
+ if (hasDynNodes) {
+ status = Status::ReadyDynamic;
+ // Here we use the following heuristic: if the number of sync nodes is less than 10 times of the number of exec
+ // nodes, it does make sense to use Sequential dynamic shapes processing due to the high overheads on context
+ // switching when the dynamic shapes are being processed in parallel and there are a lot of sync points. Also
+ // this rule works for short graphs (usually subgraphs) when the amount of nodes is to low to process them in
+ // parallel.
+ const auto exec2sync = m_executableGraphNodes.size() / m_executableSyncNodesInds.size();
+ if (exec2sync < 10 || parallel_get_max_threads() < 2) {
+ status = Status::ReadyDynamicSeq;
+ }
+ } else {
+ status = Status::ReadyStatic;
+ }
CPU_DEBUG_CAP_ENABLE(serialize(*this));
}
diff --git a/src/plugins/intel_cpu/src/graph.h b/src/plugins/intel_cpu/src/graph.h
index b3634800fb2..d50ccc152c9 100644
--- a/src/plugins/intel_cpu/src/graph.h
+++ b/src/plugins/intel_cpu/src/graph.h
@@ -49,8 +49,16 @@ public:
~Graph();
- bool IsReady() {
- return one_of(status, Status::ReadyStatic, Status::ReadyDynamic, Status::ReadyDynamicSeq);
+ bool IsStatic() const {
+ return Status::ReadyStatic == status;
+ }
+
+ bool IsDynamic() const {
+ return one_of(status, Status::ReadyDynamic, Status::ReadyDynamicSeq);
+ }
+
+ bool IsReady() const {
+ return IsStatic() || IsDynamic();
}
const Config & getConfig() const {
@@ -193,7 +201,6 @@ public:
return graphHasDynamicInput;
}
- Status getStatus() const {return status;}
const std::unordered_map<std::string, node::MemoryStateNode*>& getInternalStateNodes() const;
/**
@@ -210,6 +217,10 @@ public:
void Activate(const std::vector<MemoryPtr>& externalInputMemory = {},
const std::vector<MemoryPtr>& externalOutputMemory = {});
+ const std::unordered_map<std::size_t, ProxyMemoryBlockPtr>& getOutputNodesMemBlocksMap() const {
+ return outputNodesMemBlocksMap;
+ }
+
protected:
void ForgetGraphData() {
status = Status::NotReady;
@@ -273,7 +284,6 @@ protected:
template<typename UpdateStrategy>
void InferDynamic(SyncInferRequest* request, int numaId, UpdateStrategy&& update);
- friend class intel_cpu::SyncInferRequest;
friend std::shared_ptr<ov::Model> dump_graph_as_ie_ngraph_net(const Graph &graph);
private:
diff --git a/src/plugins/intel_cpu/src/infer_request.cpp b/src/plugins/intel_cpu/src/infer_request.cpp
index f255a46efe7..f0b817dcda8 100644
--- a/src/plugins/intel_cpu/src/infer_request.cpp
+++ b/src/plugins/intel_cpu/src/infer_request.cpp
@@ -140,7 +140,7 @@ void SyncInferRequest::infer() {
throw_if_canceled();
// update output control blocks, if any, in order to refresh internal buffers
- if (Graph::Status::ReadyDynamic == m_graph->getStatus()) {
+ if (m_graph->IsDynamic()) {
for (auto&& item : m_outputControlBlocks) {
item.second.update();
}
@@ -178,7 +178,7 @@ void SyncInferRequest::change_default_ptr() {
std::unordered_set<const void*> inputPtrs;
std::function<void(const EdgePtr &edge, ov::SoPtr<ov::ITensor>& tensor)> changeInpPtr;
- if (Graph::Status::ReadyDynamic == m_graph->getStatus()) {
+ if (m_graph->IsDynamic()) {
changeInpPtr = [&inputPtrs](const EdgePtr &edge, ov::SoPtr<ov::ITensor>& tensor) {
change_edge_ptr(edge, tensor);
inputPtrs.insert(tensor->data());
@@ -278,8 +278,8 @@ void SyncInferRequest::change_default_ptr() {
change_edge_ptr(parentEdge, it.second);
}
- if (Graph::Status::ReadyDynamic == m_graph->getStatus()) {
- const auto &outMemBlocksMap = m_graph->outputNodesMemBlocksMap;
+ if (m_graph->IsDynamic()) {
+ const auto &outMemBlocksMap = m_graph->getOutputNodesMemBlocksMap();
for (auto&& item : outMemBlocksMap) {
const auto& name = item.first;
@@ -476,7 +476,7 @@ void SyncInferRequest::init_tensor(const std::size_t& port_index, const ov::ISyn
ov::SoPtr<ITensor> tensor;
if (type == ov::ISyncInferRequest::FoundPort::Type::INPUT) {
- OPENVINO_ASSERT(m_graph->inputNodesMap.find(port_index) != m_graph->inputNodesMap.end(),
+ OPENVINO_ASSERT(m_graph->GetInputNodesMap().find(port_index) != m_graph->GetInputNodesMap().end(),
"Tensor with index: ",
port_index,
" exists in CPU plugin graph, but absents in model inputs");
@@ -509,7 +509,7 @@ void SyncInferRequest::init_tensor(const std::size_t& port_index, const ov::ISyn
}
if (type == ov::ISyncInferRequest::FoundPort::Type::OUTPUT) {
- const auto& outMap = m_graph->outputNodesMap;
+ const auto& outMap = m_graph->GetOutputNodesMap();
auto output = outMap.find(port_index);
OPENVINO_ASSERT(output != outMap.end(),
"Tensor with index: ",
diff --git a/src/plugins/intel_cpu/src/nodes/tensoriterator.cpp b/src/plugins/intel_cpu/src/nodes/tensoriterator.cpp
index 9a3b9788b83..dcf2b0f8ffd 100644
--- a/src/plugins/intel_cpu/src/nodes/tensoriterator.cpp
+++ b/src/plugins/intel_cpu/src/nodes/tensoriterator.cpp
@@ -938,7 +938,7 @@ int TensorIterator::getNumIteration(const std::vector<PortMap>& inputPortMap, co
}
bool TensorIterator::runAsDynamic() const {
- return isDynamicNode() || Graph::Status::ReadyDynamic == sub_graph.getStatus();
+ return isDynamicNode() || sub_graph.IsDynamic();
}
bool TensorIterator::created() const {