Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / train / TensorManager.h
1 /*
2  * Copyright (c) 2023 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_TRAIN_TENSOR_MANAGER_H__
18 #define __ONERT_BACKEND_TRAIN_TENSOR_MANAGER_H__
19
20 #include "MemoryManager.h"
21 #include "TensorRegistry.h"
22
23 #include <ir/OperandIndexMap.h>
24 #include <ir/OperandInfo.h>
25
26 namespace onert
27 {
28 namespace backend
29 {
30 namespace train
31 {
32
33 class TensorManager
34 {
35 public:
36   TensorManager(const std::shared_ptr<TensorRegistry> &reg, const std::string planner_id);
37   virtual ~TensorManager() = default;
38
39   void allocateNonConstTensors();
40   void allocateTrainableTensors();
41   void allocateDerivativeTensors();
42   void allocateGradientTensors();
43   // TODO Add member functions to deallocate tensors
44
45   void claimNonConstPlan(const ir::OperandIndex &ind);
46   void releaseNonConstPlan(const ir::OperandIndex &ind);
47   void claimTrainablePlan(const ir::OperandIndex &ind);
48   void releaseTrainablePlan(const ir::OperandIndex &ind);
49   void claimDerivativePlan(const ir::OperandIndex &ind);
50   void releaseDerivativePlan(const ir::OperandIndex &ind);
51   void claimGradientPlan(const ir::OperandIndex &ind);
52   void releaseGradientPlan(const ir::OperandIndex &ind);
53
54 private:
55   std::unique_ptr<MemoryManager> _nonconst_mgr;
56   std::unique_ptr<MemoryManager> _trainable_mgr;
57   std::unique_ptr<MemoryManager> _derivative_mgr;
58   std::unique_ptr<MemoryManager> _gradient_mgr;
59   const std::shared_ptr<TensorRegistry> _tensors;
60 };
61
62 } // namespace train
63 } // namespace backend
64 } // namespace onert
65
66 #endif // __ONERT_BACKEND_TRAIN_TENSOR_MANAGER_H__