Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / loco / src / IR / Graph.cpp
1 /*
2  * Copyright (c) 2019 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 "loco/IR/Graph.h"
18
19 #include <stdex/Memory.h>
20
21 #include <cassert>
22
23 namespace
24 {
25
26 std::unique_ptr<loco::TensorShape> make_tensor_shape(std::initializer_list<loco::Dimension> dims)
27 {
28   auto tensor_shape = stdex::make_unique<loco::TensorShape>();
29
30   tensor_shape->rank(dims.size());
31   {
32     uint32_t axis = 0;
33     for (auto it = dims.begin(); it != dims.end(); ++it)
34     {
35       tensor_shape->dim(axis++) = *it;
36     }
37     assert(axis == dims.size());
38   }
39
40   return tensor_shape;
41 }
42
43 } // namespace
44
45 namespace loco
46 {
47
48 void Mixin<Trait::TensorShaped>::shape(std::initializer_list<Dimension> dims)
49 {
50   shape(make_tensor_shape(dims));
51 }
52
53 GraphInput *Graph::InputContext::create(void)
54 {
55   return take(stdex::make_unique<GraphInput>(size()));
56 }
57
58 GraphOutput *Graph::OutputContext::create(void)
59 {
60   return take(stdex::make_unique<GraphOutput>(size()));
61 }
62
63 std::set<loco::Node *> all_nodes(loco::Graph *g)
64 {
65   std::set<loco::Node *> res;
66
67   for (uint32_t n = 0; n < g->nodes()->size(); ++n)
68   {
69     res.insert(g->nodes()->at(n));
70   }
71
72   return res;
73 }
74
75 std::vector<Node *> input_nodes(const Graph *g)
76 {
77   std::map<GraphInputIndex, loco::Node *> table;
78
79   for (uint32_t n = 0; n < g->nodes()->size(); ++n)
80   {
81     auto node = g->nodes()->at(n);
82
83     if (auto service = node->dialect()->service<GraphInputIndexQueryService>())
84     {
85       if (service->associated(node))
86       {
87         auto input_index = service->index(node);
88         assert(table.find(input_index) == table.end());
89         table[input_index] = node;
90       }
91     }
92   }
93
94   std::vector<loco::Node *> res;
95
96   for (uint32_t n = 0; n < g->inputs()->size(); ++n)
97   {
98     auto it = table.find(n);
99     res.emplace_back(it == table.end() ? nullptr : it->second);
100   }
101
102   return res;
103 }
104
105 std::vector<loco::Node *> output_nodes(loco::Graph *g)
106 {
107   std::map<GraphOutputIndex, loco::Node *> table;
108
109   for (uint32_t n = 0; n < g->nodes()->size(); ++n)
110   {
111     auto node = g->nodes()->at(n);
112
113     if (auto service = node->dialect()->service<GraphOutputIndexQueryService>())
114     {
115       if (service->associated(node))
116       {
117         auto output_index = service->index(node);
118         assert(table.find(output_index) == table.end());
119         table[output_index] = node;
120       }
121     }
122   }
123
124   std::vector<loco::Node *> res;
125
126   for (uint32_t n = 0; n < g->outputs()->size(); ++n)
127   {
128     auto it = table.find(n);
129     res.emplace_back(it == table.end() ? nullptr : it->second);
130   }
131
132   return res;
133 }
134
135 std::unique_ptr<Graph> make_graph(void) { return std::unique_ptr<Graph>{new Graph}; }
136
137 } // namespace loco