Skip to content

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

[PyAPI] improvements of memory consumption (#28878)

Anastasia Kuporosovab60f5c5a0ecf
diff --git a/src/bindings/python/src/openvino/_ov_api.py b/src/bindings/python/src/openvino/_ov_api.py
index 2d9607135c0..1bbcdac8d96 100644
--- a/src/bindings/python/src/openvino/_ov_api.py
+++ b/src/bindings/python/src/openvino/_ov_api.py
@@ -2,6 +2,7 @@
# Copyright (C) 2018-2025 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
+import io
from types import TracebackType
from typing import Any, Iterable, Union, Optional, Dict, Tuple, List
from typing import Type as TypingType
@@ -538,8 +539,8 @@ class Core(CoreBase):
"""
def read_model(
self,
- model: Union[str, bytes, object],
- weights: Union[object, str, bytes, Tensor] = None,
+ model: Union[str, bytes, object, io.BytesIO],
+ weights: Union[object, str, bytes, Tensor, io.BytesIO] = None,
config: Optional[dict] = None
) -> Model:
config = {} if config is None else config
diff --git a/src/bindings/python/src/pyopenvino/core/compiled_model.cpp b/src/bindings/python/src/pyopenvino/core/compiled_model.cpp
index b7c12c4310f..9dd4073f3b6 100644
--- a/src/bindings/python/src/pyopenvino/core/compiled_model.cpp
+++ b/src/bindings/python/src/pyopenvino/core/compiled_model.cpp
@@ -46,12 +46,18 @@ void regclass_CompiledModel(py::module m) {
cls.def(
"export_model",
[](ov::CompiledModel& self) {
- std::stringstream _stream;
- {
- py::gil_scoped_release release;
- self.export_model(_stream);
- }
- return py::bytes(_stream.str());
+ py::object model_stream = py::module::import("io").attr("BytesIO")();
+
+ Common::utils::OutPyBuffer mb(model_stream);
+ std::ostream _stream(&mb);
+
+ self.export_model(_stream);
+
+ _stream.flush();
+ model_stream.attr("flush")();
+ model_stream.attr("seek")(0); // Always rewind stream!
+
+ return model_stream;
},
R"(
Exports the compiled model to bytes/output stream.
@@ -81,13 +87,14 @@ void regclass_CompiledModel(py::module m) {
"`model_stream` must be an io.BytesIO object but " +
(std::string)(py::repr(model_stream)) + "` provided");
}
- std::stringstream _stream;
- {
- py::gil_scoped_release release;
- self.export_model(_stream);
- }
+
+ Common::utils::OutPyBuffer mb(model_stream);
+ std::ostream _stream(&mb);
+
+ self.export_model(_stream);
+
+ _stream.flush();
model_stream.attr("flush")();
- model_stream.attr("write")(py::bytes(_stream.str()));
model_stream.attr("seek")(0); // Always rewind stream!
},
py::arg("model_stream"),
diff --git a/src/bindings/python/src/pyopenvino/core/core.cpp b/src/bindings/python/src/pyopenvino/core/core.cpp
index 778ca6a9874..be396b9b014 100644
--- a/src/bindings/python/src/pyopenvino/core/core.cpp
+++ b/src/bindings/python/src/pyopenvino/core/core.cpp
@@ -213,13 +213,10 @@ void regclass_Core(py::module m) {
cls.def(
"compile_model",
[](ov::Core& self,
- const py::object& model_buffer,
+ const py::object& model,
const py::object& weight_buffer,
const std::string& device_name,
const std::map<std::string, py::object>& properties) {
- std::stringstream _stream;
- _stream << model_buffer.cast<std::string>();
-
py::buffer_info info;
if (!py::isinstance<py::none>(weight_buffer)) {
auto p = weight_buffer.cast<py::bytes>();
@@ -236,7 +233,7 @@ void regclass_Core(py::module m) {
}
auto _properties = Common::utils::properties_to_any_map(properties);
py::gil_scoped_release release;
- return self.compile_model(_stream.str(), tensor, device_name, _properties);
+ return self.compile_model(model.cast<std::string>(), tensor, device_name, _properties);
},
py::arg("model_buffer"),
py::arg("weight_buffer"),
@@ -452,16 +449,14 @@ void regclass_Core(py::module m) {
py::object model_path,
py::object weights_path,
const std::map<std::string, py::object>& config) {
- if (py::isinstance(model_path, pybind11::module::import("io").attr("BytesIO"))) {
- std::stringstream _stream;
+ if (py::isinstance(model_path, py::module::import("io").attr("BytesIO"))) {
model_path.attr("seek")(0); // Always rewind stream!
- _stream << model_path
- .attr("read")() // alternative: model_path.attr("get_value")()
- .cast<std::string>();
+ py::buffer_info buffer_info = model_path.attr("getbuffer")().cast<py::buffer>().request();
+
py::buffer_info info;
if (!py::isinstance<py::none>(weights_path)) {
- auto p = weights_path.cast<py::bytes>();
- info = py::buffer(p).request();
+ py::object buffer = weights_path.attr("getbuffer")();
+ info = py::buffer(buffer).request();
}
size_t bin_size = static_cast<size_t>(info.size);
ov::Tensor tensor(ov::element::Type_t::u8, {bin_size});
@@ -471,7 +466,7 @@ void regclass_Core(py::module m) {
std::memcpy(tensor.data(), bin, bin_size);
}
py::gil_scoped_release release;
- return self.read_model(_stream.str(), tensor);
+ 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)) {
const std::string model_path_cpp{py::str(model_path)};
@@ -484,10 +479,8 @@ void regclass_Core(py::module m) {
return self.read_model(model_path_cpp, weights_path_cpp, any_map);
}
- std::stringstream str;
- str << "Provided python object type " << py::str(model_path.get_type())
- << " isn't supported as 'model' argument.";
- OPENVINO_THROW(str.str());
+ throw py::type_error("Provided python object type " + (std::string)(py::str(model_path.get_type())) +
+ " isn't supported as 'model' argument.");
},
py::arg("model"),
py::arg("weights") = py::none(),
@@ -506,7 +499,7 @@ void regclass_Core(py::module m) {
For PDPD format (*.pdmodel) weights parameter is not used.
For TF format (*.pb): weights parameter is not used.
For TFLite format (*.tflite) weights parameter is not used.
- :type weights: pathlib.Path
+ :type weights: Union[pathlib.Path, io.BytesIO]
:param config: Optional map of pairs: (property name, property value) relevant only for this read operation.
:type config: dict, optional
:return: A model.
diff --git a/src/bindings/python/src/pyopenvino/utils/utils.hpp b/src/bindings/python/src/pyopenvino/utils/utils.hpp
index 86ded201b0d..80b92be43d2 100644
--- a/src/bindings/python/src/pyopenvino/utils/utils.hpp
+++ b/src/bindings/python/src/pyopenvino/utils/utils.hpp
@@ -32,6 +32,52 @@ namespace py = pybind11;
namespace Common {
namespace utils {
+
+class OutPyBuffer : public std::streambuf {
+public:
+ OutPyBuffer(py::object bytes_io_stream) : m_py_stream{std::move(bytes_io_stream)} {}
+
+protected:
+ std::streamsize xsputn(const char_type* s, std::streamsize n) override {
+ return m_py_stream.attr("write")(py::bytes(s, n)).cast<std::streamsize>();
+ }
+
+ int_type overflow(int_type c) override {
+ char as_char = c;
+ if (m_py_stream.attr("write")(py::bytes(&as_char, 1)).cast<int>() != 1) {
+ return traits_type::eof();
+ }
+ return c;
+ }
+
+ pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which) override {
+ const int python_direction = [&]{
+ switch (dir) {
+ case std::ios_base::beg:
+ return 0;
+ case std::ios_base::cur:
+ return 1;
+ case std::ios_base::end:
+ return 2;
+ default:
+ return -1;
+ }
+ }();
+ if (python_direction == -1) {
+ return pos_type(off_type(-1));
+ }
+ const auto abs_pos = m_py_stream.attr("seek")(off, python_direction).cast<int>();
+ return pos_type(abs_pos);
+ }
+
+ pos_type seekpos(pos_type pos, std::ios_base::openmode which) override {
+ return seekoff(pos, std::ios_base::beg, which);
+ }
+
+private:
+ py::object m_py_stream;
+};
+
class MemoryBuffer : public std::streambuf {
public:
MemoryBuffer(char* data, std::size_t size) {
@@ -54,13 +100,14 @@ protected:
break;
default:
return pos_type(off_type(-1));
- }
+ }
return (gptr() < eback() || gptr() > egptr()) ? pos_type(off_type(-1)) : pos_type(gptr() - eback());
}
pos_type seekpos(pos_type pos, std::ios_base::openmode which) override {
return seekoff(pos, std::ios_base::beg, which);
}
+
};
enum class PY_TYPE : int { UNKNOWN = 0, STR, INT, FLOAT, BOOL, PARTIAL_SHAPE };
diff --git a/src/bindings/python/tests/test_runtime/test_compiled_model.py b/src/bindings/python/tests/test_runtime/test_compiled_model.py
index 9a914b80365..d8eae65ddb1 100644
--- a/src/bindings/python/tests/test_runtime/test_compiled_model.py
+++ b/src/bindings/python/tests/test_runtime/test_compiled_model.py
@@ -314,6 +314,7 @@ def test_compiled_model_from_buffer_in_memory(request, tmp_path, device):
xml = f.read()
compiled = core.compile_model(model=xml, weights=weights, device_name=device)
+ assert isinstance(compiled.outputs[0], ConstOutput)
_ = compiled([np.random.normal(size=list(input.shape)).astype(dtype=input.get_element_type().to_dtype()) for input in compiled.inputs])
diff --git a/src/bindings/python/tests/test_runtime/test_core.py b/src/bindings/python/tests/test_runtime/test_core.py
index 95868178656..cd07e978f63 100644
--- a/src/bindings/python/tests/test_runtime/test_core.py
+++ b/src/bindings/python/tests/test_runtime/test_core.py
@@ -174,7 +174,7 @@ def test_read_model_from_tensor(request, tmp_path):
def test_read_model_with_wrong_input():
core = Core()
- with pytest.raises(RuntimeError) as e:
+ with pytest.raises(TypeError) as e:
core.read_model(model=3, weights=3)
assert "Provided python object type <class 'int'> isn't supported as 'model' argument." in str(e.value)
@@ -228,6 +228,22 @@ def test_read_model_from_buffer(request, tmp_path):
assert isinstance(model, Model)
+# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request
+def test_read_model_from_bytesio(request, tmp_path):
+ from io import BytesIO
+
+ core = Core()
+ xml_path, bin_path = create_filenames_for_ir(request.node.name, tmp_path)
+ relu_model = get_relu_model()
+ serialize(relu_model, xml_path, bin_path)
+ with open(bin_path, "rb") as f:
+ weights = BytesIO(f.read())
+ with open(xml_path, "rb") as f:
+ xml = BytesIO(f.read())
+ model = core.read_model(model=xml, weights=weights)
+ assert isinstance(model, Model)
+
+
# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request
def test_model_from_buffer_valid(request, tmp_path):
core = Core()