6bd83f5c5d1dd0568db246f541176af5817eba4a
[platform/core/ml/nnfw.git] / compiler / luci / pass / src / ReplaceSubWithAddPass.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
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 "luci/Pass/ReplaceSubWithAddPass.h"
18
19 #include <luci/IR/CircleNodes.h>
20 #include <luci/Service/Nodes/CircleConst.h>
21
22 namespace
23 {
24
25 bool replace_sub_with_const_rhs(luci::CircleSub *sub)
26 {
27   auto const_rhs = dynamic_cast<luci::CircleConst *>(sub->y());
28   if (const_rhs == nullptr)
29     return false;
30
31   auto graph = sub->graph();
32
33   auto neg_const_rhs = luci::clone(const_rhs);
34   if (neg_const_rhs->dtype() == loco::DataType::FLOAT32)
35   {
36     for (uint32_t i = 0; i < neg_const_rhs->size<loco::DataType::FLOAT32>(); ++i)
37       neg_const_rhs->at<loco::DataType::FLOAT32>(i) *= -1.0;
38   }
39   else
40   {
41     // TODO Support more data type
42     return false;
43   }
44
45   auto add = graph->nodes()->create<luci::CircleAdd>();
46   add->x(sub->x());
47   add->y(neg_const_rhs);
48   add->name(sub->name());
49   add->fusedActivationFunction(sub->fusedActivationFunction());
50   loco::replace(sub).with(add);
51   return true;
52 }
53
54 } // namespace
55
56 namespace luci
57 {
58
59 bool ReplaceSubWithAddPass::run(loco::Graph *g)
60 {
61   bool changed = false;
62   for (auto node : loco::active_nodes(loco::output_nodes(g)))
63   {
64     if (auto sub = dynamic_cast<luci::CircleSub *>(node))
65     {
66       if (replace_sub_with_const_rhs(sub))
67         changed = true;
68     }
69   }
70
71   return changed;
72 }
73
74 } // namespace luci