Skip to content

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

[GPU] Select new impl when propagate constant makes dynamic shape layer to constant (#34115)

Wilson Seok9d8ca1390391
diff --git a/src/plugins/intel_gpu/src/graph/graph_optimizer/propagate_constants.cpp b/src/plugins/intel_gpu/src/graph/graph_optimizer/propagate_constants.cpp
index 85a7e599da5..8e1ff2e43e4 100644
--- a/src/plugins/intel_gpu/src/graph/graph_optimizer/propagate_constants.cpp
+++ b/src/plugins/intel_gpu/src/graph/graph_optimizer/propagate_constants.cpp
@@ -10,7 +10,9 @@
#include "intel_gpu/graph/program.hpp"
#include "intel_gpu/graph/network.hpp"
#include "data_inst.h"
+#include "mutable_data_inst.h"
#include "intel_gpu/runtime/itt.hpp"
+#include "registry/implementation_manager.hpp"
#ifdef ENABLE_ONEDNN_FOR_GPU
#include "reorder_inst.h"
#include "graph/impls/onednn/utils.hpp"
@@ -19,9 +21,73 @@
#include <list>
#include <memory>
#include <utility>
+#include <unordered_set>
+#include <queue>
using namespace cldnn;
+namespace {
+// Attempts to reselect an appropriate implementation for a node after
+// propagate_constants transforms dynamic inputs into static data.
+// Refreshes stale output layouts before building kernel params to avoid
+// incorrect shape_type classification.
+void try_reselect_impl_for_node(program_node* node) {
+ bool can_select_impl = !node->is_type<data>() && !(node->is_type<mutable_data>() && node->get_dependencies().empty());
+ if (!can_select_impl)
+ return;
+
+ auto selected_impl = node->get_selected_impl();
+ bool has_selected_impl = selected_impl != nullptr;
+ bool need_new_impl_selection = !has_selected_impl;
+
+ if (has_selected_impl && !node->is_valid_output_layout()) {
+ bool is_node_dynamic = node->get_output_layout(false).is_dynamic();
+ bool is_impl_dynamic = selected_impl->is_dynamic();
+ need_new_impl_selection = (is_node_dynamic != is_impl_dynamic);
+ }
+
+ if (!need_new_impl_selection)
+ return;
+
+ // Refresh stale output layouts before building kernel params.
+ // After invalidate_users(), cached output_layouts may still reflect
+ // the old dynamic shape. Recomputing ensures get_kernel_impl_params()
+ // uses up-to-date layouts for accurate shape_type determination.
+ if (!node->is_all_valid_output_layouts()) {
+ node->get_output_layouts(false);
+ }
+
+ auto params = node->get_kernel_impl_params();
+ auto shape_type = ImplementationManager::get_shape_type(*params);
+ if (shape_type == shape_types::dynamic_shape)
+ return;
+
+ auto selected_impl_manager = node->type()->choose_impl(*node, shape_type);
+ std::string fail_reason;
+ try {
+ if (selected_impl_manager) {
+ node->set_selected_impl(selected_impl_manager->create(*node, *params));
+ } else {
+ fail_reason = "choose_impl returned nullptr (no matching implementation found)";
+ }
+ } catch (const std::exception& e) {
+ fail_reason = e.what();
+ }
+
+ OPENVINO_ASSERT(node->get_selected_impl() != nullptr,
+ "[GPU] Failed to select implementation after propagate_constants"
+ "\nname:",
+ node->id(),
+ "\ntype: ",
+ node->get_primitive()->type_string(),
+ "\noriginal_type: ",
+ node->get_primitive()->origin_op_type_name,
+ " ",
+ fail_reason);
+}
+
+} // namespace
+
// ToDo remove friendship relation from program_node and program
void propagate_constants::run(program& p) {
OV_ITT_SCOPED_TASK(ov::intel_gpu::itt::domains::intel_gpu_plugin, "pass::PropagateConstants");
@@ -74,10 +140,11 @@ void propagate_constants::run(program& p) {
// replace all constant nodes which are relevant for inference (either used by non-const user or marked as output)
// with recomputed cldnn::data
- for (auto& cout : to_replace) {
- auto& id_to_replace = std::get<0>(cout);
- auto mem_impl = std::get<1>(cout);
- auto cache_info = std::get<2>(cout);
+ std::unordered_set<program_node*> reselection_targets;
+ for (auto& entry : to_replace) {
+ auto& id_to_replace = std::get<0>(entry);
+ auto mem_impl = std::get<1>(entry);
+ auto cache_info = std::get<2>(entry);
auto cache_manager = std::get<0>(cache_info);
auto in_layout = std::get<1>(cache_info);
auto reorder = std::get<2>(cache_info);
@@ -109,8 +176,37 @@ void propagate_constants::run(program& p) {
curr_node.users.end(),
[](program_node* node) { return node->is_constant(); }),
curr_node.users.end());
+ bool was_dynamic = curr_node.get_output_layout().is_dynamic();
p.replace(curr_node, new_node);
- new_node.recalc_output_layout(false);
+ new_node.recalc_output_layout(was_dynamic);
+
+ // Collect transitively affected user nodes when dynamic → static transition occurs.
+ // Only users of constants that transitioned from dynamic to static need impl reselection.
+ if (was_dynamic && !new_node.get_output_layout(false).is_dynamic()) {
+ std::queue<program_node*> queue;
+ for (auto& user : new_node.get_users()) {
+ queue.push(user);
+ }
+ while (!queue.empty()) {
+ auto* n = queue.front();
+ queue.pop();
+ if (reselection_targets.count(n) > 0)
+ continue;
+ reselection_targets.insert(n);
+ if (!n->is_all_valid_output_layouts()) {
+ for (auto& user : n->get_users()) {
+ queue.push(user);
+ }
+ }
+ }
+ }
+ }
+
+ // propagate_constants is executed after compile_graph pass.
+ // If some users become static due to propagated constants, they can end up without selected_impl.
+ // Re-select implementation for affected nodes to avoid runtime _impl-nullptr validation failure.
+ for (auto* node : reselection_targets) {
+ try_reselect_impl_for_node(node);
}
}
diff --git a/src/plugins/intel_gpu/tests/unit/passes/propagate_constants_test.cpp b/src/plugins/intel_gpu/tests/unit/passes/propagate_constants_test.cpp
new file mode 100644
index 00000000000..a66877f5ade
--- /dev/null
+++ b/src/plugins/intel_gpu/tests/unit/passes/propagate_constants_test.cpp
@@ -0,0 +1,194 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "test_utils.h"
+#include "program_wrapper.h"
+#include "pass_manager.h"
+
+using namespace cldnn;
+using namespace ::tests;
+
+// Verifies that when constant propagation transforms a dynamic-layout constant node into
+// static data, downstream consumers receive updated static implementations.
+//
+// In real models (e.g., SSM with dynamic shapes), shape inference may leave intermediate
+// constant computation nodes with unresolved dynamic layouts during compile_graph.
+// After propagate_constants evaluates and folds these constant subgraphs, the resulting
+// data nodes have fully resolved static shapes, causing downstream consumers to transition
+// from dynamic to static.
+//
+// This test uses a non-trivial constant subgraph (eltwise of two weight tensors) whose
+// layout is set to dynamic to simulate unresolved shape inference. Creating a genuinely
+// dynamic constant subgraph requires model-specific conditions that are impractical to
+// reproduce in a unit test, so the manual override is necessary.
+//
+// Topology:
+// data("weights_a") ---+
+// eltwise("w_sum") --> eltwise("eltwise", sum)
+// data("weights_b") ---+ /
+// input_layout("input", static) ---------/
+//
+// After propagation: w_sum is folded into a static data node (was_dynamic=true),
+// triggering impl reselection for eltwise which transitions to static.
+TEST(propagate_constants, need_impl_reselection_dynamic_to_static_transition) {
+ auto& engine = get_test_engine();
+
+ auto static_layout = layout{{1, 3, 4, 4}, data_types::f32, format::bfyx};
+
+ topology topology(
+ input_layout("input", static_layout),
+ data("weights_a", engine.allocate_memory(layout{{1, 3, 4, 4}, data_types::f32, format::bfyx})),
+ data("weights_b", engine.allocate_memory(layout{{1, 3, 4, 4}, data_types::f32, format::bfyx})),
+ eltwise("w_sum", input_info("weights_a"), input_info("weights_b"), eltwise_mode::sum),
+ eltwise("eltwise", input_info("input"), input_info("w_sum"), eltwise_mode::sum)
+ );
+
+ 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);
+
+ // Simulate unresolved dynamic shape on the constant computation node.
+ // This models real scenarios where shape inference cannot fully resolve
+ // intermediate constant shapes during compile_graph.
+ auto& w_sum_node = prog->get_node("w_sum");
+ auto dyn_layout = layout{ov::PartialShape::dynamic(4), data_types::f32, format::bfyx};
+ w_sum_node.set_output_layout(dyn_layout, true);
+
+ program_wrapper::apply_opt_pass<compile_graph>(*prog);
+
+ // After compile_graph: eltwise should have a dynamic impl since one input is dynamic.
+ auto& eltwise_node = prog->get_node("eltwise");
+ auto impl_before = eltwise_node.get_selected_impl();
+ ASSERT_TRUE(impl_before == nullptr || impl_before->is_dynamic());
+
+ // propagate_constants evaluates and folds w_sum into static data.
+ // Dynamic -> static transition triggers impl reselection for eltwise.
+ program_wrapper::apply_opt_pass<propagate_constants>(*prog);
+
+ // w_sum should be replaced by a propagated data node
+ // (program::replace renames the new node to the old id).
+ auto& w_sum_replaced = prog->get_node("w_sum");
+ ASSERT_TRUE(w_sum_replaced.is_type<data>());
+
+ auto impl_after = eltwise_node.get_selected_impl();
+ ASSERT_NE(impl_after, nullptr);
+ ASSERT_FALSE(impl_after->is_dynamic());
+}
+
+// Verifies that when impl reselection is triggered, nodes that remain dynamic
+// (due to a dynamic non-constant input) are correctly skipped — they should NOT
+// receive a static implementation.
+//
+// Uses a non-trivial constant subgraph (eltwise of two weight tensors) shared by
+// two consumers: one with a static input and one with a dynamic input.
+//
+// Topology:
+// input_layout("input_static", static) ---> eltwise("eltwise_becomes_static", sum)
+// input_layout("input_dynamic", dynamic) --> eltwise("eltwise_stays_dynamic", sum)
+// data("weights_a") ---+ /
+// eltwise("w_sum") --/ (shared constant input for both)
+// data("weights_b") ---+
+//
+// After propagation: eltwise_becomes_static transitions to static (gets static impl),
+// while eltwise_stays_dynamic remains dynamic (keeps dynamic impl or nullptr).
+TEST(propagate_constants, need_impl_reselection_skips_still_dynamic_nodes) {
+ auto& engine = get_test_engine();
+
+ auto static_input_layout = layout{{1, 3, 4, 4}, data_types::f32, format::bfyx};
+ auto dynamic_input_layout = layout{ov::PartialShape::dynamic(4), data_types::f32, format::bfyx};
+
+ topology topology(
+ input_layout("input_static", static_input_layout),
+ input_layout("input_dynamic", dynamic_input_layout),
+ data("weights_a", engine.allocate_memory(layout{{1, 3, 4, 4}, data_types::f32, format::bfyx})),
+ data("weights_b", engine.allocate_memory(layout{{1, 3, 4, 4}, data_types::f32, format::bfyx})),
+ eltwise("w_sum", input_info("weights_a"), input_info("weights_b"), eltwise_mode::sum),
+ eltwise("eltwise_becomes_static", input_info("input_static"), input_info("w_sum"), eltwise_mode::sum),
+ eltwise("eltwise_stays_dynamic", input_info("input_dynamic"), input_info("w_sum"), eltwise_mode::sum)
+ );
+
+ 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);
+
+ // Simulate unresolved dynamic shape on the constant computation node.
+ auto& w_sum_node = prog->get_node("w_sum");
+ auto dyn_layout = layout{ov::PartialShape::dynamic(4), data_types::f32, format::bfyx};
+ w_sum_node.set_output_layout(dyn_layout, true);
+
+ program_wrapper::apply_opt_pass<compile_graph>(*prog);
+
+ // Both eltwises should be dynamic after compile_graph (one dynamic input each).
+ auto& static_eltwise = prog->get_node("eltwise_becomes_static");
+ auto& dynamic_eltwise = prog->get_node("eltwise_stays_dynamic");
+
+ auto static_impl_before = static_eltwise.get_selected_impl();
+ auto dynamic_impl_before = dynamic_eltwise.get_selected_impl();
+ ASSERT_TRUE(static_impl_before == nullptr || static_impl_before->is_dynamic());
+ ASSERT_TRUE(dynamic_impl_before == nullptr || dynamic_impl_before->is_dynamic());
+
+ program_wrapper::apply_opt_pass<propagate_constants>(*prog);
+
+ // eltwise_becomes_static: all inputs now static → should get static impl.
+ auto static_impl_after = static_eltwise.get_selected_impl();
+ ASSERT_NE(static_impl_after, nullptr);
+ ASSERT_FALSE(static_impl_after->is_dynamic());
+
+ // eltwise_stays_dynamic: input_dynamic is still dynamic → should keep dynamic
+ // impl or remain without impl (both are valid for dynamic nodes).
+ auto dynamic_impl_after = dynamic_eltwise.get_selected_impl();
+ ASSERT_TRUE(dynamic_impl_after == nullptr || dynamic_impl_after->is_dynamic());
+}
+
+// Verifies that when all propagated constants are already static (was_dynamic=false),
+// need_impl_reselection is NOT triggered and existing implementations are preserved.
+//
+// Topology (same structure, but w_reorder keeps its static layout):
+// input_layout("input", static) ---> eltwise("eltwise", sum)
+// data("weights") --> reorder("w_reorder") --/
+//
+// Since w_reorder is static before propagation, was_dynamic=false.
+// The pass returns early without entering the impl reselection loop.
+TEST(propagate_constants, no_reselection_when_constants_are_static) {
+ auto& engine = get_test_engine();
+
+ auto static_layout = layout{{1, 3, 4, 4}, data_types::f32, format::bfyx};
+
+ topology topology(
+ input_layout("input", static_layout),
+ data("weights", engine.allocate_memory(layout{{1, 3, 4, 4}, data_types::f32, format::bfyx})),
+ reorder("w_reorder", input_info("weights"), format::bfyx, data_types::f32),
+ eltwise("eltwise", input_info("input"), input_info("w_reorder"), eltwise_mode::sum)
+ );
+
+ 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);
+
+ // Do NOT set w_reorder to dynamic — it stays static.
+ program_wrapper::apply_opt_pass<compile_graph>(*prog);
+
+ auto& eltwise_node = prog->get_node("eltwise");
+ auto impl_before = eltwise_node.get_selected_impl();
+ ASSERT_NE(impl_before, nullptr);
+ ASSERT_FALSE(impl_before->is_dynamic());
+
+ program_wrapper::apply_opt_pass<propagate_constants>(*prog);
+
+ // w_reorder should be folded into a data node.
+ auto& w_reorder_replaced = prog->get_node("w_reorder");
+ ASSERT_TRUE(w_reorder_replaced.is_type<data>());
+
+ // Eltwise's impl should be preserved (was_dynamic=false, no reselection triggered).
+ auto impl_after = eltwise_node.get_selected_impl();
+ ASSERT_NE(impl_after, nullptr);
+ ASSERT_FALSE(impl_after->is_dynamic());
+ ASSERT_EQ(impl_before, impl_after);
+}