|
| 1 | +// Copyright (C) 2025 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#define _USE_MATH_DEFINES |
| 6 | + |
| 7 | +#include "transformations/common_optimizations/constants_reduce.hpp" |
| 8 | + |
| 9 | +#include <gtest/gtest.h> |
| 10 | +#include <math.h> |
| 11 | + |
| 12 | +#include <memory> |
| 13 | + |
| 14 | +#include "common_test_utils/ov_test_utils.hpp" |
| 15 | +#include "openvino/core/model.hpp" |
| 16 | +#include "openvino/opsets/opset8.hpp" |
| 17 | +#include "openvino/pass/manager.hpp" |
| 18 | + |
| 19 | +using namespace testing; |
| 20 | +using namespace ov; |
| 21 | + |
| 22 | +TEST(TransformationTests, ConstantsReduce) { |
| 23 | + auto param = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 4}); |
| 24 | + |
| 25 | + // Intentionally equal to each other |
| 26 | + auto add_constant_1 = opset8::Constant::create(element::f32, Shape{1, 4}, {1.0, 2.0, 3.0, 4.0}); |
| 27 | + auto add_constant_2 = opset8::Constant::create(element::f32, Shape{1, 4}, {1.0, 2.0, 3.0, 4.0}); |
| 28 | + auto add_1 = std::make_shared<opset8::Add>(param, add_constant_1); |
| 29 | + auto add_2 = std::make_shared<opset8::Add>(add_1, add_constant_2); |
| 30 | + |
| 31 | + auto result = std::make_shared<ov::op::v0::Result>(add_2); |
| 32 | + auto f = std::make_shared<Model>(ResultVector{result}, ParameterVector{param}); |
| 33 | + |
| 34 | + pass::Manager pass_manager; |
| 35 | + pass_manager.register_pass<ov::pass::ConstantsReduce>(); |
| 36 | + pass_manager.run_passes(f); |
| 37 | + |
| 38 | + // One constant should be reduced since they are equal |
| 39 | + ASSERT_EQ(count_ops_of_type<opset8::Constant>(f), 1); |
| 40 | +} |
| 41 | + |
| 42 | +TEST(TransformationTests, ConstantsReduceChain) { |
| 43 | + auto param = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 4}); |
| 44 | + |
| 45 | + // Intentionally equal to each other |
| 46 | + auto add_constant_1 = opset8::Constant::create(element::f32, Shape{1, 4}, {1.0, 2.0, 3.0, 4.0}); |
| 47 | + auto add_constant_2 = opset8::Constant::create(element::f32, Shape{1, 4}, {1.0, 2.0, 3.0, 4.0}); |
| 48 | + auto add_constant_3 = opset8::Constant::create(element::f32, Shape{1, 4}, {1.0, 2.0, 3.0, 4.0}); |
| 49 | + auto add_constant_4 = opset8::Constant::create(element::f32, Shape{1, 4}, {1.0, 2.0, 3.0, 4.0}); |
| 50 | + |
| 51 | + // Intentionally different |
| 52 | + auto add_constant_5 = opset8::Constant::create(element::f32, Shape{1, 4}, {2.0, 2.0, 3.0, 4.0}); |
| 53 | + auto add_1 = std::make_shared<opset8::Add>(param, add_constant_1); |
| 54 | + auto add_2 = std::make_shared<opset8::Add>(add_1, add_constant_2); |
| 55 | + auto add_3 = std::make_shared<opset8::Add>(add_2, add_constant_3); |
| 56 | + auto add_4 = std::make_shared<opset8::Add>(add_3, add_constant_4); |
| 57 | + auto add_5 = std::make_shared<opset8::Add>(add_4, add_constant_5); |
| 58 | + |
| 59 | + auto result = std::make_shared<ov::op::v0::Result>(add_5); |
| 60 | + auto f = std::make_shared<Model>(ResultVector{result}, ParameterVector{param}); |
| 61 | + |
| 62 | + pass::Manager pass_manager; |
| 63 | + pass_manager.register_pass<ov::pass::ConstantsReduce>(); |
| 64 | + pass_manager.run_passes(f); |
| 65 | + |
| 66 | + // All constants should be reduced to one except the one that is different |
| 67 | + ASSERT_EQ(count_ops_of_type<opset8::Constant>(f), 2); |
| 68 | +} |
| 69 | + |
| 70 | +TEST(TransformationTests, ConstantsReduceChain2) { |
| 71 | + auto param = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 4}); |
| 72 | + |
| 73 | + // Intentionally equal to each other |
| 74 | + auto add_constant_1 = opset8::Constant::create(element::f32, Shape{1, 4}, {1.0, 2.0, 3.0, 4.0}); |
| 75 | + auto add_constant_2 = opset8::Constant::create(element::f32, Shape{1, 4}, {1.0, 2.0, 3.0, 4.0}); |
| 76 | + auto add_constant_3 = opset8::Constant::create(element::f32, Shape{1, 4}, {1.0, 2.0, 3.0, 4.0}); |
| 77 | + auto add_constant_4 = opset8::Constant::create(element::f32, Shape{1, 4}, {1.0, 2.0, 3.0, 4.0}); |
| 78 | + auto add_constant_5 = opset8::Constant::create(element::f32, Shape{1, 4}, {1.0, 2.0, 3.0, 4.0}); |
| 79 | + |
| 80 | + auto add_1 = std::make_shared<opset8::Add>(param, add_constant_1); |
| 81 | + auto add_2 = std::make_shared<opset8::Add>(add_1, add_constant_2); |
| 82 | + auto add_3 = std::make_shared<opset8::Add>(add_2, add_constant_3); |
| 83 | + auto add_4 = std::make_shared<opset8::Add>(add_3, add_constant_4); |
| 84 | + auto add_5 = std::make_shared<opset8::Add>(add_4, add_constant_5); |
| 85 | + |
| 86 | + auto result = std::make_shared<ov::op::v0::Result>(add_5); |
| 87 | + auto f = std::make_shared<Model>(ResultVector{result}, ParameterVector{param}); |
| 88 | + |
| 89 | + pass::Manager pass_manager; |
| 90 | + pass_manager.register_pass<ov::pass::ConstantsReduce>(); |
| 91 | + pass_manager.run_passes(f); |
| 92 | + |
| 93 | + // All constants should be reduced to one |
| 94 | + ASSERT_EQ(count_ops_of_type<opset8::Constant>(f), 1); |
| 95 | +} |
| 96 | + |
| 97 | +TEST(TransformationTests, ConstantsReduceNeg) { |
| 98 | + auto param = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 4}); |
| 99 | + |
| 100 | + // Intentionally unequal to each other |
| 101 | + auto add_constant_1 = opset8::Constant::create(element::f32, Shape{1, 4}, {1.0, 2.0, 3.0, 4.0}); |
| 102 | + auto add_constant_2 = opset8::Constant::create(element::f32, Shape{1, 4}, {1.0, 2.0, 3.0, 4.5}); |
| 103 | + auto add_1 = std::make_shared<opset8::Add>(param, add_constant_1); |
| 104 | + auto add_2 = std::make_shared<opset8::Add>(add_1, add_constant_2); |
| 105 | + |
| 106 | + auto result = std::make_shared<ov::op::v0::Result>(add_2); |
| 107 | + auto f = std::make_shared<Model>(ResultVector{result}, ParameterVector{param}); |
| 108 | + |
| 109 | + pass::Manager pass_manager; |
| 110 | + pass_manager.register_pass<ov::pass::ConstantsReduce>(); |
| 111 | + pass_manager.run_passes(f); |
| 112 | + |
| 113 | + // No reduction here |
| 114 | + ASSERT_EQ(count_ops_of_type<opset8::Constant>(f), 2); |
| 115 | +} |
0 commit comments