Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / backend / basic / StaticTensorManager.cc
1 /*
2  * Copyright (c) 2019 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 "backend/basic/StaticTensorManager.h"
18
19 #include "backend/basic/DynamicTensorManager.h"
20 #include "backend/basic/Tensor.h"
21 #include <util/logging.h>
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace basic
28 {
29
30 StaticTensorManager::StaticTensorManager(const std::shared_ptr<TensorRegistry> &reg,
31                                          DynamicTensorManager *dynamic_tensor_manager)
32   : _nonconst_mgr{new MemoryManager()}, _tensors{reg}, _dynamic_tensor_manager{
33                                                          dynamic_tensor_manager}
34 {
35   // DO NOTHING
36 }
37
38 StaticTensorManager::StaticTensorManager(const std::shared_ptr<TensorRegistry> &reg,
39                                          const std::string planner_id,
40                                          DynamicTensorManager *dynamic_tensor_manager)
41   : _nonconst_mgr{new MemoryManager(planner_id)}, _tensors{reg}, _dynamic_tensor_manager{
42                                                                    dynamic_tensor_manager}
43 {
44   // DO NOTHING
45 }
46
47 void StaticTensorManager::allocateNonconsts(void)
48 {
49   _nonconst_mgr->allocate();
50
51   for (auto &&pair : _tensors->native_tensors())
52   {
53     const auto &ind = pair.first;
54     auto tensor = pair.second.get();
55     if (!_as_constants[ind] && !tensor->is_dynamic())
56     {
57       auto *buffer = _nonconst_mgr->getBuffer(ind);
58       tensor->setBuffer(buffer);
59
60       VERBOSE(CPU_StaticTensorManager)
61         << "TENSOR " << ind << " : " << static_cast<void *>(buffer) << std::endl;
62     }
63   }
64 }
65
66 void StaticTensorManager::deallocateNonconsts(void) { _nonconst_mgr->deallocate(); }
67
68 void StaticTensorManager::buildTensor(const ir::OperandIndex &ind,
69                                       const ir::OperandInfo &tensor_info, ir::Layout backend_layout,
70                                       bool as_const)
71 {
72   assert(!_tensors->getNativeTensor(ind));
73   if (as_const)
74   {
75     auto tensor = std::make_unique<ExternalTensor>(tensor_info, backend_layout);
76     _tensors->setNativeTensor(ind, std::move(tensor));
77   }
78   else
79   {
80     auto tensor = std::make_unique<Tensor>(tensor_info, backend_layout,
81                                            _dynamic_tensor_manager->dynamic_mem_mgr().get());
82     _tensors->setNativeTensor(ind, std::move(tensor));
83   }
84   _as_constants[ind] = as_const;
85 }
86
87 void StaticTensorManager::claimPlan(const ir::OperandIndex &ind, uint32_t size)
88 {
89   assert(_tensors->getNativeTensor(ind));
90
91   // This method is called only when a tensor has proper shape
92   assert(!_tensors->getNativeTensor(ind)->is_dynamic());
93
94   if (!_as_constants[ind])
95     _nonconst_mgr->claimPlan(ind, size);
96 }
97
98 void StaticTensorManager::releasePlan(const ir::OperandIndex &ind)
99 {
100   assert(_tensors->getNativeTensor(ind));
101
102   // This method is called only when a tensor has proper shape
103   assert(!_tensors->getNativeTensor(ind)->is_dynamic());
104
105   if (!_as_constants[ind])
106     _nonconst_mgr->releasePlan(ind);
107 }
108
109 void StaticTensorManager::iterate(const std::function<void(const ir::OperandIndex &)> &fn)
110 {
111   for (const auto &it : _tensors->native_tensors())
112     fn(it.first);
113 }
114
115 } // namespace basic
116 } // namespace backend
117 } // namespace onert