Skip to content

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

[GPU] Reject oneDNN in-place sum/mul post-ops on network inputs and in-place views (#39667)

Bartlomiej Filipek56346f1a8b82
diff --git a/src/plugins/intel_gpu/src/graph/program_helpers.cpp b/src/plugins/intel_gpu/src/graph/program_helpers.cpp
index 13af4d10a1a..32d8636ab85 100644
--- a/src/plugins/intel_gpu/src/graph/program_helpers.cpp
+++ b/src/plugins/intel_gpu/src/graph/program_helpers.cpp
@@ -3,15 +3,19 @@
//
#include "program_helpers.h"
-#include "intel_gpu/graph/program.hpp"
-#include "data_inst.h"
-#include "pooling_inst.h"
-#include "fully_connected_inst.h"
-#include "transformations/symbolic_transformations/utils.hpp"
+
#include <algorithm>
+#include <sstream>
#include <utility>
#include <vector>
-#include <sstream>
+
+#include "data_inst.h"
+#include "fully_connected_inst.h"
+#include "intel_gpu/graph/program.hpp"
+#include "pooling_inst.h"
+#include "read_value_inst.h"
+#include "reshape_inst.h"
+#include "transformations/symbolic_transformations/utils.hpp"
namespace cldnn {
void program_helpers::reshape_deconvolution_weights(const std::vector<float> &deconv_weights,
@@ -149,6 +153,20 @@ static bool is_direct_ancestor(const program_node& child, const program_node& ta
return false;
}
+// An in-place post-op (sum or binary_mul_inplace) writes into dep_node's buffer, or into the
+// buffer of the first node above it that is not an in-place view. A network input (input_layout)
+// or variable state (read_value) has no material buffer at build time, and at runtime
+// input_layout::set_data replaces its buffer without rebinding static users (while dynamic
+// in-place binding would overwrite caller or state memory). Include is_type<reshape>() alongside
+// can_be_optimized() so calls before handle_reshape or prepare_buffer_fusing still walk reshapes.
+static bool dep_is_network_input_or_state(const program_node& dep_node) {
+ const program_node* owner = &dep_node;
+ while ((owner->can_be_optimized() || owner->is_type<reshape>()) && !owner->is_type<read_value>() && !owner->get_dependencies().empty()) {
+ owner = &owner->get_dependency(0);
+ }
+ return owner->is_type<input_layout>() || owner->is_type<read_value>();
+}
+
add_fusing_type onednn_eltwise_fusing_helpers::get_add_fusing_type(
const program_node& p_node, const fused_primitive_desc& desc) {
if (!desc.is_type<eltwise>()) {
@@ -188,15 +206,10 @@ add_fusing_type onednn_eltwise_fusing_helpers::get_add_fusing_type(
};
if (is_full_tensor(p_layout) && is_full_tensor(d_layout)) {
- if (data_type_traits::size_of(p_layout.data_type) == data_type_traits::size_of(d_layout.data_type)
- && p_layout.format == d_layout.format && p_layout.get_tensor() == d_layout.get_tensor()
- && p_layout.data_padding == d_layout.data_padding
- && (dep_node.get_users().size() == 1 || is_direct_ancestor(p_node, dep_node))
- && !dep_node.is_constant()
- && !p_node.is_type<pooling>()
- && !p_node.is_output()
- && (!dep_node.is_type<input_layout>() || dep_node.get_users().size() <= 1)
- && !p_node_consumes_dep_node()) {
+ if (data_type_traits::size_of(p_layout.data_type) == data_type_traits::size_of(d_layout.data_type) && p_layout.format == d_layout.format &&
+ p_layout.get_tensor() == d_layout.get_tensor() && p_layout.data_padding == d_layout.data_padding &&
+ (dep_node.get_users().size() == 1 || is_direct_ancestor(p_node, dep_node)) && !dep_node.is_constant() && !p_node.is_type<pooling>() &&
+ !p_node.is_output() && !p_node_consumes_dep_node() && !dep_is_network_input_or_state(dep_node)) {
return add_fusing_type::sum;
}
if (p_layout.get_tensor() == d_layout.get_tensor()) {
@@ -239,7 +252,7 @@ bool onednn_eltwise_fusing_helpers::can_use_mul_inplace(const program_node& p_no
return false;
}
- return dep_node.get_users().size() == 1 && !dep_node.is_constant() && !p_node.is_output();
+ return dep_node.get_users().size() == 1 && !dep_node.is_constant() && !p_node.is_output() && !dep_is_network_input_or_state(dep_node);
}
int32_t onednn_eltwise_fusing_helpers::get_reused_eltwmem_idx(const program_node& node) {
diff --git a/src/plugins/intel_gpu/tests/functional/subgraph_tests/convolution_sum_fusion.cpp b/src/plugins/intel_gpu/tests/functional/subgraph_tests/convolution_sum_fusion.cpp
index eddc02f5493..094a883a8ee 100644
--- a/src/plugins/intel_gpu/tests/functional/subgraph_tests/convolution_sum_fusion.cpp
+++ b/src/plugins/intel_gpu/tests/functional/subgraph_tests/convolution_sum_fusion.cpp
@@ -2,12 +2,16 @@
// SPDX-License-Identifier: Apache-2.0
//
-#include "shared_test_classes/base/ov_subgraph.hpp"
-
#include "openvino/op/add.hpp"
#include "openvino/op/constant.hpp"
+#include "openvino/op/convert.hpp"
#include "openvino/op/convolution.hpp"
+#include "openvino/op/matmul.hpp"
+#include "openvino/op/multiply.hpp"
#include "openvino/op/parameter.hpp"
+#include "openvino/op/reshape.hpp"
+#include "openvino/op/softmax.hpp"
+#include "shared_test_classes/base/ov_subgraph.hpp"
namespace {
// The added tensor is the input of the same convolution, so a sum post-op would let onednn
@@ -49,4 +53,58 @@ INSTANTIATE_TEST_SUITE_P(smoke_GPU_Convolution, ConvolutionOwnInputSum,
ov::Shape{1, 16, 16, 16}),
ConvolutionOwnInputSum::getTestCaseName);
+class FCCompressedReshapedInputSum : virtual public ov::test::SubgraphBaseStaticTest {
+protected:
+ void SetUp() override {
+ targetDevice = ov::test::utils::DEVICE_GPU;
+ const auto type = ov::element::f16;
+ rel_threshold = 0.02f;
+ abs_threshold = 0.02f;
+
+ auto input = std::make_shared<ov::op::v0::Parameter>(type, ov::Shape{1, 32, 4, 4});
+ auto residual = std::make_shared<ov::op::v0::Parameter>(type, ov::Shape{1, 32, 16});
+ auto in_2d = std::make_shared<ov::op::v1::Reshape>(input, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{2}, {32, 16}), false);
+ auto weights_u8 = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{16, 16}, {2});
+ auto weights_f16 = std::make_shared<ov::op::v0::Convert>(weights_u8, type);
+ auto scale = ov::op::v0::Constant::create(type, ov::Shape{16, 1}, {0.1f});
+ auto decomp = std::make_shared<ov::op::v1::Multiply>(weights_f16, scale);
+ auto mm = std::make_shared<ov::op::v0::MatMul>(in_2d, decomp, false, true);
+ auto mm_3d = std::make_shared<ov::op::v1::Reshape>(mm, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{3}, {1, 32, 16}), false);
+ auto sum = std::make_shared<ov::op::v1::Add>(mm_3d, residual);
+ auto out = std::make_shared<ov::op::v8::Softmax>(sum, 2);
+
+ function = std::make_shared<ov::Model>(ov::OutputVector{out}, ov::ParameterVector{input, residual});
+ }
+};
+
+TEST_F(FCCompressedReshapedInputSum, Inference) {
+ run();
+}
+
+class FCCompressedDirectInputSum : virtual public ov::test::SubgraphBaseStaticTest {
+protected:
+ void SetUp() override {
+ targetDevice = ov::test::utils::DEVICE_GPU;
+ const auto type = ov::element::f16;
+ rel_threshold = 0.02f;
+ abs_threshold = 0.02f;
+
+ auto input = std::make_shared<ov::op::v0::Parameter>(type, ov::Shape{32, 16});
+ auto residual = std::make_shared<ov::op::v0::Parameter>(type, ov::Shape{32, 16});
+ auto weights_u8 = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{16, 16}, {2});
+ auto weights_f16 = std::make_shared<ov::op::v0::Convert>(weights_u8, type);
+ auto scale = ov::op::v0::Constant::create(type, ov::Shape{16, 1}, {0.1f});
+ auto decomp = std::make_shared<ov::op::v1::Multiply>(weights_f16, scale);
+ auto mm = std::make_shared<ov::op::v0::MatMul>(input, decomp, false, true);
+ auto sum = std::make_shared<ov::op::v1::Add>(mm, residual);
+ auto out = std::make_shared<ov::op::v8::Softmax>(sum, 1);
+
+ function = std::make_shared<ov::Model>(ov::OutputVector{out}, ov::ParameterVector{input, residual});
+ }
+};
+
+TEST_F(FCCompressedDirectInputSum, Inference) {
+ run();
+}
+
} // namespace
diff --git a/src/plugins/intel_gpu/tests/unit/passes/add_onednn_optimization_attributes_test.cpp b/src/plugins/intel_gpu/tests/unit/passes/add_onednn_optimization_attributes_test.cpp
index c2424065682..c24fba73ab2 100644
--- a/src/plugins/intel_gpu/tests/unit/passes/add_onednn_optimization_attributes_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/passes/add_onednn_optimization_attributes_test.cpp
@@ -12,6 +12,7 @@
#include "eltwise_inst.h"
#include "activation_inst.h"
#include "reorder_inst.h"
+#include "reshape_inst.h"
#include "convolution_inst.h"
#include "pass_manager.h"
#include "to_string_utils.h"
@@ -100,8 +101,7 @@ TEST(add_onednn_optimization_attributes, sum_post_op_for_residual_connection) {
}
-
-TEST(add_onednn_optimization_attributes, fc_sum_u8_single_user_input_uses_sum) {
+TEST(add_onednn_optimization_attributes, fc_sum_u8_single_user_input_uses_binary) {
auto& engine = get_test_engine();
if (!engine.get_device_info().supports_immad)
@@ -138,7 +138,7 @@ TEST(add_onednn_optimization_attributes, fc_sum_u8_single_user_input_uses_sum) {
ASSERT_EQ(fused.size(), 1);
auto fusing_type = onednn_eltwise_fusing_helpers::get_add_fusing_type(fc_node, fused[0]);
- ASSERT_EQ(fusing_type, add_fusing_type::sum);
+ ASSERT_EQ(fusing_type, add_fusing_type::binary_per_tensor);
}
TEST(add_onednn_optimization_attributes, fc_sum_u8_residual_input_uses_binary) {
@@ -181,6 +181,81 @@ TEST(add_onednn_optimization_attributes, fc_sum_u8_residual_input_uses_binary) {
ASSERT_EQ(fusing_type, add_fusing_type::binary_per_tensor);
}
+// The addend is a network input seen through a reshape, as in a decoder's first residual after
+// MoveEltwiseUpThroughDataMovFusableProducer. A sum would accumulate into the input's buffer, which
+// has no memory at build time and belongs to the caller.
+TEST(add_onednn_optimization_attributes, fc_sum_refused_for_reshaped_network_input) {
+ auto& engine = get_test_engine();
+
+ if (!engine.get_device_info().supports_immad)
+ GTEST_SKIP() << "onednn sum post-ops are only selected on a device with immad";
+
+ auto weights_mem = engine.allocate_memory({ov::PartialShape({16, 16}), data_types::f16, format::bfyx});
+
+ topology topology;
+ topology.add(data("weights", weights_mem));
+ topology.add(input_layout("input", layout{ov::PartialShape({32, 16}), data_types::f16, format::bfyx}));
+ topology.add(input_layout("residual", layout{ov::PartialShape({1, 32, 16}), data_types::f16, format::bfyx}));
+ topology.add(fully_connected("fc", input_info("input"), "weights"));
+ topology.add(reshape("residual_view", input_info("residual"), false, {32, 16}, ov::PartialShape{32, 16}));
+ topology.add(eltwise("sum", {input_info("fc"), input_info("residual_view")}, eltwise_mode::sum));
+ topology.add(reorder("out", input_info("sum"), format::bfyx, data_types::f32));
+
+ ExecutionConfig config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+
+ auto prog = program::build_program(engine, topology, config, false, true);
+ ASSERT_NE(prog, nullptr);
+
+ prog->get_layout_optimizer().add_all_onednn_impls_optimization_attribute();
+
+ program_wrapper::apply_opt_pass<prepare_primitive_fusing>(*prog);
+ program_wrapper::apply_opt_pass<add_onednn_optimization_attributes>(*prog);
+
+ auto& fc_node = prog->get_node("fc");
+ auto& fused = fc_node.get_fused_primitives();
+ ASSERT_EQ(fused.size(), 1);
+
+ auto fusing_type = onednn_eltwise_fusing_helpers::get_add_fusing_type(fc_node, fused[0]);
+ ASSERT_EQ(fusing_type, add_fusing_type::binary_per_tensor);
+}
+
+TEST(add_onednn_optimization_attributes, fc_mul_inplace_refused_for_reshaped_network_input) {
+ auto& engine = get_test_engine();
+
+ if (!engine.get_device_info().supports_immad)
+ GTEST_SKIP() << "onednn post-ops are only selected on a device with immad";
+
+ auto weights_mem = engine.allocate_memory({ov::PartialShape({16, 16}), data_types::f16, format::bfyx});
+
+ topology topology;
+ topology.add(data("weights", weights_mem));
+ topology.add(input_layout("input", layout{ov::PartialShape({32, 16}), data_types::f16, format::bfyx}));
+ topology.add(input_layout("mul_in", layout{ov::PartialShape({1, 32, 16}), data_types::f16, format::bfyx}));
+ topology.add(fully_connected("fc", input_info("input"), "weights"));
+ topology.add(reshape("mul_view", input_info("mul_in"), false, {32, 16}, ov::PartialShape{32, 16}));
+ topology.add(eltwise("mul", {input_info("fc"), input_info("mul_view")}, eltwise_mode::prod));
+ topology.add(reorder("out", input_info("mul"), format::bfyx, data_types::f32));
+
+ ExecutionConfig config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+
+ auto prog = program::build_program(engine, topology, config, false, true);
+ ASSERT_NE(prog, nullptr);
+
+ prog->get_layout_optimizer().add_all_onednn_impls_optimization_attribute();
+
+ program_wrapper::apply_opt_pass<prepare_primitive_fusing>(*prog);
+ program_wrapper::apply_opt_pass<add_onednn_optimization_attributes>(*prog);
+
+ auto& fc_node = prog->get_node("fc");
+ auto& fused = fc_node.get_fused_primitives();
+ ASSERT_EQ(fused.size(), 1u);
+ ASSERT_FALSE(onednn_eltwise_fusing_helpers::can_use_mul_inplace(fc_node, fused[0]));
+}
+
// Builds input -> conv1 -> ... -> conv{depth} plus eltwise(conv1, conv{depth}), which puts conv1
// exactly depth-1 dependency(0) hops above the node the eltwise fuses into.
static add_fusing_type residual_chain_fusing_type(cldnn::engine& engine, size_t depth) {
diff --git a/src/plugins/intel_gpu/tests/unit/passes/basic_memory_dependencies_test.cpp b/src/plugins/intel_gpu/tests/unit/passes/basic_memory_dependencies_test.cpp
index d71d1ddab6a..b07a0057c47 100644
--- a/src/plugins/intel_gpu/tests/unit/passes/basic_memory_dependencies_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/passes/basic_memory_dependencies_test.cpp
@@ -427,4 +427,291 @@ TEST(basic_memory_dependencies, onednn_sum_root_stays_in_pool) {
ASSERT_EQ(ref_ptr[i], out_ptr[i]) << "index " << i << ": a pooled residual buffer was overwritten while still read";
}
}
+
+// A decoder's first residual after MoveEltwiseUpThroughDataMovFusableProducer: the addend of the
+// fused sum is a network input reached through an in-place reshape. Building the network used to
+// bind the FC output to that input's buffer, which has no memory at build time, and crash.
+TEST(basic_memory_dependencies, onednn_fc_sum_with_reshaped_network_input) {
+ auto& engine = get_test_engine();
+ if (!engine.get_device_info().supports_immad)
+ GTEST_SKIP();
+
+ auto in_layout = layout{ov::PartialShape({32, 16}), data_types::f16, format::bfyx};
+ auto residual_layout = layout{ov::PartialShape({1, 32, 16}), data_types::f16, format::bfyx};
+ auto weights_layout = layout{ov::PartialShape({16, 16}), data_types::f16, format::bfyx};
+ auto weights_mem = engine.allocate_memory(weights_layout);
+ auto input_mem = engine.allocate_memory(in_layout);
+ auto residual_mem = engine.allocate_memory(residual_layout);
+ tests::random_generator rg;
+ rg.set_seed("onednn_fc_sum_with_reshaped_network_input");
+ set_values(weights_mem, rg.generate_random_1d<ov::float16>(weights_layout.count(), -0.25f, 0.25f));
+ set_values(input_mem, rg.generate_random_1d<ov::float16>(in_layout.count(), -0.25f, 0.25f));
+ const auto residual_values = rg.generate_random_1d<ov::float16>(residual_layout.count(), -0.25f, 0.25f);
+ set_values(residual_mem, residual_values);
+
+ auto make_topology = [&]() {
+ topology topology;
+ topology.add(input_layout("input", in_layout));
+ topology.add(input_layout("residual", residual_layout));
+ topology.add(data("weights", weights_mem));
+ topology.add(fully_connected("fc", input_info("input"), "weights"));
+ topology.add(reshape("residual_view", input_info("residual"), false, {32, 16}, ov::PartialShape{32, 16}));
+ topology.add(eltwise("sum", {input_info("fc"), input_info("residual_view")}, eltwise_mode::sum));
+ // Not a reorder: that would fold into the FC, turn its output into f32 and rule out the sum by size alone.
+ topology.add(softmax("out", input_info("sum"), 1));
+ return topology;
+ };
+
+ auto config_ref = get_test_default_config(engine);
+ config_ref.set_property(ov::intel_gpu::optimize_data(false));
+ config_ref.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ network net_ref(engine, make_topology(), config_ref);
+
+ ov::intel_gpu::ImplementationDesc fc_impl = {format::bfyx, "", impl_types::onednn};
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ config.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{{"fc", fc_impl}}));
+ network net(program::build_program(engine, make_topology(), config), 0);
+
+ ASSERT_TRUE(net.get_primitive("residual_view")->can_be_optimized());
+ auto& fc_node = net.get_program()->get_node("fc");
+ ASSERT_EQ(fc_node.get_preferred_impl_type(), impl_types::onednn);
+ ASSERT_EQ(fc_node.get_fused_primitives().size(), 1u);
+ ASSERT_EQ(onednn_eltwise_fusing_helpers::get_reused_eltwmem_idx(fc_node), -1);
+
+ for (auto* n : {&net_ref, &net}) {
+ n->set_input_data("input", input_mem);
+ n->set_input_data("residual", residual_mem);
+ }
+ auto out_ref = net_ref.execute().at("out").get_memory();
+ auto out = net.execute().at("out").get_memory();
+ ASSERT_EQ(out_ref->count(), out->count());
+ {
+ cldnn::mem_lock<ov::float16> ref_ptr(out_ref, get_test_stream());
+ cldnn::mem_lock<ov::float16> out_ptr(out, get_test_stream());
+ for (size_t i = 0; i < out_ref->count(); i++) {
+ ASSERT_NEAR(static_cast<float>(ref_ptr[i]), static_cast<float>(out_ptr[i]), 1e-2f) << "index " << i;
+ }
+ }
+ cldnn::mem_lock<ov::float16> residual_ptr(residual_mem, get_test_stream());
+ for (size_t i = 0; i < residual_values.size(); i++) {
+ ASSERT_EQ(static_cast<float>(residual_ptr[i]), static_cast<float>(residual_values[i]))
+ << "index " << i << ": the caller's residual input was overwritten";
+ }
+}
+
+// A direct single-user network input feeding a fused sum must also use binary_add rather than
+// in-place sum: input_layout::set_data replaces the input's memory at runtime without rebinding
+// static users, which would leave the FC accumulating into an orphaned build-time placeholder.
+TEST(basic_memory_dependencies, onednn_fc_sum_with_direct_network_input) {
+ auto& engine = get_test_engine();
+ if (!engine.get_device_info().supports_immad)
+ GTEST_SKIP();
+
+ auto in_layout = layout{ov::PartialShape({32, 16}), data_types::f16, format::bfyx};
+ auto residual_layout = layout{ov::PartialShape({32, 16}), data_types::f16, format::bfyx};
+ auto weights_layout = layout{ov::PartialShape({16, 16}), data_types::f16, format::bfyx};
+ auto weights_mem = engine.allocate_memory(weights_layout);
+ auto input_mem = engine.allocate_memory(in_layout);
+ auto residual_mem = engine.allocate_memory(residual_layout);
+ tests::random_generator rg;
+ rg.set_seed("onednn_fc_sum_with_direct_network_input");
+ set_values(weights_mem, rg.generate_random_1d<ov::float16>(weights_layout.count(), -0.25f, 0.25f));
+ set_values(input_mem, rg.generate_random_1d<ov::float16>(in_layout.count(), -0.25f, 0.25f));
+ const auto residual_values = rg.generate_random_1d<ov::float16>(residual_layout.count(), 1.0f, 2.0f);
+ set_values(residual_mem, residual_values);
+
+ auto make_topology = [&]() {
+ topology topology;
+ topology.add(input_layout("input", in_layout));
+ topology.add(input_layout("residual", residual_layout));
+ topology.add(data("weights", weights_mem));
+ topology.add(fully_connected("fc", input_info("input"), "weights"));
+ topology.add(eltwise("sum", {input_info("fc"), input_info("residual")}, eltwise_mode::sum));
+ topology.add(softmax("out", input_info("sum"), 1));
+ return topology;
+ };
+
+ auto config_ref = get_test_default_config(engine);
+ config_ref.set_property(ov::intel_gpu::optimize_data(false));
+ config_ref.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ network net_ref(engine, make_topology(), config_ref);
+
+ ov::intel_gpu::ImplementationDesc fc_impl = {format::bfyx, "", impl_types::onednn};
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ config.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{{"fc", fc_impl}}));
+ network net(program::build_program(engine, make_topology(), config), 0);
+
+ auto& fc_node = net.get_program()->get_node("fc");
+ ASSERT_EQ(fc_node.get_preferred_impl_type(), impl_types::onednn);
+ ASSERT_EQ(fc_node.get_fused_primitives().size(), 1u);
+ ASSERT_EQ(onednn_eltwise_fusing_helpers::get_reused_eltwmem_idx(fc_node), -1);
+
+ for (auto* n : {&net_ref, &net}) {
+ n->set_input_data("input", input_mem);
+ n->set_input_data("residual", residual_mem);
+ }
+ auto out_ref = net_ref.execute().at("out").get_memory();
+ auto out = net.execute().at("out").get_memory();
+ ASSERT_EQ(out_ref->count(), out->count());
+ {
+ cldnn::mem_lock<ov::float16> ref_ptr(out_ref, get_test_stream());
+ cldnn::mem_lock<ov::float16> out_ptr(out, get_test_stream());
+ for (size_t i = 0; i < out_ref->count(); i++) {
+ ASSERT_NEAR(static_cast<float>(ref_ptr[i]), static_cast<float>(out_ptr[i]), 1e-2f) << "index " << i;
+ }
+ }
+ cldnn::mem_lock<ov::float16> residual_ptr(residual_mem, get_test_stream());
+ for (size_t i = 0; i < residual_values.size(); i++) {
+ ASSERT_EQ(static_cast<float>(residual_ptr[i]), static_cast<float>(residual_values[i]))
+ << "index " << i << ": the caller's residual input was overwritten";
+ }
+}
+
+// A network input reached through an optimized-out layout-compatible reorder has no memory at
+// build time either, and must also use a binary post-op instead of in-place sum.
+TEST(basic_memory_dependencies, onednn_fc_sum_with_reordered_network_input) {
+ auto& engine = get_test_engine();
+ if (!engine.get_device_info().supports_immad)
+ GTEST_SKIP();
+
+ auto in_layout = layout{ov::PartialShape({32, 16}), data_types::f16, format::bfyx};
+ // For a 2D shape {32, 16} (y=1, x=1), byxf and bfyx have identical memory layout (compatible, not
+ // identical), so remove_redundant_reorders marks the reorder can_be_optimized(true) and keeps it.
+ auto residual_layout = layout{ov::PartialShape({32, 16}), data_types::f16, format::byxf};
+ auto weights_layout = layout{ov::PartialShape({16, 16}), data_types::f16, format::bfyx};
+ auto weights_mem = engine.allocate_memory(weights_layout);
+ auto input_mem = engine.allocate_memory(in_layout);
+ auto residual_mem = engine.allocate_memory(residual_layout);
+ tests::random_generator rg;
+ rg.set_seed("onednn_fc_sum_with_reordered_network_input");
+ set_values(weights_mem, rg.generate_random_1d<ov::float16>(weights_layout.count(), -0.25f, 0.25f));
+ set_values(input_mem, rg.generate_random_1d<ov::float16>(in_layout.count(), -0.25f, 0.25f));
+ const auto residual_values = rg.generate_random_1d<ov::float16>(residual_layout.count(), -0.25f, 0.25f);
+ set_values(residual_mem, residual_values);
+
+ auto make_topology = [&]() {
+ topology topology;
+ topology.add(input_layout("input", in_layout));
+ topology.add(input_layout("residual", residual_layout));
+ topology.add(data("weights", weights_mem));
+ topology.add(fully_connected("fc", input_info("input"), "weights"));
+ topology.add(reorder("residual_view", input_info("residual"), format::bfyx, data_types::f16));
+ topology.add(eltwise("sum", {input_info("fc"), input_info("residual_view")}, eltwise_mode::sum));
+ topology.add(softmax("out", input_info("sum"), 1));
+ return topology;
+ };
+
+ auto config_ref = get_test_default_config(engine);
+ config_ref.set_property(ov::intel_gpu::optimize_data(false));
+ config_ref.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ network net_ref(engine, make_topology(), config_ref);
+
+ ov::intel_gpu::ImplementationDesc fc_impl = {format::bfyx, "", impl_types::onednn};
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ config.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{{"fc", fc_impl}}));
+ network net(program::build_program(engine, make_topology(), config), 0);
+
+ ASSERT_TRUE(net.get_primitive("residual_view")->can_be_optimized());
+ auto& fc_node = net.get_program()->get_node("fc");
+ ASSERT_EQ(fc_node.get_preferred_impl_type(), impl_types::onednn);
+ ASSERT_EQ(fc_node.get_fused_primitives().size(), 1u);
+ ASSERT_EQ(onednn_eltwise_fusing_helpers::get_reused_eltwmem_idx(fc_node), -1);
+
+ for (auto* n : {&net_ref, &net}) {
+ n->set_input_data("input", input_mem);
+ n->set_input_data("residual", residual_mem);
+ }
+ auto out_ref = net_ref.execute().at("out").get_memory();
+ auto out = net.execute().at("out").get_memory();
+ ASSERT_EQ(out_ref->count(), out->count());
+ {
+ cldnn::mem_lock<ov::float16> ref_ptr(out_ref, get_test_stream());
+ cldnn::mem_lock<ov::float16> out_ptr(out, get_test_stream());
+ for (size_t i = 0; i < out_ref->count(); i++) {
+ ASSERT_NEAR(static_cast<float>(ref_ptr[i]), static_cast<float>(out_ptr[i]), 1e-2f) << "index " << i;
+ }
+ }
+ cldnn::mem_lock<ov::float16> residual_ptr(residual_mem, get_test_stream());
+ for (size_t i = 0; i < residual_values.size(); i++) {
+ ASSERT_EQ(static_cast<float>(residual_ptr[i]), static_cast<float>(residual_values[i]))
+ << "index " << i << ": the caller's residual input was overwritten";
+ }
+}
+
+// Same reshaped-network-input pattern with eltwise_mode::prod (can_use_mul_inplace): must fuse as
+// binary_mul rather than binary_mul_inplace so the FC does not bind its output to a null view or
+// overwrite the caller's input buffer.
+TEST(basic_memory_dependencies, onednn_fc_mul_with_reshaped_network_input) {
+ auto& engine = get_test_engine();
+ if (!engine.get_device_info().supports_immad)
+ GTEST_SKIP();
+
+ auto in_layout = layout{ov::PartialShape({32, 16}), data_types::f16, format::bfyx};
+ auto mul_layout = layout{ov::PartialShape({1, 32, 16}), data_types::f16, format::bfyx};
+ auto weights_layout = layout{ov::PartialShape({16, 16}), data_types::f16, format::bfyx};
+ auto weights_mem = engine.allocate_memory(weights_layout);
+ auto input_mem = engine.allocate_memory(in_layout);
+ auto mul_mem = engine.allocate_memory(mul_layout);
+ tests::random_generator rg;
+ rg.set_seed("onednn_fc_mul_with_reshaped_network_input");
+ set_values(weights_mem, rg.generate_random_1d<ov::float16>(weights_layout.count(), -0.25f, 0.25f));
+ set_values(input_mem, rg.generate_random_1d<ov::float16>(in_layout.count(), -0.25f, 0.25f));
+ const auto mul_values = rg.generate_random_1d<ov::float16>(mul_layout.count(), -0.25f, 0.25f);
+ set_values(mul_mem, mul_values);
+
+ auto make_topology = [&]() {
+ topology topology;
+ topology.add(input_layout("input", in_layout));
+ topology.add(input_layout("mul_in", mul_layout));
+ topology.add(data("weights", weights_mem));
+ topology.add(fully_connected("fc", input_info("input"), "weights"));
+ topology.add(reshape("mul_view", input_info("mul_in"), false, {32, 16}, ov::PartialShape{32, 16}));
+ topology.add(eltwise("mul", {input_info("fc"), input_info("mul_view")}, eltwise_mode::prod));
+ topology.add(softmax("out", input_info("mul"), 1));
+ return topology;
+ };
+
+ auto config_ref = get_test_default_config(engine);
+ config_ref.set_property(ov::intel_gpu::optimize_data(false));
+ config_ref.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ network net_ref(engine, make_topology(), config_ref);
+
+ ov::intel_gpu::ImplementationDesc fc_impl = {format::bfyx, "", impl_types::onednn};
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ config.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{{"fc", fc_impl}}));
+ network net(program::build_program(engine, make_topology(), config), 0);
+
+ ASSERT_TRUE(net.get_primitive("mul_view")->can_be_optimized());
+ auto& fc_node = net.get_program()->get_node("fc");
+ ASSERT_EQ(fc_node.get_preferred_impl_type(), impl_types::onednn);
+ ASSERT_EQ(fc_node.get_fused_primitives().size(), 1u);
+ ASSERT_EQ(onednn_eltwise_fusing_helpers::get_reused_eltwmem_idx(fc_node), -1);
+
+ for (auto* n : {&net_ref, &net}) {
+ n->set_input_data("input", input_mem);
+ n->set_input_data("mul_in", mul_mem);
+ }
+ auto out_ref = net_ref.execute().at("out").get_memory();
+ auto out = net.execute().at("out").get_memory();
+ ASSERT_EQ(out_ref->count(), out->count());
+ {
+ cldnn::mem_lock<ov::float16> ref_ptr(out_ref, get_test_stream());
+ cldnn::mem_lock<ov::float16> out_ptr(out, get_test_stream());
+ for (size_t i = 0; i < out_ref->count(); i++) {
+ ASSERT_NEAR(static_cast<float>(ref_ptr[i]), static_cast<float>(out_ptr[i]), 1e-2f) << "index " << i;
+ }
+ }
+ cldnn::mem_lock<ov::float16> mul_ptr(mul_mem, get_test_stream());
+ for (size_t i = 0; i < mul_values.size(); i++) {
+ ASSERT_EQ(static_cast<float>(mul_ptr[i]), static_cast<float>(mul_values[i])) << "index " << i << ": the caller's multiplier input was overwritten";
+ }
+}
#endif // ENABLE_ONEDNN_FOR_GPU