|
| 1 | +// Copyright (C) 2018-2021 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#include <gtest/gtest.h> |
| 6 | + |
| 7 | +#include <string> |
| 8 | +#include <memory> |
| 9 | +#include <queue> |
| 10 | + |
| 11 | +#include <ngraph/function.hpp> |
| 12 | +#include <ngraph/opsets/opset8.hpp> |
| 13 | +#include <transformations/common_optimizations/leaky_relu_fusion.hpp> |
| 14 | +#include <transformations/init_node_info.hpp> |
| 15 | +#include <transformations/utils/utils.hpp> |
| 16 | +#include <ngraph/pass/manager.hpp> |
| 17 | +#include <ngraph/pass/constant_folding.hpp> |
| 18 | + |
| 19 | +#include "common_test_utils/ngraph_test_utils.hpp" |
| 20 | + |
| 21 | + |
| 22 | +using namespace testing; |
| 23 | +using namespace ngraph; |
| 24 | + |
| 25 | +TEST(TransformationTests, LeakyReluFusionConstant) { |
| 26 | + std::shared_ptr<Function> f(nullptr), f_ref(nullptr); |
| 27 | + { |
| 28 | + auto data = std::make_shared<opset8::Parameter>(element::f32, Shape{2, 2}); |
| 29 | + auto alpha = opset8::Constant::create(element::f32, Shape{1}, {0.1}); |
| 30 | + auto multiply = std::make_shared<opset8::Multiply>(data, alpha); |
| 31 | + auto max = std::make_shared<opset8::Maximum>(data, multiply); |
| 32 | + f = std::make_shared<Function>(NodeVector{max}, ParameterVector{data}); |
| 33 | + |
| 34 | + pass::Manager m; |
| 35 | + m.register_pass<pass::InitNodeInfo>(); |
| 36 | + m.register_pass<pass::LeakyReluFusion>(); |
| 37 | + m.run_passes(f); |
| 38 | + ASSERT_NO_THROW(check_rt_info(f)); |
| 39 | + } |
| 40 | + |
| 41 | + { |
| 42 | + auto data = std::make_shared<opset1::Parameter>(element::f32, Shape{2, 2}); |
| 43 | + auto alpha = opset8::Constant::create(element::f32, Shape{1}, {0.1}); |
| 44 | + auto leaky_relu = std::make_shared<opset8::PRelu>(data, alpha); |
| 45 | + f_ref = std::make_shared<Function>(NodeVector{leaky_relu}, ParameterVector{data}); |
| 46 | + } |
| 47 | + |
| 48 | + auto res = compare_functions(f, f_ref); |
| 49 | + ASSERT_TRUE(res.first) << res.second; |
| 50 | +} |
| 51 | + |
| 52 | +TEST(TransformationTests, LeakyReluFusionScalar) { |
| 53 | + std::shared_ptr<Function> f(nullptr), f_ref(nullptr); |
| 54 | + { |
| 55 | + auto data = std::make_shared<opset8::Parameter>(element::f32, Shape{2, 2}); |
| 56 | + auto alpha = opset8::Constant::create(element::f32, Shape{}, {0.1}); |
| 57 | + auto multiply = std::make_shared<opset8::Multiply>(data, alpha); |
| 58 | + auto max = std::make_shared<opset8::Maximum>(data, multiply); |
| 59 | + f = std::make_shared<Function>(NodeVector{max}, ParameterVector{data}); |
| 60 | + |
| 61 | + pass::Manager m; |
| 62 | + m.register_pass<pass::InitNodeInfo>(); |
| 63 | + m.register_pass<pass::LeakyReluFusion>(); |
| 64 | + m.run_passes(f); |
| 65 | + ASSERT_NO_THROW(check_rt_info(f)); |
| 66 | + } |
| 67 | + |
| 68 | + { |
| 69 | + auto data = std::make_shared<opset1::Parameter>(element::f32, Shape{2, 2}); |
| 70 | + auto alpha = opset8::Constant::create(element::f32, Shape{}, {0.1}); |
| 71 | + auto leaky_relu = std::make_shared<opset8::PRelu>(data, alpha); |
| 72 | + f_ref = std::make_shared<Function>(NodeVector{leaky_relu}, ParameterVector{data}); |
| 73 | + } |
| 74 | + |
| 75 | + auto res = compare_functions(f, f_ref); |
| 76 | + ASSERT_TRUE(res.first) << res.second; |
| 77 | +} |
| 78 | + |
| 79 | +TEST(TransformationTests, LeakyReluFusionParameter) { |
| 80 | + std::shared_ptr<Function> f(nullptr), f_ref(nullptr); |
| 81 | + { |
| 82 | + auto data = std::make_shared<opset8::Parameter>(element::f32, Shape{2, 2}); |
| 83 | + auto alpha = std::make_shared<opset8::Parameter>(element::f32, Shape{}); |
| 84 | + auto multiply = std::make_shared<opset8::Multiply>(data, alpha); |
| 85 | + auto max = std::make_shared<opset8::Maximum>(data, multiply); |
| 86 | + f = std::make_shared<Function>(NodeVector{max}, ParameterVector{data, alpha}); |
| 87 | + |
| 88 | + pass::Manager m; |
| 89 | + m.register_pass<pass::InitNodeInfo>(); |
| 90 | + m.register_pass<pass::LeakyReluFusion>(); |
| 91 | + m.run_passes(f); |
| 92 | + ASSERT_NO_THROW(check_rt_info(f)); |
| 93 | + } |
| 94 | + |
| 95 | + { |
| 96 | + auto data = std::make_shared<opset1::Parameter>(element::f32, Shape{2, 2}); |
| 97 | + auto alpha = std::make_shared<opset8::Parameter>(element::f32, Shape{}); |
| 98 | + auto leaky_relu = std::make_shared<opset8::PRelu>(data, alpha); |
| 99 | + f_ref = std::make_shared<Function>(NodeVector{leaky_relu}, ParameterVector{data, alpha}); |
| 100 | + } |
| 101 | + |
| 102 | + auto res = compare_functions(f, f_ref); |
| 103 | + ASSERT_TRUE(res.first) << res.second; |
| 104 | +} |
0 commit comments