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] Branch import_model and export_model implementations based on the model size #26237

Draft
wants to merge 35 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
9ad7cff
Initial commit
p-wysocki Aug 23, 2024
4ee2e30
Checkpoint
p-wysocki Aug 26, 2024
19fc4c9
Minor change
p-wysocki Aug 26, 2024
d84cea6
Minor change
p-wysocki Aug 26, 2024
9483d3b
Clang
p-wysocki Aug 26, 2024
212d46b
Cleanup
p-wysocki Aug 26, 2024
0c51150
Linter
p-wysocki Aug 26, 2024
a989a42
CR
p-wysocki Aug 26, 2024
1238c2a
Change expected val
p-wysocki Aug 26, 2024
2ad1884
Lower the test model size
p-wysocki Aug 26, 2024
5f12914
Merge branch 'master' into py_fstream
p-wysocki Aug 26, 2024
21c96b4
Modify test
p-wysocki Aug 27, 2024
28be873
Merge branch 'master' of https://github.com/openvinotoolkit/openvino …
p-wysocki Aug 27, 2024
74a8c67
Merge branch 'py_fstream' of https://github.com/p-wysocki/openvino in…
p-wysocki Aug 27, 2024
aef1750
Remove debug print
p-wysocki Aug 27, 2024
d0eb771
Experiment with GIL
p-wysocki Aug 27, 2024
3b9ed88
Experiment with GIL
p-wysocki Aug 27, 2024
1fbf729
Experiment with GIL
p-wysocki Aug 27, 2024
50fe743
Update src/bindings/python/tests/test_runtime/test_compiled_model.py
akuporos Aug 27, 2024
2d981d5
Reduce np array size
p-wysocki Aug 27, 2024
607c553
Merge branch 'py_fstream' of https://github.com/p-wysocki/openvino in…
p-wysocki Aug 27, 2024
8c009b2
Add support for export_model
p-wysocki Aug 28, 2024
34ad1c7
Merge branch 'master' into py_fstream
p-wysocki Aug 28, 2024
d997310
Fix data type
p-wysocki Aug 28, 2024
c5a9c47
Minor change
p-wysocki Aug 28, 2024
2c3486d
Change py::bytes to std::string conversion
p-wysocki Aug 28, 2024
ffed371
Reduce np array size
p-wysocki Aug 28, 2024
36fb6fd
Change rtype of util
p-wysocki Aug 30, 2024
ff4f6e1
Merge branch 'master' of https://github.com/openvinotoolkit/openvino …
p-wysocki Aug 30, 2024
ff50a1a
Merge branch 'master' of https://github.com/openvinotoolkit/openvino …
p-wysocki Sep 3, 2024
c973537
Merge branch 'master' into py_fstream
mlukasze Sep 4, 2024
5bb4c25
Merge branch 'master' into py_fstream
mlukasze Sep 5, 2024
fd10e60
Replace unknowns with TODOs
p-wysocki Sep 6, 2024
f66022a
Merge branch 'master' of https://github.com/openvinotoolkit/openvino …
p-wysocki Sep 6, 2024
e36f5dd
Merge branch 'py_fstream' of https://github.com/p-wysocki/openvino in…
p-wysocki Sep 6, 2024
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
34 changes: 19 additions & 15 deletions src/bindings/python/src/pyopenvino/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,28 +552,30 @@ void regclass_Core(py::module m) {
"`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);
py::buffer_info info = py::buffer(model_stream.attr("getbuffer")()).request();
constexpr auto one_gigabyte = static_cast<double>(1024) * 1024 * 1024;
const double size_in_gb = (info.size * info.itemsize) / one_gigabyte;
model_stream.attr("seek")(0); // Always rewind stream!
if (_stream.is_open()) {
ov::CompiledModel result;
// std::stringstream cannot handle streams > 2GB, in that case use std::fstream
if (size_in_gb > 2) {
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);
OPENVINO_ASSERT(_stream.is_open(), "Failed to open temporary file for model stream");
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;
std::fstream _fstream(filename, std::ios::in | std::ios::binary);
OPENVINO_ASSERT(_fstream.is_open(), "Failed to open temporary file for model stream");
result = self.import_model(_fstream, device_name, _properties);
_fstream.close();
if (std::remove(filename.c_str()) != 0) {
Expand All @@ -583,9 +585,11 @@ void regclass_Core(py::module m) {
PyErr_WarnEx(PyExc_RuntimeWarning, warning_message.c_str(), 1);
}
} else {
OPENVINO_THROW("Failed to open temporary file for model stream");
std::stringstream _stream;
_stream << model_stream.attr("read")().cast<std::string>();
py::gil_scoped_release release;
result = self.import_model(_stream, device_name, _properties);
}

return result;
},
py::arg("model_stream"),
Expand Down
21 changes: 20 additions & 1 deletion src/bindings/python/tests/test_runtime/test_compiled_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
generate_image,
generate_model_and_image,
generate_relu_compiled_model,
generate_model_with_memory,
create_filename_for_test)
from openvino import Model, Shape, Core, Tensor, serialize
from openvino import Model, Shape, Core, Tensor, serialize, Type
from openvino.runtime import ConstOutput

import openvino.properties as props
Expand Down Expand Up @@ -76,6 +77,24 @@ def test_export_import_advanced(device):
assert np.argmax(res[new_compiled.outputs[0]]) == 531


def test_export_import_large_model(device):
import io
core = Core()
if props.device.Capability.EXPORT_IMPORT not in core.get_property(device, props.device.capabilities):
pytest.skip(f"{core.get_property(device, props.device.full_name)} plugin due-to export, import model API isn't implemented.")

# model of size of roughly 2.01GB
model = generate_model_with_memory([6, 10000, 9000], Type.f32)
core = Core()
compiled_model = core.compile_model(model, device, {})
user_stream = io.BytesIO()
compiled_model.export_model(user_stream)
new_compiled = core.import_model(user_stream, device)
img = generate_image([6, 10000, 9000])
res = new_compiled.infer_new_request({"input_data": img})
assert np.argmax(res[new_compiled.outputs[0]]) == 14970
akuporos marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.parametrize("input_arguments", [[0], ["data"], []])
def test_get_input(device, input_arguments):
compiled_model = generate_relu_compiled_model(device)
Expand Down
Loading