8e2c2fba613bd08f35adc7935463a5c43fc40e70
[platform/core/ml/nnfw.git] / runtime / onert / api / src / nnfw_api_internal.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 #ifndef __API_NNFW_API_INTERNAL_H__
18 #define __API_NNFW_API_INTERNAL_H__
19
20 #include "nnfw.h"
21 #include "nnfw_experimental.h"
22
23 #include <util/TracingCtx.h>
24
25 #include <string>
26 #include <memory>
27 #include <thread>
28 #include <vector>
29
30 namespace onert
31 {
32 namespace api
33 {
34 class CustomKernelRegistry;
35 } // namespace api
36 namespace exec
37 {
38 class Execution;
39 } // namespace exec
40 namespace ir
41 {
42 class Graph;
43 class Model;
44 class NNPkg;
45 } // namespace ir
46 namespace compiler
47 {
48 struct CompilerArtifact;
49 class CompilerOptions;
50 } // namespace compiler
51 } // namespace onert
52
53 struct nnfw_session
54 {
55 private:
56   /**
57    * @brief Enum class to express the session's state
58    *
59    * State transition diagram:
60    *
61    *           +--------------+
62    *           | INITIALIZED  |
63    *           +--------------+
64    *             |
65    *             | load_model
66    *             v
67    *           +--------------+
68    *           | MODEL_LOADED |
69    *           +--------------+
70    *             |
71    *             | prepare
72    *             v
73    *           +--------------+
74    *           |   PREPARED   | --------+
75    *           +--------------+         |
76    *             |                      |
77    *             | run                  |
78    *             v                      |
79    *           +--------------+  run    |
80    *           |              | -----+  |
81    *   +-----> | FINISHED_RUN |      |  | run_async
82    *   |       |              | <----+  |
83    *   |       +--------------+         |
84    *   |         |                      |
85    *   | await   | run_async            |
86    *   |         v                      |
87    *   |       +--------------+         |
88    *   +------ |   RUNNING    | <-------+
89    *           +--------------+
90    */
91   enum class State
92   {
93     INITIALIZED,  //< Session is initialized and nothing has done to it
94     MODEL_LOADED, //< Model is loaded
95     PREPARED,     //< Prepared(compiled) for execution
96     RUNNING,      //< Execution is in progress (only for asynchronous execution)
97     FINISHED_RUN  //< Executed at least once
98   };
99
100 public:
101   /**
102    * @brief Factory method. It creates and initialize nnfw_session
103    *
104    * @note  Use factory instead of constructor to get status
105    */
106   static NNFW_STATUS create(nnfw_session **session);
107
108 private:
109   nnfw_session();
110
111 public:
112   ~nnfw_session();
113   NNFW_STATUS load_model_from_nnpackage(const char *package_file_path);
114   NNFW_STATUS prepare();
115   NNFW_STATUS prepare_pipeline(const char *map_file_path);
116   NNFW_STATUS run();
117
118   NNFW_STATUS run_async();
119   NNFW_STATUS await();
120
121   NNFW_STATUS set_input(uint32_t index, NNFW_TYPE type, const void *buffer, size_t length);
122   NNFW_STATUS set_output(uint32_t index, NNFW_TYPE type, void *buffer, size_t length);
123
124   NNFW_STATUS input_size(uint32_t *number);
125   NNFW_STATUS output_size(uint32_t *number);
126
127   NNFW_STATUS set_input_layout(uint32_t index, NNFW_LAYOUT layout);
128   NNFW_STATUS set_output_layout(uint32_t index, NNFW_LAYOUT layout);
129
130   NNFW_STATUS apply_tensorinfo(uint32_t index, nnfw_tensorinfo ti); // Will be deprecated
131   NNFW_STATUS set_input_tensorinfo(uint32_t index, const nnfw_tensorinfo *ti);
132
133   NNFW_STATUS input_tensorinfo(uint32_t index, nnfw_tensorinfo *ti);
134   NNFW_STATUS output_tensorinfo(uint32_t index, nnfw_tensorinfo *ti);
135
136   NNFW_STATUS set_available_backends(const char *backends);
137   NNFW_STATUS set_op_backend(const char *op, const char *backend);
138
139   //
140   // Internal-only API
141   //
142
143   NNFW_STATUS set_config(const char *key, const char *value);
144   NNFW_STATUS get_config(const char *key, char *value, size_t value_size);
145   NNFW_STATUS load_circle_from_buffer(uint8_t *buffer, size_t size);
146   NNFW_STATUS load_model_from_modelfile(const char *file_path);
147
148   //
149   // Experimental API
150   //
151   NNFW_STATUS push_pipeline_input(std::vector<void *> *inputs, std::vector<uint32_t> *lengths);
152   NNFW_STATUS pop_pipeline_output(std::vector<void *> *outputs);
153
154   NNFW_STATUS register_custom_operation(const std::string &id, nnfw_custom_eval eval_func);
155   NNFW_STATUS input_tensorindex(const char *tensorname, uint32_t *index);
156   NNFW_STATUS output_tensorindex(const char *tensorname, uint32_t *index);
157   /**
158    * @brief   Set backends with string-encoded mapping from operation index to backend type
159    *          (cpu, acl_cl)
160    */
161   NNFW_STATUS set_backends_per_operation(const char *backend_settings);
162
163 private:
164   const onert::ir::Graph *primary_subgraph();
165   uint32_t getInputSize();
166   uint32_t getOutputSize();
167
168   bool isStateInitialized();
169   bool isStateModelLoaded();
170   bool isStatePrepared();
171   bool isStateRunning();
172   bool isStateFinishedRun();
173   bool isStatePreparedOrFinishedRun();
174
175 private:
176   State _state{State::INITIALIZED};
177   std::shared_ptr<onert::ir::NNPkg> _nnpkg;
178   std::vector<std::unique_ptr<onert::compiler::CompilerOptions>> _coptions;
179   std::shared_ptr<onert::compiler::CompilerArtifact> _compiler_artifact;
180   std::unique_ptr<onert::exec::Execution> _execution;
181   std::shared_ptr<onert::api::CustomKernelRegistry> _kernel_registry;
182   std::vector<std::thread> _threads;
183 };
184
185 #endif // __API_NNFW_API_INTERNAL_H__