Skip to content

Commit

Permalink
[ONNX] Preparing environment for moving ONNX tests to API2.0 (#21528)
Browse files Browse the repository at this point in the history
* Added common functions for onnx tests
* Removed test cases which are non-related to ONNX
Extended convert_model by extension
* Extended test utils by convert_partially
* Added model convertion using istream as input
* Moved common functionality to a ov namespace
* Removed unused function
  • Loading branch information
gkrivor authored Dec 9, 2023
1 parent 04c9a52 commit 8960da9
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 93 deletions.
2 changes: 1 addition & 1 deletion src/frontends/onnx/onnx_common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ target_include_directories(${TARGET_NAME}
$<INSTALL_INTERFACE:${FRONTEND_INSTALL_INCLUDE}>
PRIVATE ${ONNX_COMMON_SRC_DIR})

target_link_libraries(${TARGET_NAME} PRIVATE openvino::runtime)
target_link_libraries(${TARGET_NAME} PRIVATE openvino::runtime openvino::util)

ov_link_system_libraries(${TARGET_NAME} PUBLIC onnx_proto onnx)

Expand Down
23 changes: 14 additions & 9 deletions src/frontends/onnx/onnx_common/include/onnx_common/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,37 @@
//

#pragma once
#include <onnx/onnx_pb.h>

#include <fstream>
#include <string>

/// \ingroup ngraph_cpp_api
namespace ONNX_NAMESPACE {
class ModelProto;
}

namespace ngraph {
namespace ov {
namespace frontend {
namespace onnx_common {
using namespace ::ONNX_NAMESPACE;
/// \brief Parses an ONNX model from a file located on a storage device.
///
/// \param file_path Path to the file containing an ONNX model.
///
/// \return The parsed in-memory representation of the ONNX model
ONNX_NAMESPACE::ModelProto parse_from_file(const std::string& file_path);
ModelProto parse_from_file(const std::string& file_path);
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
ONNX_NAMESPACE::ModelProto parse_from_file(const std::wstring& file_path);
ModelProto parse_from_file(const std::wstring& file_path);
#endif

/// \brief Parses an ONNX model from a stream (representing for example a file)
///
/// \param model_stream Path to the file containing an ONNX model.
///
/// \return The parsed in-memory representation of the ONNX model
ONNX_NAMESPACE::ModelProto parse_from_istream(std::istream& model_stream);
ModelProto parse_from_istream(std::istream& model_stream);
} // namespace onnx_common
} // namespace frontend
} // namespace ov

namespace ngraph {
namespace onnx_common {
using namespace ov::frontend::onnx_common;
}
} // namespace ngraph
15 changes: 7 additions & 8 deletions src/frontends/onnx/onnx_common/src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#include <google/protobuf/text_format.h>
#include <onnx/onnx_pb.h>

#include <ngraph/file_util.hpp>
#include "openvino/core/except.hpp"
#include "openvino/util/file_util.hpp"

#include "ngraph/except.hpp"

namespace ngraph {
namespace ov {
namespace frontend {
namespace onnx_common {
ONNX_NAMESPACE::ModelProto parse_from_file(const std::string& file_path) {
std::ifstream file_stream{file_path.c_str(), std::ios::in | std::ios::binary};
Expand All @@ -31,9 +31,7 @@ ONNX_NAMESPACE::ModelProto parse_from_file(const std::wstring& file_path) {
std::ifstream file_stream{file_path.c_str(), std::ios::in | std::ios::binary};

if (!file_stream.is_open()) {
NGRAPH_SUPPRESS_DEPRECATED_START
OPENVINO_THROW("Could not open the file: " + file_util::wstring_to_string(file_path));
NGRAPH_SUPPRESS_DEPRECATED_END
OPENVINO_THROW("Could not open the file: " + ov::util::wstring_to_string(file_path));
};

auto model_proto = parse_from_istream(file_stream);
Expand All @@ -60,4 +58,5 @@ ONNX_NAMESPACE::ModelProto parse_from_istream(std::istream& model_stream) {
return model_proto;
}
} // namespace onnx_common
} // namespace ngraph
} // namespace frontend
} // namespace ov
11 changes: 8 additions & 3 deletions src/frontends/onnx/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ set(MULTI_TEST_SRC
onnx_import_rnn.in.cpp
onnx_import_signal.in.cpp
onnx_import_quant.in.cpp
onnx_test_utils.in.cpp
onnx_import_with_editor.in.cpp)
set(SRC
conversion.cpp
Expand All @@ -83,6 +82,7 @@ set(SRC
onnx_importer_test.cpp
onnx_tensor_names.cpp
onnx_test_util.cpp
onnx_utils.cpp
onnx_transformations.cpp
op_extension.cpp
telemetry.cpp
Expand Down Expand Up @@ -142,8 +142,13 @@ if(ONNX_TESTS_DEPENDENCIES)
add_dependencies(ov_onnx_frontend_tests ${ONNX_TESTS_DEPENDENCIES})
endif()

target_link_libraries(ov_onnx_frontend_tests PRIVATE gtest_main_manifest openvino::runtime::dev
openvino_onnx_frontend openvino_onnx_common func_test_utils)
target_link_libraries(ov_onnx_frontend_tests PRIVATE
gtest_main_manifest
frontend_shared_test_classes
openvino::runtime::dev
openvino_onnx_frontend
openvino_onnx_common
func_test_utils)

# It's needed by onnx_import_library.cpp and onnx_import_exceptions.cpp tests to include onnx_pb.h.
# Not linking statically to libprotobuf (linked into libonnx) avoids false-failing onnx_editor tests.
Expand Down
12 changes: 8 additions & 4 deletions src/frontends/onnx/tests/onnx_test_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,10 @@ ComparisonResult compare_onnx_graphs(const ONNX_NAMESPACE::GraphProto& graph,
return compare_nodes(graph, ref_graph, comp);
}
} // namespace
namespace ngraph {
namespace test {
namespace ov {
namespace frontend {
namespace onnx {
namespace tests {

bool default_name_comparator(std::string lhs, std::string rhs) {
return lhs == rhs;
Expand Down Expand Up @@ -220,5 +222,7 @@ std::string change_opset_version(const std::string& model,
return model_proto.SerializeAsString();
}

} // namespace test
} // namespace ngraph
} // namespace tests
} // namespace onnx
} // namespace frontend
} // namespace ov
14 changes: 12 additions & 2 deletions src/frontends/onnx/tests/onnx_test_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
#include <string>
#include <vector>

namespace ngraph {
namespace test {
namespace ov {
namespace frontend {
namespace onnx {
namespace tests {
struct ComparisonResult {
ComparisonResult() = default;
ComparisonResult(std::string error) : is_ok{false}, error_message{std::move(error)} {}
Expand Down Expand Up @@ -40,5 +42,13 @@ ComparisonResult compare_onnx_models(const std::string& model,
std::string change_opset_version(const std::string& model,
const std::vector<int64_t>& new_opset_version,
const std::string& domain = "ai.onnx");
} // namespace tests
} // namespace onnx
} // namespace frontend
} // namespace ov

namespace ngraph {
namespace test {
using namespace ov::frontend::onnx::tests;
} // namespace test
} // namespace ngraph
58 changes: 0 additions & 58 deletions src/frontends/onnx/tests/onnx_test_utils.in.cpp

This file was deleted.

99 changes: 99 additions & 0 deletions src/frontends/onnx/tests/onnx_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "onnx_utils.hpp"

#include <openvino/runtime/core.hpp>
#include <vector>

#include "utils.hpp"

using namespace std;
using namespace ov;
using namespace ov::frontend;

// For compatibility purposes, need to remove when will be unused
const std::string ONNX_FE = "onnx";

namespace ov {
namespace frontend {
namespace onnx {
namespace tests {

const std::string ONNX_FE = ::ONNX_FE;

shared_ptr<Model> convert_model(const string& model_path, const ov::frontend::ConversionExtensionBase::Ptr& conv_ext) {
auto fem = FrontEndManager();
FrontEnd::Ptr front_end = fem.load_by_framework(ONNX_FE);
if (!front_end) {
throw "ONNX FrontEnd is not initialized";
}

if (conv_ext) {
front_end->add_extension(conv_ext);
}

auto full_path = FrontEndTestUtils::make_model_path(string(TEST_ONNX_MODELS_DIRNAME) + model_path);
InputModel::Ptr input_model = front_end->load(full_path);
if (!input_model) {
throw "Input Model is not loaded";
}

shared_ptr<Model> model = front_end->convert(input_model);
if (!model) {
throw "Model is not converted";
}

return model;
}

shared_ptr<Model> convert_model(ifstream& model_stream) {
auto fem = FrontEndManager();
FrontEnd::Ptr front_end = fem.load_by_framework(ONNX_FE);
if (!front_end) {
throw "ONNX FrontEnd is not initialized";
}

InputModel::Ptr input_model = front_end->load(dynamic_cast<istream*>(&model_stream));
if (!input_model) {
throw "Input Model is not loaded";
}

shared_ptr<Model> model = front_end->convert(input_model);
if (!model) {
throw "Model is not converted";
}

return model;
}

shared_ptr<Model> convert_partially(const string& model_path) {
auto fem = FrontEndManager();
FrontEnd::Ptr front_end = fem.load_by_framework(ONNX_FE);
if (!front_end) {
throw "ONNX FrontEnd is not initialized";
}

auto full_path = FrontEndTestUtils::make_model_path(string(TEST_ONNX_MODELS_DIRNAME) + model_path);
InputModel::Ptr input_model = front_end->load(full_path);
if (!input_model) {
throw "Input Model is not loaded";
}

shared_ptr<Model> model = front_end->convert_partially(input_model);
if (!model) {
throw "Model is not converted";
}

return model;
}

std::string onnx_backend_manifest(const std::string& manifest) {
return ov::util::path_join({ov::test::utils::getExecutableDirectory(), manifest});
}

} // namespace tests
} // namespace onnx
} // namespace frontend
} // namespace ov
39 changes: 31 additions & 8 deletions src/frontends/onnx/tests/onnx_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,47 @@

#pragma once

#include <stdlib.h>

#include <openvino/core/model.hpp>
#include <openvino/frontend/extension.hpp>
#include <string>

#include "openvino/runtime/core.hpp"
#include "common_test_utils/test_constants.hpp"

static const std::string ONNX_FE = "onnx";

// Resolves different backend names to an internal device enumeration
inline std::string backend_name_to_device(const std::string& backend_name) {
if (backend_name == "INTERPRETER")
return ov::test::utils::DEVICE_TEMPLATE;
if (backend_name == "IE_CPU")
return ov::test::utils::DEVICE_CPU;
if (backend_name == "IE_GPU")
return ov::test::utils::DEVICE_GPU;
OPENVINO_THROW("Unsupported backend name");
throw "Unsupported backend name";
}

inline std::shared_ptr<ov::Model> function_from_ir(const std::string& xml_path, const std::string& bin_path = {}) {
ov::Core c;
return c.read_model(xml_path, bin_path);
}
namespace ov {
namespace frontend {
namespace onnx {
namespace tests {

extern const std::string ONNX_FE;

// A wrapper to create ONNX Frontend and configure the conversion pipeline
std::shared_ptr<ov::Model> convert_model(const std::string& model_path,
const ov::frontend::ConversionExtensionBase::Ptr& conv_ext = nullptr);
// A wrapper to create ONNX Frontend and configure the conversion pipeline
std::shared_ptr<ov::Model> convert_model(std::ifstream& model_stream);
// A wrapper to create ONNX Frontend and configure the conversion pipeline to get
// a model with possible Framework Nodes
std::shared_ptr<ov::Model> convert_partially(const std::string& model_path);

// Returns path to a manifest file
std::string onnx_backend_manifest(const std::string& manifest);
} // namespace tests
} // namespace onnx
} // namespace frontend
} // namespace ov

// For compatibility purposes, need to remove when will be unused
extern const std::string ONNX_FE;

0 comments on commit 8960da9

Please sign in to comment.