• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Contains all the entry points to the C Neural Networks API.
18 // We do basic validation of the operands and then call the class
19 // that implements the functionality.
20 
21 #define LOG_TAG "NeuralNetworks"
22 
23 #include "NeuralNetworks.h"
24 
25 #include "BurstBuilder.h"
26 #include "Callbacks.h"
27 #include "CompilationBuilder.h"
28 #include "ExecutionBuilder.h"
29 #include "Manager.h"
30 #include "Memory.h"
31 #include "ModelBuilder.h"
32 #include "NeuralNetworksExtensions.h"
33 #include "NeuralNetworksOEM.h"
34 #include "Tracing.h"
35 #include "Utils.h"
36 
37 #include "vndk/hardware_buffer.h"
38 
39 #include <cstddef>
40 #include <memory>
41 #include <vector>
42 
43 // Make sure the constants defined in the header files have not changed values.
44 // IMPORTANT: When adding new values, update kNumberOfDataTypes or kNumberOfDataTypesOEM
45 // in Utils.h.
46 static_assert(ANEURALNETWORKS_FLOAT32 == 0, "ANEURALNETWORKS_FLOAT32 has changed");
47 static_assert(ANEURALNETWORKS_INT32 == 1, "ANEURALNETWORKS_INT32 has changed");
48 static_assert(ANEURALNETWORKS_UINT32 == 2, "ANEURALNETWORKS_UINT32 has changed");
49 static_assert(ANEURALNETWORKS_TENSOR_FLOAT32 == 3, "ANEURALNETWORKS_TENSOR_FLOAT32 has changed");
50 static_assert(ANEURALNETWORKS_TENSOR_INT32 == 4, "ANEURALNETWORKS_TENSOR_INT32 has changed");
51 static_assert(ANEURALNETWORKS_TENSOR_QUANT8_ASYMM == 5,
52               "ANEURALNETWORKS_TENSOR_QUANT8_ASYMM has changed");
53 static_assert(ANEURALNETWORKS_BOOL == 6, "ANEURALNETWORKS_BOOL has changed");
54 static_assert(ANEURALNETWORKS_TENSOR_QUANT16_SYMM == 7,
55               "ANEURALNETWORKS_TENSOR_QUANT16_SYMM has changed");
56 static_assert(ANEURALNETWORKS_TENSOR_FLOAT16 == 8, "ANEURALNETWORKS_TENSOR_FLOAT16 has changed");
57 static_assert(ANEURALNETWORKS_TENSOR_BOOL8 == 9, "ANEURALNETWORKS_TENSOR_BOOL8 has changed");
58 static_assert(ANEURALNETWORKS_FLOAT16 == 10, "ANEURALNETWORKS_FLOAT16 has changed");
59 static_assert(ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL == 11,
60               "ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL has changed");
61 static_assert(ANEURALNETWORKS_TENSOR_QUANT16_ASYMM == 12,
62               "ANEURALNETWORKS_TENSOR_QUANT16_ASYMM has changed");
63 static_assert(ANEURALNETWORKS_TENSOR_QUANT8_SYMM == 13,
64               "ANEURALNETWORKS_TENSOR_QUANT8_SYMM has changed");
65 static_assert(ANEURALNETWORKS_OEM_SCALAR == 10000, "ANEURALNETWORKS_OEM_SCALAR has changed");
66 static_assert(ANEURALNETWORKS_TENSOR_OEM_BYTE == 10001,
67               "ANEURALNETWORKS_TENSOR_OEM_BYTE has changed");
68 
69 // IMPORTANT: When adding new values, update kNumberOfOperationTypes or
70 // kNumberOfOperationTypesOEMin Utils.h.
71 static_assert(ANEURALNETWORKS_ADD == 0, "ANEURALNETWORKS_ADD has changed");
72 static_assert(ANEURALNETWORKS_AVERAGE_POOL_2D == 1, "ANEURALNETWORKS_AVERAGE_POOL_2D has changed");
73 static_assert(ANEURALNETWORKS_CONCATENATION == 2, "ANEURALNETWORKS_CONCATENATION has changed");
74 static_assert(ANEURALNETWORKS_CONV_2D == 3, "ANEURALNETWORKS_CONV_2D has changed");
75 static_assert(ANEURALNETWORKS_DEPTHWISE_CONV_2D == 4,
76               "ANEURALNETWORKS_DEPTHWISE_CONV_2D has changed");
77 static_assert(ANEURALNETWORKS_DEPTH_TO_SPACE == 5, "ANEURALNETWORKS_DEPTH_TO_SPACE has changed");
78 static_assert(ANEURALNETWORKS_DEQUANTIZE == 6, "ANEURALNETWORKS_DEQUANTIZE has changed");
79 static_assert(ANEURALNETWORKS_EMBEDDING_LOOKUP == 7,
80               "ANEURALNETWORKS_EMBEDDING_LOOKUP has changed");
81 static_assert(ANEURALNETWORKS_FLOOR == 8, "ANEURALNETWORKS_FLOOR has changed");
82 static_assert(ANEURALNETWORKS_FULLY_CONNECTED == 9, "ANEURALNETWORKS_FULLY_CONNECTED has changed");
83 static_assert(ANEURALNETWORKS_HASHTABLE_LOOKUP == 10,
84               "ANEURALNETWORKS_HASHTABLE_LOOKUP has changed");
85 static_assert(ANEURALNETWORKS_L2_NORMALIZATION == 11,
86               "ANEURALNETWORKS_L2_NORMALIZATION has changed");
87 static_assert(ANEURALNETWORKS_L2_POOL_2D == 12, "ANEURALNETWORKS_L2_POOL has changed");
88 static_assert(ANEURALNETWORKS_LOCAL_RESPONSE_NORMALIZATION == 13,
89               "ANEURALNETWORKS_LOCAL_RESPONSE_NORMALIZATION has changed");
90 static_assert(ANEURALNETWORKS_LOGISTIC == 14, "ANEURALNETWORKS_LOGISTIC has changed");
91 static_assert(ANEURALNETWORKS_LSH_PROJECTION == 15, "ANEURALNETWORKS_LSH_PROJECTION has changed");
92 static_assert(ANEURALNETWORKS_LSTM == 16, "ANEURALNETWORKS_LSTM has changed");
93 static_assert(ANEURALNETWORKS_MAX_POOL_2D == 17, "ANEURALNETWORKS_MAX_POOL has changed");
94 static_assert(ANEURALNETWORKS_MUL == 18, "ANEURALNETWORKS_MUL has changed");
95 static_assert(ANEURALNETWORKS_RELU == 19, "ANEURALNETWORKS_RELU has changed");
96 static_assert(ANEURALNETWORKS_RELU1 == 20, "ANEURALNETWORKS_RELU1 has changed");
97 static_assert(ANEURALNETWORKS_RELU6 == 21, "ANEURALNETWORKS_RELU6 has changed");
98 static_assert(ANEURALNETWORKS_RESHAPE == 22, "ANEURALNETWORKS_RESHAPE has changed");
99 static_assert(ANEURALNETWORKS_RESIZE_BILINEAR == 23, "ANEURALNETWORKS_RESIZE_BILINEAR has changed");
100 static_assert(ANEURALNETWORKS_RNN == 24, "ANEURALNETWORKS_RNN has changed");
101 static_assert(ANEURALNETWORKS_SOFTMAX == 25, "ANEURALNETWORKS_SOFTMAX has changed");
102 static_assert(ANEURALNETWORKS_SPACE_TO_DEPTH == 26, "ANEURALNETWORKS_SPACE_TO_DEPTH has changed");
103 static_assert(ANEURALNETWORKS_SVDF == 27, "ANEURALNETWORKS_SVDF has changed");
104 static_assert(ANEURALNETWORKS_TANH == 28, "ANEURALNETWORKS_TANH has changed");
105 
106 static_assert(ANEURALNETWORKS_BATCH_TO_SPACE_ND == 29,
107               "ANEURALNETWORKS_BATCH_TO_SPACE_ND has changed");
108 static_assert(ANEURALNETWORKS_DIV == 30, "ANEURALNETWORKS_DIV has changed");
109 static_assert(ANEURALNETWORKS_MEAN == 31, "ANEURALNETWORKS_MEAN has changed");
110 static_assert(ANEURALNETWORKS_PAD == 32, "ANEURALNETWORKS_PAD has changed");
111 static_assert(ANEURALNETWORKS_SPACE_TO_BATCH_ND == 33,
112               "ANEURALNETWORKS_SPACE_TO_BATCH_ND has changed");
113 static_assert(ANEURALNETWORKS_SQUEEZE == 34, "ANEURALNETWORKS_SQUEEZE has changed");
114 static_assert(ANEURALNETWORKS_STRIDED_SLICE == 35, "ANEURALNETWORKS_STRIDED_SLICE has changed");
115 static_assert(ANEURALNETWORKS_SUB == 36, "ANEURALNETWORKS_TANH has changed");
116 static_assert(ANEURALNETWORKS_TRANSPOSE == 37, "ANEURALNETWORKS_TRANSPOSE has changed");
117 
118 static_assert(ANEURALNETWORKS_ABS == 38, "ANEURALNETWORKS_ABS has changed");
119 static_assert(ANEURALNETWORKS_ARGMAX == 39, "ANEURALNETWORKS_ARGMAX has changed");
120 static_assert(ANEURALNETWORKS_ARGMIN == 40, "ANEURALNETWORKS_ARGMIN has changed");
121 static_assert(ANEURALNETWORKS_AXIS_ALIGNED_BBOX_TRANSFORM == 41,
122               "ANEURALNETWORKS_AXIS_ALIGNED_BBOX_TRANSFORM has changed");
123 static_assert(ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_LSTM == 42,
124               "ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_LSTM has changed");
125 static_assert(ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_RNN == 43,
126               "ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_RNN has changed");
127 static_assert(ANEURALNETWORKS_BOX_WITH_NMS_LIMIT == 44,
128               "ANEURALNETWORKS_BOX_WITH_NMS_LIMIT has changed");
129 static_assert(ANEURALNETWORKS_CAST == 45, "ANEURALNETWORKS_CAST has changed");
130 static_assert(ANEURALNETWORKS_CHANNEL_SHUFFLE == 46, "ANEURALNETWORKS_CHANNEL_SHUFFLE has changed");
131 static_assert(ANEURALNETWORKS_DETECTION_POSTPROCESSING == 47,
132               "ANEURALNETWORKS_DETECTION_POSTPROCESSING has changed");
133 static_assert(ANEURALNETWORKS_EQUAL == 48, "ANEURALNETWORKS_EQUAL has changed");
134 static_assert(ANEURALNETWORKS_EXP == 49, "ANEURALNETWORKS_EXP has changed");
135 static_assert(ANEURALNETWORKS_EXPAND_DIMS == 50, "ANEURALNETWORKS_EXPAND_DIMS has changed");
136 static_assert(ANEURALNETWORKS_GATHER == 51, "ANEURALNETWORKS_GATHER has changed");
137 static_assert(ANEURALNETWORKS_GENERATE_PROPOSALS == 52,
138               "ANEURALNETWORKS_GENERATE_PROPOSALS has changed");
139 static_assert(ANEURALNETWORKS_GREATER == 53, "ANEURALNETWORKS_GREATER has changed");
140 static_assert(ANEURALNETWORKS_GREATER_EQUAL == 54, "ANEURALNETWORKS_GREATER_EQUAL has changed");
141 static_assert(ANEURALNETWORKS_GROUPED_CONV_2D == 55, "ANEURALNETWORKS_GROUPED_CONV_2D has changed");
142 static_assert(ANEURALNETWORKS_HEATMAP_MAX_KEYPOINT == 56,
143               "ANEURALNETWORKS_HEATMAP_MAX_KEYPOINT has changed");
144 static_assert(ANEURALNETWORKS_INSTANCE_NORMALIZATION == 57,
145               "ANEURALNETWORKS_INSTANCE_NORMALIZATION has changed");
146 static_assert(ANEURALNETWORKS_LESS == 58, "ANEURALNETWORKS_LESS has changed");
147 static_assert(ANEURALNETWORKS_LESS_EQUAL == 59, "ANEURALNETWORKS_LESS_EQUAL has changed");
148 static_assert(ANEURALNETWORKS_LOG == 60, "ANEURALNETWORKS_LOG has changed");
149 static_assert(ANEURALNETWORKS_LOGICAL_AND == 61, "ANEURALNETWORKS_LOGICAL_AND has changed");
150 static_assert(ANEURALNETWORKS_LOGICAL_NOT == 62, "ANEURALNETWORKS_LOGICAL_NOT has changed");
151 static_assert(ANEURALNETWORKS_LOGICAL_OR == 63, "ANEURALNETWORKS_LOGICAL_OR has changed");
152 static_assert(ANEURALNETWORKS_LOG_SOFTMAX == 64, "ANEURALNETWORKS_LOG_SOFTMAX has changed");
153 static_assert(ANEURALNETWORKS_MAXIMUM == 65, "ANEURALNETWORKS_MAXIMUM has changed");
154 static_assert(ANEURALNETWORKS_MINIMUM == 66, "ANEURALNETWORKS_MINIMUM has changed");
155 static_assert(ANEURALNETWORKS_NEG == 67, "ANEURALNETWORKS_NEG has changed");
156 static_assert(ANEURALNETWORKS_NOT_EQUAL == 68, "ANEURALNETWORKS_NOT_EQUAL has changed");
157 static_assert(ANEURALNETWORKS_PAD_V2 == 69, "ANEURALNETWORKS_PAD_V2 has changed");
158 static_assert(ANEURALNETWORKS_POW == 70, "ANEURALNETWORKS_POW has changed");
159 static_assert(ANEURALNETWORKS_PRELU == 71, "ANEURALNETWORKS_PRELU has changed");
160 static_assert(ANEURALNETWORKS_QUANTIZE == 72, "ANEURALNETWORKS_QUANTIZE has changed");
161 static_assert(ANEURALNETWORKS_QUANTIZED_16BIT_LSTM == 73,
162               "ANEURALNETWORKS_QUANTIZED_16BIT_LSTM has changed");
163 static_assert(ANEURALNETWORKS_RANDOM_MULTINOMIAL == 74,
164               "ANEURALNETWORKS_RANDOM_MULTINOMIAL has changed");
165 static_assert(ANEURALNETWORKS_REDUCE_ALL == 75, "ANEURALNETWORKS_REDUCE_ALL has changed");
166 static_assert(ANEURALNETWORKS_REDUCE_ANY == 76, "ANEURALNETWORKS_REDUCE_ANY has changed");
167 static_assert(ANEURALNETWORKS_REDUCE_MAX == 77, "ANEURALNETWORKS_REDUCE_MAX has changed");
168 static_assert(ANEURALNETWORKS_REDUCE_MIN == 78, "ANEURALNETWORKS_REDUCE_MIN has changed");
169 static_assert(ANEURALNETWORKS_REDUCE_PROD == 79, "ANEURALNETWORKS_REDUCE_PROD has changed");
170 static_assert(ANEURALNETWORKS_REDUCE_SUM == 80, "ANEURALNETWORKS_REDUCE_SUM has changed");
171 static_assert(ANEURALNETWORKS_ROI_ALIGN == 81, "ANEURALNETWORKS_ROI_ALIGN has changed");
172 static_assert(ANEURALNETWORKS_ROI_POOLING == 82, "ANEURALNETWORKS_ROI_POOLING has changed");
173 static_assert(ANEURALNETWORKS_RSQRT == 83, "ANEURALNETWORKS_RSQRT has changed");
174 static_assert(ANEURALNETWORKS_SELECT == 84, "ANEURALNETWORKS_SELECT has changed");
175 static_assert(ANEURALNETWORKS_SIN == 85, "ANEURALNETWORKS_SIN has changed");
176 static_assert(ANEURALNETWORKS_SLICE == 86, "ANEURALNETWORKS_SLICE has changed");
177 static_assert(ANEURALNETWORKS_SPLIT == 87, "ANEURALNETWORKS_SPLIT has changed");
178 static_assert(ANEURALNETWORKS_SQRT == 88, "ANEURALNETWORKS_SQRT has changed");
179 static_assert(ANEURALNETWORKS_TILE == 89, "ANEURALNETWORKS_TILE has changed");
180 static_assert(ANEURALNETWORKS_TOPK_V2 == 90, "ANEURALNETWORKS_TOPK_V2 has changed");
181 static_assert(ANEURALNETWORKS_TRANSPOSE_CONV_2D == 91,
182               "ANEURALNETWORKS_TRANSPOSE_CONV_2D has changed");
183 static_assert(ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_LSTM == 92,
184               "ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_LSTM has changed");
185 static_assert(ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_RNN == 93,
186               "ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_RNN has changed");
187 
188 static_assert(ANEURALNETWORKS_OEM_OPERATION == 10000, "ANEURALNETWORKS_OEM_OPERATION has changed");
189 
190 static_assert(ANEURALNETWORKS_FUSED_NONE == 0, "ANEURALNETWORKS_FUSED_NONE has changed");
191 static_assert(ANEURALNETWORKS_FUSED_RELU == 1, "ANEURALNETWORKS_FUSED_RELU has changed");
192 static_assert(ANEURALNETWORKS_FUSED_RELU1 == 2, "ANEURALNETWORKS_FUSED_RELU1 has changed");
193 static_assert(ANEURALNETWORKS_FUSED_RELU6 == 3, "ANEURALNETWORKS_FUSED_RELU6 has changed");
194 
195 static_assert(ANEURALNETWORKS_PREFER_LOW_POWER == 0,
196               "ANEURALNETWORKS_PREFER_LOW_POWER has changed");
197 static_assert(ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER == 1,
198               "ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER has changed");
199 static_assert(ANEURALNETWORKS_PREFER_SUSTAINED_SPEED == 2,
200               "ANEURALNETWORKS_PREFER_SUSTAINED_SPEED has changed");
201 
202 static_assert(ANEURALNETWORKS_NO_ERROR == 0, "ANEURALNETWORKS_NO_ERROR has changed");
203 static_assert(ANEURALNETWORKS_OUT_OF_MEMORY == 1, "ANEURALNETWORKS_OUT_OF_MEMORY has changed");
204 static_assert(ANEURALNETWORKS_INCOMPLETE == 2, "ANEURALNETWORKS_INCOMPLETE has changed");
205 static_assert(ANEURALNETWORKS_UNEXPECTED_NULL == 3, "ANEURALNETWORKS_UNEXPECTED_NULL has changed");
206 static_assert(ANEURALNETWORKS_BAD_DATA == 4, "ANEURALNETWORKS_BAD_DATA has changed");
207 static_assert(ANEURALNETWORKS_OP_FAILED == 5, "ANEURALNETWORKS_OP_FAILED has changed");
208 static_assert(ANEURALNETWORKS_BAD_STATE == 6, "ANEURALNETWORKS_BAD_STATE has changed");
209 static_assert(ANEURALNETWORKS_UNMAPPABLE == 7, "ANEURALNETWORKS_UNMAPPABLE has changed");
210 static_assert(ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE == 8,
211               "ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE has changed");
212 static_assert(ANEURALNETWORKS_UNAVAILABLE_DEVICE == 9,
213               "ANEURALNETWORKS_UNAVAILABLE_DEVICE has changed");
214 
215 static_assert(ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES == 128,
216               "ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES has changed");
217 
218 static_assert(ANEURALNETWORKS_DEVICE_UNKNOWN == 0, "ANEURALNETWORKS_DEVICE_UNKNOWN has changed");
219 static_assert(ANEURALNETWORKS_DEVICE_OTHER == 1, "ANEURALNETWORKS_DEVICE_OTHER has changed");
220 static_assert(ANEURALNETWORKS_DEVICE_CPU == 2, "ANEURALNETWORKS_DEVICE_CPU has changed");
221 static_assert(ANEURALNETWORKS_DEVICE_GPU == 3, "ANEURALNETWORKS_DEVICE_GPU has changed");
222 static_assert(ANEURALNETWORKS_DEVICE_ACCELERATOR == 4,
223               "ANEURALNETWORKS_DEVICE_ACCELERATOR has changed");
224 
225 static_assert(ANEURALNETWORKS_DURATION_ON_HARDWARE == 0,
226               "ANEURALNETWORKS_DURATION_ON_HARDWARE has changed");
227 static_assert(ANEURALNETWORKS_DURATION_IN_DRIVER == 1,
228               "ANEURALNETWORKS_DURATION_IN_DRIVER has changed");
229 
230 // Make sure that the constants are compatible with the values defined in
231 // hardware/interfaces/neuralnetworks/1.0/types.hal.
232 static_assert(static_cast<int32_t>(OperandType::OEM) == ANEURALNETWORKS_OEM_SCALAR,
233               "OEM != ANEURALNETWORKS_OEM");
234 static_assert(static_cast<int32_t>(OperandType::FLOAT32) == ANEURALNETWORKS_FLOAT32,
235               "FLOAT32 != ANEURALNETWORKS_FLOAT32");
236 static_assert(static_cast<int32_t>(OperandType::INT32) == ANEURALNETWORKS_INT32,
237               "INT32 != ANEURALNETWORKS_INT32");
238 static_assert(static_cast<int32_t>(OperandType::UINT32) == ANEURALNETWORKS_UINT32,
239               "UINT32 != ANEURALNETWORKS_UINT32");
240 static_assert(static_cast<int32_t>(OperandType::TENSOR_OEM_BYTE) == ANEURALNETWORKS_TENSOR_OEM_BYTE,
241               "TENSOR_OEM_BYTE != ANEURALNETWORKS_TENSOR_OEM_BYTE");
242 static_assert(static_cast<int32_t>(OperandType::TENSOR_FLOAT32) == ANEURALNETWORKS_TENSOR_FLOAT32,
243               "TENSOR_FLOAT32 != ANEURALNETWORKS_TENSOR_FLOAT32");
244 static_assert(static_cast<int32_t>(OperandType::TENSOR_QUANT8_ASYMM) ==
245                       ANEURALNETWORKS_TENSOR_QUANT8_ASYMM,
246               "TENSOR_QUANT8_ASYMM != ANEURALNETWORKS_TENSOR_QUANT8_ASYMM");
247 
248 static_assert(static_cast<int32_t>(OperationType::ADD) == ANEURALNETWORKS_ADD,
249               "OperationType::ADD != ANEURALNETWORKS_ADD");
250 static_assert(static_cast<int32_t>(OperationType::AVERAGE_POOL_2D) ==
251                       ANEURALNETWORKS_AVERAGE_POOL_2D,
252               "OperationType::AVERAGE_POOL_2D != ANEURALNETWORKS_AVERAGE_POOL_2D");
253 static_assert(static_cast<int32_t>(OperationType::CONV_2D) == ANEURALNETWORKS_CONV_2D,
254               "OperationType::CONV_2D != ANEURALNETWORKS_CONV_2D");
255 static_assert(static_cast<int32_t>(OperationType::DEPTHWISE_CONV_2D) ==
256                       ANEURALNETWORKS_DEPTHWISE_CONV_2D,
257               "OperationType::DEPTHWISE_CONV_2D != ANEURALNETWORKS_DEPTHWISE_CONV_2D");
258 static_assert(static_cast<int32_t>(OperationType::DEPTH_TO_SPACE) == ANEURALNETWORKS_DEPTH_TO_SPACE,
259               "OperationType::DEPTH_TO_SPACE != ANEURALNETWORKS_DEPTH_TO_SPACE");
260 static_assert(static_cast<int32_t>(OperationType::DEQUANTIZE) == ANEURALNETWORKS_DEQUANTIZE,
261               "OperationType::DEQUANTIZE != ANEURALNETWORKS_DEQUANTIZE");
262 static_assert(static_cast<int32_t>(OperationType::EMBEDDING_LOOKUP) ==
263                       ANEURALNETWORKS_EMBEDDING_LOOKUP,
264               "OperationType::EMBEDDING_LOOKUP != ANEURALNETWORKS_EMBEDDING_LOOKUP");
265 static_assert(static_cast<int32_t>(OperationType::FLOOR) == ANEURALNETWORKS_FLOOR,
266               "OperationType::FLOOR != ANEURALNETWORKS_FLOOR");
267 static_assert(static_cast<int32_t>(OperationType::FULLY_CONNECTED) ==
268                       ANEURALNETWORKS_FULLY_CONNECTED,
269               "OperationType::FULLY_CONNECTED != ANEURALNETWORKS_FULLY_CONNECTED");
270 static_assert(static_cast<int32_t>(OperationType::HASHTABLE_LOOKUP) ==
271                       ANEURALNETWORKS_HASHTABLE_LOOKUP,
272               "OperationType::HASHTABLE_LOOKUP != ANEURALNETWORKS_HASHTABLE_LOOKUP");
273 static_assert(static_cast<int32_t>(OperationType::L2_NORMALIZATION) ==
274                       ANEURALNETWORKS_L2_NORMALIZATION,
275               "OperationType::L2_NORMALIZATION != ANEURALNETWORKS_L2_NORMALIZATION");
276 static_assert(static_cast<int32_t>(OperationType::L2_POOL_2D) == ANEURALNETWORKS_L2_POOL_2D,
277               "OperationType::L2_POOL_2D != ANEURALNETWORKS_L2_POOL_2D");
278 static_assert(static_cast<int32_t>(OperationType::LOCAL_RESPONSE_NORMALIZATION) ==
279                       ANEURALNETWORKS_LOCAL_RESPONSE_NORMALIZATION,
280               "OperationType::LOCAL_RESPONSE_NORMALIZATION != "
281               "ANEURALNETWORKS_LOCAL_RESPONSE_NORMALIZATION");
282 static_assert(static_cast<int32_t>(OperationType::LOGISTIC) == ANEURALNETWORKS_LOGISTIC,
283               "OperationType::LOGISTIC != ANEURALNETWORKS_LOGISTIC");
284 static_assert(static_cast<int32_t>(OperationType::LSH_PROJECTION) == ANEURALNETWORKS_LSH_PROJECTION,
285               "OperationType::LSH_PROJECTION != ANEURALNETWORKS_LSH_PROJECTION");
286 static_assert(static_cast<int32_t>(OperationType::LSTM) == ANEURALNETWORKS_LSTM,
287               "OperationType::LSTM != ANEURALNETWORKS_LSTM");
288 static_assert(static_cast<int32_t>(OperationType::MAX_POOL_2D) == ANEURALNETWORKS_MAX_POOL_2D,
289               "OperationType::MAX_POOL_2D != ANEURALNETWORKS_MAX_POOL_2D");
290 static_assert(static_cast<int32_t>(OperationType::MUL) == ANEURALNETWORKS_MUL,
291               "OperationType::MUL != ANEURALNETWORKS_MUL");
292 static_assert(static_cast<int32_t>(OperationType::RELU) == ANEURALNETWORKS_RELU,
293               "OperationType::RELU != ANEURALNETWORKS_RELU");
294 static_assert(static_cast<int32_t>(OperationType::RELU1) == ANEURALNETWORKS_RELU1,
295               "OperationType::RELU1 != ANEURALNETWORKS_RELU1");
296 static_assert(static_cast<int32_t>(OperationType::RELU6) == ANEURALNETWORKS_RELU6,
297               "OperationType::RELU6 != ANEURALNETWORKS_RELU6");
298 static_assert(static_cast<int32_t>(OperationType::RESHAPE) == ANEURALNETWORKS_RESHAPE,
299               "OperationType::RESHAPE != ANEURALNETWORKS_RESHAPE");
300 static_assert(static_cast<int32_t>(OperationType::RESIZE_BILINEAR) ==
301                       ANEURALNETWORKS_RESIZE_BILINEAR,
302               "OperationType::RESIZE_BILINEAR != ANEURALNETWORKS_RESIZE_BILINEAR");
303 static_assert(static_cast<int32_t>(OperationType::RNN) == ANEURALNETWORKS_RNN,
304               "OperationType::RNN != ANEURALNETWORKS_RNN");
305 static_assert(static_cast<int32_t>(OperationType::SOFTMAX) == ANEURALNETWORKS_SOFTMAX,
306               "OperationType::SOFTMAX != ANEURALNETWORKS_SOFTMAX");
307 static_assert(static_cast<int32_t>(OperationType::SPACE_TO_DEPTH) == ANEURALNETWORKS_SPACE_TO_DEPTH,
308               "OperationType::SPACE_TO_DEPTH != ANEURALNETWORKS_SPACE_TO_DEPTH");
309 static_assert(static_cast<int32_t>(OperationType::SVDF) == ANEURALNETWORKS_SVDF,
310               "OperationType::SVDF != ANEURALNETWORKS_SVDF");
311 static_assert(static_cast<int32_t>(OperationType::TANH) == ANEURALNETWORKS_TANH,
312               "OperationType::TANH != ANEURALNETWORKS_TANH");
313 
314 static_assert(static_cast<int32_t>(FusedActivationFunc::NONE) == ANEURALNETWORKS_FUSED_NONE,
315               "FusedActivationFunc::NONE != ANEURALNETWORKS_FUSED_NONE");
316 static_assert(static_cast<int32_t>(FusedActivationFunc::RELU) == ANEURALNETWORKS_FUSED_RELU,
317               "FusedActivationFunc::RELU != ANEURALNETWORKS_FUSED_RELU");
318 static_assert(static_cast<int32_t>(FusedActivationFunc::RELU1) == ANEURALNETWORKS_FUSED_RELU1,
319               "FusedActivationFunc::RELU1 != ANEURALNETWORKS_FUSED_RELU1");
320 static_assert(static_cast<int32_t>(FusedActivationFunc::RELU6) == ANEURALNETWORKS_FUSED_RELU6,
321               "FusedActivationFunc::RELU6 != ANEURALNETWORKS_FUSED_RELU6");
322 
323 // Make sure that the constants are compatible with the values defined in
324 // hardware/interfaces/neuralnetworks/1.1/types.hal.
325 static_assert(static_cast<int32_t>(OperationType::BATCH_TO_SPACE_ND) ==
326                       ANEURALNETWORKS_BATCH_TO_SPACE_ND,
327               "OperationType::BATCH_TO_SPACE_ND != ANEURALNETWORKS_BATCH_TO_SPACE_ND");
328 static_assert(static_cast<int32_t>(OperationType::DIV) == ANEURALNETWORKS_DIV,
329               "OperationType::DIV != ANEURALNETWORKS_DIV");
330 static_assert(static_cast<int32_t>(OperationType::MEAN) == ANEURALNETWORKS_MEAN,
331               "OperationType::MEAN != ANEURALNETWORKS_MEAN");
332 static_assert(static_cast<int32_t>(OperationType::PAD) == ANEURALNETWORKS_PAD,
333               "OperationType::PAD != ANEURALNETWORKS_PAD");
334 static_assert(static_cast<int32_t>(OperationType::SPACE_TO_BATCH_ND) ==
335                       ANEURALNETWORKS_SPACE_TO_BATCH_ND,
336               "OperationType::SPACE_TO_BATCH_ND != ANEURALNETWORKS_SPACE_TO_BATCH_ND");
337 static_assert(static_cast<int32_t>(OperationType::SQUEEZE) == ANEURALNETWORKS_SQUEEZE,
338               "OperationType::SQUEEZE != ANEURALNETWORKS_SQUEEZE");
339 static_assert(static_cast<int32_t>(OperationType::STRIDED_SLICE) == ANEURALNETWORKS_STRIDED_SLICE,
340               "OperationType::STRIDED_SLICE != ANEURALNETWORKS_STRIDED_SLICE");
341 static_assert(static_cast<int32_t>(OperationType::SUB) == ANEURALNETWORKS_SUB,
342               "OperationType::SUB != ANEURALNETWORKS_SUB");
343 static_assert(static_cast<int32_t>(OperationType::TRANSPOSE) == ANEURALNETWORKS_TRANSPOSE,
344               "OperationType::TRANSPOSE != ANEURALNETWORKS_TRANSPOSE");
345 
346 // Make sure that the constants are compatible with the values defined in
347 // hardware/interfaces/neuralnetworks/1.2/types.hal.
348 static_assert(static_cast<int32_t>(OperandType::BOOL) == ANEURALNETWORKS_BOOL,
349               "BOOL != ANEURALNETWORKS_BOOL");
350 static_assert(static_cast<int32_t>(OperandType::TENSOR_QUANT16_SYMM) ==
351                       ANEURALNETWORKS_TENSOR_QUANT16_SYMM,
352               "TENSOR_QUANT16_SYMM != ANEURALNETWORKS_TENSOR_QUANT16_SYMM");
353 static_assert(static_cast<int32_t>(OperandType::TENSOR_FLOAT16) == ANEURALNETWORKS_TENSOR_FLOAT16,
354               "TENSOR_FLOAT16 != ANEURALNETWORKS_TENSOR_FLOAT16");
355 static_assert(static_cast<int32_t>(OperandType::TENSOR_BOOL8) == ANEURALNETWORKS_TENSOR_BOOL8,
356               "TENSOR_BOOL8 != ANEURALNETWORKS_TENSOR_BOOL8");
357 static_assert(static_cast<int32_t>(OperandType::FLOAT16) == ANEURALNETWORKS_FLOAT16,
358               "FLOAT16 != ANEURALNETWORKS_FLOAT16");
359 static_assert(static_cast<int32_t>(OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) ==
360                       ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL,
361               "TENSOR_QUANT8_SYMM_PER_CHANNEL != ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL");
362 static_assert(static_cast<int32_t>(OperandType::TENSOR_QUANT16_ASYMM) ==
363                       ANEURALNETWORKS_TENSOR_QUANT16_ASYMM,
364               "TENSOR_QUANT16_ASYMM != ANEURALNETWORKS_TENSOR_QUANT16_ASYMM");
365 static_assert(static_cast<int32_t>(OperandType::TENSOR_QUANT8_SYMM) ==
366                       ANEURALNETWORKS_TENSOR_QUANT8_SYMM,
367               "TENSOR_QUANT8_SYMM != ANEURALNETWORKS_TENSOR_QUANT8_SYMM");
368 
369 static_assert(static_cast<int32_t>(OperationType::ABS) == ANEURALNETWORKS_ABS,
370               "OperationType::ABS != ANEURALNETWORKS_ABS");
371 static_assert(static_cast<int32_t>(OperationType::ARGMAX) == ANEURALNETWORKS_ARGMAX,
372               "OperationType::ARGMAX != ANEURALNETWORKS_ARGMAX");
373 static_assert(static_cast<int32_t>(OperationType::ARGMIN) == ANEURALNETWORKS_ARGMIN,
374               "OperationType::ARGMIN != ANEURALNETWORKS_ARGMIN");
375 static_assert(static_cast<int32_t>(OperationType::AXIS_ALIGNED_BBOX_TRANSFORM) ==
376                       ANEURALNETWORKS_AXIS_ALIGNED_BBOX_TRANSFORM,
377               "OperationType::AXIS_ALIGNED_BBOX_TRANSFORM != "
378               "ANEURALNETWORKS_AXIS_ALIGNED_BBOX_TRANSFORM");
379 static_assert(static_cast<int32_t>(OperationType::BIDIRECTIONAL_SEQUENCE_LSTM) ==
380                       ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_LSTM,
381               "OperationType::BIDIRECTIONAL_SEQUENCE_LSTM != "
382               "ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_LSTM");
383 static_assert(
384         static_cast<int32_t>(OperationType::BIDIRECTIONAL_SEQUENCE_RNN) ==
385                 ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_RNN,
386         "OperationType::BIDIRECTIONAL_SEQUENCE_RNN != ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_RNN");
387 static_assert(static_cast<int32_t>(OperationType::BOX_WITH_NMS_LIMIT) ==
388                       ANEURALNETWORKS_BOX_WITH_NMS_LIMIT,
389               "OperationType::BOX_WITH_NMS_LIMIT != ANEURALNETWORKS_BOX_WITH_NMS_LIMIT");
390 static_assert(static_cast<int32_t>(OperationType::CAST) == ANEURALNETWORKS_CAST,
391               "OperationType::CAST != ANEURALNETWORKS_CAST");
392 static_assert(static_cast<int32_t>(OperationType::CHANNEL_SHUFFLE) ==
393                       ANEURALNETWORKS_CHANNEL_SHUFFLE,
394               "OperationType::CHANNEL_SHUFFLE != ANEURALNETWORKS_CHANNEL_SHUFFLE");
395 static_assert(
396         static_cast<int32_t>(OperationType::DETECTION_POSTPROCESSING) ==
397                 ANEURALNETWORKS_DETECTION_POSTPROCESSING,
398         "OperationType::DETECTION_POSTPROCESSING != ANEURALNETWORKS_DETECTION_POSTPROCESSING");
399 static_assert(static_cast<int32_t>(OperationType::EQUAL) == ANEURALNETWORKS_EQUAL,
400               "OperationType::EQUAL != ANEURALNETWORKS_EQUAL");
401 static_assert(static_cast<int32_t>(OperationType::EXP) == ANEURALNETWORKS_EXP,
402               "OperationType::EXP != ANEURALNETWORKS_EXP");
403 static_assert(static_cast<int32_t>(OperationType::EXPAND_DIMS) == ANEURALNETWORKS_EXPAND_DIMS,
404               "OperationType::EXPAND_DIMS != ANEURALNETWORKS_EXPAND_DIMS");
405 static_assert(static_cast<int32_t>(OperationType::GATHER) == ANEURALNETWORKS_GATHER,
406               "OperationType::GATHER != ANEURALNETWORKS_GATHER");
407 static_assert(static_cast<int32_t>(OperationType::GENERATE_PROPOSALS) ==
408                       ANEURALNETWORKS_GENERATE_PROPOSALS,
409               "OperationType::GENERATE_PROPOSALS != ANEURALNETWORKS_GENERATE_PROPOSALS");
410 static_assert(static_cast<int32_t>(OperationType::GREATER) == ANEURALNETWORKS_GREATER,
411               "OperationType::GREATER != ANEURALNETWORKS_GREATER");
412 static_assert(static_cast<int32_t>(OperationType::GREATER_EQUAL) == ANEURALNETWORKS_GREATER_EQUAL,
413               "OperationType::GREATER_EQUAL != ANEURALNETWORKS_GREATER_EQUAL");
414 static_assert(static_cast<int32_t>(OperationType::GROUPED_CONV_2D) ==
415                       ANEURALNETWORKS_GROUPED_CONV_2D,
416               "OperationType::GROUPED_CONV_2D != ANEURALNETWORKS_GROUPED_CONV_2D");
417 static_assert(static_cast<int32_t>(OperationType::HEATMAP_MAX_KEYPOINT) ==
418                       ANEURALNETWORKS_HEATMAP_MAX_KEYPOINT,
419               "OperationType::HEATMAP_MAX_KEYPOINT != ANEURALNETWORKS_HEATMAP_MAX_KEYPOINT");
420 static_assert(static_cast<int32_t>(OperationType::INSTANCE_NORMALIZATION) ==
421                       ANEURALNETWORKS_INSTANCE_NORMALIZATION,
422               "OperationType::INSTANCE_NORMALIZATION != ANEURALNETWORKS_INSTANCE_NORMALIZATION");
423 static_assert(static_cast<int32_t>(OperationType::LESS) == ANEURALNETWORKS_LESS,
424               "OperationType::LESS != ANEURALNETWORKS_LESS");
425 static_assert(static_cast<int32_t>(OperationType::LESS_EQUAL) == ANEURALNETWORKS_LESS_EQUAL,
426               "OperationType::LESS_EQUAL != ANEURALNETWORKS_LESS_EQUAL");
427 static_assert(static_cast<int32_t>(OperationType::LOG) == ANEURALNETWORKS_LOG,
428               "OperationType::LOG != ANEURALNETWORKS_LOG");
429 static_assert(static_cast<int32_t>(OperationType::LOGICAL_AND) == ANEURALNETWORKS_LOGICAL_AND,
430               "OperationType::LOGICAL_AND != ANEURALNETWORKS_LOGICAL_AND");
431 static_assert(static_cast<int32_t>(OperationType::LOGICAL_NOT) == ANEURALNETWORKS_LOGICAL_NOT,
432               "OperationType::LOGICAL_NOT != ANEURALNETWORKS_LOGICAL_NOT");
433 static_assert(static_cast<int32_t>(OperationType::LOGICAL_OR) == ANEURALNETWORKS_LOGICAL_OR,
434               "OperationType::LOGICAL_OR != ANEURALNETWORKS_LOGICAL_OR");
435 static_assert(static_cast<int32_t>(OperationType::LOG_SOFTMAX) == ANEURALNETWORKS_LOG_SOFTMAX,
436               "OperationType::LOG_SOFTMAX != ANEURALNETWORKS_LOG_SOFTMAX");
437 static_assert(static_cast<int32_t>(OperationType::MAXIMUM) == ANEURALNETWORKS_MAXIMUM,
438               "OperationType::MAXIMUM != ANEURALNETWORKS_MAXIMUM");
439 static_assert(static_cast<int32_t>(OperationType::MINIMUM) == ANEURALNETWORKS_MINIMUM,
440               "OperationType::MINIMUM != ANEURALNETWORKS_MINIMUM");
441 static_assert(static_cast<int32_t>(OperationType::NEG) == ANEURALNETWORKS_NEG,
442               "OperationType::NEG != ANEURALNETWORKS_NEG");
443 static_assert(static_cast<int32_t>(OperationType::NOT_EQUAL) == ANEURALNETWORKS_NOT_EQUAL,
444               "OperationType::NOT_EQUAL != ANEURALNETWORKS_NOT_EQUAL");
445 static_assert(static_cast<int32_t>(OperationType::PAD_V2) == ANEURALNETWORKS_PAD_V2,
446               "OperationType::PAD_V2 != ANEURALNETWORKS_PAD_V2");
447 static_assert(static_cast<int32_t>(OperationType::POW) == ANEURALNETWORKS_POW,
448               "OperationType::POW != ANEURALNETWORKS_POW");
449 static_assert(static_cast<int32_t>(OperationType::PRELU) == ANEURALNETWORKS_PRELU,
450               "OperationType::PRELU != ANEURALNETWORKS_PRELU");
451 static_assert(static_cast<int32_t>(OperationType::QUANTIZE) == ANEURALNETWORKS_QUANTIZE,
452               "OperationType::QUANTIZE != ANEURALNETWORKS_QUANTIZE");
453 static_assert(static_cast<int32_t>(OperationType::QUANTIZED_16BIT_LSTM) ==
454                       ANEURALNETWORKS_QUANTIZED_16BIT_LSTM,
455               "OperationType::QUANTIZED_16BIT_LSTM != ANEURALNETWORKS_QUANTIZED_16BIT_LSTM");
456 static_assert(static_cast<int32_t>(OperationType::RANDOM_MULTINOMIAL) ==
457                       ANEURALNETWORKS_RANDOM_MULTINOMIAL,
458               "OperationType::RANDOM_MULTINOMIAL != ANEURALNETWORKS_RANDOM_MULTINOMIAL");
459 static_assert(static_cast<int32_t>(OperationType::REDUCE_ALL) == ANEURALNETWORKS_REDUCE_ALL,
460               "OperationType::REDUCE_ALL != ANEURALNETWORKS_REDUCE_ALL");
461 static_assert(static_cast<int32_t>(OperationType::REDUCE_ANY) == ANEURALNETWORKS_REDUCE_ANY,
462               "OperationType::REDUCE_ANY != ANEURALNETWORKS_REDUCE_ANY");
463 static_assert(static_cast<int32_t>(OperationType::REDUCE_MAX) == ANEURALNETWORKS_REDUCE_MAX,
464               "OperationType::REDUCE_MAX != ANEURALNETWORKS_REDUCE_MAX");
465 static_assert(static_cast<int32_t>(OperationType::REDUCE_MIN) == ANEURALNETWORKS_REDUCE_MIN,
466               "OperationType::REDUCE_MIN != ANEURALNETWORKS_REDUCE_MIN");
467 static_assert(static_cast<int32_t>(OperationType::REDUCE_PROD) == ANEURALNETWORKS_REDUCE_PROD,
468               "OperationType::REDUCE_PROD != ANEURALNETWORKS_REDUCE_PROD");
469 static_assert(static_cast<int32_t>(OperationType::REDUCE_SUM) == ANEURALNETWORKS_REDUCE_SUM,
470               "OperationType::REDUCE_SUM != ANEURALNETWORKS_REDUCE_SUM");
471 static_assert(static_cast<int32_t>(OperationType::ROI_ALIGN) == ANEURALNETWORKS_ROI_ALIGN,
472               "OperationType::ROI_ALIGN != ANEURALNETWORKS_ROI_ALIGN");
473 static_assert(static_cast<int32_t>(OperationType::ROI_POOLING) == ANEURALNETWORKS_ROI_POOLING,
474               "OperationType::ROI_POOLING != ANEURALNETWORKS_ROI_POOLING");
475 static_assert(static_cast<int32_t>(OperationType::RSQRT) == ANEURALNETWORKS_RSQRT,
476               "OperationType::RSQRT != ANEURALNETWORKS_RSQRT");
477 static_assert(static_cast<int32_t>(OperationType::SELECT) == ANEURALNETWORKS_SELECT,
478               "OperationType::SELECT != ANEURALNETWORKS_SELECT");
479 static_assert(static_cast<int32_t>(OperationType::SIN) == ANEURALNETWORKS_SIN,
480               "OperationType::SIN != ANEURALNETWORKS_SIN");
481 static_assert(static_cast<int32_t>(OperationType::SLICE) == ANEURALNETWORKS_SLICE,
482               "OperationType::SLICE != ANEURALNETWORKS_SLICE");
483 static_assert(static_cast<int32_t>(OperationType::SPLIT) == ANEURALNETWORKS_SPLIT,
484               "OperationType::SPLIT != ANEURALNETWORKS_SPLIT");
485 static_assert(static_cast<int32_t>(OperationType::SQRT) == ANEURALNETWORKS_SQRT,
486               "OperationType::SQRT != ANEURALNETWORKS_SQRT");
487 static_assert(static_cast<int32_t>(OperationType::TILE) == ANEURALNETWORKS_TILE,
488               "OperationType::TILE != ANEURALNETWORKS_TILE");
489 static_assert(static_cast<int32_t>(OperationType::TOPK_V2) == ANEURALNETWORKS_TOPK_V2,
490               "OperationType::TOPK_V2 != ANEURALNETWORKS_TOPK_V2");
491 static_assert(static_cast<int32_t>(OperationType::TRANSPOSE_CONV_2D) ==
492                       ANEURALNETWORKS_TRANSPOSE_CONV_2D,
493               "OperationType::TRANSPOSE_CONV_2D != ANEURALNETWORKS_TRANSPOSE_CONV_2D");
494 static_assert(static_cast<int32_t>(OperationType::UNIDIRECTIONAL_SEQUENCE_LSTM) ==
495                       ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_LSTM,
496               "OperationType::UNIDIRECTIONAL_SEQUENCE_LSTM != "
497               "ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_LSTM");
498 static_assert(static_cast<int32_t>(OperationType::UNIDIRECTIONAL_SEQUENCE_RNN) ==
499                       ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_RNN,
500               "OperationType::UNIDIRECTIONAL_SEQUENCE_RNN != "
501               "ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_RNN");
502 static_assert(static_cast<int32_t>(OperationType::RESIZE_NEAREST_NEIGHBOR) ==
503                       ANEURALNETWORKS_RESIZE_NEAREST_NEIGHBOR,
504               "OperationType::RESIZE_NEAREST_NEIGHBOR != ANEURALNETWORKS_RESIZE_NEAREST_NEIGHBOR");
505 
506 static_assert(static_cast<int32_t>(DeviceType::OTHER) == ANEURALNETWORKS_DEVICE_OTHER,
507               "DeviceType::OTHER != ANEURALNETWORKS_DEVICE_OTHER");
508 static_assert(static_cast<int32_t>(DeviceType::CPU) == ANEURALNETWORKS_DEVICE_CPU,
509               "DeviceType::CPU != ANEURALNETWORKS_DEVICE_CPU");
510 static_assert(static_cast<int32_t>(DeviceType::GPU) == ANEURALNETWORKS_DEVICE_GPU,
511               "DeviceType::GPU != ANEURALNETWORKS_DEVICE_GPU");
512 static_assert(static_cast<int32_t>(DeviceType::ACCELERATOR) == ANEURALNETWORKS_DEVICE_ACCELERATOR,
513               "DeviceType::ACCELERATOR != ANEURALNETWORKS_DEVICE_ACCELERATOR");
514 
515 // Asserts for ANeuralNetworksOperandType memory layout
516 static_assert(offsetof(ANeuralNetworksOperandType, type) == 0,
517               "ANeuralNetworksOperandType.type offset != 0");
518 static_assert(offsetof(ANeuralNetworksOperandType, dimensionCount) == 4,
519               "ANeuralNetworksOperandType.dimensionCount offset != 4");
520 static_assert(offsetof(ANeuralNetworksOperandType, dimensions) == 8,
521               "ANeuralNetworksOperandType.dimensions offset != 8");
522 static_assert(offsetof(ANeuralNetworksOperandType, scale) == 8 + sizeof(void*),
523               "ANeuralNetworksOperandType.scale offset != 8 + sizeof(void*)");
524 static_assert(offsetof(ANeuralNetworksOperandType, zeroPoint) == 12 + sizeof(void*),
525               "ANeuralNetworksOperandType.zeroPoint offset != 12 + sizeof(void*)");
526 static_assert(sizeof(ANeuralNetworksOperandType) == 16 + sizeof(void*),
527               "ANeuralNetworksOperandType size changed");
528 static_assert(alignof(ANeuralNetworksOperandType) == alignof(void*),
529               "ANeuralNetworksOperandType alignment changed");
530 
531 // Asserts for ANeuralNetworksSymmPerChannelQuantParams memory layout
532 static_assert(offsetof(ANeuralNetworksSymmPerChannelQuantParams, channelDim) == 0,
533               "ANeuralNetworksSymmPerChannelQuantParams.channelDim offset != 4 + sizeof(void*)");
534 static_assert(offsetof(ANeuralNetworksSymmPerChannelQuantParams, scaleCount) == 4,
535               "ANeuralNetworksSymmPerChannelQuantParams.scaleCount offset != 0");
536 static_assert(offsetof(ANeuralNetworksSymmPerChannelQuantParams, scales) == 8,
537               "ANeuralNetworksSymmPerChannelQuantParams.scales offset != 4");
538 static_assert(sizeof(ANeuralNetworksSymmPerChannelQuantParams) == 8 + sizeof(void*),
539               "ANeuralNetworksSymmPerChannelQuantParams size != 8 + sizeof(void*)");
540 static_assert(alignof(ANeuralNetworksSymmPerChannelQuantParams) == alignof(void*),
541               "ANeuralNetworksOperandType alignment changed");
542 
543 // Asserts for compilation caching
544 static_assert(ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN == 32,
545               "ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN has changed");
546 static_assert(static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN) ==
547                       ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN,
548               "Constant::BYTE_SIZE_OF_CACHE_TOKEN != ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN");
549 
550 using android::sp;
551 using namespace android::nn;
552 
ANeuralNetworks_getDeviceCount(uint32_t * numDevices)553 int ANeuralNetworks_getDeviceCount(uint32_t* numDevices) {
554     if (numDevices == nullptr) {
555         LOG(ERROR) << "ANeuralNetworks_getDeviceCount passed a nullptr";
556         return ANEURALNETWORKS_UNEXPECTED_NULL;
557     }
558     *numDevices = DeviceManager::get()->getDrivers().size();
559     return ANEURALNETWORKS_NO_ERROR;
560 }
561 
ANeuralNetworks_getDevice(uint32_t devIndex,ANeuralNetworksDevice ** device)562 int ANeuralNetworks_getDevice(uint32_t devIndex, ANeuralNetworksDevice** device) {
563     if (device == nullptr) {
564         LOG(ERROR) << "ANeuralNetworks_getDevice passed a nullptr";
565         return ANEURALNETWORKS_UNEXPECTED_NULL;
566     }
567     const std::vector<std::shared_ptr<Device>>& devices = DeviceManager::get()->getDrivers();
568     if (devIndex >= devices.size()) {
569         LOG(ERROR) << "ANeuralNetworks_getDevice passed an invalid device index";
570         return ANEURALNETWORKS_BAD_DATA;
571     }
572     *device = reinterpret_cast<ANeuralNetworksDevice*>(devices.at(devIndex).get());
573     return ANEURALNETWORKS_NO_ERROR;
574 }
575 
ANeuralNetworksDevice_getName(const ANeuralNetworksDevice * device,const char ** name)576 int ANeuralNetworksDevice_getName(const ANeuralNetworksDevice* device, const char** name) {
577     if (device == nullptr || name == nullptr) {
578         LOG(ERROR) << "ANeuralNetworksDevice_getName passed a nullptr";
579         return ANEURALNETWORKS_UNEXPECTED_NULL;
580     }
581     const Device* d = reinterpret_cast<const Device*>(device);
582     *name = d->getName();
583     return ANEURALNETWORKS_NO_ERROR;
584 }
585 
ANeuralNetworksDevice_getVersion(const ANeuralNetworksDevice * device,const char ** version)586 int ANeuralNetworksDevice_getVersion(const ANeuralNetworksDevice* device, const char** version) {
587     if (device == nullptr || version == nullptr) {
588         LOG(ERROR) << "ANeuralNetworksDevice_getVersion passed a nullptr";
589         return ANEURALNETWORKS_UNEXPECTED_NULL;
590     }
591     const Device* d = reinterpret_cast<const Device*>(device);
592     *version = d->getVersionString();
593     return ANEURALNETWORKS_NO_ERROR;
594 }
595 
ANeuralNetworksDevice_getType(const ANeuralNetworksDevice * device,int32_t * type)596 int ANeuralNetworksDevice_getType(const ANeuralNetworksDevice* device, int32_t* type) {
597     if (device == nullptr || type == nullptr) {
598         LOG(ERROR) << "ANeuralNetworksDevice_getType passed a nullptr";
599         return ANEURALNETWORKS_UNEXPECTED_NULL;
600     }
601     const Device* d = reinterpret_cast<const Device*>(device);
602     int32_t dType = d->getType();
603     if (dType < 0) {
604         return ANEURALNETWORKS_OP_FAILED;
605     }
606     *type = d->getType();
607     return ANEURALNETWORKS_NO_ERROR;
608 }
609 
ANeuralNetworksDevice_getFeatureLevel(const ANeuralNetworksDevice * device,int64_t * featureLevel)610 int ANeuralNetworksDevice_getFeatureLevel(const ANeuralNetworksDevice* device,
611                                           int64_t* featureLevel) {
612     if (device == nullptr || featureLevel == nullptr) {
613         LOG(ERROR) << "ANeuralNetworksDevice_getFeatureLevel passed a nullptr";
614         return ANEURALNETWORKS_UNEXPECTED_NULL;
615     }
616     Device* d = reinterpret_cast<Device*>(const_cast<ANeuralNetworksDevice*>(device));
617     int64_t dFeatureLevel = d->getFeatureLevel();
618     if (dFeatureLevel < 0) {
619         return ANEURALNETWORKS_BAD_STATE;
620     }
621     *featureLevel = dFeatureLevel;
622     return ANEURALNETWORKS_NO_ERROR;
623 }
624 
ANeuralNetworksModel_getSupportedOperationsForDevices(const ANeuralNetworksModel * model,const ANeuralNetworksDevice * const * devices,uint32_t numDevices,bool * supportedOps)625 int ANeuralNetworksModel_getSupportedOperationsForDevices(
626         const ANeuralNetworksModel* model, const ANeuralNetworksDevice* const* devices,
627         uint32_t numDevices, bool* supportedOps) {
628     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksModel_getSupportedOperationsForDevices");
629     if (model == nullptr || devices == nullptr || supportedOps == nullptr) {
630         LOG(ERROR) << "ANeuralNetworksModel_getSupportedOperationsForDevices passed a nullptr";
631         return ANEURALNETWORKS_UNEXPECTED_NULL;
632     }
633     if (numDevices == 0) {
634         LOG(ERROR) << "ANeuralNetworksModel_getSupportedOperationsForDevices passed an empty "
635                       "device list";
636         return ANEURALNETWORKS_BAD_DATA;
637     }
638     const ModelBuilder* m = reinterpret_cast<const ModelBuilder*>(model);
639     if (!m->isFinished() || !m->isValid()) {
640         LOG(ERROR) << "ANeuralNetworksModel_getSupportedOperationsForDevices passed an unfinished "
641                       "or invalid Model";
642         return ANEURALNETWORKS_BAD_STATE;
643     }
644 
645     Model hidlModel;
646     m->setHidlModel(&hidlModel);
647     const std::vector<uint32_t>& opMap = m->getSortedOperationMapping();
648     // init the output array to false for all the operations.
649     std::fill(supportedOps, supportedOps + opMap.size(), false);
650     for (uint32_t i = 0; i < numDevices; i++) {
651         if (devices[i] == nullptr) {
652             LOG(ERROR) << "ANeuralNetworksModel_getSupportedOperationsForDevices passed a nullptr "
653                           "as a device";
654             return ANEURALNETWORKS_UNEXPECTED_NULL;
655         }
656         for (uint32_t j = i + 1; j < numDevices; j++) {
657             if (devices[i] == devices[j]) {
658                 LOG(ERROR) << "ANeuralNetworksModel_getSupportedOperationsForDevices passed "
659                               "duplicate devices";
660                 return ANEURALNETWORKS_BAD_DATA;
661             }
662         }
663 
664         Device* d = reinterpret_cast<Device*>(const_cast<ANeuralNetworksDevice*>(devices[i]));
665         hidl_vec<bool> supportsByDevice;
666         d->getSupportedOperations(hidlModel, &supportsByDevice);
667         for (uint32_t j = 0; j < supportsByDevice.size(); j++) {
668             uint32_t originalIdx = opMap[j];
669             supportedOps[originalIdx] |= supportsByDevice[j];
670         }
671     }
672     return ANEURALNETWORKS_NO_ERROR;
673 }
674 
ANeuralNetworksCompilation_createForDevices(ANeuralNetworksModel * model,const ANeuralNetworksDevice * const * devices,uint32_t numDevices,ANeuralNetworksCompilation ** compilation)675 int ANeuralNetworksCompilation_createForDevices(ANeuralNetworksModel* model,
676                                                 const ANeuralNetworksDevice* const* devices,
677                                                 uint32_t numDevices,
678                                                 ANeuralNetworksCompilation** compilation) {
679     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_createForDevices");
680     if (model == nullptr || devices == nullptr || compilation == nullptr) {
681         LOG(ERROR) << "ANeuralNetworksCompilation_createForDevices passed a nullptr";
682         return ANEURALNETWORKS_UNEXPECTED_NULL;
683     }
684 
685     if (numDevices == 0) {
686         LOG(ERROR) << "ANeuralNetworksCompilation_createForDevices passed an empty device list";
687         return ANEURALNETWORKS_BAD_DATA;
688     }
689 
690     std::vector<std::shared_ptr<Device>> selectedDevices;
691     for (uint32_t i = 0; i < numDevices; i++) {
692         if (devices[i] == nullptr) {
693             LOG(ERROR)
694                     << "ANeuralNetworksCompilation_createForDevices passed a nullptr as a device";
695             return ANEURALNETWORKS_UNEXPECTED_NULL;
696         }
697         for (uint32_t j = i + 1; j < numDevices; j++) {
698             if (devices[i] == devices[j]) {
699                 LOG(ERROR)
700                         << "ANeuralNetworksCompilation_createForDevices passed duplicate devices";
701                 return ANEURALNETWORKS_BAD_DATA;
702             }
703         }
704         for (auto& device : DeviceManager::get()->getDrivers()) {
705             if (device.get() == reinterpret_cast<const Device*>(devices[i])) {
706                 // Find a match
707                 selectedDevices.push_back(device);
708                 break;
709             }
710         }
711     }
712 
713     if (selectedDevices.size() != numDevices) {
714         LOG(ERROR) << "ANeuralNetworksCompilation_createForDevices passed an invalid device set";
715         return ANEURALNETWORKS_BAD_DATA;
716     }
717     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
718     CompilationBuilder* c = nullptr;
719     // No CPU fallback when user specifies the list of devices manually.
720     int result = m->createCompilation(&c, selectedDevices, /* explicitDeviceList */ true);
721     *compilation = reinterpret_cast<ANeuralNetworksCompilation*>(c);
722     return result;
723 }
724 
ANeuralNetworksExecution_compute(ANeuralNetworksExecution * execution)725 int ANeuralNetworksExecution_compute(ANeuralNetworksExecution* execution) {
726     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_compute");
727     if (!execution) {
728         LOG(ERROR) << "ANeuralNetworksExecution_compute passed a nullptr";
729         return ANEURALNETWORKS_UNEXPECTED_NULL;
730     }
731     // TODO validate the rest
732 
733     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
734     return r->computeSynchronously();
735 }
736 
ANeuralNetworksExecution_setMeasureTiming(ANeuralNetworksExecution * execution,bool measure)737 int ANeuralNetworksExecution_setMeasureTiming(ANeuralNetworksExecution* execution, bool measure) {
738     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_setMeasureTiming");
739     if (!execution) {
740         LOG(ERROR) << "ANeuralNetworksExecution_setMeasureTiming passed a nullptr";
741         return ANEURALNETWORKS_UNEXPECTED_NULL;
742     }
743     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
744     return r->setMeasureTiming(measure);
745 }
746 
ANeuralNetworksExecution_getDuration(const ANeuralNetworksExecution * execution,int32_t durationCode,uint64_t * duration)747 int ANeuralNetworksExecution_getDuration(const ANeuralNetworksExecution* execution,
748                                          int32_t durationCode, uint64_t* duration) {
749     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_getDuration");
750     if (!execution || !duration) {
751         LOG(ERROR) << "ANeuralNetworksExecution_getDuration passed a nullptr";
752         return ANEURALNETWORKS_UNEXPECTED_NULL;
753     }
754     switch (durationCode) {
755         case ANEURALNETWORKS_DURATION_ON_HARDWARE:
756         case ANEURALNETWORKS_DURATION_IN_DRIVER:
757             break;
758         default:
759             LOG(ERROR) << "ANeuralNetworksExecution_getDuration passed a bad durationCode "
760                        << durationCode;
761             return ANEURALNETWORKS_BAD_DATA;
762     }
763     const ExecutionBuilder* r = reinterpret_cast<const ExecutionBuilder*>(execution);
764     return r->getDuration(durationCode, duration);
765 }
766 
ANeuralNetworksBurst_create(ANeuralNetworksCompilation * compilation,ANeuralNetworksBurst ** burst)767 int ANeuralNetworksBurst_create(ANeuralNetworksCompilation* compilation,
768                                 ANeuralNetworksBurst** burst) {
769     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksBurst_create");
770     if (!compilation || !burst) {
771         LOG(ERROR) << "ANeuralNetworksBurst_create passed a nullptr";
772         return ANEURALNETWORKS_UNEXPECTED_NULL;
773     }
774 
775     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
776     BurstBuilder* b = nullptr;
777     int result = c->createBurst(&b);
778     *burst = reinterpret_cast<ANeuralNetworksBurst*>(b);
779     return result;
780 }
781 
ANeuralNetworksBurst_free(ANeuralNetworksBurst * burst)782 void ANeuralNetworksBurst_free(ANeuralNetworksBurst* burst) {
783     NNTRACE_RT(NNTRACE_PHASE_TERMINATION, "ANeuralNetworksBurst_free");
784     // No validation.  Free of nullptr is valid.
785     BurstBuilder* b = reinterpret_cast<BurstBuilder*>(burst);
786     delete b;
787 }
788 
ANeuralNetworksExecution_burstCompute(ANeuralNetworksExecution * execution,ANeuralNetworksBurst * burst)789 int ANeuralNetworksExecution_burstCompute(ANeuralNetworksExecution* execution,
790                                           ANeuralNetworksBurst* burst) {
791     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_burstCompute");
792     if (!execution || !burst) {
793         LOG(ERROR) << "ANeuralNetworksExecution_burstCompute passed a nullptr";
794         return ANEURALNETWORKS_UNEXPECTED_NULL;
795     }
796 
797     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
798     BurstBuilder* b = reinterpret_cast<BurstBuilder*>(burst);
799 
800     if (r->getCompilation() != b->getCompilation()) {
801         LOG(ERROR) << "ANeuralNetworksBurst and ANeuralNetworksExecution "
802                       "used in ANeuralNetworksExecution_burstCompute must "
803                       "originate from the same ANeuralNetworksCompilation";
804         return ANEURALNETWORKS_BAD_DATA;
805     }
806 
807     const bool locked = b->tryLock();
808     if (!locked) {
809         LOG(ERROR) << "ANeuralNetworksBurst is already being used in another "
810                       "call to ANeuralNetworksExecution_burstCompute";
811         return ANEURALNETWORKS_BAD_STATE;
812     }
813 
814     const int n = r->burstCompute(b);
815     b->unlock();
816 
817     return n;
818 }
819 
ANeuralNetworksMemory_createFromFd(size_t size,int prot,int fd,size_t offset,ANeuralNetworksMemory ** memory)820 int ANeuralNetworksMemory_createFromFd(size_t size, int prot, int fd, size_t offset,
821                                        ANeuralNetworksMemory** memory) {
822     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksMemory_createFromFd");
823     *memory = nullptr;
824     std::unique_ptr<MemoryFd> m = std::make_unique<MemoryFd>();
825     if (m == nullptr) {
826         return ANEURALNETWORKS_OUT_OF_MEMORY;
827     }
828     int n = m->set(size, prot, fd, offset);
829     if (n != ANEURALNETWORKS_NO_ERROR) {
830         return n;
831     }
832     *memory = reinterpret_cast<ANeuralNetworksMemory*>(m.release());
833     return ANEURALNETWORKS_NO_ERROR;
834 }
835 
ANeuralNetworksMemory_createFromAHardwareBuffer(const AHardwareBuffer * ahwb,ANeuralNetworksMemory ** memory)836 int ANeuralNetworksMemory_createFromAHardwareBuffer(const AHardwareBuffer* ahwb,
837                                                     ANeuralNetworksMemory** memory) {
838     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksMemory_createFromAHardwareBuffer");
839     *memory = nullptr;
840     std::unique_ptr<MemoryAHWB> m = std::make_unique<MemoryAHWB>();
841     if (m == nullptr) {
842         return ANEURALNETWORKS_OUT_OF_MEMORY;
843     }
844     int n = m->set(ahwb);
845     if (n != ANEURALNETWORKS_NO_ERROR) {
846         return n;
847     }
848     *memory = reinterpret_cast<ANeuralNetworksMemory*>(m.release());
849     return ANEURALNETWORKS_NO_ERROR;
850 }
851 
ANeuralNetworksMemory_free(ANeuralNetworksMemory * memory)852 void ANeuralNetworksMemory_free(ANeuralNetworksMemory* memory) {
853     NNTRACE_RT(NNTRACE_PHASE_TERMINATION, "ANeuralNetworksMemory_free");
854     // No validation.  Free of nullptr is valid.
855     Memory* m = reinterpret_cast<Memory*>(memory);
856     delete m;
857 }
858 
ANeuralNetworksModel_create(ANeuralNetworksModel ** model)859 int ANeuralNetworksModel_create(ANeuralNetworksModel** model) {
860     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_create");
861     initVLogMask();
862     if (!model) {
863         LOG(ERROR) << "ANeuralNetworksModel_create passed a nullptr";
864         return ANEURALNETWORKS_UNEXPECTED_NULL;
865     }
866     ModelBuilder* m = new (std::nothrow) ModelBuilder();
867     if (m == nullptr) {
868         *model = nullptr;
869         return ANEURALNETWORKS_OUT_OF_MEMORY;
870     }
871     *model = reinterpret_cast<ANeuralNetworksModel*>(m);
872     return ANEURALNETWORKS_NO_ERROR;
873 }
874 
ANeuralNetworksModel_free(ANeuralNetworksModel * model)875 void ANeuralNetworksModel_free(ANeuralNetworksModel* model) {
876     NNTRACE_RT(NNTRACE_PHASE_TERMINATION, "ANeuralNetworksModel_free");
877     // No validation.  Free of nullptr is valid.
878     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
879     delete m;
880 }
881 
ANeuralNetworksModel_finish(ANeuralNetworksModel * model)882 int ANeuralNetworksModel_finish(ANeuralNetworksModel* model) {
883     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_finish");
884     if (!model) {
885         LOG(ERROR) << "ANeuralNetworksModel_finish passed a nullptr";
886         return ANEURALNETWORKS_UNEXPECTED_NULL;
887     }
888     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
889     return m->finish();
890 }
891 
ANeuralNetworksModel_addOperand(ANeuralNetworksModel * model,const ANeuralNetworksOperandType * type)892 int ANeuralNetworksModel_addOperand(ANeuralNetworksModel* model,
893                                     const ANeuralNetworksOperandType* type) {
894     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_addOperand");
895     if (!model || !type) {
896         LOG(ERROR) << "ANeuralNetworksModel_addOperand passed a nullptr";
897         return ANEURALNETWORKS_UNEXPECTED_NULL;
898     }
899     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
900     return m->addOperand(*type);
901 }
902 
ANeuralNetworksModel_setOperandValue(ANeuralNetworksModel * model,int32_t index,const void * buffer,size_t length)903 int ANeuralNetworksModel_setOperandValue(ANeuralNetworksModel* model, int32_t index,
904                                          const void* buffer, size_t length) {
905     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_setOperandValue");
906     if (!model || (!buffer && length != 0)) {
907         LOG(ERROR) << "ANeuralNetworksModel_setOperandValue passed a nullptr";
908         return ANEURALNETWORKS_UNEXPECTED_NULL;
909     }
910     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
911     return m->setOperandValue(index, buffer, length);
912 }
913 
ANeuralNetworksModel_setOperandValueFromMemory(ANeuralNetworksModel * model,int32_t index,const ANeuralNetworksMemory * memory,size_t offset,size_t length)914 int ANeuralNetworksModel_setOperandValueFromMemory(ANeuralNetworksModel* model, int32_t index,
915                                                    const ANeuralNetworksMemory* memory,
916                                                    size_t offset, size_t length) {
917     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_setOperandValueFromMemory");
918     if (!model || !memory) {
919         LOG(ERROR) << "ANeuralNetworksModel_setOperandValue passed a nullptr";
920         return ANEURALNETWORKS_UNEXPECTED_NULL;
921     }
922     const Memory* mem = reinterpret_cast<const Memory*>(memory);
923     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
924     return m->setOperandValueFromMemory(index, mem, offset, length);
925 }
926 
ANeuralNetworksModel_addOperation(ANeuralNetworksModel * model,ANeuralNetworksOperationType type,uint32_t inputCount,const uint32_t * inputs,uint32_t outputCount,const uint32_t * outputs)927 int ANeuralNetworksModel_addOperation(ANeuralNetworksModel* model,
928                                       ANeuralNetworksOperationType type, uint32_t inputCount,
929                                       const uint32_t* inputs, uint32_t outputCount,
930                                       const uint32_t* outputs) {
931     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_addOperation");
932     if (!model || !inputs || !outputs) {
933         LOG(ERROR) << "ANeuralNetworksModel_addOperation passed a nullptr";
934         return ANEURALNETWORKS_UNEXPECTED_NULL;
935     }
936     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
937     return m->addOperation(type, inputCount, inputs, outputCount, outputs);
938 }
939 
ANeuralNetworksModel_setOperandSymmPerChannelQuantParams(ANeuralNetworksModel * model,int32_t index,const ANeuralNetworksSymmPerChannelQuantParams * channelQuant)940 int ANeuralNetworksModel_setOperandSymmPerChannelQuantParams(
941         ANeuralNetworksModel* model, int32_t index,
942         const ANeuralNetworksSymmPerChannelQuantParams* channelQuant) {
943     NNTRACE_RT(NNTRACE_PHASE_PREPARATION,
944                "ANeuralNetworksModel_setOperandSymmPerChannelQuantParams");
945     if (!model || !channelQuant) {
946         LOG(ERROR) << "ANeuralNetworksModel_setOperandSymmPerChannelQuantParams passed a nullptr";
947         return ANEURALNETWORKS_UNEXPECTED_NULL;
948     }
949     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
950     return m->setOperandSymmPerChannelQuantParams(index, *channelQuant);
951 }
952 
ANeuralNetworksModel_identifyInputsAndOutputs(ANeuralNetworksModel * model,uint32_t inputCount,const uint32_t * inputs,uint32_t outputCount,const uint32_t * outputs)953 int ANeuralNetworksModel_identifyInputsAndOutputs(ANeuralNetworksModel* model, uint32_t inputCount,
954                                                   const uint32_t* inputs, uint32_t outputCount,
955                                                   const uint32_t* outputs) {
956     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_identifyInputsAndOutputs");
957     if (!model || !inputs || !outputs) {
958         LOG(ERROR) << ("ANeuralNetworksModel_identifyInputsAndOutputs passed a nullptr");
959         return ANEURALNETWORKS_UNEXPECTED_NULL;
960     }
961     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
962     return m->identifyInputsAndOutputs(inputCount, inputs, outputCount, outputs);
963 }
964 
ANeuralNetworksModel_relaxComputationFloat32toFloat16(ANeuralNetworksModel * model,bool allow)965 int ANeuralNetworksModel_relaxComputationFloat32toFloat16(ANeuralNetworksModel* model, bool allow) {
966     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_relaxComputationFloat32toFloat16");
967     if (!model) {
968         LOG(ERROR) << ("ANeuralNetworksModel_relaxComputationFloat32toFloat16 passed a nullptr");
969         return ANEURALNETWORKS_UNEXPECTED_NULL;
970     }
971     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
972     return m->relaxComputationFloat32toFloat16(allow);
973 }
974 
ANeuralNetworksCompilation_create(ANeuralNetworksModel * model,ANeuralNetworksCompilation ** compilation)975 int ANeuralNetworksCompilation_create(ANeuralNetworksModel* model,
976                                       ANeuralNetworksCompilation** compilation) {
977     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_create");
978     if (!model || !compilation) {
979         LOG(ERROR) << "ANeuralNetworksCompilation_create passed a nullptr";
980         return ANEURALNETWORKS_UNEXPECTED_NULL;
981     }
982 
983     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
984     CompilationBuilder* c = nullptr;
985     int result = m->createCompilation(&c, DeviceManager::get()->getDrivers());
986     *compilation = reinterpret_cast<ANeuralNetworksCompilation*>(c);
987     return result;
988 }
989 
ANeuralNetworksCompilation_free(ANeuralNetworksCompilation * compilation)990 void ANeuralNetworksCompilation_free(ANeuralNetworksCompilation* compilation) {
991     NNTRACE_RT(NNTRACE_PHASE_TERMINATION, "ANeuralNetworksCompilation_free");
992     // No validation.  Free of nullptr is valid.
993     // TODO specification says that a compilation-in-flight can be deleted
994     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
995     delete c;
996 }
997 
ANeuralNetworksCompilation_setPreference(ANeuralNetworksCompilation * compilation,int32_t preference)998 int ANeuralNetworksCompilation_setPreference(ANeuralNetworksCompilation* compilation,
999                                              int32_t preference) {
1000     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_setPreference");
1001     if (!compilation) {
1002         LOG(ERROR) << "ANeuralNetworksCompilation_setPreference passed a nullptr";
1003         return ANEURALNETWORKS_UNEXPECTED_NULL;
1004     }
1005     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1006     return c->setPreference(preference);
1007 }
1008 
ANeuralNetworksCompilation_setCaching(ANeuralNetworksCompilation * compilation,const char * cacheDir,const uint8_t * token)1009 int ANeuralNetworksCompilation_setCaching(ANeuralNetworksCompilation* compilation,
1010                                           const char* cacheDir, const uint8_t* token) {
1011     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_setCaching");
1012     if (!compilation || !cacheDir || !token) {
1013         LOG(ERROR) << "ANeuralNetworksCompilation_setCaching passed a nullptr";
1014         return ANEURALNETWORKS_UNEXPECTED_NULL;
1015     }
1016     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1017     return c->setCaching(cacheDir, token);
1018 }
1019 
ANeuralNetworksCompilation_finish(ANeuralNetworksCompilation * compilation)1020 int ANeuralNetworksCompilation_finish(ANeuralNetworksCompilation* compilation) {
1021     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_finish");
1022     if (!compilation) {
1023         LOG(ERROR) << "ANeuralNetworksCompilation_finish passed a nullptr";
1024         return ANEURALNETWORKS_UNEXPECTED_NULL;
1025     }
1026     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1027     return c->finish();
1028 }
1029 
ANeuralNetworksExecution_create(ANeuralNetworksCompilation * compilation,ANeuralNetworksExecution ** execution)1030 int ANeuralNetworksExecution_create(ANeuralNetworksCompilation* compilation,
1031                                     ANeuralNetworksExecution** execution) {
1032     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_create");
1033     if (!compilation || !execution) {
1034         LOG(ERROR) << "ANeuralNetworksExecution_create passed a nullptr";
1035         return ANEURALNETWORKS_UNEXPECTED_NULL;
1036     }
1037 
1038     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1039     ExecutionBuilder* r = nullptr;
1040     int result = c->createExecution(&r);
1041     *execution = reinterpret_cast<ANeuralNetworksExecution*>(r);
1042     return result;
1043 }
1044 
ANeuralNetworksExecution_free(ANeuralNetworksExecution * execution)1045 void ANeuralNetworksExecution_free(ANeuralNetworksExecution* execution) {
1046     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_free");
1047     // TODO specification says that an execution-in-flight can be deleted
1048     // No validation.  Free of nullptr is valid.
1049     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1050     delete r;
1051 }
1052 
ANeuralNetworksExecution_getOutputOperandRank(ANeuralNetworksExecution * execution,int32_t index,uint32_t * rank)1053 int ANeuralNetworksExecution_getOutputOperandRank(ANeuralNetworksExecution* execution,
1054                                                   int32_t index, uint32_t* rank) {
1055     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_getOutputOperandRank");
1056     if (!execution || !rank) {
1057         LOG(ERROR) << "ANeuralNetworksExecution_getOutputOperandRank passed a nullptr";
1058         return ANEURALNETWORKS_UNEXPECTED_NULL;
1059     }
1060     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1061     return r->getOutputOperandRank(index, rank);
1062 }
1063 
ANeuralNetworksExecution_getOutputOperandDimensions(ANeuralNetworksExecution * execution,int32_t index,uint32_t * dimensions)1064 int ANeuralNetworksExecution_getOutputOperandDimensions(ANeuralNetworksExecution* execution,
1065                                                         int32_t index, uint32_t* dimensions) {
1066     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_getOutputOperandDimensions");
1067     if (!execution || !dimensions) {
1068         LOG(ERROR) << "ANeuralNetworksExecution_getOutputOperandDimensions passed a nullptr";
1069         return ANEURALNETWORKS_UNEXPECTED_NULL;
1070     }
1071     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1072     return r->getOutputOperandDimensions(index, dimensions);
1073 }
1074 
ANeuralNetworksExecution_setInput(ANeuralNetworksExecution * execution,int32_t index,const ANeuralNetworksOperandType * type,const void * buffer,size_t length)1075 int ANeuralNetworksExecution_setInput(ANeuralNetworksExecution* execution, int32_t index,
1076                                       const ANeuralNetworksOperandType* type, const void* buffer,
1077                                       size_t length) {
1078     NNTRACE_RT(NNTRACE_PHASE_INPUTS_AND_OUTPUTS, "ANeuralNetworksExecution_setInput");
1079     if (!execution || (!buffer && length != 0)) {
1080         LOG(ERROR) << "ANeuralNetworksExecution_setInput passed a nullptr";
1081         return ANEURALNETWORKS_UNEXPECTED_NULL;
1082     }
1083     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1084     return r->setInput(index, type, buffer, length);
1085 }
1086 
ANeuralNetworksExecution_setInputFromMemory(ANeuralNetworksExecution * execution,int32_t index,const ANeuralNetworksOperandType * type,const ANeuralNetworksMemory * memory,size_t offset,size_t length)1087 int ANeuralNetworksExecution_setInputFromMemory(ANeuralNetworksExecution* execution, int32_t index,
1088                                                 const ANeuralNetworksOperandType* type,
1089                                                 const ANeuralNetworksMemory* memory, size_t offset,
1090                                                 size_t length) {
1091     NNTRACE_RT(NNTRACE_PHASE_INPUTS_AND_OUTPUTS, "ANeuralNetworksExecution_setInputFromMemory");
1092     if (!execution || !memory) {
1093         LOG(ERROR) << "ANeuralNetworksExecution_setInputFromMemory passed a nullptr";
1094         return ANEURALNETWORKS_UNEXPECTED_NULL;
1095     }
1096 
1097     const Memory* m = reinterpret_cast<const Memory*>(memory);
1098     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1099     return r->setInputFromMemory(index, type, m, offset, length);
1100 }
1101 
ANeuralNetworksExecution_setOutput(ANeuralNetworksExecution * execution,int32_t index,const ANeuralNetworksOperandType * type,void * buffer,size_t length)1102 int ANeuralNetworksExecution_setOutput(ANeuralNetworksExecution* execution, int32_t index,
1103                                        const ANeuralNetworksOperandType* type, void* buffer,
1104                                        size_t length) {
1105     NNTRACE_RT(NNTRACE_PHASE_INPUTS_AND_OUTPUTS, "ANeuralNetworksExecution_setOutput");
1106     if (!execution || (!buffer && length != 0)) {
1107         LOG(ERROR) << "ANeuralNetworksExecution_setOutput passed a nullptr";
1108         return ANEURALNETWORKS_UNEXPECTED_NULL;
1109     }
1110     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1111     return r->setOutput(index, type, buffer, length);
1112 }
1113 
ANeuralNetworksExecution_setOutputFromMemory(ANeuralNetworksExecution * execution,int32_t index,const ANeuralNetworksOperandType * type,const ANeuralNetworksMemory * memory,size_t offset,size_t length)1114 int ANeuralNetworksExecution_setOutputFromMemory(ANeuralNetworksExecution* execution, int32_t index,
1115                                                  const ANeuralNetworksOperandType* type,
1116                                                  const ANeuralNetworksMemory* memory, size_t offset,
1117                                                  size_t length) {
1118     NNTRACE_RT(NNTRACE_PHASE_INPUTS_AND_OUTPUTS, "ANeuralNetworksExecution_setOutputFromMemory");
1119     if (!execution || !memory) {
1120         LOG(ERROR) << "ANeuralNetworksExecution_setOutputFromMemory passed a nullptr";
1121         return ANEURALNETWORKS_UNEXPECTED_NULL;
1122     }
1123 
1124     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1125     const Memory* m = reinterpret_cast<const Memory*>(memory);
1126     return r->setOutputFromMemory(index, type, m, offset, length);
1127 }
1128 
ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution * execution,ANeuralNetworksEvent ** event)1129 int ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution* execution,
1130                                           ANeuralNetworksEvent** event) {
1131     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_startCompute");
1132     if (!execution || !event) {
1133         LOG(ERROR) << "ANeuralNetworksExecution_startCompute passed a nullptr";
1134         return ANEURALNETWORKS_UNEXPECTED_NULL;
1135     }
1136     // TODO validate the rest
1137 
1138     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1139 
1140     // Dynamically allocate an sp to wrap an ExecutionCallback, seen in the NN
1141     // API as an abstract event object. The sp<ExecutionCallback> object is
1142     // returned when the execution has been successfully launched, otherwise a
1143     // nullptr is returned. The sp is used for ref-counting purposes. Without
1144     // it, the HIDL service could attempt to communicate with a dead callback
1145     // object.
1146     std::unique_ptr<sp<ExecutionCallback>> e = std::make_unique<sp<ExecutionCallback>>();
1147     *event = nullptr;
1148 
1149     int n = r->computeAsynchronously(e.get());
1150     if (n != ANEURALNETWORKS_NO_ERROR) {
1151         return n;
1152     }
1153     *event = reinterpret_cast<ANeuralNetworksEvent*>(e.release());
1154     return ANEURALNETWORKS_NO_ERROR;
1155 }
1156 
ANeuralNetworksEvent_wait(ANeuralNetworksEvent * event)1157 int ANeuralNetworksEvent_wait(ANeuralNetworksEvent* event) {
1158     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksEvent_wait");
1159     if (event == nullptr) {
1160         LOG(ERROR) << "ANeuralNetworksEvent_wait passed a nullptr";
1161         return ANEURALNETWORKS_UNEXPECTED_NULL;
1162     }
1163 
1164     sp<ExecutionCallback>* e = reinterpret_cast<sp<ExecutionCallback>*>(event);
1165     (*e)->wait();
1166     return convertErrorStatusToResultCode((*e)->getStatus());
1167 }
1168 
ANeuralNetworksEvent_free(ANeuralNetworksEvent * event)1169 void ANeuralNetworksEvent_free(ANeuralNetworksEvent* event) {
1170     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksEvent_free");
1171     // No validation.  Free of nullptr is valid.
1172     if (event) {
1173         sp<ExecutionCallback>* e = reinterpret_cast<sp<ExecutionCallback>*>(event);
1174         (*e)->wait();
1175         delete e;
1176     }
1177 }
1178 
ANeuralNetworksDevice_getExtensionSupport(const ANeuralNetworksDevice * device,const char * extensionName,bool * isExtensionSupported)1179 int ANeuralNetworksDevice_getExtensionSupport(const ANeuralNetworksDevice* device,
1180                                               const char* extensionName,
1181                                               bool* isExtensionSupported) {
1182     if (device == nullptr || extensionName == nullptr || isExtensionSupported == nullptr) {
1183         LOG(ERROR) << "ANeuralNetworksDevice_getExtensionSupport passed a nullptr";
1184         return ANEURALNETWORKS_UNEXPECTED_NULL;
1185     }
1186 
1187     Device* d = reinterpret_cast<Device*>(const_cast<ANeuralNetworksDevice*>(device));
1188     hidl_vec<Extension> supportedExtensions = d->getSupportedExtensions();
1189 
1190     *isExtensionSupported = false;
1191     for (const Extension& supportedExtension : supportedExtensions) {
1192         if (supportedExtension.name == extensionName) {
1193             *isExtensionSupported = true;
1194             break;
1195         }
1196     }
1197 
1198     return ANEURALNETWORKS_NO_ERROR;
1199 }
1200 
ANeuralNetworksModel_getExtensionOperandType(ANeuralNetworksModel * model,const char * extensionName,uint16_t operandCodeWithinExtension,int32_t * type)1201 int ANeuralNetworksModel_getExtensionOperandType(ANeuralNetworksModel* model,
1202                                                  const char* extensionName,
1203                                                  uint16_t operandCodeWithinExtension,
1204                                                  int32_t* type) {
1205     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_getExtensionOperandType");
1206     if (!model || !extensionName || !type) {
1207         LOG(ERROR) << "ANeuralNetworksModel_getExtensionOperandType passed a nullptr";
1208         return ANEURALNETWORKS_UNEXPECTED_NULL;
1209     }
1210     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1211     return m->getExtensionType(extensionName, operandCodeWithinExtension, type);
1212 }
1213 
ANeuralNetworksModel_getExtensionOperationType(ANeuralNetworksModel * model,const char * extensionName,uint16_t operationCodeWithinExtension,ANeuralNetworksOperationType * type)1214 int ANeuralNetworksModel_getExtensionOperationType(ANeuralNetworksModel* model,
1215                                                    const char* extensionName,
1216                                                    uint16_t operationCodeWithinExtension,
1217                                                    ANeuralNetworksOperationType* type) {
1218     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_getExtensionOperationType");
1219     if (!model || !extensionName || !type) {
1220         LOG(ERROR) << "ANeuralNetworksModel_getExtensionOperationType passed a nullptr";
1221         return ANEURALNETWORKS_UNEXPECTED_NULL;
1222     }
1223     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1224     return m->getExtensionType(extensionName, operationCodeWithinExtension, type);
1225 }
1226 
ANeuralNetworksModel_setOperandExtensionData(ANeuralNetworksModel * model,int32_t index,const void * data,size_t length)1227 int ANeuralNetworksModel_setOperandExtensionData(ANeuralNetworksModel* model, int32_t index,
1228                                                  const void* data, size_t length) {
1229     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_setOperandExtensionData");
1230     if (!model || (!data && length != 0)) {
1231         LOG(ERROR) << "ANeuralNetworksModel_setOperandExtensionData passed a nullptr";
1232         return ANEURALNETWORKS_UNEXPECTED_NULL;
1233     }
1234     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1235     return m->setOperandExtensionData(index, data, length);
1236 }
1237