Skip to content

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

[GPU] allocate from memory pool for nodes that are optimizable but not skipped runtime (#26321)

cecilia peng8b846b6fa958
diff --git a/src/plugins/intel_gpu/src/graph/broadcast.cpp b/src/plugins/intel_gpu/src/graph/broadcast.cpp
index 741a3ad8a30..3af0300602d 100644
--- a/src/plugins/intel_gpu/src/graph/broadcast.cpp
+++ b/src/plugins/intel_gpu/src/graph/broadcast.cpp
@@ -146,6 +146,12 @@ void broadcast_inst::update_output_memory() {
GPU_DEBUG_TRACE_DETAIL << id() << " : update_output_memory with mem of input " << get_node().get_dependency(0).id()
<< " : " << input_memory_ptr()->buffer_ptr() << std::endl;
+ // Can_be_optimized nodes are allocating from memory_pool too. In this case,
+ // we need release the legacy output memory from memory pool explicitly.
+ if (static_cast<bool>(_outputs[0]) &&
+ _node->get_program().get_config().get_property(ov::intel_gpu::enable_memory_pool)) {
+ _network.get_memory_pool().release_memory(_outputs[0].get(), _node->get_unique_id(), _node->id(), _network.get_id());
+ }
_outputs[0] = input_memory_ptr();
_mem_allocated = false;
}
diff --git a/src/plugins/intel_gpu/src/graph/crop.cpp b/src/plugins/intel_gpu/src/graph/crop.cpp
index 0fd733be570..e17cc3e5552 100644
--- a/src/plugins/intel_gpu/src/graph/crop.cpp
+++ b/src/plugins/intel_gpu/src/graph/crop.cpp
@@ -275,6 +275,12 @@ void crop_inst::update_output_memory() {
if (_outputs[0] && _network.get_engine().is_the_same_buffer(output_memory(), input_memory()))
return;
+ // Can_be_optimized nodes are allocating from memory_pool too. In this case,
+ // we need release the legacy output memory from memory pool explicitly.
+ if (static_cast<bool>(_outputs[0]) &&
+ _node->get_program().get_config().get_property(ov::intel_gpu::enable_memory_pool)) {
+ _network.get_memory_pool().release_memory(_outputs[0].get(), _node->get_unique_id(), _node->id(), _network.get_id());
+ }
_outputs[0] = _network.get_engine().reinterpret_buffer(input_memory(), _impl_params->get_output_layout());
_mem_allocated = false;
}
diff --git a/src/plugins/intel_gpu/src/graph/gather.cpp b/src/plugins/intel_gpu/src/graph/gather.cpp
index b4fdc8b2f81..6a361563653 100644
--- a/src/plugins/intel_gpu/src/graph/gather.cpp
+++ b/src/plugins/intel_gpu/src/graph/gather.cpp
@@ -147,6 +147,12 @@ void gather_inst::update_output_memory() {
GPU_DEBUG_TRACE_DETAIL << id() << " : update_output_memory with mem of input " << get_node().get_dependency(0).id()
<< " : " << input_memory_ptr()->buffer_ptr() << std::endl;
+ // Can_be_optimized nodes are allocating from memory_pool too. In this case,
+ // we need release the legacy output memory from memory pool explicitly.
+ if (static_cast<bool>(_outputs[0]) &&
+ _node->get_program().get_config().get_property(ov::intel_gpu::enable_memory_pool)) {
+ _network.get_memory_pool().release_memory(_outputs[0].get(), _node->get_unique_id(), _node->id(), _network.get_id());
+ }
_outputs[0] = input_memory_ptr();
_mem_allocated = false;
}
diff --git a/src/plugins/intel_gpu/src/graph/include/pass_manager.h b/src/plugins/intel_gpu/src/graph/include/pass_manager.h
index 8b1a5b12aad..281f95a8926 100644
--- a/src/plugins/intel_gpu/src/graph/include/pass_manager.h
+++ b/src/plugins/intel_gpu/src/graph/include/pass_manager.h
@@ -282,13 +282,35 @@ public:
class memory_dependency_pass : public base_pass {
public:
explicit memory_dependency_pass(const std::string& pass_name) : base_pass(pass_name) {}
+
+ // Program node with can_be_optimized true could also allocate from memory pool during runtime, if it cannot be
+ // optimized out (with inst_impl.can_be_optimized false).
+ // If it is optimized out, alternatively, it could reuse the memory from its parent (e.g. reshape) or chilren (e.g. concat).
+ // The memory dependency pass need consider both situations, by iteratively referencing to all nodes that can_be_optimized until
+ // it meets the first node with can_be_optimize==false along the searching path.
+ // For example, in such a subgraph like -
+ // Node1 (can_be_optimized false) -> Node2 (can_be_optimized false) -> Reshape (skippable true) -> Permute (skippable true)
+ // -> Node3 (can_be_optimized false).
+ // Since Reshape MAY or MAY NOT be optimized out in runtime, Permute should be memory-dependent to all of its two predecessors. Otherwise
+ // Permute may allocate from the same block of Node2 and override its input; Similarly, since Reshape and Permute MAY or MAY NOT be optimized
+ // out in runtime, Node3 should be memory-dependent to all of its three predecessors, to avoid memory conflicting.
void add_memory_dependency(program_node* node, program_node* dep) {
- if (node->can_be_optimized() || !dep->can_be_optimized()) {
+ if (node->get_unique_id() == dep->get_unique_id()) {
+ return;
+ }
+
+ if ((node->can_be_optimized() && !node->is_runtime_skippable()) || !dep->can_be_optimized()) {
node->add_memory_dependency(static_cast<int32_t>(dep->get_unique_id()));
} else {
- if (node->id() == dep->id()) {
+ if (node->is_runtime_skippable() || dep->is_runtime_skippable()) {
+ node->add_memory_dependency(static_cast<int32_t>(dep->get_unique_id()));
+ for (const auto& subdep : dep->get_dependencies()) {
+ add_memory_dependency(node, subdep.first);
+ add_memory_dependency(subdep.first, node);
+ }
return;
}
+
for (const auto& subdep : dep->get_dependencies()) {
add_memory_dependency(node, subdep.first);
add_memory_dependency(subdep.first, node);
diff --git a/src/plugins/intel_gpu/src/graph/network.cpp b/src/plugins/intel_gpu/src/graph/network.cpp
index 59f3647f987..92d62782828 100644
--- a/src/plugins/intel_gpu/src/graph/network.cpp
+++ b/src/plugins/intel_gpu/src/graph/network.cpp
@@ -952,6 +952,10 @@ void network::execute_impl(const std::vector<event::ptr>& events) {
if (iters.empty() || iters.find(curr_iter) != iters.end()) {
GPU_DEBUG_COUT << "============================================================================" << std::endl;
GPU_DEBUG_COUT << "Start network execution (net_id : " << get_id() << ", iter :" << curr_iter << ")" << std::endl;
+ if (curr_iter == 0 && get_id() > 0) {
+ dump_memory_pool(debug_config->dump_memory_pool_path, curr_iter);
+ GPU_DEBUG_COUT << "============================================================================" << std::endl;
+ }
}
} else {
GPU_DEBUG_TRACE << "============================================================================" << std::endl;
@@ -1306,7 +1310,7 @@ void network::dump_memory_pool(std::string dump_path, int64_t curr_iter) {
auto get_variables_mem_size = [&](allocation_type type) -> size_t {
size_t mem_size = 0;
for (auto& var : get_variables()) {
- if (var.second->get_memory()->get_allocation_type() == type)
+ if (var.second->get_memory() && var.second->get_memory()->get_allocation_type() == type)
mem_size += var.second->get_actual_mem_size();
}
return mem_size;
diff --git a/src/plugins/intel_gpu/src/graph/non_max_suppression.cpp b/src/plugins/intel_gpu/src/graph/non_max_suppression.cpp
index 00fdc5dc6db..ba1cbbdf781 100644
--- a/src/plugins/intel_gpu/src/graph/non_max_suppression.cpp
+++ b/src/plugins/intel_gpu/src/graph/non_max_suppression.cpp
@@ -154,6 +154,12 @@ void non_max_suppression_gather_inst::update_output_memory() {
if (output_memory_ptr(i) != nullptr && _network.get_engine().is_the_same_buffer(output_memory(i), input_memory(i)))
return;
+ // Can_be_optimized nodes are allocating from memory_pool too. In this case,
+ // we need release the legacy output memory from memory pool explicitly.
+ if (static_cast<bool>(_outputs[i]) &&
+ _node->get_program().get_config().get_property(ov::intel_gpu::enable_memory_pool)) {
+ _network.get_memory_pool().release_memory(_outputs[i].get(), _node->get_unique_id(), _node->id(), _network.get_id());
+ }
_outputs[i] = {_network.get_engine().reinterpret_buffer(input_memory(i), _impl_params->get_output_layout(i))};
}
}
diff --git a/src/plugins/intel_gpu/src/graph/permute.cpp b/src/plugins/intel_gpu/src/graph/permute.cpp
index d73e6052fd5..2e4c792729f 100644
--- a/src/plugins/intel_gpu/src/graph/permute.cpp
+++ b/src/plugins/intel_gpu/src/graph/permute.cpp
@@ -143,6 +143,12 @@ void permute_inst::update_output_memory() {
GPU_DEBUG_TRACE_DETAIL << id() << " : update_output_memory with mem of input " << get_node().get_dependency(0).id()
<< " : " << input_memory_ptr()->buffer_ptr() << std::endl;
+ // Can_be_optimized nodes are allocating from memory_pool too. In this case,
+ // we need release the legacy output memory from memory pool explicitly.
+ if (static_cast<bool>(_outputs[0]) &&
+ _node->get_program().get_config().get_property(ov::intel_gpu::enable_memory_pool)) {
+ _network.get_memory_pool().release_memory(_outputs[0].get(), _node->get_unique_id(), _node->id(), _network.get_id());
+ }
_outputs = {_network.get_engine().reinterpret_buffer(input_memory(), _impl_params->get_output_layout())};
_mem_allocated = false;
}
diff --git a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
index b0f7c4db565..08f3ec622a8 100644
--- a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
+++ b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
@@ -2122,7 +2122,7 @@ memory::ptr primitive_inst::allocate_output(engine& _engine,
GPU_DEBUG_LOG << "[" << _node.id() << ": constant]" << std::endl;
return ov::intel_gpu::allocate_memory_evenif_zero_bytes(_engine, layout, alloc_type, reset);
}
- } else if (!_node.can_share_buffer() || _node.can_be_optimized() || _node.is_output()) {
+ } else if (!_node.can_share_buffer() || impl_params.can_be_optimized() || _node.is_output()) {
GPU_DEBUG_LOG << "[" << _node.id() << ": output]" << std::endl;
return ov::intel_gpu::allocate_memory_evenif_zero_bytes(_engine, layout, alloc_type, reset);
} else {
diff --git a/src/plugins/intel_gpu/src/graph/reorder.cpp b/src/plugins/intel_gpu/src/graph/reorder.cpp
index e322baeee95..93698432e73 100644
--- a/src/plugins/intel_gpu/src/graph/reorder.cpp
+++ b/src/plugins/intel_gpu/src/graph/reorder.cpp
@@ -284,6 +284,13 @@ void reorder_inst::update_output_memory() {
if (!dependencies().front().first->outputs_allocated())
return;
+ // Can_be_optimized nodes are allocating from memory_pool too. In this case,
+ // we need release the legacy output memory from memory pool explicitly.
+ if (static_cast<bool>(_outputs[0]) &&
+ _node->get_program().get_config().get_property(ov::intel_gpu::enable_memory_pool)) {
+ _network.get_memory_pool().release_memory(_outputs[0].get(), _node->get_unique_id(), _node->id(), _network.get_id());
+ }
+
if (requires_reinterpret()) {
_outputs[0] = _network.get_engine().reinterpret_buffer(input_memory(), get_output_layout());
} else {
diff --git a/src/plugins/intel_gpu/src/graph/reshape.cpp b/src/plugins/intel_gpu/src/graph/reshape.cpp
index 0830dc3e5b9..213a0aa175f 100644
--- a/src/plugins/intel_gpu/src/graph/reshape.cpp
+++ b/src/plugins/intel_gpu/src/graph/reshape.cpp
@@ -315,6 +315,13 @@ void reshape_inst::update_output_memory() {
if (node->get_program().is_new_shape_infer() && input_memory_ptr() == nullptr)
return;
OPENVINO_ASSERT(input_memory_ptr() != nullptr, "[GPU] Failed to reuse input in ", id(), " primitive: input memory was not allocated");
+
+ // Can_be_optimized nodes are allocating from memory_pool too. In this case,
+ // we need release the legacy output memory from memory pool explicitly.
+ if (static_cast<bool>(_outputs[0]) &&
+ _node->get_program().get_config().get_property(ov::intel_gpu::enable_memory_pool)) {
+ _network.get_memory_pool().release_memory(_outputs[0].get(), _node->get_unique_id(), _node->id(), _network.get_id());
+ }
_outputs = {_network.get_engine().reinterpret_buffer(input_memory(), _impl_params->get_output_layout())};
}
diff --git a/src/plugins/intel_gpu/src/graph/scatter_elements_update.cpp b/src/plugins/intel_gpu/src/graph/scatter_elements_update.cpp
index df9236a1547..1e901f991dd 100644
--- a/src/plugins/intel_gpu/src/graph/scatter_elements_update.cpp
+++ b/src/plugins/intel_gpu/src/graph/scatter_elements_update.cpp
@@ -70,6 +70,12 @@ void scatter_elements_update_inst::update_output_memory() {
if (_node != nullptr)
build_deps();
+ // Can_be_optimized nodes are allocating from memory_pool too. In this case,
+ // we need release the legacy output memory from memory pool explicitly.
+ if (static_cast<bool>(_outputs[0]) &&
+ _node->get_program().get_config().get_property(ov::intel_gpu::enable_memory_pool)) {
+ _network.get_memory_pool().release_memory(_outputs[0].get(), _node->get_unique_id(), _node->id(), _network.get_id());
+ }
_outputs = {_network.get_engine().reinterpret_buffer(input_memory(), _impl_params->get_output_layout())};
_mem_allocated = false;
}
diff --git a/src/plugins/intel_gpu/src/graph/scatter_nd_update.cpp b/src/plugins/intel_gpu/src/graph/scatter_nd_update.cpp
index 8f66889c25b..71ef4d05206 100644
--- a/src/plugins/intel_gpu/src/graph/scatter_nd_update.cpp
+++ b/src/plugins/intel_gpu/src/graph/scatter_nd_update.cpp
@@ -83,6 +83,12 @@ void scatter_nd_update_inst::update_output_memory() {
if (_node != nullptr)
build_deps();
+ // Can_be_optimized nodes are allocating from memory_pool too. In this case,
+ // we need release the legacy output memory from memory pool explicitly.
+ if (static_cast<bool>(_outputs[0]) &&
+ _node->get_program().get_config().get_property(ov::intel_gpu::enable_memory_pool)) {
+ _network.get_memory_pool().release_memory(_outputs[0].get(), _node->get_unique_id(), _node->id(), _network.get_id());
+ }
_outputs = {_network.get_engine().reinterpret_buffer(input_memory(), _impl_params->get_output_layout())};
_mem_allocated = false;
}
diff --git a/src/plugins/intel_gpu/src/graph/scatter_update.cpp b/src/plugins/intel_gpu/src/graph/scatter_update.cpp
index 59995341d83..8f9099572fa 100644
--- a/src/plugins/intel_gpu/src/graph/scatter_update.cpp
+++ b/src/plugins/intel_gpu/src/graph/scatter_update.cpp
@@ -62,6 +62,12 @@ void scatter_update_inst::update_output_memory() {
if (_node != nullptr)
build_deps();
+ // Can_be_optimized nodes are allocating from memory_pool too. In this case,
+ // we need release the legacy output memory from memory pool explicitly.
+ if (static_cast<bool>(_outputs[0]) &&
+ _node->get_program().get_config().get_property(ov::intel_gpu::enable_memory_pool)) {
+ _network.get_memory_pool().release_memory(_outputs[0].get(), _node->get_unique_id(), _node->id(), _network.get_id());
+ }
_outputs = {_network.get_engine().reinterpret_buffer(input_memory(), _impl_params->get_output_layout())};
_mem_allocated = false;
}
diff --git a/src/plugins/intel_gpu/src/graph/strided_slice.cpp b/src/plugins/intel_gpu/src/graph/strided_slice.cpp
index ab321edb961..9c33ec22b0b 100644
--- a/src/plugins/intel_gpu/src/graph/strided_slice.cpp
+++ b/src/plugins/intel_gpu/src/graph/strided_slice.cpp
@@ -201,6 +201,12 @@ void strided_slice_inst::update_output_memory() {
GPU_DEBUG_TRACE_DETAIL << id() << " : update_output_memory with mem of input " << get_node().get_dependency(0).id()
<< " : " << input_memory_ptr()->buffer_ptr() << std::endl;
+ // Can_be_optimized nodes are allocating from memory_pool too. In this case,
+ // we need release the legacy output memory from memory pool explicitly.
+ if (static_cast<bool>(_outputs[0]) &&
+ _node->get_program().get_config().get_property(ov::intel_gpu::enable_memory_pool)) {
+ _network.get_memory_pool().release_memory(_outputs[0].get(), _node->get_unique_id(), _node->id(), _network.get_id());
+ }
_outputs[0] = input_memory_ptr();
_mem_allocated = false;
}