1 //*****************************************************************************
2 // Copyright 2017-2020 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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 //*****************************************************************************
21 #include <unordered_map>
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"
33 NGRAPH_SUPPRESS_DEPRECATED_START
36 using namespace ngraph;
38 static bool replace_broadcast_like(const std::shared_ptr<ngraph::Node>& node)
40 // Replace a broadcast like with the broadcast to eliminate the pseudo-dependency on the "like"
42 auto broadcast_like = as_type_ptr<op::BroadcastLike>(node);
44 make_shared<op::Broadcast>(broadcast_like->input_value(0),
45 broadcast_like->get_broadcast_shape(),
46 broadcast_like->get_broadcast_axes()));
50 static const map<NodeTypeInfo, function<bool(const shared_ptr<Node>&)>> dispatcher{
51 {op::BroadcastLike::type_info, replace_broadcast_like}};
53 bool pass::LikeReplacement::run_on_function(shared_ptr<Function> function_ptr)
55 static const map<NodeTypeInfo, function<bool(const shared_ptr<Node>&)>> dispatcher{
56 {op::BroadcastLike::type_info, replace_broadcast_like}};
58 bool clobbered = false;
59 for (const auto& n : function_ptr->get_ops())
61 // Work around a warning [-Wpotentially-evaluated-expression]
62 auto handler = dispatcher.find(n->get_type_info());
63 if (handler != dispatcher.end())
65 clobbered = handler->second(n) || clobbered;