Imported Upstream version 1.9.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(const std::shared_ptr<TensorRegistry> &tensor_reg)
31     : _tensor_reg{tensor_reg}, _dynamic_tensor_mgr{new DynamicTensorManager(_tensor_reg)},
32       _static_tensor_mgr{
33           new cpu_common::StaticTensorManager(_tensor_reg->base_reg(), _dynamic_tensor_mgr.get())}
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   // TODO Enhance the way of checking user tensors
58   if (_tensor_info_map.find(ind) == _tensor_info_map.end()) // Do not proceed for user tensors
59     return;
60
61   const auto tensor_info = _tensor_info_map.at(ind);
62
63   if (!nativeOwnTensorAt(ind)->is_dynamic())
64   {
65     const auto size = tensor_info.total_size();
66     _static_tensor_mgr->claimPlan(ind, size);
67   }
68 }
69
70 void TensorBuilder::notifyLastUse(const ir::OperandIndex &ind)
71 {
72   // TODO Enhance the way of checking user tensors
73   if (_tensor_info_map.find(ind) == _tensor_info_map.end()) // Do not proceed for user tensors
74     return;
75
76   if (!nativeOwnTensorAt(ind)->is_dynamic())
77   {
78     _static_tensor_mgr->releasePlan(ind);
79   }
80 }
81
82 bool TensorBuilder::isRegistered(const ir::OperandIndex &ind) const
83 {
84   // User tensors are not registered in _tensor_info_map but objects for them are exist
85   // in the tensor registry.
86   // TODO Enhance the way of checking user tensors
87   if (_tensor_reg->getITensor(ind))
88     return true;
89   return _tensor_info_map.find(ind) != _tensor_info_map.end();
90 }
91
92 void TensorBuilder::prepare(void)
93 {
94   _static_tensor_mgr->allocateConsts();
95   _static_tensor_mgr->allocateNonconsts();
96 }
97
98 void TensorBuilder::allocate()
99 {
100   // NOTE For now nothing to do. Allocation is done in prepare stage, which is not appropriate
101   //      This is because CPU kernels require `ITensor`s to be allocated before Kernel Generation.
102 }
103
104 std::shared_ptr<cpu_common::Tensor> TensorBuilder::nativeOwnTensorAt(const ir::OperandIndex &ind)
105 {
106   return _tensor_reg->getNativeOwnTensor(ind);
107 }
108
109 std::unique_ptr<ITensorManager> TensorBuilder::releaseStaticTensorManager(void)
110 {
111   return std::move(_static_tensor_mgr);
112 }
113
114 std::unique_ptr<ITensorManager> TensorBuilder::releaseDynamicTensorManager(void)
115 {
116   return std::move(_dynamic_tensor_mgr);
117 }
118
119 void TensorBuilder::setNativeUserTensor(const ir::OperandIndex &ind,
120                                         const std::shared_ptr<UserTensor> &tensor)
121 {
122   _tensor_reg->setNativeUserTensor(ind, tensor);
123 }
124
125 } // namespace controlflow
126 } // namespace backend
127 } // namespace onert