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_NEURALNETWORKSTYPES_H_
16 #define TENSORFLOW_LITE_NNAPI_NEURALNETWORKSTYPES_H_
17 
18 #include <stdint.h>
19 #include <stdio.h>
20 
21 typedef struct AHardwareBuffer AHardwareBuffer;
22 
23 // NN api types based on NNAPI header file
24 // https://developer.android.com/ndk/reference/group/neural-networks
25 
26 /**
27  * Operand types.
28  *
29  * The type of operands that can be added to a model.
30  *
31  * Although we define many types, most operators accept just a few
32  * types.  Most used are ANEURALNETWORKS_TENSOR_FLOAT32,
33  * ANEURALNETWORKS_TENSOR_QUANT8_ASYMM, and ANEURALNETWORKS_INT32.
34  */
35 enum {
36   ANEURALNETWORKS_FLOAT32 = 0,
37   ANEURALNETWORKS_INT32 = 1,
38   ANEURALNETWORKS_UINT32 = 2,
39   ANEURALNETWORKS_TENSOR_FLOAT32 = 3,
40   ANEURALNETWORKS_TENSOR_INT32 = 4,
41   ANEURALNETWORKS_TENSOR_QUANT8_ASYMM = 5,
42   ANEURALNETWORKS_TENSOR_QUANT8_SYMM = 13,
43 };
44 
45 /**
46  * Operation types.
47  *
48  * The type of operations that can be added to a model.
49  */
50 enum {
51   ANEURALNETWORKS_ADD = 0,
52   ANEURALNETWORKS_AVERAGE_POOL_2D = 1,
53   ANEURALNETWORKS_CONCATENATION = 2,
54   ANEURALNETWORKS_CONV_2D = 3,
55   ANEURALNETWORKS_DEPTHWISE_CONV_2D = 4,
56   ANEURALNETWORKS_DEPTH_TO_SPACE = 5,
57   ANEURALNETWORKS_DEQUANTIZE = 6,
58   ANEURALNETWORKS_EMBEDDING_LOOKUP = 7,
59   ANEURALNETWORKS_FLOOR = 8,
60   ANEURALNETWORKS_FULLY_CONNECTED = 9,
61   ANEURALNETWORKS_HASHTABLE_LOOKUP = 10,
62   ANEURALNETWORKS_L2_NORMALIZATION = 11,
63   ANEURALNETWORKS_L2_POOL_2D = 12,
64   ANEURALNETWORKS_LOCAL_RESPONSE_NORMALIZATION = 13,
65   ANEURALNETWORKS_LOGISTIC = 14,
66   ANEURALNETWORKS_LSH_PROJECTION = 15,
67   ANEURALNETWORKS_LSTM = 16,
68   ANEURALNETWORKS_MAX_POOL_2D = 17,
69   ANEURALNETWORKS_MUL = 18,
70   ANEURALNETWORKS_RELU = 19,
71   ANEURALNETWORKS_RELU1 = 20,
72   ANEURALNETWORKS_RELU6 = 21,
73   ANEURALNETWORKS_RESHAPE = 22,
74   ANEURALNETWORKS_RESIZE_BILINEAR = 23,
75   ANEURALNETWORKS_RNN = 24,
76   ANEURALNETWORKS_SOFTMAX = 25,
77   ANEURALNETWORKS_SPACE_TO_DEPTH = 26,
78   ANEURALNETWORKS_SVDF = 27,
79   ANEURALNETWORKS_TANH = 28,
80   ANEURALNETWORKS_BATCH_TO_SPACE_ND = 29,
81   ANEURALNETWORKS_DIV = 30,
82   ANEURALNETWORKS_MEAN = 31,
83   ANEURALNETWORKS_PAD = 32,
84   ANEURALNETWORKS_SPACE_TO_BATCH_ND = 33,
85   ANEURALNETWORKS_SQUEEZE = 34,
86   ANEURALNETWORKS_STRIDED_SLICE = 35,
87   ANEURALNETWORKS_SUB = 36,
88   ANEURALNETWORKS_TRANSPOSE = 37,
89 };
90 
91 /**
92  * Fused activation function types.
93  *
94  */
95 enum {
96   ANEURALNETWORKS_FUSED_NONE = 0,
97   ANEURALNETWORKS_FUSED_RELU = 1,
98   ANEURALNETWORKS_FUSED_RELU1 = 2,
99   ANEURALNETWORKS_FUSED_RELU6 = 3,
100 };
101 
102 /**
103  * Execution preferences.
104  */
105 enum {
106   ANEURALNETWORKS_PREFER_LOW_POWER = 0,
107   ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER = 1,
108   ANEURALNETWORKS_PREFER_SUSTAINED_SPEED = 2,
109 };
110 
111 /**
112  * Result codes.
113  */
114 enum {
115   ANEURALNETWORKS_NO_ERROR = 0,
116   ANEURALNETWORKS_OUT_OF_MEMORY = 1,
117   ANEURALNETWORKS_INCOMPLETE = 2,
118   ANEURALNETWORKS_UNEXPECTED_NULL = 3,
119   ANEURALNETWORKS_BAD_DATA = 4,
120   ANEURALNETWORKS_OP_FAILED = 5,
121   ANEURALNETWORKS_BAD_STATE = 6,
122   ANEURALNETWORKS_UNMAPPABLE = 7,
123   ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE = 8,
124   ANEURALNETWORKS_UNAVAILABLE_DEVICE = 9,
125 };
126 
127 /**
128  * Implicit padding algorithms.
129  */
130 enum {
131   ANEURALNETWORKS_PADDING_SAME = 1,
132   ANEURALNETWORKS_PADDING_VALID = 2,
133 };
134 
135 /**
136  * Device types.
137  *
138  * The type of NNAPI device.
139  */
140 enum {
141   /** The device type cannot be provided. */
142   ANEURALNETWORKS_DEVICE_UNKNOWN = 0,
143   /** The device does not fall into any category below. */
144   ANEURALNETWORKS_DEVICE_OTHER = 1,
145   /** The device runs NNAPI models on single or multi-core CPU. */
146   ANEURALNETWORKS_DEVICE_CPU = 2,
147   /** The device can run NNAPI models and also accelerate graphics APIs such
148    * as OpenGL ES and Vulkan. */
149   ANEURALNETWORKS_DEVICE_GPU = 3,
150   /** Dedicated accelerator for Machine Learning workloads. */
151   ANEURALNETWORKS_DEVICE_ACCELERATOR = 4,
152 };
153 
154 /**
155  * ANeuralNetworksMemory is an opaque type that represents memory.
156  *
157  * This type is used to represent shared memory, memory mapped files,
158  * and similar memories.
159  *
160  * By using shared memory, a program can efficiently communicate to the
161  * runtime and drivers the tensors that define a model. See
162  * {@link ANeuralNetworksModel_setOperandValueFromMemory}. An application
163  * should typically create one shared memory object that contains every tensor
164  * needed to define a model. {@link ANeuralNetworksMemory_createFromFd} can be
165  * used to create shared memory from a file handle. {@link
166  * ANeuralNetworksMemory_createShared} can be used to directly created shared
167  * memory.
168  *
169  * Memory objects can also be used to specify the input and output arguments of
170  * an execution. See {@link ANeuralNetworksExecution_setInputFromMemory}
171  * and {@link ANeuralNetworksExecution_setOutputFromMemory}.
172  */
173 typedef struct ANeuralNetworksMemory ANeuralNetworksMemory;
174 
175 /**
176  * ANeuralNetworksModel is an opaque type that contains a description of the
177  * mathematical operations that constitute the model.
178  *
179  * <p>The model will be built by calling<ul>
180  * <li>{@link ANeuralNetworksModel_create},</li>
181  * <li>{@link ANeuralNetworksModel_addOperation},</li>
182  * <li>{@link ANeuralNetworksModel_addOperand},</li>
183  * </ul>
184  *
185  * A model is completed by calling {@link ANeuralNetworksModel_finish}.
186  * A model is destroyed by calling {@link ANeuralNetworksModel_free}.
187  *
188  * <p>It is the application's responsibility to make sure that only one thread
189  * modifies a model at a given time. It is however safe for more than one
190  * thread to use the model once {@link ANeuralNetworksModel_finish} has
191  * returned.</p>
192  *
193  * <p>It is also the application's responsibility to ensure that there are no
194  * other uses of the model after calling {@link ANeuralNetworksModel_free}. This
195  * includes any compilation or execution object created using the model.</p>
196  */
197 typedef struct ANeuralNetworksModel ANeuralNetworksModel;
198 
199 /**
200  * ANeuralNetworksCompilation is an opaque type that can be used to compile
201  * a machine learning model.
202  *
203  * <p>To use:<ul>
204  *    <li>Create a new compilation instance by calling the
205  *        {@link ANeuralNetworksCompilation_create} function.</li>
206  *    <li>Perform the compilation with {@link
207  * ANeuralNetworksCompilation_start}.</li> <li>Wait for the compilation to
208  * complete with {@link ANeuralNetworksCompilation_wait}.</li> <li>Use the
209  * compilation as many times as needed with {@link
210  * ANeuralNetworksExecution_create}.</li> <li>Destroy the compilation with
211  * {@link ANeuralNetworksCompilation_free} once all executions using the
212  * compilation have completed.</li></ul></p>
213  *
214  * <p>A compilation cannot be modified once {@link
215  * ANeuralNetworksCompilation_start} has been called on it.</p>
216  *
217  * <p>It is the application's responsibility to make sure that only one thread
218  * modifies a compilation at a given time. It is however safe for more than one
219  * thread to use {@link ANeuralNetworksCompilation_wait} at the same time.
220  * It is also safe for multiple threads to use a compilation object once
221  * {@link ANeuralNetworksCompilation_wait} has completed.</p>
222  *
223  * <p>It is also the application's responsibility to ensure that there are no
224  * other uses of the compilation after calling {@link
225  * ANeuralNetworksCompilation_free}. This includes any execution object created
226  * using the compilation.</p>
227  */
228 typedef struct ANeuralNetworksCompilation ANeuralNetworksCompilation;
229 
230 /**
231  * ANeuralNetworksExecution is an opaque type that can be used to apply a
232  * machine learning model to a set of inputs.
233  *
234  * <p>To use:<ul>
235  *    <li>Create a new execution instance by calling the
236  *        {@link ANeuralNetworksExecution_create} function.</li>
237  *    <li>Associate data to the model inputs with
238  *        {@link ANeuralNetworksExecution_setInput} or
239  *        {@link ANeuralNetworksExecution_setInputFromMemory}.</li>
240  *    <li>Associate output buffers to the model outputs with
241  *        {@link ANeuralNetworksExecution_setOutput} or
242  *        {@link ANeuralNetworksExecution_setOutputFromMemory}.</li>
243  *    <li>Apply the model with {@link
244  * ANeuralNetworksExecution_startCompute}.</li> <li>Wait for the execution to
245  * complete with {@link ANeuralNetworksExecution_wait}.</li> <li>Destroy the
246  * execution with
247  *        {@link ANeuralNetworksExecution_free}.</li></ul></p>
248  *
249  * <p>An execution cannot be modified once {@link
250  * ANeuralNetworksExecution_start} has been called on it.</p>
251  *
252  * <p>An execution can be applied to a model with
253  * {@link ANeuralNetworksExecution_startCompute} only once. Create new
254  * executions to do new evaluations of the model.</p>
255  *
256  * <p>It is the application's responsibility to make sure that only one thread
257  * modifies an execution at a given time. It is however safe for more than one
258  * thread to use {@link ANeuralNetworksExecution_wait} at the same time.</p>
259  *
260  * <p>It is also the application's responsibility to ensure that there are no
261  * other uses of the request after calling {@link
262  * ANeuralNetworksRequest_free}.</p>
263  */
264 typedef struct ANeuralNetworksExecution ANeuralNetworksExecution;
265 
266 /**
267  * Parameters for ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL operand.
268  */
269 typedef struct ANeuralNetworksSymmPerChannelQuantParams {
270   /* The index of the channel dimension. */
271   uint32_t channelDim;
272   /** The size of the scale array. Should be equal to dimension[channelDim] of
273    * the Operand. */
274   uint32_t scaleCount;
275   /** The array of scaling values for each channel. Each value must be greater
276    * than zero. */
277   const float* scales;
278 } ANeuralNetworksSymmPerChannelQuantParams;
279 
280 /**
281  * ANeuralNetworksBurst is an opaque type that can be used to reduce the latency
282  * of a rapid sequence of executions. It will likely cause overhead if only used
283  * for a single execution.
284  *
285  * ANeuralNetworksBurst serves as a context object for any number of inferences
286  * using {@link ANeuralNetworksExecution} objects. An ANeuralNetworksBurst
287  * object and the {@link ANeuralNetworksExecution} objects used with it must all
288  * have been created from the same {@link ANeuralNetworksCompilation} object.
289  *
290  * This object is also used as a hint to drivers, providing insight to the
291  * lifetime of a rapid sequence of executions. For example, a driver may choose
292  * to increase the clock frequency of its accelerator for the lifetime of a
293  * burst object.
294  *
295  * <p>To use:<ul>
296  *    <li>Create a new burst object by calling the
297  *        {@link ANeuralNetworksBurst_create} function.</li>
298  *    <li>For each execution:</li><ul>
299  *        <li>Create {@link ANeuralNetworksExecution} and configure its
300  *            properties (see {@link ANeuralNetworksExecution} for
301  * details).</li> <li>Apply the model synchronously with
302  *            {@link ANeuralNetworksExecution_burstCompute}, reusing the same
303  *            {@link ANeuralNetworksBurst} with the new
304  *            {@link ANeuralNetworksExecution}.</li>
305  *        <li>Use and free the {@link ANeuralNetworksExecution}.</li></ul>
306  *    <li>Destroy the burst with
307  *        {@link ANeuralNetworksBurst_free}.</li></ul></p>
308  *
309  * Available since API level 29.
310  */
311 typedef struct ANeuralNetworksBurst ANeuralNetworksBurst;
312 
313 /**
314  * ANeuralNetworksOperandType describes the type of an operand.
315  * This structure is used to describe both scalars and tensors.
316  */
317 typedef struct ANeuralNetworksOperandType {
318   /** The data type, e.g ANEURALNETWORKS_INT8. */
319   int32_t type;
320   /** The number of dimensions. It should be 0 for scalars. */
321   uint32_t dimensionCount;
322   /** The dimensions of the tensor. It should be nullptr for scalars. */
323   const uint32_t* dimensions;
324   /** These two fields are only used for quantized tensors.
325    * They should be zero for scalars and non-fixed point tensors.
326    * The dequantized value of each entry is (value - offset) * scale.
327    */
328   float scale;
329   int32_t zeroPoint;
330 } ANeuralNetworksOperandType;
331 
332 /**
333  * ANeuralNetworksEvent is an opaque type that represents an event
334  * that will be signaled once an execution completes.
335  */
336 typedef struct ANeuralNetworksEvent ANeuralNetworksEvent;
337 
338 typedef int32_t ANeuralNetworksOperationType;
339 
340 /**
341  * ANeuralNetworksDevice is an opaque type that represents a device.
342  *
343  * This type is used to query basic properties and supported operations of the
344  * corresponding device, and control which device(s) a model is to be run on.
345  *
346  * Available since API level 29.
347  */
348 typedef struct ANeuralNetworksDevice ANeuralNetworksDevice;
349 
350 // nn api function types
351 
352 typedef int (*ANeuralNetworksMemory_createFromFd_fn)(
353     size_t size, int protect, int fd, size_t offset,
354     ANeuralNetworksMemory** memory);
355 
356 typedef void (*ANeuralNetworksMemory_free_fn)(ANeuralNetworksMemory* memory);
357 
358 typedef int (*ANeuralNetworksModel_create_fn)(ANeuralNetworksModel** model);
359 
360 typedef int (*ANeuralNetworksModel_finish_fn)(ANeuralNetworksModel* model);
361 
362 typedef void (*ANeuralNetworksModel_free_fn)(ANeuralNetworksModel* model);
363 
364 typedef int (*ANeuralNetworksCompilation_create_fn)(
365     ANeuralNetworksModel* model, ANeuralNetworksCompilation** compilation);
366 
367 typedef void (*ANeuralNetworksCompilation_free_fn)(
368     ANeuralNetworksCompilation* compilation);
369 
370 typedef int (*ANeuralNetworksCompilation_setPreference_fn)(
371     ANeuralNetworksCompilation* compilation, int32_t preference);
372 
373 typedef int (*ANeuralNetworksCompilation_finish_fn)(
374     ANeuralNetworksCompilation* compilation);
375 
376 typedef int (*ANeuralNetworksModel_addOperand_fn)(
377     ANeuralNetworksModel* model, const ANeuralNetworksOperandType* type);
378 
379 typedef int (*ANeuralNetworksModel_setOperandValue_fn)(
380     ANeuralNetworksModel* model, int32_t index, const void* buffer,
381     size_t length);
382 
383 typedef int (*ANeuralNetworksModel_setOperandSymmPerChannelQuantParams_fn)(
384     ANeuralNetworksModel* model, int32_t index,
385     const ANeuralNetworksSymmPerChannelQuantParams* channelQuant);
386 
387 typedef int (*ANeuralNetworksModel_setOperandValueFromMemory_fn)(
388     ANeuralNetworksModel* model, int32_t index,
389     const ANeuralNetworksMemory* memory, size_t offset, size_t length);
390 
391 typedef int (*ANeuralNetworksModel_addOperation_fn)(
392     ANeuralNetworksModel* model, ANeuralNetworksOperationType type,
393     uint32_t inputCount, const uint32_t* inputs, uint32_t outputCount,
394     const uint32_t* outputs);
395 
396 typedef int (*ANeuralNetworksModel_identifyInputsAndOutputs_fn)(
397     ANeuralNetworksModel* model, uint32_t inputCount, const uint32_t* inputs,
398     uint32_t outputCount, const uint32_t* outputs);
399 
400 typedef int (*ANeuralNetworksModel_relaxComputationFloat32toFloat16_fn)(
401     ANeuralNetworksModel* model, bool allow);
402 
403 typedef int (*ANeuralNetworksExecution_create_fn)(
404     ANeuralNetworksCompilation* compilation,
405     ANeuralNetworksExecution** execution);
406 
407 typedef void (*ANeuralNetworksExecution_free_fn)(
408     ANeuralNetworksExecution* execution);
409 
410 typedef int (*ANeuralNetworksExecution_setInput_fn)(
411     ANeuralNetworksExecution* execution, int32_t index,
412     const ANeuralNetworksOperandType* type, const void* buffer, size_t length);
413 
414 typedef int (*ANeuralNetworksExecution_setInputFromMemory_fn)(
415     ANeuralNetworksExecution* execution, int32_t index,
416     const ANeuralNetworksOperandType* type, const ANeuralNetworksMemory* memory,
417     size_t offset, size_t length);
418 
419 typedef int (*ANeuralNetworksExecution_setOutput_fn)(
420     ANeuralNetworksExecution* execution, int32_t index,
421     const ANeuralNetworksOperandType* type, void* buffer, size_t length);
422 
423 typedef int (*ANeuralNetworksExecution_setOutputFromMemory_fn)(
424     ANeuralNetworksExecution* execution, int32_t index,
425     const ANeuralNetworksOperandType* type, const ANeuralNetworksMemory* memory,
426     size_t offset, size_t length);
427 
428 typedef int (*ANeuralNetworksExecution_startCompute_fn)(
429     ANeuralNetworksExecution* execution, ANeuralNetworksEvent** event);
430 
431 typedef int (*ANeuralNetworksEvent_wait_fn)(ANeuralNetworksEvent* event);
432 
433 typedef void (*ANeuralNetworksEvent_free_fn)(ANeuralNetworksEvent* event);
434 
435 typedef int (*ASharedMemory_create_fn)(const char* name, size_t size);
436 
437 typedef int (*ANeuralNetworks_getDeviceCount_fn)(uint32_t* numDevices);
438 
439 typedef int (*ANeuralNetworks_getDevice_fn)(uint32_t devIndex,
440                                             ANeuralNetworksDevice** device);
441 
442 typedef int (*ANeuralNetworksDevice_getName_fn)(
443     const ANeuralNetworksDevice* device, const char** name);
444 
445 typedef int (*ANeuralNetworksDevice_getType_fn)(
446     const ANeuralNetworksDevice* device, int32_t* type);
447 
448 typedef int (*ANeuralNetworksDevice_getVersion_fn)(
449     const ANeuralNetworksDevice* device, const char** version);
450 
451 typedef int (*ANeuralNetworksDevice_getFeatureLevel_fn)(
452     const ANeuralNetworksDevice* device, int64_t* featureLevel);
453 
454 typedef int (*ANeuralNetworksModel_getSupportedOperationsForDevices_fn)(
455     const ANeuralNetworksModel* model,
456     const ANeuralNetworksDevice* const* devices, uint32_t numDevices,
457     bool* supportedOps);
458 
459 typedef int (*ANeuralNetworksCompilation_createForDevices_fn)(
460     ANeuralNetworksModel* model, const ANeuralNetworksDevice* const* devices,
461     uint32_t numDevices, ANeuralNetworksCompilation** compilation);
462 
463 typedef int (*ANeuralNetworksCompilation_setCaching_fn)(
464     ANeuralNetworksCompilation* compilation, const char* cacheDir,
465     const uint8_t* token);
466 
467 typedef int (*ANeuralNetworksExecution_compute_fn)(
468     ANeuralNetworksExecution* execution);
469 
470 typedef int (*ANeuralNetworksExecution_getOutputOperandRank_fn)(
471     ANeuralNetworksExecution* execution, int32_t index, uint32_t* rank);
472 
473 typedef int (*ANeuralNetworksExecution_getOutputOperandDimensions_fn)(
474     ANeuralNetworksExecution* execution, int32_t index, uint32_t* dimensions);
475 
476 typedef int (*ANeuralNetworksBurst_create_fn)(
477     ANeuralNetworksCompilation* compilation, ANeuralNetworksBurst** burst);
478 
479 typedef void (*ANeuralNetworksBurst_free_fn)(ANeuralNetworksBurst* burst);
480 
481 typedef int (*ANeuralNetworksExecution_burstCompute_fn)(
482     ANeuralNetworksExecution* execution, ANeuralNetworksBurst* burst);
483 
484 typedef int (*ANeuralNetworksMemory_createFromAHardwareBuffer_fn)(
485     const AHardwareBuffer* ahwb, ANeuralNetworksMemory** memory);
486 
487 typedef int (*ANeuralNetworksExecution_setMeasureTiming_fn)(
488     ANeuralNetworksExecution* execution, bool measure);
489 
490 typedef enum {
491   // Execution time on hardware (not driver, which runs on host processor).
492   ANEURALNETWORKS_DURATION_ON_HARDWARE = 0,
493   // Execution time in driver (including time on hardware).  Excludes overhead
494   // such as that of the runtime itself and the IPC needed for the runtime to
495   // communicate with the driver.
496   ANEURALNETWORKS_DURATION_IN_DRIVER = 1,
497 } DurationCode;
498 
499 typedef int (*ANeuralNetworksExecution_getDuration_fn)(
500     const ANeuralNetworksExecution* execution, int32_t durationCode,
501     uint64_t* duration);
502 
503 #endif  // TENSORFLOW_LITE_NNAPI_NEURALNETWORKSTYPES_H_
504