Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / core / include / exec / Execution.h
1 /*
2  * Copyright (c) 2019 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 /**
18  * @file  Execution.h
19  * @brief This file defines execution
20  */
21 #ifndef __ONERT_EXEC_EXECUTION_H__
22 #define __ONERT_EXEC_EXECUTION_H__
23
24 #include "ir/Layout.h"
25 #include "exec/IExecutors.h"
26 #include "IODescription.h"
27
28 #include <thread>
29 #include <deque>
30 #include <semaphore.h>
31
32 namespace onert
33 {
34 namespace exec
35 {
36
37 /**
38  * @brief Class to define execution instance to collect input/output information for inference
39  *        and prepare executor run (TODO)
40  */
41 class Execution
42 {
43
44 public:
45   /**
46    * @brief     Construct a new Execution object
47    * @param[in] executor  Model executor
48    */
49   Execution(const std::shared_ptr<IExecutors> &executors);
50
51 public:
52   /**
53    * @brief   Returns primary graph object
54    * @return  Graph object
55    */
56   const ir::Graph &primary_subgraph() const { return entryExecutor()->graph(); }
57
58   /**
59    * @brief     Change input shape
60    * @param[in] index   Input index
61    * @param[in] new_shape shape to change
62    */
63   void changeInputShape(const ir::IOIndex &index, const ir::Shape &new_shape);
64
65   /**
66    * @brief     Set input data's information
67    * @param[in] index   Input index
68    * @param[in] buffer  Input data's buffer pointer
69    * @param[in] length  Input data's length
70    * @param[in] layout  Input data's data format
71    */
72   void setInput(const ir::IOIndex &index, const void *buffer, size_t length,
73                 ir::Layout layout = ir::Layout::NHWC);
74
75   /**
76    * @brief     Set input data's information, especially to specify unknown dimensions on model
77    * build time.
78    * @param[in] index   Input index
79    * @param[in] type    Input data's type info
80    * @param[in] shape   Input data's shape
81    * @param[in] buffer  Input data's buffer pointer
82    * @param[in] length  Input data's length
83    * @param[in] layout  Input data's data format
84    */
85   void setInput(const ir::IOIndex &index, const ir::TypeInfo &type, const ir::Shape &shape,
86                 const void *buffer, size_t length, ir::Layout layout = ir::Layout::NHWC);
87   /**
88    * @brief     Set output data's information
89    * @param[in] index   Output index
90    * @param[in] buffer  Output data's buffer pointer
91    * @param[in] length  Output data's length
92    * @param[in] layout  Output data's data format
93    */
94   void setOutput(const ir::IOIndex &index, void *buffer, size_t length,
95                  ir::Layout layout = ir::Layout::NHWC);
96   /**
97    * @brief     Set output data's information, especially to specify unknown dimensions on model
98    * build time.
99    * @param[in] index   Output index
100    * @param[in] type    Output data's type info
101    * @param[in] shape   Output data's shape
102    * @param[in] buffer  Output data's buffer pointer
103    * @param[in] length  Output data's length
104    * @param[in] layout  Output data's data format
105    */
106   void setOutput(const ir::IOIndex &index, const ir::TypeInfo &type, const ir::Shape &shape,
107                  void *buffer, size_t length, ir::Layout layout = ir::Layout::NHWC);
108   /**
109    * @brief     Set input data's data format
110    * @param[in] index   Input index
111    * @param[in] layout  Input data's data format
112    */
113   void setInputLayout(const ir::IOIndex &index, ir::Layout layout);
114   /**
115    * @brief     Set output data's data format
116    * @param[in] index   Output index
117    * @param[in] layout  Output data's data format
118    */
119   void setOutputLayout(const ir::IOIndex &index, ir::Layout layout);
120   /**
121    * @brief  Execution
122    * @note   It should be called after setting input and output buffer
123    */
124   void execute();
125
126   /**
127    * @brief Start asynchronous execution
128    * @note  It returns after execution thread is started
129    *        It should be called after setting input and output buffer
130    */
131   void startExecute(void);
132
133   /**
134    * @brief Return when execution is finished
135    * @note  It waits until execution is finished
136    */
137   void waitFinish(void);
138
139   /**
140    * @brief   Check execution is finished
141    * @return  @c true if execution is finished, otherwise @c false
142    */
143   bool isFinished(void) const;
144
145 #ifdef ONERT_TRAIN
146   /**
147    * @brief  Train
148    * @note   It should be called after setting input and output buffer
149    * @param training_step The number of iterations of the training process.
150    *                      In other words, the number of gradient update.
151    */
152   void train(uint32_t training_step);
153
154   /**
155    * @brief     Get loss
156    * @note      It should be called after training
157    * @param[in] ind   Output index
158    * @return @c float Loss value
159    */
160   float getLoss(const ir::IOIndex &ind);
161 #endif // ONERT_TRAIN
162
163   ir::Shape getInputShape(ir::IOIndex ind) const;
164   ir::Shape getOutputShape(ir::IOIndex ind) const;
165   size_t getInputTotalSize(ir::IOIndex ind) const;
166   size_t getOutputTotalSize(ir::IOIndex ind) const;
167
168 private:
169   const IExecutor *entryExecutor() const { return _executors->entryExecutor(); };
170   IExecutor *entryExecutor() { return _executors->entryExecutor(); };
171
172 private:
173   const std::shared_ptr<IExecutors> _executors;
174   IODescription _io_desc;
175   std::unique_ptr<std::thread> _exec_thread;
176   bool finished{false};
177 };
178
179 } // namespace exec
180 } // namespace onert
181
182 #endif // __ONERT_EXEC_EXECUTION_H__