2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "rua/DynamicBinder.h"
20 #include "NeuralNetworksLoadHelpers.h"
30 typedef int (*ANeuralNetworksMemory_createFromFd_fn)(size_t size, int protect, int fd,
31 size_t offset, ANeuralNetworksMemory **memory);
33 typedef void (*ANeuralNetworksMemory_free_fn)(ANeuralNetworksMemory *memory);
35 struct MemoryServiceImpl final : public MemoryService
37 int createFromFd(size_t size, int protect, int fd, size_t offset,
38 ANeuralNetworksMemory **memory) const override
40 LOAD_FUNCTION(ANeuralNetworksMemory_createFromFd);
41 EXECUTE_FUNCTION_RETURN(size, protect, fd, offset, memory);
44 void free(ANeuralNetworksMemory *memory) const override
46 LOAD_FUNCTION(ANeuralNetworksMemory_free);
47 EXECUTE_FUNCTION(memory);
59 typedef int (*ANeuralNetworksEvent_wait_fn)(ANeuralNetworksEvent *event);
61 typedef void (*ANeuralNetworksEvent_free_fn)(ANeuralNetworksEvent *event);
63 struct EventServiceImpl final : public EventService
66 int wait(ANeuralNetworksEvent *event) const override
68 LOAD_FUNCTION(ANeuralNetworksEvent_wait);
69 EXECUTE_FUNCTION_RETURN(event);
72 void free(ANeuralNetworksEvent *event) const override
74 LOAD_FUNCTION(ANeuralNetworksEvent_free);
75 EXECUTE_FUNCTION(event);
87 typedef int (*ANeuralNetworksModel_create_fn)(ANeuralNetworksModel **model);
89 typedef int (*ANeuralNetworksModel_finish_fn)(ANeuralNetworksModel *model);
91 typedef void (*ANeuralNetworksModel_free_fn)(ANeuralNetworksModel *model);
93 typedef int (*ANeuralNetworksModel_addOperand_fn)(ANeuralNetworksModel *model,
94 const ANeuralNetworksOperandType *type);
96 typedef int (*ANeuralNetworksModel_setOperandValue_fn)(ANeuralNetworksModel *model, int32_t index,
97 const void *buffer, size_t length);
99 typedef int (*ANeuralNetworksModel_setOperandValueFromMemory_fn)(
100 ANeuralNetworksModel *model, int32_t index, const ANeuralNetworksMemory *memory, size_t offset,
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);
108 typedef int (*ANeuralNetworksModel_identifyInputsAndOutputs_fn)(ANeuralNetworksModel *model,
110 const uint32_t *inputs,
111 uint32_t outputCount,
112 const uint32_t *outputs);
114 typedef int (*ANeuralNetworksModel_relaxComputationFloat32toFloat16_fn)(ANeuralNetworksModel *model,
117 struct ModelServiceImpl final : public ModelService
119 int create(ANeuralNetworksModel **model) const override
121 LOAD_FUNCTION(ANeuralNetworksModel_create);
122 EXECUTE_FUNCTION_RETURN(model);
125 int addOperand(ANeuralNetworksModel *model, const ANeuralNetworksOperandType *type) const override
127 LOAD_FUNCTION(ANeuralNetworksModel_addOperand);
128 EXECUTE_FUNCTION_RETURN(model, type);
130 int setOperandValue(ANeuralNetworksModel *model, int32_t index, const void *buffer,
131 size_t length) const override
133 LOAD_FUNCTION(ANeuralNetworksModel_setOperandValue);
134 EXECUTE_FUNCTION_RETURN(model, index, buffer, length);
137 int setOperandValueFromMemory(ANeuralNetworksModel *model, int32_t index,
138 const ANeuralNetworksMemory *memory, size_t offset,
139 size_t length) const override
141 LOAD_FUNCTION(ANeuralNetworksModel_setOperandValueFromMemory);
142 EXECUTE_FUNCTION_RETURN(model, index, memory, offset, length);
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
149 LOAD_FUNCTION(ANeuralNetworksModel_addOperation);
150 EXECUTE_FUNCTION_RETURN(model, type, inputCount, inputs, outputCount, outputs);
153 int identifyInputsAndOutputs(ANeuralNetworksModel *model, uint32_t inputCount,
154 const uint32_t *inputs, uint32_t outputCount,
155 const uint32_t *outputs) const override
157 LOAD_FUNCTION(ANeuralNetworksModel_identifyInputsAndOutputs);
158 EXECUTE_FUNCTION_RETURN(model, inputCount, inputs, outputCount, outputs);
161 int relaxComputationFloat32toFloat16(ANeuralNetworksModel *model, bool allow) const override
163 LOAD_FUNCTION(ANeuralNetworksModel_relaxComputationFloat32toFloat16);
164 EXECUTE_FUNCTION_RETURN(model, allow);
167 int finish(ANeuralNetworksModel *model) const override
169 LOAD_FUNCTION(ANeuralNetworksModel_finish);
170 EXECUTE_FUNCTION_RETURN(model);
173 void free(ANeuralNetworksModel *model) const override
175 LOAD_FUNCTION(ANeuralNetworksModel_free);
176 EXECUTE_FUNCTION(model);
188 typedef int (*ANeuralNetworksCompilation_create_fn)(ANeuralNetworksModel *model,
189 ANeuralNetworksCompilation **compilation);
191 typedef void (*ANeuralNetworksCompilation_free_fn)(ANeuralNetworksCompilation *compilation);
193 typedef int (*ANeuralNetworksCompilation_setPreference_fn)(ANeuralNetworksCompilation *compilation,
196 typedef int (*ANeuralNetworksCompilation_finish_fn)(ANeuralNetworksCompilation *compilation);
198 struct CompilationServiceImpl : public CompilationService
201 int create(ANeuralNetworksModel *model, ANeuralNetworksCompilation **compilation) const override
203 LOAD_FUNCTION(ANeuralNetworksCompilation_create);
204 EXECUTE_FUNCTION_RETURN(model, compilation);
207 int setPreference(ANeuralNetworksCompilation *compilation, int32_t preference) const override
209 LOAD_FUNCTION(ANeuralNetworksCompilation_setPreference);
210 EXECUTE_FUNCTION_RETURN(compilation, preference);
213 int finish(ANeuralNetworksCompilation *compilation) const override
215 LOAD_FUNCTION(ANeuralNetworksCompilation_finish);
216 EXECUTE_FUNCTION_RETURN(compilation);
219 void free(ANeuralNetworksCompilation *compilation) const override
221 LOAD_FUNCTION(ANeuralNetworksCompilation_free);
222 EXECUTE_FUNCTION(compilation);
234 typedef int (*ANeuralNetworksExecution_create_fn)(ANeuralNetworksCompilation *compilation,
235 ANeuralNetworksExecution **execution);
237 typedef void (*ANeuralNetworksExecution_free_fn)(ANeuralNetworksExecution *execution);
239 typedef int (*ANeuralNetworksExecution_setInput_fn)(ANeuralNetworksExecution *execution,
241 const ANeuralNetworksOperandType *type,
242 const void *buffer, size_t length);
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);
248 typedef int (*ANeuralNetworksExecution_setOutput_fn)(ANeuralNetworksExecution *execution,
250 const ANeuralNetworksOperandType *type,
251 void *buffer, size_t length);
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);
257 typedef int (*ANeuralNetworksExecution_startCompute_fn)(ANeuralNetworksExecution *execution,
258 ANeuralNetworksEvent **event);
260 struct ExecutionServiceImpl : public ExecutionService
263 int create(ANeuralNetworksCompilation *compilation,
264 ANeuralNetworksExecution **execution) const override
266 LOAD_FUNCTION(ANeuralNetworksExecution_create);
267 EXECUTE_FUNCTION_RETURN(compilation, execution);
270 int setInput(ANeuralNetworksExecution *execution, int32_t index,
271 const ANeuralNetworksOperandType *type, const void *buffer,
272 size_t length) const override
274 LOAD_FUNCTION(ANeuralNetworksExecution_setInput);
275 EXECUTE_FUNCTION_RETURN(execution, index, type, buffer, length);
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
283 LOAD_FUNCTION(ANeuralNetworksExecution_setInputFromMemory);
284 EXECUTE_FUNCTION_RETURN(execution, index, type, memory, offset, length);
287 int setOutput(ANeuralNetworksExecution *execution, int32_t index,
288 const ANeuralNetworksOperandType *type, void *buffer, size_t length) const override
290 LOAD_FUNCTION(ANeuralNetworksExecution_setOutput);
291 EXECUTE_FUNCTION_RETURN(execution, index, type, buffer, length);
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
299 LOAD_FUNCTION(ANeuralNetworksExecution_setOutputFromMemory);
300 EXECUTE_FUNCTION_RETURN(execution, index, type, memory, offset, length);
303 int startCompute(ANeuralNetworksExecution *execution, ANeuralNetworksEvent **event) const override
305 LOAD_FUNCTION(ANeuralNetworksExecution_startCompute);
306 EXECUTE_FUNCTION_RETURN(execution, event);
309 void free(ANeuralNetworksExecution *execution) const override
311 LOAD_FUNCTION(ANeuralNetworksExecution_free);
312 EXECUTE_FUNCTION(execution);
324 class RuntimeImpl final : public RuntimeService
327 const MemoryService *memory(void) const override { return &_memory; }
328 const EventService *event(void) const override { return &_event; }
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; }
335 MemoryServiceImpl _memory;
336 EventServiceImpl _event;
338 ModelServiceImpl _model;
339 CompilationServiceImpl _compilation;
340 ExecutionServiceImpl _execution;
348 const RuntimeService *DynamicBinder::get(void)
350 static RuntimeImpl runtime;