Reimplement Extension.OnnxModelWith* cases w/o model in source code (#8112)

Ticket: 62567
This commit is contained in:
Mateusz Tabaka 2021-11-03 12:43:44 +01:00 committed by GitHub
parent 4e4e179d67
commit 3fc210af21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 196 additions and 188 deletions

View File

@ -86,6 +86,11 @@ ov_model_convert("${OpenVINO_SOURCE_DIR}/${rel_path}"
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_model_zoo/onnx_import"
ie_onnx_import_out_files)
set(rel_path "docs/onnx_custom_op")
ov_model_convert("${OpenVINO_SOURCE_DIR}/${rel_path}"
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_model_zoo/docs/models"
docs_onnx_out_files)
if(ENABLE_TESTS)
if(NGRAPH_ONNX_FRONTEND_ENABLE AND ENABLE_REQUIREMENTS_INSTALL)
find_package(PythonInterp 3 REQUIRED)
@ -124,7 +129,8 @@ if(ENABLE_TESTS)
${ft_out_files}
${ie_onnx_out_files}
${ie_serialize_out_files}
${ie_onnx_import_out_files})
${ie_onnx_import_out_files}
${docs_onnx_out_files})
if(TARGET test_pip_prerequsites)
add_dependencies(test_model_zoo test_pip_prerequsites)

View File

@ -26,7 +26,18 @@ New operator registration must happen before an ONNX model is read. For example,
Reregistering ONNX operators within the same process is supported. If you register an existing operator, you get a warning.
The example below demonstrates an exemplary model that requires a previously created `CustomRelu` operator:
@snippet onnx_custom_op/onnx_custom_op.cpp onnx_custom_op:model
@include onnx_custom_op/custom_relu_model.prototxt
This model is in text format, so before it can be passed to Inference Engine, it has to be converted to binary using:
```py
from google.protobuf import text_format
import onnx
with open("custom_relu_model.prototxt") as in_file:
proto = onnx.ModelProto()
text_format.Parse(in_file.read(), proto, allow_field_number=True)
s = onnx._serialize(proto)
onnx._save_bytes(s, "custom_relu_model.onnx")
```
To create a graph with nGraph operations, visit [Custom nGraph Operations](AddingNGraphOps.md).

View File

@ -0,0 +1,52 @@
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "in"
output: "out"
name: "customrelu"
op_type: "CustomRelu"
domain: "com.example"
attribute {
name: "alpha"
type: FLOAT
f: 2
}
attribute {
name: "beta"
type: FLOAT
f: 3
}
}
name: "custom relu graph"
input {
name: "in"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 8
}
}
}
}
}
output {
name: "out"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 8
}
}
}
}
}
}
opset_import {
domain: "com.example"
version: 1
}

View File

