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 : _const_mgr{new DynamicMemoryManager()}, _nonconst_mgr{new MemoryManager()}, _tensors{reg}
35 void StaticTensorManager::allocateConsts(void)
37 for (auto &pair : _tensors->native_tensors())
39 const auto &ind = pair.first;
40 auto tensor = pair.second;
41 if (_as_constants[ind])
43 auto mem_alloc = _const_mgr->allocate(ind, tensor->total_size());
44 tensor->setBuffer(mem_alloc);
45 auto buffer = mem_alloc->base();
46 VERBOSE(CPU_COMMON_StaticTensorManager) << "CONSTANT TENSOR(#" << ind.value()
47 << "): " << static_cast<void *>(buffer)
48 << "size : " << tensor->total_size() << std::endl;
53 void StaticTensorManager::allocateNonconsts(void)
55 _nonconst_mgr->allocate();
57 for (auto &pair : _tensors->native_tensors())
59 const auto &ind = pair.first;
60 auto tensor = pair.second;
61 if (!_as_constants[ind] && !tensor->is_dynamic())
63 auto *buffer = _nonconst_mgr->getBuffer(ind);
64 tensor->setBuffer(buffer);
66 VERBOSE(CPU_COMMON_StaticTensorManager) << "TENSOR(#" << ind.value()
67 << "): " << static_cast<void *>(buffer) << std::endl;
72 void StaticTensorManager::deallocateConsts(void) { _const_mgr->deallocate(); }
74 void StaticTensorManager::deallocateNonconsts(void) { _nonconst_mgr->deallocate(); }
76 void StaticTensorManager::buildTensor(const ir::OperandIndex &ind,
77 const ir::OperandInfo &tensor_info, ir::Layout backend_layout,
80 assert(!_tensors->getNativeTensor(ind));
81 auto tensor = std::make_shared<Tensor>(tensor_info, backend_layout, nullptr);
82 _tensors->setNativeTensor(ind, tensor);
83 _as_constants[ind] = as_const;
86 void StaticTensorManager::claimPlan(const ir::OperandIndex &ind, uint32_t size)
88 assert(_tensors->getNativeTensor(ind));
90 // This method is called only when a tensor has proper shape
91 assert(!_tensors->getNativeTensor(ind)->is_dynamic());
93 if (!_as_constants[ind])
94 _nonconst_mgr->claimPlan(ind, size);
97 void StaticTensorManager::releasePlan(const ir::OperandIndex &ind)
99 assert(_tensors->getNativeTensor(ind));
101 // This method is called only when a tensor has proper shape
102 assert(!_tensors->getNativeTensor(ind)->is_dynamic());
104 if (!_as_constants[ind])
105 _nonconst_mgr->releasePlan(ind);
108 void StaticTensorManager::iterate(const std::function<void(const ir::OperandIndex &)> &fn)
110 for (const auto &it : _tensors->native_tensors())
114 } // namespace cpu_common
115 } // namespace backend