Skip to content

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

[PyOV][Free threaded] Always attach `PyThreadState` in multithreading contexts (#32853)

Przemyslaw Wysockifab9a560f9b9
diff --git a/src/bindings/python/src/pyopenvino/core/async_infer_queue.cpp b/src/bindings/python/src/pyopenvino/core/async_infer_queue.cpp
index ef5a5027f3d..080ad986b52 100644
--- a/src/bindings/python/src/pyopenvino/core/async_infer_queue.cpp
+++ b/src/bindings/python/src/pyopenvino/core/async_infer_queue.cpp
@@ -46,7 +46,7 @@ public:
bool _is_ready() {
// Check if any request has finished already
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
// acquire the mutex to access m_errors and m_idle_handles
std::lock_guard<std::mutex> lock(m_mutex);
if (m_errors.size() > 0)
@@ -57,7 +57,7 @@ public:
size_t get_idle_request_id() {
// Wait for any request to complete and return its id
// release GIL to avoid deadlock on python callback
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
// acquire the mutex to access m_errors and m_idle_handles
std::unique_lock<std::mutex> lock(m_mutex);
m_cv.wait(lock, [this] {
@@ -74,7 +74,7 @@ public:
void wait_all() {
// Wait for all request to complete
// release GIL to avoid deadlock on python callback
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
for (auto&& request : m_requests) {
request.m_request->wait();
}
@@ -118,8 +118,8 @@ public:
m_requests[handle].m_request->set_callback([this, callback_sp, handle](std::exception_ptr exception_ptr) {
*m_requests[handle].m_end_time = Time::now();
if (exception_ptr == nullptr) {
- // Acquire GIL, execute Python function
- ConditionalGILScopedAcquire acquire;
+ // For free-threaded Python, gil_scoped_acquire still ensures thread is attached
+ py::gil_scoped_acquire acquire;
try {
(*callback_sp)(m_requests[handle], m_user_ids[handle]);
} catch (const py::error_already_set& py_error) {
@@ -200,7 +200,7 @@ void regclass_AsyncInferQueue(py::module m) {
self.m_requests[handle].m_request->set_input_tensor(inputs);
// Now GIL can be released - we are NOT working with Python objects in this block
{
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
*self.m_requests[handle].m_start_time = Time::now();
// Start InferRequest in asynchronus mode
self.m_requests[handle].m_request->start_async();
@@ -246,7 +246,7 @@ void regclass_AsyncInferQueue(py::module m) {
Common::set_request_tensors(*self.m_requests[handle].m_request, inputs);
// Now GIL can be released - we are NOT working with Python objects in this block
{
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
*self.m_requests[handle].m_start_time = Time::now();
// Start InferRequest in asynchronus mode
self.m_requests[handle].m_request->start_async();
diff --git a/src/bindings/python/src/pyopenvino/core/common.cpp b/src/bindings/python/src/pyopenvino/core/common.cpp
index b20e74ef3ec..0e99343bd34 100644
--- a/src/bindings/python/src/pyopenvino/core/common.cpp
+++ b/src/bindings/python/src/pyopenvino/core/common.cpp
@@ -419,7 +419,7 @@ std::shared_ptr<ov::SharedBuffer<py::array>> get_shared_memory(py::array& array)
array.ndim() == 0 ? array.itemsize() : array.nbytes(),
array);
std::shared_ptr<ov::SharedBuffer<py::array>> memory(buffer, [](ov::SharedBuffer<py::array>* buffer) {
- ConditionalGILScopedAcquire acquire;
+ py::gil_scoped_acquire acquire;
delete buffer;
});
return memory;
diff --git a/src/bindings/python/src/pyopenvino/core/compiled_model.cpp b/src/bindings/python/src/pyopenvino/core/compiled_model.cpp
index d7bc71a4ed5..ac9fea33a25 100644
--- a/src/bindings/python/src/pyopenvino/core/compiled_model.cpp
+++ b/src/bindings/python/src/pyopenvino/core/compiled_model.cpp
@@ -30,7 +30,7 @@ void regclass_CompiledModel(py::module m) {
// Create temporary ov::InferRequest and move it to actual wrapper class.
ov::InferRequest request;
{
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
request = self.create_infer_request();
}
return std::make_shared<InferRequestWrapper>(std::move(request), self.inputs(), self.outputs());
@@ -171,7 +171,7 @@ void regclass_CompiledModel(py::module m) {
cls.def("get_runtime_model",
&ov::CompiledModel::get_runtime_model,
- CallGuardConditionalGILRelease(),
+ py::call_guard<py::gil_scoped_release>(),
R"(
Gets runtime model information from a device.
@@ -186,7 +186,7 @@ void regclass_CompiledModel(py::module m) {
cls.def("release_memory",
&ov::CompiledModel::release_memory,
- CallGuardConditionalGILRelease(),
+ py::call_guard<py::gil_scoped_release>(),
R"(
Release intermediate memory.
diff --git a/src/bindings/python/src/pyopenvino/core/core.cpp b/src/bindings/python/src/pyopenvino/core/core.cpp
index 86d0166cf14..bf68976c563 100644
--- a/src/bindings/python/src/pyopenvino/core/core.cpp
+++ b/src/bindings/python/src/pyopenvino/core/core.cpp
@@ -157,7 +157,7 @@ void regclass_Core(py::module m) {
const std::string& device_name,
const std::map<std::string, py::object>& properties) {
auto _properties = Common::utils::properties_to_any_map(properties);
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
return self.compile_model(model, device_name, _properties);
},
py::arg("model"),
@@ -186,7 +186,7 @@ void regclass_Core(py::module m) {
const std::shared_ptr<const ov::Model>& model,
const std::map<std::string, py::object>& properties) {
auto _properties = Common::utils::properties_to_any_map(properties);
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
return self.compile_model(model, _properties);
},
py::arg("model"),
@@ -214,7 +214,7 @@ void regclass_Core(py::module m) {
const std::map<std::string, py::object>& properties) {
auto _properties = Common::utils::properties_to_any_map(properties);
std::string path = Common::utils::convert_path_to_string(model_path);
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
return self.compile_model(path, device_name, _properties);
},
py::arg("model_path"),
@@ -259,7 +259,7 @@ void regclass_Core(py::module m) {
tensor = ov::Tensor(ov::element::Type_t::u8, {bin_size});
}
auto _properties = Common::utils::properties_to_any_map(properties);
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
return self.compile_model(model.cast<std::string>(), tensor, device_name, _properties);
},
py::arg("model_buffer"),
@@ -290,7 +290,7 @@ void regclass_Core(py::module m) {
[](ov::Core& self, const py::object& model_path, const std::map<std::string, py::object>& properties) {
auto _properties = Common::utils::properties_to_any_map(properties);
std::string path = Common::utils::convert_path_to_string(model_path);
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
return self.compile_model(path, _properties);
},
py::arg("model_path"),
@@ -317,7 +317,7 @@ void regclass_Core(py::module m) {
const RemoteContextWrapper& context,
const std::map<std::string, py::object>& properties) {
auto _properties = Common::utils::properties_to_any_map(properties);
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
return self.compile_model(model, context.context, _properties);
},
py::arg("model"),
@@ -398,7 +398,7 @@ void regclass_Core(py::module m) {
const uint8_t* bin = reinterpret_cast<const uint8_t*>(info.ptr);
std::memcpy(tensor.data(), bin, bin_size);
}
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
return self.read_model(ir, tensor);
},
py::arg("model"),
@@ -423,7 +423,7 @@ void regclass_Core(py::module m) {
const std::string& weight_path,
const std::map<std::string, py::object>& config) {
const auto any_map = Common::utils::properties_to_any_map(config);
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
return self.read_model(model_path, weight_path, any_map);
},
py::arg("model"),
@@ -453,7 +453,7 @@ void regclass_Core(py::module m) {
cls.def(
"read_model",
(std::shared_ptr<ov::Model>(ov::Core::*)(const std::string&, const ov::Tensor&) const) & ov::Core::read_model,
- CallGuardConditionalGILRelease(),
+ py::call_guard<py::gil_scoped_release>(),
py::arg("model"),
py::arg("weights"),
R"(
@@ -492,7 +492,7 @@ void regclass_Core(py::module m) {
const uint8_t* bin = reinterpret_cast<const uint8_t*>(info.ptr);
std::memcpy(tensor.data(), bin, bin_size);
}
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
return self.read_model(std::string(static_cast<char*>(buffer_info.ptr), buffer_info.size), tensor);
} else if (py::isinstance(model_path, py::module_::import("pathlib").attr("Path")) ||
py::isinstance<py::str>(model_path)) {
@@ -502,7 +502,7 @@ void regclass_Core(py::module m) {
weights_path_cpp = py::str(weights_path);
}
const auto any_map = Common::utils::properties_to_any_map(config);
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
return self.read_model(model_path_cpp, weights_path_cpp, any_map);
}
@@ -540,7 +540,7 @@ void regclass_Core(py::module m) {
const std::string& device_name,
const std::map<std::string, py::object>& properties) {
const auto _properties = Common::utils::properties_to_any_map(properties);
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
return self.import_model(exported_blob, device_name, _properties);
},
py::arg("tensor"),
@@ -587,7 +587,7 @@ void regclass_Core(py::module m) {
ov::SharedStreamBuffer mb{info.ptr, static_cast<size_t>(info.size)};
std::istream stream{&mb};
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
return self.import_model(stream, device_name, _properties);
},
py::arg("model_stream"),
@@ -701,7 +701,7 @@ void regclass_Core(py::module m) {
const std::string& device_name,
const std::map<std::string, py::object>& properties) -> std::map<std::string, std::string> {
auto _properties = Common::utils::properties_to_any_map(properties);
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
return self.query_model(model, device_name, _properties);
},
py::arg("model"),
@@ -768,7 +768,7 @@ void regclass_Core(py::module m) {
cls.def("get_available_devices",
&ov::Core::get_available_devices,
- CallGuardConditionalGILRelease(),
+ py::call_guard<py::gil_scoped_release>(),
R"(
Returns devices available for inference Core objects goes over all registered plugins.
@@ -783,7 +783,7 @@ void regclass_Core(py::module m) {
cls.def_property_readonly("available_devices",
&ov::Core::get_available_devices,
- CallGuardConditionalGILRelease(),
+ py::call_guard<py::gil_scoped_release>(),
R"(
Returns devices available for inference Core objects goes over all registered plugins.
diff --git a/src/bindings/python/src/pyopenvino/core/infer_request.cpp b/src/bindings/python/src/pyopenvino/core/infer_request.cpp
index d79381e4602..a21c4d557c9 100644
--- a/src/bindings/python/src/pyopenvino/core/infer_request.cpp
+++ b/src/bindings/python/src/pyopenvino/core/infer_request.cpp
@@ -19,7 +19,7 @@ PYBIND11_MAKE_OPAQUE(ov::TensorVector);
inline py::object run_sync_infer(InferRequestWrapper& self, bool share_outputs, bool decode_strings) {
{
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
*self.m_start_time = Time::now();
self.m_request->infer();
*self.m_end_time = Time::now();
@@ -320,7 +320,7 @@ void regclass_InferRequest(py::module m) {
PyErr_WarnEx(PyExc_RuntimeWarning, "There is no callback function to pass `userdata` into!", 1);
}
}
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
*self.m_start_time = Time::now();
self.m_request->start_async();
},
@@ -359,7 +359,7 @@ void regclass_InferRequest(py::module m) {
PyErr_WarnEx(PyExc_RuntimeWarning, "There is no callback function!", 1);
}
}
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
*self.m_start_time = Time::now();
self.m_request->start_async();
},
@@ -392,7 +392,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"wait",
[](InferRequestWrapper& self) {
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
self.m_request->wait();
},
R"(
@@ -405,7 +405,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"wait_for",
[](InferRequestWrapper& self, const int timeout) {
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
return self.m_request->wait_for(std::chrono::milliseconds(timeout));
},
py::arg("timeout"),
@@ -440,8 +440,8 @@ void regclass_InferRequest(py::module m) {
} catch (const std::exception& e) {
OPENVINO_THROW("Caught exception: ", e.what());
}
- // Acquire GIL, execute Python function
- ConditionalGILScopedAcquire acquire;
+ // For free-threaded Python, gil_scoped_acquire still ensures thread is attached
+ py::gil_scoped_acquire acquire;
(*callback_sp)(self.m_userdata);
});
},
@@ -698,7 +698,7 @@ void regclass_InferRequest(py::module m) {
[](InferRequestWrapper& self) {
return self.m_request->get_profiling_info();
},
- CallGuardConditionalGILRelease(),
+ py::call_guard<py::gil_scoped_release>(),
R"(
Queries performance is measured per layer to get feedback on what
is the most time-consuming operation, not all plugins provide
@@ -715,7 +715,7 @@ void regclass_InferRequest(py::module m) {
[](InferRequestWrapper& self) {
return self.m_request->query_state();
},
- CallGuardConditionalGILRelease(),
+ py::call_guard<py::gil_scoped_release>(),
R"(
Gets state control interface for given infer request.
@@ -819,7 +819,7 @@ void regclass_InferRequest(py::module m) {
[](InferRequestWrapper& self) {
return self.m_request->get_profiling_info();
},
- CallGuardConditionalGILRelease(),
+ py::call_guard<py::gil_scoped_release>(),
R"(
Performance is measured per layer to get feedback on the most time-consuming operation.
Not all plugins provide meaningful data!
diff --git a/src/bindings/python/src/pyopenvino/core/remote_context.cpp b/src/bindings/python/src/pyopenvino/core/remote_context.cpp
index 7638fd6d8ef..8e2ff5482c2 100644
--- a/src/bindings/python/src/pyopenvino/core/remote_context.cpp
+++ b/src/bindings/python/src/pyopenvino/core/remote_context.cpp
@@ -51,7 +51,7 @@ void regclass_RemoteContext(py::module m) {
const ov::Shape& shape,
const std::map<std::string, py::object>& properties) {
auto _properties = Common::utils::properties_to_any_map(properties);
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
return RemoteTensorWrapper(self.context.create_tensor(type, shape, _properties));
},
py::arg("type"),
@@ -79,7 +79,7 @@ void regclass_RemoteContext(py::module m) {
[](RemoteContextWrapper& self, const ov::element::Type& type, const ov::Shape& shape) {
return self.context.create_host_tensor(type, shape);
},
- CallGuardConditionalGILRelease(),
+ py::call_guard<py::gil_scoped_release>(),
py::arg("type"),
py::arg("shape"),
R"(
@@ -132,7 +132,7 @@ void regclass_VAContext(py::module m) {
[](VAContextWrapper& self, const size_t height, const size_t width, const uint32_t nv12_surface) {
ov::RemoteTensor y_tensor, uv_tensor;
{
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
ov::AnyMap tensor_params = {
{ov::intel_gpu::shared_mem_type.name(), ov::intel_gpu::SharedMemType::VA_SURFACE},
{ov::intel_gpu::dev_object_handle.name(), nv12_surface},
@@ -174,7 +174,7 @@ void regclass_VAContext(py::module m) {
{ov::intel_gpu::va_plane.name(), plane}};
return VASurfaceTensorWrapper(self.context.create_tensor(type, shape, params));
},
- CallGuardConditionalGILRelease(),
+ py::call_guard<py::gil_scoped_release>(),
py::arg("type"),
py::arg("shape"),
py::arg("surface"),
diff --git a/src/bindings/python/src/pyopenvino/frontend/extension.cpp b/src/bindings/python/src/pyopenvino/frontend/extension.cpp
index ce965c14bc5..69c0453ae93 100644
--- a/src/bindings/python/src/pyopenvino/frontend/extension.cpp
+++ b/src/bindings/python/src/pyopenvino/frontend/extension.cpp
@@ -40,15 +40,15 @@ void regclass_frontend_TelemetryExtension(py::module m) {
const std::string& action,
const std::string& label,
int value) {
- ConditionalGILScopedAcquire acquire;
+ py::gil_scoped_acquire acquire;
(*send_event_sp)(category, action, label, value);
},
[send_error_sp](const std::string& category, const std::string& error_message) {
- ConditionalGILScopedAcquire acquire;
+ py::gil_scoped_acquire acquire;
(*send_error_sp)(category, error_message);
},
[send_stack_trace_sp](const std::string& category, const std::string& error_message) {
- ConditionalGILScopedAcquire acquire;
+ py::gil_scoped_acquire acquire;
(*send_stack_trace_sp)(category, error_message);
});
}));
@@ -125,7 +125,7 @@ void regclass_frontend_ProgressReporterExtension(py::module m) {
ext.def(py::init([](py::function& callback) {
return std::make_shared<ProgressReporterExtension>([callback](float a, unsigned int b, unsigned int c) {
- ConditionalGILScopedAcquire acquire;
+ py::gil_scoped_acquire acquire;
callback(a, b, c);
});
}));
diff --git a/src/bindings/python/src/pyopenvino/graph/model.cpp b/src/bindings/python/src/pyopenvino/graph/model.cpp
index a4eef9fc1cd..51e21a0f143 100644
--- a/src/bindings/python/src/pyopenvino/graph/model.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/model.cpp
@@ -493,7 +493,7 @@ void regclass_graph_Model(py::module m) {
"reshape",
[](ov::Model& self, const ov::PartialShape& partial_shape, const py::dict& variables_shapes) {
const auto new_variable_shapes = get_variables_shapes(variables_shapes);
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
self.reshape(partial_shape, new_variable_shapes);
},
py::arg("partial_shape"),
@@ -535,7 +535,7 @@ void regclass_graph_Model(py::module m) {
[](ov::Model& self, const py::list& partial_shape, const py::dict& variables_shapes) {
const auto new_shape = Common::partial_shape_from_list(partial_shape);
const auto new_variables_shapes = get_variables_shapes(variables_shapes);
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
self.reshape(new_shape, new_variables_shapes);
},
py::arg("partial_shape"),
@@ -577,7 +577,7 @@ void regclass_graph_Model(py::module m) {
[](ov::Model& self, const py::tuple& partial_shape, const py::dict& variables_shapes) {
const auto new_shape = Common::partial_shape_from_list(partial_shape.cast<py::list>());
const auto new_variables_shapes = get_variables_shapes(variables_shapes);
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
self.reshape(new_shape, new_variables_shapes);
},
py::arg("partial_shape"),
@@ -618,7 +618,7 @@ void regclass_graph_Model(py::module m) {
"reshape",
[](ov::Model& self, const std::string& partial_shape, const py::dict& variables_shapes) {
const auto new_variables_shape = get_variables_shapes(variables_shapes);
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
self.reshape(ov::PartialShape(partial_shape), new_variables_shape);
},
py::arg("partial_shape"),
@@ -666,7 +666,7 @@ void regclass_graph_Model(py::module m) {
partial_shape_from_handle(item.second));
}
const auto new_variables_shapes = get_variables_shapes(variables_shapes);
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
self.reshape(new_shapes, new_variables_shapes);
},
py::arg("partial_shapes"),
diff --git a/src/bindings/python/src/pyopenvino/graph/op.cpp b/src/bindings/python/src/pyopenvino/graph/op.cpp
index 300c1e1f066..64c281efdf0 100644
--- a/src/bindings/python/src/pyopenvino/graph/op.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/op.cpp
@@ -24,7 +24,7 @@ void PyOp::validate_and_infer_types() {
}
bool PyOp::visit_attributes(ov::AttributeVisitor& value) {
- ConditionalGILScopedAcquire gil; // Acquire the GIL while in this scope.
+ py::gil_scoped_acquire gil; // Acquire the GIL while in this scope.
// Try to look up the overridden method on the Python side.
py::function overrided_py_method = pybind11::get_override(this, "visit_attributes");
if (overrided_py_method) { // method is found
@@ -34,7 +34,7 @@ bool PyOp::visit_attributes(ov::AttributeVisitor& value) {
}
std::shared_ptr<ov::Node> PyOp::clone_with_new_inputs(const ov::OutputVector& new_args) const {
- ConditionalGILScopedAcquire gil; // Acquire the GIL while in this scope.
+ py::gil_scoped_acquire gil; // Acquire the GIL while in this scope.
// Try to look up the overridden method on the Python side.
py::function overrided_py_method = pybind11::get_override(this, "clone_with_new_inputs");
if (overrided_py_method) { // method is found
@@ -62,7 +62,7 @@ bool PyOp::evaluate(ov::TensorVector& output_values, const ov::TensorVector& inp
}
bool PyOp::has_evaluate() const {
- ConditionalGILScopedAcquire gil; // Acquire the GIL while in this scope.
+ py::gil_scoped_acquire gil; // Acquire the GIL while in this scope.
// Try to look up the overridden method on the Python side.
py::function overrided_py_method = pybind11::get_override(this, "has_evaluate");
if (overrided_py_method) { // method is found
@@ -76,7 +76,7 @@ bool PyOp::has_evaluate() const {
}
void PyOp::update_type_info() {
- ConditionalGILScopedAcquire gil; // Acquire the GIL while in this scope.
+ py::gil_scoped_acquire gil; // Acquire the GIL while in this scope.
// Try to look up the overridden method on the Python side.
py::function overriden_py_method = pybind11::get_override(this, "get_type_info");
diff --git a/src/bindings/python/src/pyopenvino/graph/op_extension.cpp b/src/bindings/python/src/pyopenvino/graph/op_extension.cpp
index 3b459833143..ff1268b25c5 100644
--- a/src/bindings/python/src/pyopenvino/graph/op_extension.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/op_extension.cpp
@@ -20,7 +20,7 @@ const ov::DiscreteTypeInfo& PyOpExtension::get_type_info() const {
}
ov::OutputVector PyOpExtension::create(const ov::OutputVector& inputs, ov::AttributeVisitor& visitor) const {
- ConditionalGILScopedAcquire acquire;
+ py::gil_scoped_acquire acquire;
const auto node = py_handle_dtype();
diff --git a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp
index c56fe69f8f5..fc78dd527d5 100644
--- a/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/preprocess/pre_post_process.cpp
@@ -605,7 +605,7 @@ void regclass_graph_PrePostProcessor(py::module m) {
proc.def("build", [](ov::preprocess::PrePostProcessor& self) {
std::shared_ptr<ov::Model> model;
{
- ConditionalGILScopedRelease release;
+ py::gil_scoped_release release;
model = self.build();
}
py::type model_class = py::module_::import("openvino").attr("Model");
diff --git a/src/bindings/python/src/pyopenvino/utils/utils.cpp b/src/bindings/python/src/pyopenvino/utils/utils.cpp
index 8a233317a9d..e8b9450f8b2 100644
--- a/src/bindings/python/src/pyopenvino/utils/utils.cpp
+++ b/src/bindings/python/src/pyopenvino/utils/utils.cpp
@@ -299,26 +299,26 @@ std::map<std::string, ov::Any> properties_to_any_map(const std::map<std::string,
// Wrapped to sp due-to we need to hold GIL upon destruction of python function
auto py_encrypt = std::shared_ptr<py::function>(new py::function(std::move(property_list[0])),
[](py::function* py_encrypt) {
- ConditionalGILScopedAcquire acquire;
+ py::gil_scoped_acquire acquire;
delete py_encrypt;
});
auto py_decrypt = std::shared_ptr<py::function>(new py::function(std::move(property_list[1])),
[](py::function* py_decrypt) {
- ConditionalGILScopedAcquire acquire;
+ py::gil_scoped_acquire acquire;
delete py_decrypt;
});
std::function<std::string(const std::string&)> encrypt_func =
[py_encrypt](const std::string& in_str) -> std::string {
// Acquire GIL, execute Python function
- ConditionalGILScopedAcquire acquire;
+ py::gil_scoped_acquire acquire;
return (*py_encrypt)(py::bytes(in_str)).cast<std::string>();
};
std::function<std::string(const std::string&)> decrypt_func =
[py_decrypt](const std::string& in_str) -> std::string {
// Acquire GIL, execute Python function
- ConditionalGILScopedAcquire acquire;
+ py::gil_scoped_acquire acquire;
return (*py_decrypt)(py::bytes(in_str)).cast<std::string>();
};
ov::EncryptionCallbacks encryption_callbacks{encrypt_func, decrypt_func};
@@ -581,7 +581,7 @@ ov::Any py_object_to_any(const py::object& py_obj) {
// If there is no match fallback to py::object
} else if (py::isinstance<py::object>(py_obj)) {
return std::shared_ptr<py::object>(new py::object(py_obj), [](py::object* py_obj_reference) {
- ConditionalGILScopedAcquire acquire;
+ py::gil_scoped_acquire acquire;
delete py_obj_reference;
});
}
@@ -589,7 +589,8 @@ ov::Any py_object_to_any(const py::object& py_obj) {
}
std::shared_ptr<py::function> wrap_pyfunction(py::function f_callback) {
auto callback_sp = std::shared_ptr<py::function>(new py::function(std::move(f_callback)), [](py::function* c) {
- ConditionalGILScopedAcquire acquire;
+ // For free-threaded Python, gil_scoped_acquire still ensures thread is attached
+ py::gil_scoped_acquire acquire;
delete c;
});
return callback_sp;
diff --git a/src/bindings/python/src/pyopenvino/utils/utils.hpp b/src/bindings/python/src/pyopenvino/utils/utils.hpp
index 4aee27ed27a..c314b0a52a1 100644
--- a/src/bindings/python/src/pyopenvino/utils/utils.hpp
+++ b/src/bindings/python/src/pyopenvino/utils/utils.hpp
@@ -33,21 +33,6 @@
namespace py = pybind11;
-// Conditional GIL management for PEP 703 (free-threaded Python)
-inline constexpr bool PY_GIL_DISABLED {
-#if defined(Py_GIL_DISABLED) && Py_GIL_DISABLED
- true
-#else
- false
-#endif
-};
-
-using ConditionalGILScopedRelease = std::conditional_t<PY_GIL_DISABLED, std::monostate, py::gil_scoped_release>;
-using ConditionalGILScopedAcquire = std::conditional_t<PY_GIL_DISABLED, std::monostate, py::gil_scoped_acquire>;
-
-// For free-threaded Python, we don't need call_guard - use empty call_guard
-using CallGuardConditionalGILRelease = std::conditional_t<PY_GIL_DISABLED, py::call_guard<>, py::call_guard<py::gil_scoped_release>>;
-
namespace Common {
namespace utils {
diff --git a/src/bindings/python/tests/test_graph/test_custom_op.py b/src/bindings/python/tests/test_graph/test_custom_op.py
index 31fb8f5c937..ab993c07523 100644
--- a/src/bindings/python/tests/test_graph/test_custom_op.py
+++ b/src/bindings/python/tests/test_graph/test_custom_op.py
@@ -139,7 +139,6 @@ def prepared_paths(request, tmp_path):
({"wrong_np": np.array([1.5, 2.5], dtype="complex128")}, pytest.raises(TypeError), "Unsupported NumPy array dtype: complex128"),
({"wrong": {}}, pytest.raises(TypeError), "Unsupported attribute type: <class 'dict'>")
])
[email protected](sysconfig.get_config_var("Py_GIL_DISABLED"), reason="Ticket: 171534")
def test_visit_attributes_custom_op(device, prepared_paths, attributes, expectation, raise_msg):
input_shape = [2, 1]
@@ -199,7 +198,6 @@ def test_custom_add_model():
assert op_types == ["Parameter", "Parameter", "CustomAdd", "Result"]
[email protected](sysconfig.get_config_var("Py_GIL_DISABLED"), reason="Ticket: 171534")
def test_custom_op(device):
model = create_snake_model()
compiled_model = compile_model(model, device)
diff --git a/src/bindings/python/tests/test_runtime/test_async_infer_request.py b/src/bindings/python/tests/test_runtime/test_async_infer_request.py
index 54a547878f1..060da2393e2 100644
--- a/src/bindings/python/tests/test_runtime/test_async_infer_request.py
+++ b/src/bindings/python/tests/test_runtime/test_async_infer_request.py
@@ -28,7 +28,6 @@ from tests.utils.helpers import (
)
[email protected](sysconfig.get_config_var("Py_GIL_DISABLED"), reason="Ticket: 171534")
@pytest.mark.parametrize("share_inputs", [True, False])
def test_infer_queue(device, share_inputs):
jobs = 8
@@ -95,7 +94,6 @@ def test_infer_queue_userdata_is_empty_more_jobs(device):
assert infer_queue.userdata == [None, None, None, None, None]
[email protected](sysconfig.get_config_var("Py_GIL_DISABLED"), reason="Ticket: 171534")
def test_infer_queue_fail_on_cpp_model(device):
jobs = 6
num_request = 4
@@ -118,7 +116,6 @@ def test_infer_queue_fail_on_cpp_model(device):
assert "Port for tensor name Unknown was not found" in str(e.value)
[email protected](sysconfig.get_config_var("Py_GIL_DISABLED"), reason="Ticket: 171534")
def test_infer_queue_fail_on_py_model(device):
jobs = 1
num_request = 1
@@ -193,7 +190,6 @@ def test_infer_queue_get_idle_handle(device):
@pytest.mark.parametrize("share_inputs", [True, False])
[email protected](sysconfig.get_config_var("Py_GIL_DISABLED"), reason="Ticket: 171534")
def test_results_async_infer(device, share_inputs):
jobs = 8
num_request = 4
@@ -288,7 +284,6 @@ def test_cancel(device):
assert "[ INFER_CANCELLED ]" in str(e.value)
[email protected](sysconfig.get_config_var("Py_GIL_DISABLED"), reason="Ticket: 171534")
@pytest.mark.parametrize("share_inputs", [True, False])
def test_start_async(device, share_inputs):
core = Core()
diff --git a/src/bindings/python/tests/test_runtime/test_deprecated_runtime.py b/src/bindings/python/tests/test_runtime/test_deprecated_runtime.py
index 51a2a1e11a3..fa9749d720b 100644
--- a/src/bindings/python/tests/test_runtime/test_deprecated_runtime.py
+++ b/src/bindings/python/tests/test_runtime/test_deprecated_runtime.py
@@ -168,7 +168,6 @@ def test_output_replace():
assert res.input_value(0).get_node() == exp
[email protected](sysconfig.get_config_var("Py_GIL_DISABLED"), reason="Ticket: 171534")
@pytest.mark.parametrize("share_inputs", [True, False])
def test_infer_queue(device, share_inputs):
jobs = 8