Skip to content

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

[GPU]Optimize unfused subgraph usage based on fusion validity (#39126)

liqianhao1112abe4c91db66
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 52ec687c509..4810f0c728a 100644
--- a/src/plugins/intel_gpu/src/graph/include/primitive_inst.h
+++ b/src/plugins/intel_gpu/src/graph/include/primitive_inst.h
@@ -350,6 +350,7 @@ public:
bool is_constant() const { return _is_constant; }
bool needs_completion_event() const { return _needs_completion_event; }
bool has_unfused_subgraph() const { return (_unfused_subgraph != nullptr); }
+ bool use_unfused_subgraph() const { return _use_unfused_subgraph; }
bool has_node() const { return _node != nullptr; }
bool has_inner_networks() const;
void allocate_internal_buffers(bool reset = true);
@@ -442,6 +443,8 @@ protected:
// Needed for dynamic path only, as fusion in some cases may be illegal, but it can't be checked on program build phase,
// thus we do less restrictive fusion with runtime sanity check and unfusion when needed.
cldnn::network::ptr _unfused_subgraph = nullptr;
+ // Fusion validity depends on runtime shapes, so the cached subgraph is used only when the current execution needs it.
+ bool _use_unfused_subgraph = false;
// _output is optional because its initialization might be postponed (reshape_inst may either allocate it's own
// buffer or attach input as output
diff --git a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
index dd71e5c0dfd..27790fdcb61 100644
--- a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
+++ b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
@@ -2630,9 +2630,17 @@ void primitive_inst::prepare_primitive() {
if (!is_valid_fusion()) {
OV_ITT_SCOPED_TASK(ov::intel_gpu::itt::domains::intel_gpu_plugin, openvino::itt::handle("unfused_subgraph_build: " + id()));
get_unfused_subgraph();
+ _use_unfused_subgraph = true;
return;
}
+ if (_use_unfused_subgraph) {
+ // The output still points to the unfused subgraph's buffer, so re-select impl and reallocate output.
+ _use_unfused_subgraph = false;
+ clear_output_memory();
+ set_flag(ExecutionFlags::SHAPE_CHANGED);
+ }
+
// Try update impl if current impl is dynamic because opt kernel may be added to impl cache through async compilation.
// Only try update weight and realloc when impl is updated.
const bool can_use_async_compilation = use_async_compilation();
@@ -2771,7 +2779,7 @@ void primitive_inst::execute() {
return;
}
- if (_unfused_subgraph != nullptr) {
+ if (_use_unfused_subgraph) {
GPU_DEBUG_TRACE << "Now execute unfused subgraph " << _node->id() << std::endl;
for (auto& d : _deps) {
if (!d.first->get_node().is_type<data>()) {
@@ -2801,7 +2809,14 @@ void primitive_inst::execute() {
OPENVINO_ASSERT(outputs.find(last_prim_id) != outputs.end(), "[GPU] Can't find output primitive ", last_prim_id, " for unfused subgraph");
- _outputs[0] = outputs.at(last_prim_id).get_memory();
+ auto subgraph_output = outputs.at(last_prim_id).get_memory();
+ // Consumers rebind kernel arguments only when MEMORY_CHANGED is set.
+ if (!_outputs[0] || !get_network().get_engine().is_the_same_buffer(*_outputs[0], *subgraph_output)) {
+ set_flag(ExecutionFlags::MEMORY_CHANGED);
+ }
+ // The pooled fused output would otherwise stay registered to this primitive until network destruction.
+ release_and_clear_output_memory();
+ _outputs[0] = std::move(subgraph_output);
_impl_params->output_layouts[0] = _unfused_subgraph->get_output_layout(last_prim_id);
set_out_event(outputs.at(last_prim_id).get_event());
@@ -3540,7 +3555,7 @@ bool primitive_inst::is_valid_fusion() const {
}
}
- const auto& out_pshape = (_unfused_subgraph != nullptr && !get_flag(ExecutionFlags::SHAPE_CHANGED))
+ const auto& out_pshape = (_use_unfused_subgraph && !get_flag(ExecutionFlags::SHAPE_CHANGED))
? _unfused_subgraph->get_primitive(get_node().id())->get_output_layout().get_partial_shape()
: _impl_params->get_output_layout().get_partial_shape();
for (auto& fd : fused_eltwise_prims) {
diff --git a/src/plugins/intel_gpu/tests/unit/passes/prepare_primitive_fusing_test.cpp b/src/plugins/intel_gpu/tests/unit/passes/prepare_primitive_fusing_test.cpp
index 8c170dacbdd..53c22eeda30 100644
--- a/src/plugins/intel_gpu/tests/unit/passes/prepare_primitive_fusing_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/passes/prepare_primitive_fusing_test.cpp
@@ -14,6 +14,7 @@
#include "intel_gpu/graph/program.hpp"
#include "intel_gpu/primitives/concatenation.hpp"
#include "intel_gpu/primitives/permute.hpp"
+#include "intel_gpu/primitives/pooling.hpp"
#include "intel_gpu/primitives/scatter_update.hpp"
#include "intel_gpu/runtime/engine.hpp"
#include "pass_manager.h"
@@ -513,7 +514,7 @@ TEST(prepare_primitive_fusing, eltwise_fusing_residual_connection) {
auto conv_in_layout = layout{ ov::PartialShape{1, 3, -1, -1}, data_types::f16, format::bfyx};
auto weight_layout = layout{ ov::PartialShape{10, 3, 3, 3}, data_types::f16, format::bfyx};
auto weight_mem = engine.allocate_memory(weight_layout);
- auto weight_data = rg.generate_random_4d<ov::float16>(10, 3, 3, 3, -1, 1);
+ auto weight_data = rg.generate_random_1d<ov::float16>(10 * 3 * 3 * 3, -1, 1);
set_values(weight_mem, weight_data);
auto elt1_in1_layout = layout{ ov::PartialShape{1, 10, -1, -1}, data_types::f16, format::bfyx};
@@ -538,29 +539,129 @@ TEST(prepare_primitive_fusing, eltwise_fusing_residual_connection) {
cldnn::network net(prog, 0);
// Valid
- auto conv_input_data = rg.generate_random_4d<ov::float16>(1, 3, 7, 7, -1, 1);
+ auto conv_input_data = rg.generate_random_1d<ov::float16>(1 * 3 * 7 * 7, -1, 1);
auto conv_input_mem = engine.allocate_memory(layout{ov::PartialShape{1, 3, 7, 7}, data_types::f16, format::bfyx});
set_values(conv_input_mem, conv_input_data);
- auto elt_input_data = rg.generate_random_4d<ov::float16>(1, 10, 5, 5, -10, 10);
+ auto elt_input_data = rg.generate_random_1d<ov::float16>(1 * 10 * 5 * 5, -10, 10);
auto elt_input_mem = engine.allocate_memory(layout{ov::PartialShape{1, 10, 5, 5}, data_types::f16, format::bfyx});
set_values(elt_input_mem, elt_input_data);
net.set_input_data("conv_input", conv_input_mem);
net.set_input_data("elt1_input", elt_input_mem);
- net.execute();
+ auto read_output = [&](const std::map<primitive_id, network_output>& outputs) {
+ auto mem = outputs.at("reorder").get_memory();
+ mem_lock<float, mem_lock_type::read> lock(mem, get_test_stream());
+ return std::vector<float>(lock.data(), lock.data() + mem->get_layout().count());
+ };
+
+ const auto fused_ref = read_output(net.execute());
const auto& conv_inst = net.get_primitive("conv");
ASSERT_FALSE(conv_inst->has_unfused_subgraph());
+ ASSERT_FALSE(conv_inst->use_unfused_subgraph());
// Invalid => unfusion
- auto conv_input_data2 = rg.generate_random_4d<ov::float16>(1, 3, 3, 3, -1, 1);
+ auto conv_input_data2 = rg.generate_random_1d<ov::float16>(1 * 3 * 3 * 3, -1, 1);
auto conv_input_mem2 = engine.allocate_memory(layout{ov::PartialShape{1, 3, 3, 3}, data_types::f16, format::bfyx});
set_values(conv_input_mem2, conv_input_data2);
net.set_input_data("conv_input", conv_input_mem2);
net.set_input_data("elt1_input", elt_input_mem);
net.execute();
ASSERT_TRUE(conv_inst->has_unfused_subgraph());
+ ASSERT_TRUE(conv_inst->use_unfused_subgraph());
+
+ // Valid again => fused primitive is used while the unfused subgraph stays cached
+ net.set_input_data("conv_input", conv_input_mem);
+ net.set_input_data("elt1_input", elt_input_mem);
+ const auto fused_again = read_output(net.execute());
+ ASSERT_TRUE(conv_inst->has_unfused_subgraph());
+ ASSERT_FALSE(conv_inst->use_unfused_subgraph());
+ ASSERT_EQ(fused_ref, fused_again);
+}
+
+// Reproducer for two issues found in the unfused-subgraph path (PR #39126 review):
+// - N1 (High): a non-output consumer reads a stale fused buffer on every valid -> invalid transition,
+// because the unfused execution swaps _outputs[0] without publishing MEMORY_CHANGED.
+// - F1 (Medium): one pooled fused output buffer is orphaned per fused -> unfused transition.
+// Sequence: valid(b2,x1) -> INVALID(b1,x2) -> valid(b2,x1) -> INVALID(b1,x3) -> valid(b2,x1).
+// Expected per step (fc weights all-ones [4,8], dep = [[1,1,1,1],[2,2,2,2]], pool = 1x1 identity):
+// valid: fc = [2,4] (all 8), fused prod dep -> rows (8,16)
+// INVALID: fc = [1,4] (all 16), unfused broadcast -> rows (16,32) (N1 stale-read gives (8,16))
+// INVALID x3: fc = [1,4] (all 24) -> rows (24,48) (N1 stale-read gives (8,16))
+TEST(prepare_primitive_fusing, unfused_subgraph_stale_consumer_and_pool_growth) {
+ auto& engine = get_test_engine();
+
+ auto w_mem = engine.allocate_memory(layout{ov::PartialShape{4, 8}, data_types::f16, format::bfyx});
+ set_values(w_mem, std::vector<ov::float16>(32, ov::float16(1.0f)));
+ auto d_mem = engine.allocate_memory(layout{ov::PartialShape{2, 4}, data_types::f16, format::bfyx});
+ {
+ std::vector<ov::float16> dv(8, ov::float16(1.0f));
+ for (size_t i = 4; i < 8; ++i) dv[i] = ov::float16(2.0f);
+ set_values(d_mem, dv);
+ }
+
+ topology topo;
+ topo.add(input_layout("fc_input", layout{ov::PartialShape{-1, 8}, data_types::f16, format::bfyx}));
+ topo.add(data("weights", w_mem));
+ topo.add(data("dep", d_mem));
+ topo.add(fully_connected("fc", input_info("fc_input"), {"weights"}, "", data_types::f16));
+ topo.add(eltwise("eltw", { input_info("fc"), input_info("dep") }, eltwise_mode::prod));
+ topo.add(pooling("pool", input_info("eltw"), pooling_mode::max, {1, 1}, {1, 1}));
+ topo.add(reorder("reorder", input_info("pool"), 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));
+ // Full program pipeline: production memory restrictions are required for the F1 pool-growth
+ // assertion to be meaningful (a partial build has no self-restriction and self-heals the pool).
+ auto prog = program::build_program(engine, topo, config, false, false);
+ ASSERT_NE(prog, nullptr);
+ cldnn::network net(prog, 0);
+
+ const auto fc_inst = net.get_primitive("fc");
+ ASSERT_NE(fc_inst, nullptr);
+
+ auto make_in = [&](size_t batch, float v) {
+ auto m = engine.allocate_memory(layout{ov::PartialShape{(int)batch, 8}, data_types::f16, format::bfyx});
+ set_values(m, std::vector<ov::float16>(batch * 8, ov::float16(v)));
+ return m;
+ };
+ auto read_out = [&](const std::map<primitive_id, network_output>& outputs) {
+ auto om = outputs.at("reorder").get_memory();
+ mem_lock<float, mem_lock_type::read> lock(om, get_test_stream());
+ return std::vector<float>(lock.data(), lock.data() + 8);
+ };
+ auto ex = [&]() { return net.execute(); };
+
+ net.set_input_data("fc_input", make_in(2, 1.0f));
+ auto o1 = read_out(ex());
+ const size_t pool_after_first_valid = net.get_memory_pool().get_non_padded_pool_size();
+
+ net.set_input_data("fc_input", make_in(1, 2.0f));
+ auto o2 = read_out(ex());
+ ASSERT_TRUE(fc_inst->use_unfused_subgraph()) << "invalid step must run the unfused subgraph";
+
+ net.set_input_data("fc_input", make_in(2, 1.0f));
+ auto o3 = read_out(ex());
+ ASSERT_FALSE(fc_inst->use_unfused_subgraph()) << "switch back to fused";
+
+ net.set_input_data("fc_input", make_in(1, 3.0f));
+ auto o4 = read_out(ex());
+
+ net.set_input_data("fc_input", make_in(2, 1.0f));
+ auto o5 = read_out(ex());
+ const size_t pool_final = net.get_memory_pool().get_non_padded_pool_size();
+
+ // N1: non-output consumer (pool) must see the fresh subgraph result on invalid steps
+ EXPECT_EQ(o1[0], 8.0f); EXPECT_EQ(o1[4], 16.0f);
+ EXPECT_EQ(o2[0], 16.0f); EXPECT_EQ(o2[4], 32.0f) << "N1: consumer read stale fused buffer";
+ EXPECT_EQ(o3[0], 8.0f); EXPECT_EQ(o3[4], 16.0f);
+ EXPECT_EQ(o4[0], 24.0f); EXPECT_EQ(o4[4], 48.0f) << "N1: consumer read stale fused buffer";
+ EXPECT_EQ(o5[0], 8.0f); EXPECT_EQ(o5[4], 16.0f);
+
+ // F1: pool must not grow across the valid <-> invalid transitions
+ EXPECT_LE(pool_final, pool_after_first_valid) << "F1: pooled fused output buffer orphaned per transition";
}
TEST(prepare_primitive_fusing, fuse_constant_transposes_removal_check) {