Skip to content

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

[GPU] Bound memory pool searches to restore decode latency (#39642)

Aleksandr Vorona1f0ea08348c
diff --git a/src/plugins/intel_gpu/src/runtime/memory_pool.cpp b/src/plugins/intel_gpu/src/runtime/memory_pool.cpp
index 656bce776ea..8a0762a7d3a 100644
--- a/src/plugins/intel_gpu/src/runtime/memory_pool.cpp
+++ b/src/plugins/intel_gpu/src/runtime/memory_pool.cpp
@@ -55,14 +55,21 @@ void memory_pool::release_memory(memory* mem, const size_t& unique_id, primitive
}
auto type = mem->get_allocation_type();
const auto _layout_bytes_count = _layout.bytes_count();
+ const auto get_mem_handle = [](const memory& buffer) {
+ if (memory_capabilities::is_usm_type(buffer.get_allocation_type())) {
+ if (auto* ptr = buffer.buffer_ptr())
+ return ptr;
+ }
+ return buffer.get_internal_params().mem;
+ };
+ auto* const mem_handle = get_mem_handle(*mem);
{
auto it = _non_padded_pool.lower_bound(_layout_bytes_count);
+ const auto mem_tracker = mem->get_mem_tracker();
- while (it != _non_padded_pool.end()) {
- if (it->second._network_id == network_id &&
- it->second._type == type &&
- it->second._memory->get_internal_params().mem == mem->get_internal_params().mem) {
+ while (it != _non_padded_pool.end() && (!mem_tracker || it->first <= mem_tracker->size())) {
+ if (it->second._network_id == network_id && it->second._type == type && get_mem_handle(*it->second._memory) == mem_handle) {
auto user_it = it->second._users.find({MEM_USER(unique_id, network_id, prim_id, _layout_bytes_count)});
// normally there should be only one entry
if (user_it != it->second._users.end()) {
@@ -96,9 +103,7 @@ void memory_pool::release_memory(memory* mem, const size_t& unique_id, primitive
auto list_itr = list.begin();
while (list_itr != list.end()) {
- if (list_itr->_memory.get()->get_internal_params().mem == mem->get_internal_params().mem &&
- list_itr->_network_id == network_id &&
- list_itr->_type == type) {
+ if (get_mem_handle(*list_itr->_memory) == mem_handle && list_itr->_network_id == network_id && list_itr->_type == type) {
auto user_it = list_itr->_users.find({MEM_USER(unique_id, network_id, prim_id, _layout_bytes_count)});
// normally there should be only one entry
@@ -133,9 +138,7 @@ void memory_pool::release_memory(memory* mem, const size_t& unique_id, primitive
#ifdef GPU_DEBUG_CONFIG
GPU_DEBUG_IF(_config.get_dump_memory_pool()) {
auto iter = std::find_if(_no_reusable_mems.begin(), _no_reusable_mems.end(), [&](const cldnn::memory_record& r) {
- return (network_id == r._network_id
- && type == r._type
- && mem->get_internal_params().mem == r._memory->get_internal_params().mem);
+ return (network_id == r._network_id && type == r._type && mem_handle == get_mem_handle(*r._memory));
});
if (iter != _no_reusable_mems.end()) {
GPU_DEBUG_IF(_config.get_dump_memory_pool()) {
@@ -180,20 +183,17 @@ memory::ptr memory_pool::get_from_non_padded_pool(const layout& layout,
#endif // ENABLE_ONEDNN_FOR_GPU
auto it = _non_padded_pool.lower_bound(layout_bytes_count);
while (it != _non_padded_pool.end()) {
+ if (is_dynamic && !(layout_bytes_count > it->first * _mem_pool_util_threshold))
+ break;
const auto& mem_layout = it->second._memory->get_layout();
- if ((!is_dynamic || (layout_bytes_count > mem_layout.bytes_count() * _mem_pool_util_threshold)) &&
- (it->second._network_id == network_id &&
- it->second._type == type &&
- mem_layout.format != format::fs_b_yx_fsv32 &&
+ if (it->second._network_id == network_id && it->second._type == type && mem_layout.format != format::fs_b_yx_fsv32 &&
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)) &&
+ ((layout.format != format::b_fs_yx_fsv32 && layout.format != format::b_fs_zyx_fsv32) || (layout.feature() % 32 == 0)) &&
#ifdef ENABLE_ONEDNN_FOR_GPU
(!format::is_blocked(layout.format) || layout.feature() % f_block_size == 0 ||
- (mem_layout.format == layout.format &&
- mem_layout.feature() % f_block_size == layout.feature() % f_block_size)) &&
+ (mem_layout.format == layout.format && mem_layout.feature() % f_block_size == layout.feature() % f_block_size)) &&
#endif // ENABLE_ONEDNN_FOR_GPU
- !has_conflict(it->second._users, restrictions))) {
+ !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);
ret_mem->from_memory_pool = true;
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/memory_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/memory_test.cpp
index 21908f7c238..730fcf071c3 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/memory_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/memory_test.cpp
@@ -2,22 +2,25 @@
// SPDX-License-Identifier: Apache-2.0
//
-#include "test_utils.h"
-#include "random_generator.hpp"
+#include <fully_connected_inst.h>
+#include <primitive_inst.h>
-#include <intel_gpu/primitives/input_layout.hpp>
#include <intel_gpu/primitives/activation.hpp>
-#include <intel_gpu/primitives/pooling.hpp>
#include <intel_gpu/primitives/concatenation.hpp>
-#include <intel_gpu/primitives/data.hpp>
-#include "intel_gpu/primitives/fully_connected.hpp"
-#include <intel_gpu/primitives/reshape.hpp>
#include <intel_gpu/primitives/crop.hpp>
+#include <intel_gpu/primitives/data.hpp>
#include <intel_gpu/primitives/eltwise.hpp>
#include <intel_gpu/primitives/grid_sample.hpp>
+#include <intel_gpu/primitives/input_layout.hpp>
+#include <intel_gpu/primitives/pooling.hpp>
#include <intel_gpu/primitives/reorder.hpp>
-#include <primitive_inst.h>
-#include <fully_connected_inst.h>
+#include <intel_gpu/primitives/reshape.hpp>
+#include <intel_gpu/runtime/memory_pool.hpp>
+#include <limits>
+
+#include "intel_gpu/primitives/fully_connected.hpp"
+#include "random_generator.hpp"
+#include "test_utils.h"
using namespace cldnn;
using namespace ::tests;
@@ -943,6 +946,90 @@ TEST_F(memory_pool, dynamic_mem_reuse_for_null_sel_impl) {
this->test_dynamic_mem_reuse_for_null_sel_impl();
}
+TEST_F(memory_pool, dynamic_reuse_respects_utilization_boundary_and_restrictions) {
+ auto& engine = get_test_engine();
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::mem_pool_util_threshold(0.5f));
+ cldnn::memory_pool pool(engine, config);
+ const auto alloc_type = engine.get_preferred_memory_allocation_type();
+ const layout large_layout({1, 1, 1, 256}, data_types::i32, format::bfyx);
+ const layout eligible({1, 1, 1, 192}, data_types::i32, format::bfyx);
+ const layout boundary({1, 1, 1, 128}, data_types::i32, format::bfyx);
+
+ memory_restricter<uint32_t> none;
+ auto a = pool.get_memory(large_layout, "a", 11, 0, none, alloc_type);
+ memory_restricter<uint32_t> restricted;
+ restricted.insert(11);
+ auto b = pool.get_memory(large_layout, "b", 12, 0, restricted, alloc_type);
+ ASSERT_FALSE(engine.is_the_same_buffer(*a, *b));
+
+ auto reused = pool.get_memory(eligible, "reused", 13, 0, restricted, alloc_type, true, false, true);
+ EXPECT_TRUE(engine.is_the_same_buffer(*b, *reused));
+ auto too_small = pool.get_memory(boundary, "boundary", 14, 0, none, alloc_type, true, false, true);
+ EXPECT_FALSE(engine.is_the_same_buffer(*a, *too_small));
+ EXPECT_FALSE(engine.is_the_same_buffer(*b, *too_small));
+}
+
+TEST_F(memory_pool, dynamic_reuse_edge_thresholds_and_static_reuse) {
+ auto& engine = get_test_engine();
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::mem_pool_util_threshold(0.0f));
+ cldnn::memory_pool pool(engine, config);
+ const auto alloc_type = engine.get_preferred_memory_allocation_type();
+ const layout large_layout({1, 1, 1, 256}, data_types::i32, format::bfyx);
+ const layout small_layout({1, 1, 1, 16}, data_types::i32, format::bfyx);
+ memory_restricter<uint32_t> none;
+ auto a = pool.get_memory(large_layout, "a", 11, 0, none, alloc_type);
+ auto dynamic = pool.get_memory(small_layout, "dynamic", 12, 0, none, alloc_type, true, false, true);
+ EXPECT_TRUE(engine.is_the_same_buffer(*a, *dynamic));
+
+ auto static_config = get_test_default_config(engine);
+ static_config.set_property(ov::intel_gpu::mem_pool_util_threshold(1.0f));
+ cldnn::memory_pool static_pool(engine, static_config);
+ auto b = static_pool.get_memory(large_layout, "b", 21, 0, none, alloc_type);
+ auto reused = static_pool.get_memory(small_layout, "static", 22, 0, none, alloc_type, true, false, false);
+ EXPECT_TRUE(engine.is_the_same_buffer(*b, *reused));
+
+ auto unordered_config = get_test_default_config(engine);
+ unordered_config.set_property(ov::intel_gpu::mem_pool_util_threshold(std::numeric_limits<float>::quiet_NaN()));
+ cldnn::memory_pool unordered_pool(engine, unordered_config);
+ auto c = unordered_pool.get_memory(large_layout, "c", 31, 0, none, alloc_type);
+ auto not_reused = unordered_pool.get_memory(small_layout, "unordered", 32, 0, none, alloc_type, true, false, true);
+ EXPECT_FALSE(engine.is_the_same_buffer(*c, *not_reused));
+}
+
+TEST_F(memory_pool, release_reinterpreted_buffers_preserves_other_users) {
+ auto& engine = get_test_engine();
+ for (auto type : {allocation_type::cl_mem, allocation_type::usm_host, allocation_type::usm_device, allocation_type::usm_shared}) {
+ if (!engine.supports_allocation(type))
+ continue;
+ SCOPED_TRACE(::testing::Message() << type);
+ auto config = get_test_default_config(engine);
+ cldnn::memory_pool pool(engine, config);
+ const layout large_layout({1, 1, 1, 64}, data_types::i32, format::bfyx);
+ const layout small_layout({1, 1, 1, 16}, data_types::i32, format::bfyx);
+ memory_restricter<uint32_t> none;
+ auto a = pool.get_memory(large_layout, "a", 11, 0, none, type);
+ auto view = pool.get_memory(small_layout, "view", 12, 0, none, type);
+ ASSERT_TRUE(engine.is_the_same_buffer(*a, *view));
+ pool.release_memory(view.get(), 12, "view", 0);
+ EXPECT_EQ(pool.get_non_padded_pool_size(), 1);
+
+ memory_restricter<uint32_t> restricted;
+ restricted.insert(11);
+ auto b = pool.get_memory(large_layout, "b", 13, 0, restricted, type);
+ ASSERT_FALSE(engine.is_the_same_buffer(*a, *b));
+ pool.release_memory(a.get(), 11, "a", 0);
+ EXPECT_EQ(pool.get_non_padded_pool_size(), 1);
+ auto reused = pool.get_memory(large_layout, "reused", 14, 0, none, type);
+ EXPECT_TRUE(engine.is_the_same_buffer(*b, *reused));
+ pool.release_memory(b.get(), 13, "b", 0);
+ EXPECT_EQ(pool.get_non_padded_pool_size(), 1);
+ pool.release_memory(reused.get(), 14, "reused", 0);
+ EXPECT_EQ(pool.get_non_padded_pool_size(), 0);
+ }
+}
+
#ifdef ENABLE_ONEDNN_FOR_GPU
TEST_F(memory_pool, test_static_reuse_unaligned_feature) {
this->test_static_reuse_unaligned_feature();