ONE - On-device Neural Engine
Loading...
Searching...
No Matches
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
23namespace onert
24{
25namespace backend
26{
27namespace builtin
28{
29
30TensorBuilder::TensorBuilder(const std::shared_ptr<TensorRegistry> &tensor_reg)
31 : _tensor_reg{tensor_reg}, _dynamic_tensor_mgr{new DynamicTensorManager(_tensor_reg->base_reg())},
32 _static_tensor_mgr{new basic::StaticTensorManager(
33 _tensor_reg->base_reg(), _dynamic_tensor_mgr.get(), ir::OperandIndexMap<ir::OperandIndex>{})}
34{
35 /* empty */
36}
37
38void TensorBuilder::registerTensorInfo(const ir::OperandIndex &ind, const ir::OperandInfo &info)
39{
40 _tensor_info_map.emplace(ind, info);
41
42 VERBOSE_F() << "cpucommon REGISTER!! " << ind << std::endl;
43 if (info.isDynamic())
44 {
45 _dynamic_tensor_mgr->buildTensor(ind, info);
46 }
47 else
48 {
49 _static_tensor_mgr->buildTensor(ind, info, info.isConstant());
50 }
51}
52
53void TensorBuilder::notifyFirstUse(const ir::OperandIndex &ind)
54{
55 // TODO Enhance the way of checking user tensors
56 if (_tensor_info_map.find(ind) == _tensor_info_map.end()) // Do not proceed for user tensors
57 return;
58
59 const auto &tensor_info = _tensor_info_map.at(ind);
60
61 if (!nativeOwnTensorAt(ind)->is_dynamic())
62 {
63 const auto size = tensor_info.total_size();
64 _static_tensor_mgr->claimPlan(ind, size);
65 }
66}
67
68void TensorBuilder::notifyLastUse(const ir::OperandIndex &ind)
69{
70 // TODO Enhance the way of checking user tensors
71 if (_tensor_info_map.find(ind) == _tensor_info_map.end()) // Do not proceed for user tensors
72 return;
73
74 if (!nativeOwnTensorAt(ind)->is_dynamic())
75 {
76 _static_tensor_mgr->releasePlan(ind);
77 }
78}
79
80bool TensorBuilder::isRegistered(const ir::OperandIndex &ind) const
81{
82 // User tensors are not registered in _tensor_info_map but objects for them are exist
83 // in the tensor registry.
84 // TODO Enhance the way of checking user tensors
85 if (_tensor_reg->getITensor(ind))
86 return true;
87 return _tensor_info_map.find(ind) != _tensor_info_map.end();
88}
89
90void TensorBuilder::allocate(void) { _static_tensor_mgr->allocateNonconsts(); }
91
92DynamicTensorManager *TensorBuilder::dynamicTensorManager(void)
93{
94 return _dynamic_tensor_mgr.get();
95}
96
97basic::Tensor *TensorBuilder::nativeOwnTensorAt(const ir::OperandIndex &ind)
98{
99 return _tensor_reg->getNativeOwnTensor(ind);
100}
101
102} // namespace builtin
103} // namespace backend
104} // namespace onert
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:56
volatile const char info[]
int32_t size[5]
Definition Slice.cpp:35
#define VERBOSE_F()
Definition logging.h:75