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 <backend/basic/TensorBuilder.h>
19 #include <util/logging.h>
30 TensorBuilder::TensorBuilder(const std::shared_ptr<TensorRegistry> &tensor_reg)
31 : _tensor_reg{tensor_reg}, _dynamic_tensor_mgr{new DynamicTensorManager(_tensor_reg)},
32 _static_tensor_mgr{new StaticTensorManager(_tensor_reg, _dynamic_tensor_mgr.get())}
37 TensorBuilder::TensorBuilder(const std::shared_ptr<TensorRegistry> &tensor_reg,
38 const std::string planner_id)
39 : _tensor_reg{tensor_reg}, _dynamic_tensor_mgr{new DynamicTensorManager(_tensor_reg)},
40 _static_tensor_mgr{new StaticTensorManager(_tensor_reg, planner_id, _dynamic_tensor_mgr.get())}
45 void TensorBuilder::registerTensorInfo(const ir::OperandIndex &ind, const ir::OperandInfo &info,
48 _tensor_info_map.emplace(ind, info);
50 // CPU backend supports only one layout as NHWC
51 assert(layout == ir::Layout::NHWC);
54 _dynamic_tensor_mgr->buildTensor(ind, info, layout);
58 _static_tensor_mgr->buildTensor(ind, info, layout, info.isConstant());
62 void TensorBuilder::notifyFirstUse(const ir::OperandIndex &ind)
64 assert(_tensor_info_map.find(ind) != _tensor_info_map.end());
65 const auto tensor_info = _tensor_info_map.at(ind);
67 if (!_tensor_reg->getNativeTensor(ind)->is_dynamic())
69 const auto size = tensor_info.total_size();
70 _static_tensor_mgr->claimPlan(ind, size);
74 void TensorBuilder::notifyLastUse(const ir::OperandIndex &ind)
76 if (!_tensor_reg->getNativeTensor(ind)->is_dynamic())
78 _static_tensor_mgr->releasePlan(ind);
82 bool TensorBuilder::isRegistered(const ir::OperandIndex &ind) const
84 return _tensor_info_map.find(ind) != _tensor_info_map.end();
87 void TensorBuilder::allocate(void) { _static_tensor_mgr->allocateNonconsts(); }
90 } // namespace backend