Skip to content

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

[GPU] Defer allocations of inputs (#35126)

Bartlomiej Filipekdbc544df4076
diff --git a/src/plugins/intel_gpu/src/graph/broadcast.cpp b/src/plugins/intel_gpu/src/graph/broadcast.cpp
index 516262657c6..4982916f44c 100644
--- a/src/plugins/intel_gpu/src/graph/broadcast.cpp
+++ b/src/plugins/intel_gpu/src/graph/broadcast.cpp
@@ -137,11 +137,15 @@ void broadcast_inst::on_execute() {
void broadcast_inst::update_output_memory() {
if (!can_be_optimized())
return;
- if (static_cast<bool>(_outputs[0]) && _network.get_engine().is_the_same_buffer(output_memory(), input_memory()))
- return;
build_deps();
+ if (input_memory_ptr() == nullptr)
+ return;
+
+ if (static_cast<bool>(_outputs[0]) && _network.get_engine().is_the_same_buffer(output_memory(), input_memory()))
+ return;
+
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,
diff --git a/src/plugins/intel_gpu/src/graph/crop.cpp b/src/plugins/intel_gpu/src/graph/crop.cpp
index 20c38342e48..368b21f378a 100644
--- a/src/plugins/intel_gpu/src/graph/crop.cpp
+++ b/src/plugins/intel_gpu/src/graph/crop.cpp
@@ -268,7 +268,7 @@ void crop_inst::update_output_memory() {
build_deps();
- if (get_node().get_program().is_new_shape_infer() && input_memory_ptr() == nullptr)
+ if (input_memory_ptr() == nullptr)
return;
if (_outputs[0] && get_network().get_engine().is_the_same_buffer(output_memory(), input_memory()))
diff --git a/src/plugins/intel_gpu/src/graph/dynamic_quantize.cpp b/src/plugins/intel_gpu/src/graph/dynamic_quantize.cpp
index de6e2f48585..68d4d01c211 100644
--- a/src/plugins/intel_gpu/src/graph/dynamic_quantize.cpp
+++ b/src/plugins/intel_gpu/src/graph/dynamic_quantize.cpp
@@ -143,14 +143,17 @@ void dynamic_quantize_inst::update_output_memory() {
if (!can_be_optimized())
return;
+ if (_node != nullptr)
+ build_deps();
+
+ if (input_memory_ptr() == nullptr)
+ return;
+
if (static_cast<bool>(_outputs[0])
&& _network.get_engine().is_the_same_buffer(output_memory(), input_memory())
&& output_memory().get_layout().identical(get_output_layout()))
return;
- if (_node != nullptr)
- build_deps();
-
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,
diff --git a/src/plugins/intel_gpu/src/graph/gather.cpp b/src/plugins/intel_gpu/src/graph/gather.cpp
index 286e4c53597..04582cab7ac 100644
--- a/src/plugins/intel_gpu/src/graph/gather.cpp
+++ b/src/plugins/intel_gpu/src/graph/gather.cpp
@@ -141,11 +141,15 @@ void gather_inst::on_execute() {
void gather_inst::update_output_memory() {
if (!can_be_optimized())
return;
- if (static_cast<bool>(_outputs[0]) && _network.get_engine().is_the_same_buffer(output_memory(), input_memory()))
- return;
build_deps();
+ if (input_memory_ptr() == nullptr)
+ return;
+
+ if (static_cast<bool>(_outputs[0]) && _network.get_engine().is_the_same_buffer(output_memory(), input_memory()))
+ return;
+
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,
diff --git a/src/plugins/intel_gpu/src/graph/input_layout.cpp b/src/plugins/intel_gpu/src/graph/input_layout.cpp
index 6f0eb046254..5595e521e7f 100644
--- a/src/plugins/intel_gpu/src/graph/input_layout.cpp
+++ b/src/plugins/intel_gpu/src/graph/input_layout.cpp
@@ -11,28 +11,30 @@
#include <algorithm>
namespace {
-bool has_optimized_users(cldnn::input_layout_node const& node) {
- for (auto& user : node.get_users()) {
- if (user->can_be_optimized()) {
- return true;
- }
+
+bool requires_eager_input_allocation(const cldnn::input_layout_node& node) {
+ if (node.is_dynamic()) {
+ return false;
}
- return false;
+ const auto& out_layout = node.get_output_layout();
+
+ // Scalars assume input memory is always materialized. Blocked inputs still have
+ // initialization paths that depend on an eager placeholder allocation.
+ return out_layout.count() <= 1 || cldnn::format::is_blocked(out_layout.format);
}
} // namespace
namespace cldnn {
GPU_DEFINE_PRIMITIVE_TYPE_ID(input_layout)
-input_layout_node::typed_program_node(const std::shared_ptr<input_layout> dprim, program& prog)
- : parent(dprim, prog) {
+input_layout_node::typed_program_node(const std::shared_ptr<input_layout> dprim, program& prog) : parent(dprim, prog) {
can_share_buffer(false);
}
-input_layout_inst::typed_primitive_inst(network& network, input_layout_node const& node)
- : parent(network, node, !node.is_dynamic() && (!network.is_internal() || has_optimized_users(node))) {
- _has_valid_input = false; // by default input for 'input_layout' is invalid as long as user doesn't call set_data
+input_layout_inst::typed_primitive_inst(network& network, const input_layout_node& node)
+ : parent(network, node, /*allocate_mem*/ requires_eager_input_allocation(node)) {
+ _has_valid_input = false; // by default input for 'input_layout' is invalid as long as user doesn't call set_data
}
event::ptr input_layout_inst::set_data(memory::ptr mem, bool need_to_check_memory_to_set) {
@@ -49,16 +51,27 @@ event::ptr input_layout_inst::set_data(memory::ptr mem, bool need_to_check_memor
// Allow to set dummy simple_attached_memory empty tensor as network input
if (mem->is_allocated_by(engine) || mem->get_layout().count() == 0) {
- OPENVINO_ASSERT(!_outputs.empty(), "[GPU] Can't set data for empty input memory");
+ if (_outputs.empty()) {
+ _outputs.resize(1);
+ }
+ if (_max_output_layout_count.empty()) {
+ _max_output_layout_count.resize(1);
+ }
+
+ // Do not use set_output_memory() for externally-owned memory here.
+ // The same device buffer can be rebound with a different logical layout,
+ // and primitive_inst::set_output_memory() short-circuits on buffer identity.
+ // That keeps a stale memory object/layout on input_layout and breaks
+ // dynamic stateful flows that feed previous outputs back as inputs.
_outputs[0] = mem;
+ _max_output_layout_count[0] = mem->get_layout().get_linear_size();
} else {
- if (_outputs.empty() || !_outputs[0]) {
+ if (_outputs.empty()) {
_outputs.resize(1);
- _outputs[0] = engine.allocate_memory(mem->get_layout(), engine.get_preferred_memory_allocation_type(), false);
}
-
- if (ol.is_dynamic() && _outputs[0]->size() < mem->size()) {
- _outputs[0] = engine.allocate_memory(mem->get_layout(), engine.get_preferred_memory_allocation_type(), false);
+ // Only allocate if needed and not already large enough
+ if (!_outputs[0] || _outputs[0]->size() < mem->size()) {
+ set_output_memory(engine.allocate_memory(mem->get_layout(), engine.get_preferred_memory_allocation_type(), false), false);
}
mem_lock<uint8_t> src(mem, stream);
ev = _outputs[0]->copy_from(stream, src.data(), false);
@@ -78,7 +91,7 @@ void input_layout_inst::update_shape() {
_impl_params->output_layouts[0] = mem_layout;
}
-std::string input_layout_inst::to_string(input_layout_node const& node) {
+std::string input_layout_inst::to_string(const input_layout_node& node) {
auto node_info = node.desc_to_json();
std::stringstream primitive_description;
diff --git a/src/plugins/intel_gpu/src/graph/network.cpp b/src/plugins/intel_gpu/src/graph/network.cpp
index 2b71b98e845..7503bd7cb12 100644
--- a/src/plugins/intel_gpu/src/graph/network.cpp
+++ b/src/plugins/intel_gpu/src/graph/network.cpp
@@ -402,10 +402,29 @@ event::ptr network::set_input_data(const primitive_id& id, memory::ptr data, boo
if (primitive_inst->type() != input_layout::type_id()) {
CLDNN_ERROR_MESSAGE(id, "primitive " + id + " is not an input");
}
-
auto input = std::static_pointer_cast<input_layout_inst>(primitive_inst);
+ const bool was_unallocated = !input->output_memory_ptr();
+ auto ev = input->set_data(data, need_to_check_memory_to_set);
+
+ if (was_unallocated) {
+ // The initial set_arguments() skipped nodes whose dep buffer was null —
+ // force a fresh rebind now that the buffer is available.
+ _reset_arguments = true;
+ }
- return input->set_data(data, need_to_check_memory_to_set);
+ // Update the shared mem type hint for the surfaces lock fast-path in execute().
+ // We deduplicate by type value to prevent unbounded growth across inferences.
+ // Note: this vector is a conservative hint - false positives (stale surface types
+ // after input memory switches back to non-surface) are harmless since execute()
+ // re-checks live memory state when building in_out_mem.
+ // TODO: possibly remove or redesign _in_out_shared_mem_types solution
+ if (input->output_memory_ptr()) {
+ const auto in_mem_type = input->output_memory_ptr()->get_internal_params().mem_type;
+ if (std::find(_in_out_shared_mem_types.begin(), _in_out_shared_mem_types.end(), in_mem_type) == _in_out_shared_mem_types.end())
+ _in_out_shared_mem_types.push_back(in_mem_type);
+ }
+
+ return ev;
}
void network::add_default_output_chains() {
@@ -465,6 +484,7 @@ network::output_chains_map::iterator network::add_output_chain(std::shared_ptr<p
std::vector<primitive_inst*> chain;
std::stack<const primitive_inst*> candidates;
auto& eng = get_engine();
+
const auto& mem_orig = p_inst->output_memory();
auto add_mdata_chain = [&](primitive_inst* p_inst) {
@@ -632,6 +652,10 @@ void network::allocate_primitives() {
if (!node->get_dependencies().empty() && opt_inst->dependencies().empty()) {
opt_inst->build_deps();
}
+ // Skip if the dependency's memory is not yet allocated (e.g. lazy input_layout).
+ // The output memory will be set up at runtime when the input becomes available.
+ if (!opt_inst->dependencies().empty() && opt_inst->dep_memory_ptr(0) == nullptr)
+ continue;
opt_inst->update_output_memory();
}
}
@@ -951,7 +975,8 @@ std::vector<primitive_id> network::get_input_ids() const {
std::vector<layout> network::get_input_layouts() const {
std::vector<layout> ret;
ret.reserve(_inputs.size());
- for (auto const& input : _inputs) ret.push_back(input->output_memory_ptr()->get_layout());
+ for (auto const& input : _inputs)
+ ret.push_back(input->output_memory_ptr() ? input->output_memory_ptr()->get_layout() : input->get_output_layout());
return ret;
}
diff --git a/src/plugins/intel_gpu/src/graph/permute.cpp b/src/plugins/intel_gpu/src/graph/permute.cpp
index 05580279d4e..edf9690066e 100644
--- a/src/plugins/intel_gpu/src/graph/permute.cpp
+++ b/src/plugins/intel_gpu/src/graph/permute.cpp
@@ -141,12 +141,15 @@ void permute_inst::update_output_memory() {
if (!can_be_optimized() || _impl_params->is_dynamic())
return;
+ build_deps();
+
+ if (input_memory_ptr() == nullptr)
+ return;
+
if (_outputs.size() > 0 && static_cast<bool>(_outputs[0])
&& _network.get_engine().is_the_same_buffer(output_memory(), input_memory()))
return;
- build_deps();
-
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,
diff --git a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
index f209f740856..3a444c2ffce 100644
--- a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
+++ b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
@@ -2400,6 +2400,12 @@ primitive_inst::primitive_inst(network & network, program_node const& node, bool
// sum post-op can use the input buffer as the output buffer
auto& eltw_node = node.get_dependency(reused_eltwmem_idx);
const auto& eltw_inst = _network.get_primitive(eltw_node.id());
+ if (eltw_node.is_type<input_layout>() && !eltw_inst->outputs_allocated()) {
+ auto eltw_mem = _network.get_engine().allocate_memory(eltw_node.get_output_layout(),
+ _network.get_engine().get_preferred_memory_allocation_type(),
+ /*reset*/false);
+ eltw_inst->set_output_memory(eltw_mem, /*check*/false);
+ }
auto& eltw_mem = eltw_inst->output_memory();
auto new_mem = eltw_mem.get_engine()->reinterpret_buffer(eltw_mem, node.get_output_layout());
_outputs.push_back(new_mem);
diff --git a/src/plugins/intel_gpu/src/graph/resample.cpp b/src/plugins/intel_gpu/src/graph/resample.cpp
index dcfab0b6723..230b582df2d 100644
--- a/src/plugins/intel_gpu/src/graph/resample.cpp
+++ b/src/plugins/intel_gpu/src/graph/resample.cpp
@@ -227,7 +227,7 @@ void resample_inst::update_output_memory() {
build_deps();
- if (get_node().get_program().is_new_shape_infer() && input_memory_ptr() == nullptr)
+ if (input_memory_ptr() == nullptr)
return;
if (_outputs[0] && get_network().get_engine().is_the_same_buffer(output_memory(), input_memory()))
diff --git a/src/plugins/intel_gpu/src/graph/reshape.cpp b/src/plugins/intel_gpu/src/graph/reshape.cpp
index 9cc1378be7f..b380967c3a5 100644
--- a/src/plugins/intel_gpu/src/graph/reshape.cpp
+++ b/src/plugins/intel_gpu/src/graph/reshape.cpp
@@ -334,9 +334,8 @@ void reshape_inst::update_output_memory() {
return;
build_deps(); // reshape need deps
- if (get_node().get_program().is_new_shape_infer() && input_memory_ptr() == nullptr)
+ if (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.
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 7efb5d5c92b..75d10fd79eb 100644
--- a/src/plugins/intel_gpu/src/graph/scatter_elements_update.cpp
+++ b/src/plugins/intel_gpu/src/graph/scatter_elements_update.cpp
@@ -71,6 +71,9 @@ void scatter_elements_update_inst::update_output_memory() {
build_deps();
+ if (input_memory_ptr() == nullptr)
+ 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]) &&
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 6911b4d729a..87229bab29e 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,9 @@ void scatter_nd_update_inst::update_output_memory() {
build_deps();
+ if (input_memory_ptr() == nullptr)
+ 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]) &&
diff --git a/src/plugins/intel_gpu/src/graph/scatter_update.cpp b/src/plugins/intel_gpu/src/graph/scatter_update.cpp
index 7e254df2597..6948ab827f7 100644
--- a/src/plugins/intel_gpu/src/graph/scatter_update.cpp
+++ b/src/plugins/intel_gpu/src/graph/scatter_update.cpp
@@ -63,6 +63,9 @@ void scatter_update_inst::update_output_memory() {
if (_node != nullptr)
build_deps();
+ if (input_memory_ptr() == nullptr)
+ 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]) &&
diff --git a/src/plugins/intel_gpu/src/graph/slice_scatter.cpp b/src/plugins/intel_gpu/src/graph/slice_scatter.cpp
index 7bb0d7d134b..6362c7966ad 100644
--- a/src/plugins/intel_gpu/src/graph/slice_scatter.cpp
+++ b/src/plugins/intel_gpu/src/graph/slice_scatter.cpp
@@ -67,10 +67,13 @@ void slice_scatter_inst::update_output_memory() {
if (!can_be_optimized() || _impl_params->is_dynamic())
return;
- if (_outputs.size() > 0 && static_cast<bool>(_outputs[0]) && _network.get_engine().is_the_same_buffer(output_memory(), input_memory()))
+ build_deps();
+
+ if (input_memory_ptr() == nullptr)
return;
- build_deps();
+ if (_outputs.size() > 0 && static_cast<bool>(_outputs[0]) && _network.get_engine().is_the_same_buffer(output_memory(), input_memory()))
+ return;
if (static_cast<bool>(_outputs[0]) && get_node().get_program().get_config().get_enable_memory_pool()) {
_network.get_memory_pool().release_memory(_outputs[0].get(), get_node().get_unique_id(), get_node().id(), _network.get_id());
diff --git a/src/plugins/intel_gpu/src/graph/strided_slice.cpp b/src/plugins/intel_gpu/src/graph/strided_slice.cpp
index 9af41e66d49..0a8ff4ed9e6 100644
--- a/src/plugins/intel_gpu/src/graph/strided_slice.cpp
+++ b/src/plugins/intel_gpu/src/graph/strided_slice.cpp
@@ -196,14 +196,14 @@ void strided_slice_inst::update_output_memory() {
if (!can_be_optimized())
return;
- if (get_node().get_program().is_new_shape_infer() && input_memory_ptr() == nullptr)
+ build_deps();
+
+ if (input_memory_ptr() == nullptr)
return;
if (static_cast<bool>(_outputs[0]) && _network.get_engine().is_the_same_buffer(output_memory(), input_memory()))
return;
- build_deps();
-
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,
diff --git a/src/plugins/intel_gpu/tests/unit/module_tests/network_test.cpp b/src/plugins/intel_gpu/tests/unit/module_tests/network_test.cpp
index e4f45a70e94..5164fcaedcb 100644
--- a/src/plugins/intel_gpu/tests/unit/module_tests/network_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/module_tests/network_test.cpp
@@ -13,6 +13,7 @@
#include "intel_gpu/primitives/reorder.hpp"
#include "intel_gpu/primitives/reshape.hpp"
#include "intel_gpu/primitives/fully_connected.hpp"
+#include "intel_gpu/primitives/slice_scatter.hpp"
#include "primitive_inst.h"
#include "runtime/ocl/ocl_event.hpp"
@@ -250,4 +251,45 @@ TEST(network_test, scratchpad_test) {
}
}
+// this test verifies the case where an example primitive - in this case it's slice_scatter
+// calls update_output_memory before build_deps is called, in this case we expect
+// that build_deps will be called from update_output_memory before accessing calling things like input_memory_ptr
+TEST(network_test, update_output_memory_calls_build_deps) {
+ auto& engine = get_test_engine();
+
+ auto data_mem = engine.allocate_memory({data_types::f32, format::bfyx, {1, 1, 2, 3}});
+ auto updates_mem = engine.allocate_memory({data_types::f32, format::bfyx, {1, 1, 2, 3}});
+ auto start_mem = engine.allocate_memory(layout{ov::PartialShape{4}, data_types::i64, format::bfyx});
+ auto stop_mem = engine.allocate_memory(layout{ov::PartialShape{4}, data_types::i64, format::bfyx});
+ auto step_mem = engine.allocate_memory(layout{ov::PartialShape{4}, data_types::i64, format::bfyx});
+
+ set_values<float>(data_mem, {0.f, 1.f, 2.f, 3.f, 4.f, 5.f});
+ set_values<float>(updates_mem, {10.f, 20.f, 30.f, 40.f, 50.f, 60.f});
+ set_values<int64_t>(start_mem, {0, 0, 0, 0});
+ set_values<int64_t>(stop_mem, {1, 1, 2, 3});
+ set_values<int64_t>(step_mem, {1, 1, 1, 1});
+
+ topology topo;
+ topo.add(input_layout("data", data_mem->get_layout()));
+ topo.add(input_layout("updates", updates_mem->get_layout()));
+ topo.add(data("start", start_mem));
+ topo.add(data("stop", stop_mem));
+ topo.add(data("step", step_mem));
+ // slice scatter is used here, but there are other primitives that have similar logic: gather, scatter_update, etc...
+ topo.add(slice_scatter("slice_scatter", {input_info("data"), input_info("updates"), input_info("start"), input_info("stop"), input_info("step")}));
+
+ ExecutionConfig config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+
+ auto prog = program::build_program(engine, topo, config);
+ auto& slice_scatter_node = prog->get_node("slice_scatter");
+ // force the flag to be true, so that it can pass the early return at the top of slice_scatter_inst::update_output_memory
+ slice_scatter_node.can_be_optimized(true);
+ ASSERT_TRUE(slice_scatter_node.can_be_optimized());
+
+ cldnn::network::ptr network;
+ ASSERT_NO_THROW(network = network::allocate_network(engine, prog));
+ ASSERT_NE(network, nullptr);
+}
+
#endif
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 1919af97e8e..9d2bc02b9e4 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
@@ -15,6 +15,8 @@
#include <intel_gpu/primitives/crop.hpp>
#include <intel_gpu/primitives/eltwise.hpp>
#include <intel_gpu/primitives/grid_sample.hpp>
+#include <intel_gpu/primitives/reorder.hpp>
+#include <primitive_inst.h>
#include <fully_connected_inst.h>
using namespace cldnn;
@@ -91,7 +93,9 @@ public:
network->set_input_data("input", input);
auto outputs = network->execute();
- ASSERT_EQ(engine->get_max_used_device_memory(), (uint64_t)64);
+ // input_layout no longer pre-allocates memory at network construction time
+ // so we have only 16 bytes usm_host for the input buffer, 16 bytes for "relu" output, and 16 bytes for "relu5" output.
+ ASSERT_EQ(engine->get_max_used_device_memory(), 48ull);
}
void test_basic_non_padded_relu_and_pooling_pipe(bool is_caching_test) {
@@ -123,7 +127,11 @@ public:
network->set_input_data("input", input);
auto outputs = network->execute();
- ASSERT_EQ(engine->get_max_used_device_memory(), (uint64_t)896);
+ // input_layout no longer pre-allocates memory at network construction time,
+ // The remaining peak is 640 bytes:
+ // 256 bytes host for the input, 256 bytes device for relu output,
+ // 64 bytes device for pool1 output, and 64 bytes host for relu5 output.
+ ASSERT_EQ(engine->get_max_used_device_memory(), 640ull);
}
void test_multi_outputs_network(bool is_caching_test) {
@@ -158,7 +166,20 @@ public:
network->set_input_data("input", input);
auto outputs = network->execute();
- ASSERT_EQ(engine->get_max_used_device_memory(), (uint64_t) 1536);
+ auto input_inst = network->get_primitive("input");
+ auto relu_inst = network->get_primitive("relu");
+ auto relu2_inst = network->get_primitive("relu2");
+
+ // Both direct consumers of the lazy-allocated input must see the same external buffer.
+ ASSERT_TRUE(engine->is_the_same_buffer(*input_inst->output_memory_ptr(), *input));
+ ASSERT_TRUE(engine->is_the_same_buffer(*relu_inst->dep_memory_ptr(0), *input));
+ ASSERT_TRUE(engine->is_the_same_buffer(*relu2_inst->dep_memory_ptr(0), *input));
+
+ // input_layout no longer pre-allocates memory at network construction time,
+ // The remaining peak is 1280 bytes:
+ // 256 bytes host for the input, 256 bytes host for relu4 output, 256 bytes host for relu7 output,
+ // and 256 bytes device for two outputs from the first relu on a branch
+ ASSERT_EQ(engine->get_max_used_device_memory(), 1280ull);
}
void test_oooq(bool is_caching_test) {
@@ -196,7 +217,12 @@ public:
network->set_input_data("input", input);
auto outputs = network->execute();
- ASSERT_EQ(engine->get_max_used_device_memory(), (uint64_t) 2560);
+ // input_layout no longer pre-allocates memory at network construction time,
+ // The remaining peak is 2304 bytes:
+ // 256 bytes host for the input, 768 bytes host for the final relu6 output,
+ // and 1280 bytes device for intermediates: 256 bytes each for relu1,
+ // relu2, and the lower branch feeding concat2, plus 512 bytes for concat1 / relu4.
+ ASSERT_EQ(engine->get_max_used_device_memory(), 2304ull);
}
void test_shared_mem_pool_same_topology_twice() {
@@ -380,6 +406,7 @@ public:
auto input_1 = engine->allocate_memory(lay_batch_1);
auto input_8 = engine->allocate_memory(lay_batch_8);
auto weights = engine->allocate_memory({ dt, fmt, { 1, 3, 3, 2 } });
+ // so far we allocated 192+1536+72 = 1800 bytes host memory
std::vector<float> dummy_input_data_1 = rg.generate_random_1d<float>(batch_1 * feature_num * inp_x_size * inp_y_size, 0, 1);
std::vector<float> dummy_input_data_8 = rg.generate_random_1d<float>(batch_8 * feature_num * inp_x_size * inp_y_size, 0, 1);
@@ -403,15 +430,144 @@ public:
network_first->set_input_data("input", input_8);
auto outputs = network_first->execute();
- auto dev_info = engine->get_device_info();
- ASSERT_EQ(engine->get_max_used_device_memory(), (uint64_t)4744);
+ // input_layout no longer pre-allocates memory at network construction time,
+ // the network will use 1152 bytes for weight reorder node, 128 bytes for conv output and 128 bytes for softmax output
+ // in total we have 1800 bytes + 128 + 1152 + 128 = 3208, with no lazy allocations for inputs we'd go up to 4744...
+ ASSERT_EQ(engine->get_max_used_device_memory(), 3208ull);
topo.change_input_layout("input", input_1->get_layout());//change input layout to batch=1
network::ptr network_second = get_network(*engine, topo, config, get_test_stream_ptr(), is_caching_test);
network_second->set_input_data("input", input_1);
auto outputs_second = network_second->execute();
- ASSERT_EQ(engine->get_max_used_device_memory(), (uint64_t)5912);
+
+ // now we have the previous peak + another 1152 for the second network's reorder node, plus 16 bytes for
+ // convolution output, and 16 bytes for the softmax output.
+ // so 3208 + 1152 + 16 + 16 = 4392, without lazy allocations for input we'd go into 5928 bytes...
+ ASSERT_EQ(engine->get_max_used_device_memory(), 4392ull);
+ }
+
+ void test_rebind_external_memory_multiple_times(bool is_caching_test) {
+ auto engine = create_test_engine();
+ layout input_lay{data_types::f32, format::bfyx, {1, 1, 2, 4}};
+ auto input_first = engine->allocate_memory(input_lay);
+ auto input_second = engine->allocate_memory(input_lay);
+
+ const std::vector<float> first_values = {-1.0f, 2.0f, -3.0f, 4.0f, -5.0f, 6.0f, -7.0f, 8.0f};
+ const std::vector<float> second_values = {10.0f, -20.0f, 30.0f, -40.0f, 50.0f, -60.0f, 70.0f, -80.0f};
+ set_values(input_first, first_values);
+ set_values(input_second, second_values);
+
+ topology topology;
+ topology.add(input_layout("input", input_lay));
+ topology.add(activation("relu", input_info("input"), activation_func::relu));
+
+ ExecutionConfig config = get_test_default_config(*engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+
+ network::ptr network = get_network(*engine, topology, config, get_test_stream_ptr(), is_caching_test);
+
+ auto input_inst = network->get_primitive("input");
+ auto relu_inst = network->get_primitive("relu");
+
+ // Lazy input allocation means input_layout should stay unmaterialized until set_input_data().
+ ASSERT_EQ(input_inst->output_memory_ptr(), nullptr);
+
+ network->set_input_data("input", input_first);
+ auto outputs_first = network->execute();
+
+ // After the first bind, both input_layout and the downstream consumer must point to the
+ // external memory object passed through set_input_data().
+ ASSERT_TRUE(engine->is_the_same_buffer(*input_inst->output_memory_ptr(), *input_first));
+ ASSERT_TRUE(engine->is_the_same_buffer(*relu_inst->dep_memory_ptr(0), *input_first));
+
+ auto first_bound_input_ptr = relu_inst->dep_memory_ptr(0)->buffer_ptr();
+ {
+ cldnn::mem_lock<float> first_output_ptr(outputs_first.at("relu").get_memory(), get_test_stream());
+ ASSERT_EQ(first_output_ptr[0], 0.0f);
+ ASSERT_EQ(first_output_ptr[1], 2.0f);
+ ASSERT_EQ(first_output_ptr[2], 0.0f);
+ ASSERT_EQ(first_output_ptr[3], 4.0f);
+ ASSERT_EQ(first_output_ptr[4], 0.0f);
+ ASSERT_EQ(first_output_ptr[5], 6.0f);
+ ASSERT_EQ(first_output_ptr[6], 0.0f);
+ ASSERT_EQ(first_output_ptr[7], 8.0f);
+ }
+
+ network->set_input_data("input", input_second);
+ auto outputs_second = network->execute();
+
+ // Rebinding must refresh the consumer dependency as well, not only input_layout itself.
+ ASSERT_TRUE(engine->is_the_same_buffer(*input_inst->output_memory_ptr(), *input_second));
+ ASSERT_TRUE(engine->is_the_same_buffer(*relu_inst->dep_memory_ptr(0), *input_second));
+
+ auto second_bound_input_ptr = relu_inst->dep_memory_ptr(0)->buffer_ptr();
+ // The consumer should no longer reference the buffer from the first inference.
+ ASSERT_NE(first_bound_input_ptr, second_bound_input_ptr);
+
+ cldnn::mem_lock<float> second_output_ptr(outputs_second.at("relu").get_memory(), get_test_stream());
+ ASSERT_EQ(second_output_ptr[0], 10.0f);
+ ASSERT_EQ(second_output_ptr[1], 0.0f);
+ ASSERT_EQ(second_output_ptr[2], 30.0f);
+ ASSERT_EQ(second_output_ptr[3], 0.0f);
+ ASSERT_EQ(second_output_ptr[4], 50.0f);
+ ASSERT_EQ(second_output_ptr[5], 0.0f);
+ ASSERT_EQ(second_output_ptr[6], 70.0f);
+ ASSERT_EQ(second_output_ptr[7], 0.0f);
+ }
+
+ void test_aliasing_through_chain_of_optimized_ops(bool is_caching_test) {
+ auto engine = create_test_engine();
+ layout input_lay{ov::PartialShape{5, 6, 1, 1}, data_types::f32, format::bfyx};
+ auto input = engine->allocate_memory(input_lay);
+
+ set_values(input, { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f,
+ 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f,
+ 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f,
+ 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f,
+ 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f });
+
+ topology topology;
+ topology.add(input_layout("input", input_lay));
+ topology.add(crop("crop1", {input_info("input")}, tensor(5, 5, 1, 1), {tensor(0, 0, 0, 0)}));
+ topology.add(crop("crop2", {input_info("crop1")}, tensor(5, 4, 1, 1), {tensor(0, 0, 0, 0)}));
+ topology.add(reorder("reorder_out", input_info("crop2"), layout{ov::PartialShape{5, 4, 1, 1}, data_types::f32, format::bfyx}));
+
+ ExecutionConfig config = get_test_default_config(*engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+
+ network::ptr network = get_network(*engine, topology, config, get_test_stream_ptr(), is_caching_test);
+
+ auto input_inst = network->get_primitive("input");
+ auto crop1_inst = network->get_primitive("crop1");
+ auto crop2_inst = network->get_primitive("crop2");
+ auto reorder_inst = network->get_primitive("reorder_out");
+
+ ASSERT_EQ(input_inst->output_memory_ptr(), nullptr);
+
+ network->set_input_data("input", input);
+ auto outputs = network->execute();
+
+ ASSERT_TRUE(crop1_inst->can_be_optimized());
+ ASSERT_TRUE(crop2_inst->can_be_optimized());
+
+ // The optimized crop chain should stay backed by the original external input buffer.
+ ASSERT_TRUE(engine->is_the_same_buffer(*input_inst->output_memory_ptr(), *input));
+ ASSERT_TRUE(engine->is_the_same_buffer(*network->get_output_memory("crop1"), *input));
+ ASSERT_TRUE(engine->is_the_same_buffer(*network->get_output_memory("crop2"), *input));
+
+ // The final consumer must receive the aliased view from the last optimized op in the chain.
+ ASSERT_TRUE(engine->is_the_same_buffer(*reorder_inst->dep_memory_ptr(0), *network->get_output_memory("crop2")));
+
+ cldnn::mem_lock<float> output_ptr(outputs.at("reorder_out").get_memory(), get_test_stream());
+ const std::vector<float> expected = { 0.0f, 1.0f, 2.0f, 3.0f,
+ 0.0f, 1.0f, 2.0f, 3.0f,
+ 0.0f, 1.0f, 2.0f, 3.0f,
+ 0.0f, 1.0f, 2.0f, 3.0f,
+ 0.0f, 1.0f, 2.0f, 3.0f };
+ for (size_t i = 0; i < expected.size(); ++i) {
+ ASSERT_EQ(output_ptr[i], expected[i]);
+ }
}
void test_shared_dep_two_output(bool is_caching_test) {
@@ -723,6 +879,14 @@ TEST_F(memory_pool, shared_mem_pool_diff_batches) {
this->test_shared_mem_pool_diff_batches(false);
}
+TEST_F(memory_pool, rebind_external_memory_multiple_times) {
+ this->test_rebind_external_memory_multiple_times(false);
+}
+
+TEST_F(memory_pool, aliasing_through_chain_of_optimized_ops) {
+ this->test_aliasing_through_chain_of_optimized_ops(false);
+}
+
TEST_F(memory_pool, shared_dep_two_output) {
this->test_shared_dep_two_output(false);
}
@@ -770,6 +934,14 @@ TEST_F(memory_pool, shared_mem_pool_diff_batches_cached) {
this->test_shared_mem_pool_diff_batches(true);
}
+TEST_F(memory_pool, rebind_external_memory_multiple_times_cached) {
+ this->test_rebind_external_memory_multiple_times(true);
+}
+
+TEST_F(memory_pool, aliasing_through_chain_of_optimized_ops_cached) {
+ this->test_aliasing_through_chain_of_optimized_ops(true);
+}
+
TEST_F(memory_pool, shared_dep_two_output_cached) {
this->test_shared_dep_two_output(true);
}