d6d5e84833922111a92d619de8cdfaa1db9ce9b7
[platform/upstream/dldt.git] / ngraph / test / runtime / pass / like_replacement.cpp
1 //*****************************************************************************
2 // Copyright 2017-2020 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //*****************************************************************************
16
17 #include <functional>
18 #include <memory>
19 #include <typeindex>
20 #include <typeinfo>
21 #include <unordered_map>
22
23 #include "like_replacement.hpp"
24 #include "ngraph/op/broadcast.hpp"
25 #include "ngraph/op/constant.hpp"
26 #include "ngraph/op/convert.hpp"
27 #include "ngraph/op/pad.hpp"
28 #include "ngraph/op/slice.hpp"
29 #include "ngraph/op/stop_gradient.hpp"
30 #include "ngraph/op/sum.hpp"
31 #include "ngraph/util.hpp"
32
33 NGRAPH_SUPPRESS_DEPRECATED_START
34
35 using namespace std;
36 using namespace ngraph;
37
38 static bool replace_broadcast_like(const std::shared_ptr<ngraph::Node>& node)
39 {
40     // Replace a broadcast like with the broadcast to eliminate the pseudo-dependency on the "like"
41     // argument
42     auto broadcast_like = as_type_ptr<op::BroadcastLike>(node);
43     replace_node(node,
44                  make_shared<op::Broadcast>(broadcast_like->input_value(0),
45                                             broadcast_like->get_broadcast_shape(),
46                                             broadcast_like->get_broadcast_axes()));
47     return true;
48 }
49
50 static const map<NodeTypeInfo, function<bool(const shared_ptr<Node>&)>> dispatcher{
51     {op::BroadcastLike::type_info, replace_broadcast_like}};
52
53 bool pass::LikeReplacement::run_on_function(shared_ptr<Function> function_ptr)
54 {
55     static const map<NodeTypeInfo, function<bool(const shared_ptr<Node>&)>> dispatcher{
56         {op::BroadcastLike::type_info, replace_broadcast_like}};
57
58     bool clobbered = false;
59     for (const auto& n : function_ptr->get_ops())
60     {
61         // Work around a warning [-Wpotentially-evaluated-expression]
62         auto handler = dispatcher.find(n->get_type_info());
63         if (handler != dispatcher.end())
64         {
65             clobbered = handler->second(n) || clobbered;
66         }
67     }
68
69     return clobbered;
70 }