2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "backend/cpu_common/StaticTensorManager.h"
19 #include "backend/cpu_common/DynamicTensorManager.h"
20 #include <util/logging.h>
29 StaticTensorManager::StaticTensorManager(const std::shared_ptr<TensorRegistry> ®,
30 IDynamicTensorManager *dynamic_tensor_manager)
31 : _const_mgr{new DynamicMemoryManager()}, _nonconst_mgr{new MemoryManager()}, _tensors{reg},
32 _dynamic_tensor_manager{dynamic_tensor_manager}
37 void StaticTensorManager::allocateConsts(void)
39 for (auto &pair : _tensors->native_tensors())
41 const auto &ind = pair.first;
42 auto tensor = pair.second;
43 if (_as_constants[ind])
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;
55 void StaticTensorManager::allocateNonconsts(void)
57 _nonconst_mgr->allocate();
59 for (auto &pair : _tensors->native_tensors())
61 const auto &ind = pair.first;
62 auto tensor = pair.second;
63 if (!_as_constants[ind] && !tensor->is_dynamic())
65 auto *buffer = _nonconst_mgr->getBuffer(ind);
66 tensor->setBuffer(buffer);
68 VERBOSE(CPU_COMMON_StaticTensorManager) << "TENSOR(#" << ind.value()
69 << "): " << static_cast<void *>(buffer) << std::endl;
74 void StaticTensorManager::deallocateConsts(void) { _const_mgr->deallocate(); }
76 void StaticTensorManager::deallocateNonconsts(void) { _nonconst_mgr->deallocate(); }
78 void StaticTensorManager::buildTensor(const ir::OperandIndex &ind,
79 const ir::OperandInfo &tensor_info, ir::Layout backend_layout,
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;
88 void StaticTensorManager::claimPlan(const ir::OperandIndex &ind, uint32_t size)
90 assert(_tensors->getNativeTensor(ind));
92 // This method is called only when a tensor has proper shape
93 assert(!_tensors->getNativeTensor(ind)->is_dynamic());
95 if (!_as_constants[ind])
96 _nonconst_mgr->claimPlan(ind, size);
99 void StaticTensorManager::releasePlan(const ir::OperandIndex &ind)
101 assert(_tensors->getNativeTensor(ind));
103 // This method is called only when a tensor has proper shape
104 assert(!_tensors->getNativeTensor(ind)->is_dynamic());
106 if (!_as_constants[ind])
107 _nonconst_mgr->releasePlan(ind);
110 void StaticTensorManager::iterate(const std::function<void(const ir::OperandIndex &)> &fn)
112 for (const auto &it : _tensors->native_tensors())
116 } // namespace cpu_common
117 } // namespace backend