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 38 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 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
b66b0cd
Merge branch 'master' of https://github.com/openvinotoolkit/openvino …
p-wysocki Nov 27, 2024
73ea6d9
Adjust to new master
p-wysocki Nov 27, 2024
660ff88
Adjust to new master
p-wysocki Nov 27, 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
44 changes: 38 additions & 6 deletions src/bindings/python/src/pyopenvino/core/compiled_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <pybind11/iostream.h>
#include <pybind11/stl.h>

#include <fstream>
#include <random>

#include "common.hpp"
#include "pyopenvino/core/compiled_model.hpp"
#include "pyopenvino/core/infer_request.hpp"
Expand Down Expand Up @@ -81,13 +84,42 @@ 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);
model_stream.attr("seek")(0); // Always rewind stream!
// std::stringstream cannot handle streams > 2GB, in that case we use std::fstream
// TODO: Determine model size after serialization
if (true) {
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 _fstream(filename, std::ios::out | std::ios::binary);
OPENVINO_ASSERT(_fstream.is_open(), "Failed to open temporary file for model stream");

// TODO: Assert all plugins' export_model functions work correctly for std::fstream and models > 2GB
self.export_model(_fstream);
_fstream.seekg(0, std::ios::beg);
{
std::string str((std::istreambuf_iterator<char>(_fstream)), std::istreambuf_iterator<char>());
_fstream.close();
model_stream.attr("flush")();
model_stream.attr("write")(py::bytes(str));
}
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 {
std::stringstream _stream;
{
py::gil_scoped_release release;
self.export_model(_stream);
}
model_stream.attr("flush")();
model_stream.attr("write")(py::bytes(_stream.str()));
}
model_stream.attr("flush")();
model_stream.attr("write")(py::bytes(_stream.str()));
model_stream.attr("seek")(0); // Always rewind stream!
},
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 @@ -12,12 +12,13 @@
generate_model_and_image,
generate_concat_compiled_model,
generate_relu_compiled_model,
generate_big_model_with_tile,
generate_relu_compiled_model_with_config,
encrypt_base64,
decrypt_base64,
create_filenames_for_ir,
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.runtime.opset13 as ops
Expand Down Expand Up @@ -99,6 +100,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_big_model_with_tile([1, 10, 9], [6, 10000, 9000], [6, 1000, 1000], 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([1, 10, 9])
res = new_compiled.infer_new_request({"input_data": img})
assert np.argmax(res[new_compiled.outputs[0]]) == 63006


# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request
@pytest.fixture
def prepare_blob_path(request, tmp_path):
Expand Down
8 changes: 8 additions & 0 deletions src/bindings/python/tests/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ def generate_model_with_memory(input_shape, data_type) -> openvino._pyopenvino.M
return model


def generate_big_model_with_tile(input_shape, constant_shape, tile_shape, data_type) -> openvino._pyopenvino.Model:
input_data = ops.parameter(input_shape, name="input_data", dtype=data_type)
init_val = ops.constant(np.ones(constant_shape, np.float32), data_type)
tile = ops.tile(input_data, tile_shape)
add = ops.add(init_val, tile, name="MemoryAdd")
return Model(add, [input_data], "TestModel")


def generate_concat_compiled_model(device, input_shape: List[int] = None, ov_type=Type.f32, numpy_dtype=np.float32):
if input_shape is None:
input_shape = [5]
Expand Down
Loading