Imported Upstream version 1.8.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
25 #include <string>
26 #include <memory>
27
28 namespace onert
29 {
30 namespace frontend
31 {
32 namespace custom
33 {
34 class KernelRegistry;
35 }
36 } // namespace frontend
37 namespace exec
38 {
39 class Execution;
40 } // namespace exec
41 namespace ir
42 {
43 class Graph;
44 class Subgraphs;
45 } // namespace ir
46 namespace compiler
47 {
48 class Compiler;
49 } // namespace compiler
50 } // namespace onert
51
52 struct nnfw_session
53 {
54 private:
55   /**
56    * @brief Enum class to express the session's state
57    *
58    * State transition diagram:
59    *
60    *           +--------------+
61    *           | INITIALIZED  |
62    *           +--------------+
63    *             |
64    *             | load_model
65    *             v
66    *           +--------------+
67    *           | MODEL_LOADED |
68    *           +--------------+
69    *             |
70    *             | prepare
71    *             v
72    *           +--------------+
73    *           |   PREPARED   | --------+
74    *           +--------------+         |
75    *             |                      |
76    *             | run                  |
77    *             v                      |
78    *           +--------------+  run    |
79    *           |              | -----+  |
80    *   +-----> | FINISHED_RUN |      |  | run_async
81    *   |       |              | <----+  |
82    *   |       +--------------+         |
83    *   |         |                      |
84    *   | await   | run_async            |
85    *   |         v                      |
86    *   |       +--------------+         |
87    *   +------ |   RUNNING    | <-------+
88    *           +--------------+
89    */
90   enum class State
91   {
92     INITIALIZED,  //< Session is initialized and nothing has done to it
93     MODEL_LOADED, //< Model is loaded
94     PREPARED,     //< Prepared(compiled) for execution
95     RUNNING,      //< Execution is in progress (only for asynchronous execution)
96     FINISHED_RUN  //< Executed at least once
97   };
98
99 public:
100   nnfw_session();
101   ~nnfw_session();
102
103   NNFW_STATUS load_model_from_file(const char *package_file_path);
104   NNFW_STATUS prepare();
105   NNFW_STATUS run();
106
107   NNFW_STATUS run_async();
108   NNFW_STATUS await();
109
110   NNFW_STATUS set_input(uint32_t index, NNFW_TYPE type, const void *buffer, size_t length);
111   NNFW_STATUS set_output(uint32_t index, NNFW_TYPE type, void *buffer, size_t length);
112
113   NNFW_STATUS input_size(uint32_t *number);
114   NNFW_STATUS output_size(uint32_t *number);
115
116   NNFW_STATUS set_input_layout(uint32_t index, NNFW_LAYOUT layout);
117   NNFW_STATUS set_output_layout(uint32_t index, NNFW_LAYOUT layout);
118
119   NNFW_STATUS apply_tensorinfo(uint32_t index, nnfw_tensorinfo ti); // Will be deprecated
120   NNFW_STATUS set_input_tensorinfo(uint32_t index, const nnfw_tensorinfo *ti);
121
122   NNFW_STATUS input_tensorinfo(uint32_t index, nnfw_tensorinfo *ti);
123   NNFW_STATUS output_tensorinfo(uint32_t index, nnfw_tensorinfo *ti);
124
125   NNFW_STATUS register_custom_operation(const std::string &id, nnfw_custom_eval eval_func);
126
127   NNFW_STATUS set_available_backends(const char *backends);
128   NNFW_STATUS set_op_backend(const char *op, const char *backend);
129
130   //
131   // Internal-only API
132   //
133
134   NNFW_STATUS set_config(const char *key, const char *value);
135   NNFW_STATUS get_config(const char *key, char *value, size_t value_size);
136
137   NNFW_STATUS load_circle_from_buffer(uint8_t *buffer, size_t size);
138
139 private:
140   onert::ir::Graph *primary_subgraph();
141   bool isStateInitialized();
142   bool isStateModelLoaded();
143   bool isStatePrepared();
144   bool isStateRunning();
145   bool isStateFinishedRun();
146   bool isStatePreparedOrFinishedRun();
147
148 private:
149   State _state{State::INITIALIZED};
150   std::shared_ptr<onert::ir::Subgraphs> _subgraphs;
151   std::unique_ptr<onert::compiler::Compiler> _compiler;
152   std::shared_ptr<onert::exec::Execution> _execution;
153   std::shared_ptr<onert::frontend::custom::KernelRegistry> _kernel_registry;
154 };
155
156 #endif // __API_NNFW_API_INTERNAL_H__