2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @brief This file defines execution
21 #ifndef __ONERT_EXEC_EXECUTION_H__
22 #define __ONERT_EXEC_EXECUTION_H__
24 #include "ir/Layout.h"
25 #include "exec/IExecutors.h"
26 #include "IODescription.h"
30 #include <semaphore.h>
38 * @brief Class to define execution instance to collect input/output information for inference
39 * and prepare executor run (TODO)
46 * @brief Construct a new Execution object
47 * @param[in] executor Model executor
49 Execution(const std::shared_ptr<IExecutors> &executors);
53 * @brief Returns primary graph object
54 * @return Graph object
56 const ir::Graph &primary_subgraph() const { return entryExecutor()->graph(); }
59 * @brief Change input shape
60 * @param[in] index Input index
61 * @param[in] new_shape shape to change
63 void changeInputShape(const ir::IOIndex &index, const ir::Shape &new_shape);
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
72 void setInput(const ir::IOIndex &index, const void *buffer, size_t length,
73 ir::Layout layout = ir::Layout::NHWC);
76 * @brief Set input data's information, especially to specify unknown dimensions on model
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
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);
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
94 void setOutput(const ir::IOIndex &index, void *buffer, size_t length,
95 ir::Layout layout = ir::Layout::NHWC);
97 * @brief Set output data's information, especially to specify unknown dimensions on model
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
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);
109 * @brief Set input data's data format
110 * @param[in] index Input index
111 * @param[in] layout Input data's data format
113 void setInputLayout(const ir::IOIndex &index, ir::Layout layout);
115 * @brief Set output data's data format
116 * @param[in] index Output index
117 * @param[in] layout Output data's data format
119 void setOutputLayout(const ir::IOIndex &index, ir::Layout layout);
122 * @note It should be called after setting input and output buffer
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
131 void startExecute(void);
134 * @brief Return when execution is finished
135 * @note It waits until execution is finished
137 void waitFinish(void);
140 * @brief Check execution is finished
141 * @return @c true if execution is finished, otherwise @c false
143 bool isFinished(void) const;
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.
152 void train(uint32_t training_step);
156 * @note It should be called after training
157 * @param[in] ind Output index
158 * @return @c float Loss value
160 float getLoss(const ir::IOIndex &ind);
161 #endif // ONERT_TRAIN
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;
169 const IExecutor *entryExecutor() const { return _executors->entryExecutor(); };
170 IExecutor *entryExecutor() { return _executors->entryExecutor(); };
173 const std::shared_ptr<IExecutors> _executors;
174 IODescription _io_desc;
175 std::unique_ptr<std::thread> _exec_thread;
176 bool finished{false};
182 #endif // __ONERT_EXEC_EXECUTION_H__