OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] Sum post op bug fix when input[0]==sum_target (#38715)
Łukasz Ślusarczyk2665d014ff0c
diff --git a/src/plugins/intel_gpu/include/intel_gpu/graph/fused_primitive_desc.hpp b/src/plugins/intel_gpu/include/intel_gpu/graph/fused_primitive_desc.hppindex 7bb34114a6e..2732821d3ef 100644--- a/src/plugins/intel_gpu/include/intel_gpu/graph/fused_primitive_desc.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/graph/fused_primitive_desc.hpp@@ -105,10 +105,13 @@ struct fused_primitive_desc {std::vector<std::pair<primitive_id, size_t>> deps;std::map<primitive_id, size_t> fused_deps;- // TODO:- // Currently, it assumes very simple case where dep 0 is the fused node and no input sharing b/w fused node and peer node- // To cover such cases where some of the peer node uses input of fused node, we need to maintain actual indexes of the dependencies- // not only the "starting index".+ // TODO: store the actual dependency indices with an alias flag, not only the start of the block.+ // If the peer node and the fused node share a dependency, it is appended a second time instead of+ // being marked as an alias. The second entry adds one to the shared node's user count. Passes that+ // require a single user then skip the node.+ // Do not just drop the duplicate because it is the only record of the sharing in the graph. Without an+ // alias flag the node reports one user. One user is taken as permission to reuse its buffer in place,+ // for example by get_add_fusing_type.int32_t outer_dep_start_idx = -1; // if -1, no external dep after fusingsize_t total_num_deps = 0;};diff --git a/src/plugins/intel_gpu/src/graph/program_helpers.cpp b/src/plugins/intel_gpu/src/graph/program_helpers.cppindex cc63448ba62..13af4d10a1a 100644--- a/src/plugins/intel_gpu/src/graph/program_helpers.cpp+++ b/src/plugins/intel_gpu/src/graph/program_helpers.cpp@@ -160,6 +160,13 @@ add_fusing_type onednn_eltwise_fusing_helpers::get_add_fusing_type(if (!desc.has_outer_dep()) {return add_fusing_type::not_supported;}+ // prepare_primitive_fusing only fuses a two-input eltwise with p_node as one input, so exactly one+ // dependency arrives here and the single index below is enough.+ OPENVINO_ASSERT(desc.deps.size() == 1,+ "[GPU] A fused eltwise sum must bring exactly one dependency, got ",+ desc.deps.size(),+ " on node ",+ p_node.id());auto& dep_node = p_node.get_dependency(desc.outer_dep_start_idx);auto p_layout = p_node.get_output_layout();auto d_layout = dep_node.get_output_layout();@@ -168,6 +175,18 @@ add_fusing_type onednn_eltwise_fusing_helpers::get_add_fusing_type(return add_fusing_type::not_supported;}+ // onednn accumulates a sum post-op into dep_node's buffer, so p_node must not also read it;+ // is_direct_ancestor answers yes for an own input too+ auto p_node_consumes_dep_node = [&]() {+ // has_outer_dep() above rejects a negative index.+ const auto outer_dep_start_idx = static_cast<size_t>(desc.outer_dep_start_idx);+ for (size_t i = 0; i < outer_dep_start_idx; i++) {+ if (&p_node.get_dependency(i) == &dep_node)+ return true;+ }+ return false;+ };+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()@@ -176,7 +195,8 @@ add_fusing_type onednn_eltwise_fusing_helpers::get_add_fusing_type(&& !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)) {+ && (!dep_node.is_type<input_layout>() || dep_node.get_users().size() <= 1)+ && !p_node_consumes_dep_node()) {return add_fusing_type::sum;}if (p_layout.get_tensor() == d_layout.get_tensor()) {diff --git a/src/plugins/intel_gpu/tests/functional/sources.cmake b/src/plugins/intel_gpu/tests/functional/sources.cmakeindex 348656847b4..f7494308417 100644--- a/src/plugins/intel_gpu/tests/functional/sources.cmake+++ b/src/plugins/intel_gpu/tests/functional/sources.cmake@@ -323,6 +323,7 @@ set(GPU_FUNCTIONAL_TEST_SOURCES${CMAKE_CURRENT_LIST_DIR}/single_layer_tests/selective_ssm.cpp${CMAKE_CURRENT_LIST_DIR}/single_layer_tests/tensor_iterator.cpp${CMAKE_CURRENT_LIST_DIR}/subgraph_tests/condition.cpp+ ${CMAKE_CURRENT_LIST_DIR}/subgraph_tests/convolution_sum_fusion.cpp${CMAKE_CURRENT_LIST_DIR}/subgraph_tests/dynamic/activations_scaling.cpp${CMAKE_CURRENT_LIST_DIR}/subgraph_tests/dynamic/broadcast_eltwise.cpp${CMAKE_CURRENT_LIST_DIR}/subgraph_tests/dynamic/concat_in_place_dynamic_batch.cppdiff --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.cppnew file mode 100644index 00000000000..eddc02f5493--- /dev/null+++ b/src/plugins/intel_gpu/tests/functional/subgraph_tests/convolution_sum_fusion.cpp@@ -0,0 +1,52 @@+// Copyright (C) 2018-2026 Intel Corporation+// 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/convolution.hpp"+#include "openvino/op/parameter.hpp"++namespace {+// The added tensor is the input of the same convolution, so a sum post-op would let onednn+// accumulate into the buffer the convolution reads from.+class ConvolutionOwnInputSum : public testing::WithParamInterface<ov::Shape>,+ virtual public ov::test::SubgraphBaseStaticTest {+public:+ static std::string getTestCaseName(const testing::TestParamInfo<ov::Shape>& params) {+ std::ostringstream result;+ result << "IS=" << ov::test::utils::partialShape2str({params.param});+ return result.str();+ }++protected:+ void SetUp() override {+ targetDevice = ov::test::utils::DEVICE_GPU;+ const auto type = ov::element::f16;+ const auto shape = GetParam();+ // the kernel window accumulates many terms in f16, so keep the tolerance relative+ rel_threshold = 0.02f;++ auto input = std::make_shared<ov::op::v0::Parameter>(type, shape);+ auto weights = ov::op::v0::Constant::create(type, ov::Shape{shape[1], shape[1], 3, 3}, {1});+ auto conv = std::make_shared<ov::op::v1::Convolution>(input, weights, ov::Strides{1, 1},+ ov::CoordinateDiff{1, 1}, ov::CoordinateDiff{1, 1},+ ov::Strides{1, 1});+ auto sum = std::make_shared<ov::op::v1::Add>(conv, input);++ function = std::make_shared<ov::Model>(ov::OutputVector{sum}, ov::ParameterVector{input});+ }+};++TEST_P(ConvolutionOwnInputSum, Inference) {+ run();+}++INSTANTIATE_TEST_SUITE_P(smoke_GPU_Convolution, ConvolutionOwnInputSum,+ ::testing::Values(ov::Shape{1, 1, 4, 4},+ ov::Shape{1, 16, 16, 16}),+ ConvolutionOwnInputSum::getTestCaseName);++} // namespacediff --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.cppindex 5c1d18f5dd2..c2424065682 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@@ -218,6 +218,19 @@ static add_fusing_type residual_chain_fusing_type(cldnn::engine& engine, size_treturn onednn_eltwise_fusing_helpers::get_add_fusing_type(last_node, cldnn_post_ops[0]);}+// The degenerate end of the same construction. A chain of two puts conv1 one hop up, which makes+// it conv2's own input, and is_direct_ancestor tests for equality before the first hop so it+// answers yes here too. An in-place sum would then leave the convolution reading and writing one+// buffer, so the addend has to arrive as a binary post-op instead.+TEST(add_onednn_optimization_attributes, sum_post_op_refused_for_own_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";++ ASSERT_EQ(residual_chain_fusing_type(engine, 2), add_fusing_type::binary_per_tensor);+}+// An attention residual reaches six hops back -- projection, reshape, attention, reshape, QKV// matmul, norm, block input -- and the walk must still find it. See the hop-by-hop listing on// max_ancestor_walk_depth in program_helpers.cpp.