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] Replace std::stringstream with std::fstream in import_model #25724

Merged
merged 9 commits into from
Aug 5, 2024
40 changes: 33 additions & 7 deletions src/bindings/python/src/pyopenvino/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <pybind11/stl.h>

#include <fstream>
#include <openvino/core/any.hpp>
#include <openvino/runtime/core.hpp>
#include <pyopenvino/core/tensor.hpp>
Expand Down Expand Up @@ -544,19 +545,44 @@ void regclass_Core(py::module m) {
const py::object& model_stream,
const std::string& device_name,
const std::map<std::string, py::object>& properties) {
auto _properties = Common::utils::properties_to_any_map(properties);
const auto _properties = Common::utils::properties_to_any_map(properties);
if (!(py::isinstance(model_stream, pybind11::module::import("io").attr("BytesIO")))) {
throw py::type_error("CompiledModel.import_model(model_stream) incompatible function argument: "
"`model_stream` must be an io.BytesIO object but " +
(std::string)(py::repr(model_stream)) + "` provided");
}
model_stream.attr("seek")(0); // Always rewind stream!
std::stringstream _stream;
_stream << model_stream
.attr("read")() // alternative: model_stream.attr("get_value")()
.cast<std::string>();
py::gil_scoped_release release;
return self.import_model(_stream, device_name, _properties);
const std::string filename = "model_stream.txt";
praasz marked this conversation as resolved.
Show resolved Hide resolved
std::fstream _stream(filename, std::ios::out | std::ios::binary);
p-wysocki marked this conversation as resolved.
Show resolved Hide resolved
ilya-lavrenov marked this conversation as resolved.
Show resolved Hide resolved
if (_stream.is_open()) {
const py::bytes data = model_stream.attr("read")();
// convert the Python bytes object to C++ string
char* buffer;
Py_ssize_t length;
PYBIND11_BYTES_AS_STRING_AND_SIZE(data.ptr(), &buffer, &length);
_stream.write(buffer, length);
_stream.close();
} else {
OPENVINO_THROW("Failed to open temporary file for model stream");
}

ov::CompiledModel result;
std::fstream _fstream(filename, std::ios::in | std::ios::binary);
if (_fstream.is_open()) {
py::gil_scoped_release release;
result = self.import_model(_fstream, device_name, _properties);
_fstream.close();
if (std::remove(filename.c_str()) != 0) {
const std::string abs_path =
py::module_::import("os").attr("getcwd")().cast<std::string>() + "/" + filename;
const std::string warning_message = "Temporary file " + abs_path + " failed to delete!";
PyErr_WarnEx(PyExc_RuntimeWarning, warning_message.c_str(), 1);
}
} else {
OPENVINO_THROW("Failed to open temporary file for model stream");
}

return result;
},
py::arg("model_stream"),
py::arg("device_name"),
Expand Down
Loading