Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / loader / KernelBuilderHelper.h
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 #ifndef LUCI_INTERPRETER_LOADER_KERNELBUILDER_HELPER_H
18 #define LUCI_INTERPRETER_LOADER_KERNELBUILDER_HELPER_H
19
20 #include "core/Kernel.h"
21 #include "core/RuntimeGraph.h"
22
23 #include <loco/IR/Graph.h>
24 #include <loco/IR/Node.h>
25
26 #include <vector>
27 #include <unordered_map>
28
29 namespace luci_interpreter
30 {
31
32 class KernelBuilderHelper
33 {
34 public:
35   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)
39   {
40   }
41
42 public:
43   const Tensor *getInputTensor(const loco::Node *node) const;
44   const Tensor *getOptionalInputTensor(const loco::Node *node) const;
45
46   Tensor *getOutputTensor(const loco::Node *node) const;
47   std::vector<Tensor *> getOutputTensors(const std::vector<const loco::Node *> &nodes) const;
48
49   RuntimeGraph *getRuntimeGraph(const loco::Graph *graph) const;
50
51 public:
52   const std::unordered_map<const loco::Graph *, RuntimeGraph *> &graph_to_runtime_graph() const
53   {
54     return _graph_to_runtime_graph;
55   }
56
57   const std::unordered_map<const loco::Node *, Tensor *> &node_to_tensor() const
58   {
59     return _node_to_tensor;
60   }
61
62 private:
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;
65 };
66
67 template <typename CircleNodeOut>
68 std::vector<const loco::Node *> collectOutputNodes(const loco::Node *node)
69 {
70   std::vector<const CircleNodeOut *> output_nodes;
71   for (const loco::Node *loco_node : loco::succs(node))
72   {
73     output_nodes.push_back(loco::must_cast<const CircleNodeOut *>(loco_node));
74   }
75   std::sort(output_nodes.begin(), output_nodes.end(),
76             [](const CircleNodeOut *node1, const CircleNodeOut *node2) {
77               return node1->index() < node2->index();
78             });
79   return {output_nodes.cbegin(), output_nodes.cend()};
80 }
81
82 } // namespace luci_interpreter
83
84 #endif // LUCI_INTERPRETER_LOADER_KERNELBUILDER_HELPER_H