Skip to content

Commit

Permalink
[PyOV] Replace std::stringstream with std::fstream in `import_mod…
Browse files Browse the repository at this point in the history
…el` (#25724)

### Details:
 - The current implementation breaks when the model size is > 2gb
 - `std::fstream` does not limit the model size
- Tested in
https://github.com/openvinotoolkit/openvino/blob/master/src/bindings/python/tests/test_runtime/test_compiled_model.py#L57
 - The fix has been verified

### TODO:
 - Should we simulate > 2gb model case in tests?

### Tickets:
 - EISW-130771
  • Loading branch information
p-wysocki authored Aug 5, 2024
1 parent e35acf9 commit a6413b4
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/bindings/python/src/pyopenvino/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

#include <pybind11/stl.h>

#include <fstream>
#include <openvino/core/any.hpp>
#include <openvino/runtime/core.hpp>
#include <pyopenvino/core/tensor.hpp>
#include <random>

#include "common.hpp"
#include "pyopenvino/core/remote_context.hpp"
Expand Down Expand Up @@ -544,19 +546,47 @@ 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");
}
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!
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);
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

0 comments on commit a6413b4

Please sign in to comment.