Skip to content

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

[Py OV] Use pybind release_gil_before_calling_cpp_dtor flag for InferRequest (#32877)

Alicja Miloszewska2c204f445e9f
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 080ad986b52..b7fce5967d5 100644
--- a/src/bindings/python/src/pyopenvino/core/async_infer_queue.cpp
+++ b/src/bindings/python/src/pyopenvino/core/async_infer_queue.cpp
@@ -65,7 +65,7 @@ public:
});
size_t idle_handle = m_idle_handles.front();
// wait for request to make sure it returned from callback
- m_requests[idle_handle].m_request->wait();
+ m_requests[idle_handle].m_request.wait();
if (m_errors.size() > 0)
throw m_errors.front();
return idle_handle;
@@ -76,7 +76,7 @@ public:
// release GIL to avoid deadlock on python callback
py::gil_scoped_release release;
for (auto&& request : m_requests) {
- request.m_request->wait();
+ request.m_request.wait();
}
// acquire the mutex to access m_errors
std::lock_guard<std::mutex> lock(m_mutex);
@@ -88,7 +88,7 @@ public:
for (size_t handle = 0; handle < m_requests.size(); handle++) {
// auto end_time = m_requests[handle].m_end_time; // TODO: pass it bellow? like in InferRequestWrapper
- m_requests[handle].m_request->set_callback([this, handle /* ... */](std::exception_ptr exception_ptr) {
+ m_requests[handle].m_request.set_callback([this, handle /* ... */](std::exception_ptr exception_ptr) {
*m_requests[handle].m_end_time = Time::now();
{
// acquire the mutex to access m_idle_handles
@@ -115,7 +115,7 @@ public:
auto callback_sp = Common::utils::wrap_pyfunction(std::move(f_callback));
for (size_t handle = 0; handle < m_requests.size(); handle++) {
- m_requests[handle].m_request->set_callback([this, callback_sp, handle](std::exception_ptr exception_ptr) {
+ 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) {
// For free-threaded Python, gil_scoped_acquire still ensures thread is attached
@@ -197,13 +197,13 @@ void regclass_AsyncInferQueue(py::module m) {
// Set new inputs label/id from user
self.m_user_ids[handle] = userdata;
// Update inputs if there are any
- self.m_requests[handle].m_request->set_input_tensor(inputs);
+ 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
{
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();
+ self.m_requests[handle].m_request.start_async();
}
},
py::arg("inputs"),
@@ -243,13 +243,13 @@ void regclass_AsyncInferQueue(py::module m) {
// Set new inputs label/id from user
self.m_user_ids[handle] = userdata;
// Update inputs if there are any
- Common::set_request_tensors(*self.m_requests[handle].m_request, inputs);
+ 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
{
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();
+ self.m_requests[handle].m_request.start_async();
}
},
py::arg("inputs"),
diff --git a/src/bindings/python/src/pyopenvino/core/common.cpp b/src/bindings/python/src/pyopenvino/core/common.cpp
index 0e99343bd34..3ab448fe071 100644
--- a/src/bindings/python/src/pyopenvino/core/common.cpp
+++ b/src/bindings/python/src/pyopenvino/core/common.cpp
@@ -637,7 +637,7 @@ uint32_t get_optimal_number_of_requests(const ov::CompiledModel& actual) {
py::dict outputs_to_dict(InferRequestWrapper& request, bool share_outputs, bool decode_strings) {
py::dict res;
for (const auto& out : request.m_outputs) {
- auto t = request.m_request->get_tensor(out);
+ auto t = request.m_request.get_tensor(out);
if (t.get_element_type() == ov::element::string) {
if (share_outputs) {
PyErr_WarnEx(PyExc_RuntimeWarning, "Result of a string type will be copied to OVDict!", 1);
diff --git a/src/bindings/python/src/pyopenvino/core/infer_request.cpp b/src/bindings/python/src/pyopenvino/core/infer_request.cpp
index a21c4d557c9..205657035db 100644
--- a/src/bindings/python/src/pyopenvino/core/infer_request.cpp
+++ b/src/bindings/python/src/pyopenvino/core/infer_request.cpp
@@ -21,14 +21,17 @@ inline py::object run_sync_infer(InferRequestWrapper& self, bool share_outputs,
{
py::gil_scoped_release release;
*self.m_start_time = Time::now();
- self.m_request->infer();
+ self.m_request.infer();
*self.m_end_time = Time::now();
}
return Common::outputs_to_dict(self, share_outputs, decode_strings);
}
void regclass_InferRequest(py::module m) {
- py::class_<InferRequestWrapper, std::shared_ptr<InferRequestWrapper>> cls(m, "InferRequest");
+ py::class_<InferRequestWrapper, std::shared_ptr<InferRequestWrapper>> cls(
+ m,
+ "InferRequest",
+ py::release_gil_before_calling_cpp_dtor());
cls.doc() = "openvino.InferRequest represents infer request which can be run in asynchronous or "
"synchronous manners.";
@@ -41,7 +44,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_tensors",
[](InferRequestWrapper& self, const py::dict& inputs) {
- Common::set_request_tensors(*self.m_request, inputs);
+ Common::set_request_tensors(self.m_request, inputs);
},
py::arg("inputs"),
R"(
@@ -54,7 +57,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_tensors",
[](InferRequestWrapper& self, const std::string& tensor_name, const ov::TensorVector& tensors) {
- self.m_request->set_tensors(tensor_name, tensors);
+ self.m_request.set_tensors(tensor_name, tensors);
},
py::arg("tensor_name"),
py::arg("tensors"),
@@ -76,7 +79,7 @@ void regclass_InferRequest(py::module m) {
"set_tensors",
[](InferRequestWrapper& self, const std::string& tensor_name, const py::list& tensors) {
py::object pyTensorVector = py::module_::import("openvino").attr("TensorVector");
- self.m_request->set_tensors(tensor_name, pyTensorVector(tensors).cast<ov::TensorVector>());
+ self.m_request.set_tensors(tensor_name, pyTensorVector(tensors).cast<ov::TensorVector>());
},
py::arg("tensor_name"),
py::arg("tensors"),
@@ -98,7 +101,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_tensors",
[](InferRequestWrapper& self, const ov::Output<const ov::Node>& port, const ov::TensorVector& tensors) {
- self.m_request->set_tensors(port, tensors);
+ self.m_request.set_tensors(port, tensors);
},
py::arg("port"),
py::arg("tensors"),
@@ -123,7 +126,7 @@ void regclass_InferRequest(py::module m) {
"set_tensors",
[](InferRequestWrapper& self, const ov::Output<const ov::Node>& port, const py::list& tensors) {
py::object pyTensorVector = py::module_::import("openvino").attr("TensorVector");
- self.m_request->set_tensors(port, pyTensorVector(tensors).cast<ov::TensorVector>());
+ self.m_request.set_tensors(port, pyTensorVector(tensors).cast<ov::TensorVector>());
},
py::arg("port"),
py::arg("tensors"),
@@ -150,7 +153,7 @@ void regclass_InferRequest(py::module m) {
[](InferRequestWrapper& self, const py::dict& outputs) {
auto outputs_map = Common::containers::cast_to_tensor_index_map(outputs);
for (auto&& output : outputs_map) {
- self.m_request->set_output_tensor(output.first, output.second);
+ self.m_request.set_output_tensor(output.first, output.second);
}
},
py::arg("outputs"),
@@ -167,7 +170,7 @@ void regclass_InferRequest(py::module m) {
[](InferRequestWrapper& self, const py::dict& inputs) {
auto inputs_map = Common::containers::cast_to_tensor_index_map(inputs);
for (auto&& input : inputs_map) {
- self.m_request->set_input_tensor(input.first, input.second);
+ self.m_request.set_input_tensor(input.first, input.second);
}
},
py::arg("inputs"),
@@ -181,7 +184,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_input_tensors",
[](InferRequestWrapper& self, const ov::TensorVector& tensors) {
- self.m_request->set_input_tensors(tensors);
+ self.m_request.set_input_tensors(tensors);
},
py::arg("tensors"),
R"(
@@ -199,7 +202,7 @@ void regclass_InferRequest(py::module m) {
"set_input_tensors",
[](InferRequestWrapper& self, const py::list& tensors) {
py::object pyTensorVector = py::module_::import("openvino").attr("TensorVector");
- self.m_request->set_input_tensors(pyTensorVector(tensors).cast<ov::TensorVector>());
+ self.m_request.set_input_tensors(pyTensorVector(tensors).cast<ov::TensorVector>());
},
py::arg("tensors"),
R"(
@@ -216,7 +219,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_input_tensors",
[](InferRequestWrapper& self, size_t idx, const ov::TensorVector& tensors) {
- self.m_request->set_input_tensors(idx, tensors);
+ self.m_request.set_input_tensors(idx, tensors);
},
py::arg("idx"),
py::arg("tensors"),
@@ -237,7 +240,7 @@ void regclass_InferRequest(py::module m) {
"set_input_tensors",
[](InferRequestWrapper& self, size_t idx, const py::list& tensors) {
py::object pyTensorVector = py::module_::import("openvino").attr("TensorVector");
- self.m_request->set_input_tensors(idx, pyTensorVector(tensors).cast<ov::TensorVector>());
+ self.m_request.set_input_tensors(idx, pyTensorVector(tensors).cast<ov::TensorVector>());
},
py::arg("idx"),
py::arg("tensors"),
@@ -258,7 +261,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"infer",
[](InferRequestWrapper& self, const ov::Tensor& inputs, bool share_outputs, bool decode_strings) {
- self.m_request->set_input_tensor(inputs);
+ self.m_request.set_input_tensor(inputs);
return run_sync_infer(self, share_outputs, decode_strings);
},
py::arg("inputs"),
@@ -287,7 +290,7 @@ void regclass_InferRequest(py::module m) {
"infer",
[](InferRequestWrapper& self, const py::dict& inputs, bool share_outputs, bool decode_strings) {
// Update inputs if there are any
- Common::set_request_tensors(*self.m_request, inputs);
+ Common::set_request_tensors(self.m_request, inputs);
// Call Infer function
return run_sync_infer(self, share_outputs, decode_strings);
},
@@ -312,17 +315,17 @@ void regclass_InferRequest(py::module m) {
"start_async",
[](InferRequestWrapper& self, const ov::Tensor& inputs, py::object& userdata) {
// Update inputs if there are any
- self.m_request->set_input_tensor(inputs);
+ self.m_request.set_input_tensor(inputs);
if (!userdata.is(py::none())) {
if (self.m_user_callback_defined) {
- self.m_userdata = userdata;
+ *self.m_userdata = userdata;
} else {
PyErr_WarnEx(PyExc_RuntimeWarning, "There is no callback function to pass `userdata` into!", 1);
}
}
py::gil_scoped_release release;
*self.m_start_time = Time::now();
- self.m_request->start_async();
+ self.m_request.start_async();
},
py::arg("inputs"),
py::arg("userdata"),
@@ -351,17 +354,17 @@ void regclass_InferRequest(py::module m) {
"start_async",
[](InferRequestWrapper& self, const py::dict& inputs, py::object& userdata) {
// Update inputs if there are any
- Common::set_request_tensors(*self.m_request, inputs);
+ Common::set_request_tensors(self.m_request, inputs);
if (!userdata.is(py::none())) {
if (self.m_user_callback_defined) {
- self.m_userdata = userdata;
+ *self.m_userdata = userdata;
} else {
PyErr_WarnEx(PyExc_RuntimeWarning, "There is no callback function!", 1);
}
}
py::gil_scoped_release release;
*self.m_start_time = Time::now();
- self.m_request->start_async();
+ self.m_request.start_async();
},
py::arg("inputs"),
py::arg("userdata"),
@@ -383,7 +386,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"cancel",
[](InferRequestWrapper& self) {
- self.m_request->cancel();
+ self.m_request.cancel();
},
R"(
Cancels inference request.
@@ -393,7 +396,7 @@ void regclass_InferRequest(py::module m) {
"wait",
[](InferRequestWrapper& self) {
py::gil_scoped_release release;
- self.m_request->wait();
+ self.m_request.wait();
},
R"(
Waits for the result to become available.
@@ -406,7 +409,7 @@ void regclass_InferRequest(py::module m) {
"wait_for",
[](InferRequestWrapper& self, const int timeout) {
py::gil_scoped_release release;
- return self.m_request->wait_for(std::chrono::milliseconds(timeout));
+ return self.m_request.wait_for(std::chrono::milliseconds(timeout));
},
py::arg("timeout"),
R"(
@@ -425,13 +428,13 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_callback",
[](InferRequestWrapper& self, py::function callback, py::object& userdata) {
- self.m_userdata = userdata;
+ *self.m_userdata = userdata;
self.m_user_callback_defined = true;
// need to acquire GIL before py::function deletion
auto callback_sp = Common::utils::wrap_pyfunction(std::move(callback));
- self.m_request->set_callback([&self, callback_sp](std::exception_ptr exception_ptr) {
+ self.m_request.set_callback([&self, callback_sp](std::exception_ptr exception_ptr) {
*self.m_end_time = Time::now();
try {
if (exception_ptr) {
@@ -442,7 +445,7 @@ void regclass_InferRequest(py::module m) {
}
// For free-threaded Python, gil_scoped_acquire still ensures thread is attached
py::gil_scoped_acquire acquire;
- (*callback_sp)(self.m_userdata);
+ (*callback_sp)(*self.m_userdata);
});
},
py::arg("callback"),
@@ -459,7 +462,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"get_tensor",
[](InferRequestWrapper& self, const std::string& name) {
- return self.m_request->get_tensor(name);
+ return self.m_request.get_tensor(name);
},
py::arg("name"),
R"(
@@ -474,7 +477,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"get_tensor",
[](InferRequestWrapper& self, const ov::Output<const ov::Node>& port) {
- return self.m_request->get_tensor(port);
+ return self.m_request.get_tensor(port);
},
py::arg("port"),
R"(
@@ -489,7 +492,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"get_tensor",
[](InferRequestWrapper& self, const ov::Output<ov::Node>& port) {
- return self.m_request->get_tensor(port);
+ return self.m_request.get_tensor(port);
},
py::arg("port"),
R"(
@@ -504,7 +507,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"get_input_tensor",
[](InferRequestWrapper& self, size_t idx) {
- return self.m_request->get_input_tensor(idx);
+ return self.m_request.get_input_tensor(idx);
},
py::arg("index"),
R"(
@@ -521,7 +524,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"get_input_tensor",
[](InferRequestWrapper& self) {
- return self.m_request->get_input_tensor();
+ return self.m_request.get_input_tensor();
},
R"(
Gets input tensor of InferRequest.
@@ -534,7 +537,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"get_output_tensor",
[](InferRequestWrapper& self, size_t idx) {
- return self.m_request->get_output_tensor(idx);
+ return self.m_request.get_output_tensor(idx);
},
py::arg("index"),
R"(
@@ -550,7 +553,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"get_output_tensor",
[](InferRequestWrapper& self) {
- return self.m_request->get_output_tensor();
+ return self.m_request.get_output_tensor();
},
R"(
Gets output tensor of InferRequest.
@@ -563,7 +566,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_tensor",
[](InferRequestWrapper& self, const std::string& name, const RemoteTensorWrapper& tensor) {
- self.m_request->set_tensor(name, tensor.tensor);
+ self.m_request.set_tensor(name, tensor.tensor);
},
py::arg("name"),
py::arg("tensor"),
@@ -580,7 +583,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_tensor",
[](InferRequestWrapper& self, const std::string& name, const ov::Tensor& tensor) {
- self.m_request->set_tensor(name, tensor);
+ self.m_request.set_tensor(name, tensor);
},
py::arg("name"),
py::arg("tensor"),
@@ -597,7 +600,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_tensor",
[](InferRequestWrapper& self, const ov::Output<const ov::Node>& port, const ov::Tensor& tensor) {
- self.m_request->set_tensor(port, tensor);
+ self.m_request.set_tensor(port, tensor);
},
py::arg("port"),
py::arg("tensor"),
@@ -614,7 +617,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_tensor",
[](InferRequestWrapper& self, const ov::Output<ov::Node>& port, const ov::Tensor& tensor) {
- self.m_request->set_tensor(port, tensor);
+ self.m_request.set_tensor(port, tensor);
},
py::arg("port"),
py::arg("tensor"),
@@ -631,7 +634,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_input_tensor",
[](InferRequestWrapper& self, size_t idx, const ov::Tensor& tensor) {
- self.m_request->set_input_tensor(idx, tensor);
+ self.m_request.set_input_tensor(idx, tensor);
},
py::arg("index"),
py::arg("tensor"),
@@ -649,7 +652,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_input_tensor",
[](InferRequestWrapper& self, const ov::Tensor& tensor) {
- self.m_request->set_input_tensor(tensor);
+ self.m_request.set_input_tensor(tensor);
},
py::arg("tensor"),
R"(
@@ -664,7 +667,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_output_tensor",
[](InferRequestWrapper& self, size_t idx, const ov::Tensor& tensor) {
- self.m_request->set_output_tensor(idx, tensor);
+ self.m_request.set_output_tensor(idx, tensor);
},
py::arg("index"),
py::arg("tensor"),
@@ -681,7 +684,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"set_output_tensor",
[](InferRequestWrapper& self, const ov::Tensor& tensor) {
- self.m_request->set_output_tensor(tensor);
+ self.m_request.set_output_tensor(tensor);
},
py::arg("tensor"),
R"(
@@ -696,7 +699,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"get_profiling_info",
[](InferRequestWrapper& self) {
- return self.m_request->get_profiling_info();
+ return self.m_request.get_profiling_info();
},
py::call_guard<py::gil_scoped_release>(),
R"(
@@ -713,7 +716,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"query_state",
[](InferRequestWrapper& self) {
- return self.m_request->query_state();
+ return self.m_request.query_state();
},
py::call_guard<py::gil_scoped_release>(),
R"(
@@ -728,7 +731,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"reset_state",
[](InferRequestWrapper& self) {
- return self.m_request->reset_state();
+ return self.m_request.reset_state();
},
R"(
Resets all internal variable states for relevant infer request to
@@ -738,7 +741,7 @@ void regclass_InferRequest(py::module m) {
cls.def(
"get_compiled_model",
[](InferRequestWrapper& self) {
- return self.m_request->get_compiled_model();
+ return self.m_request.get_compiled_model();
},
R"(
Returns the compiled model.
@@ -750,7 +753,7 @@ void regclass_InferRequest(py::module m) {
cls.def_property_readonly(
"userdata",
[](InferRequestWrapper& self) {
- return self.m_userdata;
+ return *self.m_userdata;
},
R"(
Gets currently held userdata.
@@ -817,7 +820,7 @@ void regclass_InferRequest(py::module m) {
cls.def_property_readonly(
"profiling_info",
[](InferRequestWrapper& self) {
- return self.m_request->get_profiling_info();
+ return self.m_request.get_profiling_info();
},
py::call_guard<py::gil_scoped_release>(),
R"(
diff --git a/src/bindings/python/src/pyopenvino/core/infer_request.hpp b/src/bindings/python/src/pyopenvino/core/infer_request.hpp
index a8d71ba9629..f85ff05b56f 100644
--- a/src/bindings/python/src/pyopenvino/core/infer_request.hpp
+++ b/src/bindings/python/src/pyopenvino/core/infer_request.hpp
@@ -10,6 +10,7 @@
#include <openvino/runtime/infer_request.hpp>
#include "openvino/core/except.hpp"
+#include "pyopenvino/utils/utils.hpp"
namespace py = pybind11;
@@ -32,10 +33,10 @@ public:
const std::vector<ov::Output<const ov::Node>>& outputs,
bool set_default_callback = true,
py::object userdata = py::none())
- : m_request{InferRequestWrapper::wrap_infer_request_to_sp(std::move(request))},
+ : m_request{std::move(request)},
m_inputs{inputs},
m_outputs{outputs},
- m_userdata{userdata} {
+ m_userdata{Common::utils::wrap_pyobject_to_sp(std::move(userdata))} {
m_start_time = std::make_shared<Time::time_point>(Time::time_point{});
m_end_time = std::make_shared<Time::time_point>(Time::time_point{});
@@ -44,7 +45,7 @@ public:
// Bump reference counter
auto end_time = m_end_time;
// Set standard callback which saves "end-time" for inference call
- m_request->set_callback([end_time](std::exception_ptr exception_ptr) {
+ m_request.set_callback([end_time](std::exception_ptr exception_ptr) {
*end_time = Time::now();
try {
if (exception_ptr) {
@@ -73,14 +74,14 @@ public:
}
// Original ov::InferRequest class that is held by this wrapper
- std::shared_ptr<ov::InferRequest> m_request;
+ ov::InferRequest m_request;
// Inputs and Outputs inherrited from ov::CompiledModel
std::vector<ov::Output<const ov::Node>> m_inputs;
std::vector<ov::Output<const ov::Node>> m_outputs;
// A flag which is set when a user defines a custom callback on InferRequest
bool m_user_callback_defined = false;
// Data that is passed by user from Python->C++
- py::object m_userdata;
+ std::shared_ptr<py::object> m_userdata;
// Times of inference's start and finish
std::shared_ptr<Time::time_point> m_start_time; // proposal: change to unique_ptr
std::shared_ptr<Time::time_point> m_end_time;
@@ -91,18 +92,11 @@ private:
tensors.reserve(v.size());
for (auto&& node : v) {
- tensors.push_back(m_request->get_tensor(node));
+ tensors.push_back(m_request.get_tensor(node));
}
return tensors;
}
-
- static std::shared_ptr<ov::InferRequest> wrap_infer_request_to_sp(ov::InferRequest request) {
- return std::shared_ptr<ov::InferRequest>(new ov::InferRequest(std::move(request)), [](ov::InferRequest* request) {
- py::gil_scoped_release release;
- delete request;
- });
- }
};
void regclass_InferRequest(py::module m);
diff --git a/src/bindings/python/src/pyopenvino/utils/utils.cpp b/src/bindings/python/src/pyopenvino/utils/utils.cpp
index e8b9450f8b2..245860029e4 100644
--- a/src/bindings/python/src/pyopenvino/utils/utils.cpp
+++ b/src/bindings/python/src/pyopenvino/utils/utils.cpp
@@ -596,6 +596,13 @@ std::shared_ptr<py::function> wrap_pyfunction(py::function f_callback) {
return callback_sp;
}
+std::shared_ptr<py::object> wrap_pyobject_to_sp(py::object obj) {
+ return std::shared_ptr<py::object>(new py::object(std::move(obj)), [](py::object* o) {
+ py::gil_scoped_acquire acquire;
+ delete o;
+ });
+}
+
std::filesystem::path to_fs_path(const py::object& path) {
// import pathlib.Path
py::object Path = py::module_::import("pathlib").attr("Path");
diff --git a/src/bindings/python/src/pyopenvino/utils/utils.hpp b/src/bindings/python/src/pyopenvino/utils/utils.hpp
index c314b0a52a1..62460929de4 100644
--- a/src/bindings/python/src/pyopenvino/utils/utils.hpp
+++ b/src/bindings/python/src/pyopenvino/utils/utils.hpp
@@ -119,6 +119,8 @@ private:
std::shared_ptr<py::function> wrap_pyfunction(py::function f_callback);
+ std::shared_ptr<py::object> wrap_pyobject_to_sp(py::object obj);
+
std::filesystem::path to_fs_path(const py::object& path);
}; // namespace utils
}; // namespace Common