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