OSDN Git Service

a6453934cca7b9f59c41bb048d922dcd18e88f9f
[android-x86/hardware-interfaces.git] / neuralnetworks / 1.0 / types.hal
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package android.hardware.neuralnetworks@1.0;
18
19 /**
20  * Operand types.
21  *
22  * The type of an operand in a model.
23  *
24  * Types prefaced with TENSOR_* must be used for tensor data (i.e., tensors
25  * with at least one dimension). Types not prefaced by TENSOR_* represent
26  * scalar values and must have no dimensions.
27  */
28 enum OperandType : int32_t {
29     /**
30      * The following entries are used to declare scalars.
31      */
32     FLOAT32             = 0,
33     INT32               = 1,
34     UINT32              = 2,
35
36     /**
37      * The following entries are used to declare tensors.
38      */
39     TENSOR_FLOAT32      = 3,
40     TENSOR_INT32        = 4,
41
42     /**
43      * A tensor of 8 bit integers that represent real numbers.
44      *
45      * Attached to this tensor are two numbers that can be used to convert the
46      * 8 bit integer to the real value and vice versa. These two numbers are:
47      * - scale: a 32 bit floating point value
48      * - zero_value: a 32 bit integer
49      *
50      * The formula is:
51      * real_value = (integer_value - zero_value) * scale.
52      */
53     TENSOR_QUANT8_ASYMM = 5,
54
55     /**
56      * The following entries are OEM specific operand types.
57      */
58     OEM                 = 10000,
59     TENSOR_OEM_BYTE     = 10001,
60 };
61
62 /**
63  * Operation types.
64  *
65  * The type of an operation in a model.
66  */
67 enum OperationType : int32_t {
68     /**
69      * Adds two tensors, elment-wise.
70      *
71      * Takes two input tensors of identical type and compatible dimensions.  The output
72      * is the sum of both input tensors, optionally modified by an activation function.
73      *
74      * Two dimensions are compatible when:
75      *     1. they are equal, or
76      *     2. one of them is 1
77      *
78      * The size of the output is the maximum size along each dimension of the input operands.
79      * It starts with the trailing dimensions, and works its way forward.
80      *
81      * Example:
82      *     input1.dimension =    {4, 1, 2}
83      *     input2.dimension = {5, 4, 3, 1}
84      *     output.dimension = {5, 4, 3, 2}
85      *
86      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
87      * Supported tensor rank: up to 4
88      *
89      * Inputs:
90      * 0: A tensor.
91      * 1: A tensor of the same type, and compatible dimensions as input0.
92      * 2: An INT32 value, and has to be one of the {@link FusedActivationFunc} values.
93      *    Specifies the activation to invoke on the result of each addition.
94      *
95      * Ouputs:
96      * 0: The sum, a tensor of the same type as input0.
97      */
98     ADD = 0,
99
100     /**
101      * Performs a 2-D average pooling operation.
102      *
103      * The output dimensions are functions of the filter dimensions, stride, and padding.
104      *
105      * The values in output Tensor is computed as:
106      *     output[batch, row, col, channel] =
107      *         sum_{i, j}(input[batch, row + i, col + j, channel]) / sum(1)
108      *
109      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
110      *                         {@link OperandType::TENSOR_QUANT8_ASYMM}
111      * Supported tensor rank: 4, with "NHWC" data layout.
112      *
113      * Inputs:
114      * 0: A 4-D tensor, of shape [batches, height, width, depth], specifying the input.
115      * 1: An INT32 value, specifying the padding on the left, in the ‘width’ dimension.
116      * 2: An INT32 value, specifying the padding on the right,in the ‘width’ dimension.
117      * 3: An INT32 value, specifying the padding on the top, in the ‘height’ dimension.
118      * 4: An INT32 value, specifying the padding on the bottom, in the ‘height’ dimension.
119      * 5: An INT32 value, specifying the output stride in the ‘width’ dimension.
120      * 6: An INT32 value, specifying the output stride in the ‘height’ dimension.
121      * 7: An INT32 value, specifying the filter width.
122      * 8: An INT32 value, specifying the filter height.
123      * 9: An INT32 value, and has to be one of the {@link FusedActivationFunc} values.
124      *    Specifies the activation to invoke on the result of each addition.
125      *
126      * Ouputs:
127      * 0: The output 4-D tensor, of shape [batches, out_height, out_width, depth].
128      */
129     AVERAGE_POOL_2D = 1,
130
131     /**
132      * Concatenates the input tensors along the given dimension.
133      *
134      * The input tensors must have identical type and the same dimensions except the
135      * dimension along the concatenation axis.
136      *
137      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
138      *                         {@link OperandType::TENSOR_QUANT8_ASYMM}
139      * Supported tensor rank: up to 4
140      *
141      * Inputs:
142      * 0 ~ n: The list on n input tensors, of shape [D0, D1, ..., Daxis(i), ..., Dm]
143      * n+1: An INT32 value, specifying the concatenation axis.
144      * n+2: An INT32 value, and has to be one of the {@link FusedActivationFunc} values.
145      *    Specifies the activation to invoke on the result of each addition.
146      *
147      * Ouputs:
148      * 0: The output, a tensor of the same type as the input tensors.
149           The output shape is [D0, D1, ..., sum(Daxis(i)), ..., Dm].
150      */
151     CONCATENATION = 2,
152
153     /**
154      * Performs an 2-D convolution operation.
155      *
156      * The CONV_2D op sweeps a 2-D filter that can mix channels together over a batch of
157      * images, applying the filter to each window of each image of the appropriate size.
158      *
159      * The output dimensions are functions of the filter dimensions, stride, and padding.
160      *
161      * The values in output Tensor is computed as:
162      *     output[batch, row, col, channel] =
163      *         sum_{i, j} (
164      *             input[batch, row + i, col + j, k] *
165      *             filter[channel, row + i, col + j, k] +
166      *             bias[channel]
167      *         )
168      *
169      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
170      *                         {@link OperandType::TENSOR_QUANT8_ASYMM}
171      * Supported tensor rank: 4, with "NHWC" data layout.
172      *
173      * Inputs:
174      * 0: A 4-D tensor, of shape [batches, height, width, depth_in], specifying the input.
175      * 1: A 4-D tensor, of shape [depth_out, filter_height, filter_width, depth_in],
176      *    specifying the filter.
177      * 2: A 1-D tensor, of shape [depth_out], specifying the bias.
178      *    For input tensor of {@link OperandType::TENSOR_FLOAT32} type, the bias should
179      *    also be of {@link OperandType::TENSOR_FLOAT32}.
180      *    For input tensor of {@link OperandType::TENSOR_QUANT8_ASYMM} type, the bias
181      *    should be of {@link OperandType::TENSOR_INT32}.
182      * 3: An INT32 value, specifying the padding on the left, in the ‘width’ dimension.
183      * 4: An INT32 value, specifying the padding on the right,in the ‘width’ dimension.
184      * 5: An INT32 value, specifying the padding on the top, in the ‘height’ dimension.
185      * 6: An INT32 value, specifying the padding on the bottom, in the ‘height’ dimension.
186      * 7: An INT32 value, specifying the output stride in the ‘width’ dimension.
187      * 8: An INT32 value, specifying the output stride in the ‘height’ dimension.
188      * 9: An INT32 value, and has to be one of the {@link FusedActivationFunc} values.
189      *    Specifies the activation to invoke on the result of each addition.
190      *
191      * Ouputs:
192      * 0: The output 4-D tensor, of shape [batches, out_height, out_width, depth_out].
193      */
194     CONV_2D = 3,
195
196     /**
197      * Performs an depthwise 2-D convolution operation.
198      *
199      * Given an input tensor of shape [batches, height, width, depth_in] and a filter
200      * tensor of shape [depth_out, filter_height, filter_width, depth_in] containing
201      * in_channels convolutional filters of depth 1, DEPTHWISE_CONV applies a different
202      * filter to each input channel (expanding from 1 channel to channel_multiplier channels
203      * for each), then concatenates the results together.
204      *
205      * The output has depth_out = depth_in * depth_multiplier channels.
206      * The output dimensions are functions of the filter dimensions, stride, and padding.
207      *
208      * The values in output Tensor is computed as:
209      *     output[b, i, j, k * channel_multiplier + q] =
210      *         sum_{di, dj} (
211      *             input[b, strides[1] * i + di, strides[2] * j + dj, k] *
212      *             filter[di, dj, k, q]
213      *         )
214      *
215      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
216      *                         {@link OperandType::TENSOR_QUANT8_ASYMM}
217      * Supported tensor rank: 4, with "NHWC" data layout.
218      *
219      * Inputs:
220      * 0: A 4-D tensor, of shape [batches, height, width, depth_in], specifying the input.
221      * 1: A 4-D tensor, of shape [1, filter_height, filter_width, depth_out],
222      *    specifying the filter.
223      * 2: A 1-D tensor, of shape [depth_out], specifying the bias.
224      *    For input tensor of {@link OperandType::TENSOR_FLOAT32} type, the bias should
225      *    also be of {@link OperandType::TENSOR_FLOAT32}.
226      *    For input tensor of {@link OperandType::TENSOR_QUANT8_ASYMM} type, the bias
227      *    should be of {@link OperandType::TENSOR_INT32}.
228      * 3: An INT32 value, specifying the padding on the left, in the ‘width’ dimension.
229      * 4: An INT32 value, specifying the padding on the right,in the ‘width’ dimension.
230      * 5: An INT32 value, specifying the padding on the top, in the ‘height’ dimension.
231      * 6: An INT32 value, specifying the padding on the bottom, in the ‘height’ dimension.
232      * 7: An INT32 value, specifying the output stride in the ‘width’ dimension.
233      * 8: An INT32 value, specifying the output stride in the ‘height’ dimension.
234      * 9: An INT32 value, specifying the depthwise multiplier.
235      * 10: An INT32 value, and has to be one of the {@link FusedActivationFunc} values.
236      *    Specifies the activation to invoke on the result of each addition.
237      *
238      * Ouputs:
239      * 0: The output 4-D tensor, of shape [batches, out_height, out_width, depth_out].
240      */
241     DEPTHWISE_CONV_2D = 4,
242
243     /**
244      * Rearranges data from depth into blocks of spatial data.
245      *
246      * More specifically, this op outputs a copy of the input tensor where values from
247      * the depth dimension are moved in spatial blocks to the height and width dimensions.
248      * The value block_size indicates the input block size and how the data is moved.
249      *
250      * Chunks of data of size block_size * block_size from depth are rearranged into
251      * non-overlapping blocks of size block_size x block_size.
252      *
253      * The width of the output tensor is input_depth * block_size, whereas the height is
254      * input_height * block_size.
255      * The depth of the input tensor must be divisible by block_size * block_size
256      *
257      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
258      *                         {@link OperandType::TENSOR_QUANT8_ASYMM}
259      * Supported tensor rank: 4, with "NHWC" data layout.
260      *
261      * Inputs:
262      * 0: A 4-D tensor, of shape [batches, height, width, depth_in], specifying the input.
263      * 1: An INT32 value, specifying the block_size. block_size must be >=1 and
264      *    block_size * block_size must be a divisor of the input depth.
265      *
266      * Ouputs:
267      * 0: The output 4-D tensor, of shape [batch, height*block_size, width*block_size,
268      *    depth/(block_size*block_size)].
269      */
270     DEPTH_TO_SPACE = 5,
271
272     /**
273      * Dequantizes the input tensor.
274      *
275      * The formula is:
276      *     output = (input - zero_value) * scale.
277      *
278      * Supported tensor types: {@link OperandType::TENSOR_QUANT8_ASYMM}
279      * Supported tensor rank: up to 4
280      *
281      * Inputs:
282      * 0: A tensor of type {@link OperandType::TENSOR_QUANT8_ASYMM}.
283      *
284      * Ouputs:
285      * 0: The output tensor of same shape as input0, but with type
286           {@link OperandType::TENSOR_FLOAT32}.
287      */
288     DEQUANTIZE = 6,
289
290     /**
291      * Looks up items from a given tensor.
292      *
293      * Each item in the output is a raw copy of the corresponding item in
294      * the input “values”. If the the given “lookup” indices are out of bounds,
295      * the op will fail and an error will be reported.
296      *
297      * Inputs:
298      * * 0: Values. An n-D tensor of any type X (where n >= 2). E.g., if n is 2,
299      *      then the shape would be [lookup_dimension, values_dimension], where
300      *      “lookup_dimension” corresponds to the indexing dimension in the lookup
301      *      table, and “values_dimension” to the contents.
302      * * 1: Lookups. An 1-D tensor of type T, of shape [lookup_size], where
303      *      “lookup_size” is the number of elements to look for, and each entry
304      *      corresponds to the first dimension of the “values” tensor.
305      *
306      * Output:
307      * * 0: A n-D tensor of type X and the same rank and shape as the “values”
308      *      tensor, except for the first dimension which has size “lookup_size”.
309      */
310     EMBEDDING_LOOKUP = 7,
311
312     /**
313      * Computes element-wise floor() on the input tensor.
314      *
315      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
316      * Supported tensor rank: up to 4
317      *
318      * Inputs:
319      * 0: A tensor.
320      *
321      * Ouputs:
322      * 0: The output, a tensor of the same type and dimensions as input0.
323      */
324     FLOOR = 8,
325
326     /**
327      * Denotes a fully (densely) connected layer, which connects all elements in the input
328      * tensor with each element in the output tensor.
329      *
330      * This layer implements the operation:
331      *     outputs = activation(inputs * weights’ + bias)
332      *
333      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
334      *                         {@link OperandType::TENSOR_QUANT8_ASYMM}
335      * Supported tensor rank: up to 4.
336      *
337      * Inputs:
338      * 0: A tensor, specifying the input. If rank is greater than 2, then it gets flattened to
339      *    a 2-D Tensor. The 2-D Tensor is handled as if dimensions corresponded to shape
340      *    [batch_size, input_size], where “batch_size” corresponds to the batching dimension,
341      *    and “input_size” is the size of the input.
342      * 1: A 2-D tensor, specifying the weights, of shape [num_units, input_size], where “num_units”
343      *    corresponds to the number of output nodes.
344      * 2: A 1-D tensor, of shape [num_units], specifying the bias.
345      *    For input tensor of {@link OperandType::TENSOR_FLOAT32} type, the bias should
346      *    also be of {@link OperandType::TENSOR_FLOAT32}.
347      *    For input tensor of {@link OperandType::TENSOR_QUANT8_ASYMM} type, the bias
348      *    should be of {@link OperandType::TENSOR_INT32}.
349      * 3: An INT32 value, and has to be one of the {@link FusedActivationFunc} values.
350      *    Specifies the activation to invoke on the result of each addition.
351      *
352      * Ouputs:
353      * 0: The output tensor, of shape [batch_size, num_units].
354      */
355     FULLY_CONNECTED = 9,
356
357     /**
358      * Looks up values of a hash table with given keys.
359      *
360      * Inputs:
361      * * 0: Lookups. A 1-D int32 tensor with shape [ k ].
362      * * 1: Keys. A 1-D int32 tensor with shape [ n ], *MUST* be sorted in
363      *      ascending order.
364      * * 2: Values. A tensor with shape [ n … ].
365      *
366      * Outputs:
367      * * 0: Output. A tensor with shape [ k …].
368      * * 1: Hits. A uint8 tensor with shape [ k ] indicates whether the lookup
369      *      hits or not.
370      */
371     HASHTABLE_LOOKUP = 10,
372
373     /**
374      * Applies L2 normalization along a the depth dimension.
375      *
376      * The values in output Tensor is computed as:
377      *     output[batch, row, col, channel] =
378      *         input[batch, row, col, channel] /
379      *         sqrt(sum_{c} pow(input[batch, row, col, c], 2))
380      *
381      * For x with more dimensions, independently normalizes each 1-D slice along dimension dim.
382      *
383      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
384      * Supported tensor rank: 4, with "NHWC" data layout.
385      *
386      * Inputs:
387      * 0: A 4-D tensor, of shape [batches, height, width, depth], specifying the input.
388      *
389      * Ouputs:
390      * 0: The output 4-D tensor, of shape [batches, out_height, out_width, depth].
391      */
392     L2_NORMALIZATION = 11,
393
394     /**
395      * Performs an 2-D L2 pooling operation.
396      *
397      * The output dimensions are functions of the filter dimensions, stride, and padding.
398      *
399      * The values in output Tensor is computed as:
400      *     output[batch, row, col, channel] =
401      *         sqrt(sum_{i, j} pow(input[batch, row + i, col + j, channel], 2) / sum(1))
402      *
403      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
404      * Supported tensor rank: 4, with "NHWC" data layout.
405      *
406      * Inputs:
407      * 0: A 4-D tensor, of shape [batches, height, width, depth], specifying the input.
408      * 1: An INT32 value, specifying the padding on the left, in the ‘width’ dimension.
409      * 2: An INT32 value, specifying the padding on the right,in the ‘width’ dimension.
410      * 3: An INT32 value, specifying the padding on the top, in the ‘height’ dimension.
411      * 4: An INT32 value, specifying the padding on the bottom, in the ‘height’ dimension.
412      * 5: An INT32 value, specifying the output stride in the ‘width’ dimension.
413      * 6: An INT32 value, specifying the output stride in the ‘height’ dimension.
414      * 7: An INT32 value, specifying the filter width.
415      * 8: An INT32 value, specifying the filter height.
416      * 9: An INT32 value, and has to be one of the {@link FusedActivationFunc} values.
417      *    Specifies the activation to invoke on the result of each addition.
418      *
419      * Ouputs:
420      * 0: The output 4-D tensor, of shape [batches, out_height, out_width, depth].
421      */
422     L2_POOL_2D = 12,
423
424     /**
425      * Applies Local Response Normalization along the depth dimension.
426      *
427      * The 4-D input tensor is treated as a 3-D array of 1-D vectors (along the last
428      * dimension), and each vector is normalized independently. Within a given vector,
429      * each component is divided by the weighted, squared sum of inputs within depth_radius.
430      *
431      * In details:
432      *     sqr_sum[a, b, c, d] =
433      *         sum(pow(input[a, b, c, d - depth_radius : d + depth_radius + 1], 2)
434      *     output = input / pow((bias + alpha * sqr_sum), beta)
435      *
436      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
437      * Supported tensor rank: 4, with "NHWC" data layout.
438      *
439      * Inputs:
440      * 0: A 4-D tensor, of shape [batches, height, width, depth], specifying the input.
441      * 1: An INT32 value, specifying the radius of the normalization window.
442      * 2: A FLOAT32 value, specifying the bias, must not be zero.
443      * 3: A FLOAT32 value, specifying the scale factor, alpha.
444      * 4: A FLOAT32 value, specifying the exponent, beta.
445      *
446      * Ouputs:
447      * 0: The output tensor of same shape as input0.
448      */
449     LOCAL_RESPONSE_NORMALIZATION = 13,
450
451     /**
452      * Computes sigmoid activation on the input tensor element-wise.
453      *
454      * In details:
455      *     output = 1 / (1 + exp(-input))
456      *
457      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
458      *                         {@link OperandType::TENSOR_QUANT8_ASYMM}
459      * Supported tensor rank: up to 4.
460      *
461      * Inputs:
462      * 0: A tensor, specifying the input.
463      *
464      * Ouputs:
465      * 0: The output tensor of same shape as input0.
466      */
467     LOGISTIC = 14,
468
469     /**
470      * Projects an input to a bit vector via locality senstive hashing.
471      *
472      * Inputs:
473      * * 0: Hash functions. Dim.size == 2, DataType: Float.
474      *            Tensor[0].Dim[0]: Number of hash functions.
475      *            Tensor[0].Dim[1]: Number of seeds per hash functions.
476      *            Tensor[0].Dim[1] <= 32 in sparse case.
477      *
478      * * 1: Input. Dim.size >= 1, no restriction on DataType.
479      * * 2: Weight. Optional. Dim.size == 1, DataType: Float.
480      *     If not set, each input element is considered to have the same weight of
481      *     1.0.
482      *     Tensor[1].Dim[0] == Tensor[2].Dim[0]
483      * * 3: Type:
484      *        Sparse: Value LSHProjectionType_SPARSE(=1).
485      *          Computed bit vector is considered to be sparse.
486      *          Each output element is an int32 made up of multiple bits computed from
487      *          hash functions.
488      *
489      *        Dense: Value LSHProjectionType_DENSE(=2).
490      *          Computed bit vector is considered to be dense. Each output element
491      *          represents a bit and can take the value of either 0 or 1.
492      *
493      * Outputs:
494      * * 0: If the projection type is sparse:
495      *        Output.Dim == { Tensor[0].Dim[0] }
496      *        A tensor of int32 that represents hash signatures.
497      *      If the projection type is Dense:
498      *        Output.Dim == { Tensor[0].Dim[0] * Tensor[0].Dim[1] }
499      *        A flattened tensor that represents projected bit vectors.
500      */
501     LSH_PROJECTION = 15,
502
503     /**
504      * Long short-term memory unit (LSTM) recurrent network layer.
505      *
506      * The default non-peephole implementation is based on:
507      * http://deeplearning.cs.cmu.edu/pdfs/Hochreiter97_lstm.pdf
508      * S. Hochreiter and J. Schmidhuber. "Long Short-Term Memory". Neural
509      * Computation, 9(8):1735-1780, 1997.
510      *
511      * The peephole implementation is based on:
512      * https://research.google.com/pubs/archive/43905.pdf
513      * Hasim Sak, Andrew Senior, and Francoise Beaufays. "Long short-term memory
514      * recurrent neural network architectures for large scale acoustic modeling."
515      * INTERSPEECH, 2014.
516      *
517      * The coupling of input and forget gate (CIFG) is based on:
518      * http://arxiv.org/pdf/1503.04069.pdf
519      * Greff et al. "LSTM: A Search Space Odyssey"
520      *
521      * The class has the following independently optional inputs:
522      * * If input gate (if CIFG): “input_to_forget_weights”,
523      *   “recurrent_to_input_weights”, “cell_to_input_weights”, “input_gate_bias”.
524      * * If no peephole connections: “cell_to_input_weights”,
525      *   “cell_to_forget_weights”, “cell_to_output_weights”.
526      * * If no projection layer: “projection_weights” and “projection_bias”.
527      * * If no projection bias: “projection_bias”.
528      *
529      * Supported tensor types:
530      * * {@link OperandType::TENSOR_FLOAT32}
531      *
532      * Inputs:
533      * * 0: Input.
534      *      A 2-D tensor of type T, of shape [batch_size, input_size], where
535      *      “batch_size” corresponds to the batching dimension, and “input_size”
536      *      is the size of the input.
537      * * 1: input_to_input_weights.
538      *      A 2-D tensor of type T, of shape [num_units, input_size], where
539      *      “num_units” corresponds to the number of cell units.
540      * * 2: input_to_forget_weights.
541      *      A 2-D tensor of type T, of shape [num_units, input_size].
542      * * 3: input_to_cell_weights.
543      *      A 2-D tensor of type T, of shape [num_units, input_size].
544      * * 4: input_to_output_weights.
545      *      A 2-D tensor of type T, of shape [num_units, input_size].
546      * * 5: recurrent_to_input_weights.
547      *      A 2-D tensor of type T, of shape [num_units, output_size], where
548      *      “output_size” corresponds to either the number of cell units (i.e.,
549      *      “num_units”), or the second dimension of the “projection_weights”, if
550      *      defined.
551      * * 6: recurrent_to_forget_weights.
552      *      A 2-D tensor of type T, of shape [num_units, output_size].
553      * * 7: recurrent_to_cell_weights.
554      *      A 2-D tensor of type T, of shape [num_units, output_size].
555      * * 8: recurrent_to_output_weights.
556      *      A 2-D tensor of type T, of shape [num_units, output_size].
557      * * 9: cell_to_input_weights.
558      *      A 1-D tensor of type T, of shape [num_units].
559      * * 10:cell_to_forget_weights.
560      *      A 1-D tensor of type T, of shape [num_units].
561      * * 11:cell_to_output_weights.
562      *      A 1-D tensor of type T, of shape [num_units].
563      * * 12:input_gate_bias.
564      *      A 1-D tensor of type T, of shape [num_units].
565      * * 13:forget_gate_bias.
566      *      A 1-D tensor of type T, of shape [num_units].
567      * * 14:cell_bias.
568      *      A 1-D tensor of type T, of shape [num_units].
569      * * 15:output_gate_bias.
570      *      A 1-D tensor of type T, of shape [num_units].
571      * * 16:projection_weights.
572      *      A 2-D tensor of type T, of shape [output_size, num_units].
573      * * 17:projection_bias.
574      *      A 1-D tensor of type T, of shape [output_size].
575      *
576      * Parameters:
577      * * 18:fused_activation_function.
578      *      An (optional) ActivationFunctionType indicating the activation
579      *      function.
580      *      If “NONE” is specified then it results in a linear activation.
581      * * 19:cell_clip.
582      *      A clipping threshold for the cell state, such that values are bound
583      *      within [-cell_clip, cell_clip]. If set to 0.0 then clipping is
584      *      disabled.
585      * * 20:proj_clip.
586      *      A clipping threshold for the output from the projection layer, such
587      *      that values are bound within [-proj_clip, proj_clip]. If set to 0.0
588      *      then clipping is disabled.
589      *
590      * Outputs:
591      * * 0: scratch_buffer.
592      *      A 3-D tensor of type T, of shape [batch_size, num_cell, 4].
593      * * 1: output_state.
594      *      A 2-D tensor of type T, of shape [batch_size, output_size].
595      * * 2: cell_state.
596      *      A 2-D tensor of type T, of shape [batch_size, num_units].
597      * * 3: output.
598      *      A 2-D tensor of type T, of shape [batch_size, output_size]. This is
599      *      effectively the same as the current “output_state” value.
600      */
601     LSTM = 16,
602
603     /**
604      * Performs an 2-D max pooling operation.
605      *
606      * The output dimensions are functions of the filter dimensions, stride, and padding.
607      *
608      * The values in output Tensor is computed as:
609      *     output[batch, row, col, channel] =
610      *         max_{i, j} (input[batch, row + i, col + j, channel])
611      *
612      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
613      *                         {@link OperandType::TENSOR_QUANT8_ASYMM}
614      * Supported tensor rank: 4, with "NHWC" data layout.
615      *
616      * Inputs:
617      * 0: A 4-D tensor, of shape [batches, height, width, depth], specifying the input.
618      * 1: An INT32 value, specifying the padding on the left, in the ‘width’ dimension.
619      * 2: An INT32 value, specifying the padding on the right,in the ‘width’ dimension.
620      * 3: An INT32 value, specifying the padding on the top, in the ‘height’ dimension.
621      * 4: An INT32 value, specifying the padding on the bottom, in the ‘height’ dimension.
622      * 5: An INT32 value, specifying the output stride in the ‘width’ dimension.
623      * 6: An INT32 value, specifying the output stride in the ‘height’ dimension.
624      * 7: An INT32 value, specifying the filter width.
625      * 8: An INT32 value, specifying the filter height.
626      * 9: An INT32 value, and has to be one of the {@link FusedActivationFunc} values.
627      *    Specifies the activation to invoke on the result of each addition.
628      *
629      * Ouputs:
630      * 0: The output 4-D tensor, of shape [batches, out_height, out_width, depth].
631      */
632     MAX_POOL_2D = 17,
633
634     /**
635      * Multiplies two tensors, elment-wise.
636      *
637      * Takes two input tensors of identical type and compatible dimensions.  The output
638      * is the product of both input tensors, optionally modified by an activation function.
639      *
640      * Two dimensions are compatible when:
641      *     1. they are equal, or
642      *     2. one of them is 1
643      *
644      * The size of the resulting output is the maximum size along each dimension of the
645      * input operands. It starts with the trailing dimensions, and works its way forward.
646      *
647      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
648      * Supported tensor rank: up to 4
649      *
650      * Inputs:
651      * 0: A tensor.
652      * 1: A tensor of the same type, and compatible dimensions as input0.
653      * 2: An INT32 value, and has to be one of the {@link FusedActivationFunc} values.
654      *    Specifies the activation to invoke on the result of each addition.
655      *
656      * Ouputs:
657      * 0: The product, a tensor of the same type as input0.
658      */
659     MUL = 18,
660
661     /**
662      * Computes rectified linear activation on the input tensor element-wise.
663      *
664      * In details:
665      *     output = max(0, input)
666      *
667      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
668      *                         {@link OperandType::TENSOR_QUANT8_ASYMM}
669      * Supported tensor rank: up to 4.
670      *
671      * Inputs:
672      * 0: A tensor, specifying the input.
673      *
674      * Ouputs:
675      * 0: The output tensor of same shape as input0.
676      */
677     RELU = 19,
678
679     /**
680      * Computes rectified linear 1 activation on the input tensor element-wise.
681      *
682      * In details:
683      *     output = min(1.f, max(-1.f, input))
684      *
685      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
686      *                         {@link OperandType::TENSOR_QUANT8_ASYMM}
687      * Supported tensor rank: up to 4.
688      *
689      * Inputs:
690      * 0: A tensor, specifying the input.
691      *
692      * Ouputs:
693      * 0: The output tensor of same shape as input0.
694      */
695     RELU1 = 20,
696
697     /**
698      * Computes rectified linear 6 activation on the input tensor element-wise.
699      *
700      * In details:
701      *     output = min(6, max(0, input))
702      *
703      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
704      *                         {@link OperandType::TENSOR_QUANT8_ASYMM}
705      * Supported tensor rank: up to 4.
706      *
707      * Inputs:
708      * 0: A tensor, specifying the input.
709      *
710      * Ouputs:
711      * 0: The output tensor of same shape as input0.
712      */
713     RELU6 = 21,
714
715     /**
716      * Reshapes a tensor.
717      *
718      * Given tensor, this operation returns a tensor that has the same values as tensor,
719      * but with a newly specified shape.
720      *
721      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
722      *                         {@link OperandType::TENSOR_QUANT8_ASYMM}
723      * Supported tensor rank: up to 4.
724      *
725      * Inputs:
726      * 0: A tensor, specifying the tensor to be reshaped.
727      * 1: A 1-D tensor of type {@link OperandType::TENSOR_INT32}, defining the shape
728      *    of the output tensor. The number of elements implied by shape must be the same
729      *    as the number of elements in the input tensor.
730      *
731      * Ouputs:
732      * 0: The output tensor, of shape specified by the input shape.
733      */
734     RESHAPE = 22,
735
736     /**
737      * Resizes images to given size using the bilinear interpretation.
738      *
739      * Resized images will be distorted if their original aspect ratio is not the same as input.
740      *
741      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
742      * Supported tensor rank: 4, with "NHWC" data layout.
743      *
744      * Inputs:
745      * 0: A 4-D tensor, of shape [batches, height, width, depth], specifying the input.
746      * 1: An INT32 value, specifying the output width of the output tensor.
747      * 2: An INT32 value, specifying the output height of the output tensor.
748      *
749      * Ouputs:
750      * 0: The output 4-D tensor, of shape [batches, new_height, new_width, depth].
751      */
752     RESIZE_BILINEAR = 23,
753
754     /**
755      * A basic recurrent neural network layer.
756      *
757      * This layer implements the operation:
758      * outputs = state = activation(inputs * input_weights + state * recurrent_weights + bias)
759      *
760      * Where:
761      * * “input_weights” is a weight matrix that multiplies the inputs;
762      * * “recurrent_weights” is a weight matrix that multiplies the current
763      *    “state” which itself is the output from the previous time step
764      *    computation;
765      * * “bias” is a bias vector (added to each output vector in the batch);
766      * * “activation” is the function passed as the “fused_activation_function”
767      *   argument (if not “NONE”).
768      *
769      * Supported tensor types:
770      * * {@link OperandType::TENSOR_FLOAT32}
771      *
772      * Inputs:
773      * * 0: input.
774      *      A 2-D tensor of type T, of shape [batch_size, input_size], where
775      *      “batch_size” corresponds to the batching dimension, and “input_size” is
776      *      the size of the input.
777      * * 1: weights.
778      *      A 2-D tensor of type T, of shape [num_units, input_size], where
779      *      “num_units” corresponds to the number of units.
780      * * 2: recurrent_weights.
781      *      A 2-D tensor of type T, of shape [num_units, num_units], with columns
782      *      corresponding to the weights from each unit.
783      * * 3: bias.
784      *      A 1-D tensor of type T, of shape [num_units].
785      *
786      *    For FLOAT32 input tensor, bias must also be FLOAT32.
787      *    For UINT8 input tensor, bias must be INT32.
788      *
789      * Parameters
790      * * 4: fused_activation_function.
791      *      An (optional) ActivationFunctionType indicating the activation
792      *      function. If “NONE” is specified then it results in a linear
793      *      activation.
794      *
795      * * 5: Hidden state.
796      *      A 2-D tensor of type T, of shape [batch_size, num_units].
797      *
798      * Outputs:
799      * * 0: output.
800      *      A 2-D tensor of type T, of shape [batch_size, num_units]. This is
801      *      effectively the same as the current state value.
802      */
803     RNN = 24,
804
805     /**
806      * Computes the softmax activation on the input tensor element-wise, per batch, by
807      * normalizing the input vector so the maximum coefficient is zero.
808      *
809      * In details:
810      *     output[batch, i] =
811      *         exp((input[batch, i] - max(input[batch, :])) * beta) /
812      *         sum_{k}{exp((input[batch, k] - max(input[batch, :])) * beta)}
813      *
814      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
815      *                         {@link OperandType::TENSOR_QUANT8_ASYMM}
816      * Supported tensor rank: 2 or 4.
817      *
818      * Inputs:
819      * 0: A 2-D or 4-D tensor, specifying the tensor to be reshaped.
820      * 1: A FLOAT32 value, specifying the scaling factor for the exponent, beta.
821      *
822      * Ouputs:
823      * 0: The output tensor of same shape as input0.
824      */
825     SOFTMAX = 25,
826
827     /**
828      * Rearranges blocks of spatial data, into depth.
829      *
830      * More specifically, this op outputs a copy of the input tensor where values from
831      * the height and width dimensions are moved to the depth dimension.
832      * The value block_size indicates the input block size and how the data is moved.
833      *
834      * Chunks of data of size block_size * block_size from depth are rearranged into
835      * non-overlapping blocks of size block_size x block_size.
836      *
837      * The depth of the output tensor is input_depth * block_size * block_size.
838      * The input tensor's height and width must be divisible by block_size.
839      *
840      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
841      *                         {@link OperandType::TENSOR_QUANT8_ASYMM}
842      * Supported tensor rank: 4, with "NHWC" data layout.
843      *
844      * Inputs:
845      * 0: A 4-D tensor, of shape [batches, height, width, depth_in], specifying the input.
846      * 1: An INT32 value, specifying the block_size. block_size must be >=1 and
847      *    block_size must be a divisor of both the input height and width.
848      *
849      * Ouputs:
850      * 0: The output 4-D tensor, of shape [batch, height/block_size, width/block_size,
851      *    depth*block_size*block_size].
852      */
853     SPACE_TO_DEPTH = 26,
854
855     /**
856      * SVDF op is a kind of stateful layer derived from the notion that a
857      * densely connected layer that's processing a sequence of input frames can
858      * be approximated by using a singular value decomposition of each of its
859      * nodes. The implementation is based on:
860      *
861      * https://research.google.com/pubs/archive/43813.pdf
862      *
863      * P. Nakkiran, R. Alvarez, R. Prabhavalkar, C. Parada.
864      * “Compressing Deep Neural Networks using a Rank-Constrained Topology”.
865      * INTERSPEECH, 2015.
866      *
867      * It processes the incoming input using a 2-stage filtering mechanism:
868      * * stage 1 performs filtering on the "features" dimension, whose outputs get
869      *   pushed into a memory of fixed-size memory_size.
870      * * stage 2 performs filtering on the "time" dimension of the memory_size
871      *   memoized outputs of stage 1.
872      *
873      * Specifically, for rank 1, this layer implements the operation:
874      *
875      *    memory = push(conv1d(inputs, weights_feature, feature_dim, "VALID"));
876      *    outputs = activation(memory * weights_time + bias);
877      *
878      * Where:
879      * * “weights_feature” is a weights matrix that processes the inputs (by
880      *   convolving the input with every “feature filter”), and whose outputs get
881      *   pushed, stacked in order, into the fixed-size “memory” (the oldest entry
882      *   gets dropped);
883      * * “weights_time” is a weights matrix that processes the “memory” (by a
884      *   batched matrix multiplication on the num_units);
885      * * “bias” is an optional bias vector (added to each output vector in the
886      *   batch); and
887      * * “activation” is the function passed as the “fused_activation_function”
888      *   argument (if not “NONE”).
889      *
890      * Each rank adds a dimension to the weights matrices by means of stacking
891      * the filters.
892      *
893      * Supported tensor types:
894      * * {@link OperandType::TENSOR_FLOAT32}
895      *
896      * Inputs:
897      * * 0: input.
898      *      A 2-D tensor of type T, of shape [batch_size, input_size], where
899      *      “batch_size” corresponds to the batching dimension, and “input_size” is
900      *      the size of the input.
901      * * 1: weights_feature.
902      *      A 2-D tensor of type T, of shape [num_units, input_size], where
903      *      “num_units” corresponds to the number of units.
904      * * 2: weights_time.
905      *      A 2-D tensor of type T, of shape [num_units, memory_size], where
906      *      “memory_size” corresponds to the fixed-size of the memory.
907      * * 3: bias.
908      *      A optional 1-D tensor of type T, of shape [num_units].
909      *
910      *    For FLOAT32 input tensor, bias must also be FLOAT32.
911      *    For UINT8 input tensor, bias must be INT32.
912      *
913      * Parameters:
914      * * 4: rank.
915      *      The rank of the SVD approximation.
916      * * 5: fused_activation_function.
917      *      An (optional) ActivationFunctionType indicating the activation function.
918      *      If “NONE” is specified then it results in a linear activation.
919      *
920      * Outputs:
921      * * 0: state.
922      *      A 2-D tensor of type T, of shape [batch_size, (memory_size - 1) * num_units * rank].
923      * * 1: output.
924      *      A 2-D tensor of type T, of shape [batch_size, num_units].
925      */
926     SVDF = 27,
927
928     /**
929      * Computes hyperbolic tangent of input tensor element-wise.
930      *
931      * In details:
932      *     output = tanh(input)
933      *
934      * Supported tensor types: {@link OperandType::TENSOR_FLOAT32}
935      * Supported tensor rank: up to 4.
936      *
937      * Inputs:
938      * 0: A tensor, specifying the input.
939      *
940      * Ouputs:
941      * 0: The output tensor of same shape as input0.
942      */
943     TANH = 28,
944
945     /**
946      * OEM specific operation.
947      *
948      * This operation is OEM specific. It should only be used for OEM applications.
949      */
950     OEM_OPERATION = 10000,
951 };
952
953 /**
954  * Fused activation function types.
955  */
956 enum FusedActivationFunc : int32_t {
957     NONE  = 0,
958     RELU  = 1,
959     RELU1 = 2,
960     RELU6 = 3,
961 };
962
963 /**
964  * How an operand is used.
965  */
966 enum OperandLifeTime : int32_t {
967     /**
968      * The operand is internal to the model.  It's created by an operation
969      * and consumed by other operations.
970      */
971     TEMPORARY_VARIABLE,
972
973     /**
974      * The operand is an input of the model. An operand can't be both
975      * input and output of a model.
976      */
977     MODEL_INPUT,
978
979     /**
980      * The operand is an output of the model.
981      */
982     MODEL_OUTPUT,
983
984     /**
985      * The operand is a constant found in Model.operandValues.
986      */
987     CONSTANT_COPY,
988
989     /**
990      * The operand is a constant that was specified via a Memory object.
991      */
992     CONSTANT_REFERENCE,
993
994     /**
995      * The operand does not have a value. This is valid only for optional arguments
996      * of operations.
997      */
998     NO_VALUE,
999 };
1000
1001 /**
1002  * Status of a device.
1003  */
1004 enum DeviceStatus : int32_t {
1005     AVAILABLE,
1006     BUSY,
1007     OFFLINE,
1008     UNKNOWN,
1009 };
1010
1011 /**
1012  * Performance information for the reference workload.
1013  *
1014  * Used by a driver to report its performance characteristics.
1015  */
1016 struct PerformanceInfo {
1017     /**
1018      * Execution time in nanoseconds.
1019      */
1020     float execTime;
1021
1022     /**
1023      * Power usage in picoJoules.
1024      */
1025     float powerUsage;
1026 };
1027
1028 /**
1029  * The capabilities of a driver.
1030  */
1031 struct Capabilities {
1032     /**
1033      * Driver performance when operating on float32 data.
1034      */
1035     PerformanceInfo float32Performance;
1036
1037     /**
1038      * Driver performance when operating on asymmetric 8-bit quantized data.
1039      */
1040     PerformanceInfo quantized8Performance;
1041 };
1042
1043 /**
1044  * Describes the location of a data object.
1045  */
1046 struct DataLocation {
1047     /**
1048      * The index of the memory pool where this location is found.
1049      */
1050     uint32_t poolIndex;
1051
1052     /**
1053      * Offset in bytes from the start of the pool.
1054      */
1055     uint32_t offset;
1056
1057     /**
1058      * The length of the data in bytes.
1059      */
1060     uint32_t length;
1061 };
1062
1063 /**
1064  * Describes one operand of the model's graph.
1065  */
1066 struct Operand {
1067     /**
1068      * Data type of the operand.
1069      */
1070     OperandType type;
1071
1072     /**
1073      * Dimensions of the operand.
1074      */
1075     vec<uint32_t> dimensions;
1076
1077     /**
1078      * The number of operations that use this operand as input.
1079      */
1080     uint32_t numberOfConsumers;
1081
1082     /**
1083      * Quantized scale of the operand.
1084      *
1085      * Only applicable if the operand is of type TENSOR_QUANT8_ASYMM or
1086      * TENSOR_INT32.
1087      */
1088     float scale;
1089
1090     /**
1091      * Quantized zero-point offset of the operand.
1092      *
1093      * Only applicable if the operand is of type TENSOR_QUANT8_ASYMM.
1094      */
1095     int32_t zeroPoint;
1096
1097     /**
1098      * How the operand is used.
1099      */
1100     OperandLifeTime lifetime;
1101
1102     /**
1103      * Where to find the data for this operand.
1104      * If the lifetime is TEMPORARY_VARIABLE, MODEL_INPUT, MODEL_OUTPUT, or NO_VALUE:
1105      * - All the fields will be 0.
1106      * If the lifetime is CONSTANT_COPY:
1107      * - location.poolIndex is 0.
1108      * - location.offset is the offset in bytes into Model.operandValues.
1109      * - location.length is set.
1110      * If the lifetime is CONSTANT_REFERENCE:
1111      * - location.poolIndex is set.
1112      * - location.offset is the offset in bytes into the specified pool.
1113      * - location.length is set.
1114      */
1115     DataLocation location;
1116 };
1117
1118 /**
1119  * Describes one operation of the model's graph.
1120  */
1121 struct Operation {
1122     /**
1123      * The operation type.
1124      */
1125     OperationType type;
1126
1127     /**
1128      * Describes the table that contains the indexes of the inputs of the
1129      * operation. The offset is the index in the operandIndexes table.
1130      */
1131     vec<uint32_t> inputs;
1132
1133     /**
1134      * Describes the table that contains the indexes of the outputs of the
1135      * operation. The offset is the index in the operandIndexes table.
1136      */
1137     vec<uint32_t> outputs;
1138 };
1139
1140 /**
1141  * A Neural Network Model.
1142  *
1143  * This includes not only the execution graph, but also constant data such as
1144  * weights or scalars added at construction time. The only information that
1145  * might not be known is the shape of the input tensors.
1146  */
1147 struct Model {
1148     /**
1149      * All operands included in the model.
1150      */
1151     vec<Operand> operands;
1152
1153     /**
1154      * All operations included in the model.
1155      *
1156      * The operations are sorted into execution order.
1157      */
1158     vec<Operation> operations;
1159
1160     /**
1161      * Input indexes of the model.
1162      *
1163      * Each value corresponds to the index of the operand in "operands".
1164      */
1165     vec<uint32_t> inputIndexes;
1166
1167     /**
1168      * Output indexes of the model.
1169      *
1170      * Each value corresponds to the index of the operand in "operands".
1171      */
1172     vec<uint32_t> outputIndexes;
1173
1174     /**
1175      * A byte buffer containing operand data that were copied into the model.
1176      *
1177      * An operand's value must be located here if and only if Operand::lifetime
1178      * equals OperandLifeTime::CONSTANT_COPY.
1179      */
1180     vec<uint8_t> operandValues;
1181
1182     /**
1183      * A collection of shared memory pools containing operand data that were
1184      * registered by the model.
1185      *
1186      * An operand's value must be located here if and only if Operand::lifetime
1187      * equals OperandLifeTime::CONSTANT_REFERENCE.
1188      */
1189     vec<memory> pools;
1190 };
1191
1192 /**
1193  * Metadata information specifying the location of the input or output data and
1194  * any updates to the input or output operand.
1195  */
1196 struct RequestArgument {
1197     /**
1198      * If true, the argument does not have a value. This can be used for operations
1199      * that take optional arguments. If true, the fields of location are set to 0 and
1200      * the dimensions vector is left empty.
1201      */
1202     bool hasNoValue;
1203
1204     /**
1205      * The location within one of the memory pools passed in the Request.
1206      */
1207     DataLocation location;
1208
1209     /**
1210      * Updated dimension information.
1211      *
1212      * If dimensions.size() > 0, dimension information was provided along with the
1213      * argument.  This can be the case for models that accept inputs of varying size.
1214      * This can't change the rank, just the value of the dimensions that were
1215      * unspecified in the model.
1216      */
1217     vec<uint32_t> dimensions;
1218 };
1219
1220 /**
1221  * Inputs to be sent to and outputs to be retrieved from a prepared model.
1222  *
1223  * A Request serves two primary tasks:
1224  * 1) Provides the input and output data to be used when executing the model.
1225  * 2) Specifies any updates to the input operand metadata that were left
1226  *    unspecified at model preparation time.
1227  */
1228 struct Request {
1229     /**
1230      * Input data and information to be used in the execution of a prepared
1231      * model.
1232      *
1233      * The index of the input corresponds to the index in Model.inputIndexes.
1234      *   E.g., input[i] corresponds to Model.inputIndexes[i].
1235      */
1236     vec<RequestArgument> inputs;
1237
1238     /**
1239      * Output data and information to be used in the execution of a prepared
1240      * model.
1241      *
1242      * The index of the output corresponds to the index in Model.outputIndexes.
1243      *   E.g., output[i] corresponds to Model.outputIndexes[i].
1244      */
1245     vec<RequestArgument> outputs;
1246
1247     /**
1248      * A collection of shared memory pools containing operand data for both the
1249      * inputs and the outputs to a model.
1250      */
1251     vec<memory> pools;
1252 };
1253
1254 /**
1255  * Return status of a function.
1256  */
1257 enum ErrorStatus : int32_t {
1258     NONE,
1259     DEVICE_UNAVAILABLE,
1260     GENERAL_FAILURE,
1261     OUTPUT_INSUFFICIENT_SIZE,
1262     INVALID_ARGUMENT,
1263 };