Expand ONNX functions in Graph ctor (#11599)

* Expand ONNX function in constructor of class Graph

* Add test to expanding function GreaterOrEqual inside If op
This commit is contained in:
Artur Kulikowski authored and GitHub committed 2022-05-02 10:12:41 +02:00
1 parent 8d221d1e06
commit 26aa197cb1
4 files changed
+259 -1

No files matched your search

@@ -0,0 +1,234 @@
ir_version: 6
producer_name: "nGraph ONNX Importer"
graph {
name: "if graph"
node {
input: "condition"
output: "if"
name: "if"
op_type: "If"
attribute {
name: "then_branch"
g {
name: "then_branch"
node {
input: "x"
input: "y"
output: "greater_or_equal"
name: "greater_or_equal"
op_type: "GreaterOrEqual"
}
node {
input: "greater_or_equal"
output: "cast_to_int"
name: "cast_to_int"
op_type: "Cast"
attribute {
name: "to"
type: INT
i: 6
}
}
node {
input: "cast_to_int"
output: "reduce_max"
name: "reduce_max"
op_type: "ReduceMax"
attribute {
name: "keepdims"
type: INT
i: 0
}
}
node {
input: "reduce_max"
output: "cast_to_bool"
name: "cast_to_bool"
op_type: "Cast"
attribute {
name: "to"
type: INT
i: 9
}
}
node {
input: "cast_to_bool"
output: "if_inside"
name: "if"
op_type: "If"
attribute {
name: "then_branch"
type: GRAPH
g {
name: "then_branch_inside"
node {
input: "x"
input: "y"
output: "mul"
name: "mul"
op_type: "Mul"
}
output {
name: "mul"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 10
}
dim {
dim_value: 4
}
}
}
}
}
}
}
attribute {
name: "else_branch"
type: GRAPH
g {
name: "else_branch_inside"
node {
input: "x"
input: "y"
output: "add"
name: "add"
op_type: "Add"
}
output {
name: "add"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 10
}
dim {
dim_value: 4
}
}
}
}
}
}
}
}
output {
name: "if_inside"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 10
}
dim {
dim_value: 4
}
}
}
}
}
}
type: GRAPH
}
attribute {
name: "else_branch"
type: GRAPH
g {
name: "else_branch"
node {
input: "x"
input: "y"
output: "sub"
name: "sub"
op_type: "Sub"
}
output {
name: "sub"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 10
}
dim {
dim_value: 4
}
}
}
}
}
}
}
}
input {
name: "condition"
type {
tensor_type {
elem_type: 9
shape {
dim {
dim_value: 1
}
}
}
}
}
input {
name: "x"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 10
}
dim {
dim_value: 4
}
}
}
}
}
input {
name: "y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 10
}
dim {
dim_value: 4
}
}
}
}
}
output {
name: "if"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 10
}
dim {
dim_value: 4
}
}
}
}
}
}
opset_import {
version: 13
}
+22
View File
@@ -418,6 +418,28 @@ NGRAPH_TEST(${BACKEND_NAME}, onnx_expand_function_dependency_to_created_subgraph
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_expand_function_greater_or_equal_inside_if) {
const auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/transformations/greater_or_equal_inside_if.onnx"));
auto test_case = test::TestCase(function, s_device);
// case when condition == true and any(x >= y)
// expected value == x * y
std::vector<float> x(40, 2);
std::vector<float> y(40);
std::iota(y.begin(), y.end(), -20);
std::vector<float> expected;
std::transform(x.begin(), x.end(), y.begin(), std::back_inserter(expected), [](float i, float j) -> float {
return i * j;
});
test_case.add_input<bool>({true}); // condition
test_case.add_input<float>(x);
test_case.add_input<float>(y);
test_case.add_expected_output<float>(expected);
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, onnx_expand_context_dependent_function) {
auto function = onnx_import::import_onnx_model(
file_util::path_join(SERIALIZED_ZOO, "onnx/transformations/softmax_crossentropy_consumed.onnx"));
@@ -9,6 +9,7 @@
#include <numeric>
#include <sstream>
#include "core/transform.hpp"
#include "core/value_info.hpp"
#include "default_opset.hpp"
#include "exceptions.hpp"
@@ -62,6 +63,8 @@ Graph::Graph(const std::shared_ptr<ONNX_NAMESPACE::ModelProto>& model_proto,
: m_model{common::make_unique<Model>(model_proto)},
m_cache{std::move(cache)},
m_extensions{std::move(extensions)} {
transform::expand_onnx_functions(*model_proto);
std::map<std::string, Tensor> initializers;
// Process all initializers in the graph
@@ -52,7 +52,6 @@ void remove_dangling_results(std::shared_ptr<Function>& function) {
}
void apply_transformations(ONNX_NAMESPACE::ModelProto& model_proto, const std::string& model_path) {
transform::expand_onnx_functions(model_proto);
transform::fixup_legacy_operators(model_proto);
transform::update_external_data_paths(model_proto, model_path);
}