Skip to content

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

[GPU] Optimize node's memdeps to reduce memory footprints. (#29237)

cecilia peng4c248f6130a3
diff --git a/src/plugins/intel_gpu/include/intel_gpu/graph/serialization/set_serializer.hpp b/src/plugins/intel_gpu/include/intel_gpu/graph/serialization/set_serializer.hpp
index 7391e1b126a..758322e28d3 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/graph/serialization/set_serializer.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/graph/serialization/set_serializer.hpp
@@ -55,7 +55,7 @@ public:
static void load(BufferType& buffer, std::unordered_set<T>& set) {
typename std::unordered_set<T>::size_type set_size = 0UL;
buffer >> set_size;
-
+ if (set.empty()) set.reserve(set_size);
for (long unsigned int i = 0; i < set_size; i++) {
T el;
buffer >> el;
diff --git a/src/plugins/intel_gpu/include/intel_gpu/runtime/memory_pool.hpp b/src/plugins/intel_gpu/include/intel_gpu/runtime/memory_pool.hpp
index 1d27eaf63ef..66146198302 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/runtime/memory_pool.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/runtime/memory_pool.hpp
@@ -26,6 +26,47 @@ class engine;
using primitive_id = std::string;
using memory_ptr = std::shared_ptr<memory>;
+template<typename Key, typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
+class memory_restricter {
+ private:
+ const std::unordered_set<Key, Hash, KeyEqual>* set1; // Const reference to immutable set
+ std::unordered_set<Key, Hash, KeyEqual> set2; // Internal mutable set
+
+ public:
+ memory_restricter() : set1(nullptr) {};
+
+ // Constructor to initialize with a const reference for set1
+ explicit memory_restricter(const std::unordered_set<Key, Hash, KeyEqual>* externalSet)
+ : set1(externalSet) {}
+
+ // Insert into set2 (set1 is read-only)
+ void insert(const Key& key) {
+ if (set1->find(key) == set1->end())
+ set2.insert(key);
+ }
+
+ // Check existence in either set
+ bool contains(const Key& key) const {
+ return set1->find(key) != set1->end() || set2.find(key) != set2.end();
+ }
+
+ // Total size of both sets
+ size_t size() const {
+ return set1->size() + set2.size();
+ }
+
+ // Check if both sets are empty
+ bool empty() const {
+ return set1->empty() && set2.empty();
+ }
+
+ // Iterate over both sets
+ void for_each(void(*func)(const Key&)) const {
+ for (const auto& key : set1) func(key);
+ for (const auto& key : set2) func(key);
+ }
+}; // end of memory_restricter
+
struct memory_user {
size_t _unique_id;
uint32_t _network_id;
@@ -112,7 +153,7 @@ struct padded_pool_comparer {
class memory_pool {
memory_ptr alloc_memory(const layout& layout, allocation_type type, bool reset = true);
- static bool has_conflict(const memory_set&, const std::unordered_set<size_t>&, uint32_t network_id);
+ static bool has_conflict(const memory_set&, const memory_restricter<uint32_t>&);
std::multimap<uint64_t, memory_record> _non_padded_pool;
std::map<layout, std::list<memory_record>, padded_pool_comparer> _padded_pool;
@@ -127,7 +168,7 @@ public:
const primitive_id& id,
size_t unique_id,
uint32_t network_id,
- const std::unordered_set<size_t>& restrictions,
+ const memory_restricter<uint32_t>& restrictions,
allocation_type type,
bool reusable = true,
bool reset = true,
@@ -137,7 +178,7 @@ public:
const primitive_id& prim_id,
size_t unique_id,
uint32_t network_id,
- const std::unordered_set<size_t>&,
+ const memory_restricter<uint32_t>&,
allocation_type type,
bool reset = true,
bool is_dynamic = false);
@@ -145,13 +186,8 @@ public:
const primitive_id& prim_id,
size_t unique_id,
uint32_t network_id,
- const std::unordered_set<size_t>& restrictions,
+ const memory_restricter<uint32_t>& restrictions,
allocation_type type);
- memory_ptr get_from_across_networks_pool(const layout& layout,
- const primitive_id& id,
- size_t unique_id,
- uint32_t network_id,
- allocation_type type);
void clear_pool_for_network(uint32_t network_id);
void release_memory(memory* memory, const size_t& unique_id, primitive_id prim_id, uint32_t network_id);
diff --git a/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_quantization.cpp b/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_quantization.cpp
index b3b507440b8..8890f7a21ba 100644
--- a/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_quantization.cpp
+++ b/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_quantization.cpp
@@ -288,10 +288,10 @@ void prepare_quantization::prepare_scale_shift_opt(program &p, quantize_node& qu
p.add_connection(in_shift_node, new_quantize_node);
p.add_connection(out_scale_node, new_quantize_node);
p.add_connection(out_shift_node, new_quantize_node);
- new_quantize_node.add_memory_dependency(in_scale_node.get_unique_id());
- new_quantize_node.add_memory_dependency(in_shift_node.get_unique_id());
- new_quantize_node.add_memory_dependency(out_scale_node.get_unique_id());
- new_quantize_node.add_memory_dependency(out_shift_node.get_unique_id());
+ new_quantize_node.add_memory_dependency(in_scale_node);
+ new_quantize_node.add_memory_dependency(in_shift_node);
+ new_quantize_node.add_memory_dependency(out_scale_node);
+ new_quantize_node.add_memory_dependency(out_shift_node);
p.get_processing_order().insert(&new_quantize_node, &in_shift_node);
p.get_processing_order().insert(&new_quantize_node, &in_scale_node);
p.get_processing_order().insert(&new_quantize_node, &out_shift_node);
diff --git a/src/plugins/intel_gpu/src/graph/graph_optimizer/skipped_branch_memory_dependencies.cpp b/src/plugins/intel_gpu/src/graph/graph_optimizer/skipped_branch_memory_dependencies.cpp
index e1243faa23f..53d4ae75a4e 100644
--- a/src/plugins/intel_gpu/src/graph/graph_optimizer/skipped_branch_memory_dependencies.cpp
+++ b/src/plugins/intel_gpu/src/graph/graph_optimizer/skipped_branch_memory_dependencies.cpp
@@ -19,7 +19,7 @@ void skipped_branch_memory_dependencies::run(program& p) {
while (itrB != processing_order.end()) {
auto& nodeB = *itrB;
auto itrA = ++itrB;
- if (nodeB->is_constant())
+ if (!nodeB->may_use_mempool())
continue;
if (nodeB->get_users().size() == 0)
continue;
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 b79c652fc26..6106d44c5ca 100644
--- a/src/plugins/intel_gpu/src/graph/include/pass_manager.h
+++ b/src/plugins/intel_gpu/src/graph/include/pass_manager.h
@@ -322,10 +322,10 @@ public:
if ((!dep->can_be_optimized() || !dep->is_runtime_skippable()) && ((node->can_be_optimized() && !node->is_runtime_skippable())
|| !dep->can_be_optimized())) {
- node->add_memory_dependency(static_cast<int32_t>(dep->get_unique_id()));
+ node->add_memory_dependency(*dep);
} else {
if (node->is_runtime_skippable() || dep->is_runtime_skippable() || dep->can_be_optimized()) {
- node->add_memory_dependency(static_cast<int32_t>(dep->get_unique_id()));
+ node->add_memory_dependency(*dep);
}
for (const auto& subdep : dep->get_dependencies()) {
diff --git a/src/plugins/intel_gpu/src/graph/include/primitive_inst.h b/src/plugins/intel_gpu/src/graph/include/primitive_inst.h
index 70bfeb26d64..53f3e6138b0 100644
--- a/src/plugins/intel_gpu/src/graph/include/primitive_inst.h
+++ b/src/plugins/intel_gpu/src/graph/include/primitive_inst.h
@@ -222,7 +222,7 @@ public:
_users = _network.get_primitives(users);
}
- const std::unordered_set<size_t>& get_runtime_memory_dependencies() const { return _runtime_memory_dependencies; }
+ const memory_restricter<uint32_t>& get_runtime_memory_dependencies() const { return _runtime_memory_dependencies; }
const kernel_impl_params* get_impl_params() const { return _impl_params.get(); }
// return pointer to const to prevent arbitrary 'execute' call -> use primitive_inst.execute() instead
@@ -307,7 +307,7 @@ public:
memory_pool& pool,
const program_node& _node,
const kernel_impl_params& impl_params,
- const std::unordered_set<size_t>& memory_dependencies,
+ const memory_restricter<uint32_t>& memory_dependencies,
uint32_t net_id,
bool is_internal,
size_t idx = 0,
@@ -379,7 +379,7 @@ protected:
std::vector<primitive_inst*> _exec_deps;
// List of primitive ids that this primitive can't share memory buffers with
- std::unordered_set<size_t> _runtime_memory_dependencies;
+ memory_restricter<uint32_t> _runtime_memory_dependencies;
// This is sub-network generated on demand to execute unfused primitives sequence instead of single fused primitive
// Needed for dynamic path only, as fusion in some cases may be illegal, but it can't be checked on program build phase,
diff --git a/src/plugins/intel_gpu/src/graph/include/program_node.h b/src/plugins/intel_gpu/src/graph/include/program_node.h
index b6a62136f18..2fcc9fce53a 100644
--- a/src/plugins/intel_gpu/src/graph/include/program_node.h
+++ b/src/plugins/intel_gpu/src/graph/include/program_node.h
@@ -12,6 +12,7 @@
#include "intel_gpu/graph/fused_primitive_desc.hpp"
#include "intel_gpu/graph/kernel_impl_params.hpp"
#include "intel_gpu/primitives/reorder.hpp"
+#include "intel_gpu/primitives/read_value.hpp"
#include "intel_gpu/runtime/utils.hpp"
#include <set>
@@ -207,9 +208,15 @@ public:
size_t get_dependency_index(const program_node& node) const;
size_t get_user_index(const program_node& node) const;
- std::unordered_set<size_t> get_memory_dependencies() const;
- void add_memory_dependency(size_t);
+ const std::unordered_set<uint32_t>& get_memory_dependencies() const;
+
void add_memory_dependency(std::vector<size_t>);
+ void add_memory_dependency(const program_node& node);
+
+ // At least the following scenarios are not allocating from memory pool:
+ // 1. constant nodes
+ // 2. read_value nodes that are optimized out to reuse from Variables.
+ bool may_use_mempool() const { return !(is_constant() || (is_type<read_value>() && optimized)); }
template <class PType>
bool have_user_with_type() const {
@@ -497,7 +504,7 @@ protected:
std::list<program_node*> users;
// list of primitives that can reuse same memory buffers due to execution order conflicts
- std::unordered_set<size_t> memory_dependencies;
+ std::unordered_set<uint32_t> memory_dependencies;
impl_types impl_type = impl_types::any;
impl_types forced_impl_type = impl_types::any;
diff --git a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
index 0d880999f1e..de7ab36cf76 100644
--- a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
+++ b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
@@ -157,7 +157,7 @@ static memory::ptr get_memory_from_pool(engine& _engine,
const layout& layout,
allocation_type type,
bool reusable_across_network,
- const std::unordered_set<size_t>& memory_dependencies,
+ const memory_restricter<uint32_t>& memory_dependencies,
bool reset = true,
memory* curr_memory = nullptr) {
OPENVINO_ASSERT(!layout.is_dynamic() || layout.has_upper_bound(),
@@ -1340,7 +1340,7 @@ void primitive_inst::do_runtime_skip_reorder() {
update_memory_dependencies = [&](std::vector<primitive_inst*> users) {
for (auto& user : users) {
GPU_DEBUG_TRACE_DETAIL << "[do runtime skip reorder] add " << id() << " to restriction list of " << user->id() << std::endl;
- user->_runtime_memory_dependencies.insert(get_node().get_unique_id());
+ user->_runtime_memory_dependencies.insert(static_cast<uint32_t>(get_node().get_unique_id()));
if (user->can_be_optimized())
update_memory_dependencies(user->get_user_insts());
}
@@ -2085,7 +2085,7 @@ primitive_inst::primitive_inst(network & network, program_node const& node, bool
, _use_shared_kernels(node.get_program().get_config().get_enable_kernels_reuse())
, _impl_params(node.get_kernel_impl_params())
, _impl(node.get_selected_impl() ? node.get_selected_impl()->clone() : nullptr)
- , _runtime_memory_dependencies(node.get_memory_dependencies())
+ , _runtime_memory_dependencies(&node.get_memory_dependencies())
, _outputs({})
, _reordered_weights_cache(network.get_weights_cache_capacity())
, _is_dynamic(node.is_dynamic())
@@ -2390,7 +2390,7 @@ memory::ptr primitive_inst::allocate_output(engine& _engine,
memory_pool& pool,
const program_node& _node,
const kernel_impl_params& impl_params,
- const std::unordered_set<size_t>& memory_dependencies,
+ const memory_restricter<uint32_t>& memory_dependencies,
uint32_t net_id,
bool is_internal,
size_t idx,
diff --git a/src/plugins/intel_gpu/src/graph/program.cpp b/src/plugins/intel_gpu/src/graph/program.cpp
index c6489ac8b25..1933cbe485e 100644
--- a/src/plugins/intel_gpu/src/graph/program.cpp
+++ b/src/plugins/intel_gpu/src/graph/program.cpp
@@ -748,7 +748,7 @@ void program::prepare_memory_dependencies() {
if (!_config.get_enable_memory_pool())
return;
for (auto& node : get_processing_order()) {
- node->add_memory_dependency(node->get_unique_id());
+ node->add_memory_dependency(*node);
}
apply_opt_pass<basic_memory_dependencies>();
apply_opt_pass<skipped_branch_memory_dependencies>();
@@ -766,7 +766,7 @@ std::string program::get_memory_dependencies_string() const {
.append("(unique_id:")
.append(std::to_string(node->get_unique_id()))
.append(") restricted list: ");
- for (auto it : node->get_memory_dependencies())
+ for (const auto& it : node->get_memory_dependencies())
mem_dep = mem_dep.append(std::to_string(it)).append(",");
mem_dep = mem_dep.append("\n");
}
@@ -1715,7 +1715,7 @@ std::pair<int64_t, int64_t> program::get_estimated_device_mem_usage() {
pool,
*node,
*node->get_kernel_impl_params(),
- node->get_memory_dependencies(),
+ memory_restricter<uint32_t>(&node->get_memory_dependencies()),
0,
false,
0,
diff --git a/src/plugins/intel_gpu/src/graph/program_node.cpp b/src/plugins/intel_gpu/src/graph/program_node.cpp
index 5f6c065e571..49340058bb6 100644
--- a/src/plugins/intel_gpu/src/graph/program_node.cpp
+++ b/src/plugins/intel_gpu/src/graph/program_node.cpp
@@ -195,12 +195,17 @@ void program_node::remove_dependency(size_t idx) {
dependencies.erase(dependencies.begin() + idx);
}
-std::unordered_set<size_t> program_node::get_memory_dependencies() const { return memory_dependencies; }
-
-void program_node::add_memory_dependency(size_t prim) { memory_dependencies.insert(prim); }
+const std::unordered_set<uint32_t>& program_node::get_memory_dependencies() const { return memory_dependencies; }
void program_node::add_memory_dependency(std::vector<size_t> prim_list) {
- memory_dependencies.insert(prim_list.begin(), prim_list.end());
+ for (size_t val : prim_list) {
+ memory_dependencies.insert(static_cast<uint32_t>(val));
+ }
+}
+
+void program_node::add_memory_dependency(const program_node& dep) {
+ if (dep.may_use_mempool() && may_use_mempool())
+ memory_dependencies.insert(static_cast<uint32_t>(dep.get_unique_id()));
}
std::unique_ptr<json_composite> program_node::desc_to_json() const {
diff --git a/src/plugins/intel_gpu/src/runtime/memory_pool.cpp b/src/plugins/intel_gpu/src/runtime/memory_pool.cpp
index 809471d4c55..81e629871e1 100644
--- a/src/plugins/intel_gpu/src/runtime/memory_pool.cpp
+++ b/src/plugins/intel_gpu/src/runtime/memory_pool.cpp
@@ -37,10 +37,9 @@ memory::ptr memory_pool::alloc_memory(const layout& layout, allocation_type type
memory_pool::~memory_pool() {}
bool memory_pool::has_conflict(const memory_set& mem_cand,
- const std::unordered_set<size_t>& restrictions,
- uint32_t b_network_id) {
+ const memory_restricter<uint32_t>& restrictions) {
for (const auto& mem_usr : mem_cand) {
- if (restrictions.find(mem_usr._unique_id) != restrictions.end())
+ if (restrictions.contains(static_cast<uint32_t>(mem_usr._unique_id)))
return true;
}
return false;
@@ -155,7 +154,7 @@ memory::ptr memory_pool::get_from_non_padded_pool(const layout& layout,
const primitive_id& prim_id,
size_t unique_id,
uint32_t network_id,
- const std::unordered_set<size_t>& restrictions,
+ const memory_restricter<uint32_t>& restrictions,
allocation_type type,
bool reset,
bool is_dynamic) {
@@ -169,7 +168,7 @@ memory::ptr memory_pool::get_from_non_padded_pool(const layout& layout,
layout.format != format::fs_b_yx_fsv32 &&
((layout.format != format::b_fs_yx_fsv32 && layout.format != format::b_fs_zyx_fsv32) ||
(layout.feature() % 32 == 0)) &&
- !has_conflict(it->second._users, restrictions, network_id))) {
+ !has_conflict(it->second._users, restrictions))) {
it->second._users.insert(memory_user(MEM_USER(unique_id, network_id, prim_id, layout_bytes_count)));
auto ret_mem = _engine->reinterpret_buffer(*it->second._memory, layout);
GPU_DEBUG_CODE(ret_mem->from_memory_pool = true);
@@ -201,7 +200,7 @@ memory::ptr memory_pool::get_from_padded_pool(const layout& layout,
const primitive_id& prim_id,
size_t unique_id,
uint32_t network_id,
- const std::unordered_set<size_t>& restrictions,
+ const memory_restricter<uint32_t>& restrictions,
allocation_type type) {
auto first_level_cache = _padded_pool.find(layout);
if (first_level_cache != _padded_pool.end()) {
@@ -215,7 +214,7 @@ memory::ptr memory_pool::get_from_padded_pool(const layout& layout,
layout.batch() <= rec_list._memory->get_layout().batch() &&
rec_list._memory->get_layout().format != format::fs_b_yx_fsv32 &&
layout.format != format::fs_b_yx_fsv32 &&
- !has_conflict(rec_list._users, restrictions, network_id)) {
+ !has_conflict(rec_list._users, restrictions)) {
auto ret_mem = _engine->reinterpret_buffer(*(rec_list._memory), layout);
rec_list._users.insert({MEM_USER(unique_id, network_id, prim_id, ret_mem->size())});
GPU_DEBUG_CODE(ret_mem->from_memory_pool = true);
@@ -254,38 +253,6 @@ memory::ptr memory_pool::get_from_padded_pool(const layout& layout,
return mem;
}
-/*
- This is not reusable within one network or it's internal micro networks. But we can use this memory records
- between networks.
- */
-memory::ptr memory_pool::get_from_across_networks_pool(const layout& layout,
- const primitive_id& prim_id,
- size_t unique_id,
- uint32_t network_id,
- allocation_type type) {
- const auto layout_bytes_count = layout.bytes_count();
- auto it = _no_reusable_pool.lower_bound(layout_bytes_count);
-
- while (it != _no_reusable_pool.end()) {
- if (it->second._network_id != network_id &&
- it->second._type == type) { // don't use non reusable resources within the same network
- if (!has_conflict(it->second._users, {}, network_id)) {
- it->second._users.insert(memory_user(MEM_USER(unique_id, network_id, prim_id, layout_bytes_count)));
- auto ret_mem = _engine->reinterpret_buffer(*it->second._memory, layout);
- GPU_DEBUG_CODE(ret_mem->from_memory_pool = true);
- return ret_mem;
- }
- }
- ++it;
- }
- auto mem = alloc_memory(layout, type);
- {
- _no_reusable_pool.emplace(layout_bytes_count,
- memory_record({{MEM_USER(unique_id, network_id, prim_id, layout_bytes_count)}}, mem, network_id, type));
- }
- return mem;
-}
-
memory::ptr memory_pool::get_memory(const layout& layout, allocation_type type, bool reset) {
return alloc_memory(layout, type, reset);
}
@@ -294,7 +261,7 @@ memory::ptr memory_pool::get_memory(const layout& layout,
const primitive_id& prim_id,
const size_t unique_id,
uint32_t network_id,
- const std::unordered_set<size_t>& restrictions,
+ const memory_restricter<uint32_t>& restrictions,
allocation_type type,
bool reusable_across_network,
bool reset,
diff --git a/src/plugins/intel_gpu/tests/unit/dynamic_execution/skip_redundant_reorder_at_runtime.cpp b/src/plugins/intel_gpu/tests/unit/dynamic_execution/skip_redundant_reorder_at_runtime.cpp
index 2b34c51f0f0..41faec6d66d 100644
--- a/src/plugins/intel_gpu/tests/unit/dynamic_execution/skip_redundant_reorder_at_runtime.cpp
+++ b/src/plugins/intel_gpu/tests/unit/dynamic_execution/skip_redundant_reorder_at_runtime.cpp
@@ -161,9 +161,9 @@ TEST(skip_reorder_at_runtime, correct_memory_reuse) {
auto reshape_memory_deps = reshape_inst->get_runtime_memory_dependencies();
auto fc_unique_id = network.get_primitive("fc")->get_node().get_unique_id();
- ASSERT_TRUE(reshape_memory_deps.find(fc_unique_id) != reshape_memory_deps.end());
+ ASSERT_TRUE(reshape_memory_deps.contains(static_cast<uint32_t>(fc_unique_id)));
auto reorder_fsv16_memory_deps = reorder_fsv16_inst->get_runtime_memory_dependencies();
- ASSERT_TRUE(reorder_fsv16_memory_deps.find(fc_unique_id) != reorder_fsv16_memory_deps.end());
+ ASSERT_TRUE(reorder_fsv16_memory_deps.contains(static_cast<uint32_t>(fc_unique_id)));
}
} // memory_realloc_tests