Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / backend / controlflow / DynamicTensorManager.cc
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 "DynamicTensorManager.h"
18
19 #include "util/logging.h"
20 #include "util/Exceptions.h"
21 #include "ir/DataType.h"
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace controlflow
28 {
29
30 DynamicTensorManager::DynamicTensorManager(const std::shared_ptr<TensorRegistry> &tensors)
31     : _dynamic_mem_mgr{new cpu_common::DynamicMemoryManager()}, _tensors{tensors}
32 {
33   // DO NOTHING
34 }
35
36 void DynamicTensorManager::applyShape(const ir::OperandIndex &ind, const ir::Shape &new_shape)
37 {
38   // NOTE Handle user tensors first
39   auto user_tensor = _tensors->getNativeUserTensor(ind);
40   if (user_tensor)
41   {
42     // User tensors cannot be reallocated.
43     auto buffer_size = user_tensor->total_size();
44     auto new_size = new_shape.num_elements() * sizeOfDataType(user_tensor->data_type());
45     if (buffer_size < new_size)
46       throw InsufficientBufferSizeException{"Output buffer size is less than output tensor size"};
47     user_tensor->setShape(new_shape);
48     return;
49   }
50
51   // NOTE Then handle own tensors
52   auto tensor = _tensors->getNativeOwnTensor(ind);
53   assert(tensor);
54
55   bool previously_dynamic = tensor->is_dynamic();
56
57   auto allocTensorMem = [&](bool overwrite = false) {
58     auto capacity = tensor->total_size();
59     auto alloc = _dynamic_mem_mgr->allocate(ind, capacity);
60
61     if (overwrite)
62       tensor->overwriteBuffer(alloc);
63     else
64       tensor->setBuffer(alloc);
65   };
66
67   if (!previously_dynamic)
68   {
69     // TODO deallocate tensor->buffer()
70     // issue is that staticTensorManager might have allocate this memory
71     tensor->setShape(new_shape);
72     tensor->set_dynamic();
73     allocTensorMem(true);
74   }
75   else if (tensor->buffer() == nullptr)
76   {
77     tensor->setShape(new_shape);
78     tensor->set_dynamic();
79     allocTensorMem();
80   }
81   // when buffer was already allocated and new_shape requires different size
82   else
83   {
84     auto previous_size = tensor->total_size();
85     auto new_size = new_shape.num_elements() * sizeOfDataType(tensor->data_type());
86     if (previous_size != new_size)
87     {
88       _dynamic_mem_mgr->deallocate(ind);
89
90       tensor->setShape(new_shape);
91       tensor->set_dynamic();
92       allocTensorMem(true);
93     }
94     else
95     { // when buffer with same size was already allocated, shape could differ
96       tensor->setShape(new_shape);
97     }
98   }
99 }
100
101 void DynamicTensorManager::buildTensor(const ir::OperandIndex &ind,
102                                        const ir::OperandInfo &tensor_info,
103                                        ir::Layout backend_layout)
104 {
105   auto tensor = std::make_shared<cpu_common::Tensor>(tensor_info, backend_layout, this);
106   _tensors->setNativeOwnTensor(ind, tensor);
107 }
108
109 void DynamicTensorManager::planDealloc(ir::OperationIndex op_ind, ir::OperandIndex operand_ind)
110 {
111   _dealloc_tensor_map[op_ind].emplace(operand_ind);
112 }
113
114 void DynamicTensorManager::deallocInput(ir::OperationIndex op_ind)
115 {
116   auto find = _dealloc_tensor_map.find(op_ind);
117   if (find == _dealloc_tensor_map.end())
118     return;
119
120   auto &input_set = find->second;
121   for (auto input_ind : input_set)
122   {
123     if (!_tensors->getNativeTensor(input_ind)->is_dynamic())
124       continue;
125
126     _dynamic_mem_mgr->deallocate(input_ind);
127     VERBOSE(DynamicTensorManager) << "Deallocating #" << input_ind.value()
128                                   << " (input of op_ind: " << op_ind.value() << ")" << std::endl;
129   }
130 }
131
132 void DynamicTensorManager::deallocSubgraphOutput(ir::OperandIndex output_ind)
133 {
134   if (!_tensors->getNativeTensor(output_ind)->is_dynamic())
135     return;
136
137   _dynamic_mem_mgr->deallocate(output_ind);
138   VERBOSE(DynamicTensorManager) << "Deallocating #" << output_ind.value()
139                                 << " (output of a subgraph)" << std::endl;
140 }
141
142 } // namespace controlflow
143 } // namespace backend
144 } // namespace onert