2 * Copyright (c) 2020 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 "TensorBuilder.h"
19 #include <util/logging.h>
30 TensorBuilder::TensorBuilder(const std::shared_ptr<TensorRegistry> &tensor_reg)
31 : _tensor_reg{tensor_reg},
32 _dynamic_tensor_mgr{new DynamicTensorManager(_tensor_reg->base_reg())},
34 new cpu_common::StaticTensorManager(_tensor_reg->base_reg(), _dynamic_tensor_mgr.get())}
39 void TensorBuilder::registerTensorInfo(const ir::OperandIndex &ind, const ir::OperandInfo &info,
40 ir::Layout backend_layout)
42 _tensor_info_map.emplace(ind, info);
44 _tensor_layout_map.insert({ind, backend_layout});
48 _dynamic_tensor_mgr->buildTensor(ind, info, _tensor_layout_map[ind]);
52 _static_tensor_mgr->buildTensor(ind, info, _tensor_layout_map[ind], info.isConstant());
56 void TensorBuilder::notifyFirstUse(const ir::OperandIndex &ind)
58 // TODO Enhance the way of checking user tensors
59 if (_tensor_info_map.find(ind) == _tensor_info_map.end()) // Do not proceed for user tensors
62 const auto tensor_info = _tensor_info_map.at(ind);
64 if (!nativeOwnTensorAt(ind)->is_dynamic())
66 const auto size = tensor_info.total_size();
67 _static_tensor_mgr->claimPlan(ind, size);
71 void TensorBuilder::notifyLastUse(const ir::OperandIndex &ind)
73 // TODO Enhance the way of checking user tensors
74 if (_tensor_info_map.find(ind) == _tensor_info_map.end()) // Do not proceed for user tensors
77 if (!nativeOwnTensorAt(ind)->is_dynamic())
79 _static_tensor_mgr->releasePlan(ind);
83 bool TensorBuilder::isRegistered(const ir::OperandIndex &ind) const
85 // User tensors are not registered in _tensor_info_map but objects for them are exist
86 // in the tensor registry.
87 // TODO Enhance the way of checking user tensors
88 if (_tensor_reg->getITensor(ind))
90 return _tensor_info_map.find(ind) != _tensor_info_map.end();
93 void TensorBuilder::prepare(void) { _static_tensor_mgr->allocateNonconsts(); }
95 void TensorBuilder::allocate()
97 // NOTE For now nothing to do. Allocation is done in prepare stage, which is not appropriate
98 // This is because CPU kernels require `ITensor`s to be allocated before Kernel Generation.
101 DynamicTensorManager *TensorBuilder::dynamicTensorManager(void)
103 return _dynamic_tensor_mgr.get();
106 cpu_common::Tensor *TensorBuilder::nativeOwnTensorAt(const ir::OperandIndex &ind)
108 return _tensor_reg->getNativeOwnTensor(ind);
111 } // namespace controlflow
112 } // namespace backend