From 3fc210af210302a445816c0a2de8b4175aa60f0b Mon Sep 17 00:00:00 2001 From: Mateusz Tabaka Date: Wed, 3 Nov 2021 12:43:44 +0100 Subject: [PATCH] Reimplement Extension.OnnxModelWith* cases w/o model in source code (#8112) Ticket: 62567 --- cmake/test_model_zoo.cmake | 8 +- .../IE_DG/Extensibility_DG/Custom_ONNX_Ops.md | 13 +- .../onnx_custom_op/custom_relu_model.prototxt | 52 +++++++ docs/onnx_custom_op/onnx_custom_op.cpp | 62 -------- docs/onnx_custom_op/onnx_custom_op.hpp | 3 - .../functional/plugin/cpu/CMakeLists.txt | 2 + .../plugin/cpu/extension/extension.cpp | 137 ++---------------- .../shared/models/custom_abs_op.prototxt | 42 ++++++ .../shared/models/custom_template_op.prototxt | 65 +++++++++ 9 files changed, 196 insertions(+), 188 deletions(-) create mode 100644 docs/onnx_custom_op/custom_relu_model.prototxt create mode 100644 inference-engine/tests/functional/plugin/shared/models/custom_abs_op.prototxt create mode 100644 inference-engine/tests/functional/plugin/shared/models/custom_template_op.prototxt diff --git a/cmake/test_model_zoo.cmake b/cmake/test_model_zoo.cmake index 64ec7a6909d..9d974e8a927 100644 --- a/cmake/test_model_zoo.cmake +++ b/cmake/test_model_zoo.cmake @@ -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) diff --git a/docs/IE_DG/Extensibility_DG/Custom_ONNX_Ops.md b/docs/IE_DG/Extensibility_DG/Custom_ONNX_Ops.md index dd554320241..5875995e21e 100644 --- a/docs/IE_DG/Extensibility_DG/Custom_ONNX_Ops.md +++ b/docs/IE_DG/Extensibility_DG/Custom_ONNX_Ops.md @@ -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). diff --git a/docs/onnx_custom_op/custom_relu_model.prototxt b/docs/onnx_custom_op/custom_relu_model.prototxt new file mode 100644 index 00000000000..3845cc00ce2 --- /dev/null +++ b/docs/onnx_custom_op/custom_relu_model.prototxt @@ -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 +} diff --git a/docs/onnx_custom_op/onnx_custom_op.cpp b/docs/onnx_custom_op/onnx_custom_op.cpp index 2d8c6198e64..09ea58cc2b6 100644 --- a/docs/onnx_custom_op/onnx_custom_op.cpp +++ b/docs/onnx_custom_op/onnx_custom_op.cpp @@ -9,68 +9,6 @@ #include //! [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 diff --git a/docs/onnx_custom_op/onnx_custom_op.hpp b/docs/onnx_custom_op/onnx_custom_op.hpp index fae68709704..e1670a5f82b 100644 --- a/docs/onnx_custom_op/onnx_custom_op.hpp +++ b/docs/onnx_custom_op/onnx_custom_op.hpp @@ -4,8 +4,5 @@ #pragma once -#include - -std::string custom_relu_model(); void register_custom_relu_operator(); void unregister_custom_relu_operator(); diff --git a/inference-engine/tests/functional/plugin/cpu/CMakeLists.txt b/inference-engine/tests/functional/plugin/cpu/CMakeLists.txt index 5e973d1f57a..a9b7f932241 100644 --- a/inference-engine/tests/functional/plugin/cpu/CMakeLists.txt +++ b/inference-engine/tests/functional/plugin/cpu/CMakeLists.txt @@ -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 diff --git a/inference-engine/tests/functional/plugin/cpu/extension/extension.cpp b/inference-engine/tests/functional/plugin/cpu/extension/extension.cpp index 1f9a430a24d..baf3dc3e7f8 100644 --- a/inference-engine/tests/functional/plugin/cpu/extension/extension.cpp +++ b/inference-engine/tests/functional/plugin/cpu/extension/extension.cpp @@ -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& input_values, const std::vector& 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(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 input_values{1, -2, 3, -4, 5, -6, 7, -8, 9, -10}; std::vector expected{1, 4, 3, 8, 5, 12, 7, 16, 9, 20}; InferenceEngine::Core ie; @@ -207,7 +163,8 @@ opset_import { return {std::make_shared(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 expected{1, 4, 3, 8, 5, 12, 7, 16, 9, 20}; InferenceEngine::Core ie; ie.AddExtension(std::make_shared()); - 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 expected{12, 13, 14, 15, 16, 17, 18, 19}; InferenceEngine::Core ie; ie.AddExtension(std::make_shared(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 input_values{1, 2, 3, 4, 5, 6, 7, 8}; std::vector expected{12, 13, 14, 15, 16, 17, 18, 19}; InferenceEngine::Core ie; ie.AddExtension(std::make_shared(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(); } diff --git a/inference-engine/tests/functional/plugin/shared/models/custom_abs_op.prototxt b/inference-engine/tests/functional/plugin/shared/models/custom_abs_op.prototxt new file mode 100644 index 00000000000..4b42d612bee --- /dev/null +++ b/inference-engine/tests/functional/plugin/shared/models/custom_abs_op.prototxt @@ -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" +} diff --git a/inference-engine/tests/functional/plugin/shared/models/custom_template_op.prototxt b/inference-engine/tests/functional/plugin/shared/models/custom_template_op.prototxt new file mode 100644 index 00000000000..38e0fec48b6 --- /dev/null +++ b/inference-engine/tests/functional/plugin/shared/models/custom_template_op.prototxt @@ -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" +}