Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PyOV] Improve import_model memory consumption #27451

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 10 additions & 49 deletions src/bindings/python/src/pyopenvino/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,70 +496,31 @@ void regclass_Core(py::module m) {
:rtype: openvino.runtime.Model
)");

cls.def(
"import_model",
[](ov::Core& self,
const std::string& model_stream,
const std::string& device_name,
const std::map<std::string, py::object>& properties) {
auto _properties = Common::utils::properties_to_any_map(properties);
py::gil_scoped_release release;
std::stringstream _stream;
_stream << model_stream;
return self.import_model(_stream, device_name, _properties);
},
py::arg("model_stream"),
py::arg("device_name"),
py::arg("properties"),
R"(
Imports a compiled model from a previously exported one.

GIL is released while running this function.

:param model_stream: Input stream, containing a model previously exported, using export_model method.
:type model_stream: bytes
:param device_name: Name of device to which compiled model is imported.
Note: if device_name is not used to compile the original model, an exception is thrown.
:type device_name: str
:param properties: Optional map of pairs: (property name, property value) relevant only for this load operation.
:type properties: dict, optional
:return: A compiled model.
:rtype: openvino.runtime.CompiledModel

:Example:
.. code-block:: python

user_stream = compiled.export_model()

with open('./my_model', 'wb') as f:
f.write(user_stream)

# ...

new_compiled = core.import_model(user_stream, "CPU")
)");

// keep as second one to solve overload resolution problem
cls.def(
"import_model",
[](ov::Core& self,
const py::object& model_stream,
const std::string& device_name,
const std::map<std::string, py::object>& properties) {
const auto _properties = Common::utils::properties_to_any_map(properties);
if (!(py::isinstance(model_stream, pybind11::module::import("io").attr("BytesIO")))) {
if (!(py::isinstance(model_stream, pybind11::module::import("io").attr("BytesIO"))) && !py::isinstance<py::bytes>(model_stream)) {
throw py::type_error("CompiledModel.import_model(model_stream) incompatible function argument: "
"`model_stream` must be an io.BytesIO object but " +
"`model_stream` must be an io.BytesIO object or bytes but " +
(std::string)(py::repr(model_stream)) + "` provided");
}
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distr(1000, 9999);
std::string filename = "model_stream_" + std::to_string(distr(gen)) + ".txt";
std::fstream _stream(filename, std::ios::out | std::ios::binary);
model_stream.attr("seek")(0); // Always rewind stream!
if (_stream.is_open()) {
const py::bytes data = model_stream.attr("read")();
py::bytes data;
if (py::isinstance(model_stream, pybind11::module::import("io").attr("BytesIO"))) {
model_stream.attr("seek")(0); // Always rewind stream!
data = model_stream.attr("read")();
} else {
data = model_stream;
}
// convert the Python bytes object to C++ string
char* buffer;
Py_ssize_t length;
Expand Down Expand Up @@ -601,7 +562,7 @@ void regclass_Core(py::module m) {


:param model_stream: Input stream, containing a model previously exported, using export_model method.
:type model_stream: io.BytesIO
:type model_stream: Union[io.BytesIO, bytes]
:param device_name: Name of device to which compiled model is imported.
Note: if device_name is not used to compile the original model, an exception is thrown.
:type device_name: str
Expand Down
Loading