Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / backend / controlflow / TensorBuilder.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 "TensorBuilder.h"
18
19 #include <util/logging.h>
20
21 #include <cassert>
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace controlflow
28 {
29
30 TensorBuilder::TensorBuilder()
31     : _tensor_reg{new cpu_common::TensorRegistry()}, _user_tensor_reg{new UserTensorRegistry()},
32       _static_tensor_mgr{new cpu_common::StaticTensorManager(_tensor_reg)},
33       _dynamic_tensor_mgr{new DynamicTensorManager(_tensor_reg, _user_tensor_reg)}
34 {
35   /* empty */
36 }
37
38 void TensorBuilder::registerTensorInfo(const ir::OperandIndex &ind, const ir::OperandInfo &info,
39                                        ir::Layout backend_layout)
40 {
41   _tensor_info_map.emplace(ind, info);
42
43   _tensor_layout_map.insert({ind, backend_layout});
44
45   if (info.isDynamic())
46   {
47     _dynamic_tensor_mgr->buildTensor(ind, info, _tensor_layout_map[ind]);
48   }
49   else
50   {
51     _static_tensor_mgr->buildTensor(ind, info, _tensor_layout_map[ind], info.isConstant());
52   }
53 }
54
55 void TensorBuilder::notifyFirstUse(const ir::OperandIndex &ind)
56 {
57   assert(_tensor_info_map.find(ind) != _tensor_info_map.end());
58   const auto tensor_info = _tensor_info_map.at(ind);
59
60   if (!at(ind)->is_dynamic())
61   {
62     const auto size = tensor_info.total_size();
63     _static_tensor_mgr->claimPlan(ind, size);
64   }
65 }
66
67 void TensorBuilder::notifyLastUse(const ir::OperandIndex &ind)
68 {
69   if (!at(ind)->is_dynamic())
70   {
71     _static_tensor_mgr->releasePlan(ind);
72   }
73 }
74
75 bool TensorBuilder::isRegistered(const ir::OperandIndex &ind) const
76 {
77   return _tensor_info_map.find(ind) != _tensor_info_map.end();
78 }
79
80 void TensorBuilder::prepare(void)
81 {
82   _static_tensor_mgr->allocateConsts();
83   _static_tensor_mgr->allocateNonconsts();
84 }
85
86 void TensorBuilder::allocate()
87 {
88   // NOTE For now nothing to do. Allocation is done in prepare stage, which is not appropriate
89   //      This is because CPU kernels require `ITensor`s to be allocated before Kernel Generation.
90 }
91
92 std::shared_ptr<ITensor> TensorBuilder::tensorAt(const ir::OperandIndex &ind)
93 {
94   // NOTE Find from User Tensor Registry first
95   // FIXME There may be both user tensor and native tensor for a `ind` which is a waste
96   auto user_tensor = _user_tensor_reg->getITensor(ind);
97   auto tensor = _tensor_reg->getITensor(ind);
98   if (user_tensor)
99   {
100     return user_tensor;
101   }
102   else
103     return tensor;
104 }
105
106 void TensorBuilder::iterate(const IterateFunction &fn) { _static_tensor_mgr->iterate(fn); }
107
108 std::shared_ptr<cpu_common::Tensor> TensorBuilder::at(const ir::OperandIndex &ind)
109 {
110   return _tensor_reg->getNativeTensor(ind);
111 }
112
113 std::unique_ptr<ITensorManager> TensorBuilder::releaseStaticTensorManager(void)
114 {
115   return std::move(_static_tensor_mgr);
116 }
117
118 std::unique_ptr<ITensorManager> TensorBuilder::releaseDynamicTensorManager(void)
119 {
120   return std::move(_dynamic_tensor_mgr);
121 }
122
123 void TensorBuilder::setUserTensor(const ir::OperandIndex &ind,
124                                   const std::shared_ptr<UserTensor> &tensor)
125 {
126   _user_tensor_reg->setNativeTensor(ind, tensor);
127 }
128
129 } // namespace controlflow
130 } // namespace backend
131 } // namespace onert