Skip to content

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

[GPU] memory reset bugfix (#32479)

Mingyu Kimfd4efb325d27
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 645f646fd87..3177b90881a 100644
--- a/src/plugins/intel_gpu/src/graph/include/primitive_inst.h
+++ b/src/plugins/intel_gpu/src/graph/include/primitive_inst.h
@@ -481,21 +481,7 @@ protected:
return false;
}
- virtual bool need_reset_output_memory() const {
- for (const auto& user_inst : get_user_insts()) {
- // Check users of optimized_out inst, as the optimized out inst will not be able to
- // reset it's memory
- if (user_inst->can_be_optimized()) {
- if (user_inst->need_reset_output_memory())
- return true;
- continue;
- }
-
- if (user_inst->need_reset_input_memory(user_inst->get_node().get_dependency_index(get_node())))
- return true;
- }
- return false;
- }
+ virtual bool need_reset_output_memory() const;
void clear_output_memory();
diff --git a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
index 49dc0a9fe27..fc6ed79aca5 100644
--- a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
+++ b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
@@ -589,6 +589,48 @@ bool primitive_inst::all_dependencies_cpu_impl() const {
return check_all_deps_cpu(this);
}
+bool primitive_inst::need_reset_output_memory() const {
+ for (const auto& user_inst : get_user_insts()) {
+ // Check users of optimized_out inst, as the optimized out inst will not be able to
+ // reset it's memory
+ if (user_inst->can_be_optimized()) {
+ if (user_inst->need_reset_output_memory())
+ return true;
+ continue;
+ }
+
+ if (user_inst->need_reset_input_memory(user_inst->get_node().get_dependency_index(get_node())))
+ return true;
+
+ // OneDNN requires zero-filled input for padded area
+ const bool is_user_onednn_impl = user_inst->get_node().get_preferred_impl_type() == impl_types::onednn;
+ const bool is_user_conv = user_inst->get_node().is_type<convolution>();
+ if (is_user_conv && is_user_onednn_impl) {
+ auto& output_layout = _impl_params->get_output_layout(0);
+
+ auto get_feature_block_size = [](format fmt) {
+ int feature_block_size = 1;
+ for (auto &e : fmt.block_sizes()) {
+ if (e.first == 1) {
+ OPENVINO_ASSERT(feature_block_size == 1, "UNSUPPORTED: multi-blocking for feature axis is not considered");
+ feature_block_size = e.second;
+ }
+ }
+ return feature_block_size;
+ };
+
+ const auto fmt = output_layout.format;
+ auto feature_block_size = get_feature_block_size(fmt);
+ // if layout is single blocked and feature size is not aligned with the blocking size, need to reset output so that we can guarantee zero-filling
+ // NOTE: We may improve this logic to avoid reset if we are sure that it is not "corrupted" by other layers.
+ if (output_layout.feature() % feature_block_size != 0) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
void primitive_inst::clear_output_memory() {
_outputs[0] = nullptr;
_max_output_layout_count[0] = 0;
diff --git a/src/plugins/intel_gpu/tests/unit/module_tests/mem_reset_test.cpp b/src/plugins/intel_gpu/tests/unit/module_tests/mem_reset_test.cpp
new file mode 100644
index 00000000000..ce821bda8fa
--- /dev/null
+++ b/src/plugins/intel_gpu/tests/unit/module_tests/mem_reset_test.cpp
@@ -0,0 +1,99 @@
+// Copyright (C) 2025-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "test_utils.h"
+#include "random_generator.hpp"
+#include "primitive_inst_test_helper.h"
+
+#include <intel_gpu/primitives/input_layout.hpp>
+#include <intel_gpu/primitives/convolution.hpp>
+#include <intel_gpu/primitives/eltwise.hpp>
+
+#include <algorithm>
+#include <array>
+#include <cmath>
+#include <iostream>
+#include <iomanip>
+#include <thread>
+#include <type_traits>
+#include <fstream>
+#include <tuple>
+
+#include "convolution_inst.h"
+
+using namespace cldnn;
+using namespace ::tests;
+
+namespace {
+const std::string no_bias = "";
+}
+
+struct mem_reset_params {
+ ov::Dimension::value_type in_channel;
+ bool need_reset;
+};
+
+class mem_reset_test : public testing::TestWithParam<mem_reset_params> {};
+
+TEST_P(mem_reset_test, need_reset_output_memory_test) {
+ auto p = GetParam();
+ auto& engine = get_test_engine();
+ if (!engine.get_device_info().supports_immad)
+ return;
+
+ tests::random_generator rg(GET_SUITE_NAME);
+ ov::PartialShape input_pshape = {1, p.in_channel, 64, 64};
+ ov::PartialShape weights_pshape = {16, p.in_channel, 3, 3};
+ layout in_layout{ input_pshape, data_types::f16, format::bfyx };
+ layout weights_layout{ weights_pshape, data_types::f16, format::bfyx };
+ auto input_data = rg.generate_random_1d<ov::float16>(in_layout.count(), -1, 1);
+ auto input_mem = engine.allocate_memory(in_layout);
+ set_values(input_mem, input_data);
+
+ auto weights_data = rg.generate_random_1d<ov::float16>(weights_layout.count(), -1, 1);
+ auto weights_mem = engine.allocate_memory(weights_layout);
+ set_values(weights_mem, weights_data);
+
+ auto input1 = input_layout("input1", input_mem->get_layout());
+ auto input2 = input_layout("input2", input_mem->get_layout());
+ auto weights = data("weights", weights_mem);
+ auto eltw = eltwise("eltwise", {input_info("input1"), input_info("input2")}, eltwise_mode::sum);
+ auto eltw_reorder = reorder("reorder1", input_info("eltwise"), format::b_fs_yx_fsv16, data_types::f16 );
+ auto conv = convolution("conv",
+ input_info("reorder1"),
+ "weights",
+ no_bias,
+ 1,
+ ov::Strides{1, 1},
+ ov::Strides{1, 1},
+ ov::CoordinateDiff{0, 0},
+ ov::CoordinateDiff{0, 0},
+ false);
+ auto output_reorder = reorder("reorder", input_info("conv"), format::bfyx, data_types::f32 );
+
+ topology t(input1, input2, weights, eltw, eltw_reorder, conv, output_reorder);
+
+ ExecutionConfig config_test_blocked = get_test_default_config(engine);
+ ov::intel_gpu::ImplementationDesc conv_impl_test_blocked = { format::b_fs_yx_fsv16, "", impl_types::onednn };
+ config_test_blocked.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{ { "conv", conv_impl_test_blocked } }));
+ config_test_blocked.set_property(ov::intel_gpu::optimize_data(true));
+ config_test_blocked.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+
+ network network_test_blocked(engine, t, config_test_blocked);
+
+ network_test_blocked.set_input_data("input1", input_mem);
+ network_test_blocked.set_input_data("input2", input_mem);
+
+ auto outputs_test_blocked = network_test_blocked.execute();
+
+ auto reorder_inst = network_test_blocked.get_primitive("reorder1");
+ ASSERT_TRUE(PrimitiveInstTestHelper::need_reset_output_memory(reorder_inst) == p.need_reset);
+}
+
+INSTANTIATE_TEST_SUITE_P(smoke, mem_reset_test,
+ testing::Values(
+ mem_reset_params{ 9, true }, // If tensor is not packed(not aligned to 16), need_reset_output_memory == true
+ mem_reset_params{ 16, false }
+ )
+);
diff --git a/src/plugins/intel_gpu/tests/unit/test_utils/primitive_inst_test_helper.h b/src/plugins/intel_gpu/tests/unit/test_utils/primitive_inst_test_helper.h
index 8d65a9ae373..f0c348ab35e 100644
--- a/src/plugins/intel_gpu/tests/unit/test_utils/primitive_inst_test_helper.h
+++ b/src/plugins/intel_gpu/tests/unit/test_utils/primitive_inst_test_helper.h
@@ -14,5 +14,8 @@ public:
static void set_allocation_done_by_other(const std::shared_ptr<primitive_inst>& inst, bool val) {
inst->_allocation_done_by_other = val;
}
+ static bool need_reset_output_memory(const std::shared_ptr<primitive_inst>& inst) {
+ return inst->need_reset_output_memory();
+ }
};
} // namespace cldnn