2 * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
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.
17 #include "luci/Pass/ReplaceSubWithAddPass.h"
19 #include <luci/IR/CircleNodes.h>
20 #include <luci/Profile/CircleNodeOrigin.h>
21 #include <luci/Service/Nodes/CircleConst.h>
26 bool replace_sub_with_const_rhs(luci::CircleSub *sub)
28 auto const_rhs = dynamic_cast<luci::CircleConst *>(sub->y());
29 if (const_rhs == nullptr)
32 auto graph = sub->graph();
34 auto neg_const_rhs = luci::clone(const_rhs);
35 if (neg_const_rhs->dtype() == loco::DataType::FLOAT32)
37 for (uint32_t i = 0; i < neg_const_rhs->size<loco::DataType::FLOAT32>(); ++i)
38 neg_const_rhs->at<loco::DataType::FLOAT32>(i) *= -1.0;
42 // TODO Support more data type
46 auto add = graph->nodes()->create<luci::CircleAdd>();
48 add->y(neg_const_rhs);
49 add->name(sub->name());
50 add->fusedActivationFunction(sub->fusedActivationFunction());
51 luci::add_origin(add, luci::get_origin(sub));
52 loco::replace(sub).with(add);
61 bool ReplaceSubWithAddPass::run(loco::Graph *g)
64 for (auto node : loco::active_nodes(loco::output_nodes(g)))
66 if (auto sub = dynamic_cast<luci::CircleSub *>(node))
68 if (replace_sub_with_const_rhs(sub))