1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_NNAPI_NEURALNETWORKSSHIM_H_
16 #define TENSORFLOW_LITE_NNAPI_NEURALNETWORKSSHIM_H_
17
18 #include <dlfcn.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "tensorflow/lite/nnapi/NeuralNetworksTypes.h"
24
25 // This interface is now deprecated. You should use instead
26 // nnapi_implementation.
27
28 // TODO(b/123017568): Update all current usages of this file.
29
30 // helpers
31
32 #define NNAPI_LOG(format, ...) fprintf(stderr, format "\n", __VA_ARGS__);
33 #define LOAD_FUNCTION(name) \
34 static name##_fn fn = reinterpret_cast<name##_fn>(loadFunction(#name));
35 #define EXECUTE_FUNCTION(...) \
36 if (fn != nullptr) { \
37 fn(__VA_ARGS__); \
38 }
39 #define EXECUTE_FUNCTION_RETURN(...) return fn != nullptr ? fn(__VA_ARGS__) : 0;
40
loadLibrary(const char * name)41 inline void* loadLibrary(const char* name) {
42 // TODO: change RTLD_LOCAL? Assumes there can be multiple instances of nn
43 // api RT
44 void* handle = nullptr;
45 #ifdef __ANDROID__
46 handle = dlopen(name, RTLD_LAZY | RTLD_LOCAL);
47 if (handle == nullptr) {
48 NNAPI_LOG("nnapi error: unable to open library %s", name);
49 }
50 #endif
51 return handle;
52 }
53
54 // ASharedMemory_create was added in Android 8.0, so safe to use with NNAPI
55 // which was added in 8.1.
ASharedMemory_create(const char * name,size_t size)56 inline int ASharedMemory_create(const char* name, size_t size) {
57 static void* handle = loadLibrary("libandroid.so");
58 static ASharedMemory_create_fn fn =
59 handle != nullptr ? reinterpret_cast<ASharedMemory_create_fn>(
60 dlsym(handle, "ASharedMemory_create"))
61 : nullptr;
62 int fd = fn != nullptr ? fn(name, size) : -1;
63 return fd;
64 }
65
getLibraryHandle()66 inline void* getLibraryHandle() {
67 static void* handle = loadLibrary("libneuralnetworks.so");
68 return handle;
69 }
70
loadFunction(const char * name)71 inline void* loadFunction(const char* name) {
72 void* fn = nullptr;
73 if (getLibraryHandle() != nullptr) {
74 fn = dlsym(getLibraryHandle(), name);
75 }
76 if (fn == nullptr) {
77 NNAPI_LOG("nnapi error: unable to open function %s", name);
78 }
79 return fn;
80 }
81
NNAPIExists()82 inline bool NNAPIExists() {
83 static bool nnapi_is_available = getLibraryHandle();
84 return nnapi_is_available;
85 }
86
87 // NN api types based on NNAPI header file
88 // https://developer.android.com/ndk/reference/group/neural-networks
89
90 /**
91 * Creates a shared memory object from a file descriptor.
92 *
93 * The shared memory is backed by a file descriptor via mmap.
94 * See {@link ANeuralNetworksMemory} for a description on how to use
95 * this shared memory.
96 *
97 * @param size The requested size in bytes.
98 * Must not be larger than the file size.
99 * @param prot The desired memory protection for the mapping.
100 * It is either PROT_NONE or the bitwise OR of one or
101 * more of the following flags: PROT_READ, PROT_WRITE.
102 * @param fd The requested file descriptor.
103 * The file descriptor has to be mmap-able. The file
104 * descriptor will be duplicated.
105 * @param offset The offset to the beginning of the file of the area to map.
106 * The offset has to be aligned to a page size.
107 * @param memory The memory object to be created.
108 * Set to NULL if unsuccessful.
109 *
110 * @return ANEURALNETWORKS_NO_ERROR if the request completed normally.
111 */
ANeuralNetworksMemory_createFromFd(size_t size,int protect,int fd,size_t offset,ANeuralNetworksMemory ** memory)112 inline int ANeuralNetworksMemory_createFromFd(size_t size, int protect, int fd,
113 size_t offset,
114 ANeuralNetworksMemory** memory) {
115 LOAD_FUNCTION(ANeuralNetworksMemory_createFromFd);
116 EXECUTE_FUNCTION_RETURN(size, protect, fd, offset, memory);
117 }
118
119 /**
120 * Delete a memory object.
121 *
122 * Destroys the object used by the run time to keep track of the memory.
123 * This will free the underlying actual memory if no other code has open
124 * handles to this memory.
125 *
126 * @param memory The memory object to be freed.
127 */
ANeuralNetworksMemory_free(ANeuralNetworksMemory * memory)128 inline void ANeuralNetworksMemory_free(ANeuralNetworksMemory* memory) {
129 LOAD_FUNCTION(ANeuralNetworksMemory_free);
130 EXECUTE_FUNCTION(memory);
131 }
132
133 /**
134 * Create an empty {@link ANeuralNetworksModel}.
135 *
136 * <p>This only creates the object. Computation is performed once
137 * {@link ANeuralNetworksExecution_startCompute} is invoked.
138 *
139 * The model should be constructed with calls to
140 * {@link ANeuralNetworksModel_addOperation} and
141 * {@link ANeuralNetworksModel_addOperand}
142 *
143 * <p>{@link ANeuralNetworksModel_finish} should be called once the model
144 * has been fully constructed.</p>
145 *
146 * <p>{@link ANeuralNetworksModel_free} should be called once the model
147 * is no longer needed.</p>
148 *
149 * @param model The {@link ANeuralNetworksModel} to be created.
150 * Set to NULL if unsuccessful.
151 *
152 * @return ANEURALNETWORKS_NO_ERROR if successful.
153 */
ANeuralNetworksModel_create(ANeuralNetworksModel ** model)154 inline int ANeuralNetworksModel_create(ANeuralNetworksModel** model) {
155 LOAD_FUNCTION(ANeuralNetworksModel_create);
156 EXECUTE_FUNCTION_RETURN(model);
157 }
158
159 /**
160 * Destroy a model.
161 *
162 * The model need not have been finished by a call to
163 * {@link ANeuralNetworksModel_finish}.
164 *
165 * See {@link ANeuralNetworksModel} for information on multithreaded usage.
166 *
167 * @param model The model to be destroyed. Passing NULL is acceptable and
168 * results in no operation.
169 */
ANeuralNetworksModel_free(ANeuralNetworksModel * model)170 inline void ANeuralNetworksModel_free(ANeuralNetworksModel* model) {
171 LOAD_FUNCTION(ANeuralNetworksModel_free);
172 EXECUTE_FUNCTION(model);
173 }
174
175 /**
176 * Indicate that we have finished modifying a model. Required before
177 * calling {@link ANeuralNetworksCompilation_compile}.
178 *
179 * An application is responsible to make sure that no other thread uses
180 * the model at the same time.
181 *
182 * See {@link ANeuralNetworksModel} for information on multithreaded usage.
183 *
184 * @param model The model to be finished.
185 *
186 * @return ANEURALNETWORKS_NO_ERROR if successful.
187 */
ANeuralNetworksModel_finish(ANeuralNetworksModel * model)188 inline int ANeuralNetworksModel_finish(ANeuralNetworksModel* model) {
189 LOAD_FUNCTION(ANeuralNetworksModel_finish);
190 EXECUTE_FUNCTION_RETURN(model);
191 }
192
193 /**
194 * Add an operand to a model.
195 *
196 * The order in which the operands are added is important. The first one added
197 * to a model will have the index value 0, the second 1, etc. These indexes are
198 * used as operand identifiers in {@link ANeuralNetworksModel_addOperation},
199 * {@link ANeuralNetworksExecution_setInput},
200 * {@link ANeuralNetworksExecution_setInputFromMemory},
201 * {@link ANeuralNetworksExecution_setOutput},
202 * {@link ANeuralNetworksExecution_setOutputFromMemory} and
203 * {@link ANeuralNetworksExecution_setOperandValue}.
204 *
205 * To build a model that can accommodate inputs of various sizes, as you may
206 * want to do for a CNN, set the size of the dimensions that will vary at run
207 * time to 0. If you do so, provide the full dimensions when calling
208 * {@link ANeuralNetworksExecution_setInput} or {@link
209 * ANeuralNetworksExecution_setInputFromMemory}.
210 *
211 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has
212 * been called will return an error.
213 *
214 * See {@link ANeuralNetworksModel} for information on multithreaded usage.
215 *
216 * @param model The model to be modified.
217 * @param type The {@link ANeuralNetworksOperandType} that describes the shape
218 * of the operand.
219 *
220 * @return ANEURALNETWORKS_NO_ERROR if successful.
221 */
ANeuralNetworksModel_addOperand(ANeuralNetworksModel * model,const ANeuralNetworksOperandType * type)222 inline int ANeuralNetworksModel_addOperand(
223 ANeuralNetworksModel* model, const ANeuralNetworksOperandType* type) {
224 LOAD_FUNCTION(ANeuralNetworksModel_addOperand);
225 EXECUTE_FUNCTION_RETURN(model, type);
226 }
227
228 /**
229 * Sets an operand to a constant value.
230 *
231 * For scalar values, the content of buffer is copied into the model.
232 *
233 * For tensor values, a pointer to the buffer is stored within the model.
234 * The application is responsible for not changing the content of this region
235 * until all executions using this model have completed. As the data may
236 * be copied during processing, modifying the data after this call yields
237 * undefined results.
238 *
239 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has
240 * been called will return an error.
241 *
242 * See {@link ANeuralNetworksModel} for information on multithreaded usage.
243 *
244 * @param model The model to be modified.
245 * @param index The index of the model operand we're setting.
246 * @param buffer A pointer to the data to use.
247 * @param length The size in bytes of the data value.
248 *
249 * @return ANEURALNETWORKS_NO_ERROR if successful.
250 */
ANeuralNetworksModel_setOperandValue(ANeuralNetworksModel * model,int32_t index,const void * buffer,size_t length)251 inline int ANeuralNetworksModel_setOperandValue(ANeuralNetworksModel* model,
252 int32_t index,
253 const void* buffer,
254 size_t length) {
255 LOAD_FUNCTION(ANeuralNetworksModel_setOperandValue);
256 EXECUTE_FUNCTION_RETURN(model, index, buffer, length);
257 }
258
259 /**
260 * Sets an operand's per channel quantization parameters.
261 *
262 * Sets parameters required by a tensor of type
263 * {@link ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL}.
264 * This function must be called for every tensor of type
265 * {@link ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL} before
266 * calling {@link ANeuralNetworksModel_finish}.
267 *
268 * Available since API level 29.
269 *
270 * @param model The model to be modified.
271 * @param index The index of the model operand we're setting.
272 * @param channelQuant The per channel quantization parameters for the operand.
273 * No memory in this struct needs to outlive the call to
274 * this function.
275 *
276 * @return ANEURALNETWORKS_NO_ERROR if successful.
277 */
ANeuralNetworksModel_setOperandSymmPerChannelQuantParams(ANeuralNetworksModel * model,int32_t index,const ANeuralNetworksSymmPerChannelQuantParams * channelQuant)278 inline int ANeuralNetworksModel_setOperandSymmPerChannelQuantParams(
279 ANeuralNetworksModel* model, int32_t index,
280 const ANeuralNetworksSymmPerChannelQuantParams* channelQuant) {
281 LOAD_FUNCTION(ANeuralNetworksModel_setOperandSymmPerChannelQuantParams);
282 EXECUTE_FUNCTION_RETURN(model, index, channelQuant);
283 }
284
285 /**
286 * Sets an operand to a value stored in a memory object.
287 *
288 * The content of the memory is not copied. A reference to that memory is stored
289 * inside the model. The application is responsible for not changing the content
290 * of the memory region until all executions using this model have completed.
291 * As the data may be copied during processing, modifying the data after this
292 * call yields undefined results.
293 *
294 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has
295 * been called will return an error.
296 *
297 * See {@link ANeuralNetworksModel} for information on multithreaded usage.
298 *
299 * @param model The model to be modified.
300 * @param index The index of the model operand we're setting.
301 * @param buffer A pointer to the data to use.
302 * @param memory The memory containing the data.
303 * @param offset This specifies the location of the data within the memory.
304 * The offset is in bytes from the start of memory.
305 * @param length The size in bytes of the data value.
306 *
307 * @return ANEURALNETWORKS_NO_ERROR if successful.
308 */
ANeuralNetworksModel_setOperandValueFromMemory(ANeuralNetworksModel * model,int32_t index,const ANeuralNetworksMemory * memory,size_t offset,size_t length)309 inline int ANeuralNetworksModel_setOperandValueFromMemory(
310 ANeuralNetworksModel* model, int32_t index,
311 const ANeuralNetworksMemory* memory, size_t offset, size_t length) {
312 LOAD_FUNCTION(ANeuralNetworksModel_setOperandValueFromMemory);
313 EXECUTE_FUNCTION_RETURN(model, index, memory, offset, length);
314 }
315
316 /**
317 * Add an operation to a model.
318 *
319 * @param model The model to be modified.
320 * @param type The type of the operation.
321 * @param inputCount The number of entries in the inputs array.
322 * @param inputs An array of indexes identifying each operand.
323 * @param outputCount The number of entries in the outputs array.
324 * @param outputs An array of indexes identifying each operand.
325 *
326 * The operands specified by inputs and outputs must have been
327 * previously added by calls to {@link ANeuralNetworksModel_addOperand}.
328 *
329 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has
330 * been called will return an error.
331 *
332 * See {@link ANeuralNetworksModel} for information on multithreaded usage.
333 *
334 * @return ANEURALNETWORKS_NO_ERROR if successful.
335 */
ANeuralNetworksModel_addOperation(ANeuralNetworksModel * model,ANeuralNetworksOperationType type,uint32_t inputCount,const uint32_t * inputs,uint32_t outputCount,const uint32_t * outputs)336 inline int ANeuralNetworksModel_addOperation(ANeuralNetworksModel* model,
337 ANeuralNetworksOperationType type,
338 uint32_t inputCount,
339 const uint32_t* inputs,
340 uint32_t outputCount,
341 const uint32_t* outputs) {
342 LOAD_FUNCTION(ANeuralNetworksModel_addOperation);
343 EXECUTE_FUNCTION_RETURN(model, type, inputCount, inputs, outputCount,
344 outputs);
345 }
346
347 /**
348 * Specifies which operands will be the model's inputs and outputs.
349 *
350 * An operand cannot be used for both input and output. Doing so will
351 * return an error.
352 *
353 * @param model The model to be modified.
354 * @param inputCount The number of entries in the inputs array.
355 * @param inputs An array of indexes identifying the input operands.
356 * @param outputCount The number of entries in the outputs array.
357 * @param outputs An array of indexes identifying the output operands.
358 *
359 * The operands specified by inputs and outputs must have been
360 * previously added by calls to {@link ANeuralNetworksModel_addOperand}.
361 *
362 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has
363 * been called will return an error.
364 *
365 * See {@link ANeuralNetworksModel} for information on multithreaded usage.
366 *
367 */
ANeuralNetworksModel_identifyInputsAndOutputs(ANeuralNetworksModel * model,uint32_t inputCount,const uint32_t * inputs,uint32_t outputCount,const uint32_t * outputs)368 inline int ANeuralNetworksModel_identifyInputsAndOutputs(
369 ANeuralNetworksModel* model, uint32_t inputCount, const uint32_t* inputs,
370 uint32_t outputCount, const uint32_t* outputs) {
371 LOAD_FUNCTION(ANeuralNetworksModel_identifyInputsAndOutputs);
372 EXECUTE_FUNCTION_RETURN(model, inputCount, inputs, outputCount, outputs);
373 }
374
375 /**
376 * Specifies whether {@link ANEURALNETWORKS_TENSOR_FLOAT32} is allowed to be
377 * calculated with range and/or precision as low as that of the IEEE 754 16-bit
378 * floating-point format. By default, {@link ANEURALNETWORKS_TENSOR_FLOAT32}
379 * must be calculated using at least the range and precision of the IEEE 754
380 * 32-bit floating-point format.
381 *
382 * @param model The model to be modified.
383 * @param allow 'true' indicates {@link ANEURALNETWORKS_TENSOR_FLOAT32} may be
384 * calculated with range and/or precision as low as that of the
385 * IEEE 754 16-bit floating point format. 'false' indicates
386 * {@link ANEURALNETWORKS_TENSOR_FLOAT32} must be calculated using
387 * at least the range and precision of the IEEE 754 32-bit floating
388 * point format.
389 *
390 * Attempting to modify a model once {@link ANeuralNetworksModel_finish} has
391 * been called will return an error.
392 *
393 * Available since API level 28.
394 *
395 * See {@link ANeuralNetworksModel} for information on multithreaded usage.
396 */
ANeuralNetworksModel_relaxComputationFloat32toFloat16(ANeuralNetworksModel * model,bool allow)397 inline int ANeuralNetworksModel_relaxComputationFloat32toFloat16(
398 ANeuralNetworksModel* model, bool allow) {
399 LOAD_FUNCTION(ANeuralNetworksModel_relaxComputationFloat32toFloat16);
400 EXECUTE_FUNCTION_RETURN(model, allow);
401 }
402
403 /**
404 * Create a {@link ANeuralNetworksCompilation} to compile the given model.
405 * This only creates the object. Compilation is only performed once
406 * {@link ANeuralNetworksCompilation_start} is invoked.
407 *
408 * <p>The provided model must outlive the compilation.</p>
409 *
410 * The model must already have been finished by a call to
411 * {@link ANeuralNetworksModel_finish}.
412 *
413 * See {@link ANeuralNetworksCompilation} for information on multithreaded
414 * usage.
415 *
416 * @param model The {@link ANeuralNetworksModel} to be compiled.
417 * @param compilation The newly created object or NULL if unsuccessful.
418 *
419 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA
420 * if the model is invalid.
421 */
ANeuralNetworksCompilation_create(ANeuralNetworksModel * model,ANeuralNetworksCompilation ** compilation)422 inline int ANeuralNetworksCompilation_create(
423 ANeuralNetworksModel* model, ANeuralNetworksCompilation** compilation) {
424 LOAD_FUNCTION(ANeuralNetworksCompilation_create);
425 EXECUTE_FUNCTION_RETURN(model, compilation);
426 }
427
428 /**
429 * Destroy a compilation.
430 *
431 * <p>If called on a compilation for which
432 * {@link ANeuralNetworksCompilation_start} has been called, the
433 * function will return immediately but will mark the compilation to be deleted
434 * once the compilation completes. The {@link ANeuralNetworksCompilation_wait}
435 * will return ERROR_DELETED.
436 *
437 * See {@link ANeuralNetworksCompilation} for information on multithreaded
438 * usage.
439 *
440 * @param compilation The compilation to be destroyed. Passing NULL is
441 * acceptable and results in no operation.
442 */
ANeuralNetworksCompilation_free(ANeuralNetworksCompilation * compilation)443 inline void ANeuralNetworksCompilation_free(
444 ANeuralNetworksCompilation* compilation) {
445 LOAD_FUNCTION(ANeuralNetworksCompilation_free);
446 EXECUTE_FUNCTION(compilation);
447 }
448
449 /**
450 * Sets the execution preference.
451 *
452 * <p>Provides guidance to the runtime when trade-offs are possible.</p>
453 *
454 * See {@link ANeuralNetworksCompilation} for information on multithreaded
455 * usage.
456 *
457 * @param compilation The compilation to be modified.
458 * @param preference Either {@link PREFER_LOW_POWER},
459 * {@link PREFER_SINGLE_FAST_ANSWER}, or
460 * {@link PREFER_SUSTAINED_SPEED}.
461 *
462 * @return ANEURALNETWORKS_NO_ERROR if successful.
463 */
ANeuralNetworksCompilation_setPreference(ANeuralNetworksCompilation * compilation,int32_t preference)464 inline int ANeuralNetworksCompilation_setPreference(
465 ANeuralNetworksCompilation* compilation, int32_t preference) {
466 LOAD_FUNCTION(ANeuralNetworksCompilation_setPreference);
467 EXECUTE_FUNCTION_RETURN(compilation, preference);
468 }
469
470 /**
471 * Waits until the compilation completes.
472 *
473 * More than one thread can wait on a compilation. When the compilation
474 * completes, all threads will be released.
475 *
476 * See {@link ANeuralNetworksCompilation} for information on multithreaded
477 * usage.
478 *
479 * @return ANEURALNETWORKS_NO_ERROR if the compilation completed normally.
480 */
ANeuralNetworksCompilation_finish(ANeuralNetworksCompilation * compilation)481 inline int ANeuralNetworksCompilation_finish(
482 ANeuralNetworksCompilation* compilation) {
483 LOAD_FUNCTION(ANeuralNetworksCompilation_finish);
484 EXECUTE_FUNCTION_RETURN(compilation);
485 }
486 /**
487 * Create a {@link ANeuralNetworksExecution} to apply the given compilation.
488 * This only creates the object. Computation is only performed once
489 * {@link ANeuralNetworksExecution_startCompute} is invoked.
490 *
491 * <p>The provided compilation must outlive the execution.</p>
492 *
493 * See {@link ANeuralNetworksExecution} for information on multithreaded usage.
494 *
495 * @param compilation The {@link ANeuralNetworksCompilation} to be evaluated.
496 * @param execution The newly created object or NULL if unsuccessful.
497 *
498 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA
499 * if the compilation is invalid.
500 */
ANeuralNetworksExecution_create(ANeuralNetworksCompilation * compilation,ANeuralNetworksExecution ** execution)501 inline int ANeuralNetworksExecution_create(
502 ANeuralNetworksCompilation* compilation,
503 ANeuralNetworksExecution** execution) {
504 LOAD_FUNCTION(ANeuralNetworksExecution_create);
505 EXECUTE_FUNCTION_RETURN(compilation, execution);
506 }
507
508 /**
509 * Destroy an execution.
510 *
511 * <p>If called on an execution for which
512 * {@link ANeuralNetworksExecution_startCompute} has been called, the
513 * function will return immediately but will mark the execution to be deleted
514 * once the computation completes. The {link ANeuralNetworksExecution_wait}
515 * will return ANEURALNETWORKS_ERROR_DELETED.
516 *
517 * See {@link ANeuralNetworksExecution} for information on multithreaded usage.
518 *
519 * @param execution The execution to be destroyed. Passing NULL is acceptable
520 * and results in no operation.
521 */
ANeuralNetworksExecution_free(ANeuralNetworksExecution * execution)522 inline void ANeuralNetworksExecution_free(ANeuralNetworksExecution* execution) {
523 LOAD_FUNCTION(ANeuralNetworksExecution_free);
524 EXECUTE_FUNCTION(execution);
525 }
526
527 /**
528 * Associate a user buffer with an input of the model of the
529 * {@link ANeuralNetworksExecution}.
530 *
531 * <p>The provided buffer must outlive the execution.</p>
532 *
533 * See {@link ANeuralNetworksExecution} for information on multithreaded usage.
534 *
535 * @param execution The execution to be modified.
536 * @param index The index of the input argument we are setting. It is
537 * an index into the lists passed to
538 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is not
539 * the index associated with {@link
540 * ANeuralNetworksModel_addOperand}.
541 * @param type The type of the operand. This should be used to specify the
542 * dimensions that were set to 0 when the operand was added to the
543 * model. All other properties of the type must be the same as
544 * specified in the model. If the type is the same as specified
545 * when the model was built, NULL can be passed.
546 * @param buffer The buffer containing the data.
547 * @param length The length in bytes of the buffer.
548 *
549 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA if
550 * the name is not recognized or the buffer is too small for the input.
551 */
ANeuralNetworksExecution_setInput(ANeuralNetworksExecution * execution,int32_t index,const ANeuralNetworksOperandType * type,const void * buffer,size_t length)552 inline int ANeuralNetworksExecution_setInput(
553 ANeuralNetworksExecution* execution, int32_t index,
554 const ANeuralNetworksOperandType* type, const void* buffer, size_t length) {
555 LOAD_FUNCTION(ANeuralNetworksExecution_setInput);
556 EXECUTE_FUNCTION_RETURN(execution, index, type, buffer, length);
557 }
558
559 /**
560 * Associate part of a memory object with an input of the model of the
561 * {@link ANeuralNetworksExecution}.
562 *
563 * <p>The provided memory must outlive the execution.</p>
564 *
565 * See {@link ANeuralNetworksExecution} for information on multithreaded usage.
566 *
567 * @param execution The execution to be modified.
568 * @param index The index of the input argument we are setting. It is
569 * an index into the lists passed to
570 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is not
571 * the index associated with {@link
572 * ANeuralNetworksModel_addOperand}.
573 * @param type The type of the operand. This can be used to specify the
574 * dimensions that were set to 0 when the operand was added to the
575 * model. All other values must be the same as specified in the
576 * model. If the type is the same as specified when the model
577 * was built, NULL can be passed.
578 * @param memory The memory containing the data.
579 * @param offset This specifies the location of the data within the memory.
580 * The offset is in bytes from the start of memory.
581 * @param length The size in bytes of the data value.
582 *
583 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA if
584 * the name is not recognized or the buffer is too small for the input.
585 */
ANeuralNetworksExecution_setInputFromMemory(ANeuralNetworksExecution * execution,int32_t index,const ANeuralNetworksOperandType * type,const ANeuralNetworksMemory * memory,size_t offset,size_t length)586 inline int ANeuralNetworksExecution_setInputFromMemory(
587 ANeuralNetworksExecution* execution, int32_t index,
588 const ANeuralNetworksOperandType* type, const ANeuralNetworksMemory* memory,
589 size_t offset, size_t length) {
590 LOAD_FUNCTION(ANeuralNetworksExecution_setInputFromMemory);
591 EXECUTE_FUNCTION_RETURN(execution, index, type, memory, offset, length);
592 }
593
594 /**
595 * Associate a user buffer with an output of the model of the
596 * {@link ANeuralNetworksExecution}.
597 *
598 * <p>The provided buffer must outlive the execution.</p>
599 *
600 * See {@link ANeuralNetworksExecution} for information on multithreaded usage.
601 *
602 * @param execution The execution to be modified.
603 * @param index The index of the output argument we are setting. It is
604 * an index into the lists passed to
605 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is not
606 * the index associated with {@link
607 * ANeuralNetworksModel_addOperand}.
608 * @param type The type of the operand. This can be used to specify the
609 * dimensions that were set to 0 when the operand was added to the
610 * model. All other values must be the same as specified in the
611 * model. If the type is the same as specified when the model
612 * was built, NULL can be passed.
613 * @param buffer The buffer where the data is to be written.
614 * @param length The length in bytes of the buffer.
615 *
616 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA if
617 * the name is not recognized or the buffer is too small for the output.
618 */
ANeuralNetworksExecution_setOutput(ANeuralNetworksExecution * execution,int32_t index,const ANeuralNetworksOperandType * type,void * buffer,size_t length)619 inline int ANeuralNetworksExecution_setOutput(
620 ANeuralNetworksExecution* execution, int32_t index,
621 const ANeuralNetworksOperandType* type, void* buffer, size_t length) {
622 LOAD_FUNCTION(ANeuralNetworksExecution_setOutput);
623 EXECUTE_FUNCTION_RETURN(execution, index, type, buffer, length);
624 }
625
626 /**
627 * Associate part of a memory object with an output of the model of the
628 * {@link ANeuralNetworksExecution}.
629 *
630 * <p>The provided memory must outlive the execution.</p>
631 *
632 * See {@link ANeuralNetworksExecution} for information on multithreaded usage.
633 *
634 * @param execution The execution to be modified.
635 * @param index The index of the output argument we are setting. It is
636 * an index into the lists passed to
637 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is not
638 * the index associated with {@link
639 * ANeuralNetworksModel_addOperand}.
640 * @param type The type of the operand. This can be used to specify the
641 * dimensions that were set to 0 when the operand was added to the
642 * model. All other values must be the same as specified in the
643 * model. If the type is the same as specified when the model
644 * was built, NULL can be passed.
645 * @param memory The memory where the data is to be stored.
646 * @param offset This specifies the location of the data within the memory.
647 * The offset is in bytes from the start of memory.
648 * @param length The length in bytes of the data value.
649 *
650 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA if
651 * the name is not recognized or the buffer is too small for the output.
652 */
ANeuralNetworksExecution_setOutputFromMemory(ANeuralNetworksExecution * execution,int32_t index,const ANeuralNetworksOperandType * type,const ANeuralNetworksMemory * memory,size_t offset,size_t length)653 inline int ANeuralNetworksExecution_setOutputFromMemory(
654 ANeuralNetworksExecution* execution, int32_t index,
655 const ANeuralNetworksOperandType* type, const ANeuralNetworksMemory* memory,
656 size_t offset, size_t length) {
657 LOAD_FUNCTION(ANeuralNetworksExecution_setOutputFromMemory);
658 EXECUTE_FUNCTION_RETURN(execution, index, type, memory, offset, length);
659 }
660
661 /**
662 * Schedule evaluation of the execution.
663 *
664 * <p>Schedules evaluation of the execution. Once the model has been
665 * applied and the outputs are ready to be consumed, the execution will be
666 * signaled. Use {@link ANeuralNetworksExecution_wait} to wait for that signal.
667 * </p>
668 *
669 * Multiple executions can be scheduled and evaluated concurrently, and
670 * compilations can be performed concurrently with executions. The runtime makes
671 * no guarantee on the ordering of the completion of compilations and
672 * executions. If it's important to the application, the application should
673 * enforce the ordering by using {@link ANeuralNetworksCompilation_wait} and
674 * {@link ANeuralNetworksExecution_wait}.
675 *
676 * ANeuralNetworksExecution_wait must be called to recuperate the resources used
677 * by the execution.
678 *
679 * See {@link ANeuralNetworksExecution} for information on multithreaded usage.
680 *
681 * @param execution The execution to be scheduled and executed.
682 *
683 * @return ANEURALNETWORKS_NO_ERROR if successful.
684 */
ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution * execution,ANeuralNetworksEvent ** event)685 inline int ANeuralNetworksExecution_startCompute(
686 ANeuralNetworksExecution* execution, ANeuralNetworksEvent** event) {
687 LOAD_FUNCTION(ANeuralNetworksExecution_startCompute);
688 EXECUTE_FUNCTION_RETURN(execution, event);
689 }
690
691 /**
692 * Waits until the execution completes.
693 *
694 * More than one thread can wait on an event. When the execution completes,
695 * all threads will be released.
696 *
697 * See {@link ANeuralNetworksExecution} for information on multithreaded usage.
698 *
699 * @return ANEURALNETWORKS_NO_ERROR if the execution completed normally.
700 */
ANeuralNetworksEvent_wait(ANeuralNetworksEvent * event)701 inline int ANeuralNetworksEvent_wait(ANeuralNetworksEvent* event) {
702 LOAD_FUNCTION(ANeuralNetworksEvent_wait);
703 EXECUTE_FUNCTION_RETURN(event);
704 }
705
706 /**
707 * Destroys the event.
708 *
709 * See {@link ANeuralNetworksExecution} for information on multithreaded usage.
710 */
ANeuralNetworksEvent_free(ANeuralNetworksEvent * event)711 inline void ANeuralNetworksEvent_free(ANeuralNetworksEvent* event) {
712 LOAD_FUNCTION(ANeuralNetworksEvent_free);
713 EXECUTE_FUNCTION(event);
714 }
715
716 /**
717 * Get the number of available devices.
718 *
719 * @param numDevices Used to return the number of devices.
720 *
721 * @return ANEURALNETWORKS_NO_ERROR if successful.
722 *
723 * Available since API level 29.
724 */
ANeuralNetworks_getDeviceCount(uint32_t * numDevices)725 inline int ANeuralNetworks_getDeviceCount(uint32_t* numDevices) {
726 LOAD_FUNCTION(ANeuralNetworks_getDeviceCount);
727 EXECUTE_FUNCTION_RETURN(numDevices);
728 }
729
730 /**
731 * Get the representation of the specified device.
732 *
733 * @param devIndex The index of the specified device. Must be less than the
734 * number of available devices.
735 * @param device The representation of the specified device.
736 * The same representation will always be returned for the
737 * specified device.
738 *
739 * @return ANEURALNETWORKS_NO_ERROR if successful.
740 *
741 * Available since API level 29.
742 */
743
ANeuralNetworks_getDevice(uint32_t devIndex,ANeuralNetworksDevice ** device)744 inline int ANeuralNetworks_getDevice(uint32_t devIndex,
745 ANeuralNetworksDevice** device) {
746 LOAD_FUNCTION(ANeuralNetworks_getDevice);
747 EXECUTE_FUNCTION_RETURN(devIndex, device);
748 }
749
750 /**
751 * Get the name of the specified device.
752 *
753 * @param device The representation of the specified device.
754 * @param name The returned name of the specified device. The name will be in
755 * UTF-8 and will be null-terminated. It will be recognizable as a
756 * known device name rather than a cryptic string. For devices
757 * with API level 29 and above, the format of the name is
758 * {VENDOR}-{DEVICE}, e.g. “google-ipu”. For devices with feature
759 * level 28 or lower, the name will always be “unknown-device”.
760 * The name will remain valid for the duration of the application.
761 *
762 * @return ANEURALNETWORKS_NO_ERROR if successful.
763 *
764 * Available since API level 29.
765 */
ANeuralNetworksDevice_getName(const ANeuralNetworksDevice * device,const char ** name)766 inline int ANeuralNetworksDevice_getName(const ANeuralNetworksDevice* device,
767 const char** name) {
768 LOAD_FUNCTION(ANeuralNetworksDevice_getName);
769 EXECUTE_FUNCTION_RETURN(device, name);
770 }
771
772 /**
773 * Get the version of the driver implementation of the specified device.
774 *
775 * It’s the responsibility of the driver implementor to insure that this version
776 * string uniquely distinguishes this implementation from all previous
777 * implementations.
778 *
779 * This version string must not be confused with the feature level which is
780 * solely defined by {@link ANeuralNetworksDevice_getFeatureLevel}. There is no
781 * implicit ordering of the versions. For example, it is not possible to filter
782 * all drivers older than a certain version.
783 *
784 * Application developers may use this version string to avoid or prefer
785 * specific driver implementations. For example, an application may want to do
786 * so because:
787 * - A specific version of the driver does not provide the required
788 * performance, perhaps because of a performance regression.
789 * - A specific version of the driver has a bug or returns results that
790 * don’t match the minimum precision requirement for the application.
791 *
792 * @param device The representation of the specified device.
793 * @param version The returned version string of the driver for the specified
794 * device. The string will be in UTF-8 and will be
795 * null-terminated. For devices with feature level 28 or lower,
796 * "UNKNOWN" will be returned. The version string will remain
797 * valid for the duration of the application.
798 *
799 * @return ANEURALNETWORKS_NO_ERROR if successful.
800 *
801 * Available since API level 29.
802 */
ANeuralNetworksDevice_getVersion(const ANeuralNetworksDevice * device,const char ** version)803 inline int ANeuralNetworksDevice_getVersion(const ANeuralNetworksDevice* device,
804 const char** version) {
805 LOAD_FUNCTION(ANeuralNetworksDevice_getVersion);
806 EXECUTE_FUNCTION_RETURN(device, version);
807 }
808
809 /**
810 * Get the supported NNAPI version of the specified device.
811 *
812 * Each device has a supported feature level, which is the most advanced feature
813 * this driver implements. For example, if the driver implements the features
814 * introduced in Android P, but does not implement the features introduced after
815 * Android P, the value would be 28. Developers could decide whether or not the
816 * specified device should be used for a Model that has certain feature
817 * requirements.
818 *
819 * @param device The representation of the specified device.
820 * @param featureLevel The API level of the most advanced feature this driver
821 * implements.
822 *
823 * @return ANEURALNETWORKS_NO_ERROR if successful.
824 *
825 * Available since API level 29.
826 */
ANeuralNetworksDevice_getFeatureLevel(const ANeuralNetworksDevice * device,int64_t * featureLevel)827 inline int ANeuralNetworksDevice_getFeatureLevel(
828 const ANeuralNetworksDevice* device, int64_t* featureLevel) {
829 LOAD_FUNCTION(ANeuralNetworksDevice_getFeatureLevel);
830 EXECUTE_FUNCTION_RETURN(device, featureLevel);
831 }
832
833 /**
834 * Get the supported operations for a specified set of devices. If multiple
835 * devices are selected, the supported operation list is a union of supported
836 * operations of all selected devices.
837 *
838 * @param model The model to be queried.
839 * @param devices The set of devices. Must not contain duplicates.
840 * @param numDevices The number of devices in the set.
841 * @param supportedOps The boolean array to be filled. True means supported. The
842 * size of the boolean array must be at least as large as
843 * the number of operations in the model. The order of
844 * elements in the supportedOps array matches the order in
845 * which the corresponding operations were added to the
846 * model.
847 *
848 * @return ANEURALNETWORKS_NO_ERROR if successful.
849 *
850 * Available since API level 29.
851 */
ANeuralNetworksModel_getSupportedOperationsForDevices(const ANeuralNetworksModel * model,const ANeuralNetworksDevice * const * devices,uint32_t numDevices,bool * supportedOps)852 inline int ANeuralNetworksModel_getSupportedOperationsForDevices(
853 const ANeuralNetworksModel* model,
854 const ANeuralNetworksDevice* const* devices, uint32_t numDevices,
855 bool* supportedOps) {
856 LOAD_FUNCTION(ANeuralNetworksModel_getSupportedOperationsForDevices);
857 EXECUTE_FUNCTION_RETURN(model, devices, numDevices, supportedOps);
858 }
859
860 /**
861 * Create a {@link ANeuralNetworksCompilation} to compile the given model for a
862 * specified set of devices. If more than one device is specified, the
863 * compilation will distribute the workload automatically across the devices.
864 * The model must be fully supported by the specified set of devices. This means
865 * that ANeuralNetworksModel_getSupportedOperationsForDevices() must have
866 * returned true for every operation for that model/devices pair.
867 *
868 * @param model The {@link ANeuralNetworksModel} to be compiled.
869 * @param devices The set of devices. Must not contain duplicates.
870 * @param numDevices The number of devices in the set.
871 * @param compilation The newly created object or NULL if unsuccessful.
872 *
873 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA
874 * if the model is invalid.
875 *
876 * Available since API level 29.
877 */
ANeuralNetworksCompilation_createForDevices(ANeuralNetworksModel * model,const ANeuralNetworksDevice * const * devices,uint32_t numDevices,ANeuralNetworksCompilation ** compilation)878 inline int ANeuralNetworksCompilation_createForDevices(
879 ANeuralNetworksModel* model, const ANeuralNetworksDevice* const* devices,
880 uint32_t numDevices, ANeuralNetworksCompilation** compilation) {
881 LOAD_FUNCTION(ANeuralNetworksCompilation_createForDevices);
882 EXECUTE_FUNCTION_RETURN(model, devices, numDevices, compilation);
883 }
884
885 /**
886 * Sets the compilation caching signature and the cache directory.
887 *
888 * Provides optional caching information to the runtime for faster repeated
889 * compilation.
890 *
891 * See {@link ANeuralNetworksCompilation} for information on multithreaded
892 * usage.
893 *
894 * @param compilation The compilation to be modified.
895 * @param cacheDir The cache directory to store and retrieve caching data. It is
896 * recommended to use the code_cache provided by the Android
897 * runtime. If not using the code_cache, the user should choose
898 * a directory local to the application, and is responsible to
899 * manage and clean the cache entries.
900 * @param token The token provided by the user to specify a model, must be of
901 * length ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN. The user should
902 * ensure that the token is unique to a model within the
903 * application. The NNAPI runtime will not detected token
904 * collisions. If there is a collision, the compilation outcome may
905 * be incorrect without notifying with error.
906 *
907 * @return ANEURALNETWORKS_NO_ERROR if successful.
908 *
909 * Available since API level 29.
910 */
ANeuralNetworksCompilation_setCaching(ANeuralNetworksCompilation * compilation,const char * cacheDir,const uint8_t * token)911 inline int ANeuralNetworksCompilation_setCaching(
912 ANeuralNetworksCompilation* compilation, const char* cacheDir,
913 const uint8_t* token) {
914 LOAD_FUNCTION(ANeuralNetworksCompilation_setCaching);
915 EXECUTE_FUNCTION_RETURN(compilation, cacheDir, token);
916 }
917
918 /**
919 * Schedule synchronous evaluation of the execution.
920 *
921 * <p>Schedules synchronous evaluation of the execution. Returns once the
922 * execution has completed and the outputs are ready to be consumed.
923 * </p>
924 *
925 * See {@link ANeuralNetworksExecution} for information on multithreaded usage.
926 *
927 * See {@link ANeuralNetworksExecution_startCompute} for asynchronous execution.
928 * Synchronous execution incurs lower overhead than asynchronous execution.
929 *
930 * Available since API level 29.
931 *
932 * @param execution The execution to be scheduled and executed.
933 *
934 * @return ANEURALNETWORKS_NO_ERROR if the execution completed normally.
935 * ANEURALNETWORKS_UNMAPPABLE if the execution input or output memory
936 * cannot be properly mapped.
937 */
ANeuralNetworksExecution_compute(ANeuralNetworksExecution * execution)938 inline int ANeuralNetworksExecution_compute(
939 ANeuralNetworksExecution* execution) {
940 LOAD_FUNCTION(ANeuralNetworksExecution_compute);
941 EXECUTE_FUNCTION_RETURN(execution);
942 }
943
944 /**
945 * Get the dimensional information of the specified output operand of the model
946 * of the
947 * {@link ANeuralNetworksExecution}.
948 *
949 * On asynchronous execution initiated by {@link
950 * ANeuralNetworksExecution_startCompute},
951 * {@link ANeuralNetworksEvent_wait} must be called prior to this function to
952 * recuperate the resources used by the execution.
953 *
954 * @param execution The execution to be queried.
955 * @param index The index of the output argument we are querying. It is
956 * an index into the lists passed to
957 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is not
958 * the index associated with {@link
959 * ANeuralNetworksModel_addOperand}.
960 * @param rank The rank of the output operand.
961 *
962 * @return ANEURALNETWORKS_NO_ERROR if successful,
963 * ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE if the target output is provided an
964 * insufficient buffer at execution time, ANEURALNETWORKS_BAD_DATA if the index
965 * is invalid.
966 *
967 * Available since API level 29.
968 */
ANeuralNetworksExecution_getOutputOperandRank(ANeuralNetworksExecution * execution,int32_t index,uint32_t * rank)969 inline int ANeuralNetworksExecution_getOutputOperandRank(
970 ANeuralNetworksExecution* execution, int32_t index, uint32_t* rank) {
971 LOAD_FUNCTION(ANeuralNetworksExecution_getOutputOperandRank);
972 EXECUTE_FUNCTION_RETURN(execution, index, rank);
973 }
974
975 /**
976 * Get the dimensional information of the specified output operand of the model
977 * of the
978 * {@link ANeuralNetworksExecution}. The target output operand cannot be a
979 * scalar.
980 *
981 * On asynchronous execution initiated by
982 * {@link ANeuralNetworksExecution_startCompute},
983 * {@link ANeuralNetworksEvent_wait} must be called prior to this function to
984 * recuperate the resources used by the execution.
985 *
986 * @param execution The execution to be queried.
987 * @param index The index of the output argument we are querying. It is an index
988 * into the lists passed to
989 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is not
990 * the index associated with
991 * {@link ANeuralNetworksModel_addOperand}.
992 * @param dimensions The dimension array to be filled. The size of the array
993 * must be exactly as large as the rank of the output operand
994 * to be queried in the model.
995 *
996 * @return ANEURALNETWORKS_NO_ERROR if successful,
997 * ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE if the target output is provided an
998 * insufficient buffer at execution time, ANEURALNETWORKS_BAD_DATA if the index
999 * is invalid or if the target is a scalar.
1000 *
1001 * Available since API level 29.
1002 */
ANeuralNetworksExecution_getOutputOperandDimensions(ANeuralNetworksExecution * execution,int32_t index,uint32_t * dimensions)1003 inline int ANeuralNetworksExecution_getOutputOperandDimensions(
1004 ANeuralNetworksExecution* execution, int32_t index, uint32_t* dimensions) {
1005 LOAD_FUNCTION(ANeuralNetworksExecution_getOutputOperandDimensions);
1006 EXECUTE_FUNCTION_RETURN(execution, index, dimensions);
1007 }
1008
1009 /**
1010 * Create a {@link ANeuralNetworksBurst} to apply the given compilation.
1011 * This only creates the burst object. Computation is only performed once
1012 * {@link ANeuralNetworksExecution_burstCompute} is invoked with a valid
1013 * {@link ANeuralNetworksExecution} and {@link ANeuralNetworksBurst}.
1014 *
1015 * <p>The provided compilation must outlive the burst object.</p>
1016 *
1017 * Available since API level 29.
1018 *
1019 * @param compilation The {@link ANeuralNetworksCompilation} to be evaluated.
1020 * @param burst The newly created object or NULL if unsuccessful.
1021 *
1022 * @return ANEURALNETWORKS_NO_ERROR if successful, ANEURALNETWORKS_BAD_DATA
1023 * if the compilation is invalid.
1024 */
ANeuralNetworksBurst_create(ANeuralNetworksCompilation * compilation,ANeuralNetworksBurst ** burst)1025 inline int ANeuralNetworksBurst_create(ANeuralNetworksCompilation* compilation,
1026 ANeuralNetworksBurst** burst) {
1027 LOAD_FUNCTION(ANeuralNetworksBurst_create);
1028 EXECUTE_FUNCTION_RETURN(compilation, burst);
1029 }
1030
1031 /**
1032 * Destroys the burst object.
1033 *
1034 * Available since API level 29.
1035 *
1036 * @param burst The burst object to be destroyed. Passing NULL is acceptable and
1037 * results in no operation.
1038 */
ANeuralNetworksBurst_free(ANeuralNetworksBurst * burst)1039 inline void ANeuralNetworksBurst_free(ANeuralNetworksBurst* burst) {
1040 LOAD_FUNCTION(ANeuralNetworksBurst_free);
1041 EXECUTE_FUNCTION(burst);
1042 }
1043
1044 /**
1045 * Schedule synchronous evaluation of the execution on a burst object.
1046 *
1047 * <p>Schedules synchronous evaluation of the execution. Returns once the
1048 * execution has completed and the outputs are ready to be consumed.</p>
1049 *
1050 * <p>There must be at most one {@link ANeuralNetworksExecution} processing at
1051 * any given time for any given burst object. Any
1052 * {@link ANeuralNetworksExecution} launched before the previous has finished
1053 * will result in ANEURALNETWORKS_BAD_STATE.</p>
1054 *
1055 * Available since API level 29.
1056 *
1057 * @param burst The burst object to execute on.
1058 * @param execution The execution to be scheduled and executed. The execution
1059 * must be created from the same {@link
1060 * ANeuralNetworksCompilation} as the burst object.
1061 *
1062 * @return ANEURALNETWORKS_NO_ERROR if the execution completed normally.
1063 */
ANeuralNetworksExecution_burstCompute(ANeuralNetworksExecution * execution,ANeuralNetworksBurst * burst)1064 inline int ANeuralNetworksExecution_burstCompute(
1065 ANeuralNetworksExecution* execution, ANeuralNetworksBurst* burst) {
1066 LOAD_FUNCTION(ANeuralNetworksExecution_burstCompute);
1067 EXECUTE_FUNCTION_RETURN(execution, burst);
1068 }
1069
1070 /**
1071 * Creates a shared memory object from an AHardwareBuffer handle.
1072 *
1073 * If the shared memory is backed by an AHardwareBuffer of
1074 * AHARDWAREBUFFER_FORMAT_BLOB format, it can be used the same way as shared
1075 * memory created from a file handle. See
1076 * {@link ANeuralNetworksMemory} for a description on how to use this shared
1077 * memory.
1078 *
1079 * If the shared memory is backed by an AHardwareBuffer of a format other than
1080 * AHARDWAREBUFFER_FORMAT_BLOB, it can only be used for Model inputs and
1081 * outputs. When calling {@link ANeuralNetworksExecution_setInputFromMemory} or
1082 * {@link ANeuralNetworksExecution_setOutputFromMemory} with the shared memory,
1083 * both offset and length must be set to zero and the entire memory region will
1084 * be associated with the specified input or output operand. There is no
1085 * guarantee that an arbitrary AHardwareBuffer_Format and
1086 * AHardwareBuffer_UsageFlags combination can be used by arbitrary devices. The
1087 * execution will fail if selected set of devices cannot consume the buffer.
1088 *
1089 * Calling {@link ANeuralNetworksModel_setOperandValueFromMemory} with shared
1090 * memory backed by an AHardwareBuffer of a format other than
1091 * AHARDWAREBUFFER_FORMAT_BLOB is disallowed.
1092 *
1093 * TODO(miaowang): add documentation about intended usage with introspection
1094 * API.
1095 *
1096 * Available since API level 29.
1097 *
1098 * @param ahwb The AHardwareBuffer handle.
1099 * @param memory The memory object to be created.
1100 * Set to NULL if unsuccessful.
1101 *
1102 * @return ANEURALNETWORKS_NO_ERROR if the request completed normally.
1103 *
1104 * @see AHardwareBuffer
1105 */
ANeuralNetworksMemory_createFromAHardwareBuffer(const AHardwareBuffer * ahwb,ANeuralNetworksMemory ** memory)1106 inline int ANeuralNetworksMemory_createFromAHardwareBuffer(
1107 const AHardwareBuffer* ahwb, ANeuralNetworksMemory** memory) {
1108 LOAD_FUNCTION(ANeuralNetworksMemory_createFromAHardwareBuffer);
1109 EXECUTE_FUNCTION_RETURN(ahwb, memory);
1110 }
1111
1112 /**
1113 * Specifies whether duration of the {@link ANeuralNetworksExecution} is to be
1114 * measured. By default, duration is not measured.
1115 *
1116 * The {@link ANeuralNetworksExecution} must have been created with
1117 * {@link ANeuralNetworksCompilation_createForDevices} with numDevices = 1.
1118 *
1119 * See {@link ANeuralNetworksExecution} for information on multithreaded usage.
1120 *
1121 * Available since API level 29.
1122 *
1123 * @param execution The execution to be modified.
1124 * @param measure 'true' if duration is to be measured, 'false' if not.
1125 *
1126 * @return ANEURALNETWORKS_NO_ERROR if successful.
1127 */
ANeuralNetworksExecution_setMeasureTiming(ANeuralNetworksExecution * execution,bool measure)1128 inline int ANeuralNetworksExecution_setMeasureTiming(
1129 ANeuralNetworksExecution* execution, bool measure) {
1130 LOAD_FUNCTION(ANeuralNetworksExecution_setMeasureTiming);
1131 EXECUTE_FUNCTION_RETURN(execution, measure);
1132 }
1133
1134 /**
1135 * Get the time spent in the specified {@link ANeuralNetworksExecution}, in
1136 * nanoseconds. The execution must have completed.
1137 *
1138 * @param execution The execution to be queried.
1139 * @param durationCode The measurement to be queried, specified by {@link
1140 * DurationCode}.
1141 * @param duration The returned duration. If no measurement was requested by
1142 * {@link ANeuralNetworksExecution_setMeasureTiming}, or for
1143 * some other reason the duration is not available, UINT64_MAX will be returned.
1144 * A particular device need not support any given measurement.
1145 *
1146 * @return ANEURALNETWORKS_NO_ERROR if successful.
1147 */
ANeuralNetworksExecution_getDuration(const ANeuralNetworksExecution * execution,int32_t durationCode,uint64_t * duration)1148 inline int ANeuralNetworksExecution_getDuration(
1149 const ANeuralNetworksExecution* execution, int32_t durationCode,
1150 uint64_t* duration) {
1151 LOAD_FUNCTION(ANeuralNetworksExecution_getDuration);
1152 EXECUTE_FUNCTION_RETURN(execution, durationCode, duration);
1153 }
1154
1155 /**
1156 * Queries whether an extension is supported by the driver implementation of
1157 * the specified device.
1158 *
1159 * @param device The representation of the specified device.
1160 * @param extension The extension name.
1161 * @param isExtensionSupported The boolean value indicating whether the
1162 * extension is supported.
1163 *
1164 * @return ANEURALNETWORKS_NO_ERROR if successful.
1165 *
1166 * Available since API level 29.
1167 */
ANeuralNetworksDevice_getExtensionSupport(const ANeuralNetworksDevice * device,const char * extensionName,bool * isExtensionSupported)1168 inline int ANeuralNetworksDevice_getExtensionSupport(
1169 const ANeuralNetworksDevice* device, const char* extensionName,
1170 bool* isExtensionSupported) {
1171 LOAD_FUNCTION(ANeuralNetworksDevice_getExtensionSupport);
1172 EXECUTE_FUNCTION_RETURN(device, extensionName, isExtensionSupported);
1173 }
1174
1175 /**
1176 * Creates an operand type from an extension name and an extension operand code.
1177 *
1178 * See {@link ANeuralNetworksModel} for information on multithreaded usage.
1179 *
1180 * Available since API level 29.
1181 *
1182 * @param model The model to contain the operand.
1183 * @param extensionName The extension name.
1184 * @param operandCodeWithinExtension The extension operand code.
1185 * @param type The operand type.
1186 *
1187 * @return ANEURALNETWORKS_NO_ERROR if successful.
1188 */
ANeuralNetworksModel_getExtensionOperandType(ANeuralNetworksModel * model,const char * extensionName,uint16_t operandCodeWithinExtension,int32_t * type)1189 inline int ANeuralNetworksModel_getExtensionOperandType(
1190 ANeuralNetworksModel* model, const char* extensionName,
1191 uint16_t operandCodeWithinExtension, int32_t* type) {
1192 LOAD_FUNCTION(ANeuralNetworksModel_getExtensionOperandType);
1193 EXECUTE_FUNCTION_RETURN(model, extensionName, operandCodeWithinExtension,
1194 type);
1195 }
1196
1197 /**
1198 * Creates an operation type from an extension name and an extension operation
1199 * code.
1200 *
1201 * See {@link ANeuralNetworksModel} for information on multithreaded usage.
1202 *
1203 * Available since API level 29.
1204 *
1205 * @param model The model to contain the operation.
1206 * @param extensionName The extension name.
1207 * @param operationCodeWithinExtension The extension operation code.
1208 * @param type The operation type.
1209 *
1210 * @return ANEURALNETWORKS_NO_ERROR if successful.
1211 */
ANeuralNetworksModel_getExtensionOperationType(ANeuralNetworksModel * model,const char * extensionName,uint16_t operationCodeWithinExtension,ANeuralNetworksOperationType * type)1212 inline int ANeuralNetworksModel_getExtensionOperationType(
1213 ANeuralNetworksModel* model, const char* extensionName,
1214 uint16_t operationCodeWithinExtension, ANeuralNetworksOperationType* type) {
1215 LOAD_FUNCTION(ANeuralNetworksModel_getExtensionOperationType);
1216 EXECUTE_FUNCTION_RETURN(model, extensionName, operationCodeWithinExtension,
1217 type);
1218 }
1219
1220 /**
1221 * Sets extension operand parameters.
1222 *
1223 * Available since API level 29.
1224 *
1225 * @param model The model to be modified.
1226 * @param index The index of the model operand we're setting.
1227 * @param data A pointer to the extension operand data.
1228 * The data does not have to outlive the call to this function.
1229 * @param length The size in bytes of the data value.
1230 *
1231 * @return ANEURALNETWORKS_NO_ERROR if successful.
1232 */
ANeuralNetworksModel_setOperandExtensionData(ANeuralNetworksModel * model,int32_t index,const void * data,size_t length)1233 inline int ANeuralNetworksModel_setOperandExtensionData(
1234 ANeuralNetworksModel* model, int32_t index, const void* data,
1235 size_t length) {
1236 LOAD_FUNCTION(ANeuralNetworksModel_setOperandExtensionData);
1237 EXECUTE_FUNCTION_RETURN(model, index, data, length);
1238 }
1239
1240 /**
1241 * Create a {@link ANeuralNetworksMemoryDesc} with no properties.
1242 *
1243 * This only creates the memory descriptor. Its properties should be set with
1244 * calls to
1245 * {@link ANeuralNetworksMemoryDesc_addInputRole},
1246 * {@link ANeuralNetworksMemoryDesc_addOutputRole}, and
1247 * {@link ANeuralNetworksMemoryDesc_setDimensions}.
1248 *
1249 * {@link ANeuralNetworksMemoryDesc_finish} must be called once all properties
1250 * have been set.
1251 *
1252 * {@link ANeuralNetworksMemoryDesc_free} must be called once the memory
1253 * descriptor is no longer needed.
1254 *
1255 * Available since API level 30.
1256 *
1257 * @param desc The {@link ANeuralNetworksMemoryDesc} to be created.
1258 * Set to NULL if unsuccessful.
1259 *
1260 * @return ANEURALNETWORKS_NO_ERROR if successful.
1261 */
ANeuralNetworksMemoryDesc_create(ANeuralNetworksMemoryDesc ** desc)1262 inline int ANeuralNetworksMemoryDesc_create(ANeuralNetworksMemoryDesc** desc) {
1263 LOAD_FUNCTION(ANeuralNetworksMemoryDesc_create);
1264 EXECUTE_FUNCTION_RETURN(desc);
1265 }
1266
1267 /**
1268 * Destroy a memory descriptor.
1269 *
1270 * The memory descriptor need not have been finished by a call to
1271 * {@link ANeuralNetworksMemoryDesc_finish}.
1272 *
1273 * See {@link ANeuralNetworksMemoryDesc} for information on multithreaded usage.
1274 *
1275 * Available since API level 30.
1276 *
1277 * @param desc The memory descriptor to be destroyed. Passing NULL is acceptable
1278 * and results in no operation.
1279 */
ANeuralNetworksMemoryDesc_free(ANeuralNetworksMemoryDesc * desc)1280 inline void ANeuralNetworksMemoryDesc_free(ANeuralNetworksMemoryDesc* desc) {
1281 LOAD_FUNCTION(ANeuralNetworksMemoryDesc_free);
1282 EXECUTE_FUNCTION(desc);
1283 }
1284
1285 /**
1286 * Specify that a memory object will be playing the role of an output to an
1287 * execution created from a particular compilation.
1288 *
1289 * The compilation and the output index fully specify an output operand. This
1290 * function may be invoked multiple times on the same memory descriptor with
1291 * different output operands, and the same output operand may be specified on
1292 * multiple memory descriptors. However, specifying the same output operand on
1293 * the same memory descriptor object more than once will return an error.
1294 *
1295 * The dimensions of the corresponding model operands of all the roles specified
1296 * by
1297 * {@link ANeuralNetworksMemoryDesc_addInputRole} and
1298 * {@link ANeuralNetworksMemoryDesc_addOutputRole} must be compatible with each
1299 * other. Two dimensions are incompatible if both ranks are fully specified but
1300 * have different values, or if there is at least one axis that is fully
1301 * specified in both but has different values.
1302 *
1303 * At least one of {@link ANeuralNetworksMemoryDesc_addInputRole} and
1304 * {@link ANeuralNetworksMemoryDesc_addOutputRole} must be called on the memory
1305 * descriptor before invoking {@link ANeuralNetworksMemoryDesc_finish}.
1306 *
1307 * Attempting to modify a memory descriptor once
1308 * {@link ANeuralNetworksMemoryDesc_finish} has been called will return an
1309 * error.
1310 *
1311 * See {@link ANeuralNetworksMemoryDesc} for information on multithreaded usage.
1312 *
1313 * Available since API level 30.
1314 *
1315 * @param desc The memory descriptor to be modified.
1316 * @param compilation The compilation object. It must already have been finished
1317 * by calling {@link ANeuralNetworksCompilation_finish}, and must outlive the
1318 * memory descriptor.
1319 * @param index The index of the output argument we are referencing from the
1320 * compilation. It is an index into the outputs list passed to
1321 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is not
1322 * the index associated with {@link
1323 * ANeuralNetworksModel_addOperand}.
1324 * @param frequency A floating-point value within the range (0.0, 1.0].
1325 * Describes how likely the memory is to be used in the specified role. This is
1326 * provided as a hint to optimize the case when multiple roles
1327 * prefer different memory locations or data layouts.
1328 *
1329 * @return ANEURALNETWORKS_NO_ERROR if successful.
1330 */
ANeuralNetworksMemoryDesc_addOutputRole(ANeuralNetworksMemoryDesc * desc,const ANeuralNetworksCompilation * compilation,int32_t index,float frequency)1331 inline int ANeuralNetworksMemoryDesc_addOutputRole(
1332 ANeuralNetworksMemoryDesc* desc,
1333 const ANeuralNetworksCompilation* compilation, int32_t index,
1334 float frequency) {
1335 LOAD_FUNCTION(ANeuralNetworksMemoryDesc_addOutputRole);
1336 EXECUTE_FUNCTION_RETURN(desc, compilation, index, frequency);
1337 }
1338
1339 /**
1340 * Specify that a memory object will be playing the role of an input to an
1341 * execution created from a particular compilation.
1342 *
1343 * The compilation and the input index fully specify an input operand. This
1344 * function may be invoked multiple times on the same memory descriptor with
1345 * different input operands, and the same input operand may be specified on
1346 * multiple memory descriptors. However, specifying the same input operand on
1347 * the same memory descriptor more than once will return an error.
1348 *
1349 * The dimensions of the corresponding model operands of all the roles specified
1350 * by
1351 * {@link ANeuralNetworksMemoryDesc_addInputRole} and
1352 * {@link ANeuralNetworksMemoryDesc_addOutputRole} must be compatible with each
1353 * other. Two dimensions are incompatible if both ranks are fully specified but
1354 * have different values, or if there is at least one axis that is fully
1355 * specified in both but has different values.
1356 *
1357 * At least one of {@link ANeuralNetworksMemoryDesc_addInputRole} and
1358 * {@link ANeuralNetworksMemoryDesc_addOutputRole} must be called on a memory
1359 * descriptor before invoking {@link ANeuralNetworksMemoryDesc_finish}.
1360 *
1361 * Attempting to modify a memory descriptor once
1362 * {@link ANeuralNetworksMemoryDesc_finish} has been called will return an
1363 * error.
1364 *
1365 * See {@link ANeuralNetworksMemoryDesc} for information on multithreaded usage.
1366 *
1367 * Available since API level 30.
1368 *
1369 * @param desc The memory descriptor to be modified.
1370 * @param compilation The compilation object. It must already have been finished
1371 * by calling {@link ANeuralNetworksCompilation_finish}, and must outlive the
1372 * memory descriptor.
1373 * @param index The index of the input argument we are referencing from the
1374 * compilation. It is an index into the inputs list passed to
1375 * {@link ANeuralNetworksModel_identifyInputsAndOutputs}. It is not
1376 * the index associated with {@link
1377 * ANeuralNetworksModel_addOperand}.
1378 * @param frequency A floating-point value within the range (0.0, 1.0].
1379 * Describes how likely the memory is to be used in the specified role. This is
1380 * provided as a hint to optimize the case when different roles
1381 * prefer different memory locations or data layouts.
1382 *
1383 * @return ANEURALNETWORKS_NO_ERROR if successful.
1384 */
ANeuralNetworksMemoryDesc_addInputRole(ANeuralNetworksMemoryDesc * desc,const ANeuralNetworksCompilation * compilation,uint32_t index,float frequency)1385 inline int ANeuralNetworksMemoryDesc_addInputRole(
1386 ANeuralNetworksMemoryDesc* desc,
1387 const ANeuralNetworksCompilation* compilation, uint32_t index,
1388 float frequency) {
1389 LOAD_FUNCTION(ANeuralNetworksMemoryDesc_addInputRole);
1390 EXECUTE_FUNCTION_RETURN(desc, compilation, index, frequency);
1391 }
1392
1393 /**
1394 * Set the dimensional information of the memory descriptor.
1395 *
1396 * The specified dimensions must be compatible with the dimensions of the
1397 * corresponding model operands of all the roles specified by
1398 * {@link ANeuralNetworksMemoryDesc_addInputRole} and
1399 * {@link ANeuralNetworksMemoryDesc_addOutputRole}. Two dimensions are
1400 * incompatible if both ranks are fully specified but have different values, or
1401 * if there is at least one axis that is fully specified in both but has
1402 * different values.
1403 *
1404 * Attempting to modify a memory descriptor once
1405 * {@link ANeuralNetworksMemoryDesc_finish} has been called will return an
1406 * error.
1407 *
1408 * See {@link ANeuralNetworksMemoryDesc} for information on multithreaded usage.
1409 *
1410 * Available since API level 30.
1411 *
1412 * @param desc The memory descriptor to be modified.
1413 * @param rank The number of dimensions. Must be 0 for scalars.
1414 * @param dimensions An array of dimensions. An entry with the value 0 indicates
1415 * that the corresponding axis has an unknown size.
1416 *
1417 * @return ANEURALNETWORKS_NO_ERROR if successful.
1418 */
ANeuralNetworksMemoryDesc_setDimensions(ANeuralNetworksMemoryDesc * desc,uint32_t rank,const uint32_t * dimensions)1419 inline int ANeuralNetworksMemoryDesc_setDimensions(
1420 ANeuralNetworksMemoryDesc* desc, uint32_t rank,
1421 const uint32_t* dimensions) {
1422 LOAD_FUNCTION(ANeuralNetworksMemoryDesc_setDimensions);
1423 EXECUTE_FUNCTION_RETURN(desc, rank, dimensions);
1424 }
1425
1426 /**
1427 * Indicate that we have finished modifying a memory descriptor. Required before
1428 * calling
1429 * {@link ANeuralNetworksMemory_createFromDesc}.
1430 *
1431 * This function must only be called once for a given memory descriptor.
1432 *
1433 * See {@link ANeuralNetworksMemoryDesc} for information on multithreaded usage.
1434 *
1435 * Available since API level 30.
1436 *
1437 * @param desc The memory descriptor to be finished.
1438 *
1439 * @return ANEURALNETWORKS_NO_ERROR if successful.
1440 */
ANeuralNetworksMemoryDesc_finish(ANeuralNetworksMemoryDesc * desc)1441 inline int ANeuralNetworksMemoryDesc_finish(ANeuralNetworksMemoryDesc* desc) {
1442 LOAD_FUNCTION(ANeuralNetworksMemoryDesc_finish);
1443 EXECUTE_FUNCTION_RETURN(desc);
1444 }
1445
1446 /**
1447 * Creates a memory object from a memory descriptor.
1448 *
1449 * The memory object is created with an uninitialized buffer. A memory object
1450 * with an uninitialized buffer may only be used according to the roles
1451 * specified by
1452 * {@link ANeuralNetworksMemoryDesc_addOutputRole}, or as the destination memory
1453 * in
1454 * {@link ANeuralNetworksMemory_copy}. The buffer of a memory object is
1455 * initialized after the memory object is used as an output in a successful
1456 * execution, or used as the destination memory in a successful {@link
1457 * ANeuralNetworksMemory_copy}. A memory object with an initialized buffer may
1458 * be used according to all roles specified in
1459 * {@link ANeuralNetworksMemoryDesc}, or as the source or destination memory in
1460 * {@link ANeuralNetworksMemory_copy}. The buffer of a memory object will return
1461 * to the uninitialized state if the memory object is used as an output in a
1462 * failed execution, or used as the destination memory in a failed {@link
1463 * ANeuralNetworksMemory_copy}.
1464 *
1465 * The dimensions of the memory descriptor are deduced from the dimensions of
1466 * the corresponding model operands of all the roles specified by
1467 * {@link ANeuralNetworksMemoryDesc_addInputRole} and
1468 * {@link ANeuralNetworksMemoryDesc_addOutputRole}, as well as the dimensions
1469 * set by the call to {@link ANeuralNetworksMemoryDesc_setDimensions}, if any.
1470 * The memory descriptor may have unspecified dimensions or rank. In such a
1471 * case, the same memory object may be used with different shapes of outputs in
1472 * different executions. When the memory is used as an input, the input shape
1473 * must be the same as the output shape from the last execution using this
1474 * memory object as an output, or the last
1475 * {@link ANeuralNetworkMemory_copy} using this memory object as the destination
1476 * memory. Creating a memory object with unspecified dimensions or rank may fail
1477 * for certain sets of roles.
1478 *
1479 * Using the memory in roles or shapes that are not compatible with the rules
1480 * specified above will return an error.
1481 *
1482 * When calling {@link ANeuralNetworksExecution_setInputFromMemory} or
1483 * {@link ANeuralNetworksExecution_setOutputFromMemory} with the memory object,
1484 * both offset and length must be set to zero and the entire memory region will
1485 * be associated with the specified input or output operand.
1486 *
1487 * Calling {@link ANeuralNetworksModel_setOperandValueFromMemory} with the
1488 * memory created from this function will return an error.
1489 *
1490 * {@link ANeuralNetworksMemory_free} must be called once the memory is no
1491 * longer needed.
1492 *
1493 * Attempting to create memory from an unfinished memory descriptor will return
1494 * an error.
1495 *
1496 * The provided {@link ANeuralNetworksMemoryDesc} need not outlive the
1497 * {@link ANeuralNetworksMemory} object.
1498 *
1499 * Available since API level 30.
1500 *
1501 * @param desc The memory descriptor.
1502 * @param memory The memory object to be created.
1503 * Set to NULL if unsuccessful.
1504 *
1505 * @return ANEURALNETWORKS_NO_ERROR if successful; ANEURALNETWORKS_OP_FAILED if
1506 * the memory is created with unspecified dimensions or rank and it is not
1507 * supported for this set of roles.
1508 */
ANeuralNetworksMemory_createFromDesc(const ANeuralNetworksMemoryDesc * desc,ANeuralNetworksMemory ** memory)1509 inline int ANeuralNetworksMemory_createFromDesc(
1510 const ANeuralNetworksMemoryDesc* desc, ANeuralNetworksMemory** memory) {
1511 LOAD_FUNCTION(ANeuralNetworksMemory_createFromDesc);
1512 EXECUTE_FUNCTION_RETURN(desc, memory);
1513 }
1514
1515 /**
1516 * Copies data from one memory object to another.
1517 *
1518 * If at most one of the src and dst is created from
1519 * {@link ANeuralNetworksMemory_createFromDesc}, the src and dst must have the
1520 * same logical size:
1521 * - If the memory is created from {@link ANeuralNetworksMemory_createFromFd},
1522 * or if it is created from {@link
1523 * ANeuralNetworksMemory_createFromAHardwareBuffer} with format of
1524 * AHARDWAREBUFFER_FORMAT_BLOB, the logical size equals the size of the memory.
1525 * - If the memory is created from
1526 * {@link ANeuralNetworksMemory_createFromAHardwareBuffer} with a format other
1527 * than AHARDWAREBUFFER_FORMAT_BLOB, the logical size equals the size when there
1528 * is no padding and the data is tightly packed. This function may fail if the
1529 * AHardwareBuffer cannot be accessed.
1530 * - If the memory is created from {@link ANeuralNetworksMemory_createFromDesc},
1531 * the logical size equals the size indicated by the {@link OperandCode}
1532 * multiplied by the number of elements. This function will fail if the number
1533 * of elements is unknown.
1534 *
1535 * If both src and dst are created from {@link
1536 * ANeuralNetworksMemory_createFromDesc}, they must have compatible dimensions.
1537 * Two dimensions are incompatible if both ranks are fully specified but have
1538 * different values, or if there is at least one axis that is fully specified in
1539 * both but has different values. The dst may have unspecified dimensions or
1540 * rank. In such a case, the dimensions of dst will get updated according to the
1541 * dimensions of the src.
1542 *
1543 * In both cases, if the src is created from
1544 * {@link ANeuralNetworksMemory_createFromDesc}, it must have been used as an
1545 * output in a successful execution, or used as the destination memory in a
1546 * successful
1547 * {@link ANeuralNetworksMemory_copy}.
1548 *
1549 * The src and dst may have different data layout, in which case the data
1550 * copying is performed logically with data layout transformation.
1551 *
1552 * Available since API level 30.
1553 *
1554 * @param src The source memory object.
1555 * @param dst The destination memory object.
1556 *
1557 * @return ANEURALNETWORKS_NO_ERROR if successful.
1558 */
ANeuralNetworksMemory_copy(const ANeuralNetworksMemory * src,const ANeuralNetworksMemory * dst)1559 inline int ANeuralNetworksMemory_copy(const ANeuralNetworksMemory* src,
1560 const ANeuralNetworksMemory* dst) {
1561 LOAD_FUNCTION(ANeuralNetworksMemory_copy);
1562 EXECUTE_FUNCTION_RETURN(src, dst);
1563 }
1564
1565 /**
1566 * Create a {@link ANeuralNetworksEvent} from a sync_fence file descriptor.
1567 *
1568 * The newly created ANeuralNetworksEvent does not take ownership of the
1569 * provided sync_fence_fd, it will instead dup the provided sync_fence_fd and
1570 * own the duplicate.
1571 *
1572 * @param sync_fence_fd The sync_fence file descriptor.
1573 * @param event The newly created object or NULL if unsuccessful.
1574 *
1575 * @return ANEURALNETWORKS_NO_ERROR if successful.
1576 *
1577 * Available since API level 30.
1578 */
ANeuralNetworksEvent_createFromSyncFenceFd(int sync_fence_fd,ANeuralNetworksEvent ** event)1579 inline int ANeuralNetworksEvent_createFromSyncFenceFd(
1580 int sync_fence_fd, ANeuralNetworksEvent** event) {
1581 LOAD_FUNCTION(ANeuralNetworksEvent_createFromSyncFenceFd);
1582 EXECUTE_FUNCTION_RETURN(sync_fence_fd, event);
1583 }
1584
1585 /**
1586 * Get sync_fence file descriptor from the event.
1587 *
1588 * If the ANeuralNetworksEvent is not backed by a sync fence, the sync_fence_fd
1589 * will be set to -1, and ANEURALNETWORKS_BAD_DATA will be returned.
1590 *
1591 * See {@link ANeuralNetworksEvent_createFromSyncFenceFd} and
1592 * {@link ANeuralNetworksExecution_startComputeWithDependencies} to see how to
1593 * create an event backed by a sync fence.
1594 *
1595 * The user takes ownership of the returned fd, and must close the returned file
1596 * descriptor when it is no longer needed.
1597 *
1598 * @param event An event that is backed by a sync fence.
1599 * @param sync_fence_fd The sync_fence file descriptor. The file descriptor will
1600 * be set to -1 if there is an error.
1601 *
1602 * @return ANEURALNETWORKS_NO_ERROR if successful.
1603 *
1604 * Available since API level 30.
1605 */
ANeuralNetworksEvent_getSyncFenceFd(const ANeuralNetworksEvent * event,int * sync_fence_fd)1606 inline int ANeuralNetworksEvent_getSyncFenceFd(
1607 const ANeuralNetworksEvent* event, int* sync_fence_fd) {
1608 LOAD_FUNCTION(ANeuralNetworksEvent_getSyncFenceFd);
1609 EXECUTE_FUNCTION_RETURN(event, sync_fence_fd);
1610 }
1611
1612 /**
1613 * Schedule asynchronous evaluation of the execution with dependencies.
1614 *
1615 * The execution will wait for all the depending events to be signaled before
1616 * starting the evaluation. Once the execution has completed and the outputs
1617 * are ready to be consumed, the returned event will be signaled. Depending on
1618 * which devices are handling the execution, the event could be backed by a sync
1619 * fence. Use {@link ANeuralNetworksEvent_wait} to wait for that event.
1620 *
1621 * ANeuralNetworksEvent_wait must be called to recurperate the resources used
1622 * by the execution.
1623 *
1624 * If parts of the execution are scheduled on devices that do not support fenced
1625 * execution, the function call may wait for such parts to finish before
1626 * returning.
1627 *
1628 * The function will return an error if any of the events in dependencies is
1629 * already in a bad state. After the execution is scheduled, if any of the
1630 * events in dependencies does not complete normally, the execution will fail,
1631 * and {@link ANeuralNetworksEvent_wait} on the returned event will return an
1632 * error.
1633 *
1634 * The function will return an error if any of the execution outputs has a
1635 * tensor operand type that is not fully specified.
1636 *
1637 * The function can be passed a timeout duration in nanoseconds. This timeout
1638 * duration acts as a hint to drivers in the same way that the timeout durations
1639 * in {@link ANeuralNetworksCompilation_setTimeout} and {@link
1640 * ANeuralNetworksExecution_setTimeout} act as hints to drivers. The duration
1641 * begins when all waitFor sync fences have been signaled, and can be used
1642 * together with {@link ANeuralNetworksExecution_setTimeout} which specifies the
1643 * maximum timeout duration beginning at the call to
1644 * {@link ANeuralNetworksExecution_startComputeWithDependencies}.
1645 * If the duration is non-zero, the {@link ANeuralNetworksExecution} must have
1646 * been created from an {@link ANeuralNetworksCompilation} which in turn was
1647 * created from
1648 * {@link ANeuralNetworksCompilation_createForDevices} with numDevices = 1,
1649 * otherwise this function will fail with ANEURALNETWORKS_BAD_DATA. If either
1650 * the timeout duration from {@link ANeuralNetworksExecution_setTimeout} or the
1651 * timeout duration passed to this call is exceeded, the execution may be
1652 * aborted, in which case {@link ANEURALNETWORKS_MISSED_DEADLINE_*} will be
1653 * returned through {@link
1654 * ANeuralNetworksExecution_startComputeWithDependencies} or {@link
1655 * ANeuralNetworksEvent_wait} on the event object. If the device has a feature
1656 * level reported by {@link ANeuralNetworksDevice_getFeatureLevel} that is lower
1657 * than 30, then the timeout duration hints will be ignored.
1658 *
1659 * If this execution contains a {@link ANEURALNETWORKS_WHILE} operation, and
1660 * the condition model does not output false within the loop timeout duration,
1661 * then execution will be aborted and {@link ANEURALNETWORKS_MISSED_DEADLINE_*}
1662 * will be returned through {@link ANeuralNetworksEvent_wait} on the event
1663 * object.
1664 *
1665 * See {@link ANeuralNetworksExecution} for information on multithreaded usage.
1666 *
1667 * See {@link ANeuralNetworksExecution_compute} for synchronous execution.
1668 * See {@link ANeuralNetworksExecution_burstCompute} for burst synchronous
1669 * execution. See {@link ANeuralNetworksExecution_startCompute} for regular
1670 * asynchronous execution.
1671 *
1672 * @param execution The execution to be scheduled and executed.
1673 * @param dependencies A set of depending events. The actual evaluation will not
1674 * start until all the events are signaled.
1675 * @param num_dependencies The number of events in the dependencies set.
1676 * @param duration The maximum amount of time in nanoseconds that is expected to
1677 * be spent executing the model after all dependencies are
1678 * signaled. If set to 0, the timeout duration is considered
1679 * infinite.
1680 * @param event The event that will be signaled on completion. event is set to
1681 * NULL if there's an error.
1682 *
1683 * @return ANEURALNETWORKS_NO_ERROR if the evaluation is successfully scheduled.
1684 *
1685 * Available since API level 30.
1686 */
ANeuralNetworksExecution_startComputeWithDependencies(ANeuralNetworksExecution * execution,const ANeuralNetworksEvent * const * dependencies,uint32_t num_dependencies,uint64_t duration,ANeuralNetworksEvent ** event)1687 inline int ANeuralNetworksExecution_startComputeWithDependencies(
1688 ANeuralNetworksExecution* execution,
1689 const ANeuralNetworksEvent* const* dependencies, uint32_t num_dependencies,
1690 uint64_t duration, ANeuralNetworksEvent** event) {
1691 LOAD_FUNCTION(ANeuralNetworksExecution_startComputeWithDependencies);
1692 EXECUTE_FUNCTION_RETURN(execution, dependencies, num_dependencies, duration,
1693 event);
1694 }
1695
1696 #endif // TENSORFLOW_LITE_NNAPI_NEURALNETWORKSSHIM_H_
1697