[PyOV] Add pathlib.Path objects as arguments to serialize pass (#14746)
This commit is contained in:
parent
464b5e0b48
commit
243da9bc1d
@ -17,12 +17,17 @@
|
||||
#include <openvino/pass/serialize.hpp>
|
||||
#include <openvino/pass/visualize_tree.hpp>
|
||||
|
||||
#include "pyopenvino/utils/utils.hpp"
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
void regclass_transformations(py::module m) {
|
||||
py::class_<ov::pass::Serialize, std::shared_ptr<ov::pass::Serialize>, ov::pass::ModelPass, ov::pass::PassBase>
|
||||
serialize(m, "Serialize");
|
||||
serialize.doc() = "openvino.runtime.passes.Serialize transformation";
|
||||
serialize.def(py::init([](const std::string& path_to_xml, const std::string& path_to_bin) {
|
||||
return std::make_shared<ov::pass::Serialize>(path_to_xml, path_to_bin);
|
||||
serialize.def(py::init([](const py::object& path_to_xml, const py::object& path_to_bin) {
|
||||
return std::make_shared<ov::pass::Serialize>(Common::utils::convert_path_to_string(path_to_xml),
|
||||
Common::utils::convert_path_to_string(path_to_bin));
|
||||
}),
|
||||
py::arg("path_to_xml"),
|
||||
py::arg("path_to_bin"),
|
||||
@ -30,16 +35,18 @@ void regclass_transformations(py::module m) {
|
||||
Create Serialize pass which is used for Model to IR serialization.
|
||||
|
||||
:param path_to_xml: Path where *.xml file will be saved.
|
||||
:type path_to_xml: str
|
||||
:type path_to_xml: Union[str, bytes, pathlib.Path]
|
||||
|
||||
:param path_to_xml: Path where *.bin file will be saved.
|
||||
:type path_to_xml: str
|
||||
:type path_to_xml: Union[str, bytes, pathlib.Path]
|
||||
)");
|
||||
|
||||
serialize.def(
|
||||
py::init(
|
||||
[](const std::string& path_to_xml, const std::string& path_to_bin, ov::pass::Serialize::Version version) {
|
||||
return std::make_shared<ov::pass::Serialize>(path_to_xml, path_to_bin, version);
|
||||
[](const py::object& path_to_xml, const py::object& path_to_bin, ov::pass::Serialize::Version version) {
|
||||
return std::make_shared<ov::pass::Serialize>(Common::utils::convert_path_to_string(path_to_xml),
|
||||
Common::utils::convert_path_to_string(path_to_bin),
|
||||
version);
|
||||
}),
|
||||
py::arg("path_to_xml"),
|
||||
py::arg("path_to_bin"),
|
||||
@ -48,10 +55,10 @@ void regclass_transformations(py::module m) {
|
||||
Create Serialize pass which is used for Model to IR serialization.
|
||||
|
||||
:param path_to_xml: Path where *.xml file will be saved.
|
||||
:type path_to_xml: str
|
||||
:type path_to_xml: Union[str, bytes, pathlib.Path]
|
||||
|
||||
:param path_to_xml: Path where *.bin file will be saved.
|
||||
:type path_to_xml: str
|
||||
:type path_to_xml: Union[str, bytes, pathlib.Path]
|
||||
|
||||
:param version: serialized IR version.
|
||||
:type version: int
|
||||
|
@ -61,6 +61,7 @@
|
||||
#include "pyopenvino/graph/strides.hpp"
|
||||
#include "pyopenvino/graph/types/regmodule_graph_types.hpp"
|
||||
#include "pyopenvino/graph/util.hpp"
|
||||
#include "pyopenvino/utils/utils.hpp"
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
@ -100,14 +101,17 @@ PYBIND11_MODULE(_pyopenvino, m) {
|
||||
m.def(
|
||||
"serialize",
|
||||
[](std::shared_ptr<ov::Model>& model,
|
||||
const std::string& xml_path,
|
||||
const std::string& bin_path,
|
||||
const py::object& xml_path,
|
||||
const py::object& bin_path,
|
||||
const std::string& version) {
|
||||
ov::serialize(model, xml_path, bin_path, Common::convert_to_version(version));
|
||||
ov::serialize(model,
|
||||
Common::utils::convert_path_to_string(xml_path),
|
||||
Common::utils::convert_path_to_string(bin_path),
|
||||
Common::convert_to_version(version));
|
||||
},
|
||||
py::arg("model"),
|
||||
py::arg("xml_path"),
|
||||
py::arg("bin_path") = "",
|
||||
py::arg("bin_path") = py::str(""),
|
||||
py::arg("version") = "UNSPECIFIED",
|
||||
R"(
|
||||
Serialize given model into IR. The generated .xml and .bin files will be saved
|
||||
@ -115,10 +119,10 @@ PYBIND11_MODULE(_pyopenvino, m) {
|
||||
:param model: model which will be converted to IR representation
|
||||
:type model: openvino.runtime.Model
|
||||
:param xml_path: path where .xml file will be saved
|
||||
:type xml_path: str
|
||||
:type xml_path: Union[str, bytes, pathlib.Path]
|
||||
:param bin_path: path where .bin file will be saved (optional),
|
||||
the same name as for xml_path will be used by default.
|
||||
:type bin_path: str
|
||||
:type bin_path: Union[str, bytes, pathlib.Path]
|
||||
:param version: version of the generated IR (optional).
|
||||
Supported versions are:
|
||||
- "UNSPECIFIED" (default) : Use the latest or model version
|
||||
|
@ -130,9 +130,13 @@ std::string convert_path_to_string(const py::object& path) {
|
||||
if (py::isinstance(path, Path) || py::isinstance<py::str>(path)) {
|
||||
return path.str();
|
||||
}
|
||||
// Convert bytes to string
|
||||
if (py::isinstance<py::bytes>(path)) {
|
||||
return path.cast<std::string>();
|
||||
}
|
||||
std::stringstream str;
|
||||
str << "Path: '" << path << "'"
|
||||
<< " does not exist. Please provide valid model's path either as a string or pathlib.Path. "
|
||||
<< " does not exist. Please provide valid model's path either as a string, bytes or pathlib.Path. "
|
||||
"Examples:\n(1) '/home/user/models/model.onnx'\n(2) Path('/home/user/models/model/model.onnx')";
|
||||
throw ov::Exception(str.str());
|
||||
}
|
||||
|
@ -100,7 +100,7 @@ def test_load_wrong_path():
|
||||
assert fe is not None
|
||||
with pytest.raises(RuntimeError) as e:
|
||||
fe.load(TestClass())
|
||||
assert "Path: 'test class' does not exist. Please provide valid model's path either as a string or pathlib.Path" in str(e.value)
|
||||
assert "Path: 'test class' does not exist. Please provide valid model's path either as a string, bytes or pathlib.Path" in str(e.value)
|
||||
|
||||
|
||||
@mock_needed
|
||||
|
@ -66,7 +66,7 @@ def test_compact_api_wrong_path():
|
||||
return "test class"
|
||||
with pytest.raises(RuntimeError) as e:
|
||||
compile_model(TestClass())
|
||||
assert "Path: 'test class' does not exist. Please provide valid model's path either as a string or pathlib.Path" in str(e.value)
|
||||
assert "Path: 'test class' does not exist. Please provide valid model's path either as a string, bytes or pathlib.Path" in str(e.value)
|
||||
|
||||
|
||||
def test_compact_api_onnx():
|
||||
|
@ -594,4 +594,3 @@ def test_serialize_complex_rt_info(request):
|
||||
|
||||
os.remove(xml_path)
|
||||
os.remove(bin_path)
|
||||
serialize(res_model, xml_path, bin_path)
|
||||
|
@ -3,6 +3,7 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import os
|
||||
import pytest
|
||||
import numpy as np
|
||||
from openvino.runtime import serialize
|
||||
from openvino.offline_transformations import (
|
||||
@ -166,9 +167,18 @@ def test_fused_names_cleanup():
|
||||
|
||||
|
||||
# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request
|
||||
def test_serialize_pass_v2(request):
|
||||
@pytest.mark.parametrize("is_path_xml, is_path_bin", [ # noqa: PT006
|
||||
(True, True),
|
||||
(True, False),
|
||||
(False, True),
|
||||
(False, False),
|
||||
],
|
||||
)
|
||||
def test_serialize_pass_v2(request, is_path_xml, is_path_bin):
|
||||
core = Core()
|
||||
xml_path, bin_path = create_filename_for_test(request.node.name)
|
||||
xml_path, bin_path = create_filename_for_test(request.node.name,
|
||||
is_path_xml,
|
||||
is_path_bin)
|
||||
shape = [100, 100, 2]
|
||||
parameter_a = ov.opset8.parameter(shape, dtype=np.float32, name="A")
|
||||
parameter_b = ov.opset8.parameter(shape, dtype=np.float32, name="B")
|
||||
@ -202,9 +212,18 @@ def test_compress_model_transformation():
|
||||
|
||||
|
||||
# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request
|
||||
def test_version_default(request):
|
||||
@pytest.mark.parametrize("is_path_xml, is_path_bin", [ # noqa: PT006
|
||||
(True, True),
|
||||
(True, False),
|
||||
(False, True),
|
||||
(False, False),
|
||||
],
|
||||
)
|
||||
def test_version_default(request, is_path_xml, is_path_bin):
|
||||
core = Core()
|
||||
xml_path, bin_path = create_filename_for_test(request.node.name)
|
||||
xml_path, bin_path = create_filename_for_test(request.node.name,
|
||||
is_path_xml,
|
||||
is_path_bin)
|
||||
shape = [100, 100, 2]
|
||||
parameter_a = ov.opset8.parameter(shape, dtype=np.float32, name="A")
|
||||
parameter_b = ov.opset8.parameter(shape, dtype=np.float32, name="B")
|
||||
@ -222,8 +241,17 @@ def test_version_default(request):
|
||||
|
||||
|
||||
# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request
|
||||
def test_serialize_default_bin(request):
|
||||
xml_path, bin_path = create_filename_for_test(request.node.name)
|
||||
@pytest.mark.parametrize("is_path_xml, is_path_bin", [ # noqa: PT006
|
||||
(True, True),
|
||||
(True, False),
|
||||
(False, True),
|
||||
(False, False),
|
||||
],
|
||||
)
|
||||
def test_serialize_default_bin(request, is_path_xml, is_path_bin):
|
||||
xml_path, bin_path = create_filename_for_test(request.node.name,
|
||||
is_path_xml,
|
||||
is_path_bin)
|
||||
model = get_test_model()
|
||||
serialize(model, xml_path)
|
||||
assert os.path.exists(bin_path)
|
||||
|
@ -104,9 +104,18 @@ def test_low_latency2():
|
||||
|
||||
|
||||
# request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request
|
||||
def test_serialize_pass(request):
|
||||
@pytest.mark.parametrize("is_path_xml, is_path_bin", [ # noqa: PT006
|
||||
(True, True),
|
||||
(True, False),
|
||||
(False, True),
|
||||
(False, False),
|
||||
],
|
||||
)
|
||||
def test_serialize_pass(request, is_path_xml, is_path_bin):
|
||||
core = Core()
|
||||
xml_path, bin_path = create_filename_for_test(request.node.name)
|
||||
xml_path, bin_path = create_filename_for_test(request.node.name,
|
||||
is_path_xml,
|
||||
is_path_bin)
|
||||
|
||||
func = get_test_model()
|
||||
|
||||
|
@ -5,6 +5,7 @@
|
||||
from typing import Tuple, Union, List
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
import numpy as np
|
||||
import openvino
|
||||
import openvino.runtime.opset8 as ops
|
||||
@ -82,7 +83,17 @@ def test_deprecation_decorator():
|
||||
deprecated_function4()
|
||||
|
||||
|
||||
def create_filename_for_test(test_name):
|
||||
def create_filename_for_test(test_name, is_xml_path=False, is_bin_path=False):
|
||||
"""Return a tuple with automatically generated paths for xml and bin files.
|
||||
|
||||
:param test_name: Name used in generating.
|
||||
:param is_xml_path: True if xml file should be pathlib.Path object, otherwise return string.
|
||||
:param is_bin_path: True if bin file should be pathlib.Path object, otherwise return string.
|
||||
:return: Tuple with two objects representing xml and bin files.
|
||||
"""
|
||||
python_version = str(sys.version_info.major) + "_" + str(sys.version_info.minor)
|
||||
filename = "./" + test_name.replace("test_", "") + "_" + python_version
|
||||
return (filename + ".xml", filename + ".bin")
|
||||
filename = "./" + test_name.replace("test_", "").replace("[", "_").replace("]", "_")
|
||||
filename = filename + "_" + python_version
|
||||
_xml = Path(filename + ".xml") if is_xml_path else filename + ".xml"
|
||||
_bin = Path(filename + ".bin") if is_bin_path else filename + ".bin"
|
||||
return (_xml, _bin)
|
||||
|
Loading…
Reference in New Issue
Block a user