Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / runtime / onert / core / include / backend / ITensorBuilder.h
1 /*
2  * Copyright (c) 2018 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_ITENSOR_BUILDER_H__
18 #define __ONERT_BACKEND_ITENSOR_BUILDER_H__
19
20 #include <map>
21
22 #include "ir/Index.h"
23 #include "ir/OperandInfo.h"
24 #include "ir/Operation.h"
25 #include "ir/Layout.h"
26 #include "ITensor.h"
27 #include "ITensorManager.h"
28 #include "ITensorRegistry.h"
29 #include "IDynamicTensorManager.h"
30
31 namespace onert
32 {
33 namespace backend
34 {
35
36 struct ITensorBuilder
37 {
38   using IterateFunction = std::function<void(const ir::OperandIndex &)>;
39
40   virtual ~ITensorBuilder(void) = default;
41
42   /**
43    * @brief Returns true if this TensorBuilder support dynamic tensor
44    */
45   virtual bool supportDynamicTensor() = 0;
46
47   /**
48    * @brief Register tensor information to allocate on backend
49    *
50    * @param ind Index
51    * @param info Info
52    * @param backend_layout Backend layout
53    * @param as_const Whether this tensor is constant
54    */
55   virtual void registerTensorInfo(const ir::OperandIndex &ind, const ir::OperandInfo &info,
56                                   ir::Layout backend_layout) = 0;
57
58   /**
59    * @brief Check if the tensor has been registered with @c registerTensorInfo
60    *
61    * @return true If the tensor has been registered
62    * @return false Otherwise
63    */
64   virtual bool isRegistered(const ir::OperandIndex &) const = 0;
65
66   /**
67    * @brief Get tensor registry
68    *
69    * @return std::shared_ptr<backend::ITensorRegistry> tensor registry object
70    *
71    * @note   Backend should implement this when it has StaticTensorManager and DynamicTensorManager
72    */
73   virtual std::shared_ptr<backend::ITensorRegistry> tensorRegistry() = 0;
74
75 public: // methods for static tensor allocation
76   /**
77    * @brief Let the tensor builder know first use(start of lifetime) of a tensor
78    *        Must be called before calling @c prepare
79    *        Must be run up to once for each tensor before calling @c notifyLastUse
80    *        NOTE: Useful only for static models
81    */
82   virtual void notifyFirstUse(const ir::OperandIndex &) = 0;
83   /**
84    * @brief Let the tensor builder know last use(end of lifetime) of a tensor
85    *        Must be run up to once for each tensor after calling @c notifyFirstUse
86    *        NOTE: Useful only for static models
87    */
88   virtual void notifyLastUse(const ir::OperandIndex &) = 0;
89   /**
90    * @brief Prepare the tensors
91    *        Before calling this, all the tensors must be registered
92    */
93   virtual void prepare(void) = 0;
94   /**
95    * @brief Allocate the tensors
96    *        Before calling this, @c prepare must be called
97    */
98   virtual void allocate() = 0;
99   /**
100    * @brief Some actions after functions' @c IFunction::prepare method.
101    *        This is called right after each function's @c IFunction::prepare function has been
102    *        called.
103    */
104   virtual void postFunctionPrepare() = 0;
105
106   /**
107    * @brief Get the tensor object
108    *
109    * @param ind Index of the tensor
110    * @return std::shared_ptr<ITensor> The tensor object
111    */
112   virtual std::shared_ptr<ITensor> tensorAt(const ir::OperandIndex &ind) = 0;
113
114   /**
115    * @brief Set the migrant tensor object
116    *
117    * @return true if succeeded
118    * @return false if failed or unsupported
119    */
120   virtual bool setMigrantTensor(const ir::OperandIndex &, const std::shared_ptr<IPortableTensor> &)
121   {
122     return false;
123   }
124
125   /**
126    * @brief Iterate over tensors
127    *
128    * @param fn The function to be run
129    */
130   virtual void iterate(const IterateFunction &fn) = 0;
131
132   /**
133    * @brief Release static @c ITensorManger object which was built
134    *        Before calling this, @c allocate must have been called
135    *
136    * @return std::unique_ptr<ITensorManager> Tensor Manager object
137    */
138   virtual std::unique_ptr<ITensorManager> releaseStaticTensorManager(void) = 0;
139
140 public: // methods for dynamic tensor allocation
141   /**
142    * @brief Get dynamicTensorManager. If a backend does not support dynamic tensor, exception
143    *        will be thrown.
144    *
145    * @return pointer of IDynamicTensorManager object
146    *
147    * @note   Since it is a pointer, its life time is from the cration of TensorBuilder
148    *         to the end of execution
149    */
150   virtual IDynamicTensorManager *dynamicTensorManager(void)
151   {
152     throw std::runtime_error("dynamicTensorManager(): NYI");
153   }
154
155   /**
156    * @brief Release dynamic @c ITensorManger object which was built
157    *        Before calling this, @c allocate must have been called
158    *
159    * @return std::unique_ptr<ITensorManager> Tensor Manager object
160    */
161   virtual std::unique_ptr<ITensorManager> releaseDynamicTensorManager(void)
162   {
163     throw std::runtime_error("releaseDynamicTensorManager() for this backend is not supported");
164   }
165 };
166
167 } // namespace backend
168 } // namespace onert
169
170 #endif // __ONERT_BACKEND_ITENSOR_BUILDER_H__