ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TensorBuilder.cc
Go to the documentation of this file.
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 "TensorBuilder.h"
18
19#include <util/logging.h>
20
21#include <cassert>
22
24{
25
26TensorBuilder::TensorBuilder(const std::shared_ptr<TensorRegistry> &tensor_reg)
27 : _tensor_reg{tensor_reg}, _dynamic_tensor_mgr{new DynamicTensorManager(_tensor_reg->base_reg())},
28 _static_tensor_mgr{new basic::StaticTensorManager(
29 _tensor_reg->base_reg(), _dynamic_tensor_mgr.get(), ir::OperandIndexMap<ir::OperandIndex>{})}
30{
31 /* empty */
32}
33
34void TensorBuilder::registerTensorInfo(const ir::OperandIndex &ind, const ir::OperandInfo &info)
35{
36 _tensor_info_map.emplace(ind, info);
37
38 VERBOSE_F() << "cpucommon REGISTER!! " << ind << std::endl;
39 if (info.isDynamic())
40 {
41 _dynamic_tensor_mgr->buildTensor(ind, info);
42 }
43 else
44 {
45 _static_tensor_mgr->buildTensor(ind, info, info.isConstant());
46 }
47}
48
49void TensorBuilder::notifyFirstUse(const ir::OperandIndex &ind)
50{
51 // TODO Enhance the way of checking user tensors
52 if (_tensor_info_map.find(ind) == _tensor_info_map.end()) // Do not proceed for user tensors
53 return;
54
55 const auto &tensor_info = _tensor_info_map.at(ind);
56
57 if (!nativeOwnTensorAt(ind)->is_dynamic())
58 {
59 const auto size = tensor_info.total_size();
60 _static_tensor_mgr->claimPlan(ind, size);
61 }
62}
63
64void TensorBuilder::notifyLastUse(const ir::OperandIndex &ind)
65{
66 // TODO Enhance the way of checking user tensors
67 if (_tensor_info_map.find(ind) == _tensor_info_map.end()) // Do not proceed for user tensors
68 return;
69
70 if (!nativeOwnTensorAt(ind)->is_dynamic())
71 {
72 _static_tensor_mgr->releasePlan(ind);
73 }
74}
75
76bool TensorBuilder::isRegistered(const ir::OperandIndex &ind) const
77{
78 // User tensors are not registered in _tensor_info_map but objects for them are exist
79 // in the tensor registry.
80 // TODO Enhance the way of checking user tensors
81 if (_tensor_reg->getITensor(ind))
82 return true;
83 return _tensor_info_map.find(ind) != _tensor_info_map.end();
84}
85
86void TensorBuilder::allocate(void) { _static_tensor_mgr->allocateNonconsts(); }
87
88DynamicTensorManager *TensorBuilder::dynamicTensorManager(void)
89{
90 return _dynamic_tensor_mgr.get();
91}
92
93basic::Tensor *TensorBuilder::nativeOwnTensorAt(const ir::OperandIndex &ind)
94{
95 return _tensor_reg->getNativeOwnTensor(ind);
96}
97
98} // namespace onert::backend::builtin
Class to manage dynamic tensor and its memory.
TensorBuilder(const std::shared_ptr< TensorRegistry > &tensor_reg)
Class to save tensor's shape and type.
Definition OperandInfo.h:54
volatile const char info[]
int32_t size[5]
Definition Slice.cpp:35
#define VERBOSE_F()
Definition logging.h:69