Skip to content

Commit

Permalink
add serialize to offline_transformations (openvinotoolkit#8765)
Browse files Browse the repository at this point in the history
* add serialize to offline_transformations

* fix namespaces, add tests

* fix python style
  • Loading branch information
bszmelcz authored Nov 25, 2021
1 parent 66b75e6 commit cf813f6
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
from openvino.pyopenvino.offline_transformations_pybind import apply_pruning_transformation
from openvino.pyopenvino.offline_transformations_pybind import generate_mapping_file
from openvino.pyopenvino.offline_transformations_pybind import apply_make_stateful_transformation
from openvino.pyopenvino.offline_transformations_pybind import serialize
from openvino.pyopenvino.offline_transformations_pybind import compress_model_transformation
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
#include <openvino/pass/make_stateful.hpp>
#include <pot_transformations.hpp>
#include <pruning.hpp>
#include <transformations/common_optimizations/compress_float_constants.hpp>
#include <transformations/common_optimizations/mark_precision_sensitive_subgraphs.hpp>
#include <transformations/common_optimizations/moc_transformations.hpp>
#include <transformations/serialize.hpp>

#include "openvino/pass/low_latency.hpp"
#include "openvino/pass/manager.hpp"
Expand Down Expand Up @@ -81,4 +84,26 @@ void regmodule_offline_transformations(py::module m) {
},
py::arg("function"),
py::arg("param_res_names"));

m_offline_transformations.def(
"compress_model_transformation",
[](std::shared_ptr<ov::Function> function) {
ov::pass::Manager manager;
manager.register_pass<ov::pass::MarkPrecisionSensitiveSubgraphs>();
manager.register_pass<ov::pass::CompressFloatConstants>();
manager.run_passes(function);
},
py::arg("function"));

// todo: remove as serialize as part of passManager api will be merged
m_offline_transformations.def(
"serialize",
[](std::shared_ptr<ov::Function> function, const std::string& path_to_xml, const std::string& path_to_bin) {
ov::pass::Manager manager;
manager.register_pass<ov::pass::Serialize>(path_to_xml, path_to_bin);
manager.run_passes(function);
},
py::arg("function"),
py::arg("model_path"),
py::arg("weights_path"));
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Copyright (C) 2018-2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import os
import numpy as np
# TODO: change the module name according to the description in 69196
from openvino.offline_transformations_pybind import apply_moc_transformations, apply_pot_transformations, \
apply_low_latency_transformation, apply_pruning_transformation, apply_make_stateful_transformation
apply_low_latency_transformation, apply_pruning_transformation, apply_make_stateful_transformation, \
compress_model_transformation, serialize

from openvino import Function, PartialShape
from openvino import Function, PartialShape, Core
import openvino as ov


Expand Down Expand Up @@ -60,3 +63,57 @@ def test_make_stateful_transformations():
assert function is not None
assert len(function.get_parameters()) == 0
assert len(function.get_results()) == 0


def test_serialize_pass():
core = Core()
xml_path = "serialized_function.xml"
bin_path = "serialized_function.bin"

func = get_test_function()

serialize(func, xml_path, bin_path)

assert func is not None

res_func = core.read_model(model=xml_path, weights=bin_path)

assert func.get_parameters() == res_func.get_parameters()
assert func.get_ordered_ops() == res_func.get_ordered_ops()

os.remove(xml_path)
os.remove(bin_path)


def test_serialize_pass_v2():
core = Core()
xml_path = "./serialized_function.xml"
bin_path = "./serialized_function.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")
model = ov.opset8.floor(ov.opset8.minimum(ov.opset8.abs(parameter_a), parameter_b))
func = Function(model, [parameter_a, parameter_b], "Function")

serialize(func, xml_path, bin_path)

assert func is not None

res_func = core.read_model(model=xml_path, weights=bin_path)

assert func.get_parameters() == res_func.get_parameters()
assert func.get_ordered_ops() == res_func.get_ordered_ops()

os.remove(xml_path)
os.remove(bin_path)


def test_compress_model_transformation():
node_constant = ov.opset8.constant(np.array([[0.0, 0.1, -0.1], [-2.5, 2.5, 3.0]], dtype=np.float32))
node_ceil = ov.opset8.ceiling(node_constant)
func = Function(node_ceil, [], "TestFunction")
assert func.get_ordered_ops()[0].get_element_type().get_type_name() == "f32"
compress_model_transformation(func)

assert func is not None
assert func.get_ordered_ops()[0].get_element_type().get_type_name() == "f16"

0 comments on commit cf813f6

Please sign in to comment.