ab8ba5756583907d074a0732a25d346578d44eb7
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / TensorBuilder.cc
1 /*
2  * Copyright (c) 2018 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 cpu
28 {
29
30 TensorBuilder::TensorBuilder()
31     : _tensor_reg{new cpu_common::TensorRegistry()},
32       _dynamic_tensor_mgr{new cpu_common::DynamicTensorManager(_tensor_reg)},
33       _static_tensor_mgr{new StaticTensorManager(_tensor_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 layout)
40 {
41   _tensor_info_map.emplace(ind, info);
42
43   // CPU backend supports only one layout as NHWC
44   assert(layout == ir::Layout::NHWC);
45   if (info.isDynamic())
46   {
47     _dynamic_tensor_mgr->buildTensor(ind, info, layout);
48   }
49   else
50   {
51     _static_tensor_mgr->buildTensor(ind, info, layout, 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) { _static_tensor_mgr->allocateNonconsts(); }
81
82 void TensorBuilder::allocate()
83 {
84   // NOTE For now nothing to do. Allocation is done in prepare stage, which is not appropriate
85   //      This is because CPU kernels require `ITensor`s to be allocated before Kernel Generation.
86 }
87
88 std::shared_ptr<ITensor> TensorBuilder::tensorAt(const ir::OperandIndex &ind)
89 {
90   return _tensor_reg->getITensor(ind);
91 }
92
93 std::shared_ptr<IPortableTensor> TensorBuilder::portableAt(const ir::OperandIndex &ind)
94 {
95   return _tensor_reg->getPortableTensor(ind);
96 }
97
98 bool TensorBuilder::setMigrantTensor(const ir::OperandIndex &ind,
99                                      const std::shared_ptr<IPortableTensor> &tensor)
100 {
101   return _tensor_reg->setMigrantTensor(ind, tensor);
102 }
103
104 void TensorBuilder::iterate(const IterateFunction &fn) { _static_tensor_mgr->iterate(fn); }
105
106 std::shared_ptr<Tensor> TensorBuilder::at(const ir::OperandIndex &ind)
107 {
108   return _tensor_reg->getNativeTensor(ind);
109 }
110
111 std::unique_ptr<ITensorManager> TensorBuilder::releaseStaticTensorManager(void)
112 {
113   return std::move(_static_tensor_mgr);
114 }
115
116 std::unique_ptr<ITensorManager> TensorBuilder::releaseDynamicTensorManager(void)
117 {
118   return std::move(_dynamic_tensor_mgr);
119 }
120
121 } // namespace cpu
122 } // namespace backend
123 } // namespace onert