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