@ -9,68 +9,6 @@
#include <ngraph/opsets/opset5.hpp>
//! [onnx_custom_op:headers]
std::string custom_relu_model() {
return
//! [onnx_custom_op:model]
R"ONNX(
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "in"
output: "out"
name: "customrelu"
op_type: "CustomRelu"
domain: "com.example"
attribute {
name: "alpha"
type: FLOAT
f: 2
}
attribute {
name: "beta"
type: FLOAT
f: 3
}
}
name: "custom relu graph"
input {
name: "in"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 8
}
}
}
}
}
output {
name: "out"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 8
}
}
}
}
}
}
opset_import {
domain: "com.example"
version: 1
}
)ONNX";
//! [onnx_custom_op:model]
}
void register_custom_relu_operator() {
// CustomRelu is defined as follows:
// x >= 0 => f(x) = x * alpha

View File

@ -4,8 +4,5 @@
#pragma once
#include <string>
std::string custom_relu_model();
void register_custom_relu_operator();
void unregister_custom_relu_operator();

View File

@ -15,6 +15,7 @@ if (NGRAPH_ONNX_FRONTEND_ENABLE)
list(APPEND INCLUDES "${OpenVINO_SOURCE_DIR}/docs/onnx_custom_op")
list(APPEND LINK_LIBRARIES onnx_custom_op)
list(APPEND DEPENDENCIES template_extension onnx_custom_op)
list(APPEND DEFINES TEST_MODELS="${TEST_MODEL_ZOO}")
else()
set(EXCLUDED_SOURCE_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/extension ${CMAKE_CURRENT_SOURCE_DIR}/onnx)
endif()
@ -24,6 +25,7 @@ addIeTargetTest(
ROOT ${CMAKE_CURRENT_SOURCE_DIR}
INCLUDES ${INCLUDES}
EXCLUDED_SOURCE_PATHS ${EXCLUDED_SOURCE_PATHS}
DEFINES ${DEFINES}
DEPENDENCIES ${DEPENDENCIES}
LINK_LIBRARIES ${LINK_LIBRARIES}
ADD_CPPLINT

View File

@ -119,10 +119,8 @@ public:
}
};
void infer_model(InferenceEngine::Core& ie, const std::string& model,
static void infer_model(InferenceEngine::Core& ie, InferenceEngine::CNNNetwork& network,
const std::vector<float>& input_values, const std::vector<float>& expected) {
InferenceEngine::Blob::CPtr weights;
auto network = ie.ReadNetwork(model, weights);
auto function = network.getFunction();
auto network_inputs = network.getInputsInfo();
@ -150,53 +148,11 @@ void infer_model(InferenceEngine::Core& ie, const std::string& model,
ASSERT_EQ(expected, computed_values);
}
static std::string model_full_path(const char* path) {
return FileUtils::makePath<char>(TEST_MODELS, path);
}
TEST(Extension, OnnxModelWithCustomAbs) {
std::string model = R"V0G0N(
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "A"
output: "Y"
name: "customrelu"
op_type: "CustomAbs"
domain: "custom_domain"
}
name: "test_graph"
input {
name: "A"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 10
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 10
}
}
}
}
}
}
opset_import {
version: 1
domain: "custom_domain"
}
)V0G0N";
std::vector<float> input_values{1, -2, 3, -4, 5, -6, 7, -8, 9, -10};
std::vector<float> expected{1, 4, 3, 8, 5, 12, 7, 16, 9, 20};
InferenceEngine::Core ie;
@ -207,7 +163,8 @@ opset_import {
return {std::make_shared<CustomAbs>(ng_inputs.at(0))};
});
infer_model(ie, model, input_values, expected);
auto network = ie.ReadNetwork(model_full_path("func_tests/models/custom_abs_op.onnx"));
infer_model(ie, network, input_values, expected);
ngraph::onnx_import::unregister_operator(CustomAbs::get_type_info_static().name, 1, "custom_domain");
}
@ -255,7 +212,9 @@ TEST(Extension, XmlModelWithCustomAbs) {
std::vector<float> expected{1, 4, 3, 8, 5, 12, 7, 16, 9, 20};
InferenceEngine::Core ie;
ie.AddExtension(std::make_shared<CustomAbsExtension>());
infer_model(ie, model, input_values, expected);
InferenceEngine::Blob::CPtr weights;
auto network = ie.ReadNetwork(model, weights);
infer_model(ie, network, input_values, expected);
}
@ -320,84 +279,19 @@ TEST(Extension, XmlModelWithExtensionFromDSO) {
std::vector<float> expected{12, 13, 14, 15, 16, 17, 18, 19};
InferenceEngine::Core ie;
ie.AddExtension(std::make_shared<InferenceEngine::Extension>(get_extension_path()));
infer_model(ie, model, input_values, expected);
InferenceEngine::Blob::CPtr weights;
auto network = ie.ReadNetwork(model, weights);
infer_model(ie, network, input_values, expected);
}
TEST(Extension, OnnxModelWithExtensionFromDSO) {
std::string model = R"V0G0N(
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "A"
output: "Y"
name: "operation"
op_type: "Template"
domain: "custom_domain"
attribute {
name: "add"
type: INT
i: 11
}
}
name: "test_graph"
input {
name: "A"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 2
}
dim {
dim_value: 2
}
dim {
dim_value: 1
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 2
}
dim {
dim_value: 2
}
dim {
dim_value: 1
}
}
}
}
}
}
opset_import {
version: 1
domain: "com.example"
}
)V0G0N";
std::vector<float> input_values{1, 2, 3, 4, 5, 6, 7, 8};
std::vector<float> expected{12, 13, 14, 15, 16, 17, 18, 19};
InferenceEngine::Core ie;
ie.AddExtension(std::make_shared<InferenceEngine::Extension>(get_extension_path()));
infer_model(ie, model, input_values, expected);
auto network = ie.ReadNetwork(model_full_path("func_tests/models/custom_template_op.onnx"));
infer_model(ie, network, input_values, expected);
}
@ -407,6 +301,7 @@ TEST(Extension, OnnxModelWithCustomReluDocsExample) {
register_custom_relu_operator();
InferenceEngine::Core ie;
infer_model(ie, custom_relu_model(), input_values, expected);
auto network = ie.ReadNetwork(model_full_path("docs/models/custom_relu_model.onnx"));
infer_model(ie, network, input_values, expected);
unregister_custom_relu_operator();
}

View File

@ -0,0 +1,42 @@
ir_version: 3
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "A"
output: "Y"
name: "customrelu"
op_type: "CustomAbs"
domain: "custom_domain"
}
name: "test_graph"
input {
name: "A"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 10
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 10
}
}
}
}
}
}
opset_import {
version: 1
domain: "custom_domain"
}

View File

@ -0,0 +1,65 @@
ir_version: 7
producer_name: "nGraph ONNX Importer"
graph {
node {
input: "A"
output: "Y"
name: "operation"
op_type: "Template"
domain: "custom_domain"
attribute {
name: "add"
type: INT
i: 11
}
}
name: "test_graph"
input {
name: "A"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 2
}
dim {
dim_value: 2
}
dim {
dim_value: 1
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
dim {
dim_value: 2
}
dim {
dim_value: 2
}
dim {
dim_value: 1
}
}
}
}
}
}
opset_import {
version: 1
domain: "com.example"
}