9f2bb3754e213c60baa45df4712e3ad20862018b
[platform/core/ml/nnfw.git] / runtime / onert / core / src / backend / controlflow / TensorBuilder.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_TENSOR_BUILDER_H__
18 #define __ONERT_BACKEND_CONTROLFLOW_TENSOR_BUILDER_H__
19
20 #include <backend/cpu_common/StaticTensorManager.h>
21 #include <backend/cpu_common/TensorRegistry.h>
22 #include <backend/cpu_common/Tensor.h>
23
24 #include <backend/ITensorBuilder.h>
25 #include <ir/OperandIndexMap.h>
26
27 #include <unordered_map>
28
29 #include "DynamicTensorManager.h"
30 #include "UserTensorRegistry.h"
31
32 namespace onert
33 {
34 namespace backend
35 {
36 namespace controlflow
37 {
38
39 class TensorBuilder : public ITensorBuilder
40 {
41 public:
42   TensorBuilder();
43
44   bool supportDynamicTensor() override { return true; }
45
46   /**
47    * @brief     Register tensor information to allocate on CPU backend
48    * @param[in] ind    Operand index
49    * @param[in] info   Operand information
50    * @param[in] layout Operand data layout
51    */
52   void registerTensorInfo(const ir::OperandIndex &ind, const ir::OperandInfo &info,
53                           ir::Layout backend_layout) override;
54
55   void notifyFirstUse(const ir::OperandIndex &) override;
56   void notifyLastUse(const ir::OperandIndex &) override;
57
58   bool isRegistered(const ir::OperandIndex &) const override;
59
60   void prepare(void) override;
61   void allocate() override;
62   void postFunctionPrepare() override { /* DO NOTHING */}
63
64   /**
65    * @brief Get tensor with a specific OperandIndex
66    *
67    * @return shared_ptr<ITensor> if a tensor with given OperandIndex exists. nullptr otherwise.
68    */
69   std::shared_ptr<ITensor> tensorAt(const ir::OperandIndex &ind) override;
70
71   void iterate(const IterateFunction &fn) override;
72
73   std::unique_ptr<ITensorManager> releaseStaticTensorManager(void) override;
74
75   IDynamicTensorManager *dynamicTensorManager(void) override { return _dynamic_tensor_mgr.get(); }
76
77   std::unique_ptr<ITensorManager> releaseDynamicTensorManager(void) override;
78
79   /**
80    * @brief Get tensor with a specific OperandIndex.
81    * @param ind OperandIndex for the tensor. There must exist a tensor with this ind.
82    *        If not, program will crash with assert or exception.
83    * @return shared_ptr<operand::Tensor>
84    */
85   std::shared_ptr<cpu_common::Tensor> at(const ir::OperandIndex &ind);
86   void setUserTensor(const ir::OperandIndex &ind, const std::shared_ptr<UserTensor> &tensor);
87
88   std::shared_ptr<ITensorRegistry> tensorRegistry() override { return _tensor_reg; }
89
90 private:
91   const std::shared_ptr<cpu_common::TensorRegistry> _tensor_reg;
92   const std::shared_ptr<UserTensorRegistry> _user_tensor_reg;
93   std::unique_ptr<cpu_common::StaticTensorManager> _static_tensor_mgr;
94   std::unique_ptr<DynamicTensorManager> _dynamic_tensor_mgr;
95   ir::OperandIndexMap<ir::OperandInfo> _tensor_info_map;
96   ir::OperandIndexMap<ir::Layout> _tensor_layout_map;
97 };
98
99 } // namespace controlflow
100 } // namespace backend
101 } // namespace onert
102
103 #endif // __ONERT_BACKEND_CONTROLFLOW_TENSOR_BUILDER_H__