Skip to content

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

[GPU] Ensure user tensor is contiguous before processing in SyncInferRequest (#37961)

Andrew Yooeun Chunc626d8f7f181
diff --git a/src/plugins/intel_gpu/include/intel_gpu/plugin/sync_infer_request.hpp b/src/plugins/intel_gpu/include/intel_gpu/plugin/sync_infer_request.hpp
index 4c5cd72bf54..90a13d53c61 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/plugin/sync_infer_request.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/plugin/sync_infer_request.hpp
@@ -157,7 +157,8 @@ private:
std::vector<cldnn::event::ptr> prepare_input(const std::string& internal_name,
size_t input_idx,
const ov::Output<const ov::Node>& port,
- const TensorWrapper& user_tensor_wrapper);
+ const TensorWrapper& user_tensor_wrapper,
+ bool is_temporary_repacked_input = false);
std::vector<cldnn::event::ptr> prepare_output(size_t output_idx, const ov::Output<const ov::Node>& port, const TensorWrapper& user_tensor_wrapper);
std::vector<cldnn::event::ptr> prepare_batched_input(size_t input_idx,
const ov::Output<const ov::Node>& port,
diff --git a/src/plugins/intel_gpu/src/plugin/sync_infer_request.cpp b/src/plugins/intel_gpu/src/plugin/sync_infer_request.cpp
index 3802f91cce3..82a378460e6 100644
--- a/src/plugins/intel_gpu/src/plugin/sync_infer_request.cpp
+++ b/src/plugins/intel_gpu/src/plugin/sync_infer_request.cpp
@@ -48,6 +48,22 @@ bool same_host_mem(cldnn::memory::cptr memory, const uint8_t* host_ptr) {
return device_ptr == host_ptr;
}
+bool is_contiguous_or_remote(const std::shared_ptr<ov::ITensor>& tensor) {
+ return tensor->is_continuous() || std::dynamic_pointer_cast<ov::IRemoteTensor>(tensor) != nullptr;
+}
+
+std::shared_ptr<ov::ITensor> ensure_contiguous(const std::shared_ptr<ov::ITensor>& tensor) {
+ // remote tensors do not need repacking. RemoteTensorImpl always has default strides for its shape,
+ // and remote ROI tensors are copied via RemoteTensorImpl::copy_to with proper strides
+ if (is_contiguous_or_remote(tensor)) {
+ return tensor;
+ }
+
+ auto packed = ov::make_tensor(tensor->get_element_type(), tensor->get_shape());
+ tensor->copy_to(packed);
+ return packed;
+}
+
bool byte_ranges_overlap(const void* lhs_ptr, size_t lhs_size, const void* rhs_ptr, size_t rhs_size) {
if (lhs_ptr == nullptr || rhs_ptr == nullptr || lhs_size == 0 || rhs_size == 0)
return false;
@@ -401,7 +417,13 @@ void SyncInferRequest::enqueue() {
auto inputs = m_user_inputs.read();
user_tensor = inputs->at(port_idx);
}
- auto events = prepare_input(internal_name, port_idx, port, user_tensor);
+ // ensure user tensor is contiguous
+ const auto contiguous_tensor = ensure_contiguous(user_tensor.ptr);
+ const bool is_repacked = contiguous_tensor != user_tensor.ptr;
+ if (is_repacked) {
+ user_tensor = {contiguous_tensor, TensorOwner::PLUGIN};
+ }
+ auto events = prepare_input(internal_name, port_idx, port, user_tensor, is_repacked);
std::move(events.begin(), events.end(), std::back_inserter(dependencies));
}
@@ -1034,8 +1056,13 @@ std::vector<cldnn::event::ptr> SyncInferRequest::prepare_batched_input(size_t in
std::vector<cldnn::event::ptr> SyncInferRequest::prepare_input(const std::string& internal_name,
size_t input_idx,
const ov::Output<const ov::Node>& port,
- const TensorWrapper& user_tensor_wrapper) {
+ const TensorWrapper& user_tensor_wrapper,
+ bool is_temporary_repacked_input) {
OV_ITT_SCOPED_TASK(itt::domains::intel_gpu_plugin, openvino::itt::handle("SyncInferRequest::prepare_input: " + internal_name));
+ OPENVINO_ASSERT(is_contiguous_or_remote(user_tensor_wrapper.ptr),
+ "[GPU] Non-contiguous host input tensor must be repacked before prepare_input: ",
+ internal_name);
+
auto pshape = port.get_partial_shape();
auto is_dynamic = pshape.is_dynamic();
auto user_tensor = user_tensor_wrapper.ptr;
@@ -1174,7 +1201,8 @@ std::vector<cldnn::event::ptr> SyncInferRequest::prepare_input(const std::string
// The current input_layout (wait_for_events) does not provide proper synchronization for subsequent CPU implementations
// For IOQ, it creates an already set user event, leading to accessing memory that hasn't completed copying
// For OOOQ, it enqueues a barrier that is ignored by the memory_lock functions, also causing access to not ready memory
- ret_event = memory->copy_from(stream, src_ptr, need_lockable_mem);
+ // temporary repacked input from enqueue() is freed afterwards, so the upload needs to finish first
+ ret_event = memory->copy_from(stream, src_ptr, need_lockable_mem || is_temporary_repacked_input);
}
} else if (is_generic_remote) {
user_tensor->copy_to(device_tensor);
diff --git a/src/plugins/intel_gpu/tests/functional/behavior/infer_request.cpp b/src/plugins/intel_gpu/tests/functional/behavior/infer_request.cpp
index e686f09d7b1..61e47d78777 100644
--- a/src/plugins/intel_gpu/tests/functional/behavior/infer_request.cpp
+++ b/src/plugins/intel_gpu/tests/functional/behavior/infer_request.cpp
@@ -1314,6 +1314,85 @@ TEST(TensorTest, smoke_canReallocateDeviceInputForHostTensor) {
OV_ASSERT_NO_THROW(inf_req.infer());
}
+// An ROI tensor shares the parent's strides and may not be contiguous.
+// Inference must match a contiguous tensor with the same values, whether the ROI is the first input
+// (allocating device memory) or follows a contiguous input (reusing existing device memory).
+using InferRequestNonContiguousInputParams = std::tuple<ov::PartialShape, // Model input shape
+ std::pair<ov::Coordinate, ov::Coordinate>, // ROI begin and end in the parent tensor
+ std::string>; // Device name
+
+class InferRequestNonContiguousInput : public ::testing::TestWithParam<InferRequestNonContiguousInputParams> {
+public:
+ static std::string getTestCaseName(const testing::TestParamInfo<InferRequestNonContiguousInputParams>& obj) {
+ const auto& [input_shape, roi, target_device] = obj.param;
+
+ std::ostringstream result;
+ result << "IS=" << ov::test::utils::partialShape2str({input_shape}) << "_";
+ result << "begin=" << ov::test::utils::vec2str(roi.first) << "_";
+ result << "end=" << ov::test::utils::vec2str(roi.second) << "_";
+ result << "trgDev=" << target_device;
+ return result.str();
+ }
+};
+
+TEST_P(InferRequestNonContiguousInput, Inference) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+ const auto& [input_shape, roi, target_device] = GetParam();
+
+ // Give the parent elements distinct values so that a sheared read cannot pass by chance
+ ov::Tensor parent(ov::element::f32, {1, 3, 16, 16});
+ for (size_t i = 0; i < parent.get_size(); ++i) {
+ parent.data<float>()[i] = static_cast<float>(i);
+ }
+ auto view = ov::Tensor(parent, roi.first, roi.second);
+ ASSERT_FALSE(view.is_continuous());
+
+ // The reference input: the same values in a contiguous tensor
+ ov::Tensor packed(view.get_element_type(), view.get_shape());
+ view.copy_to(packed);
+
+ auto param = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, input_shape);
+ auto add = std::make_shared<ov::op::v1::Add>(param, ov::op::v0::Constant::create(ov::element::f32, {1}, {1.f}));
+ auto model = std::make_shared<ov::Model>(ov::OutputVector{add}, ov::ParameterVector{param});
+ auto core = ov::Core();
+ auto compiled_model = core.compile_model(model, target_device);
+
+ // Returns a copy of the output, which the request may overwrite on its next run
+ auto infer = [](ov::InferRequest& request, const ov::Tensor& input) {
+ request.set_input_tensor(input);
+ request.infer();
+ auto output = request.get_output_tensor();
+ ov::Tensor copy(output.get_element_type(), output.get_shape());
+ output.copy_to(copy);
+ return copy;
+ };
+
+ // Reused device memory: the request has already run the contiguous tensor
+ auto request = compiled_model.create_infer_request();
+ auto expected = infer(request, packed);
+ ov::test::utils::compare(expected, infer(request, view), 0.0, 0.0);
+
+ // Fresh request: the view is its very first input
+ auto fresh_request = compiled_model.create_infer_request();
+ ov::test::utils::compare(expected, infer(fresh_request, view), 0.0, 0.0);
+}
+
+const std::vector<ov::PartialShape> input_shapes = {
+ {1, 3, 8, 8}, // static
+ {1, 3, -1, -1}, // dynamic
+};
+
+// 8x8 views into the {1, 3, 16, 16} parent. Neither is contiguous; the second one starts at the parent's data()
+const std::vector<std::pair<ov::Coordinate, ov::Coordinate>> rois = {
+ {{0, 0, 4, 6}, {1, 3, 12, 14}},
+ {{0, 0, 0, 0}, {1, 3, 8, 8}},
+};
+
+INSTANTIATE_TEST_SUITE_P(smoke_GPU_BehaviorTests,
+ InferRequestNonContiguousInput,
+ ::testing::Combine(::testing::ValuesIn(input_shapes), ::testing::ValuesIn(rois), ::testing::Values(ov::test::utils::DEVICE_GPU)),
+ InferRequestNonContiguousInput::getTestCaseName);
+
TEST(VariablesTest, smoke_canSetStateTensor) {
auto ov = ov::Core();
const ov::Shape virable_shape = {1, 3, 2, 4};
diff --git a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/behavior/ov_infer_request/inference.cpp b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/behavior/ov_infer_request/inference.cpp
new file mode 100644
index 00000000000..1fc90215bfa
--- /dev/null
+++ b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/behavior/ov_infer_request/inference.cpp
@@ -0,0 +1,16 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "behavior/ov_infer_request/inference.hpp"
+
+namespace {
+
+using namespace ov::test::behavior;
+
+INSTANTIATE_TEST_SUITE_P(smoke_BehaviorTests,
+ OVInferRequestInferenceTests,
+ ::testing::Combine(::testing::Values(tensor_roi::roi_nchw(), tensor_roi::roi_1d()), ::testing::Values(ov::test::utils::DEVICE_GPU)),
+ OVInferRequestInferenceTests::getTestCaseName);
+
+} // namespace
diff --git a/src/plugins/intel_gpu/tests/functional/sources.cmake b/src/plugins/intel_gpu/tests/functional/sources.cmake
index 05b9601654c..36554fe76de 100644
--- a/src/plugins/intel_gpu/tests/functional/sources.cmake
+++ b/src/plugins/intel_gpu/tests/functional/sources.cmake
@@ -35,6 +35,7 @@ set(GPU_FUNCTIONAL_TEST_SOURCES
${CMAKE_CURRENT_LIST_DIR}/shared_tests_instances/behavior/ov_infer_request/cancellation.cpp
${CMAKE_CURRENT_LIST_DIR}/shared_tests_instances/behavior/ov_infer_request/infer_consistency.cpp
${CMAKE_CURRENT_LIST_DIR}/shared_tests_instances/behavior/ov_infer_request/infer_request_dynamic.cpp
+ ${CMAKE_CURRENT_LIST_DIR}/shared_tests_instances/behavior/ov_infer_request/inference.cpp
${CMAKE_CURRENT_LIST_DIR}/shared_tests_instances/behavior/ov_infer_request/io_tensor.cpp
${CMAKE_CURRENT_LIST_DIR}/shared_tests_instances/behavior/ov_infer_request/iteration_chaining.cpp
${CMAKE_CURRENT_LIST_DIR}/shared_tests_instances/behavior/ov_infer_request/multithreading.cpp