|
| 1 | +// Copyright (C) 2023 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#include <openvino/core/model.hpp> |
| 6 | +#include <openvino/opsets/opset10.hpp> |
| 7 | +#include <transformations/common_optimizations/push_constant_to_subgraph.hpp> |
| 8 | + |
| 9 | +#include "common_test_utils/ngraph_test_utils.hpp" |
| 10 | + |
| 11 | +using namespace testing; |
| 12 | +using namespace ov; |
| 13 | + |
| 14 | +TEST_F(TransformationTestsF, PushConstantToSubgraphLoop) { |
| 15 | + { |
| 16 | + auto trip_count = opset10::Constant::create(element::i32, Shape{}, {2}); |
| 17 | + auto term_cond = opset10::Constant::create(element::boolean, Shape{}, {true}); |
| 18 | + std::shared_ptr<Model> loop_body; |
| 19 | + { |
| 20 | + auto X = std::make_shared<opset10::Parameter>(element::f32, Shape{1, 2}); |
| 21 | + auto Y = std::make_shared<opset10::Parameter>(element::f32, Shape{1, 2}); |
| 22 | + auto Z = std::make_shared<opset10::Parameter>(element::f32, Shape{1, 2}); |
| 23 | + auto mul = std::make_shared<opset10::Multiply>(X, Y); |
| 24 | + auto add = std::make_shared<opset10::Add>(mul, Z); |
| 25 | + auto cond = opset10::Constant::create(element::boolean, Shape{}, {true}); |
| 26 | + loop_body = std::make_shared<Model>(NodeVector{add, cond}, ParameterVector{X, Y, Z}); |
| 27 | + } |
| 28 | + auto loop = std::make_shared<opset10::Loop>(trip_count, term_cond); |
| 29 | + loop->set_function(loop_body); |
| 30 | + |
| 31 | + auto X = std::make_shared<opset10::Parameter>(element::f32, Shape{2, 2}); |
| 32 | + auto constant_1 = opset10::Constant::create(element::i32, Shape{2, 2}, {11}); |
| 33 | + auto convert_1 = std::make_shared<opset10::Convert>(constant_1, element::f32); |
| 34 | + auto constant_2 = opset10::Constant::create(element::i32, Shape{1, 2}, {22}); |
| 35 | + auto convert_2 = std::make_shared<opset10::Convert>(constant_2, element::f32); |
| 36 | + const auto& loop_params = loop_body->get_parameters(); |
| 37 | + loop->set_special_body_ports({-1, 1}); |
| 38 | + loop->set_sliced_input(loop_params[0], X, 0, 1, 1, -1, 0); |
| 39 | + loop->set_sliced_input(loop_params[1], convert_1, 0, 1, 1, -1, 0); |
| 40 | + loop->set_invariant_input(loop_params[2], convert_2); |
| 41 | + auto out = loop->get_concatenated_slices(loop_body->get_results()[0], 0, 1, 1, -1, 0); |
| 42 | + function = std::make_shared<Model>(OutputVector{out}, ParameterVector{X}); |
| 43 | + |
| 44 | + manager.register_pass<pass::PushConstantToSubgraph>(); |
| 45 | + } |
| 46 | + |
| 47 | + { |
| 48 | + auto trip_count = opset10::Constant::create(element::i32, Shape{}, {2}); |
| 49 | + auto term_cond = opset10::Constant::create(element::boolean, Shape{}, {true}); |
| 50 | + std::shared_ptr<Model> loop_body; |
| 51 | + { |
| 52 | + auto constant = opset10::Constant::create(element::f32, Shape{1, 2}, {22}); |
| 53 | + auto X = std::make_shared<opset10::Parameter>(element::f32, Shape{1, 2}); |
| 54 | + auto Y = std::make_shared<opset10::Parameter>(element::f32, Shape{1, 2}); |
| 55 | + auto mul = std::make_shared<opset10::Multiply>(X, Y); |
| 56 | + auto add = std::make_shared<opset10::Add>(mul, constant); |
| 57 | + auto cond = opset10::Constant::create(element::boolean, Shape{}, {true}); |
| 58 | + loop_body = std::make_shared<Model>(NodeVector{add, cond}, ParameterVector{X, Y}); |
| 59 | + } |
| 60 | + auto loop = std::make_shared<opset10::Loop>(trip_count, term_cond); |
| 61 | + loop->set_function(loop_body); |
| 62 | + |
| 63 | + auto X = std::make_shared<opset10::Parameter>(element::f32, Shape{2, 2}); |
| 64 | + auto constant_1 = opset10::Constant::create(element::i32, Shape{2, 2}, {11}); |
| 65 | + auto convert_1 = std::make_shared<opset10::Convert>(constant_1, element::f32); |
| 66 | + const auto& loop_params = loop_body->get_parameters(); |
| 67 | + loop->set_special_body_ports({-1, 1}); |
| 68 | + loop->set_sliced_input(loop_params[0], X, 0, 1, 1, -1, 0); |
| 69 | + loop->set_sliced_input(loop_params[1], convert_1, 0, 1, 1, -1, 0); |
| 70 | + auto out = loop->get_concatenated_slices(loop_body->get_results()[0], 0, 1, 1, -1, 0); |
| 71 | + function_ref = std::make_shared<Model>(OutputVector{out}, ParameterVector{X}); |
| 72 | + } |
| 73 | + comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES); |
| 74 | + comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES); |
| 75 | + comparator.enable(FunctionsComparator::CmpValues::ACCURACY); |
| 76 | +} |
| 77 | + |
| 78 | +TEST_F(TransformationTestsF, PushConstantToSubgraphIf) { |
| 79 | + { |
| 80 | + auto cond = opset10::Constant::create(element::boolean, Shape{}, {false}); |
| 81 | + auto if_op = std::make_shared<ov::opset10::If>(cond); |
| 82 | + std::shared_ptr<ov::Model> then_body; |
| 83 | + { |
| 84 | + auto A = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 85 | + auto B = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 86 | + auto C = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 87 | + auto D = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 88 | + auto add = std::make_shared<ov::opset10::Add>(A, B); |
| 89 | + auto mul = std::make_shared<ov::opset10::Multiply>(add, C); |
| 90 | + auto sub = std::make_shared<ov::opset10::Subtract>(mul, D); |
| 91 | + then_body = std::make_shared<ov::Model>(add, ov::ParameterVector{A, B, C, D}); |
| 92 | + } |
| 93 | + std::shared_ptr<ov::Model> else_body; |
| 94 | + { |
| 95 | + auto A = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 96 | + auto B = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 97 | + auto C = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 98 | + auto D = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 99 | + auto mul = std::make_shared<ov::opset10::Multiply>(A, B); |
| 100 | + auto add = std::make_shared<ov::opset10::Add>(mul, C); |
| 101 | + auto div = std::make_shared<ov::opset10::Divide>(add, D); |
| 102 | + else_body = std::make_shared<ov::Model>(div, ov::ParameterVector{A, B, C, D}); |
| 103 | + } |
| 104 | + |
| 105 | + if_op->set_then_body(then_body); |
| 106 | + if_op->set_else_body(else_body); |
| 107 | + |
| 108 | + const auto& then_params = then_body->get_parameters(); |
| 109 | + const auto& else_params = else_body->get_parameters(); |
| 110 | + |
| 111 | + auto A = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 112 | + auto B = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 113 | + auto C = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 114 | + auto const_1 = ov::opset10::Constant::create(ov::element::i32, ov::Shape{3}, {1}); |
| 115 | + auto convert_1 = std::make_shared<ov::opset10::Convert>(const_1, ov::element::f32); |
| 116 | + auto const_2 = ov::opset10::Constant::create(ov::element::i32, ov::Shape{3}, {2}); |
| 117 | + auto convert_2 = std::make_shared<ov::opset10::Convert>(const_2, ov::element::f32); |
| 118 | + auto const_3 = ov::opset10::Constant::create(ov::element::i32, ov::Shape{3}, {3}); |
| 119 | + auto convert_3 = std::make_shared<ov::opset10::Convert>(const_3, ov::element::f32); |
| 120 | + |
| 121 | + if_op->set_input(A, then_params[0], nullptr); |
| 122 | + if_op->set_input(convert_1, then_params[1], nullptr); |
| 123 | + if_op->set_input(B, then_params[2], else_params[0]); |
| 124 | + if_op->set_input(convert_2, then_params[3], else_params[1]); |
| 125 | + |
| 126 | + if_op->set_input(C, nullptr, else_params[2]); |
| 127 | + if_op->set_input(convert_3, nullptr, else_params[3]); |
| 128 | + if_op->set_output(then_body->get_results()[0], else_body->get_results()[0]); |
| 129 | + |
| 130 | + function = std::make_shared<ov::Model>(if_op, ov::ParameterVector{A, B, C}); |
| 131 | + |
| 132 | + manager.register_pass<pass::PushConstantToSubgraph>(); |
| 133 | + } |
| 134 | + |
| 135 | + { |
| 136 | + auto cond = opset10::Constant::create(element::boolean, Shape{}, {false}); |
| 137 | + auto const_1 = ov::opset10::Constant::create(ov::element::f32, ov::Shape{3}, {1}); |
| 138 | + auto const_2 = ov::opset10::Constant::create(ov::element::f32, ov::Shape{3}, {2}); |
| 139 | + auto const_3 = ov::opset10::Constant::create(ov::element::f32, ov::Shape{3}, {3}); |
| 140 | + auto if_op = std::make_shared<ov::opset10::If>(cond); |
| 141 | + std::shared_ptr<ov::Model> then_body; |
| 142 | + { |
| 143 | + auto A = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 144 | + auto B = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 145 | + auto add = std::make_shared<ov::opset10::Add>(A, const_1); |
| 146 | + auto mul = std::make_shared<ov::opset10::Multiply>(add, B); |
| 147 | + auto sub = std::make_shared<ov::opset10::Subtract>(mul, const_2); |
| 148 | + then_body = std::make_shared<ov::Model>(add, ov::ParameterVector{A, B}); |
| 149 | + } |
| 150 | + std::shared_ptr<ov::Model> else_body; |
| 151 | + { |
| 152 | + auto A = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 153 | + auto B = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 154 | + auto mul = std::make_shared<ov::opset10::Multiply>(A, const_2); |
| 155 | + auto add = std::make_shared<ov::opset10::Add>(mul, B); |
| 156 | + auto div = std::make_shared<ov::opset10::Divide>(add, const_3); |
| 157 | + else_body = std::make_shared<ov::Model>(div, ov::ParameterVector{A, B}); |
| 158 | + } |
| 159 | + |
| 160 | + if_op->set_then_body(then_body); |
| 161 | + if_op->set_else_body(else_body); |
| 162 | + |
| 163 | + const auto& then_params = then_body->get_parameters(); |
| 164 | + const auto& else_params = else_body->get_parameters(); |
| 165 | + |
| 166 | + auto A = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 167 | + auto B = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 168 | + auto C = std::make_shared<ov::opset10::Parameter>(ov::element::f32, ov::Shape{3}); |
| 169 | + |
| 170 | + if_op->set_input(A, then_params[0], nullptr); |
| 171 | + if_op->set_input(B, then_params[1], else_params[0]); |
| 172 | + if_op->set_input(C, nullptr, else_params[1]); |
| 173 | + if_op->set_output(then_body->get_results()[0], else_body->get_results()[0]); |
| 174 | + |
| 175 | + function_ref = std::make_shared<ov::Model>(if_op, ov::ParameterVector{A, B, C}); |
| 176 | + } |
| 177 | + |
| 178 | + comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES); |
| 179 | + comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES); |
| 180 | + comparator.enable(FunctionsComparator::CmpValues::ACCURACY); |
| 181 | +} |
0 commit comments