Imported Upstream version 1.12.0
[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/GeneralConfigSource.h>
24 #include <util/TracingCtx.h>
25
26 #include <string>
27 #include <memory>
28
29 namespace onert
30 {
31 namespace frontend
32 {
33 namespace custom
34 {
35 class KernelRegistry;
36 }
37 } // namespace frontend
38 namespace exec
39 {
40 class Execution;
41 } // namespace exec
42 namespace ir
43 {
44 class Graph;
45 class Subgraphs;
46 } // namespace ir
47 namespace compiler
48 {
49 class Compiler;
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   nnfw_session();
102   ~nnfw_session();
103
104   NNFW_STATUS load_model_from_nnpackage(const char *package_file_path);
105   NNFW_STATUS prepare();
106   NNFW_STATUS run();
107
108   NNFW_STATUS run_async();
109   NNFW_STATUS await();
110
111   NNFW_STATUS set_input(uint32_t index, NNFW_TYPE type, const void *buffer, size_t length);
112   NNFW_STATUS set_output(uint32_t index, NNFW_TYPE type, void *buffer, size_t length);
113
114   NNFW_STATUS input_size(uint32_t *number);
115   NNFW_STATUS output_size(uint32_t *number);
116
117   NNFW_STATUS set_input_layout(uint32_t index, NNFW_LAYOUT layout);
118   NNFW_STATUS set_output_layout(uint32_t index, NNFW_LAYOUT layout);
119
120   NNFW_STATUS apply_tensorinfo(uint32_t index, nnfw_tensorinfo ti); // Will be deprecated
121   NNFW_STATUS set_input_tensorinfo(uint32_t index, const nnfw_tensorinfo *ti);
122
123   NNFW_STATUS input_tensorinfo(uint32_t index, nnfw_tensorinfo *ti);
124   NNFW_STATUS output_tensorinfo(uint32_t index, nnfw_tensorinfo *ti);
125
126   NNFW_STATUS set_available_backends(const char *backends);
127   NNFW_STATUS set_op_backend(const char *op, const char *backend);
128
129   //
130   // Internal-only API
131   //
132
133   NNFW_STATUS set_config(const char *key, const char *value);
134   NNFW_STATUS get_config(const char *key, char *value, size_t value_size);
135   NNFW_STATUS load_circle_from_buffer(uint8_t *buffer, size_t size);
136   NNFW_STATUS load_model_from_modelfile(const char *file_path);
137
138   //
139   // Experimental API
140   //
141
142   NNFW_STATUS register_custom_operation(const std::string &id, nnfw_custom_eval eval_func);
143   NNFW_STATUS input_tensorindex(const char *tensorname, uint32_t *index);
144   NNFW_STATUS output_tensorindex(const char *tensorname, uint32_t *index);
145
146 private:
147   onert::ir::Graph *primary_subgraph();
148   bool isStateInitialized();
149   bool isStateModelLoaded();
150   bool isStatePrepared();
151   bool isStateRunning();
152   bool isStateFinishedRun();
153   bool isStatePreparedOrFinishedRun();
154
155 private:
156   State _state{State::INITIALIZED};
157   std::shared_ptr<onert::ir::Subgraphs> _subgraphs;
158   std::unique_ptr<onert::compiler::Compiler> _compiler;
159   std::unique_ptr<onert::exec::Execution> _execution;
160   std::shared_ptr<onert::frontend::custom::KernelRegistry> _kernel_registry;
161
162   std::unique_ptr<onert::util::TracingCtx> _tracing_ctx;
163 };
164
165 #endif // __API_NNFW_API_INTERNAL_H__