Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / ruy / BackendContext.cc
1 /*
2  * Copyright (c) 2020 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 "BackendContext.h"
18
19 #include "TensorBuilder.h"
20 #include "KernelGenerator.h"
21 #include "util/logging.h"
22 #include "ir/Index.h"
23 #include "ir/OperandIndexMap.h"
24 #include "ir/OperandIndexSequence.h"
25 #include "backend/cpu_common/BackendContextHelpers.h"
26
27 namespace onert
28 {
29 namespace backend
30 {
31 namespace ruy
32 {
33
34 void BackendContext::initConsts()
35 {
36   for (auto &op : operation_list())
37   {
38     constant_initializer->setLayout(op.layout);
39     graph()->operations().at(op.index).accept(*constant_initializer);
40   }
41
42   for (auto ind : operand_list())
43   {
44     const auto &obj = graph()->operands().at(ind);
45     if (obj.isConstant() && !constant_initializer->exist(ind))
46     {
47       constant_initializer->registerDefaultInitializer(ind, obj);
48     }
49   }
50
51   constant_initializer->run();
52 }
53
54 ITensorRegistry *BackendContext::genTensors(const std::vector<onert::ir::OpSequenceIndex> &order,
55                                             const ir::OpSequences &op_seqs,
56                                             const ir::LowerInfoMap &lower_info)
57 {
58   auto model_io = (graph()->getInputs() + graph()->getOutputs()) | ir::Remove::UNDEFINED |
59                   ir::Remove::DUPLICATED;
60   for (auto index : operand_list())
61   {
62     if (model_io.contains(index))
63       continue;
64     const auto &obj = graph()->operands().at(index);
65     const auto frontend_layout = [&]() {
66       if (obj.getUses().size() == 0)
67         return ir::Layout::UNKNOWN;
68       auto use_op_ind = *obj.getUses().begin(); // FIXME What if it has two or more uses?
69       for (auto &operation_info : operation_list())
70       {
71         if (operation_info.index == use_op_ind)
72           return operation_info.layout;
73       }
74       return ir::Layout::UNKNOWN;
75     }();
76     const auto &permute_factor = lower_info.operand.at(index)->def_factors().getOnlyElement();
77     if (permute_factor.backend() != backend())
78       continue;
79     const auto backend_layout = permute_factor.layout();
80     ir::OperandInfo backend_info{permuteShape(obj.shape(), frontend_layout, backend_layout),
81                                  obj.typeInfo(), obj.info().memAllocType(), obj.isConstant()};
82     tensor_builder->registerTensorInfo(index, backend_info, backend_layout);
83   }
84
85   // TODO Get compiler options from compiler, and use it rather than getting it from Env
86   if (util::getConfigString(util::config::EXECUTOR) == "Linear")
87   {
88     cpu_common::planTensors(*this, order, op_seqs, lower_info);
89   }
90   else
91   {
92     // For the executors that does not have fixed linear execution order:
93     // To make tensors never be deallocated, this is a workaround to use static memory planner
94     for (auto ind : operand_list())
95     {
96       if (tensor_builder->isRegistered(ind))
97         tensor_builder->notifyFirstUse(ind);
98     }
99   }
100
101   tensor_builder->prepare();
102
103   return tensor_registry.get();
104 }
105
106 FunctionMap BackendContext::genKernels(const std::vector<onert::ir::OpSequenceIndex> &order,
107                                        const ir::OpSequences &op_seqs)
108 {
109   FunctionMap ret;
110
111   for (auto op_seq_ind : order)
112   {
113     const auto &op_seq = op_seqs.at(op_seq_ind);
114     bool assigned = [&]() {
115       for (auto op_info : operation_list())
116         if (op_seq.exist(op_info.index))
117           return true;
118       return false;
119     }();
120     if (!assigned)
121       continue;
122     auto fn_seq = kernel_gen->generate(op_seqs.at(op_seq_ind));
123     ret.emplace_back(op_seq_ind, std::move(fn_seq));
124   }
125
126   initConsts();
127
128   // NOTE For memory optimization, we want to free some operand data
129   for (auto ind : operand_list())
130   {
131     // TODO Remove const_cast
132     auto &obj = const_cast<ir::Graph *>(graph())->operands().at(ind);
133     obj.releaseData();
134   }
135
136   for (auto &it : ret)
137   {
138     auto &fn_seq = it.second;
139     fn_seq->iterate([&](exec::IFunction &ifunc) { ifunc.prepare(); });
140   }
141
142   return ret;
143 }
144
145 } // namespace ruy
146 } // namespace backend
147 } // namespace onert