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 #ifndef LUCI_INTERPRETER_LOADER_KERNELBUILDER_HELPER_H
18 #define LUCI_INTERPRETER_LOADER_KERNELBUILDER_HELPER_H
20 #include "core/Kernel.h"
21 #include "core/RuntimeGraph.h"
23 #include <loco/IR/Graph.h>
24 #include <loco/IR/Node.h>
27 #include <unordered_map>
29 namespace luci_interpreter
32 class KernelBuilderHelper
36 const std::unordered_map<const loco::Graph *, RuntimeGraph *> &graph_to_runtime_graph,
37 const std::unordered_map<const loco::Node *, Tensor *> &node_to_tensor)
38 : _graph_to_runtime_graph(graph_to_runtime_graph), _node_to_tensor(node_to_tensor)
43 const Tensor *getInputTensor(const loco::Node *node) const;
44 const Tensor *getOptionalInputTensor(const loco::Node *node) const;
46 Tensor *getOutputTensor(const loco::Node *node) const;
47 std::vector<Tensor *> getOutputTensors(const std::vector<const loco::Node *> &nodes) const;
49 RuntimeGraph *getRuntimeGraph(const loco::Graph *graph) const;
52 const std::unordered_map<const loco::Graph *, RuntimeGraph *> &graph_to_runtime_graph() const
54 return _graph_to_runtime_graph;
57 const std::unordered_map<const loco::Node *, Tensor *> &node_to_tensor() const
59 return _node_to_tensor;
63 const std::unordered_map<const loco::Graph *, RuntimeGraph *> &_graph_to_runtime_graph;
64 const std::unordered_map<const loco::Node *, Tensor *> &_node_to_tensor;
67 template <typename CircleNodeOut>
68 std::vector<const loco::Node *> collectOutputNodes(const loco::Node *node)
70 std::vector<const CircleNodeOut *> output_nodes;
71 for (const loco::Node *loco_node : loco::succs(node))
73 output_nodes.push_back(loco::must_cast<const CircleNodeOut *>(loco_node));
75 std::sort(output_nodes.begin(), output_nodes.end(),
76 [](const CircleNodeOut *node1, const CircleNodeOut *node2) {
77 return node1->index() < node2->index();
79 return {output_nodes.cbegin(), output_nodes.cend()};
82 } // namespace luci_interpreter
84 #endif // LUCI_INTERPRETER_LOADER_KERNELBUILDER_HELPER_H