Skip to content

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

[GPU] Defer dynamic output argument binding until execution preparation (#39354)

Zijun Yufeffa3c6f730
diff --git a/src/plugins/intel_gpu/src/graph/network.cpp b/src/plugins/intel_gpu/src/graph/network.cpp
index 775cdf1bded..1c2303e0ce7 100644
--- a/src/plugins/intel_gpu/src/graph/network.cpp
+++ b/src/plugins/intel_gpu/src/graph/network.cpp
@@ -669,7 +669,8 @@ std::vector<event::ptr> network::set_output_memory(const primitive_id& id, memor
}
ret_ev.push_back(prim->set_output_memory(mem, (!prim->is_dynamic() || !is_remote)));
- if (!_reset_arguments &&
+ // Dynamic output changes are marked by set_output_memory; bind arguments once input memory is ready.
+ if (!_reset_arguments && !prim->is_dynamic() &&
(prim->type() != cldnn::data::type_id() && (prim->type() != cldnn::mutable_data::type_id() || !prim->dependencies().empty()))) {
prim->set_arguments();
}
diff --git a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
index f8c88138afa..4dfbf9221cc 100644
--- a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
+++ b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
@@ -311,6 +311,10 @@ event::ptr primitive_inst::set_output_memory(memory::ptr mem_new, bool check, si
} else {
_outputs[idx] = mem_new;
_max_output_layout_count[idx] = mem_new->get_layout().get_linear_size();
+ // Refresh dynamic arguments during preparation even if the input shape is unchanged.
+ if (is_dynamic()) {
+ set_flag(ExecutionFlags::MEMORY_CHANGED);
+ }
// Replacing the primary output means any borrowed remote permute view is no longer active.
if (idx == 0)
_remote_permute_output_alias.reset();
diff --git a/src/plugins/intel_gpu/tests/functional/dynamic_tests/gpu_dyn_output_rebind_test.cpp b/src/plugins/intel_gpu/tests/functional/dynamic_tests/gpu_dyn_output_rebind_test.cpp
new file mode 100644
index 00000000000..425ee5c6911
--- /dev/null
+++ b/src/plugins/intel_gpu/tests/functional/dynamic_tests/gpu_dyn_output_rebind_test.cpp
@@ -0,0 +1,119 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#ifdef OV_GPU_WITH_OCL_RT
+
+# include <gtest/gtest.h>
+
+# include <algorithm>
+# include <vector>
+
+# include "openvino/op/constant.hpp"
+# include "openvino/op/convert.hpp"
+# include "openvino/op/multiply.hpp"
+# include "openvino/op/parameter.hpp"
+# include "openvino/op/reshape.hpp"
+# include "openvino/op/softmax.hpp"
+# include "openvino/runtime/core.hpp"
+# include "openvino/runtime/intel_gpu/ocl/ocl.hpp"
+# include "openvino/runtime/properties.hpp"
+
+namespace {
+class DynamicOutputRebindTest : public ::testing::TestWithParam<bool> {};
+
+TEST_P(DynamicOutputRebindTest, SharedProducerWithNewOutputBindings) {
+ using namespace ov;
+ auto input = std::make_shared<op::v0::Parameter>(element::f16, PartialShape{1, 1, -1, 8});
+ auto product = std::make_shared<op::v1::Multiply>(input, op::v0::Constant::create(element::f16, Shape{}, {2}));
+ auto converted = std::make_shared<op::v0::Convert>(product, element::f32);
+ auto model = std::make_shared<Model>(OutputVector{converted, product}, ParameterVector{input});
+ Core core;
+ auto compiled = core.compile_model(model, "GPU", hint::inference_precision(element::dynamic));
+ if (!compiled.get_context().is<intel_gpu::ocl::ClContext>())
+ GTEST_SKIP() << "This test uses OpenCL USM output tensors";
+ auto context = compiled.get_context().as<intel_gpu::ocl::ClContext>();
+ auto request = compiled.create_infer_request();
+ const Shape capacity{1, 1, 3, 8};
+ auto result_storage = context.create_usm_device_tensor(element::f32, capacity);
+ auto product_storage = context.create_usm_device_tensor(element::f16, capacity);
+
+ for (size_t tokens : {3u, 1u, 2u}) {
+ SCOPED_TRACE(tokens);
+ const Shape shape{1, 1, tokens, 8};
+ Tensor source(element::f16, shape);
+ std::fill_n(source.data<float16>(), source.get_size(), float16(0.25f));
+ // Exercise both new allocations and changed capacity at the same pointer.
+ auto result = GetParam() ? context.create_usm_device_tensor(element::f32, capacity) : context.create_tensor(element::f32, shape, result_storage.get());
+ auto raw = GetParam() ? context.create_usm_device_tensor(element::f16, capacity) : context.create_tensor(element::f16, shape, product_storage.get());
+ result.set_shape(shape);
+ raw.set_shape(shape);
+ request.set_input_tensor(source);
+ request.set_output_tensor(0, result);
+ request.set_output_tensor(1, raw);
+ ASSERT_NO_THROW(request.infer());
+ Tensor result_host(element::f32, shape);
+ Tensor product_host(element::f16, shape);
+ result.copy_to(result_host);
+ raw.copy_to(product_host);
+ for (size_t i = 0; i < result_host.get_size(); ++i) {
+ EXPECT_EQ(result_host.data<float>()[i], 0.5f);
+ EXPECT_EQ(float(product_host.data<float16>()[i]), 0.5f);
+ }
+ }
+}
+
+INSTANTIATE_TEST_SUITE_P(smoke, DynamicOutputRebindTest, ::testing::Values(false, true));
+
+// Softmax is a non-output compute node that writes straight into the output buffer and does
+// not read a Parameter, so its kernel arguments are not refreshed by input changes. Swapping the
+// bound output buffer at an unchanged shape must still rebind Softmax's output argument.
+TEST(DynamicOutputRebindTest, PingPongBuffersForNonOutputProducer) {
+ using namespace ov;
+ auto input = std::make_shared<op::v0::Parameter>(element::f16, PartialShape{1, -1, 8});
+ auto product = std::make_shared<op::v1::Multiply>(input, op::v0::Constant::create(element::f16, Shape{}, {2}));
+ auto softmax = std::make_shared<op::v8::Softmax>(product, 2);
+ auto reshape = std::make_shared<op::v1::Reshape>(softmax, op::v0::Constant::create(element::i64, Shape{3}, {0, -1, 8}), true);
+ auto model = std::make_shared<Model>(OutputVector{reshape}, ParameterVector{input});
+ Core core;
+ auto compiled = core.compile_model(model, "GPU", hint::inference_precision(element::dynamic));
+ if (!compiled.get_context().is<intel_gpu::ocl::ClContext>())
+ GTEST_SKIP() << "This test uses OpenCL USM output tensors";
+ auto context = compiled.get_context().as<intel_gpu::ocl::ClContext>();
+ auto request = compiled.create_infer_request();
+ const Shape shape{1, 3, 8};
+ // Keep the input binding fixed so only the output binding changes between iterations.
+ auto source = context.create_usm_device_tensor(element::f16, shape);
+ Tensor source_host(element::f16, shape);
+ for (size_t i = 0; i < source_host.get_size(); ++i)
+ source_host.data<float16>()[i] = float16(static_cast<float>(i % 8) / 8.0f);
+ source.copy_from(source_host);
+ request.set_input_tensor(source);
+
+ Tensor expected(element::f16, shape);
+ {
+ // Plugin-allocated warm-up output: Softmax writes the output block directly.
+ request.infer();
+ request.get_output_tensor(0).copy_to(expected);
+ }
+
+ std::vector<RemoteTensor> buffers{context.create_usm_device_tensor(element::f16, shape), context.create_usm_device_tensor(element::f16, shape)};
+ Tensor sentinel(element::f16, shape);
+ std::fill_n(sentinel.data<float16>(), sentinel.get_size(), float16(-1.0f));
+ for (size_t iter = 0; iter < 4; ++iter) {
+ SCOPED_TRACE(iter);
+ auto& output = buffers[iter % buffers.size()];
+ output.copy_from(sentinel);
+ request.set_output_tensor(0, output);
+ ASSERT_NO_THROW(request.infer());
+
+ Tensor output_host(element::f16, shape);
+ output.copy_to(output_host);
+ for (size_t i = 0; i < output_host.get_size(); ++i) {
+ ASSERT_EQ(float(output_host.data<float16>()[i]), float(expected.data<float16>()[i])) << "element " << i;
+ }
+ }
+}
+} // namespace
+
+#endif // OV_GPU_WITH_OCL_RT
diff --git a/src/plugins/intel_gpu/tests/functional/sources.cmake b/src/plugins/intel_gpu/tests/functional/sources.cmake
index 6d369322265..d73aea53719 100644
--- a/src/plugins/intel_gpu/tests/functional/sources.cmake
+++ b/src/plugins/intel_gpu/tests/functional/sources.cmake
@@ -19,6 +19,7 @@ set(GPU_FUNCTIONAL_TEST_SOURCES
${CMAKE_CURRENT_LIST_DIR}/dynamic_tests/gpu_dyn_output_aliasing_test.cpp
${CMAKE_CURRENT_LIST_DIR}/dynamic_tests/gpu_dyn_output_deep_chain_test.cpp
${CMAKE_CURRENT_LIST_DIR}/dynamic_tests/gpu_dyn_output_multiop_chain_test.cpp
+ ${CMAKE_CURRENT_LIST_DIR}/dynamic_tests/gpu_dyn_output_rebind_test.cpp
${CMAKE_CURRENT_LIST_DIR}/remote_tensor_tests/dx11_nthandle.cpp
${CMAKE_CURRENT_LIST_DIR}/remote_tensor_tests/dx11_remote_ctx_test.cpp
${CMAKE_CURRENT_LIST_DIR}/remote_tensor_tests/dx12_nthandle.cpp