Imported Upstream version 1.11.1
[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 Register tensor information to allocate on backend
44    *
45    * @param ind Index
46    * @param info Info
47    * @param backend_layout Backend layout
48    * @param as_const Whether this tensor is constant
49    */
50   virtual void registerTensorInfo(const ir::OperandIndex &ind, const ir::OperandInfo &info,
51                                   ir::Layout backend_layout) = 0;
52
53   /**
54    * @brief Check if the tensor has been registered with @c registerTensorInfo
55    *
56    * @return true If the tensor has been registered
57    * @return false Otherwise
58    */
59   virtual bool isRegistered(const ir::OperandIndex &) const = 0;
60
61 public: // methods for static tensor allocation
62   /**
63    * @brief Let the tensor builder know first use(start of lifetime) of a tensor
64    *        Must be called before calling @c prepare
65    *        Must be run up to once for each tensor before calling @c notifyLastUse
66    *        NOTE: Useful only for static models
67    */
68   virtual void notifyFirstUse(const ir::OperandIndex &) = 0;
69   /**
70    * @brief Let the tensor builder know last use(end of lifetime) of a tensor
71    *        Must be run up to once for each tensor after calling @c notifyFirstUse
72    *        NOTE: Useful only for static models
73    */
74   virtual void notifyLastUse(const ir::OperandIndex &) = 0;
75   /**
76    * @brief Prepare the tensors
77    *        Before calling this, all the tensors must be registered
78    */
79   virtual void prepare(void) = 0;
80   /**
81    * @brief Allocate the tensors
82    *        Before calling this, @c prepare must be called
83    */
84   virtual void allocate() = 0;
85   /**
86    * @brief Some actions after functions' @c IFunction::prepare method.
87    *        This is called right after each function's @c IFunction::prepare function has been
88    *        called.
89    */
90   virtual void postFunctionPrepare() = 0;
91
92 public: // methods for dynamic tensor allocation
93   /**
94    * @brief Get dynamicTensorManager. If a backend does not support dynamic tensor, exception
95    *        will be thrown.
96    *
97    * @return pointer of IDynamicTensorManager object
98    *
99    * @note   Since it is a pointer, its life time is from the cration of TensorBuilder
100    *         to the end of execution
101    */
102   virtual IDynamicTensorManager *dynamicTensorManager(void) { return nullptr; }
103 };
104
105 } // namespace backend
106 } // namespace onert
107
108 #endif // __ONERT_BACKEND_ITENSOR_BUILDER_H__