fa3f0bb1ece6378c077734c4d781f1fc3b76898b
[platform/core/ml/nnfw.git] / runtime / libs / rua / dyn / src / DynamicBinder.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "rua/DynamicBinder.h"
19
20 #include "NeuralNetworksLoadHelpers.h"
21
22 using namespace rua;
23
24 //
25 // Memory
26 //
27 namespace
28 {
29
30 typedef int (*ANeuralNetworksMemory_createFromFd_fn)(size_t size, int protect, int fd,
31                                                      size_t offset, ANeuralNetworksMemory **memory);
32
33 typedef void (*ANeuralNetworksMemory_free_fn)(ANeuralNetworksMemory *memory);
34
35 struct MemoryServiceImpl final : public MemoryService
36 {
37   int createFromFd(size_t size, int protect, int fd, size_t offset,
38                    ANeuralNetworksMemory **memory) const override
39   {
40     LOAD_FUNCTION(ANeuralNetworksMemory_createFromFd);
41     EXECUTE_FUNCTION_RETURN(size, protect, fd, offset, memory);
42   }
43
44   void free(ANeuralNetworksMemory *memory) const override
45   {
46     LOAD_FUNCTION(ANeuralNetworksMemory_free);
47     EXECUTE_FUNCTION(memory);
48   }
49 };
50
51 } // namespace
52
53 //
54 // Event
55 //
56 namespace
57 {
58
59 typedef int (*ANeuralNetworksEvent_wait_fn)(ANeuralNetworksEvent *event);
60
61 typedef void (*ANeuralNetworksEvent_free_fn)(ANeuralNetworksEvent *event);
62
63 struct EventServiceImpl final : public EventService
64 {
65
66   int wait(ANeuralNetworksEvent *event) const override
67   {
68     LOAD_FUNCTION(ANeuralNetworksEvent_wait);
69     EXECUTE_FUNCTION_RETURN(event);
70   }
71
72   void free(ANeuralNetworksEvent *event) const override
73   {
74     LOAD_FUNCTION(ANeuralNetworksEvent_free);
75     EXECUTE_FUNCTION(event);
76   }
77 };
78
79 } // namespace
80
81 //
82 // Model
83 //
84 namespace
85 {
86
87 typedef int (*ANeuralNetworksModel_create_fn)(ANeuralNetworksModel **model);
88
89 typedef int (*ANeuralNetworksModel_finish_fn)(ANeuralNetworksModel *model);
90
91 typedef void (*ANeuralNetworksModel_free_fn)(ANeuralNetworksModel *model);
92
93 typedef int (*ANeuralNetworksModel_addOperand_fn)(ANeuralNetworksModel *model,
94                                                   const ANeuralNetworksOperandType *type);
95
96 typedef int (*ANeuralNetworksModel_setOperandValue_fn)(ANeuralNetworksModel *model, int32_t index,
97                                                        const void *buffer, size_t length);
98
99 typedef int (*ANeuralNetworksModel_setOperandValueFromMemory_fn)(
100     ANeuralNetworksModel *model, int32_t index, const ANeuralNetworksMemory *memory, size_t offset,
101     size_t length);
102
103 typedef int (*ANeuralNetworksModel_addOperation_fn)(ANeuralNetworksModel *model,
104                                                     ANeuralNetworksOperationType type,
105                                                     uint32_t inputCount, const uint32_t *inputs,
106                                                     uint32_t outputCount, const uint32_t *outputs);
107
108 typedef int (*ANeuralNetworksModel_identifyInputsAndOutputs_fn)(ANeuralNetworksModel *model,
109                                                                 uint32_t inputCount,
110                                                                 const uint32_t *inputs,
111                                                                 uint32_t outputCount,
112                                                                 const uint32_t *outputs);
113
114 typedef int (*ANeuralNetworksModel_relaxComputationFloat32toFloat16_fn)(ANeuralNetworksModel *model,
115                                                                         bool allow);
116
117 struct ModelServiceImpl final : public ModelService
118 {
119   int create(ANeuralNetworksModel **model) const override
120   {
121     LOAD_FUNCTION(ANeuralNetworksModel_create);
122     EXECUTE_FUNCTION_RETURN(model);
123   }
124
125   int addOperand(ANeuralNetworksModel *model, const ANeuralNetworksOperandType *type) const override
126   {
127     LOAD_FUNCTION(ANeuralNetworksModel_addOperand);
128     EXECUTE_FUNCTION_RETURN(model, type);
129   }
130   int setOperandValue(ANeuralNetworksModel *model, int32_t index, const void *buffer,
131                       size_t length) const override
132   {
133     LOAD_FUNCTION(ANeuralNetworksModel_setOperandValue);
134     EXECUTE_FUNCTION_RETURN(model, index, buffer, length);
135   }
136
137   int setOperandValueFromMemory(ANeuralNetworksModel *model, int32_t index,
138                                 const ANeuralNetworksMemory *memory, size_t offset,
139                                 size_t length) const override
140   {
141     LOAD_FUNCTION(ANeuralNetworksModel_setOperandValueFromMemory);
142     EXECUTE_FUNCTION_RETURN(model, index, memory, offset, length);
143   }
144
145   int addOperation(ANeuralNetworksModel *model, ANeuralNetworksOperationType type,
146                    uint32_t inputCount, const uint32_t *inputs, uint32_t outputCount,
147                    const uint32_t *outputs) const override
148   {
149     LOAD_FUNCTION(ANeuralNetworksModel_addOperation);
150     EXECUTE_FUNCTION_RETURN(model, type, inputCount, inputs, outputCount, outputs);
151   }
152
153   int identifyInputsAndOutputs(ANeuralNetworksModel *model, uint32_t inputCount,
154                                const uint32_t *inputs, uint32_t outputCount,
155                                const uint32_t *outputs) const override
156   {
157     LOAD_FUNCTION(ANeuralNetworksModel_identifyInputsAndOutputs);
158     EXECUTE_FUNCTION_RETURN(model, inputCount, inputs, outputCount, outputs);
159   }
160
161   int relaxComputationFloat32toFloat16(ANeuralNetworksModel *model, bool allow) const override
162   {
163     LOAD_FUNCTION(ANeuralNetworksModel_relaxComputationFloat32toFloat16);
164     EXECUTE_FUNCTION_RETURN(model, allow);
165   }
166
167   int finish(ANeuralNetworksModel *model) const override
168   {
169     LOAD_FUNCTION(ANeuralNetworksModel_finish);
170     EXECUTE_FUNCTION_RETURN(model);
171   }
172
173   void free(ANeuralNetworksModel *model) const override
174   {
175     LOAD_FUNCTION(ANeuralNetworksModel_free);
176     EXECUTE_FUNCTION(model);
177   }
178 };
179
180 } // namespace
181
182 //
183 // Compilation
184 //
185 namespace
186 {
187
188 typedef int (*ANeuralNetworksCompilation_create_fn)(ANeuralNetworksModel *model,
189                                                     ANeuralNetworksCompilation **compilation);
190
191 typedef void (*ANeuralNetworksCompilation_free_fn)(ANeuralNetworksCompilation *compilation);
192
193 typedef int (*ANeuralNetworksCompilation_setPreference_fn)(ANeuralNetworksCompilation *compilation,
194                                                            int32_t preference);
195
196 typedef int (*ANeuralNetworksCompilation_finish_fn)(ANeuralNetworksCompilation *compilation);
197
198 struct CompilationServiceImpl : public CompilationService
199 {
200
201   int create(ANeuralNetworksModel *model, ANeuralNetworksCompilation **compilation) const override
202   {
203     LOAD_FUNCTION(ANeuralNetworksCompilation_create);
204     EXECUTE_FUNCTION_RETURN(model, compilation);
205   }
206
207   int setPreference(ANeuralNetworksCompilation *compilation, int32_t preference) const override
208   {
209     LOAD_FUNCTION(ANeuralNetworksCompilation_setPreference);
210     EXECUTE_FUNCTION_RETURN(compilation, preference);
211   }
212
213   int finish(ANeuralNetworksCompilation *compilation) const override
214   {
215     LOAD_FUNCTION(ANeuralNetworksCompilation_finish);
216     EXECUTE_FUNCTION_RETURN(compilation);
217   }
218
219   void free(ANeuralNetworksCompilation *compilation) const override
220   {
221     LOAD_FUNCTION(ANeuralNetworksCompilation_free);
222     EXECUTE_FUNCTION(compilation);
223   }
224 };
225
226 } // namespace
227
228 //
229 // Exceution
230 //
231 namespace
232 {
233
234 typedef int (*ANeuralNetworksExecution_create_fn)(ANeuralNetworksCompilation *compilation,
235                                                   ANeuralNetworksExecution **execution);
236
237 typedef void (*ANeuralNetworksExecution_free_fn)(ANeuralNetworksExecution *execution);
238
239 typedef int (*ANeuralNetworksExecution_setInput_fn)(ANeuralNetworksExecution *execution,
240                                                     int32_t index,
241                                                     const ANeuralNetworksOperandType *type,
242                                                     const void *buffer, size_t length);
243
244 typedef int (*ANeuralNetworksExecution_setInputFromMemory_fn)(
245     ANeuralNetworksExecution *execution, int32_t index, const ANeuralNetworksOperandType *type,
246     const ANeuralNetworksMemory *memory, size_t offset, size_t length);
247
248 typedef int (*ANeuralNetworksExecution_setOutput_fn)(ANeuralNetworksExecution *execution,
249                                                      int32_t index,
250                                                      const ANeuralNetworksOperandType *type,
251                                                      void *buffer, size_t length);
252
253 typedef int (*ANeuralNetworksExecution_setOutputFromMemory_fn)(
254     ANeuralNetworksExecution *execution, int32_t index, const ANeuralNetworksOperandType *type,
255     const ANeuralNetworksMemory *memory, size_t offset, size_t length);
256
257 typedef int (*ANeuralNetworksExecution_startCompute_fn)(ANeuralNetworksExecution *execution,
258                                                         ANeuralNetworksEvent **event);
259
260 struct ExecutionServiceImpl : public ExecutionService
261 {
262
263   int create(ANeuralNetworksCompilation *compilation,
264              ANeuralNetworksExecution **execution) const override
265   {
266     LOAD_FUNCTION(ANeuralNetworksExecution_create);
267     EXECUTE_FUNCTION_RETURN(compilation, execution);
268   }
269
270   int setInput(ANeuralNetworksExecution *execution, int32_t index,
271                const ANeuralNetworksOperandType *type, const void *buffer,
272                size_t length) const override
273   {
274     LOAD_FUNCTION(ANeuralNetworksExecution_setInput);
275     EXECUTE_FUNCTION_RETURN(execution, index, type, buffer, length);
276   }
277
278   int setInputFromMemory(ANeuralNetworksExecution *execution, int32_t index,
279                          const ANeuralNetworksOperandType *type,
280                          const ANeuralNetworksMemory *memory, size_t offset,
281                          size_t length) const override
282   {
283     LOAD_FUNCTION(ANeuralNetworksExecution_setInputFromMemory);
284     EXECUTE_FUNCTION_RETURN(execution, index, type, memory, offset, length);
285   }
286
287   int setOutput(ANeuralNetworksExecution *execution, int32_t index,
288                 const ANeuralNetworksOperandType *type, void *buffer, size_t length) const override
289   {
290     LOAD_FUNCTION(ANeuralNetworksExecution_setOutput);
291     EXECUTE_FUNCTION_RETURN(execution, index, type, buffer, length);
292   }
293
294   int setOutputFromMemory(ANeuralNetworksExecution *execution, int32_t index,
295                           const ANeuralNetworksOperandType *type,
296                           const ANeuralNetworksMemory *memory, size_t offset,
297                           size_t length) const override
298   {
299     LOAD_FUNCTION(ANeuralNetworksExecution_setOutputFromMemory);
300     EXECUTE_FUNCTION_RETURN(execution, index, type, memory, offset, length);
301   }
302
303   int startCompute(ANeuralNetworksExecution *execution, ANeuralNetworksEvent **event) const override
304   {
305     LOAD_FUNCTION(ANeuralNetworksExecution_startCompute);
306     EXECUTE_FUNCTION_RETURN(execution, event);
307   }
308
309   void free(ANeuralNetworksExecution *execution) const override
310   {
311     LOAD_FUNCTION(ANeuralNetworksExecution_free);
312     EXECUTE_FUNCTION(execution);
313   }
314 };
315
316 } // namespace
317
318 //
319 // Runtime
320 //
321 namespace
322 {
323
324 class RuntimeImpl final : public RuntimeService
325 {
326 public:
327   const MemoryService *memory(void) const override { return &_memory; }
328   const EventService *event(void) const override { return &_event; }
329
330   const ModelService *model(void) const override { return &_model; }
331   const CompilationService *compilation(void) const override { return &_compilation; }
332   const ExecutionService *execution(void) const override { return &_execution; }
333
334 private:
335   MemoryServiceImpl _memory;
336   EventServiceImpl _event;
337
338   ModelServiceImpl _model;
339   CompilationServiceImpl _compilation;
340   ExecutionServiceImpl _execution;
341 };
342
343 } // namespace
344
345 namespace rua
346 {
347
348 const RuntimeService *DynamicBinder::get(void)
349 {
350   static RuntimeImpl runtime;
351   return &runtime;
352 }
353
354 } // namespace rua