Skip to content

Commit

Permalink
[PyOV] GIL free creation of RemoteTensors (openvinotoolkit#23049)
Browse files Browse the repository at this point in the history
### Details:
 - Release GIL while creating RemoteTensor instances.
 - Addded missing docs to Core class.
 - To be tested(?)

### Tickets:
 - *CVS-129477*

---------

Co-authored-by: Anastasia Kuporosova <anastasia.kuporosova@intel.com>
  • Loading branch information
Jan Iwaszkiewicz and akuporos authored Feb 29, 2024
1 parent 3c821b7 commit 9c800b9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
39 changes: 36 additions & 3 deletions src/bindings/python/src/pyopenvino/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,21 @@ void regclass_Core(py::module m) {
},
py::arg("model"),
py::arg("context"),
py::arg("properties"));
py::arg("properties"),
R"(
Creates a compiled model from a source model within a specified remote context.
GIL is released while running this function.
:param model: Model acquired from read_model function.
:type model: openvino.Model
:param context: RemoteContext instance.
:type context: openvino.RemoteContext
:param properties: dict of pairs: (property name, property value) relevant only for this load operation.
:type properties: dict
:return: A compiled model.
:rtype: openvino.CompiledModel
)");

cls.def(
"create_context",
Expand All @@ -253,14 +267,33 @@ void regclass_Core(py::module m) {
return RemoteContextWrapper(self.create_context(device_name, _properties));
},
py::arg("device_name"),
py::arg("properties"));
py::arg("properties"),
R"(
Creates a new remote shared context object on the specified accelerator device
using specified plugin-specific low-level device API parameters.
:param device_name: Name of a device to create a new shared context on.
:type device_name: str
:param device_name: dict of device-specific shared context remote properties.
:type device_name: dict
:return: Remote context instance.
:rtype: openvino.RemoteContext
)");

cls.def(
"get_default_context",
[](ov::Core& self, const std::string& device_name) {
return RemoteContextWrapper(self.get_default_context(device_name));
},
py::arg("device_name"));
py::arg("device_name"),
R"(
Gets default (plugin-supplied) shared context object for the specified accelerator device.
:param device_name: Name of a device to get a default shared context from.
:type device_name: str
:return: Remote context instance.
:rtype: openvino.RemoteContext
)");

cls.def("get_versions",
&ov::Core::get_versions,
Expand Down
29 changes: 22 additions & 7 deletions src/bindings/python/src/pyopenvino/core/remote_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +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);
py::gil_scoped_release release;
return RemoteTensorWrapper(self.context.create_tensor(type, shape, _properties));
},
py::arg("type"),
Expand All @@ -61,6 +62,8 @@ void regclass_RemoteContext(py::module m) {
using the specified tensor description and low-level device-specific parameters.
Returns the object that implements the RemoteTensor interface.
GIL is released while running this function.
:param type: Defines the element type of the tensor.
:type type: openvino.Type
:param shape: Defines the shape of the tensor.
Expand All @@ -76,6 +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);
},
py::call_guard<py::gil_scoped_release>(),
py::arg("type"),
py::arg("shape"),
R"(
Expand All @@ -84,6 +88,8 @@ void regclass_RemoteContext(py::module m) {
(if corresponding extension is available), which could be more efficient
than regular host memory.
GIL is released while running this function.
:param type: Defines the element type of the tensor.
:type type: openvino.Type
:param shape: Defines the shape of the tensor.
Expand Down Expand Up @@ -124,13 +130,17 @@ void regclass_VAContext(py::module m) {
cls.def(
"create_tensor_nv12",
[](VAContextWrapper& self, const size_t height, const size_t width, const uint32_t nv12_surface) {
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},
{ov::intel_gpu::va_plane.name(), uint32_t(0)}};
auto y_tensor = self.context.create_tensor(ov::element::u8, {1, height, width, 1}, tensor_params);
tensor_params[ov::intel_gpu::va_plane.name()] = uint32_t(1);
auto uv_tensor = self.context.create_tensor(ov::element::u8, {1, height / 2, width / 2, 2}, tensor_params);
ov::RemoteTensor y_tensor, uv_tensor;
{
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},
{ov::intel_gpu::va_plane.name(), uint32_t(0)}};
y_tensor = self.context.create_tensor(ov::element::u8, {1, height, width, 1}, tensor_params);
tensor_params[ov::intel_gpu::va_plane.name()] = uint32_t(1);
uv_tensor = self.context.create_tensor(ov::element::u8, {1, height / 2, width / 2, 2}, tensor_params);
}
return py::make_tuple(VASurfaceTensorWrapper(y_tensor), VASurfaceTensorWrapper(uv_tensor));
},
py::arg("height"),
Expand All @@ -140,6 +150,8 @@ void regclass_VAContext(py::module m) {
This function is used to obtain a NV12 tensor from NV12 VA decoder output.
The result contains two remote tensors for Y and UV planes of the surface.
GIL is released while running this function.
:param height: A height of Y plane.
:type height: int
:param width: A width of Y plane
Expand All @@ -162,13 +174,16 @@ void regclass_VAContext(py::module m) {
{ov::intel_gpu::va_plane.name(), plane}};
return VASurfaceTensorWrapper(self.context.create_tensor(type, shape, params));
},
py::call_guard<py::gil_scoped_release>(),
py::arg("type"),
py::arg("shape"),
py::arg("surface"),
py::arg("plane") = 0,
R"(
Create remote tensor from VA surface handle.
GIL is released while running this function.
:param type: Defines the element type of the tensor.
:type type: openvino.Type
:param shape: Defines the shape of the tensor.
Expand Down

0 comments on commit 9c800b9

Please sign in to comment.