Skip to content

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

[GPU] Fix concurrent reset_state() corrupting shape inference across sibling InferRequests (#36560)

Siddhant Shekhar43659f868fa2
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 0bc545de44e..8855e87ad3c 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
@@ -19,6 +19,26 @@
namespace ov::intel_gpu {
class CompiledModel;
+class Graph;
+
+/// @brief Thread-safe wrapper around VariableStateBase that acquires the graph mutex
+/// before delegating reset() calls. This ensures that reset_state() from one InferRequest
+/// does not race with infer() on a sibling InferRequest sharing the same CompiledModel.
+/// See: https://github.com/openvinotoolkit/openvino/issues/36458
+class ThreadSafeVariableStateWrapper : public ov::IVariableState {
+public:
+ ThreadSafeVariableStateWrapper(std::shared_ptr<ov::IVariableState> state,
+ std::shared_ptr<Graph> graph);
+
+ void reset() override;
+ void set_state(const ov::SoPtr<ov::ITensor>& state) override;
+ ov::SoPtr<ov::ITensor> get_state() const override;
+
+private:
+ std::shared_ptr<ov::IVariableState> m_state;
+ std::shared_ptr<Graph> m_graph;
+};
+
enum class TensorOwner : uint8_t {
USER = 0,
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 7c05cfe685f..a56d4b92537 100644
--- a/src/plugins/intel_gpu/src/plugin/sync_infer_request.cpp
+++ b/src/plugins/intel_gpu/src/plugin/sync_infer_request.cpp
@@ -84,6 +84,34 @@ cldnn::data_types data_type_for_remote_tensor(ov::element::Type t) {
namespace ov::intel_gpu {
+// ----------------------------------------------------------------------------------------------- //
+// -------------- ThreadSafeVariableStateWrapper (see issue #36458) ----------------------------- //
+// ----------------------------------------------------------------------------------------------- //
+
+ThreadSafeVariableStateWrapper::ThreadSafeVariableStateWrapper(
+ std::shared_ptr<ov::IVariableState> state,
+ std::shared_ptr<Graph> graph)
+ : ov::IVariableState(state->get_name())
+ , m_state(std::move(state))
+ , m_graph(std::move(graph)) {}
+
+void ThreadSafeVariableStateWrapper::reset() {
+ // Acquire the same mutex that infer() holds during shape inference
+ // to prevent concurrent modification of variable state layouts.
+ std::lock_guard<std::mutex> lk(m_graph->get_mutex());
+ m_state->reset();
+}
+
+void ThreadSafeVariableStateWrapper::set_state(const ov::SoPtr<ov::ITensor>& state) {
+ std::lock_guard<std::mutex> lk(m_graph->get_mutex());
+ m_state->set_state(state);
+}
+
+ov::SoPtr<ov::ITensor> ThreadSafeVariableStateWrapper::get_state() const {
+ std::lock_guard<std::mutex> lk(m_graph->get_mutex());
+ return m_state->get_state();
+}
+
// ----------------------------------------------------------------------------------------------- //
// ---------------------------- OpenVINO API impl ------------------------------------------------ //
// ----------------------------------------------------------------------------------------------- //
@@ -131,7 +159,13 @@ std::vector<ov::ProfilingInfo> SyncInferRequest::get_profiling_info() const {
std::vector<ov::SoPtr<ov::IVariableState>> SyncInferRequest::query_state() const {
std::vector<ov::SoPtr<ov::IVariableState>> ret{};
for (const auto& pair : m_variables) {
- ret.emplace_back(pair.second, nullptr);
+ // Wrap each variable state with a thread-safe proxy that acquires the
+ // graph mutex before reset()/set_state(), preventing concurrent
+ // modification during shape inference on sibling InferRequests.
+ // See: https://github.com/openvinotoolkit/openvino/issues/36458
+ ret.emplace_back(
+ std::make_shared<ThreadSafeVariableStateWrapper>(pair.second, m_graph),
+ nullptr);
}
return ret;
}