RandomUniformFusion transformation. (#7187)
* Added RandomUniformFusion transformation. * Extended transformations for case with Convert, extended transformations for general min and max value case. * Set to const unchanged variables. * Apply suggestions from code review Co-authored-by: Gleb Kazantaev <gleb.nnstu@gmail.com> * Reformat code, small corrections. * Added const shape checks. * Fixed transformation for case of different const ranks. * Added type checks. * Apply suggestions from code review Co-authored-by: Gleb Kazantaev <gleb.nnstu@gmail.com> * United RandomUniformMulFusion and RandomUniformAddFusion to single transformation. * Added negative tests. * Used get_constant_from_source(). * Moved transformation to common fusions. * Added const refs. * Update inference-engine/src/transformations/src/transformations/common_optimizations/random_uniform_fusion.cpp Co-authored-by: Gleb Kazantaev <gleb.nnstu@gmail.com> * Changed to single class. * Corrected IRs checks in layer tests. * Small corrections. Co-authored-by: Gleb Kazantaev <gleb.nnstu@gmail.com>
This commit is contained in:
1 parent
790ecd54c8
commit
10f0075e90
6 files changed
+429
-26
No files matched your search
@@ -38,6 +38,7 @@
|
||||
#include <transformations/low_precision/disable_convert_constant_folding_on_const_path.hpp>
|
||||
#include <transformations/common_optimizations/leaky_relu_fusion.hpp>
|
||||
#include <transformations/common_optimizations/normalize_l2_fusion.hpp>
|
||||
#include <transformations/common_optimizations/random_uniform_fusion.hpp>
|
||||
#include "transformations/common_optimizations/mul_conv_fusion.hpp"
|
||||
|
||||
NGRAPH_RTTI_DEFINITION(ngraph::pass::MOCTransformations, "MOCTransformations", 0);
|
||||
@@ -92,6 +93,7 @@ bool ngraph::pass::MOCTransformations::run_on_function(std::shared_ptr<ngraph::F
|
||||
common_fusions->add_matcher<ngraph::pass::DilatedConvolutionConverter>();
|
||||
common_fusions->add_matcher<ngraph::pass::GeluFusion>();
|
||||
common_fusions->add_matcher<ngraph::pass::LeakyReluFusion>();
|
||||
common_fusions->add_matcher<ngraph::pass::RandomUniformFusion>();
|
||||
common_fusions->set_name("ngraph::pass::CommonFusions");
|
||||
|
||||
manager.register_pass<ngraph::pass::BinarizeWeights>();
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
// Copyright (C) 2018-2021 Intel Corporation
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ngraph/pass/graph_rewrite.hpp>
|
||||
#include <transformations_visibility.hpp>
|
||||
|
||||
namespace ngraph {
|
||||
namespace pass {
|
||||
|
||||
class TRANSFORMATIONS_API RandomUniformFusion;
|
||||
|
||||
} // namespace pass
|
||||
} // namespace ngraph
|
||||
|
||||
/**
|
||||
* @ingroup ie_transformation_common_api
|
||||
* @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::RandomUniformFusion : public ngraph::pass::MatcherPass {
|
||||
public:
|
||||
NGRAPH_RTTI_DECLARATION;
|
||||
RandomUniformFusion();
|
||||
};
|
||||
+2
@@ -29,6 +29,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"
|
||||
@@ -139,6 +140,7 @@ bool ngraph::pass::CommonOptimizations::run_on_function(std::shared_ptr<ngraph::
|
||||
common_fusions->add_matcher<ngraph::pass::GeluFusion>();
|
||||
common_fusions->add_matcher<ngraph::pass::TransposeToReshape>();
|
||||
common_fusions->add_matcher<ngraph::pass::LeakyReluFusion>();
|
||||
common_fusions->add_matcher<ngraph::pass::RandomUniformFusion>();
|
||||
common_fusions->set_name("ngraph::pass::CommonFusions");
|
||||
|
||||
manager.register_pass<ngraph::pass::ConvertPadToGroupConvolution, false>();
|
||||
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
// Copyright (C) 2018-2021 Intel Corporation
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
#include "transformations/common_optimizations/random_uniform_fusion.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <ngraph/opsets/opset8.hpp>
|
||||
#include <ngraph/pattern/op/or.hpp>
|
||||
#include <ngraph/pattern/op/wrap_type.hpp>
|
||||
#include <ngraph/rt_info.hpp>
|
||||
#include <ngraph/ngraph.hpp>
|
||||
|
||||
#include "itt.hpp"
|
||||
|
||||
NGRAPH_RTTI_DEFINITION(ngraph::pass::RandomUniformFusion, "RandomUniformFusion", 0);
|
||||
|
||||
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();
|
||||
const auto random_uniform_pattern =
|
||||
ngraph::pattern::wrap_type<opset8::RandomUniform>({data_pattern, ru_min_input_pattern, ru_max_input_pattern},
|
||||
pattern::consumers_count(1));
|
||||
const auto const_pattern = ngraph::pattern::wrap_type<opset8::Constant>();
|
||||
|
||||
const auto convert_pattern = ngraph::pattern::wrap_type<opset8::Convert>({random_uniform_pattern});
|
||||
const auto random_uniform_or_convert_pattern =
|
||||
std::make_shared<pattern::op::Or>(OutputVector{random_uniform_pattern, convert_pattern});
|
||||
|
||||
const auto mul_add_pattern =
|
||||
ngraph::pattern::wrap_type<opset8::Multiply, opset8::Add>({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 constant = pattern_map.at(const_pattern);
|
||||
const auto ru = std::dynamic_pointer_cast<opset8::RandomUniform>(random_uniform.get_node_shared_ptr());
|
||||
if (!ru)
|
||||
return false;
|
||||
if (!ru->get_out_type().is_real())
|
||||
return false;
|
||||
|
||||
const auto old_const = std::dynamic_pointer_cast<opset8::Constant>(constant.get_node_shared_ptr());
|
||||
if (!old_const)
|
||||
return false;
|
||||
if (!old_const->get_element_type().is_real())
|
||||
return false;
|
||||
|
||||
auto const_shape = old_const->get_shape();
|
||||
if (shape_size(const_shape) != 1)
|
||||
return false;
|
||||
|
||||
const auto& value = old_const->cast_vector<double>();
|
||||
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<ngraph::Node>(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& 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,
|
||||
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)) {
|
||||
const auto& convert = pattern_map.at(convert_pattern);
|
||||
const auto cvt = std::dynamic_pointer_cast<opset8::Convert>(convert.get_node_shared_ptr());
|
||||
if (!cvt)
|
||||
return false;
|
||||
if (!cvt->get_element_type().is_real())
|
||||
return false;
|
||||
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);
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
auto m = std::make_shared<ngraph::pattern::Matcher>(mul_add_pattern, matcher_name);
|
||||
this->register_matcher(m, callback);
|
||||
}
|
||||
+290
@@ -0,0 +1,290 @@
|
||||
// Copyright (C) 2018-2021 Intel Corporation
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <memory>
|
||||
#include <ngraph/function.hpp>
|
||||
#include <ngraph/opsets/opset8.hpp>
|
||||
#include <ngraph/pass/constant_folding.hpp>
|
||||
#include <ngraph/pass/manager.hpp>
|
||||
#include <string>
|
||||
#include <transformations/common_optimizations/random_uniform_fusion.hpp>
|
||||
#include <transformations/init_node_info.hpp>
|
||||
|
||||
#include "common_test_utils/ngraph_test_utils.hpp"
|
||||
|
||||
using namespace testing;
|
||||
|
||||
TEST(TransformationTests, RandomUniformMulFusing) {
|
||||
std::shared_ptr<ngraph::Function> f(nullptr), f_ref(nullptr);
|
||||
{
|
||||
auto input = std::make_shared<ngraph::opset8::Parameter>(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<ngraph::opset8::RandomUniform>(input,
|
||||
min_const,
|
||||
max_const,
|
||||
ngraph::element::f32,
|
||||
100,
|
||||
200);
|
||||
|
||||
auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1, 1, 1}, {30.0});
|
||||
auto mul = std::make_shared<ngraph::opset8::Multiply>(ru, mul_const);
|
||||
|
||||
f = std::make_shared<ngraph::Function>(ngraph::NodeVector{mul}, ngraph::ParameterVector{input});
|
||||
|
||||
ngraph::pass::Manager manager;
|
||||
manager.register_pass<ngraph::pass::InitNodeInfo>();
|
||||
manager.register_pass<ngraph::pass::RandomUniformFusion>();
|
||||
manager.run_passes(f);
|
||||
ASSERT_NO_THROW(check_rt_info(f));
|
||||
}
|
||||
|
||||
{
|
||||
auto input = std::make_shared<ngraph::opset8::Parameter>(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{}, {30.0});
|
||||
auto ru = std::make_shared<ngraph::opset8::RandomUniform>(input,
|
||||
min_const,
|
||||
max_const,
|
||||
ngraph::element::f32,
|
||||
100,
|
||||
200);
|
||||
|
||||
f_ref = std::make_shared<ngraph::Function>(ngraph::NodeVector{ru}, ngraph::ParameterVector{input});
|
||||
}
|
||||
|
||||
auto res = compare_functions(f, f_ref);
|
||||
ASSERT_TRUE(res.first) << res.second;
|
||||
}
|
||||
|
||||
TEST(TransformationTests, RandomUniformAddFusing) {
|
||||
std::shared_ptr<ngraph::Function> f(nullptr), f_ref(nullptr);
|
||||
{
|
||||
auto input = std::make_shared<ngraph::opset8::Parameter>(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{}, {30.0});
|
||||
auto ru = std::make_shared<ngraph::opset8::RandomUniform>(input,
|
||||
min_const,
|
||||
max_const,
|
||||
ngraph::element::f32,
|
||||
100,
|
||||
200);
|
||||
|
||||
auto add_const = ngraph::opset8::Constant::create(ngraph::element::f32, ngraph::Shape{1, 1, 1, 1}, {-10.0});
|
||||
auto add = std::make_shared<ngraph::opset8::Add>(ru, add_const);
|
||||
|
||||
f = std::make_shared<ngraph::Function>(ngraph::NodeVector{add}, ngraph::ParameterVector{input});
|
||||
|
||||
ngraph::pass::Manager manager;
|
||||
manager.register_pass<ngraph::pass::InitNodeInfo>();
|
||||
manager.register_pass<ngraph::pass::RandomUniformFusion>();
|
||||
manager.run_passes(f);
|
||||
ASSERT_NO_THROW(check_rt_info(f));
|
||||
}
|
||||
|
||||
{
|
||||
auto input = std::make_shared<ngraph::opset8::Parameter>(ngraph::element::i32, ngraph::Shape{3});
|
||||
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<ngraph::opset8::RandomUniform>(input,
|
||||
ru_min_const,
|
||||
ru_max_const,
|
||||
ngraph::element::f32,
|
||||
100,
|
||||
200);
|
||||
|
||||
f_ref = std::make_shared<ngraph::Function>(ngraph::NodeVector{ru}, ngraph::ParameterVector{input});
|
||||
}
|
||||
|
||||
auto res = compare_functions(f, f_ref);
|
||||
ASSERT_TRUE(res.first) << res.second;
|
||||
}
|
||||
|
||||
TEST(TransformationTests, RandomUniformWithConvertMulFusing) {
|
||||
std::shared_ptr<ngraph::Function> f(nullptr), f_ref(nullptr);
|
||||
{
|
||||
auto input = std::make_shared<ngraph::opset8::Parameter>(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<ngraph::opset8::RandomUniform>(input,
|
||||
min_const,
|
||||
max_const,
|
||||
ngraph::element::f32,
|
||||
100,
|
||||
200);
|
||||
auto conv = std::make_shared<ngraph::opset8::Convert>(ru, ngraph::element::f16);
|
||||
|
||||
auto mul_const = ngraph::opset8::Constant::create(ngraph::element::f16, ngraph::Shape{}, {30.0});
|
||||
auto mul = std::make_shared<ngraph::opset8::Multiply>(conv, mul_const);
|
||||
|
||||
f = std::make_shared<ngraph::Function>(ngraph::NodeVector{mul}, ngraph::ParameterVector{input});
|
||||
|
||||
ngraph::pass::Manager manager;
|
||||
manager.register_pass<ngraph::pass::InitNodeInfo>();
|
||||
manager.register_pass<ngraph::pass::RandomUniformFusion>();
|
||||
manager.run_passes(f);
|
||||
ASSERT_NO_THROW(check_rt_info(f));
|
||||
}
|
||||
|
||||
{
|
||||
auto input = std::make_shared<ngraph::opset8::Parameter>(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{}, {30.0});
|
||||
|
||||
auto ru = std::make_shared<ngraph::opset8::RandomUniform>(input,
|
||||
min_const,
|
||||
max_const,
|
||||
ngraph::element::f32,
|
||||
100,
|
||||
200);
|
||||
auto conv = std::make_shared<ngraph::opset8::Convert>(ru, ngraph::element::f16);
|
||||
|
||||
f_ref = std::make_shared<ngraph::Function>(ngraph::NodeVector{conv}, ngraph::ParameterVector{input});
|
||||
}
|
||||
|
||||
auto res = compare_functions(f, f_ref);
|
||||
ASSERT_TRUE(res.first) << res.second;
|
||||
}
|
||||
|
||||
TEST(TransformationTests, RandomUniformWithConvertAddFusing) {
|
||||
std::shared_ptr<ngraph::Function> f(nullptr), f_ref(nullptr);
|
||||
{
|
||||
auto input = std::make_shared<ngraph::opset8::Parameter>(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{}, {30.0});
|
||||
auto ru = std::make_shared<ngraph::opset8::RandomUniform>(input,
|
||||
min_const,
|
||||
max_const,
|
||||
ngraph::element::f32,
|
||||
100,
|
||||
200);
|
||||
auto conv = std::make_shared<ngraph::opset8::Convert>(ru, ngraph::element::f16);
|
||||
|
||||
auto add_const = ngraph::opset8::Constant::create(ngraph::element::f16, ngraph::Shape{}, {-10.0});
|
||||
auto add = std::make_shared<ngraph::opset8::Add>(conv, add_const);
|
||||
|
||||
f = std::make_shared<ngraph::Function>(ngraph::NodeVector{add}, ngraph::ParameterVector{input});
|
||||
|
||||
ngraph::pass::Manager manager;
|
||||
manager.register_pass<ngraph::pass::InitNodeInfo>();
|
||||
manager.register_pass<ngraph::pass::RandomUniformFusion>();
|
||||
manager.run_passes(f);
|
||||
ASSERT_NO_THROW(check_rt_info(f));
|
||||
}
|
||||
|
||||
{
|
||||
auto input = std::make_shared<ngraph::opset8::Parameter>(ngraph::element::i32, ngraph::Shape{3});
|
||||
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<ngraph::opset8::RandomUniform>(input,
|
||||
ru_min_const,
|
||||
ru_max_const,
|
||||
ngraph::element::f32,
|
||||
100,
|
||||
200);
|
||||
auto conv = std::make_shared<ngraph::opset8::Convert>(ru, ngraph::element::f16);
|
||||
|
||||
f_ref = std::make_shared<ngraph::Function>(ngraph::NodeVector{conv}, ngraph::ParameterVector{input});
|
||||
}
|
||||
|
||||
auto res = compare_functions(f, f_ref);
|
||||
ASSERT_TRUE(res.first) << res.second;
|
||||
}
|
||||
|
||||
|
||||
TEST(TransformationTests, RandomUniformFusingInvalidRUType) {
|
||||
std::shared_ptr<ngraph::Function> f(nullptr), f_ref(nullptr);
|
||||
{
|
||||
auto input = std::make_shared<ngraph::opset8::Parameter>(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<ngraph::opset8::RandomUniform>(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<ngraph::opset8::Multiply>(ru, mul_const);
|
||||
|
||||
f = std::make_shared<ngraph::Function>(ngraph::NodeVector{mul}, ngraph::ParameterVector{input});
|
||||
|
||||
ngraph::pass::Manager manager;
|
||||
manager.register_pass<ngraph::pass::InitNodeInfo>();
|
||||
manager.register_pass<ngraph::pass::RandomUniformFusion>();
|
||||
manager.run_passes(f);
|
||||
ASSERT_NO_THROW(check_rt_info(f));
|
||||
}
|
||||
|
||||
{
|
||||
auto input = std::make_shared<ngraph::opset8::Parameter>(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<ngraph::opset8::RandomUniform>(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<ngraph::opset8::Multiply>(ru, mul_const);
|
||||
|
||||
f_ref = std::make_shared<ngraph::Function>(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<ngraph::Function> f(nullptr), f_ref(nullptr);
|
||||
{
|
||||
auto input = std::make_shared<ngraph::opset8::Parameter>(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<ngraph::opset8::RandomUniform>(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<ngraph::opset8::Multiply>(ru, mul_const);
|
||||
|
||||
f = std::make_shared<ngraph::Function>(ngraph::NodeVector{mul}, ngraph::ParameterVector{input});
|
||||
|
||||
ngraph::pass::Manager manager;
|
||||
manager.register_pass<ngraph::pass::InitNodeInfo>();
|
||||
manager.register_pass<ngraph::pass::RandomUniformFusion>();
|
||||
manager.run_passes(f);
|
||||
ASSERT_NO_THROW(check_rt_info(f));
|
||||
}
|
||||
|
||||
{
|
||||
auto input = std::make_shared<ngraph::opset8::Parameter>(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<ngraph::opset8::RandomUniform>(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<ngraph::opset8::Multiply>(ru, mul_const);
|
||||
|
||||
f_ref = std::make_shared<ngraph::Function>(ngraph::NodeVector{mul}, ngraph::ParameterVector{input});
|
||||
}
|
||||
|
||||
auto res = compare_functions(f, f_ref);
|
||||
ASSERT_TRUE(res.first) << res.second;
|
||||
}
|
||||
@@ -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 @@ from common.tf_layer_test_class import CommonTFLayerTest
|
||||
|
||||
|
||||
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 @@ class TestTFRandomUniform(CommonTFLayerTest):
|
||||
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:
|
||||
@@ -90,15 +79,15 @@ class TestTFRandomUniform(CommonTFLayerTest):
|
||||
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,
|
||||
temp_dir=temp_dir, ir_version=ir_version, **params)
|
||||
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)
|
||||
Reference in new issue
Block a user