446427d64ebad73311215196b5d10918ec09fcfb
[platform/core/ml/nnfw.git] / runtime / onert / core / src / backend / controlflow / DynamicTensorManager.h
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 #ifndef __ONERT_BACKEND_CONTROLFLOW_DYNAMICTENSOR_MANAGER_H__
18 #define __ONERT_BACKEND_CONTROLFLOW_DYNAMICTENSOR_MANAGER_H__
19
20 #include "UserTensorRegistry.h"
21
22 #include <backend/IDynamicTensorManager.h>
23 #include <backend/cpu_common/MemoryManager.h>
24 #include <backend/cpu_common/TensorRegistry.h>
25 #include <ir/OperandInfo.h>
26 #include <ir/Operation.h>
27 #include <ir/Index.h>
28
29 namespace onert
30 {
31 namespace backend
32 {
33 namespace controlflow
34 {
35
36 // TODO Find optimized algorithm to manage memory.
37
38 /**
39  * @brief Class to manage dynamic tensor and its memory
40  */
41 class DynamicTensorManager : public backend::IDynamicTensorManager
42 {
43 public:
44   DynamicTensorManager(const std::shared_ptr<cpu_common::TensorRegistry> &reg,
45                        const std::shared_ptr<UserTensorRegistry> &user_reg);
46
47   virtual ~DynamicTensorManager() = default;
48
49   void applyShape(const ir::OperandIndex &ind, const ir::Shape &new_shape) override;
50
51   void buildTensor(const ir::OperandIndex &ind, const ir::OperandInfo &tensor_info,
52                    ir::Layout backend_layout);
53
54   void planDealloc(ir::OperationIndex op_ind, ir::OperandIndex operand_ind) override;
55   void deallocInput(ir::OperationIndex op_ind) override;
56   void deallocSubgraphOutput(ir::OperandIndex ind) override;
57
58 private:
59   /**
60    * @brief Memory manager for dynamic tensor.
61    * @todo  DynamicMemoryManager is not optimized. Optimized one is needed
62    */
63   std::shared_ptr<cpu_common::DynamicMemoryManager> _dynamic_mem_mgr;
64   // TODO Refactoring : Merge two TensorRegistries into one
65   const std::shared_ptr<cpu_common::TensorRegistry> _tensors;
66   const std::shared_ptr<UserTensorRegistry> _user_tensors;
67
68   // contains list of dynamic tensor index, which can be deallocated after running operation
69   // note: this map could contain static tensor index too. Careful use is required.
70   std::unordered_map<ir::OperationIndex, std::unordered_set<ir::OperandIndex>> _dealloc_tensor_map;
71 };
72
73 } // namespace controlflow
74 } // namespace backend
75 } // namespace onert
76
77 #endif // __ONERT_BACKEND_CONTROLFLOW_DYNAMICTENSOR_MANAGER_H__