[MO] Remove use of mapping file and its generation (#16944)

* [MO] Remove use of mapping file and its generation

Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>

* Fix pylinter findings

* Remove usage of mapping file in the layer tests

* Fixing layer tests for legacy frontend

---------

Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
This commit is contained in:
Roman Kazantsev 2023-04-15 14:38:33 +04:00 committed by GitHub
parent 507b3251ef
commit 18da874c57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 10 additions and 145 deletions

View File

@ -18,7 +18,6 @@ from openvino._pyopenvino._offline_transformations import apply_moc_legacy_trans
from openvino._pyopenvino._offline_transformations import apply_pot_transformations
from openvino._pyopenvino._offline_transformations import apply_low_latency_transformation
from openvino._pyopenvino._offline_transformations import apply_pruning_transformation
from openvino._pyopenvino._offline_transformations import generate_mapping_file
from openvino._pyopenvino._offline_transformations import apply_make_stateful_transformation
from openvino._pyopenvino._offline_transformations import compress_model_transformation
from openvino._pyopenvino._offline_transformations import compress_quantize_weights_transformation

View File

@ -64,11 +64,6 @@ def apply_pruning_transformation(model):
_base.apply_pruning_transformation(model)
@deprecated(version="2023.1", message="The module is private and following namespace " "`offline_transformations` will be removed in " "the future.")
def generate_mapping_file(model, path, extract_names):
_base.generate_mapping_file(model, path, extract_names)
@deprecated(version="2023.1", message="The module is private and following namespace " "`offline_transformations` will be removed in " "the future.")
def apply_make_stateful_transformation(model, param_res_names):
_base.apply_make_stateful_transformation(model, param_res_names)

View File

@ -7,7 +7,6 @@
#include <pybind11/stl.h>
#include <compress_quantize_weights.hpp>
#include <generate_mapping_file.hpp>
#include <openvino/pass/make_stateful.hpp>
#include <openvino/pass/serialize.hpp>
#include <pot_transformations.hpp>
@ -85,17 +84,6 @@ void regmodule_offline_transformations(py::module m) {
},
py::arg("model"));
m_offline_transformations.def(
"generate_mapping_file",
[](std::shared_ptr<ov::Model> model, std::string path, bool extract_names) {
ov::pass::Manager manager;
manager.register_pass<ngraph::pass::GenerateMappingFile>(path, extract_names);
manager.run_passes(model);
},
py::arg("model"),
py::arg("path"),
py::arg("extract_names"));
m_offline_transformations.def(
"apply_make_stateful_transformation",
[](std::shared_ptr<ov::Model> model, const std::map<std::string, std::string>& param_res_names) {

View File

@ -99,7 +99,7 @@ def _check_dict(result, obj, output_names=None):
assert _check_keys(result.keys(), outs)
assert _check_values(result)
assert _check_items(result, outs, output_names)
assert all([output_names[i] in result.names()[i] for i in range(0, len(output_names))])
assert all(output_names[i] in result.names()[i] for i in range(0, len(output_names)))
return True

View File

@ -39,7 +39,7 @@ def test_init_with_ngraph(ov_type, numpy_dtype):
ov_tensors = []
ov_tensors.append(Tensor(type=ov_type, shape=ov.Shape([1, 3, 32, 32])))
ov_tensors.append(Tensor(type=ov_type, shape=[1, 3, 32, 32]))
assert np.all([list(ov_tensor.shape) == [1, 3, 32, 32] for ov_tensor in ov_tensors])
assert np.all(list(ov_tensor.shape) == [1, 3, 32, 32] for ov_tensor in ov_tensors)
assert np.all(ov_tensor.element_type == ov_type for ov_tensor in ov_tensors)
assert np.all(ov_tensor.data.dtype == numpy_dtype for ov_tensor in ov_tensors)
assert np.all(ov_tensor.data.shape == (1, 3, 32, 32) for ov_tensor in ov_tensors)

View File

@ -22,7 +22,7 @@ add_library(${TARGET_NAME} STATIC ${LIBRARY_SRC} ${PUBLIC_HEADERS})
target_link_libraries(${TARGET_NAME} PRIVATE openvino::runtime::dev openvino::itt openvino::pugixml ngraph::reference
openvino::runtime)
set_source_files_properties(src/generate_mapping_file.cpp INCLUDE_DIRECTORIES
set_source_files_properties(INCLUDE_DIRECTORIES
$<TARGET_PROPERTY:openvino::pugixml,INTERFACE_INCLUDE_DIRECTORIES>)
target_include_directories(${TARGET_NAME} PUBLIC ${PUBLIC_HEADERS_DIR}

View File

@ -1,32 +0,0 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <memory>
#include <ngraph/pass/graph_rewrite.hpp>
namespace ngraph {
namespace pass {
class GenerateMappingFile;
} // namespace pass
} // namespace ngraph
/**
* @brief Generate mapping file based on output tensor names.
*/
class ngraph::pass::GenerateMappingFile : public ngraph::pass::FunctionPass {
std::string m_path_to_file;
bool m_extract_name;
public:
OPENVINO_RTTI("GenerateMappingFile", "0");
explicit GenerateMappingFile(const std::string& path, bool extract_name = true)
: m_path_to_file(path),
m_extract_name(extract_name) {}
bool run_on_model(const std::shared_ptr<ngraph::Function>&) override;
};

View File

@ -1,62 +0,0 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "generate_mapping_file.hpp"
#include <fstream>
#include <memory>
#include <ostream>
#include "pugixml.hpp"
bool ngraph::pass::GenerateMappingFile::run_on_model(const std::shared_ptr<ngraph::Function>& f) {
pugi::xml_document xml_doc;
pugi::xml_node root_node = xml_doc.append_child("mapping");
auto add_mapping = [&](const std::string& fw_name,
const std::string& fw_port_name,
const std::string& ir_name,
const std::string& ir_port_name) {
auto map_node = root_node.append_child("map");
auto framework_node = map_node.append_child("framework");
auto ir_node = map_node.append_child("IR");
framework_node.append_attribute("name").set_value(fw_name.c_str());
framework_node.append_attribute("output_port_id").set_value(fw_port_name.c_str());
ir_node.append_attribute("name").set_value(ir_name.c_str());
ir_node.append_attribute("output_port_id").set_value(ir_port_name.c_str());
};
auto extract_name = [](const std::string& port_name) -> std::string {
return port_name.substr(0, port_name.find(':'));
};
for (auto&& node : f->get_ordered_ops()) {
uint64_t ie_port_index{node->inputs().size()};
if (std::dynamic_pointer_cast<ov::op::v0::Result>(node))
continue;
for (auto&& output : node->outputs()) {
const auto& node_name = node->get_friendly_name();
const auto& t = output.get_tensor_ptr();
for (const auto& port_name : t->get_names()) {
add_mapping(node_name, port_name, node_name, std::to_string(ie_port_index));
if (m_extract_name) {
for (auto& name : t->get_names()) {
add_mapping(extract_name(name), port_name, node_name, std::to_string(ie_port_index));
}
}
}
++ie_port_index;
}
}
// save mapping file
std::ofstream mapping_file(m_path_to_file, std::ios::out);
xml_doc.save(mapping_file);
mapping_file.flush();
return false;
}

View File

@ -108,13 +108,6 @@ class CommonLayerTest:
# Framework infer:
fw_res = self.get_framework_results(inputs_dict=inputs_dict, model_path=model_path)
if len(fw_res) == len(infer_res) == 1:
# match output layers directly
mapping_dict = {next(iter(fw_res)): next(iter(infer_res))}
else:
# Load mapping file
mapping_dict = mapping_parser(path_to_xml.with_suffix('.mapping'))
if 'custom_eps' in kwargs and kwargs['custom_eps'] is not None:
custom_eps = kwargs['custom_eps']
else:
@ -124,7 +117,6 @@ class CommonLayerTest:
custom_eps = 5e-2
# Compare Ie results with Framework results
assert self.compare_ie_results_with_framework(infer_res=infer_res, framework_res=fw_res,
mapping_dict=mapping_dict,
framework_eps=custom_eps), \
"Comparing with Framework failed: ie_res={}; framework_res={}.".format(infer_res,
fw_res)
@ -160,17 +152,10 @@ class CommonLayerTest:
inputs_dict[input] = np.random.randint(-255, 255, inputs_dict[input]).astype(np.float32)
return inputs_dict
def compare_ie_results_with_framework(self, infer_res, framework_res, mapping_dict,
framework_eps):
def compare_ie_results_with_framework(self, infer_res, framework_res, framework_eps):
is_ok = True
from common.utils.common_utils import allclose
for framework_out_name in framework_res:
if framework_out_name not in list(infer_res.keys()):
if framework_out_name not in mapping_dict:
raise RuntimeError("Output {} not found in mapping file!".format(framework_out_name))
ie_out_name = mapping_dict[framework_out_name]
else:
ie_out_name = framework_out_name
if not allclose(infer_res[ie_out_name], framework_res[framework_out_name],

View File

@ -102,10 +102,9 @@ class InferAPI20(BaseInfer):
for tensor_name in out_obj.get_names():
result[tensor_name] = out_tensor
else:
# do not change behaviour for mapping tensor names
# between the original framework and OpenVINO
# because it leads to fixing this functionality in the legacy frontend
tensor_name = out_obj.get_any_name().split(':')[0]
for tensor_name in out_obj.get_names():
result[tensor_name] = out_tensor
tensor_name = tensor_name.split(':')[0]
result[tensor_name] = out_tensor
if "exec_net" in locals():

View File

@ -23,7 +23,6 @@ from openvino.tools.mo.utils.guess_framework import deduce_legacy_frontend_by_na
# pylint: disable=no-name-in-module,import-error
from openvino.frontend import FrontEndManager
from openvino._offline_transformations import generate_mapping_file
from openvino.runtime import serialize
@ -83,11 +82,6 @@ def main(cli_parser: argparse.ArgumentParser, framework=None):
serialize(ngraph_function, model_path.encode('utf-8'), model_path.replace('.xml', '.bin').encode('utf-8'))
# generate .mapping file
path_to_mapping = model_path_no_ext + ".mapping"
extract_names = argv.framework in ['tf', 'mxnet', 'kaldi']
generate_mapping_file(ngraph_function, path_to_mapping, extract_names)
print('[ SUCCESS ] Generated IR version {} model.'.format(get_ir_version(argv)))
print('[ SUCCESS ] XML file: {}'.format(model_path))
print('[ SUCCESS ] BIN file: {}'.format(model_path.replace('.xml', '.bin')))

View File

@ -50,8 +50,7 @@ def import_core_modules(silent: bool, path_to_module: str):
try:
from openvino._offline_transformations import apply_moc_transformations, apply_moc_legacy_transformations,\
apply_low_latency_transformation, apply_fused_names_cleanup # pylint: disable=import-error,no-name-in-module
from openvino._offline_transformations import apply_make_stateful_transformation, generate_mapping_file # pylint: disable=import-error,no-name-in-module
from openvino._offline_transformations import generate_mapping_file, apply_make_stateful_transformation # pylint: disable=import-error,no-name-in-module
from openvino._offline_transformations import apply_make_stateful_transformation # pylint: disable=import-error,no-name-in-module
from openvino.runtime import Model, serialize, get_version # pylint: disable=import-error,no-name-in-module
from openvino.runtime.op import Parameter # pylint: disable=import-error,no-name-in-module