e4b0388f9d51c2d983c0fbe2d342580b6d121c0d
[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},
32       _dynamic_tensor_mgr{new DynamicTensorManager(_tensor_reg->base_reg())},
33       _static_tensor_mgr{new cpu_common::StaticTensorManager(
34           _tensor_reg->base_reg(), _dynamic_tensor_mgr->dynamic_mem_mgr().get())}
35 {
36   /* empty */
37 }
38
39 void TensorBuilder::registerTensorInfo(const ir::OperandIndex &ind, const ir::OperandInfo &info,
40                                        ir::Layout backend_layout)
41 {
42   _tensor_info_map.emplace(ind, info);
43
44   _tensor_layout_map.insert({ind, backend_layout});
45
46   if (info.isDynamic())
47   {
48     _dynamic_tensor_mgr->buildTensor(ind, info, _tensor_layout_map[ind]);
49   }
50   else
51   {
52     _static_tensor_mgr->buildTensor(ind, info, _tensor_layout_map[ind], info.isConstant());
53   }
54 }
55
56 void TensorBuilder::notifyFirstUse(const ir::OperandIndex &ind)
57 {
58   // TODO Enhance the way of checking user tensors
59   if (_tensor_info_map.find(ind) == _tensor_info_map.end()) // Do not proceed for user tensors
60     return;
61
62   const auto tensor_info = _tensor_info_map.at(ind);
63
64   if (!nativeOwnTensorAt(ind)->is_dynamic())
65   {
66     const auto size = tensor_info.total_size();
67     _static_tensor_mgr->claimPlan(ind, size);
68   }
69 }
70
71 void TensorBuilder::notifyLastUse(const ir::OperandIndex &ind)
72 {
73   // TODO Enhance the way of checking user tensors
74   if (_tensor_info_map.find(ind) == _tensor_info_map.end()) // Do not proceed for user tensors
75     return;
76
77   if (!nativeOwnTensorAt(ind)->is_dynamic())
78   {
79     _static_tensor_mgr->releasePlan(ind);
80   }
81 }
82
83 bool TensorBuilder::isRegistered(const ir::OperandIndex &ind) const
84 {
85   // User tensors are not registered in _tensor_info_map but objects for them are exist
86   // in the tensor registry.
87   // TODO Enhance the way of checking user tensors
88   if (_tensor_reg->getITensor(ind))
89     return true;
90   return _tensor_info_map.find(ind) != _tensor_info_map.end();
91 }
92
93 void TensorBuilder::prepare(void)
94 {
95   _static_tensor_mgr->allocateConsts();
96   _static_tensor_mgr->allocateNonconsts();
97 }
98
99 void TensorBuilder::allocate()
100 {
101   // NOTE For now nothing to do. Allocation is done in prepare stage, which is not appropriate
102   //      This is because CPU kernels require `ITensor`s to be allocated before Kernel Generation.
103 }
104
105 IDynamicTensorManager *TensorBuilder::dynamicTensorManager(void)
106 {
107   return _dynamic_tensor_mgr.get();
108 }
109
110 cpu_common::Tensor *TensorBuilder::nativeOwnTensorAt(const ir::OperandIndex &ind)
111 {
112   return _tensor_reg->getNativeOwnTensor(ind);
113 }
114
115 } // namespace controlflow
116 } // namespace backend
117 } // namespace onert