From 9cae746cd253529b77a075512db03bfdfd7196dc Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Fri, 20 Aug 2021 19:44:12 +0300 Subject: [PATCH 01/18] Added RandomUniformFusion transformation. --- .../random_uniform_fusion.hpp | 60 ++++++++++ .../common_optimizations.cpp | 2 + .../random_uniform_fusion.cpp | 104 +++++++++++++++++ .../random_uniform_fusion_test.cpp | 106 ++++++++++++++++++ 4 files changed, 272 insertions(+) create mode 100644 inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp create mode 100644 inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp create mode 100644 inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp diff --git a/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp b/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp new file mode 100644 index 00000000000000..9c597aeef01d8f --- /dev/null +++ b/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp @@ -0,0 +1,60 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include + +namespace ngraph { +namespace pass { + +class TRANSFORMATIONS_API RandomUniformFusion; +class TRANSFORMATIONS_API RandomUniformMaxValFusion; +class TRANSFORMATIONS_API RandomUniformMinValFusion; + +} // namespace pass +} // namespace ngraph + +/** + * @ingroup ie_transformation_common_api + * @brief RandomUniformMaxValFusion transformation replaces RandomUniform -> Mul subgraph + * with a single RandomUniform node. + * + * * Restrictions: + * RandomUniform node should have floating output type and input max value = 1.0. + */ +class ngraph::pass::RandomUniformMaxValFusion : public ngraph::pass::MatcherPass { +public: + NGRAPH_RTTI_DECLARATION; + RandomUniformMaxValFusion(); +}; + +/** + * @ingroup ie_transformation_common_api + * @brief RandomUniformMinValFusion transformation replaces RandomUniform -> Add subgraph + * with a RandomUniform and replaces min and max const with corrected values. + * + * * Restrictions: + * RandomUniform node should have floating output type and input min value = 0.0. + */ +class ngraph::pass::RandomUniformMinValFusion : public ngraph::pass::MatcherPass { +public: + NGRAPH_RTTI_DECLARATION; + RandomUniformMinValFusion(); +}; + +/** + * @ingroup ie_transformation_common_api + * @brief RandomUniformFusion transformation replaces RandomUniform with Add or Mul sub-graphs with single + * RandomUniform. + */ +class ngraph::pass::RandomUniformFusion : public ngraph::pass::GraphRewrite { +public: + NGRAPH_RTTI_DECLARATION; + RandomUniformFusion() { + add_matcher(); + add_matcher(); + } +}; diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp index 29405d08a7d3ab..c1428b985450ec 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp @@ -26,6 +26,7 @@ #include "transformations/common_optimizations/hsigmoid_fusion.hpp" #include "transformations/common_optimizations/hswish_fusion.hpp" #include "transformations/common_optimizations/convert_quantize_dequantize.hpp" +#include "transformations/common_optimizations/random_uniform_fusion.hpp" #include "transformations/common_optimizations/relu_fake_quantize_fusion.hpp" #include "transformations/common_optimizations/add_fake_quantize_fusion.hpp" #include "transformations/common_optimizations/mul_fake_quantize_fusion.hpp" @@ -133,6 +134,7 @@ bool ngraph::pass::CommonOptimizations::run_on_function(std::shared_ptradd_matcher(); common_fusions->add_matcher(); common_fusions->add_matcher(); + common_fusions->add_matcher(); common_fusions->set_name("ngraph::pass::CommonFusions"); manager.register_pass(); diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp new file mode 100644 index 00000000000000..9eecc0c2140484 --- /dev/null +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp @@ -0,0 +1,104 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "transformations/common_optimizations/random_uniform_fusion.hpp" + +#include +#include +#include +#include + +#include "itt.hpp" + +NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformFusion, "RandomUniformFusion", 0); +NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMaxValFusion, "RandomUniformMaxValFusion", 0); +NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMinValFusion, "RandomUniformMinValFusion", 0); + +ngraph::pass::RandomUniformMaxValFusion::RandomUniformMaxValFusion() { + MATCHER_SCOPE(RandomUniformMaxValFusion); + auto data_pattern = ngraph::pattern::any_input(); + auto ru_min_const_pattern = ngraph::pattern::wrap_type(); + auto ru_max_const_pattern = ngraph::pattern::wrap_type(); + auto random_uniform_pattern = + ngraph::pattern::wrap_type({data_pattern, ru_min_const_pattern, ru_max_const_pattern}, + pattern::consumers_count(1)); + auto mul_const_pattern = ngraph::pattern::wrap_type(); + auto mul_pattern = ngraph::pattern::wrap_type({random_uniform_pattern, mul_const_pattern}); + + ngraph::matcher_pass_callback callback = [=](pattern::Matcher& m) { + auto pattern_map = m.get_pattern_value_map(); + auto data = pattern_map[data_pattern]; + auto random_uniform = pattern_map[random_uniform_pattern]; + auto ru_max_val_const = pattern_map[ru_max_const_pattern]; + auto mul = pattern_map[mul_pattern]; + auto mul_const = pattern_map[mul_const_pattern]; + auto ru = std::dynamic_pointer_cast(random_uniform.get_node_shared_ptr()); + if (!ru) + return false; + if (!ru->get_out_type().is_real()) + return false; + + auto max_val_const = std::dynamic_pointer_cast(ru_max_val_const.get_node_shared_ptr()); + if (!max_val_const) + return false; + auto max_value = max_val_const->cast_vector()[0]; + if (max_value != 1.0f) + return false; + + auto new_ru = ru->clone_with_new_inputs({data, ru->input_value(1), mul_const}); + new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); + copy_runtime_info({mul.get_node_shared_ptr(), ru}, new_ru); + ngraph::replace_node(m.get_match_root(), new_ru); + + return true; + }; + + auto m = std::make_shared(mul_pattern, matcher_name); + this->register_matcher(m, callback); +} + +ngraph::pass::RandomUniformMinValFusion::RandomUniformMinValFusion() { + MATCHER_SCOPE(RandomUniformMinValFusion); + auto data_pattern = ngraph::pattern::any_input(); + auto ru_min_const_pattern = ngraph::pattern::wrap_type(); + auto ru_max_const_pattern = ngraph::pattern::wrap_type(); + auto random_uniform_pattern = + ngraph::pattern::wrap_type({data_pattern, ru_min_const_pattern, ru_max_const_pattern}, + pattern::consumers_count(1)); + auto add_const_pattern = ngraph::pattern::wrap_type(); + auto add_pattern = ngraph::pattern::wrap_type({random_uniform_pattern, add_const_pattern}); + + ngraph::matcher_pass_callback callback = [=](pattern::Matcher& m) { + auto pattern_map = m.get_pattern_value_map(); + auto data = pattern_map[data_pattern]; + auto random_uniform = pattern_map[random_uniform_pattern]; + auto add = pattern_map[add_pattern]; + auto add_const = pattern_map[add_const_pattern]; + auto ru_max_const = pattern_map[ru_max_const_pattern]; + auto ru_min_val_const = pattern_map[ru_min_const_pattern]; + auto ru = std::dynamic_pointer_cast(random_uniform.get_node_shared_ptr()); + if (!ru) + return false; + if (!ru->get_out_type().is_real()) + return false; + + auto min_val_const = std::dynamic_pointer_cast(ru_min_val_const.get_node_shared_ptr()); + if (!min_val_const) + return false; + auto max_value = min_val_const->cast_vector()[0]; + if (max_value != 0.0f) + return false; + + auto new_add = register_new_node(add_const, ru_max_const); + auto new_ru = ru->clone_with_new_inputs({data, add_const, new_add}); + new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); + copy_runtime_info({add.get_node_shared_ptr(), ru}, new_ru); + ngraph::replace_node(m.get_match_root(), new_ru); + + return true; + }; + + auto m = std::make_shared(add_pattern, matcher_name); + this->register_matcher(m, callback); +} diff --git a/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp b/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp new file mode 100644 index 00000000000000..3218faa200c0a1 --- /dev/null +++ b/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp @@ -0,0 +1,106 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "common_test_utils/ngraph_test_utils.hpp" + +using namespace testing; + +TEST(TransformationTests, RandomUniformMaxValFusing) { + std::shared_ptr f(nullptr), f_ref(nullptr); + { + auto input = std::make_shared( + ngraph::element::i32, ngraph::Shape{3}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, + ngraph::Shape{1}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, + ngraph::Shape{1}, {1.0}); + auto ru = std::make_shared( + input, min_const, max_const, ngraph::element::f32, 100, 200); + + auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f32, + ngraph::Shape{1}, {30.0}); + auto mul = std::make_shared(ru, mul_const); + + f = std::make_shared(ngraph::NodeVector{mul}, + ngraph::ParameterVector{input}); + + ngraph::pass::Manager manager; + manager.register_pass(); + manager.register_pass(); + manager.run_passes(f); + ASSERT_NO_THROW(check_rt_info(f)); + } + + { + auto input = std::make_shared( + ngraph::element::i32, ngraph::Shape{3}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, + ngraph::Shape{1}, {0.0}); + auto add_const = ngraph::opset8::Constant::create(ngraph::element::f32, + ngraph::Shape{1}, {30.0}); + auto ru = std::make_shared( + input, min_const, add_const, ngraph::element::f32, 100, 200); + + f_ref = std::make_shared(ngraph::NodeVector{ru}, + ngraph::ParameterVector{input}); + } + + auto res = compare_functions(f, f_ref); + ASSERT_TRUE(res.first) << res.second; +} + +TEST(TransformationTests, RandomUniformMinValFusing) { + std::shared_ptr f(nullptr), f_ref(nullptr); + { + auto input = std::make_shared( + ngraph::element::i32, ngraph::Shape{3}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, + ngraph::Shape{1}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, + ngraph::Shape{1}, {30.0}); + auto ru = std::make_shared( + input, min_const, max_const, ngraph::element::f32, 100, 200); + + auto add_const = ngraph::opset8::Constant::create( + ngraph::element::f32, ngraph::Shape{1}, {-10.0}); + auto add = std::make_shared(ru, add_const); + + f = std::make_shared(ngraph::NodeVector{add}, + ngraph::ParameterVector{input}); + + ngraph::pass::Manager manager; + manager.register_pass(); + manager.register_pass(); + manager.run_passes(f); + ASSERT_NO_THROW(check_rt_info(f)); + } + + { + auto input = std::make_shared( + ngraph::element::i32, ngraph::Shape{3}); + auto add_const = ngraph::opset8::Constant::create( + ngraph::element::f32, ngraph::Shape{1}, {-10.0}); + auto ru_max_const = ngraph::opset8::Constant::create( + ngraph::element::f32, ngraph::Shape{1}, {30.0}); + auto add = std::make_shared(add_const, ru_max_const); + auto ru = std::make_shared( + input, add_const, add, ngraph::element::f32, 100, 200); + + f_ref = std::make_shared(ngraph::NodeVector{ru}, + ngraph::ParameterVector{input}); + } + + auto res = compare_functions(f, f_ref); + ASSERT_TRUE(res.first) << res.second; +} From 92395c7659d89a93765a0f89116ee03b176120f1 Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Tue, 31 Aug 2021 13:34:43 +0300 Subject: [PATCH 02/18] Extended transformations for case with Convert, extended transformations for general min and max value case. --- .../random_uniform_fusion.hpp | 4 - .../random_uniform_fusion.cpp | 234 +++++++++++------- .../random_uniform_fusion_test.cpp | 128 +++++++++- 3 files changed, 270 insertions(+), 96 deletions(-) diff --git a/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp b/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp index 9c597aeef01d8f..8cd05d28f9e271 100644 --- a/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp +++ b/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp @@ -22,8 +22,6 @@ class TRANSFORMATIONS_API RandomUniformMinValFusion; * @brief RandomUniformMaxValFusion transformation replaces RandomUniform -> Mul subgraph * with a single RandomUniform node. * - * * Restrictions: - * RandomUniform node should have floating output type and input max value = 1.0. */ class ngraph::pass::RandomUniformMaxValFusion : public ngraph::pass::MatcherPass { public: @@ -36,8 +34,6 @@ class ngraph::pass::RandomUniformMaxValFusion : public ngraph::pass::MatcherPass * @brief RandomUniformMinValFusion transformation replaces RandomUniform -> Add subgraph * with a RandomUniform and replaces min and max const with corrected values. * - * * Restrictions: - * RandomUniform node should have floating output type and input min value = 0.0. */ class ngraph::pass::RandomUniformMinValFusion : public ngraph::pass::MatcherPass { public: diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp index 9eecc0c2140484..28486d399bcff3 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp @@ -6,99 +6,163 @@ #include #include +#include #include #include #include "itt.hpp" -NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformFusion, "RandomUniformFusion", 0); -NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMaxValFusion, "RandomUniformMaxValFusion", 0); -NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMinValFusion, "RandomUniformMinValFusion", 0); +NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformFusion, "RandomUniformFusion", + 0); +NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMaxValFusion, + "RandomUniformMaxValFusion", 0); +NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMinValFusion, + "RandomUniformMinValFusion", 0); ngraph::pass::RandomUniformMaxValFusion::RandomUniformMaxValFusion() { - MATCHER_SCOPE(RandomUniformMaxValFusion); - auto data_pattern = ngraph::pattern::any_input(); - auto ru_min_const_pattern = ngraph::pattern::wrap_type(); - auto ru_max_const_pattern = ngraph::pattern::wrap_type(); - auto random_uniform_pattern = - ngraph::pattern::wrap_type({data_pattern, ru_min_const_pattern, ru_max_const_pattern}, - pattern::consumers_count(1)); - auto mul_const_pattern = ngraph::pattern::wrap_type(); - auto mul_pattern = ngraph::pattern::wrap_type({random_uniform_pattern, mul_const_pattern}); - - ngraph::matcher_pass_callback callback = [=](pattern::Matcher& m) { - auto pattern_map = m.get_pattern_value_map(); - auto data = pattern_map[data_pattern]; - auto random_uniform = pattern_map[random_uniform_pattern]; - auto ru_max_val_const = pattern_map[ru_max_const_pattern]; - auto mul = pattern_map[mul_pattern]; - auto mul_const = pattern_map[mul_const_pattern]; - auto ru = std::dynamic_pointer_cast(random_uniform.get_node_shared_ptr()); - if (!ru) - return false; - if (!ru->get_out_type().is_real()) - return false; - - auto max_val_const = std::dynamic_pointer_cast(ru_max_val_const.get_node_shared_ptr()); - if (!max_val_const) - return false; - auto max_value = max_val_const->cast_vector()[0]; - if (max_value != 1.0f) - return false; - - auto new_ru = ru->clone_with_new_inputs({data, ru->input_value(1), mul_const}); - new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); - copy_runtime_info({mul.get_node_shared_ptr(), ru}, new_ru); - ngraph::replace_node(m.get_match_root(), new_ru); - - return true; - }; - - auto m = std::make_shared(mul_pattern, matcher_name); - this->register_matcher(m, callback); + MATCHER_SCOPE(RandomUniformMaxValFusion); + auto data_pattern = ngraph::pattern::any_input(); + auto ru_min_input_pattern = ngraph::pattern::any_input(); + auto ru_max_input_pattern = ngraph::pattern::any_input(); + auto random_uniform_pattern = + ngraph::pattern::wrap_type( + {data_pattern, ru_min_input_pattern, ru_max_input_pattern}, + pattern::consumers_count(1)); + auto mul_const_pattern = ngraph::pattern::wrap_type(); + auto mul_pattern = ngraph::pattern::wrap_type( + {random_uniform_pattern, mul_const_pattern}); + + auto conv_pattern = + ngraph::pattern::wrap_type({random_uniform_pattern}); + auto mul_with_convert_pattern = ngraph::pattern::wrap_type( + {conv_pattern, mul_const_pattern}); + + auto mul_or_mul_with_convert_pattern = std::make_shared( + OutputVector{mul_pattern, mul_with_convert_pattern}); + + ngraph::matcher_pass_callback callback = [=](pattern::Matcher &m) { + auto pattern_map = m.get_pattern_value_map(); + auto data = pattern_map[data_pattern]; + auto random_uniform = pattern_map[random_uniform_pattern]; + auto mul_const = pattern_map[mul_const_pattern]; + auto mul_or_mul_with_convert = pattern_map[mul_or_mul_with_convert_pattern]; + auto ru = std::dynamic_pointer_cast( + random_uniform.get_node_shared_ptr()); + if (!ru) + return false; + + if (pattern_map.count(conv_pattern)) { + auto mul = pattern_map[mul_with_convert_pattern]; + auto ml = std::dynamic_pointer_cast( + mul.get_node_shared_ptr()); + if (!ml) + return false; + + auto conv = pattern_map[conv_pattern]; + auto cv = std::dynamic_pointer_cast( + conv.get_node_shared_ptr()); + if (!cv) + return false; + auto new_conv = register_new_node( + ml->input_value(1), ru->get_out_type()); + auto new_mul = ml->clone_with_new_inputs({ru->input_value(2), new_conv}); + auto new_ru = + ru->clone_with_new_inputs({data, ru->input_value(1), new_mul}); + new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); + auto new_ru_conv = cv->clone_with_new_inputs({new_ru}); + copy_runtime_info({ru, cv}, {new_ru, new_ru_conv}); + ngraph::replace_node(m.get_match_root(), new_ru_conv); + + } else { + auto mul = pattern_map[mul_pattern]; + auto ml = std::dynamic_pointer_cast( + mul.get_node_shared_ptr()); + if (!ml) + return false; + auto new_mul = + ml->clone_with_new_inputs({ru->input_value(2), ml->input_value(1)}); + auto new_ru = + ru->clone_with_new_inputs({data, ru->input_value(1), new_mul}); + new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); + copy_runtime_info({mul.get_node_shared_ptr(), ru}, {new_mul, new_ru}); + ngraph::replace_node(m.get_match_root(), new_ru); + } + + return true; + }; + + auto m = std::make_shared( + mul_or_mul_with_convert_pattern, matcher_name); + this->register_matcher(m, callback); } ngraph::pass::RandomUniformMinValFusion::RandomUniformMinValFusion() { - MATCHER_SCOPE(RandomUniformMinValFusion); - auto data_pattern = ngraph::pattern::any_input(); - auto ru_min_const_pattern = ngraph::pattern::wrap_type(); - auto ru_max_const_pattern = ngraph::pattern::wrap_type(); - auto random_uniform_pattern = - ngraph::pattern::wrap_type({data_pattern, ru_min_const_pattern, ru_max_const_pattern}, - pattern::consumers_count(1)); - auto add_const_pattern = ngraph::pattern::wrap_type(); - auto add_pattern = ngraph::pattern::wrap_type({random_uniform_pattern, add_const_pattern}); - - ngraph::matcher_pass_callback callback = [=](pattern::Matcher& m) { - auto pattern_map = m.get_pattern_value_map(); - auto data = pattern_map[data_pattern]; - auto random_uniform = pattern_map[random_uniform_pattern]; - auto add = pattern_map[add_pattern]; - auto add_const = pattern_map[add_const_pattern]; - auto ru_max_const = pattern_map[ru_max_const_pattern]; - auto ru_min_val_const = pattern_map[ru_min_const_pattern]; - auto ru = std::dynamic_pointer_cast(random_uniform.get_node_shared_ptr()); - if (!ru) - return false; - if (!ru->get_out_type().is_real()) - return false; - - auto min_val_const = std::dynamic_pointer_cast(ru_min_val_const.get_node_shared_ptr()); - if (!min_val_const) - return false; - auto max_value = min_val_const->cast_vector()[0]; - if (max_value != 0.0f) - return false; - - auto new_add = register_new_node(add_const, ru_max_const); - auto new_ru = ru->clone_with_new_inputs({data, add_const, new_add}); - new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); - copy_runtime_info({add.get_node_shared_ptr(), ru}, new_ru); - ngraph::replace_node(m.get_match_root(), new_ru); - - return true; - }; - - auto m = std::make_shared(add_pattern, matcher_name); - this->register_matcher(m, callback); + MATCHER_SCOPE(RandomUniformMinValFusion); + auto data_pattern = ngraph::pattern::any_input(); + auto ru_min_input_pattern = ngraph::pattern::any_input(); + auto ru_max_input_pattern = ngraph::pattern::any_input(); + auto random_uniform_pattern = + ngraph::pattern::wrap_type( + {data_pattern, ru_min_input_pattern, ru_max_input_pattern}, + pattern::consumers_count(1)); + auto add_const_pattern = ngraph::pattern::wrap_type(); + auto add_pattern = ngraph::pattern::wrap_type( + {random_uniform_pattern, add_const_pattern}); + + auto conv_pattern = + ngraph::pattern::wrap_type({random_uniform_pattern}); + auto add_with_convert_pattern = ngraph::pattern::wrap_type( + {conv_pattern, add_const_pattern}); + + auto add_or_add_with_convert_pattern = std::make_shared( + OutputVector{add_pattern, add_with_convert_pattern}); + + ngraph::matcher_pass_callback callback = [=](pattern::Matcher &m) { + auto pattern_map = m.get_pattern_value_map(); + auto data = pattern_map[data_pattern]; + auto random_uniform = pattern_map[random_uniform_pattern]; + auto add_const = pattern_map[add_const_pattern]; + auto ru_max_input = pattern_map[ru_max_input_pattern]; + auto ru_min_input = pattern_map[ru_min_input_pattern]; + auto ru = std::dynamic_pointer_cast( + random_uniform.get_node_shared_ptr()); + if (!ru) + return false; + + if (pattern_map.count(conv_pattern)) { + auto add = pattern_map[add_with_convert_pattern]; + auto conv = pattern_map[conv_pattern]; + auto cv = std::dynamic_pointer_cast( + conv.get_node_shared_ptr()); + if (!cv) + return false; + auto add_conv = register_new_node( + add_const, ru->get_out_type()); + auto add2 = + register_new_node(add_conv, ru_max_input); + auto add1 = + register_new_node(add_conv, ru_min_input); + auto new_ru = ru->clone_with_new_inputs({data, add1, add2}); + new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); + auto new_ru_conv = cv->clone_with_new_inputs({new_ru}); + copy_runtime_info({add.get_node_shared_ptr(), ru, cv}, + {new_ru, new_ru_conv}); + ngraph::replace_node(m.get_match_root(), new_ru_conv); + } else { + auto add = pattern_map[add_pattern]; + auto add2 = + register_new_node(add_const, ru_max_input); + auto add1 = + register_new_node(add_const, ru_min_input); + auto new_ru = ru->clone_with_new_inputs({data, add1, add2}); + new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); + copy_runtime_info({add.get_node_shared_ptr(), ru}, new_ru); + ngraph::replace_node(m.get_match_root(), new_ru); + } + return true; + }; + + auto m = std::make_shared( + add_or_add_with_convert_pattern, matcher_name); + this->register_matcher(m, callback); } diff --git a/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp b/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp index 3218faa200c0a1..efa5f0afd42189 100644 --- a/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp +++ b/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp @@ -13,6 +13,7 @@ #include #include "common_test_utils/ngraph_test_utils.hpp" +#include using namespace testing; @@ -47,10 +48,14 @@ TEST(TransformationTests, RandomUniformMaxValFusing) { ngraph::element::i32, ngraph::Shape{3}); auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); - auto add_const = ngraph::opset8::Constant::create(ngraph::element::f32, - ngraph::Shape{1}, {30.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, + ngraph::Shape{1}, {1.0}); + + auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f32, + ngraph::Shape{1}, {30.0}); + auto mul = std::make_shared(max_const, mul_const); auto ru = std::make_shared( - input, min_const, add_const, ngraph::element::f32, 100, 200); + input, min_const, mul, ngraph::element::f32, 100, 200); f_ref = std::make_shared(ngraph::NodeVector{ru}, ngraph::ParameterVector{input}); @@ -89,13 +94,16 @@ TEST(TransformationTests, RandomUniformMinValFusing) { { auto input = std::make_shared( ngraph::element::i32, ngraph::Shape{3}); - auto add_const = ngraph::opset8::Constant::create( - ngraph::element::f32, ngraph::Shape{1}, {-10.0}); auto ru_max_const = ngraph::opset8::Constant::create( ngraph::element::f32, ngraph::Shape{1}, {30.0}); - auto add = std::make_shared(add_const, ru_max_const); + auto ru_min_const = ngraph::opset8::Constant::create( + ngraph::element::f32, ngraph::Shape{1}, {0.0}); + auto add2_const = ngraph::opset8::Constant::create( + ngraph::element::f32, ngraph::Shape{1}, {-10.0}); + auto add1 = std::make_shared(add2_const, ru_min_const); + auto add2 = std::make_shared(add2_const, ru_max_const); auto ru = std::make_shared( - input, add_const, add, ngraph::element::f32, 100, 200); + input, add1, add2, ngraph::element::f32, 100, 200); f_ref = std::make_shared(ngraph::NodeVector{ru}, ngraph::ParameterVector{input}); @@ -104,3 +112,109 @@ TEST(TransformationTests, RandomUniformMinValFusing) { auto res = compare_functions(f, f_ref); ASSERT_TRUE(res.first) << res.second; } + +TEST(TransformationTests, RandomUniformWithConvertMaxValFusing) { + std::shared_ptr f(nullptr), f_ref(nullptr); + { + auto input = std::make_shared( + ngraph::element::i32, ngraph::Shape{3}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, + ngraph::Shape{1}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, + ngraph::Shape{1}, {1.0}); + auto ru = std::make_shared( + input, min_const, max_const, ngraph::element::f32, 100, 200); + auto conv = std::make_shared(ru, ngraph::element::f16); + + auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f16, + ngraph::Shape{1}, {30.0}); + auto mul = std::make_shared(conv, mul_const); + + f = std::make_shared(ngraph::NodeVector{mul}, + ngraph::ParameterVector{input}); + + ngraph::pass::Manager manager; + manager.register_pass(); + manager.register_pass(); + manager.run_passes(f); + ASSERT_NO_THROW(check_rt_info(f)); + } + + { + auto input = std::make_shared( + ngraph::element::i32, ngraph::Shape{3}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, + ngraph::Shape{1}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, + ngraph::Shape{1}, {1.0}); + + + auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f16, + ngraph::Shape{1}, {30.0}); + auto mul_const_conv = std::make_shared(mul_const, ngraph::element::f32); + + auto mul = std::make_shared(max_const, mul_const_conv); + + auto ru = std::make_shared( + input, min_const, mul, ngraph::element::f32, 100, 200); + auto conv = std::make_shared(ru, ngraph::element::f16); + + f_ref = std::make_shared(ngraph::NodeVector{conv}, + ngraph::ParameterVector{input}); + } + + auto res = compare_functions(f, f_ref); + ASSERT_TRUE(res.first) << res.second; +} + +TEST(TransformationTests, RandomUniformWithConvertMinValFusing) { + std::shared_ptr f(nullptr), f_ref(nullptr); + { + auto input = std::make_shared( + ngraph::element::i32, ngraph::Shape{3}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, + ngraph::Shape{1}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, + ngraph::Shape{1}, {30.0}); + auto ru = std::make_shared( + input, min_const, max_const, ngraph::element::f32, 100, 200); + auto conv = std::make_shared(ru, ngraph::element::f16); + + auto add_const = ngraph::opset8::Constant::create( + ngraph::element::f16, ngraph::Shape{1}, {-10.0}); + auto add = std::make_shared(conv, add_const); + + f = std::make_shared(ngraph::NodeVector{add}, + ngraph::ParameterVector{input}); + + ngraph::pass::Manager manager; + manager.register_pass(); + manager.register_pass(); + manager.run_passes(f); + ASSERT_NO_THROW(check_rt_info(f)); + } + + { + auto input = std::make_shared( + ngraph::element::i32, ngraph::Shape{3}); + auto add_const = ngraph::opset8::Constant::create( + ngraph::element::f16, ngraph::Shape{1}, {-10.0}); + auto conv_const = std::make_shared(add_const, ngraph::element::f32); + auto ru_min_const = ngraph::opset8::Constant::create( + ngraph::element::f32, ngraph::Shape{1}, {0.0}); + auto ru_max_const = ngraph::opset8::Constant::create( + ngraph::element::f32, ngraph::Shape{1}, {30.0}); + auto add1 = std::make_shared(conv_const, ru_min_const); + auto add2 = std::make_shared(conv_const, ru_max_const); + + auto ru = std::make_shared( + input, add1, add2, ngraph::element::f32, 100, 200); + auto conv = std::make_shared(ru, ngraph::element::f16); + + f_ref = std::make_shared(ngraph::NodeVector{conv}, + ngraph::ParameterVector{input}); + } + + auto res = compare_functions(f, f_ref); + ASSERT_TRUE(res.first) << res.second; +} \ No newline at end of file From 84441012adc707f8873b150aff7b81aa77296226 Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Tue, 31 Aug 2021 21:53:27 +0300 Subject: [PATCH 03/18] Set to const unchanged variables. --- .../random_uniform_fusion.cpp | 119 +++++++++--------- 1 file changed, 62 insertions(+), 57 deletions(-) diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp index 28486d399bcff3..96848d6ac12e10 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp @@ -21,67 +21,71 @@ NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMinValFusion, ngraph::pass::RandomUniformMaxValFusion::RandomUniformMaxValFusion() { MATCHER_SCOPE(RandomUniformMaxValFusion); - auto data_pattern = ngraph::pattern::any_input(); - auto ru_min_input_pattern = ngraph::pattern::any_input(); - auto ru_max_input_pattern = ngraph::pattern::any_input(); - auto random_uniform_pattern = + const auto data_pattern = ngraph::pattern::any_input(); + const auto ru_min_input_pattern = ngraph::pattern::any_input(); + const auto ru_max_input_pattern = ngraph::pattern::any_input(); + const auto random_uniform_pattern = ngraph::pattern::wrap_type( {data_pattern, ru_min_input_pattern, ru_max_input_pattern}, pattern::consumers_count(1)); - auto mul_const_pattern = ngraph::pattern::wrap_type(); - auto mul_pattern = ngraph::pattern::wrap_type( + const auto mul_const_pattern = ngraph::pattern::wrap_type(); + const auto mul_pattern = ngraph::pattern::wrap_type( {random_uniform_pattern, mul_const_pattern}); - auto conv_pattern = + const auto conv_pattern = ngraph::pattern::wrap_type({random_uniform_pattern}); - auto mul_with_convert_pattern = ngraph::pattern::wrap_type( - {conv_pattern, mul_const_pattern}); + const auto mul_with_convert_pattern = + ngraph::pattern::wrap_type( + {conv_pattern, mul_const_pattern}); - auto mul_or_mul_with_convert_pattern = std::make_shared( - OutputVector{mul_pattern, mul_with_convert_pattern}); + const auto mul_or_mul_with_convert_pattern = + std::make_shared( + OutputVector{mul_pattern, mul_with_convert_pattern}); ngraph::matcher_pass_callback callback = [=](pattern::Matcher &m) { auto pattern_map = m.get_pattern_value_map(); - auto data = pattern_map[data_pattern]; - auto random_uniform = pattern_map[random_uniform_pattern]; - auto mul_const = pattern_map[mul_const_pattern]; - auto mul_or_mul_with_convert = pattern_map[mul_or_mul_with_convert_pattern]; - auto ru = std::dynamic_pointer_cast( + const auto data = pattern_map[data_pattern]; + const auto random_uniform = pattern_map[random_uniform_pattern]; + const auto mul_const = pattern_map[mul_const_pattern]; + const auto mul_or_mul_with_convert = + pattern_map[mul_or_mul_with_convert_pattern]; + const auto ru = std::dynamic_pointer_cast( random_uniform.get_node_shared_ptr()); if (!ru) return false; if (pattern_map.count(conv_pattern)) { - auto mul = pattern_map[mul_with_convert_pattern]; - auto ml = std::dynamic_pointer_cast( + const auto mul = pattern_map[mul_with_convert_pattern]; + const auto ml = std::dynamic_pointer_cast( mul.get_node_shared_ptr()); if (!ml) return false; - auto conv = pattern_map[conv_pattern]; - auto cv = std::dynamic_pointer_cast( + const auto conv = pattern_map[conv_pattern]; + const auto cv = std::dynamic_pointer_cast( conv.get_node_shared_ptr()); if (!cv) return false; - auto new_conv = register_new_node( + const auto new_conv = register_new_node( ml->input_value(1), ru->get_out_type()); - auto new_mul = ml->clone_with_new_inputs({ru->input_value(2), new_conv}); - auto new_ru = + const auto new_mul = + ml->clone_with_new_inputs({ru->input_value(2), new_conv}); + const auto new_ru = ru->clone_with_new_inputs({data, ru->input_value(1), new_mul}); new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); - auto new_ru_conv = cv->clone_with_new_inputs({new_ru}); + const auto new_ru_conv = cv->clone_with_new_inputs({new_ru}); copy_runtime_info({ru, cv}, {new_ru, new_ru_conv}); ngraph::replace_node(m.get_match_root(), new_ru_conv); } else { - auto mul = pattern_map[mul_pattern]; - auto ml = std::dynamic_pointer_cast( + const auto mul = pattern_map[mul_pattern]; + const auto ml = std::dynamic_pointer_cast( mul.get_node_shared_ptr()); if (!ml) return false; - auto new_mul = + const auto new_mul = ml->clone_with_new_inputs({ru->input_value(2), ml->input_value(1)}); - auto new_ru = + const auto new_ru = ru->clone_with_new_inputs({data, ru->input_value(1), new_mul}); new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); copy_runtime_info({mul.get_node_shared_ptr(), ru}, {new_mul, new_ru}); @@ -98,63 +102,64 @@ ngraph::pass::RandomUniformMaxValFusion::RandomUniformMaxValFusion() { ngraph::pass::RandomUniformMinValFusion::RandomUniformMinValFusion() { MATCHER_SCOPE(RandomUniformMinValFusion); - auto data_pattern = ngraph::pattern::any_input(); - auto ru_min_input_pattern = ngraph::pattern::any_input(); - auto ru_max_input_pattern = ngraph::pattern::any_input(); - auto random_uniform_pattern = + const auto data_pattern = ngraph::pattern::any_input(); + const auto ru_min_input_pattern = ngraph::pattern::any_input(); + const auto ru_max_input_pattern = ngraph::pattern::any_input(); + const auto random_uniform_pattern = ngraph::pattern::wrap_type( {data_pattern, ru_min_input_pattern, ru_max_input_pattern}, pattern::consumers_count(1)); - auto add_const_pattern = ngraph::pattern::wrap_type(); - auto add_pattern = ngraph::pattern::wrap_type( + const auto add_const_pattern = ngraph::pattern::wrap_type(); + const auto add_pattern = ngraph::pattern::wrap_type( {random_uniform_pattern, add_const_pattern}); - auto conv_pattern = + const auto conv_pattern = ngraph::pattern::wrap_type({random_uniform_pattern}); - auto add_with_convert_pattern = ngraph::pattern::wrap_type( + const auto add_with_convert_pattern = ngraph::pattern::wrap_type( {conv_pattern, add_const_pattern}); - auto add_or_add_with_convert_pattern = std::make_shared( - OutputVector{add_pattern, add_with_convert_pattern}); + const auto add_or_add_with_convert_pattern = + std::make_shared( + OutputVector{add_pattern, add_with_convert_pattern}); ngraph::matcher_pass_callback callback = [=](pattern::Matcher &m) { auto pattern_map = m.get_pattern_value_map(); - auto data = pattern_map[data_pattern]; - auto random_uniform = pattern_map[random_uniform_pattern]; - auto add_const = pattern_map[add_const_pattern]; - auto ru_max_input = pattern_map[ru_max_input_pattern]; - auto ru_min_input = pattern_map[ru_min_input_pattern]; - auto ru = std::dynamic_pointer_cast( + const auto data = pattern_map[data_pattern]; + const auto random_uniform = pattern_map[random_uniform_pattern]; + const auto add_const = pattern_map[add_const_pattern]; + const auto ru_max_input = pattern_map[ru_max_input_pattern]; + const auto ru_min_input = pattern_map[ru_min_input_pattern]; + const auto ru = std::dynamic_pointer_cast( random_uniform.get_node_shared_ptr()); if (!ru) return false; if (pattern_map.count(conv_pattern)) { - auto add = pattern_map[add_with_convert_pattern]; - auto conv = pattern_map[conv_pattern]; - auto cv = std::dynamic_pointer_cast( + const auto add = pattern_map[add_with_convert_pattern]; + const auto conv = pattern_map[conv_pattern]; + const auto cv = std::dynamic_pointer_cast( conv.get_node_shared_ptr()); if (!cv) return false; - auto add_conv = register_new_node( + const auto add_conv = register_new_node( add_const, ru->get_out_type()); - auto add2 = + const auto add2 = register_new_node(add_conv, ru_max_input); - auto add1 = + const auto add1 = register_new_node(add_conv, ru_min_input); - auto new_ru = ru->clone_with_new_inputs({data, add1, add2}); + const auto new_ru = ru->clone_with_new_inputs({data, add1, add2}); new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); - auto new_ru_conv = cv->clone_with_new_inputs({new_ru}); + const auto new_ru_conv = cv->clone_with_new_inputs({new_ru}); copy_runtime_info({add.get_node_shared_ptr(), ru, cv}, {new_ru, new_ru_conv}); ngraph::replace_node(m.get_match_root(), new_ru_conv); } else { - auto add = pattern_map[add_pattern]; - auto add2 = + const auto add = pattern_map[add_pattern]; + const auto add2 = register_new_node(add_const, ru_max_input); - auto add1 = + const auto add1 = register_new_node(add_const, ru_min_input); - auto new_ru = ru->clone_with_new_inputs({data, add1, add2}); + const auto new_ru = ru->clone_with_new_inputs({data, add1, add2}); new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); copy_runtime_info({add.get_node_shared_ptr(), ru}, new_ru); ngraph::replace_node(m.get_match_root(), new_ru); @@ -162,7 +167,7 @@ ngraph::pass::RandomUniformMinValFusion::RandomUniformMinValFusion() { return true; }; - auto m = std::make_shared( + const auto m = std::make_shared( add_or_add_with_convert_pattern, matcher_name); this->register_matcher(m, callback); } From fc383d340d08b63255c139b802fe02aa1a281dfc Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Thu, 9 Sep 2021 09:34:16 +0300 Subject: [PATCH 04/18] Apply suggestions from code review Co-authored-by: Gleb Kazantaev --- .../common_optimizations/random_uniform_fusion.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp index 96848d6ac12e10..08c62d9086bc61 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp @@ -32,7 +32,7 @@ ngraph::pass::RandomUniformMaxValFusion::RandomUniformMaxValFusion() { const auto mul_pattern = ngraph::pattern::wrap_type( {random_uniform_pattern, mul_const_pattern}); - const auto conv_pattern = + const auto convert_pattern = ngraph::pattern::wrap_type({random_uniform_pattern}); const auto mul_with_convert_pattern = ngraph::pattern::wrap_type( @@ -43,7 +43,7 @@ ngraph::pass::RandomUniformMaxValFusion::RandomUniformMaxValFusion() { OutputVector{mul_pattern, mul_with_convert_pattern}); ngraph::matcher_pass_callback callback = [=](pattern::Matcher &m) { - auto pattern_map = m.get_pattern_value_map(); + const auto & pattern_map = m.get_pattern_value_map(); const auto data = pattern_map[data_pattern]; const auto random_uniform = pattern_map[random_uniform_pattern]; const auto mul_const = pattern_map[mul_const_pattern]; From ec84aa082d824875d58ec1aabecf22510b9e502f Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Thu, 9 Sep 2021 12:28:16 +0300 Subject: [PATCH 05/18] Reformat code, small corrections. --- .../src/moc_transformations.cpp | 2 + .../common_optimizations.cpp | 2 - .../random_uniform_fusion.cpp | 267 ++++++++---------- .../random_uniform_fusion_test.cpp | 253 ++++++++--------- 4 files changed, 229 insertions(+), 295 deletions(-) diff --git a/inference-engine/src/offline_transformations/src/moc_transformations.cpp b/inference-engine/src/offline_transformations/src/moc_transformations.cpp index 95aae8c819a3f4..445e53a000d7bd 100644 --- a/inference-engine/src/offline_transformations/src/moc_transformations.cpp +++ b/inference-engine/src/offline_transformations/src/moc_transformations.cpp @@ -38,6 +38,7 @@ #include #include #include +#include NGRAPH_RTTI_DEFINITION(ngraph::pass::MOCTransformations, "MOCTransformations", 0); @@ -91,6 +92,7 @@ bool ngraph::pass::MOCTransformations::run_on_function(std::shared_ptradd_matcher(); common_fusions->add_matcher(); common_fusions->add_matcher(); + common_fusions->add_matcher(); common_fusions->set_name("ngraph::pass::CommonFusions"); manager.register_pass(); diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp index 0f990cbf3d6834..253c4f113ab073 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp @@ -26,7 +26,6 @@ #include "transformations/common_optimizations/hsigmoid_fusion.hpp" #include "transformations/common_optimizations/hswish_fusion.hpp" #include "transformations/common_optimizations/convert_quantize_dequantize.hpp" -#include "transformations/common_optimizations/random_uniform_fusion.hpp" #include "transformations/common_optimizations/relu_fake_quantize_fusion.hpp" #include "transformations/common_optimizations/add_fake_quantize_fusion.hpp" #include "transformations/common_optimizations/mul_fake_quantize_fusion.hpp" @@ -136,7 +135,6 @@ bool ngraph::pass::CommonOptimizations::run_on_function(std::shared_ptradd_matcher(); common_fusions->add_matcher(); common_fusions->add_matcher(); - common_fusions->add_matcher(); common_fusions->set_name("ngraph::pass::CommonFusions"); manager.register_pass(); diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp index 08c62d9086bc61..4eb733133d8d7a 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp @@ -12,162 +12,123 @@ #include "itt.hpp" -NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformFusion, "RandomUniformFusion", - 0); -NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMaxValFusion, - "RandomUniformMaxValFusion", 0); -NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMinValFusion, - "RandomUniformMinValFusion", 0); +NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformFusion, "RandomUniformFusion", 0); +NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMaxValFusion, "RandomUniformMaxValFusion", 0); +NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMinValFusion, "RandomUniformMinValFusion", 0); ngraph::pass::RandomUniformMaxValFusion::RandomUniformMaxValFusion() { - MATCHER_SCOPE(RandomUniformMaxValFusion); - const auto data_pattern = ngraph::pattern::any_input(); - const auto ru_min_input_pattern = ngraph::pattern::any_input(); - const auto ru_max_input_pattern = ngraph::pattern::any_input(); - const auto random_uniform_pattern = - ngraph::pattern::wrap_type( - {data_pattern, ru_min_input_pattern, ru_max_input_pattern}, - pattern::consumers_count(1)); - const auto mul_const_pattern = ngraph::pattern::wrap_type(); - const auto mul_pattern = ngraph::pattern::wrap_type( - {random_uniform_pattern, mul_const_pattern}); - - const auto convert_pattern = - ngraph::pattern::wrap_type({random_uniform_pattern}); - const auto mul_with_convert_pattern = - ngraph::pattern::wrap_type( - {conv_pattern, mul_const_pattern}); - - const auto mul_or_mul_with_convert_pattern = - std::make_shared( - OutputVector{mul_pattern, mul_with_convert_pattern}); - - ngraph::matcher_pass_callback callback = [=](pattern::Matcher &m) { - const auto & pattern_map = m.get_pattern_value_map(); - const auto data = pattern_map[data_pattern]; - const auto random_uniform = pattern_map[random_uniform_pattern]; - const auto mul_const = pattern_map[mul_const_pattern]; - const auto mul_or_mul_with_convert = - pattern_map[mul_or_mul_with_convert_pattern]; - const auto ru = std::dynamic_pointer_cast( - random_uniform.get_node_shared_ptr()); - if (!ru) - return false; - - if (pattern_map.count(conv_pattern)) { - const auto mul = pattern_map[mul_with_convert_pattern]; - const auto ml = std::dynamic_pointer_cast( - mul.get_node_shared_ptr()); - if (!ml) - return false; - - const auto conv = pattern_map[conv_pattern]; - const auto cv = std::dynamic_pointer_cast( - conv.get_node_shared_ptr()); - if (!cv) - return false; - const auto new_conv = register_new_node( - ml->input_value(1), ru->get_out_type()); - const auto new_mul = - ml->clone_with_new_inputs({ru->input_value(2), new_conv}); - const auto new_ru = - ru->clone_with_new_inputs({data, ru->input_value(1), new_mul}); - new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); - const auto new_ru_conv = cv->clone_with_new_inputs({new_ru}); - copy_runtime_info({ru, cv}, {new_ru, new_ru_conv}); - ngraph::replace_node(m.get_match_root(), new_ru_conv); - - } else { - const auto mul = pattern_map[mul_pattern]; - const auto ml = std::dynamic_pointer_cast( - mul.get_node_shared_ptr()); - if (!ml) - return false; - const auto new_mul = - ml->clone_with_new_inputs({ru->input_value(2), ml->input_value(1)}); - const auto new_ru = - ru->clone_with_new_inputs({data, ru->input_value(1), new_mul}); - new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); - copy_runtime_info({mul.get_node_shared_ptr(), ru}, {new_mul, new_ru}); - ngraph::replace_node(m.get_match_root(), new_ru); - } - - return true; - }; - - auto m = std::make_shared( - mul_or_mul_with_convert_pattern, matcher_name); - this->register_matcher(m, callback); + MATCHER_SCOPE(RandomUniformMaxValFusion); + const auto data_pattern = ngraph::pattern::any_input(); + const auto ru_min_input_pattern = ngraph::pattern::any_input(); + const auto ru_max_input_pattern = ngraph::pattern::any_input(); + const auto random_uniform_pattern = + ngraph::pattern::wrap_type({data_pattern, ru_min_input_pattern, ru_max_input_pattern}, + pattern::consumers_count(1)); + const auto mul_const_pattern = ngraph::pattern::wrap_type(); + + const auto convert_pattern = ngraph::pattern::wrap_type({random_uniform_pattern}); + const auto random_uniform_or_convert_pattern = + std::make_shared(OutputVector{random_uniform_pattern, convert_pattern}); + + const auto mul_pattern = ngraph::pattern::wrap_type({random_uniform_or_convert_pattern, mul_const_pattern}); + + ngraph::matcher_pass_callback callback = [=](pattern::Matcher& m) { + const auto& pattern_map = m.get_pattern_value_map(); + const auto data = pattern_map.at(data_pattern); + const auto random_uniform = pattern_map.at(random_uniform_pattern); + const auto mul_const = pattern_map.at(mul_const_pattern); + const auto ru = std::dynamic_pointer_cast(random_uniform.get_node_shared_ptr()); + if (!ru) + return false; + + if (pattern_map.count(convert_pattern)) { + const auto mul = pattern_map.at(mul_pattern); + const auto ml = std::dynamic_pointer_cast(mul.get_node_shared_ptr()); + if (!ml) + return false; + + const auto convert = pattern_map.at(convert_pattern); + const auto cvt = std::dynamic_pointer_cast(convert.get_node_shared_ptr()); + if (!cvt) + return false; + const auto new_conv = std::make_shared(ml->input_value(1), ru->get_out_type()); + const auto new_mul = ml->clone_with_new_inputs({ru->input_value(2), new_conv}); + const auto new_ru = ru->clone_with_new_inputs({data, ru->input_value(1), new_mul}); + new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); + const auto new_ru_conv = cvt->clone_with_new_inputs({new_ru}); + copy_runtime_info({ru, cvt}, {new_ru, new_ru_conv}); + ngraph::replace_node(m.get_match_root(), new_ru_conv); + + } else { + const auto mul = pattern_map.at(mul_pattern); + const auto ml = std::dynamic_pointer_cast(mul.get_node_shared_ptr()); + if (!ml) + return false; + const auto new_mul = ml->clone_with_new_inputs({ru->input_value(2), ml->input_value(1)}); + const auto new_ru = ru->clone_with_new_inputs({data, ru->input_value(1), new_mul}); + new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); + copy_runtime_info({mul.get_node_shared_ptr(), ru}, {new_mul, new_ru}); + ngraph::replace_node(m.get_match_root(), new_ru); + } + + return true; + }; + + auto m = std::make_shared(mul_pattern, matcher_name); + this->register_matcher(m, callback); } ngraph::pass::RandomUniformMinValFusion::RandomUniformMinValFusion() { - MATCHER_SCOPE(RandomUniformMinValFusion); - const auto data_pattern = ngraph::pattern::any_input(); - const auto ru_min_input_pattern = ngraph::pattern::any_input(); - const auto ru_max_input_pattern = ngraph::pattern::any_input(); - const auto random_uniform_pattern = - ngraph::pattern::wrap_type( - {data_pattern, ru_min_input_pattern, ru_max_input_pattern}, - pattern::consumers_count(1)); - const auto add_const_pattern = ngraph::pattern::wrap_type(); - const auto add_pattern = ngraph::pattern::wrap_type( - {random_uniform_pattern, add_const_pattern}); - - const auto conv_pattern = - ngraph::pattern::wrap_type({random_uniform_pattern}); - const auto add_with_convert_pattern = ngraph::pattern::wrap_type( - {conv_pattern, add_const_pattern}); - - const auto add_or_add_with_convert_pattern = - std::make_shared( - OutputVector{add_pattern, add_with_convert_pattern}); - - ngraph::matcher_pass_callback callback = [=](pattern::Matcher &m) { - auto pattern_map = m.get_pattern_value_map(); - const auto data = pattern_map[data_pattern]; - const auto random_uniform = pattern_map[random_uniform_pattern]; - const auto add_const = pattern_map[add_const_pattern]; - const auto ru_max_input = pattern_map[ru_max_input_pattern]; - const auto ru_min_input = pattern_map[ru_min_input_pattern]; - const auto ru = std::dynamic_pointer_cast( - random_uniform.get_node_shared_ptr()); - if (!ru) - return false; - - if (pattern_map.count(conv_pattern)) { - const auto add = pattern_map[add_with_convert_pattern]; - const auto conv = pattern_map[conv_pattern]; - const auto cv = std::dynamic_pointer_cast( - conv.get_node_shared_ptr()); - if (!cv) - return false; - const auto add_conv = register_new_node( - add_const, ru->get_out_type()); - const auto add2 = - register_new_node(add_conv, ru_max_input); - const auto add1 = - register_new_node(add_conv, ru_min_input); - const auto new_ru = ru->clone_with_new_inputs({data, add1, add2}); - new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); - const auto new_ru_conv = cv->clone_with_new_inputs({new_ru}); - copy_runtime_info({add.get_node_shared_ptr(), ru, cv}, - {new_ru, new_ru_conv}); - ngraph::replace_node(m.get_match_root(), new_ru_conv); - } else { - const auto add = pattern_map[add_pattern]; - const auto add2 = - register_new_node(add_const, ru_max_input); - const auto add1 = - register_new_node(add_const, ru_min_input); - const auto new_ru = ru->clone_with_new_inputs({data, add1, add2}); - new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); - copy_runtime_info({add.get_node_shared_ptr(), ru}, new_ru); - ngraph::replace_node(m.get_match_root(), new_ru); - } - return true; - }; - - const auto m = std::make_shared( - add_or_add_with_convert_pattern, matcher_name); - this->register_matcher(m, callback); + MATCHER_SCOPE(RandomUniformMinValFusion); + const auto data_pattern = ngraph::pattern::any_input(); + const auto ru_min_input_pattern = ngraph::pattern::any_input(); + const auto ru_max_input_pattern = ngraph::pattern::any_input(); + const auto random_uniform_pattern = + ngraph::pattern::wrap_type({data_pattern, ru_min_input_pattern, ru_max_input_pattern}, + pattern::consumers_count(1)); + const auto add_const_pattern = ngraph::pattern::wrap_type(); + + const auto convert_pattern = ngraph::pattern::wrap_type({random_uniform_pattern}); + const auto convert_or_random_uniform_pattern = + std::make_shared(OutputVector{convert_pattern, random_uniform_pattern}); + const auto add_pattern = ngraph::pattern::wrap_type({convert_or_random_uniform_pattern, add_const_pattern}); + + ngraph::matcher_pass_callback callback = [=](pattern::Matcher& m) { + const auto& pattern_map = m.get_pattern_value_map(); + const auto data = pattern_map.at(data_pattern); + const auto random_uniform = pattern_map.at(random_uniform_pattern); + const auto add_const = pattern_map.at(add_const_pattern); + const auto ru_max_input = pattern_map.at(ru_max_input_pattern); + const auto ru_min_input = pattern_map.at(ru_min_input_pattern); + const auto add = pattern_map.at(add_pattern); + const auto ru = std::dynamic_pointer_cast(random_uniform.get_node_shared_ptr()); + if (!ru) + return false; + + if (pattern_map.count(convert_pattern)) { + const auto convert = pattern_map.at(convert_pattern); + const auto cvt = std::dynamic_pointer_cast(convert.get_node_shared_ptr()); + if (!cvt) + return false; + const auto add_conv = std::make_shared(add_const, ru->get_out_type()); + const auto add2 = std::make_shared(add_conv, ru_max_input); + const auto add1 = std::make_shared(add_conv, ru_min_input); + const auto new_ru = ru->clone_with_new_inputs({data, add1, add2}); + new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); + const auto new_ru_conv = cvt->clone_with_new_inputs({new_ru}); + copy_runtime_info({add.get_node_shared_ptr(), ru, cvt}, {new_ru, new_ru_conv}); + ngraph::replace_node(m.get_match_root(), new_ru_conv); + } else { + const auto add2 = std::make_shared(add_const, ru_max_input); + const auto add1 = std::make_shared(add_const, ru_min_input); + const auto new_ru = ru->clone_with_new_inputs({data, add1, add2}); + new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); + copy_runtime_info({add.get_node_shared_ptr(), ru}, new_ru); + ngraph::replace_node(m.get_match_root(), new_ru); + } + return true; + }; + + const auto m = std::make_shared(add_pattern, matcher_name); + this->register_matcher(m, callback); } diff --git a/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp b/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp index efa5f0afd42189..c8a686e4a75a8d 100644 --- a/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp +++ b/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp @@ -7,131 +7,117 @@ #include #include #include +#include #include #include #include #include #include "common_test_utils/ngraph_test_utils.hpp" -#include using namespace testing; TEST(TransformationTests, RandomUniformMaxValFusing) { - std::shared_ptr f(nullptr), f_ref(nullptr); - { - auto input = std::make_shared( - ngraph::element::i32, ngraph::Shape{3}); - auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, - ngraph::Shape{1}, {0.0}); - auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, - ngraph::Shape{1}, {1.0}); - auto ru = std::make_shared( - input, min_const, max_const, ngraph::element::f32, 100, 200); - - auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f32, - ngraph::Shape{1}, {30.0}); - auto mul = std::make_shared(ru, mul_const); - - f = std::make_shared(ngraph::NodeVector{mul}, - ngraph::ParameterVector{input}); - - ngraph::pass::Manager manager; - manager.register_pass(); - manager.register_pass(); - manager.run_passes(f); - ASSERT_NO_THROW(check_rt_info(f)); - } - - { - auto input = std::make_shared( - ngraph::element::i32, ngraph::Shape{3}); - auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, - ngraph::Shape{1}, {0.0}); - auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, - ngraph::Shape{1}, {1.0}); - - auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f32, - ngraph::Shape{1}, {30.0}); - auto mul = std::make_shared(max_const, mul_const); - auto ru = std::make_shared( - input, min_const, mul, ngraph::element::f32, 100, 200); - - f_ref = std::make_shared(ngraph::NodeVector{ru}, - ngraph::ParameterVector{input}); - } - - auto res = compare_functions(f, f_ref); - ASSERT_TRUE(res.first) << res.second; + std::shared_ptr f(nullptr), f_ref(nullptr); + { + auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {1.0}); + auto ru = std::make_shared(input, + min_const, + max_const, + ngraph::element::f32, + 100, + 200); + + auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {30.0}); + auto mul = std::make_shared(ru, mul_const); + + f = std::make_shared(ngraph::NodeVector{mul}, ngraph::ParameterVector{input}); + + ngraph::pass::Manager manager; + manager.register_pass(); + manager.register_pass(); + manager.run_passes(f); + ASSERT_NO_THROW(check_rt_info(f)); + } + + { + auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {1.0}); + + auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {30.0}); + auto mul = std::make_shared(max_const, mul_const); + auto ru = + std::make_shared(input, min_const, mul, ngraph::element::f32, 100, 200); + + f_ref = std::make_shared(ngraph::NodeVector{ru}, ngraph::ParameterVector{input}); + } + + auto res = compare_functions(f, f_ref); + ASSERT_TRUE(res.first) << res.second; } TEST(TransformationTests, RandomUniformMinValFusing) { - std::shared_ptr f(nullptr), f_ref(nullptr); - { - auto input = std::make_shared( - ngraph::element::i32, ngraph::Shape{3}); - auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, - ngraph::Shape{1}, {0.0}); - auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, - ngraph::Shape{1}, {30.0}); - auto ru = std::make_shared( - input, min_const, max_const, ngraph::element::f32, 100, 200); - - auto add_const = ngraph::opset8::Constant::create( - ngraph::element::f32, ngraph::Shape{1}, {-10.0}); - auto add = std::make_shared(ru, add_const); - - f = std::make_shared(ngraph::NodeVector{add}, - ngraph::ParameterVector{input}); - - ngraph::pass::Manager manager; - manager.register_pass(); - manager.register_pass(); - manager.run_passes(f); - ASSERT_NO_THROW(check_rt_info(f)); - } - - { - auto input = std::make_shared( - ngraph::element::i32, ngraph::Shape{3}); - auto ru_max_const = ngraph::opset8::Constant::create( - ngraph::element::f32, ngraph::Shape{1}, {30.0}); - auto ru_min_const = ngraph::opset8::Constant::create( - ngraph::element::f32, ngraph::Shape{1}, {0.0}); - auto add2_const = ngraph::opset8::Constant::create( - ngraph::element::f32, ngraph::Shape{1}, {-10.0}); - auto add1 = std::make_shared(add2_const, ru_min_const); - auto add2 = std::make_shared(add2_const, ru_max_const); - auto ru = std::make_shared( - input, add1, add2, ngraph::element::f32, 100, 200); - - f_ref = std::make_shared(ngraph::NodeVector{ru}, - ngraph::ParameterVector{input}); - } - - auto res = compare_functions(f, f_ref); - ASSERT_TRUE(res.first) << res.second; + std::shared_ptr f(nullptr), f_ref(nullptr); + { + auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {30.0}); + auto ru = std::make_shared(input, + min_const, + max_const, + ngraph::element::f32, + 100, + 200); + + auto add_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {-10.0}); + auto add = std::make_shared(ru, add_const); + + f = std::make_shared(ngraph::NodeVector{add}, ngraph::ParameterVector{input}); + + ngraph::pass::Manager manager; + manager.register_pass(); + manager.register_pass(); + manager.run_passes(f); + ASSERT_NO_THROW(check_rt_info(f)); + } + + { + auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); + auto ru_max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {30.0}); + auto ru_min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); + auto add2_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {-10.0}); + auto add1 = std::make_shared(add2_const, ru_min_const); + auto add2 = std::make_shared(add2_const, ru_max_const); + auto ru = std::make_shared(input, add1, add2, ngraph::element::f32, 100, 200); + + f_ref = std::make_shared(ngraph::NodeVector{ru}, ngraph::ParameterVector{input}); + } + + auto res = compare_functions(f, f_ref); + ASSERT_TRUE(res.first) << res.second; } TEST(TransformationTests, RandomUniformWithConvertMaxValFusing) { std::shared_ptr f(nullptr), f_ref(nullptr); { - auto input = std::make_shared( - ngraph::element::i32, ngraph::Shape{3}); - auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, - ngraph::Shape{1}, {0.0}); - auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, - ngraph::Shape{1}, {1.0}); - auto ru = std::make_shared( - input, min_const, max_const, ngraph::element::f32, 100, 200); + auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {1.0}); + auto ru = std::make_shared(input, + min_const, + max_const, + ngraph::element::f32, + 100, + 200); auto conv = std::make_shared(ru, ngraph::element::f16); - auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f16, - ngraph::Shape{1}, {30.0}); + auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f16, ngraph::Shape{1}, {30.0}); auto mul = std::make_shared(conv, mul_const); - f = std::make_shared(ngraph::NodeVector{mul}, - ngraph::ParameterVector{input}); + f = std::make_shared(ngraph::NodeVector{mul}, ngraph::ParameterVector{input}); ngraph::pass::Manager manager; manager.register_pass(); @@ -141,26 +127,20 @@ TEST(TransformationTests, RandomUniformWithConvertMaxValFusing) { } { - auto input = std::make_shared( - ngraph::element::i32, ngraph::Shape{3}); - auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, - ngraph::Shape{1}, {0.0}); - auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, - ngraph::Shape{1}, {1.0}); - + auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {1.0}); - auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f16, - ngraph::Shape{1}, {30.0}); + auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f16, ngraph::Shape{1}, {30.0}); auto mul_const_conv = std::make_shared(mul_const, ngraph::element::f32); auto mul = std::make_shared(max_const, mul_const_conv); - auto ru = std::make_shared( - input, min_const, mul, ngraph::element::f32, 100, 200); + auto ru = + std::make_shared(input, min_const, mul, ngraph::element::f32, 100, 200); auto conv = std::make_shared(ru, ngraph::element::f16); - f_ref = std::make_shared(ngraph::NodeVector{conv}, - ngraph::ParameterVector{input}); + f_ref = std::make_shared(ngraph::NodeVector{conv}, ngraph::ParameterVector{input}); } auto res = compare_functions(f, f_ref); @@ -170,22 +150,21 @@ TEST(TransformationTests, RandomUniformWithConvertMaxValFusing) { TEST(TransformationTests, RandomUniformWithConvertMinValFusing) { std::shared_ptr f(nullptr), f_ref(nullptr); { - auto input = std::make_shared( - ngraph::element::i32, ngraph::Shape{3}); - auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, - ngraph::Shape{1}, {0.0}); - auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, - ngraph::Shape{1}, {30.0}); - auto ru = std::make_shared( - input, min_const, max_const, ngraph::element::f32, 100, 200); + auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {30.0}); + auto ru = std::make_shared(input, + min_const, + max_const, + ngraph::element::f32, + 100, + 200); auto conv = std::make_shared(ru, ngraph::element::f16); - auto add_const = ngraph::opset8::Constant::create( - ngraph::element::f16, ngraph::Shape{1}, {-10.0}); + auto add_const = ngraph::opset8::Constant::create(ngraph::element::f16, ngraph::Shape{1}, {-10.0}); auto add = std::make_shared(conv, add_const); - f = std::make_shared(ngraph::NodeVector{add}, - ngraph::ParameterVector{input}); + f = std::make_shared(ngraph::NodeVector{add}, ngraph::ParameterVector{input}); ngraph::pass::Manager manager; manager.register_pass(); @@ -195,24 +174,18 @@ TEST(TransformationTests, RandomUniformWithConvertMinValFusing) { } { - auto input = std::make_shared( - ngraph::element::i32, ngraph::Shape{3}); - auto add_const = ngraph::opset8::Constant::create( - ngraph::element::f16, ngraph::Shape{1}, {-10.0}); + auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); + auto add_const = ngraph::opset8::Constant::create(ngraph::element::f16, ngraph::Shape{1}, {-10.0}); auto conv_const = std::make_shared(add_const, ngraph::element::f32); - auto ru_min_const = ngraph::opset8::Constant::create( - ngraph::element::f32, ngraph::Shape{1}, {0.0}); - auto ru_max_const = ngraph::opset8::Constant::create( - ngraph::element::f32, ngraph::Shape{1}, {30.0}); + auto ru_min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); + auto ru_max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {30.0}); auto add1 = std::make_shared(conv_const, ru_min_const); auto add2 = std::make_shared(conv_const, ru_max_const); - auto ru = std::make_shared( - input, add1, add2, ngraph::element::f32, 100, 200); + auto ru = std::make_shared(input, add1, add2, ngraph::element::f32, 100, 200); auto conv = std::make_shared(ru, ngraph::element::f16); - f_ref = std::make_shared(ngraph::NodeVector{conv}, - ngraph::ParameterVector{input}); + f_ref = std::make_shared(ngraph::NodeVector{conv}, ngraph::ParameterVector{input}); } auto res = compare_functions(f, f_ref); From 4cb096cd779d53bf1881d24ad71d708e049967c7 Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Fri, 10 Sep 2021 09:37:44 +0300 Subject: [PATCH 06/18] Added const shape checks. --- .../random_uniform_fusion.cpp | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp index 4eb733133d8d7a..a0b5389ddf4458 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp @@ -28,26 +28,37 @@ ngraph::pass::RandomUniformMaxValFusion::RandomUniformMaxValFusion() { const auto convert_pattern = ngraph::pattern::wrap_type({random_uniform_pattern}); const auto random_uniform_or_convert_pattern = - std::make_shared(OutputVector{random_uniform_pattern, convert_pattern}); + std::make_shared(OutputVector{random_uniform_pattern, convert_pattern}); - const auto mul_pattern = ngraph::pattern::wrap_type({random_uniform_or_convert_pattern, mul_const_pattern}); + const auto mul_pattern = + ngraph::pattern::wrap_type({random_uniform_or_convert_pattern, mul_const_pattern}); ngraph::matcher_pass_callback callback = [=](pattern::Matcher& m) { const auto& pattern_map = m.get_pattern_value_map(); const auto data = pattern_map.at(data_pattern); const auto random_uniform = pattern_map.at(random_uniform_pattern); - const auto mul_const = pattern_map.at(mul_const_pattern); + const auto mul_constant = pattern_map.at(mul_const_pattern); const auto ru = std::dynamic_pointer_cast(random_uniform.get_node_shared_ptr()); if (!ru) return false; + const auto mul_const = std::dynamic_pointer_cast(mul_constant.get_node_shared_ptr()); + if (!mul_const) + return false; + + auto const_shape = mul_const->get_shape(); + size_t const_shape_size = shape_size(const_shape); + + if (const_shape_size != 1 || const_shape.size() != 1) + return false; + if (pattern_map.count(convert_pattern)) { - const auto mul = pattern_map.at(mul_pattern); + const auto& mul = pattern_map.at(mul_pattern); const auto ml = std::dynamic_pointer_cast(mul.get_node_shared_ptr()); if (!ml) return false; - const auto convert = pattern_map.at(convert_pattern); + const auto& convert = pattern_map.at(convert_pattern); const auto cvt = std::dynamic_pointer_cast(convert.get_node_shared_ptr()); if (!cvt) return false; @@ -60,7 +71,7 @@ ngraph::pass::RandomUniformMaxValFusion::RandomUniformMaxValFusion() { ngraph::replace_node(m.get_match_root(), new_ru_conv); } else { - const auto mul = pattern_map.at(mul_pattern); + const auto& mul = pattern_map.at(mul_pattern); const auto ml = std::dynamic_pointer_cast(mul.get_node_shared_ptr()); if (!ml) return false; @@ -90,23 +101,35 @@ ngraph::pass::RandomUniformMinValFusion::RandomUniformMinValFusion() { const auto convert_pattern = ngraph::pattern::wrap_type({random_uniform_pattern}); const auto convert_or_random_uniform_pattern = - std::make_shared(OutputVector{convert_pattern, random_uniform_pattern}); - const auto add_pattern = ngraph::pattern::wrap_type({convert_or_random_uniform_pattern, add_const_pattern}); + std::make_shared(OutputVector{convert_pattern, random_uniform_pattern}); + const auto add_pattern = + ngraph::pattern::wrap_type({convert_or_random_uniform_pattern, add_const_pattern}); ngraph::matcher_pass_callback callback = [=](pattern::Matcher& m) { const auto& pattern_map = m.get_pattern_value_map(); const auto data = pattern_map.at(data_pattern); const auto random_uniform = pattern_map.at(random_uniform_pattern); - const auto add_const = pattern_map.at(add_const_pattern); + const auto add_constant = pattern_map.at(add_const_pattern); const auto ru_max_input = pattern_map.at(ru_max_input_pattern); const auto ru_min_input = pattern_map.at(ru_min_input_pattern); const auto add = pattern_map.at(add_pattern); const auto ru = std::dynamic_pointer_cast(random_uniform.get_node_shared_ptr()); + + const auto add_const = std::dynamic_pointer_cast(add_constant.get_node_shared_ptr()); + if (!add_const) + return false; + + auto const_shape = add_const->get_shape(); + size_t const_shape_size = shape_size(const_shape); + + if (const_shape_size != 1 || const_shape.size() != 1) + return false; + if (!ru) return false; if (pattern_map.count(convert_pattern)) { - const auto convert = pattern_map.at(convert_pattern); + const auto& convert = pattern_map.at(convert_pattern); const auto cvt = std::dynamic_pointer_cast(convert.get_node_shared_ptr()); if (!cvt) return false; From 5ab01568f8a7823c20a78c42abbd6c402fb3a895 Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Fri, 10 Sep 2021 17:08:07 +0300 Subject: [PATCH 07/18] Fixed transformation for case of different const ranks. --- .../src/moc_transformations.cpp | 4 +- .../common_optimizations.cpp | 4 ++ .../random_uniform_fusion.cpp | 30 +++++---- .../random_uniform_fusion_test.cpp | 64 +++++++++---------- 4 files changed, 56 insertions(+), 46 deletions(-) diff --git a/inference-engine/src/offline_transformations/src/moc_transformations.cpp b/inference-engine/src/offline_transformations/src/moc_transformations.cpp index 445e53a000d7bd..e9488757972791 100644 --- a/inference-engine/src/offline_transformations/src/moc_transformations.cpp +++ b/inference-engine/src/offline_transformations/src/moc_transformations.cpp @@ -92,7 +92,6 @@ bool ngraph::pass::MOCTransformations::run_on_function(std::shared_ptradd_matcher(); common_fusions->add_matcher(); common_fusions->add_matcher(); - common_fusions->add_matcher(); common_fusions->set_name("ngraph::pass::CommonFusions"); manager.register_pass(); @@ -103,6 +102,9 @@ bool ngraph::pass::MOCTransformations::run_on_function(std::shared_ptr(); + // RandomUniformFusion must be executed after LinOpSequenceFusion + manager.register_pass(); + auto conv_fusions = manager.register_pass(); conv_fusions->add_matcher(); conv_fusions->add_matcher(); diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp index af0b44446f7413..d007d272b0e65e 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp @@ -28,6 +28,7 @@ #include "transformations/common_optimizations/convert_quantize_dequantize.hpp" #include "transformations/common_optimizations/relu_fake_quantize_fusion.hpp" #include "transformations/common_optimizations/disable_random_uniform_constant_folding.hpp" +#include "transformations/common_optimizations/random_uniform_fusion.hpp" #include "transformations/common_optimizations/add_fake_quantize_fusion.hpp" #include "transformations/common_optimizations/mul_fake_quantize_fusion.hpp" #include "transformations/common_optimizations/clamp_fusion.hpp" @@ -176,6 +177,9 @@ bool ngraph::pass::CommonOptimizations::run_on_function(std::shared_ptr(); + // RandomUniformFusion must be executed after LinOpSequenceFusion + manager.register_pass(); + auto conv_fusions = manager.register_pass(); conv_fusions->add_matcher(); conv_fusions->add_matcher(); diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp index a0b5389ddf4458..60b006d4fcb119 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp @@ -49,8 +49,10 @@ ngraph::pass::RandomUniformMaxValFusion::RandomUniformMaxValFusion() { auto const_shape = mul_const->get_shape(); size_t const_shape_size = shape_size(const_shape); - if (const_shape_size != 1 || const_shape.size() != 1) + if (const_shape_size != 1) return false; + const auto value = mul_const->cast_vector(); + auto new_mul_const = op::Constant::create(ru->get_out_type(), Shape{}, value); if (pattern_map.count(convert_pattern)) { const auto& mul = pattern_map.at(mul_pattern); @@ -62,9 +64,9 @@ ngraph::pass::RandomUniformMaxValFusion::RandomUniformMaxValFusion() { const auto cvt = std::dynamic_pointer_cast(convert.get_node_shared_ptr()); if (!cvt) return false; - const auto new_conv = std::make_shared(ml->input_value(1), ru->get_out_type()); - const auto new_mul = ml->clone_with_new_inputs({ru->input_value(2), new_conv}); - const auto new_ru = ru->clone_with_new_inputs({data, ru->input_value(1), new_mul}); + const auto new_mul1 = ml->clone_with_new_inputs({ru->input_value(1), new_mul_const}); + const auto new_mul2 = ml->clone_with_new_inputs({ru->input_value(2), new_mul_const}); + const auto new_ru = ru->clone_with_new_inputs({data, new_mul1, new_mul2}); new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); const auto new_ru_conv = cvt->clone_with_new_inputs({new_ru}); copy_runtime_info({ru, cvt}, {new_ru, new_ru_conv}); @@ -75,10 +77,11 @@ ngraph::pass::RandomUniformMaxValFusion::RandomUniformMaxValFusion() { const auto ml = std::dynamic_pointer_cast(mul.get_node_shared_ptr()); if (!ml) return false; - const auto new_mul = ml->clone_with_new_inputs({ru->input_value(2), ml->input_value(1)}); - const auto new_ru = ru->clone_with_new_inputs({data, ru->input_value(1), new_mul}); + const auto new_mul1 = ml->clone_with_new_inputs({ru->input_value(1), new_mul_const}); + const auto new_mul2 = ml->clone_with_new_inputs({ru->input_value(2), new_mul_const}); + const auto new_ru = ru->clone_with_new_inputs({data, new_mul1, new_mul2}); new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); - copy_runtime_info({mul.get_node_shared_ptr(), ru}, {new_mul, new_ru}); + copy_runtime_info({mul.get_node_shared_ptr(), ru}, {new_mul1, new_mul2, new_ru}); ngraph::replace_node(m.get_match_root(), new_ru); } @@ -122,8 +125,10 @@ ngraph::pass::RandomUniformMinValFusion::RandomUniformMinValFusion() { auto const_shape = add_const->get_shape(); size_t const_shape_size = shape_size(const_shape); - if (const_shape_size != 1 || const_shape.size() != 1) + if (const_shape_size != 1) return false; + const auto value = add_const->cast_vector(); + auto new_add_const = op::Constant::create(ru->get_out_type(), Shape{}, value); if (!ru) return false; @@ -133,17 +138,16 @@ ngraph::pass::RandomUniformMinValFusion::RandomUniformMinValFusion() { const auto cvt = std::dynamic_pointer_cast(convert.get_node_shared_ptr()); if (!cvt) return false; - const auto add_conv = std::make_shared(add_const, ru->get_out_type()); - const auto add2 = std::make_shared(add_conv, ru_max_input); - const auto add1 = std::make_shared(add_conv, ru_min_input); + const auto add1 = std::make_shared(ru_min_input, new_add_const); + const auto add2 = std::make_shared(ru_max_input, new_add_const); const auto new_ru = ru->clone_with_new_inputs({data, add1, add2}); new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); const auto new_ru_conv = cvt->clone_with_new_inputs({new_ru}); copy_runtime_info({add.get_node_shared_ptr(), ru, cvt}, {new_ru, new_ru_conv}); ngraph::replace_node(m.get_match_root(), new_ru_conv); } else { - const auto add2 = std::make_shared(add_const, ru_max_input); - const auto add1 = std::make_shared(add_const, ru_min_input); + const auto add1 = std::make_shared(ru_min_input, new_add_const); + const auto add2 = std::make_shared(ru_max_input, new_add_const); const auto new_ru = ru->clone_with_new_inputs({data, add1, add2}); new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); copy_runtime_info({add.get_node_shared_ptr(), ru}, new_ru); diff --git a/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp b/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp index c8a686e4a75a8d..d46792ad53b7f6 100644 --- a/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp +++ b/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp @@ -21,8 +21,8 @@ TEST(TransformationTests, RandomUniformMaxValFusing) { std::shared_ptr f(nullptr), f_ref(nullptr); { auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); - auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); - auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {1.0}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {1.0}); auto ru = std::make_shared(input, min_const, max_const, @@ -30,7 +30,7 @@ TEST(TransformationTests, RandomUniformMaxValFusing) { 100, 200); - auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {30.0}); + auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1, 1, 1}, {30.0}); auto mul = std::make_shared(ru, mul_const); f = std::make_shared(ngraph::NodeVector{mul}, ngraph::ParameterVector{input}); @@ -44,13 +44,14 @@ TEST(TransformationTests, RandomUniformMaxValFusing) { { auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); - auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); - auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {1.0}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {1.0}); - auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {30.0}); - auto mul = std::make_shared(max_const, mul_const); + auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {30.0}); + auto mul1 = std::make_shared(min_const, mul_const); + auto mul2 = std::make_shared(max_const, mul_const); auto ru = - std::make_shared(input, min_const, mul, ngraph::element::f32, 100, 200); + std::make_shared(input, mul1, mul2, ngraph::element::f32, 100, 200); f_ref = std::make_shared(ngraph::NodeVector{ru}, ngraph::ParameterVector{input}); } @@ -63,8 +64,8 @@ TEST(TransformationTests, RandomUniformMinValFusing) { std::shared_ptr f(nullptr), f_ref(nullptr); { auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); - auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); - auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {30.0}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {30.0}); auto ru = std::make_shared(input, min_const, max_const, @@ -72,7 +73,7 @@ TEST(TransformationTests, RandomUniformMinValFusing) { 100, 200); - auto add_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {-10.0}); + auto add_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1, 1, 1, 1}, {-10.0}); auto add = std::make_shared(ru, add_const); f = std::make_shared(ngraph::NodeVector{add}, ngraph::ParameterVector{input}); @@ -86,9 +87,9 @@ TEST(TransformationTests, RandomUniformMinValFusing) { { auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); - auto ru_max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {30.0}); - auto ru_min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); - auto add2_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {-10.0}); + auto ru_max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {30.0}); + auto ru_min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {0.0}); + auto add2_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {-10.0}); auto add1 = std::make_shared(add2_const, ru_min_const); auto add2 = std::make_shared(add2_const, ru_max_const); auto ru = std::make_shared(input, add1, add2, ngraph::element::f32, 100, 200); @@ -104,8 +105,8 @@ TEST(TransformationTests, RandomUniformWithConvertMaxValFusing) { std::shared_ptr f(nullptr), f_ref(nullptr); { auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); - auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); - auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {1.0}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {1.0}); auto ru = std::make_shared(input, min_const, max_const, @@ -114,7 +115,7 @@ TEST(TransformationTests, RandomUniformWithConvertMaxValFusing) { 200); auto conv = std::make_shared(ru, ngraph::element::f16); - auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f16, ngraph::Shape{1}, {30.0}); + auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f16, ngraph::Shape{}, {30.0}); auto mul = std::make_shared(conv, mul_const); f = std::make_shared(ngraph::NodeVector{mul}, ngraph::ParameterVector{input}); @@ -128,16 +129,16 @@ TEST(TransformationTests, RandomUniformWithConvertMaxValFusing) { { auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); - auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); - auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {1.0}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {1.0}); - auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f16, ngraph::Shape{1}, {30.0}); - auto mul_const_conv = std::make_shared(mul_const, ngraph::element::f32); + auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {30.0}); - auto mul = std::make_shared(max_const, mul_const_conv); + auto mul1 = std::make_shared(min_const, mul_const); + auto mul2 = std::make_shared(max_const, mul_const); auto ru = - std::make_shared(input, min_const, mul, ngraph::element::f32, 100, 200); + std::make_shared(input, mul1, mul2, ngraph::element::f32, 100, 200); auto conv = std::make_shared(ru, ngraph::element::f16); f_ref = std::make_shared(ngraph::NodeVector{conv}, ngraph::ParameterVector{input}); @@ -151,8 +152,8 @@ TEST(TransformationTests, RandomUniformWithConvertMinValFusing) { std::shared_ptr f(nullptr), f_ref(nullptr); { auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); - auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); - auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {30.0}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {30.0}); auto ru = std::make_shared(input, min_const, max_const, @@ -161,7 +162,7 @@ TEST(TransformationTests, RandomUniformWithConvertMinValFusing) { 200); auto conv = std::make_shared(ru, ngraph::element::f16); - auto add_const = ngraph::opset8::Constant::create(ngraph::element::f16, ngraph::Shape{1}, {-10.0}); + auto add_const = ngraph::opset8::Constant::create(ngraph::element::f16, ngraph::Shape{}, {-10.0}); auto add = std::make_shared(conv, add_const); f = std::make_shared(ngraph::NodeVector{add}, ngraph::ParameterVector{input}); @@ -175,12 +176,11 @@ TEST(TransformationTests, RandomUniformWithConvertMinValFusing) { { auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); - auto add_const = ngraph::opset8::Constant::create(ngraph::element::f16, ngraph::Shape{1}, {-10.0}); - auto conv_const = std::make_shared(add_const, ngraph::element::f32); - auto ru_min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.0}); - auto ru_max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {30.0}); - auto add1 = std::make_shared(conv_const, ru_min_const); - auto add2 = std::make_shared(conv_const, ru_max_const); + auto add_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {-10.0}); + auto ru_min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {0.0}); + auto ru_max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {30.0}); + auto add1 = std::make_shared(add_const, ru_min_const); + auto add2 = std::make_shared(add_const, ru_max_const); auto ru = std::make_shared(input, add1, add2, ngraph::element::f32, 100, 200); auto conv = std::make_shared(ru, ngraph::element::f16); From b805c5345a4b94409084087a2486aa215e8059c0 Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Mon, 13 Sep 2021 11:20:17 +0300 Subject: [PATCH 08/18] Added type checks. --- .../random_uniform_fusion.hpp | 16 ++++++------- .../random_uniform_fusion.cpp | 24 ++++++++++++++----- .../random_uniform_fusion_test.cpp | 16 ++++++------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp b/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp index 8cd05d28f9e271..0a692d09e1c503 100644 --- a/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp +++ b/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp @@ -11,8 +11,8 @@ namespace ngraph { namespace pass { class TRANSFORMATIONS_API RandomUniformFusion; -class TRANSFORMATIONS_API RandomUniformMaxValFusion; -class TRANSFORMATIONS_API RandomUniformMinValFusion; +class TRANSFORMATIONS_API RandomUniformMulFusion; +class TRANSFORMATIONS_API RandomUniformAddFusion; } // namespace pass } // namespace ngraph @@ -23,10 +23,10 @@ class TRANSFORMATIONS_API RandomUniformMinValFusion; * with a single RandomUniform node. * */ -class ngraph::pass::RandomUniformMaxValFusion : public ngraph::pass::MatcherPass { +class ngraph::pass::RandomUniformMulFusion : public ngraph::pass::MatcherPass { public: NGRAPH_RTTI_DECLARATION; - RandomUniformMaxValFusion(); + RandomUniformMulFusion(); }; /** @@ -35,10 +35,10 @@ class ngraph::pass::RandomUniformMaxValFusion : public ngraph::pass::MatcherPass * with a RandomUniform and replaces min and max const with corrected values. * */ -class ngraph::pass::RandomUniformMinValFusion : public ngraph::pass::MatcherPass { +class ngraph::pass::RandomUniformAddFusion : public ngraph::pass::MatcherPass { public: NGRAPH_RTTI_DECLARATION; - RandomUniformMinValFusion(); + RandomUniformAddFusion(); }; /** @@ -50,7 +50,7 @@ class ngraph::pass::RandomUniformFusion : public ngraph::pass::GraphRewrite { public: NGRAPH_RTTI_DECLARATION; RandomUniformFusion() { - add_matcher(); - add_matcher(); + add_matcher(); + add_matcher(); } }; diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp index 60b006d4fcb119..c14d04cee86275 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp @@ -13,11 +13,11 @@ #include "itt.hpp" NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformFusion, "RandomUniformFusion", 0); -NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMaxValFusion, "RandomUniformMaxValFusion", 0); -NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMinValFusion, "RandomUniformMinValFusion", 0); +NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMulFusion, "RandomUniformMulFusion", 0); +NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformAddFusion, "RandomUniformAddFusion", 0); -ngraph::pass::RandomUniformMaxValFusion::RandomUniformMaxValFusion() { - MATCHER_SCOPE(RandomUniformMaxValFusion); +ngraph::pass::RandomUniformMulFusion::RandomUniformMulFusion() { + MATCHER_SCOPE(RandomUniformMulFusion); const auto data_pattern = ngraph::pattern::any_input(); const auto ru_min_input_pattern = ngraph::pattern::any_input(); const auto ru_max_input_pattern = ngraph::pattern::any_input(); @@ -41,10 +41,14 @@ ngraph::pass::RandomUniformMaxValFusion::RandomUniformMaxValFusion() { const auto ru = std::dynamic_pointer_cast(random_uniform.get_node_shared_ptr()); if (!ru) return false; + if (!ru->get_out_type().is_real()) + return false; const auto mul_const = std::dynamic_pointer_cast(mul_constant.get_node_shared_ptr()); if (!mul_const) return false; + if (!mul_const->get_element_type().is_real()) + return false; auto const_shape = mul_const->get_shape(); size_t const_shape_size = shape_size(const_shape); @@ -64,6 +68,8 @@ ngraph::pass::RandomUniformMaxValFusion::RandomUniformMaxValFusion() { const auto cvt = std::dynamic_pointer_cast(convert.get_node_shared_ptr()); if (!cvt) return false; + if (!cvt->get_element_type().is_real()) + return false; const auto new_mul1 = ml->clone_with_new_inputs({ru->input_value(1), new_mul_const}); const auto new_mul2 = ml->clone_with_new_inputs({ru->input_value(2), new_mul_const}); const auto new_ru = ru->clone_with_new_inputs({data, new_mul1, new_mul2}); @@ -92,8 +98,8 @@ ngraph::pass::RandomUniformMaxValFusion::RandomUniformMaxValFusion() { this->register_matcher(m, callback); } -ngraph::pass::RandomUniformMinValFusion::RandomUniformMinValFusion() { - MATCHER_SCOPE(RandomUniformMinValFusion); +ngraph::pass::RandomUniformAddFusion::RandomUniformAddFusion() { + MATCHER_SCOPE(RandomUniformAddFusion); const auto data_pattern = ngraph::pattern::any_input(); const auto ru_min_input_pattern = ngraph::pattern::any_input(); const auto ru_max_input_pattern = ngraph::pattern::any_input(); @@ -121,6 +127,8 @@ ngraph::pass::RandomUniformMinValFusion::RandomUniformMinValFusion() { const auto add_const = std::dynamic_pointer_cast(add_constant.get_node_shared_ptr()); if (!add_const) return false; + if (!add_const->get_element_type().is_real()) + return false; auto const_shape = add_const->get_shape(); size_t const_shape_size = shape_size(const_shape); @@ -132,12 +140,16 @@ ngraph::pass::RandomUniformMinValFusion::RandomUniformMinValFusion() { if (!ru) return false; + if (!ru->get_out_type().is_real()) + return false; if (pattern_map.count(convert_pattern)) { const auto& convert = pattern_map.at(convert_pattern); const auto cvt = std::dynamic_pointer_cast(convert.get_node_shared_ptr()); if (!cvt) return false; + if (!cvt->get_element_type().is_real()) + return false; const auto add1 = std::make_shared(ru_min_input, new_add_const); const auto add2 = std::make_shared(ru_max_input, new_add_const); const auto new_ru = ru->clone_with_new_inputs({data, add1, add2}); diff --git a/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp b/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp index d46792ad53b7f6..f82d0d45b9c6cd 100644 --- a/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp +++ b/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp @@ -17,7 +17,7 @@ using namespace testing; -TEST(TransformationTests, RandomUniformMaxValFusing) { +TEST(TransformationTests, RandomUniformMulFusing) { std::shared_ptr f(nullptr), f_ref(nullptr); { auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); @@ -37,7 +37,7 @@ TEST(TransformationTests, RandomUniformMaxValFusing) { ngraph::pass::Manager manager; manager.register_pass(); - manager.register_pass(); + manager.register_pass(); manager.run_passes(f); ASSERT_NO_THROW(check_rt_info(f)); } @@ -60,7 +60,7 @@ TEST(TransformationTests, RandomUniformMaxValFusing) { ASSERT_TRUE(res.first) << res.second; } -TEST(TransformationTests, RandomUniformMinValFusing) { +TEST(TransformationTests, RandomUniformAddFusing) { std::shared_ptr f(nullptr), f_ref(nullptr); { auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); @@ -80,7 +80,7 @@ TEST(TransformationTests, RandomUniformMinValFusing) { ngraph::pass::Manager manager; manager.register_pass(); - manager.register_pass(); + manager.register_pass(); manager.run_passes(f); ASSERT_NO_THROW(check_rt_info(f)); } @@ -101,7 +101,7 @@ TEST(TransformationTests, RandomUniformMinValFusing) { ASSERT_TRUE(res.first) << res.second; } -TEST(TransformationTests, RandomUniformWithConvertMaxValFusing) { +TEST(TransformationTests, RandomUniformWithConvertMulFusing) { std::shared_ptr f(nullptr), f_ref(nullptr); { auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); @@ -122,7 +122,7 @@ TEST(TransformationTests, RandomUniformWithConvertMaxValFusing) { ngraph::pass::Manager manager; manager.register_pass(); - manager.register_pass(); + manager.register_pass(); manager.run_passes(f); ASSERT_NO_THROW(check_rt_info(f)); } @@ -148,7 +148,7 @@ TEST(TransformationTests, RandomUniformWithConvertMaxValFusing) { ASSERT_TRUE(res.first) << res.second; } -TEST(TransformationTests, RandomUniformWithConvertMinValFusing) { +TEST(TransformationTests, RandomUniformWithConvertAddFusing) { std::shared_ptr f(nullptr), f_ref(nullptr); { auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); @@ -169,7 +169,7 @@ TEST(TransformationTests, RandomUniformWithConvertMinValFusing) { ngraph::pass::Manager manager; manager.register_pass(); - manager.register_pass(); + manager.register_pass(); manager.run_passes(f); ASSERT_NO_THROW(check_rt_info(f)); } From 13cba4f55dc755d9af95ef75db7212d4919be496 Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Tue, 14 Sep 2021 13:07:38 +0300 Subject: [PATCH 09/18] Apply suggestions from code review Co-authored-by: Gleb Kazantaev --- .../common_optimizations/random_uniform_fusion.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp index c14d04cee86275..ff05f3432222ae 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp @@ -51,11 +51,9 @@ ngraph::pass::RandomUniformMulFusion::RandomUniformMulFusion() { return false; auto const_shape = mul_const->get_shape(); - size_t const_shape_size = shape_size(const_shape); - - if (const_shape_size != 1) + if (shape_size(const_shape) != 1) return false; - const auto value = mul_const->cast_vector(); + const auto & value = mul_const->cast_vector(); auto new_mul_const = op::Constant::create(ru->get_out_type(), Shape{}, value); if (pattern_map.count(convert_pattern)) { From 2f907cce16c6063fdf8f8b46a0df98727836d442 Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Tue, 14 Sep 2021 15:00:12 +0300 Subject: [PATCH 10/18] United RandomUniformMulFusion and RandomUniformAddFusion to single transformation. --- .../random_uniform_fusion.hpp | 28 +--- .../random_uniform_fusion.cpp | 131 +++--------------- .../random_uniform_fusion_test.cpp | 8 +- 3 files changed, 34 insertions(+), 133 deletions(-) diff --git a/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp b/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp index 0a692d09e1c503..cd1b800ddee21b 100644 --- a/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp +++ b/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp @@ -11,34 +11,21 @@ namespace ngraph { namespace pass { class TRANSFORMATIONS_API RandomUniformFusion; -class TRANSFORMATIONS_API RandomUniformMulFusion; -class TRANSFORMATIONS_API RandomUniformAddFusion; +class TRANSFORMATIONS_API RandomUniformMulAddFusion; } // namespace pass } // namespace ngraph /** * @ingroup ie_transformation_common_api - * @brief RandomUniformMaxValFusion transformation replaces RandomUniform -> Mul subgraph - * with a single RandomUniform node. - * + * @brief RandomUniformMulAddFusion transformation replaces RandomUniform -> Add or + * RandomUniform -> Mul subgraph with a RandomUniform and replaces min and max const + * with corrected values. */ -class ngraph::pass::RandomUniformMulFusion : public ngraph::pass::MatcherPass { +class ngraph::pass::RandomUniformMulAddFusion : public ngraph::pass::MatcherPass { public: NGRAPH_RTTI_DECLARATION; - RandomUniformMulFusion(); -}; - -/** - * @ingroup ie_transformation_common_api - * @brief RandomUniformMinValFusion transformation replaces RandomUniform -> Add subgraph - * with a RandomUniform and replaces min and max const with corrected values. - * - */ -class ngraph::pass::RandomUniformAddFusion : public ngraph::pass::MatcherPass { -public: - NGRAPH_RTTI_DECLARATION; - RandomUniformAddFusion(); + RandomUniformMulAddFusion(); }; /** @@ -50,7 +37,6 @@ class ngraph::pass::RandomUniformFusion : public ngraph::pass::GraphRewrite { public: NGRAPH_RTTI_DECLARATION; RandomUniformFusion() { - add_matcher(); - add_matcher(); + add_matcher(); } }; diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp index c14d04cee86275..5408c67b555a37 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp @@ -13,161 +13,76 @@ #include "itt.hpp" NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformFusion, "RandomUniformFusion", 0); -NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMulFusion, "RandomUniformMulFusion", 0); -NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformAddFusion, "RandomUniformAddFusion", 0); +NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMulAddFusion, "RandomUniformMulAddFusion", 0); -ngraph::pass::RandomUniformMulFusion::RandomUniformMulFusion() { - MATCHER_SCOPE(RandomUniformMulFusion); +ngraph::pass::RandomUniformMulAddFusion::RandomUniformMulAddFusion() { + MATCHER_SCOPE(RandomUniformMulAddFusion); const auto data_pattern = ngraph::pattern::any_input(); const auto ru_min_input_pattern = ngraph::pattern::any_input(); const auto ru_max_input_pattern = ngraph::pattern::any_input(); const auto random_uniform_pattern = ngraph::pattern::wrap_type({data_pattern, ru_min_input_pattern, ru_max_input_pattern}, pattern::consumers_count(1)); - const auto mul_const_pattern = ngraph::pattern::wrap_type(); + const auto const_pattern = ngraph::pattern::wrap_type(); const auto convert_pattern = ngraph::pattern::wrap_type({random_uniform_pattern}); const auto random_uniform_or_convert_pattern = std::make_shared(OutputVector{random_uniform_pattern, convert_pattern}); - const auto mul_pattern = - ngraph::pattern::wrap_type({random_uniform_or_convert_pattern, mul_const_pattern}); + const auto mul_add_pattern = + ngraph::pattern::wrap_type({random_uniform_or_convert_pattern, const_pattern}); ngraph::matcher_pass_callback callback = [=](pattern::Matcher& m) { const auto& pattern_map = m.get_pattern_value_map(); const auto data = pattern_map.at(data_pattern); const auto random_uniform = pattern_map.at(random_uniform_pattern); - const auto mul_constant = pattern_map.at(mul_const_pattern); + const auto constant = pattern_map.at(const_pattern); const auto ru = std::dynamic_pointer_cast(random_uniform.get_node_shared_ptr()); if (!ru) return false; if (!ru->get_out_type().is_real()) return false; - const auto mul_const = std::dynamic_pointer_cast(mul_constant.get_node_shared_ptr()); - if (!mul_const) + const auto old_const = std::dynamic_pointer_cast(constant.get_node_shared_ptr()); + if (!old_const) return false; - if (!mul_const->get_element_type().is_real()) + if (!old_const->get_element_type().is_real()) return false; - auto const_shape = mul_const->get_shape(); + auto const_shape = old_const->get_shape(); size_t const_shape_size = shape_size(const_shape); if (const_shape_size != 1) return false; - const auto value = mul_const->cast_vector(); - auto new_mul_const = op::Constant::create(ru->get_out_type(), Shape{}, value); + const auto value = old_const->cast_vector(); + auto new_const = op::Constant::create(ru->get_out_type(), Shape{}, value); - if (pattern_map.count(convert_pattern)) { - const auto& mul = pattern_map.at(mul_pattern); - const auto ml = std::dynamic_pointer_cast(mul.get_node_shared_ptr()); - if (!ml) - return false; + const auto& mul_add = pattern_map.at(mul_add_pattern); + const auto mul_add_ptr = std::dynamic_pointer_cast(mul_add.get_node_shared_ptr()); + const auto new_mul_add1 = mul_add_ptr->clone_with_new_inputs({ru->input_value(1), new_const}); + const auto new_mul_add2 = mul_add_ptr->clone_with_new_inputs({ru->input_value(2), new_const}); + const auto new_ru = ru->clone_with_new_inputs({data, new_mul_add1, new_mul_add2}); + new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); + if (pattern_map.count(convert_pattern)) { const auto& convert = pattern_map.at(convert_pattern); const auto cvt = std::dynamic_pointer_cast(convert.get_node_shared_ptr()); if (!cvt) return false; if (!cvt->get_element_type().is_real()) return false; - const auto new_mul1 = ml->clone_with_new_inputs({ru->input_value(1), new_mul_const}); - const auto new_mul2 = ml->clone_with_new_inputs({ru->input_value(2), new_mul_const}); - const auto new_ru = ru->clone_with_new_inputs({data, new_mul1, new_mul2}); - new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); const auto new_ru_conv = cvt->clone_with_new_inputs({new_ru}); - copy_runtime_info({ru, cvt}, {new_ru, new_ru_conv}); + copy_runtime_info({ru, cvt, mul_add.get_node_shared_ptr()}, {new_mul_add1, new_mul_add2, new_ru, new_ru_conv}); ngraph::replace_node(m.get_match_root(), new_ru_conv); } else { - const auto& mul = pattern_map.at(mul_pattern); - const auto ml = std::dynamic_pointer_cast(mul.get_node_shared_ptr()); - if (!ml) - return false; - const auto new_mul1 = ml->clone_with_new_inputs({ru->input_value(1), new_mul_const}); - const auto new_mul2 = ml->clone_with_new_inputs({ru->input_value(2), new_mul_const}); - const auto new_ru = ru->clone_with_new_inputs({data, new_mul1, new_mul2}); - new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); - copy_runtime_info({mul.get_node_shared_ptr(), ru}, {new_mul1, new_mul2, new_ru}); + copy_runtime_info({ru, mul_add.get_node_shared_ptr()}, {new_mul_add1, new_mul_add2, new_ru}); ngraph::replace_node(m.get_match_root(), new_ru); } return true; }; - auto m = std::make_shared(mul_pattern, matcher_name); - this->register_matcher(m, callback); -} - -ngraph::pass::RandomUniformAddFusion::RandomUniformAddFusion() { - MATCHER_SCOPE(RandomUniformAddFusion); - const auto data_pattern = ngraph::pattern::any_input(); - const auto ru_min_input_pattern = ngraph::pattern::any_input(); - const auto ru_max_input_pattern = ngraph::pattern::any_input(); - const auto random_uniform_pattern = - ngraph::pattern::wrap_type({data_pattern, ru_min_input_pattern, ru_max_input_pattern}, - pattern::consumers_count(1)); - const auto add_const_pattern = ngraph::pattern::wrap_type(); - - const auto convert_pattern = ngraph::pattern::wrap_type({random_uniform_pattern}); - const auto convert_or_random_uniform_pattern = - std::make_shared(OutputVector{convert_pattern, random_uniform_pattern}); - const auto add_pattern = - ngraph::pattern::wrap_type({convert_or_random_uniform_pattern, add_const_pattern}); - - ngraph::matcher_pass_callback callback = [=](pattern::Matcher& m) { - const auto& pattern_map = m.get_pattern_value_map(); - const auto data = pattern_map.at(data_pattern); - const auto random_uniform = pattern_map.at(random_uniform_pattern); - const auto add_constant = pattern_map.at(add_const_pattern); - const auto ru_max_input = pattern_map.at(ru_max_input_pattern); - const auto ru_min_input = pattern_map.at(ru_min_input_pattern); - const auto add = pattern_map.at(add_pattern); - const auto ru = std::dynamic_pointer_cast(random_uniform.get_node_shared_ptr()); - - const auto add_const = std::dynamic_pointer_cast(add_constant.get_node_shared_ptr()); - if (!add_const) - return false; - if (!add_const->get_element_type().is_real()) - return false; - - auto const_shape = add_const->get_shape(); - size_t const_shape_size = shape_size(const_shape); - - if (const_shape_size != 1) - return false; - const auto value = add_const->cast_vector(); - auto new_add_const = op::Constant::create(ru->get_out_type(), Shape{}, value); - - if (!ru) - return false; - if (!ru->get_out_type().is_real()) - return false; - - if (pattern_map.count(convert_pattern)) { - const auto& convert = pattern_map.at(convert_pattern); - const auto cvt = std::dynamic_pointer_cast(convert.get_node_shared_ptr()); - if (!cvt) - return false; - if (!cvt->get_element_type().is_real()) - return false; - const auto add1 = std::make_shared(ru_min_input, new_add_const); - const auto add2 = std::make_shared(ru_max_input, new_add_const); - const auto new_ru = ru->clone_with_new_inputs({data, add1, add2}); - new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); - const auto new_ru_conv = cvt->clone_with_new_inputs({new_ru}); - copy_runtime_info({add.get_node_shared_ptr(), ru, cvt}, {new_ru, new_ru_conv}); - ngraph::replace_node(m.get_match_root(), new_ru_conv); - } else { - const auto add1 = std::make_shared(ru_min_input, new_add_const); - const auto add2 = std::make_shared(ru_max_input, new_add_const); - const auto new_ru = ru->clone_with_new_inputs({data, add1, add2}); - new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); - copy_runtime_info({add.get_node_shared_ptr(), ru}, new_ru); - ngraph::replace_node(m.get_match_root(), new_ru); - } - return true; - }; - - const auto m = std::make_shared(add_pattern, matcher_name); + auto m = std::make_shared(mul_add_pattern, matcher_name); this->register_matcher(m, callback); } diff --git a/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp b/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp index f82d0d45b9c6cd..b7ae2297254525 100644 --- a/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp +++ b/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp @@ -37,7 +37,7 @@ TEST(TransformationTests, RandomUniformMulFusing) { ngraph::pass::Manager manager; manager.register_pass(); - manager.register_pass(); + manager.register_pass(); manager.run_passes(f); ASSERT_NO_THROW(check_rt_info(f)); } @@ -80,7 +80,7 @@ TEST(TransformationTests, RandomUniformAddFusing) { ngraph::pass::Manager manager; manager.register_pass(); - manager.register_pass(); + manager.register_pass(); manager.run_passes(f); ASSERT_NO_THROW(check_rt_info(f)); } @@ -122,7 +122,7 @@ TEST(TransformationTests, RandomUniformWithConvertMulFusing) { ngraph::pass::Manager manager; manager.register_pass(); - manager.register_pass(); + manager.register_pass(); manager.run_passes(f); ASSERT_NO_THROW(check_rt_info(f)); } @@ -169,7 +169,7 @@ TEST(TransformationTests, RandomUniformWithConvertAddFusing) { ngraph::pass::Manager manager; manager.register_pass(); - manager.register_pass(); + manager.register_pass(); manager.run_passes(f); ASSERT_NO_THROW(check_rt_info(f)); } From 9882a9a2a38edad194bab331e857e1269af9d6bf Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Tue, 14 Sep 2021 16:42:12 +0300 Subject: [PATCH 11/18] Added negative tests. --- .../random_uniform_fusion.cpp | 5 +- .../random_uniform_fusion_test.cpp | 96 ++++++++++++++++++- 2 files changed, 97 insertions(+), 4 deletions(-) diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp index 5408c67b555a37..de984aa33788d1 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp @@ -50,10 +50,9 @@ ngraph::pass::RandomUniformMulAddFusion::RandomUniformMulAddFusion() { return false; auto const_shape = old_const->get_shape(); - size_t const_shape_size = shape_size(const_shape); - - if (const_shape_size != 1) + if (shape_size(const_shape) != 1) return false; + const auto value = old_const->cast_vector(); auto new_const = op::Constant::create(ru->get_out_type(), Shape{}, value); diff --git a/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp b/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp index b7ae2297254525..1ddfd800eadd5f 100644 --- a/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp +++ b/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp @@ -190,4 +190,98 @@ TEST(TransformationTests, RandomUniformWithConvertAddFusing) { auto res = compare_functions(f, f_ref); ASSERT_TRUE(res.first) << res.second; -} \ No newline at end of file +} + + +TEST(TransformationTests, RandomUniformFusingInvalidRUType) { + std::shared_ptr f(nullptr), f_ref(nullptr); + { + auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::i32, ngraph::Shape{}, {0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::i32, ngraph::Shape{}, {100}); + auto ru = std::make_shared(input, + min_const, + max_const, + ngraph::element::i32, + 100, + 200); + + auto mul_const = ngraph::opset8::Constant::create(ngraph::element::i32, ngraph::Shape{}, {30}); + auto mul = std::make_shared(ru, mul_const); + + f = std::make_shared(ngraph::NodeVector{mul}, ngraph::ParameterVector{input}); + + ngraph::pass::Manager manager; + manager.register_pass(); + manager.register_pass(); + manager.run_passes(f); + ASSERT_NO_THROW(check_rt_info(f)); + } + + { + auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::i32, ngraph::Shape{}, {0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::i32, ngraph::Shape{}, {100}); + auto ru = std::make_shared(input, + min_const, + max_const, + ngraph::element::i32, + 100, + 200); + + auto mul_const = ngraph::opset8::Constant::create(ngraph::element::i32, ngraph::Shape{}, {30}); + auto mul = std::make_shared(ru, mul_const); + + f_ref = std::make_shared(ngraph::NodeVector{mul}, ngraph::ParameterVector{input}); + } + + auto res = compare_functions(f, f_ref); + ASSERT_TRUE(res.first) << res.second; +} + + +TEST(TransformationTests, RandomUniformFusingInvalidConstShape) { + std::shared_ptr f(nullptr), f_ref(nullptr); + { + auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {1.0}); + auto ru = std::make_shared(input, + min_const, + max_const, + ngraph::element::f32, + 100, + 200); + + auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{3}, {30, 20, 15}); + auto mul = std::make_shared(ru, mul_const); + + f = std::make_shared(ngraph::NodeVector{mul}, ngraph::ParameterVector{input}); + + ngraph::pass::Manager manager; + manager.register_pass(); + manager.register_pass(); + manager.run_passes(f); + ASSERT_NO_THROW(check_rt_info(f)); + } + + { + auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); + auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {0.0}); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {1.0}); + auto ru = std::make_shared(input, + min_const, + max_const, + ngraph::element::f32, + 100, + 200); + + auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{3}, {30, 20, 15}); + auto mul = std::make_shared(ru, mul_const); + + f_ref = std::make_shared(ngraph::NodeVector{mul}, ngraph::ParameterVector{input}); + } + + auto res = compare_functions(f, f_ref); + ASSERT_TRUE(res.first) << res.second; +} From 8f5b0beb921a3f32ee59d58136679324038558b2 Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Tue, 14 Sep 2021 16:58:39 +0300 Subject: [PATCH 12/18] Used get_constant_from_source(). --- .../random_uniform_fusion.cpp | 11 +++- .../random_uniform_fusion_test.cpp | 57 ++++++++++--------- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp index de984aa33788d1..05ca757950b3f3 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "itt.hpp" @@ -53,14 +54,20 @@ ngraph::pass::RandomUniformMulAddFusion::RandomUniformMulAddFusion() { if (shape_size(const_shape) != 1) return false; - const auto value = old_const->cast_vector(); + const auto& value = old_const->cast_vector(); auto new_const = op::Constant::create(ru->get_out_type(), Shape{}, value); const auto& mul_add = pattern_map.at(mul_add_pattern); const auto mul_add_ptr = std::dynamic_pointer_cast(mul_add.get_node_shared_ptr()); const auto new_mul_add1 = mul_add_ptr->clone_with_new_inputs({ru->input_value(1), new_const}); const auto new_mul_add2 = mul_add_ptr->clone_with_new_inputs({ru->input_value(2), new_const}); - const auto new_ru = ru->clone_with_new_inputs({data, new_mul_add1, new_mul_add2}); + + auto folded_const1 = ngraph::get_constant_from_source(new_mul_add1); + auto folded_const2 = ngraph::get_constant_from_source(new_mul_add2); + + const auto new_ru = ru->clone_with_new_inputs({data, + folded_const1 ? folded_const1 : new_mul_add1, + folded_const2 ? folded_const2 : new_mul_add2}); new_ru->set_friendly_name(m.get_match_root()->get_friendly_name()); if (pattern_map.count(convert_pattern)) { diff --git a/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp b/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp index 1ddfd800eadd5f..36f4ba84542859 100644 --- a/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp +++ b/inference-engine/tests/functional/inference_engine/transformations/random_uniform_fusion_test.cpp @@ -45,13 +45,13 @@ TEST(TransformationTests, RandomUniformMulFusing) { { auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {0.0}); - auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {1.0}); - - auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {30.0}); - auto mul1 = std::make_shared(min_const, mul_const); - auto mul2 = std::make_shared(max_const, mul_const); - auto ru = - std::make_shared(input, mul1, mul2, ngraph::element::f32, 100, 200); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {30.0}); + auto ru = std::make_shared(input, + min_const, + max_const, + ngraph::element::f32, + 100, + 200); f_ref = std::make_shared(ngraph::NodeVector{ru}, ngraph::ParameterVector{input}); } @@ -87,12 +87,14 @@ TEST(TransformationTests, RandomUniformAddFusing) { { auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); - auto ru_max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {30.0}); - auto ru_min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {0.0}); - auto add2_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {-10.0}); - auto add1 = std::make_shared(add2_const, ru_min_const); - auto add2 = std::make_shared(add2_const, ru_max_const); - auto ru = std::make_shared(input, add1, add2, ngraph::element::f32, 100, 200); + auto ru_max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {20.0}); + auto ru_min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {-10.0}); + auto ru = std::make_shared(input, + ru_min_const, + ru_max_const, + ngraph::element::f32, + 100, + 200); f_ref = std::make_shared(ngraph::NodeVector{ru}, ngraph::ParameterVector{input}); } @@ -130,15 +132,14 @@ TEST(TransformationTests, RandomUniformWithConvertMulFusing) { { auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); auto min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {0.0}); - auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {1.0}); - - auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {30.0}); - - auto mul1 = std::make_shared(min_const, mul_const); - auto mul2 = std::make_shared(max_const, mul_const); + auto max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {30.0}); - auto ru = - std::make_shared(input, mul1, mul2, ngraph::element::f32, 100, 200); + auto ru = std::make_shared(input, + min_const, + max_const, + ngraph::element::f32, + 100, + 200); auto conv = std::make_shared(ru, ngraph::element::f16); f_ref = std::make_shared(ngraph::NodeVector{conv}, ngraph::ParameterVector{input}); @@ -176,13 +177,15 @@ TEST(TransformationTests, RandomUniformWithConvertAddFusing) { { auto input = std::make_shared(ngraph::element::i32, ngraph::Shape{3}); - auto add_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {-10.0}); - auto ru_min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {0.0}); - auto ru_max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {30.0}); - auto add1 = std::make_shared(add_const, ru_min_const); - auto add2 = std::make_shared(add_const, ru_max_const); + auto ru_min_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {-10.0}); + auto ru_max_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{}, {20.0}); - auto ru = std::make_shared(input, add1, add2, ngraph::element::f32, 100, 200); + auto ru = std::make_shared(input, + ru_min_const, + ru_max_const, + ngraph::element::f32, + 100, + 200); auto conv = std::make_shared(ru, ngraph::element::f16); f_ref = std::make_shared(ngraph::NodeVector{conv}, ngraph::ParameterVector{input}); From 75fce5e68072c8aba785261ca9a9faae37512c4c Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Tue, 14 Sep 2021 17:01:12 +0300 Subject: [PATCH 13/18] Moved transformation to common fusions. --- .../src/offline_transformations/src/moc_transformations.cpp | 4 +--- .../common_optimizations/common_optimizations.cpp | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/inference-engine/src/offline_transformations/src/moc_transformations.cpp b/inference-engine/src/offline_transformations/src/moc_transformations.cpp index e9488757972791..445e53a000d7bd 100644 --- a/inference-engine/src/offline_transformations/src/moc_transformations.cpp +++ b/inference-engine/src/offline_transformations/src/moc_transformations.cpp @@ -92,6 +92,7 @@ bool ngraph::pass::MOCTransformations::run_on_function(std::shared_ptradd_matcher(); common_fusions->add_matcher(); common_fusions->add_matcher(); + common_fusions->add_matcher(); common_fusions->set_name("ngraph::pass::CommonFusions"); manager.register_pass(); @@ -102,9 +103,6 @@ bool ngraph::pass::MOCTransformations::run_on_function(std::shared_ptr(); - // RandomUniformFusion must be executed after LinOpSequenceFusion - manager.register_pass(); - auto conv_fusions = manager.register_pass(); conv_fusions->add_matcher(); conv_fusions->add_matcher(); diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp index d007d272b0e65e..3406f7635e0249 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/common_optimizations.cpp @@ -138,6 +138,7 @@ bool ngraph::pass::CommonOptimizations::run_on_function(std::shared_ptradd_matcher(); common_fusions->add_matcher(); common_fusions->add_matcher(); + common_fusions->add_matcher(); common_fusions->set_name("ngraph::pass::CommonFusions"); manager.register_pass(); @@ -177,9 +178,6 @@ bool ngraph::pass::CommonOptimizations::run_on_function(std::shared_ptr(); - // RandomUniformFusion must be executed after LinOpSequenceFusion - manager.register_pass(); - auto conv_fusions = manager.register_pass(); conv_fusions->add_matcher(); conv_fusions->add_matcher(); From 131a6e7e9d76001bdae8ed75445f079951c041e1 Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Tue, 14 Sep 2021 17:08:22 +0300 Subject: [PATCH 14/18] Added const refs. --- .../common_optimizations/random_uniform_fusion.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp index 05ca757950b3f3..e326b5aeb61d69 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp @@ -62,8 +62,8 @@ ngraph::pass::RandomUniformMulAddFusion::RandomUniformMulAddFusion() { const auto new_mul_add1 = mul_add_ptr->clone_with_new_inputs({ru->input_value(1), new_const}); const auto new_mul_add2 = mul_add_ptr->clone_with_new_inputs({ru->input_value(2), new_const}); - auto folded_const1 = ngraph::get_constant_from_source(new_mul_add1); - auto folded_const2 = ngraph::get_constant_from_source(new_mul_add2); + const auto& folded_const1 = ngraph::get_constant_from_source(new_mul_add1); + const auto& folded_const2 = ngraph::get_constant_from_source(new_mul_add2); const auto new_ru = ru->clone_with_new_inputs({data, folded_const1 ? folded_const1 : new_mul_add1, From e6d496df567a09fd7d256ef5333da3d21e3a41dc Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Tue, 14 Sep 2021 17:47:54 +0300 Subject: [PATCH 15/18] Update inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp Co-authored-by: Gleb Kazantaev --- .../common_optimizations/random_uniform_fusion.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp index e326b5aeb61d69..ae2ba3891395eb 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp @@ -80,7 +80,6 @@ ngraph::pass::RandomUniformMulAddFusion::RandomUniformMulAddFusion() { const auto new_ru_conv = cvt->clone_with_new_inputs({new_ru}); copy_runtime_info({ru, cvt, mul_add.get_node_shared_ptr()}, {new_mul_add1, new_mul_add2, new_ru, new_ru_conv}); ngraph::replace_node(m.get_match_root(), new_ru_conv); - } else { copy_runtime_info({ru, mul_add.get_node_shared_ptr()}, {new_mul_add1, new_mul_add2, new_ru}); ngraph::replace_node(m.get_match_root(), new_ru); From 1e187a626a4daa9112a406fce161f6d801ddc389 Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Tue, 14 Sep 2021 17:58:15 +0300 Subject: [PATCH 16/18] Changed to single class. --- .../random_uniform_fusion.hpp | 20 +++---------------- .../random_uniform_fusion.cpp | 5 ++--- 2 files changed, 5 insertions(+), 20 deletions(-) diff --git a/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp b/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp index cd1b800ddee21b..60415fc08df10f 100644 --- a/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp +++ b/inference-engine/src/transformations/include/transformations/common_optimizations/random_uniform_fusion.hpp @@ -11,32 +11,18 @@ namespace ngraph { namespace pass { class TRANSFORMATIONS_API RandomUniformFusion; -class TRANSFORMATIONS_API RandomUniformMulAddFusion; } // namespace pass } // namespace ngraph /** * @ingroup ie_transformation_common_api - * @brief RandomUniformMulAddFusion transformation replaces RandomUniform -> Add or + * @brief RandomUniformFusion transformation replaces RandomUniform -> Add or * RandomUniform -> Mul subgraph with a RandomUniform and replaces min and max const * with corrected values. */ -class ngraph::pass::RandomUniformMulAddFusion : public ngraph::pass::MatcherPass { +class ngraph::pass::RandomUniformFusion : public ngraph::pass::MatcherPass { public: NGRAPH_RTTI_DECLARATION; - RandomUniformMulAddFusion(); -}; - -/** - * @ingroup ie_transformation_common_api - * @brief RandomUniformFusion transformation replaces RandomUniform with Add or Mul sub-graphs with single - * RandomUniform. - */ -class ngraph::pass::RandomUniformFusion : public ngraph::pass::GraphRewrite { -public: - NGRAPH_RTTI_DECLARATION; - RandomUniformFusion() { - add_matcher(); - } + RandomUniformFusion(); }; diff --git a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp index e326b5aeb61d69..6022413558f072 100644 --- a/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp +++ b/inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp @@ -14,10 +14,9 @@ #include "itt.hpp" NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformFusion, "RandomUniformFusion", 0); -NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformMulAddFusion, "RandomUniformMulAddFusion", 0); -ngraph::pass::RandomUniformMulAddFusion::RandomUniformMulAddFusion() { - MATCHER_SCOPE(RandomUniformMulAddFusion); +ngraph::pass::RandomUniformFusion::RandomUniformFusion() { + MATCHER_SCOPE(RandomUniformFusion); const auto data_pattern = ngraph::pattern::any_input(); const auto ru_min_input_pattern = ngraph::pattern::any_input(); const auto ru_max_input_pattern = ngraph::pattern::any_input(); From 316d0585e3563268e97ff2816735c0648c960285 Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Wed, 15 Sep 2021 11:42:35 +0300 Subject: [PATCH 17/18] Corrected IRs checks in layer tests. --- .../tensorflow_tests/test_tf_RandomUniform.py | 41 ++++++++----------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/tests/layer_tests/tensorflow_tests/test_tf_RandomUniform.py b/tests/layer_tests/tensorflow_tests/test_tf_RandomUniform.py index 223b8b76edcaa7..477208067321c4 100644 --- a/tests/layer_tests/tensorflow_tests/test_tf_RandomUniform.py +++ b/tests/layer_tests/tensorflow_tests/test_tf_RandomUniform.py @@ -1,7 +1,6 @@ # Copyright (C) 2018-2021 Intel Corporation # SPDX-License-Identifier: Apache-2.0 -import numpy as np import pytest import tensorflow as tf from mo.front.common.partial_infer.utils import int64_array @@ -13,7 +12,8 @@ class TestTFRandomUniform(CommonTFLayerTest): - def create_tf_random_uniform_net(self, global_seed, op_seed, x_shape, min_val, max_val, input_type, ir_version): + def create_tf_random_uniform_net(self, global_seed, op_seed, x_shape, min_val, max_val, input_type, precision, + ir_version): tf.compat.v1.reset_default_graph() # Create the graph and model @@ -47,33 +47,22 @@ def create_tf_random_uniform_net(self, global_seed, op_seed, x_shape, min_val, m nodes_attributes = { **regular_op_with_shaped_data('input', x_shape, {'type': 'Parameter'}), **const_for_layer_tests('shape', x_shape, int64_array([len(x_shape)]), int64_array([len(x_shape)])), - **const_for_layer_tests('min_val_default', 0.0, int64_array([]), int64_array([1])), - **const_for_layer_tests('max_val_default', 1.0, int64_array([]), int64_array([1])), **const_for_layer_tests('min_val', min_val, int64_array([]), int64_array([1])), **const_for_layer_tests('max_val', max_val, int64_array([]), int64_array([1])), **regular_op_with_shaped_data('random_uniform', x_shape, {'type': 'RandomUniform'}), - **regular_op_with_shaped_data('random_uniform_add', x_shape, {'type': 'Add'}), - **const_for_layer_tests('random_uniform_add_const', np.array([[min_val]]), - int64_array([1, 1]) if min_val == 0.0 else int64_array([1]), int64_array([1])), - **regular_op_with_shaped_data('random_uniform_mul', x_shape, {'type': 'Multiply'}), - **const_for_layer_tests('random_uniform_mul_const', [max_val - min_val], - int64_array([1, 1]) if max_val == 1.0 else int64_array([1]), int64_array([1])), + **regular_op_with_shaped_data('convert', x_shape, {'type': 'Convert'}), **regular_op_with_shaped_data('add', x_shape, {'type': 'Add'}), **regular_op_with_shaped_data('result', x_shape, {'type': 'Result'}), } - if input_type == tf.float32: + + if precision == 'FP16' and input_type == tf.float32: ref_net = build_graph(nodes_attributes, [*connect_const_for_layer_tests('shape', '0:random_uniform'), - *connect_const_for_layer_tests('min_val_default', '1:random_uniform'), - *connect_const_for_layer_tests('max_val_default', '2:random_uniform'), - *connect('random_uniform', '0:random_uniform_mul'), - *connect_const_for_layer_tests('random_uniform_mul_const', - '1:random_uniform_mul'), - *connect('random_uniform_mul', '0:random_uniform_add'), - *connect_const_for_layer_tests('random_uniform_add_const', - '1:random_uniform_add'), - *connect('random_uniform_add', '0:add'), + *connect_const_for_layer_tests('min_val', '1:random_uniform'), + *connect_const_for_layer_tests('max_val', '2:random_uniform'), + *connect('random_uniform', 'convert'), + *connect('convert', '0:add'), *connect('input', '1:add'), *connect('add', 'result')]) else: @@ -85,20 +74,22 @@ def create_tf_random_uniform_net(self, global_seed, op_seed, x_shape, min_val, m *connect('input', '1:add'), *connect('add', 'result')]) + return tf_net, ref_net test_data = [pytest.param( dict(global_seed=32465, op_seed=48971, min_val=0.0, max_val=1.0, x_shape=[3, 7], input_type=tf.float32), marks=pytest.mark.precommit), - dict(global_seed=None, op_seed=56197, min_val=-100, max_val=100, x_shape=[6], input_type=tf.float32), - dict(global_seed=78132, op_seed=None, min_val=-200, max_val=-50, x_shape=[5, 8], input_type=tf.int32), - dict(global_seed=4571, op_seed=48971, min_val=1.5, max_val=2.3, x_shape=[7], input_type=tf.float32), - dict(global_seed=32465, op_seed=12335, min_val=-150, max_val=-100, x_shape=[18], input_type=tf.int32)] + dict(global_seed=None, op_seed=56197, min_val=-100, max_val=100, x_shape=[6], input_type=tf.float32), + dict(global_seed=78132, op_seed=None, min_val=-200, max_val=-50, x_shape=[5, 8], input_type=tf.int32), + dict(global_seed=4571, op_seed=48971, min_val=1.5, max_val=2.3, x_shape=[7], input_type=tf.float32), + dict(global_seed=32465, op_seed=12335, min_val=-150, max_val=-100, x_shape=[18], input_type=tf.int32)] @pytest.mark.parametrize("params", test_data) @pytest.mark.nightly def test_tf_random_uniform(self, params, ie_device, precision, ir_version, temp_dir): if ie_device == 'GPU': pytest.skip("RandomUniform is not supported on GPU") - self._test(*self.create_tf_random_uniform_net(**params, ir_version=ir_version), ie_device, precision, + self._test(*self.create_tf_random_uniform_net(**params, precision=precision, ir_version=ir_version), ie_device, + precision, temp_dir=temp_dir, ir_version=ir_version, **params) From bc557796d99c257d9130f09d558c4b9dcff8d095 Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Wed, 15 Sep 2021 14:09:10 +0300 Subject: [PATCH 18/18] Small corrections. --- tests/layer_tests/tensorflow_tests/test_tf_RandomUniform.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/layer_tests/tensorflow_tests/test_tf_RandomUniform.py b/tests/layer_tests/tensorflow_tests/test_tf_RandomUniform.py index 477208067321c4..400ea147524cd0 100644 --- a/tests/layer_tests/tensorflow_tests/test_tf_RandomUniform.py +++ b/tests/layer_tests/tensorflow_tests/test_tf_RandomUniform.py @@ -74,7 +74,6 @@ def create_tf_random_uniform_net(self, global_seed, op_seed, x_shape, min_val, m *connect('input', '1:add'), *connect('add', 'result')]) - return tf_net, ref_net test_data = [pytest.param( @@ -91,5 +90,4 @@ def test_tf_random_uniform(self, params, ie_device, precision, ir_version, temp_ if ie_device == 'GPU': pytest.skip("RandomUniform is not supported on GPU") self._test(*self.create_tf_random_uniform_net(**params, precision=precision, ir_version=ir_version), ie_device, - precision, - temp_dir=temp_dir, ir_version=ir_version, **params) + precision, temp_dir=temp_dir, ir_version=ir_version, **params)