Skip to content

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

intel_gpu: raise the onednn sum post-op ancestor-walk depth (#37855)

Damien Menigaux99830d93427a
diff --git a/src/plugins/intel_gpu/src/graph/program_helpers.cpp b/src/plugins/intel_gpu/src/graph/program_helpers.cpp
index a3c0ee2a7d3..234087e97bb 100644
--- a/src/plugins/intel_gpu/src/graph/program_helpers.cpp
+++ b/src/plugins/intel_gpu/src/graph/program_helpers.cpp
@@ -107,9 +107,37 @@ static bool is_direct_ancestor(const program_node& child, const program_node& ta
if (target.get_users().size() != 2)
return false;
- // Limit the iteration depth to 5 for performance reason
+ // The walk follows dependency(0) only, so this bounds the length of the single-input chain
+ // between the two nodes; the equality test precedes the hop, so a bound of N reaches a target
+ // at most N - 1 hops away.
+ //
+ // An attention residual sits 6 hops up. Walking dependency(0) from the projection that carries
+ // the post-op, on a graph dump of a multi-head attention block:
+ //
+ // 0 the output projection <- carries the fused eltwise sum
+ // 1 reshape <- heads folded back into the feature axis
+ // 2 the attention op
+ // 3 reshape <- feature axis split into heads
+ // 4 the QKV matmul
+ // 5 the norm
+ // 6 the block input <- the residual root, two users
+ //
+ // 7 is therefore the smallest bound that reaches it, and the bound is kept at that minimum
+ // deliberately. Note the two reshapes: it is easy to count this chain as 4 hops from the
+ // operator list alone and conclude a bound of 5 or 6 is enough, and both leave every
+ // attention residual demoted.
+ //
+ // Raising it beyond the minimum is not a compile-time cost -- the walk is a bounded pointer
+ // chase. It is a trade. What the result decides is add_fusing_type::sum versus binary_add, and
+ // both are post-ops on the same onednn primitive: sum lets onednn accumulate into the added
+ // tensor's buffer, at the price of disabling implicit concat on that predecessor
+ // (prepare_buffer_fusing), inhibiting reorder elimination in two passes (layout_optimizer and
+ // remove_redundant_reorders) and blocking buffer sharing (basic_memory_dependencies). So a
+ // longer chain should only be admitted against a workload that is measured to want it.
+ static constexpr int max_ancestor_walk_depth = 7;
+
const auto* iter = &child;
- for (int i = 0; i < 5; i++) {
+ for (int i = 0; i < max_ancestor_walk_depth; i++) {
if (iter == &target)
return true;
if (iter->get_dependencies().empty())
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 7281a2ef1d6..38ab671582a 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
@@ -180,3 +180,66 @@ TEST(add_onednn_optimization_attributes, fc_sum_u8_residual_input_uses_binary) {
auto fusing_type = onednn_add_fusing_helpers::get_add_fusing_type(fc_node, fused[0]);
ASSERT_EQ(fusing_type, add_fusing_type::binary_per_tensor);
}
+
+// 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) {
+ auto in_layout = layout{ov::PartialShape({1, 16, 32, 32}), data_types::f16, format::bfyx};
+ auto weight = engine.allocate_memory(layout{ov::PartialShape({16, 16, 1, 1}), data_types::f16, format::bfyx});
+
+ topology topology;
+ topology.add(input_layout("input", in_layout));
+ topology.add(data("weight", weight));
+ for (size_t i = 1; i <= depth; i++) {
+ auto prev = (i == 1) ? input_info("input") : input_info("conv" + std::to_string(i - 1));
+ topology.add(convolution("conv" + std::to_string(i), prev, "weight", "", 1, {1, 1}, {1, 1}, {0, 0}, {0, 0}, false));
+ }
+ auto last = "conv" + std::to_string(depth);
+ topology.add(eltwise("eltwise", input_info("conv1"), input_info(last), eltwise_mode::sum));
+ topology.add(reorder("reorder", input_info("eltwise"), 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, false);
+
+ 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& last_node = prog->get_node(last);
+ auto& cldnn_post_ops = last_node.get_fused_primitives();
+ EXPECT_EQ(cldnn_post_ops.size(), 1u) << "the residual eltwise did not fuse into " << last;
+ if (cldnn_post_ops.size() != 1)
+ return add_fusing_type::not_supported;
+ // conv1 feeds both conv2 and the fused eltwise, which is what puts get_add_fusing_type on the
+ // is_direct_ancestor path rather than the single-user shortcut.
+ EXPECT_EQ(prog->get_node("conv1").get_users().size(), 2u);
+ return onednn_add_fusing_helpers::get_add_fusing_type(last_node, cldnn_post_ops[0]);
+}
+
+// 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.
+TEST(add_onednn_optimization_attributes, sum_post_op_for_six_hop_residual) {
+ auto& engine = get_test_engine();
+
+ if (!engine.get_device_info().supports_immad)
+ return;
+
+ ASSERT_EQ(residual_chain_fusing_type(engine, 7), add_fusing_type::sum);
+}
+
+// Pins the bound from both sides, so that neither raising nor lowering it passes unnoticed.
+// The two depths straddle max_ancestor_walk_depth in program_helpers.cpp: a chain of n
+// convolutions puts the target n-1 hops up, and a bound of N reaches N-1 hops. The lower side is
+// the same depth the test above uses, since the bound is held at the minimum that case needs.
+TEST(add_onednn_optimization_attributes, ancestor_walk_depth_boundary) {
+ auto& engine = get_test_engine();
+
+ if (!engine.get_device_info().supports_immad)
+ return;
+
+ ASSERT_EQ(residual_chain_fusing_type(engine, 7), add_fusing_type::sum);
+ ASSERT_EQ(residual_chain_fusing_type(engine, 8), add_fusing_type::binary_per_tensor);
+}