2 * Copyright (c) 2020 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/ResolveCustomOpBatchMatMulPass.h"
19 #include <luci/IR/CircleNodes.h>
20 #include <luci/Profile/CircleNodeOrigin.h>
22 #include <flatbuffers/flexbuffers.h>
27 bool resolve_custom_op(luci::CircleCustom *cop)
29 const std::string custom_code = cop->custom_code();
30 const std::vector<uint8_t> custom_options = cop->custom_options();
32 if (custom_code == "BatchMatMulV2")
34 auto name = cop->name();
35 assert(name.length() > 0);
37 auto batch_matmul = cop->graph()->nodes()->create<luci::CircleBatchMatMul>();
39 batch_matmul->x(cop->inputs(0));
40 batch_matmul->y(cop->inputs(1));
41 // TODO find much better way of parsing custom_options
43 auto map = flexbuffers::GetRoot(custom_options).AsMap();
44 batch_matmul->adj_x(map["adj_x"].AsBool());
45 batch_matmul->adj_y(map["adj_y"].AsBool());
46 batch_matmul->name(name + "/BatchMatMul");
47 luci::add_origin(batch_matmul, luci::get_origin(cop));
49 auto customOut = loco::succs(cop);
50 assert(customOut.size() == 1);
51 replace(*customOut.begin()).with(batch_matmul);
67 * [CircleNode] [CircleNode]
69 * [CircleCustom]("BatchMatMulV2")
78 * [CircleNode] [CircleNode]
85 bool ResolveCustomOpBatchMatMulPass::run(loco::Graph *g)
88 for (auto node : loco::active_nodes(loco::output_nodes(g)))
90 auto cop = dynamic_cast<luci::CircleCustom *>(node);
94 if (resolve_custom_op(cop))