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