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 <ControlFlow.h>
26 #include <LegacyUtils.h>
27 #include <MetaModel.h>
28 #include <Tracing.h>
29 #include <nnapi/Types.h>
30 
31 #include <algorithm>
32 #include <cstddef>
33 #include <memory>
34 #include <utility>
35 #include <vector>
36 
37 #include "BurstBuilder.h"
38 #include "CompilationBuilder.h"
39 #include "Event.h"
40 #include "ExecutionBuilder.h"
41 #include "ExecutionCallback.h"
42 #include "Manager.h"
43 #include "Memory.h"
44 #include "ModelBuilder.h"
45 #include "NeuralNetworksExtensions.h"
46 #include "NeuralNetworksOEM.h"
47 #include "Telemetry.h"
48 
49 #ifdef NN_COMPATIBILITY_LIBRARY_BUILD
50 #include "NeuralNetworksSupportLibraryImpl.h"
51 #endif  // NN_COMPATIBILITY_LIBRARY_BUILD
52 
53 using namespace android::nn;
54 
55 // Make sure the constants defined in the header files have not changed values.
56 // IMPORTANT: When adding new values, update kNumberOfDataTypes or kNumberOfDataTypesOEM
57 // in Utils.h.
58 static_assert(ANEURALNETWORKS_FLOAT32 == 0, "ANEURALNETWORKS_FLOAT32 has changed");
59 static_assert(ANEURALNETWORKS_INT32 == 1, "ANEURALNETWORKS_INT32 has changed");
60 static_assert(ANEURALNETWORKS_UINT32 == 2, "ANEURALNETWORKS_UINT32 has changed");
61 static_assert(ANEURALNETWORKS_TENSOR_FLOAT32 == 3, "ANEURALNETWORKS_TENSOR_FLOAT32 has changed");
62 static_assert(ANEURALNETWORKS_TENSOR_INT32 == 4, "ANEURALNETWORKS_TENSOR_INT32 has changed");
63 static_assert(ANEURALNETWORKS_TENSOR_QUANT8_ASYMM == 5,
64               "ANEURALNETWORKS_TENSOR_QUANT8_ASYMM has changed");
65 static_assert(ANEURALNETWORKS_BOOL == 6, "ANEURALNETWORKS_BOOL has changed");
66 static_assert(ANEURALNETWORKS_TENSOR_QUANT16_SYMM == 7,
67               "ANEURALNETWORKS_TENSOR_QUANT16_SYMM has changed");
68 static_assert(ANEURALNETWORKS_TENSOR_FLOAT16 == 8, "ANEURALNETWORKS_TENSOR_FLOAT16 has changed");
69 static_assert(ANEURALNETWORKS_TENSOR_BOOL8 == 9, "ANEURALNETWORKS_TENSOR_BOOL8 has changed");
70 static_assert(ANEURALNETWORKS_FLOAT16 == 10, "ANEURALNETWORKS_FLOAT16 has changed");
71 static_assert(ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL == 11,
72               "ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL has changed");
73 static_assert(ANEURALNETWORKS_TENSOR_QUANT16_ASYMM == 12,
74               "ANEURALNETWORKS_TENSOR_QUANT16_ASYMM has changed");
75 static_assert(ANEURALNETWORKS_TENSOR_QUANT8_SYMM == 13,
76               "ANEURALNETWORKS_TENSOR_QUANT8_SYMM has changed");
77 static_assert(ANEURALNETWORKS_OEM_SCALAR == 10000, "ANEURALNETWORKS_OEM_SCALAR has changed");
78 static_assert(ANEURALNETWORKS_TENSOR_OEM_BYTE == 10001,
79               "ANEURALNETWORKS_TENSOR_OEM_BYTE has changed");
80 
81 // IMPORTANT: When adding new values, update kNumberOfOperationTypes or
82 // kNumberOfOperationTypesOEMin Utils.h.
83 static_assert(ANEURALNETWORKS_ADD == 0, "ANEURALNETWORKS_ADD has changed");
84 static_assert(ANEURALNETWORKS_AVERAGE_POOL_2D == 1, "ANEURALNETWORKS_AVERAGE_POOL_2D has changed");
85 static_assert(ANEURALNETWORKS_CONCATENATION == 2, "ANEURALNETWORKS_CONCATENATION has changed");
86 static_assert(ANEURALNETWORKS_CONV_2D == 3, "ANEURALNETWORKS_CONV_2D has changed");
87 static_assert(ANEURALNETWORKS_DEPTHWISE_CONV_2D == 4,
88               "ANEURALNETWORKS_DEPTHWISE_CONV_2D has changed");
89 static_assert(ANEURALNETWORKS_DEPTH_TO_SPACE == 5, "ANEURALNETWORKS_DEPTH_TO_SPACE has changed");
90 static_assert(ANEURALNETWORKS_DEQUANTIZE == 6, "ANEURALNETWORKS_DEQUANTIZE has changed");
91 static_assert(ANEURALNETWORKS_EMBEDDING_LOOKUP == 7,
92               "ANEURALNETWORKS_EMBEDDING_LOOKUP has changed");
93 static_assert(ANEURALNETWORKS_FLOOR == 8, "ANEURALNETWORKS_FLOOR has changed");
94 static_assert(ANEURALNETWORKS_FULLY_CONNECTED == 9, "ANEURALNETWORKS_FULLY_CONNECTED has changed");
95 static_assert(ANEURALNETWORKS_HASHTABLE_LOOKUP == 10,
96               "ANEURALNETWORKS_HASHTABLE_LOOKUP has changed");
97 static_assert(ANEURALNETWORKS_L2_NORMALIZATION == 11,
98               "ANEURALNETWORKS_L2_NORMALIZATION has changed");
99 static_assert(ANEURALNETWORKS_L2_POOL_2D == 12, "ANEURALNETWORKS_L2_POOL has changed");
100 static_assert(ANEURALNETWORKS_LOCAL_RESPONSE_NORMALIZATION == 13,
101               "ANEURALNETWORKS_LOCAL_RESPONSE_NORMALIZATION has changed");
102 static_assert(ANEURALNETWORKS_LOGISTIC == 14, "ANEURALNETWORKS_LOGISTIC has changed");
103 static_assert(ANEURALNETWORKS_LSH_PROJECTION == 15, "ANEURALNETWORKS_LSH_PROJECTION has changed");
104 static_assert(ANEURALNETWORKS_LSTM == 16, "ANEURALNETWORKS_LSTM has changed");
105 static_assert(ANEURALNETWORKS_MAX_POOL_2D == 17, "ANEURALNETWORKS_MAX_POOL has changed");
106 static_assert(ANEURALNETWORKS_MUL == 18, "ANEURALNETWORKS_MUL has changed");
107 static_assert(ANEURALNETWORKS_RELU == 19, "ANEURALNETWORKS_RELU has changed");
108 static_assert(ANEURALNETWORKS_RELU1 == 20, "ANEURALNETWORKS_RELU1 has changed");
109 static_assert(ANEURALNETWORKS_RELU6 == 21, "ANEURALNETWORKS_RELU6 has changed");
110 static_assert(ANEURALNETWORKS_RESHAPE == 22, "ANEURALNETWORKS_RESHAPE has changed");
111 static_assert(ANEURALNETWORKS_RESIZE_BILINEAR == 23, "ANEURALNETWORKS_RESIZE_BILINEAR has changed");
112 static_assert(ANEURALNETWORKS_RNN == 24, "ANEURALNETWORKS_RNN has changed");
113 static_assert(ANEURALNETWORKS_SOFTMAX == 25, "ANEURALNETWORKS_SOFTMAX has changed");
114 static_assert(ANEURALNETWORKS_SPACE_TO_DEPTH == 26, "ANEURALNETWORKS_SPACE_TO_DEPTH has changed");
115 static_assert(ANEURALNETWORKS_SVDF == 27, "ANEURALNETWORKS_SVDF has changed");
116 static_assert(ANEURALNETWORKS_TANH == 28, "ANEURALNETWORKS_TANH has changed");
117 
118 static_assert(ANEURALNETWORKS_BATCH_TO_SPACE_ND == 29,
119               "ANEURALNETWORKS_BATCH_TO_SPACE_ND has changed");
120 static_assert(ANEURALNETWORKS_DIV == 30, "ANEURALNETWORKS_DIV has changed");
121 static_assert(ANEURALNETWORKS_MEAN == 31, "ANEURALNETWORKS_MEAN has changed");
122 static_assert(ANEURALNETWORKS_PAD == 32, "ANEURALNETWORKS_PAD has changed");
123 static_assert(ANEURALNETWORKS_SPACE_TO_BATCH_ND == 33,
124               "ANEURALNETWORKS_SPACE_TO_BATCH_ND has changed");
125 static_assert(ANEURALNETWORKS_SQUEEZE == 34, "ANEURALNETWORKS_SQUEEZE has changed");
126 static_assert(ANEURALNETWORKS_STRIDED_SLICE == 35, "ANEURALNETWORKS_STRIDED_SLICE has changed");
127 static_assert(ANEURALNETWORKS_SUB == 36, "ANEURALNETWORKS_TANH has changed");
128 static_assert(ANEURALNETWORKS_TRANSPOSE == 37, "ANEURALNETWORKS_TRANSPOSE has changed");
129 
130 static_assert(ANEURALNETWORKS_ABS == 38, "ANEURALNETWORKS_ABS has changed");
131 static_assert(ANEURALNETWORKS_ARGMAX == 39, "ANEURALNETWORKS_ARGMAX has changed");
132 static_assert(ANEURALNETWORKS_ARGMIN == 40, "ANEURALNETWORKS_ARGMIN has changed");
133 static_assert(ANEURALNETWORKS_AXIS_ALIGNED_BBOX_TRANSFORM == 41,
134               "ANEURALNETWORKS_AXIS_ALIGNED_BBOX_TRANSFORM has changed");
135 static_assert(ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_LSTM == 42,
136               "ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_LSTM has changed");
137 static_assert(ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_RNN == 43,
138               "ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_RNN has changed");
139 static_assert(ANEURALNETWORKS_BOX_WITH_NMS_LIMIT == 44,
140               "ANEURALNETWORKS_BOX_WITH_NMS_LIMIT has changed");
141 static_assert(ANEURALNETWORKS_CAST == 45, "ANEURALNETWORKS_CAST has changed");
142 static_assert(ANEURALNETWORKS_CHANNEL_SHUFFLE == 46, "ANEURALNETWORKS_CHANNEL_SHUFFLE has changed");
143 static_assert(ANEURALNETWORKS_DETECTION_POSTPROCESSING == 47,
144               "ANEURALNETWORKS_DETECTION_POSTPROCESSING has changed");
145 static_assert(ANEURALNETWORKS_EQUAL == 48, "ANEURALNETWORKS_EQUAL has changed");
146 static_assert(ANEURALNETWORKS_EXP == 49, "ANEURALNETWORKS_EXP has changed");
147 static_assert(ANEURALNETWORKS_EXPAND_DIMS == 50, "ANEURALNETWORKS_EXPAND_DIMS has changed");
148 static_assert(ANEURALNETWORKS_GATHER == 51, "ANEURALNETWORKS_GATHER has changed");
149 static_assert(ANEURALNETWORKS_GENERATE_PROPOSALS == 52,
150               "ANEURALNETWORKS_GENERATE_PROPOSALS has changed");
151 static_assert(ANEURALNETWORKS_GREATER == 53, "ANEURALNETWORKS_GREATER has changed");
152 static_assert(ANEURALNETWORKS_GREATER_EQUAL == 54, "ANEURALNETWORKS_GREATER_EQUAL has changed");
153 static_assert(ANEURALNETWORKS_GROUPED_CONV_2D == 55, "ANEURALNETWORKS_GROUPED_CONV_2D has changed");
154 static_assert(ANEURALNETWORKS_HEATMAP_MAX_KEYPOINT == 56,
155               "ANEURALNETWORKS_HEATMAP_MAX_KEYPOINT has changed");
156 static_assert(ANEURALNETWORKS_INSTANCE_NORMALIZATION == 57,
157               "ANEURALNETWORKS_INSTANCE_NORMALIZATION has changed");
158 static_assert(ANEURALNETWORKS_LESS == 58, "ANEURALNETWORKS_LESS has changed");
159 static_assert(ANEURALNETWORKS_LESS_EQUAL == 59, "ANEURALNETWORKS_LESS_EQUAL has changed");
160 static_assert(ANEURALNETWORKS_LOG == 60, "ANEURALNETWORKS_LOG has changed");
161 static_assert(ANEURALNETWORKS_LOGICAL_AND == 61, "ANEURALNETWORKS_LOGICAL_AND has changed");
162 static_assert(ANEURALNETWORKS_LOGICAL_NOT == 62, "ANEURALNETWORKS_LOGICAL_NOT has changed");
163 static_assert(ANEURALNETWORKS_LOGICAL_OR == 63, "ANEURALNETWORKS_LOGICAL_OR has changed");
164 static_assert(ANEURALNETWORKS_LOG_SOFTMAX == 64, "ANEURALNETWORKS_LOG_SOFTMAX has changed");
165 static_assert(ANEURALNETWORKS_MAXIMUM == 65, "ANEURALNETWORKS_MAXIMUM has changed");
166 static_assert(ANEURALNETWORKS_MINIMUM == 66, "ANEURALNETWORKS_MINIMUM has changed");
167 static_assert(ANEURALNETWORKS_NEG == 67, "ANEURALNETWORKS_NEG has changed");
168 static_assert(ANEURALNETWORKS_NOT_EQUAL == 68, "ANEURALNETWORKS_NOT_EQUAL has changed");
169 static_assert(ANEURALNETWORKS_PAD_V2 == 69, "ANEURALNETWORKS_PAD_V2 has changed");
170 static_assert(ANEURALNETWORKS_POW == 70, "ANEURALNETWORKS_POW has changed");
171 static_assert(ANEURALNETWORKS_PRELU == 71, "ANEURALNETWORKS_PRELU has changed");
172 static_assert(ANEURALNETWORKS_QUANTIZE == 72, "ANEURALNETWORKS_QUANTIZE has changed");
173 static_assert(ANEURALNETWORKS_QUANTIZED_16BIT_LSTM == 73,
174               "ANEURALNETWORKS_QUANTIZED_16BIT_LSTM has changed");
175 static_assert(ANEURALNETWORKS_RANDOM_MULTINOMIAL == 74,
176               "ANEURALNETWORKS_RANDOM_MULTINOMIAL has changed");
177 static_assert(ANEURALNETWORKS_REDUCE_ALL == 75, "ANEURALNETWORKS_REDUCE_ALL has changed");
178 static_assert(ANEURALNETWORKS_REDUCE_ANY == 76, "ANEURALNETWORKS_REDUCE_ANY has changed");
179 static_assert(ANEURALNETWORKS_REDUCE_MAX == 77, "ANEURALNETWORKS_REDUCE_MAX has changed");
180 static_assert(ANEURALNETWORKS_REDUCE_MIN == 78, "ANEURALNETWORKS_REDUCE_MIN has changed");
181 static_assert(ANEURALNETWORKS_REDUCE_PROD == 79, "ANEURALNETWORKS_REDUCE_PROD has changed");
182 static_assert(ANEURALNETWORKS_REDUCE_SUM == 80, "ANEURALNETWORKS_REDUCE_SUM has changed");
183 static_assert(ANEURALNETWORKS_ROI_ALIGN == 81, "ANEURALNETWORKS_ROI_ALIGN has changed");
184 static_assert(ANEURALNETWORKS_ROI_POOLING == 82, "ANEURALNETWORKS_ROI_POOLING has changed");
185 static_assert(ANEURALNETWORKS_RSQRT == 83, "ANEURALNETWORKS_RSQRT has changed");
186 static_assert(ANEURALNETWORKS_SELECT == 84, "ANEURALNETWORKS_SELECT has changed");
187 static_assert(ANEURALNETWORKS_SIN == 85, "ANEURALNETWORKS_SIN has changed");
188 static_assert(ANEURALNETWORKS_SLICE == 86, "ANEURALNETWORKS_SLICE has changed");
189 static_assert(ANEURALNETWORKS_SPLIT == 87, "ANEURALNETWORKS_SPLIT has changed");
190 static_assert(ANEURALNETWORKS_SQRT == 88, "ANEURALNETWORKS_SQRT has changed");
191 static_assert(ANEURALNETWORKS_TILE == 89, "ANEURALNETWORKS_TILE has changed");
192 static_assert(ANEURALNETWORKS_TOPK_V2 == 90, "ANEURALNETWORKS_TOPK_V2 has changed");
193 static_assert(ANEURALNETWORKS_TRANSPOSE_CONV_2D == 91,
194               "ANEURALNETWORKS_TRANSPOSE_CONV_2D has changed");
195 static_assert(ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_LSTM == 92,
196               "ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_LSTM has changed");
197 static_assert(ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_RNN == 93,
198               "ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_RNN has changed");
199 static_assert(ANEURALNETWORKS_RESIZE_NEAREST_NEIGHBOR == 94,
200               "ANEURALNETWORKS_RESIZE_NEAREST_NEIGHBOR has changed");
201 static_assert(ANEURALNETWORKS_QUANTIZED_LSTM == 95, "ANEURALNETWORKS_QUANTIZED_LSTM has changed");
202 static_assert(ANEURALNETWORKS_IF == 96, "ANEURALNETWORKS_IF has changed");
203 static_assert(ANEURALNETWORKS_WHILE == 97, "ANEURALNETWORKS_WHILE has changed");
204 static_assert(ANEURALNETWORKS_ELU == 98, "ANEURALNETWORKS_ELU has changed");
205 static_assert(ANEURALNETWORKS_HARD_SWISH == 99, "ANEURALNETWORKS_HARD_SWISH has changed");
206 static_assert(ANEURALNETWORKS_FILL == 100, "ANEURALNETWORKS_FILL has changed");
207 static_assert(ANEURALNETWORKS_RANK == 101, "ANEURALNETWORKS_RANK has changed");
208 static_assert(ANEURALNETWORKS_BATCH_MATMUL == 102, "ANEURALNETWORKS_BATCH_MATMUL has changed");
209 static_assert(ANEURALNETWORKS_PACK == 103, "ANEURALNETWORKS_PACK has changed");
210 static_assert(ANEURALNETWORKS_MIRROR_PAD == 104, "ANEURALNETWORKS_MIRROR_PAD has changed");
211 static_assert(ANEURALNETWORKS_REVERSE == 105, "ANEURALNETWORKS_REVERSE has changed");
212 static_assert(ANEURALNETWORKS_OEM_OPERATION == 10000, "ANEURALNETWORKS_OEM_OPERATION has changed");
213 
214 static_assert(ANEURALNETWORKS_FUSED_NONE == 0, "ANEURALNETWORKS_FUSED_NONE has changed");
215 static_assert(ANEURALNETWORKS_FUSED_RELU == 1, "ANEURALNETWORKS_FUSED_RELU has changed");
216 static_assert(ANEURALNETWORKS_FUSED_RELU1 == 2, "ANEURALNETWORKS_FUSED_RELU1 has changed");
217 static_assert(ANEURALNETWORKS_FUSED_RELU6 == 3, "ANEURALNETWORKS_FUSED_RELU6 has changed");
218 
219 static_assert(ANEURALNETWORKS_PREFER_LOW_POWER == 0,
220               "ANEURALNETWORKS_PREFER_LOW_POWER has changed");
221 static_assert(ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER == 1,
222               "ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER has changed");
223 static_assert(ANEURALNETWORKS_PREFER_SUSTAINED_SPEED == 2,
224               "ANEURALNETWORKS_PREFER_SUSTAINED_SPEED has changed");
225 
226 static_assert(ANEURALNETWORKS_NO_ERROR == 0, "ANEURALNETWORKS_NO_ERROR has changed");
227 static_assert(ANEURALNETWORKS_OUT_OF_MEMORY == 1, "ANEURALNETWORKS_OUT_OF_MEMORY has changed");
228 static_assert(ANEURALNETWORKS_INCOMPLETE == 2, "ANEURALNETWORKS_INCOMPLETE has changed");
229 static_assert(ANEURALNETWORKS_UNEXPECTED_NULL == 3, "ANEURALNETWORKS_UNEXPECTED_NULL has changed");
230 static_assert(ANEURALNETWORKS_BAD_DATA == 4, "ANEURALNETWORKS_BAD_DATA has changed");
231 static_assert(ANEURALNETWORKS_OP_FAILED == 5, "ANEURALNETWORKS_OP_FAILED has changed");
232 static_assert(ANEURALNETWORKS_BAD_STATE == 6, "ANEURALNETWORKS_BAD_STATE has changed");
233 static_assert(ANEURALNETWORKS_UNMAPPABLE == 7, "ANEURALNETWORKS_UNMAPPABLE has changed");
234 static_assert(ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE == 8,
235               "ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE has changed");
236 static_assert(ANEURALNETWORKS_UNAVAILABLE_DEVICE == 9,
237               "ANEURALNETWORKS_UNAVAILABLE_DEVICE has changed");
238 static_assert(ANEURALNETWORKS_MISSED_DEADLINE_TRANSIENT == 10,
239               "ANEURALNETWORKS_MISSED_DEADLINE_TRANSIENT has changed");
240 static_assert(ANEURALNETWORKS_MISSED_DEADLINE_PERSISTENT == 11,
241               "ANEURALNETWORKS_MISSED_DEADLINE_PERSISTENT has changed");
242 static_assert(ANEURALNETWORKS_RESOURCE_EXHAUSTED_TRANSIENT == 12,
243               "ANEURALNETWORKS_RESOURCE_EXHAUSTED_TRANSIENT has changed");
244 static_assert(ANEURALNETWORKS_RESOURCE_EXHAUSTED_PERSISTENT == 13,
245               "ANEURALNETWORKS_RESOURCE_EXHAUSTED_PERSISTENT has changed");
246 static_assert(ANEURALNETWORKS_DEAD_OBJECT == 14, "ANEURALNETWORKS_DEAD_OBJECT has changed");
247 
248 static_assert(ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES == 128,
249               "ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES has changed");
250 
251 static_assert(ANEURALNETWORKS_DEVICE_UNKNOWN == 0, "ANEURALNETWORKS_DEVICE_UNKNOWN has changed");
252 static_assert(ANEURALNETWORKS_DEVICE_OTHER == 1, "ANEURALNETWORKS_DEVICE_OTHER has changed");
253 static_assert(ANEURALNETWORKS_DEVICE_CPU == 2, "ANEURALNETWORKS_DEVICE_CPU has changed");
254 static_assert(ANEURALNETWORKS_DEVICE_GPU == 3, "ANEURALNETWORKS_DEVICE_GPU has changed");
255 static_assert(ANEURALNETWORKS_DEVICE_ACCELERATOR == 4,
256               "ANEURALNETWORKS_DEVICE_ACCELERATOR has changed");
257 
258 static_assert(ANEURALNETWORKS_DURATION_ON_HARDWARE == 0,
259               "ANEURALNETWORKS_DURATION_ON_HARDWARE has changed");
260 static_assert(ANEURALNETWORKS_DURATION_IN_DRIVER == 1,
261               "ANEURALNETWORKS_DURATION_IN_DRIVER has changed");
262 static_assert(ANEURALNETWORKS_FENCED_DURATION_ON_HARDWARE == 2,
263               "ANEURALNETWORKS_FENCED_DURATION_ON_HARDWARE has changed");
264 static_assert(ANEURALNETWORKS_FENCED_DURATION_IN_DRIVER == 3,
265               "ANEURALNETWORKS_FENCED_DURATION_IN_DRIVER has changed");
266 
267 // Make sure that the constants are compatible with the values defined in
268 // hardware/interfaces/neuralnetworks/1.0/types.hal.
269 static_assert(static_cast<int32_t>(OperandType::OEM) == ANEURALNETWORKS_OEM_SCALAR,
270               "OEM != ANEURALNETWORKS_OEM");
271 static_assert(static_cast<int32_t>(OperandType::FLOAT32) == ANEURALNETWORKS_FLOAT32,
272               "FLOAT32 != ANEURALNETWORKS_FLOAT32");
273 static_assert(static_cast<int32_t>(OperandType::INT32) == ANEURALNETWORKS_INT32,
274               "INT32 != ANEURALNETWORKS_INT32");
275 static_assert(static_cast<int32_t>(OperandType::UINT32) == ANEURALNETWORKS_UINT32,
276               "UINT32 != ANEURALNETWORKS_UINT32");
277 static_assert(static_cast<int32_t>(OperandType::TENSOR_OEM_BYTE) == ANEURALNETWORKS_TENSOR_OEM_BYTE,
278               "TENSOR_OEM_BYTE != ANEURALNETWORKS_TENSOR_OEM_BYTE");
279 static_assert(static_cast<int32_t>(OperandType::TENSOR_FLOAT32) == ANEURALNETWORKS_TENSOR_FLOAT32,
280               "TENSOR_FLOAT32 != ANEURALNETWORKS_TENSOR_FLOAT32");
281 static_assert(static_cast<int32_t>(OperandType::TENSOR_QUANT8_ASYMM) ==
282                       ANEURALNETWORKS_TENSOR_QUANT8_ASYMM,
283               "TENSOR_QUANT8_ASYMM != ANEURALNETWORKS_TENSOR_QUANT8_ASYMM");
284 
285 static_assert(static_cast<int32_t>(OperationType::ADD) == ANEURALNETWORKS_ADD,
286               "OperationType::ADD != ANEURALNETWORKS_ADD");
287 static_assert(static_cast<int32_t>(OperationType::AVERAGE_POOL_2D) ==
288                       ANEURALNETWORKS_AVERAGE_POOL_2D,
289               "OperationType::AVERAGE_POOL_2D != ANEURALNETWORKS_AVERAGE_POOL_2D");
290 static_assert(static_cast<int32_t>(OperationType::CONV_2D) == ANEURALNETWORKS_CONV_2D,
291               "OperationType::CONV_2D != ANEURALNETWORKS_CONV_2D");
292 static_assert(static_cast<int32_t>(OperationType::DEPTHWISE_CONV_2D) ==
293                       ANEURALNETWORKS_DEPTHWISE_CONV_2D,
294               "OperationType::DEPTHWISE_CONV_2D != ANEURALNETWORKS_DEPTHWISE_CONV_2D");
295 static_assert(static_cast<int32_t>(OperationType::DEPTH_TO_SPACE) == ANEURALNETWORKS_DEPTH_TO_SPACE,
296               "OperationType::DEPTH_TO_SPACE != ANEURALNETWORKS_DEPTH_TO_SPACE");
297 static_assert(static_cast<int32_t>(OperationType::DEQUANTIZE) == ANEURALNETWORKS_DEQUANTIZE,
298               "OperationType::DEQUANTIZE != ANEURALNETWORKS_DEQUANTIZE");
299 static_assert(static_cast<int32_t>(OperationType::EMBEDDING_LOOKUP) ==
300                       ANEURALNETWORKS_EMBEDDING_LOOKUP,
301               "OperationType::EMBEDDING_LOOKUP != ANEURALNETWORKS_EMBEDDING_LOOKUP");
302 static_assert(static_cast<int32_t>(OperationType::FLOOR) == ANEURALNETWORKS_FLOOR,
303               "OperationType::FLOOR != ANEURALNETWORKS_FLOOR");
304 static_assert(static_cast<int32_t>(OperationType::FULLY_CONNECTED) ==
305                       ANEURALNETWORKS_FULLY_CONNECTED,
306               "OperationType::FULLY_CONNECTED != ANEURALNETWORKS_FULLY_CONNECTED");
307 static_assert(static_cast<int32_t>(OperationType::HASHTABLE_LOOKUP) ==
308                       ANEURALNETWORKS_HASHTABLE_LOOKUP,
309               "OperationType::HASHTABLE_LOOKUP != ANEURALNETWORKS_HASHTABLE_LOOKUP");
310 static_assert(static_cast<int32_t>(OperationType::L2_NORMALIZATION) ==
311                       ANEURALNETWORKS_L2_NORMALIZATION,
312               "OperationType::L2_NORMALIZATION != ANEURALNETWORKS_L2_NORMALIZATION");
313 static_assert(static_cast<int32_t>(OperationType::L2_POOL_2D) == ANEURALNETWORKS_L2_POOL_2D,
314               "OperationType::L2_POOL_2D != ANEURALNETWORKS_L2_POOL_2D");
315 static_assert(static_cast<int32_t>(OperationType::LOCAL_RESPONSE_NORMALIZATION) ==
316                       ANEURALNETWORKS_LOCAL_RESPONSE_NORMALIZATION,
317               "OperationType::LOCAL_RESPONSE_NORMALIZATION != "
318               "ANEURALNETWORKS_LOCAL_RESPONSE_NORMALIZATION");
319 static_assert(static_cast<int32_t>(OperationType::LOGISTIC) == ANEURALNETWORKS_LOGISTIC,
320               "OperationType::LOGISTIC != ANEURALNETWORKS_LOGISTIC");
321 static_assert(static_cast<int32_t>(OperationType::LSH_PROJECTION) == ANEURALNETWORKS_LSH_PROJECTION,
322               "OperationType::LSH_PROJECTION != ANEURALNETWORKS_LSH_PROJECTION");
323 static_assert(static_cast<int32_t>(OperationType::LSTM) == ANEURALNETWORKS_LSTM,
324               "OperationType::LSTM != ANEURALNETWORKS_LSTM");
325 static_assert(static_cast<int32_t>(OperationType::MAX_POOL_2D) == ANEURALNETWORKS_MAX_POOL_2D,
326               "OperationType::MAX_POOL_2D != ANEURALNETWORKS_MAX_POOL_2D");
327 static_assert(static_cast<int32_t>(OperationType::MUL) == ANEURALNETWORKS_MUL,
328               "OperationType::MUL != ANEURALNETWORKS_MUL");
329 static_assert(static_cast<int32_t>(OperationType::RELU) == ANEURALNETWORKS_RELU,
330               "OperationType::RELU != ANEURALNETWORKS_RELU");
331 static_assert(static_cast<int32_t>(OperationType::RELU1) == ANEURALNETWORKS_RELU1,
332               "OperationType::RELU1 != ANEURALNETWORKS_RELU1");
333 static_assert(static_cast<int32_t>(OperationType::RELU6) == ANEURALNETWORKS_RELU6,
334               "OperationType::RELU6 != ANEURALNETWORKS_RELU6");
335 static_assert(static_cast<int32_t>(OperationType::RESHAPE) == ANEURALNETWORKS_RESHAPE,
336               "OperationType::RESHAPE != ANEURALNETWORKS_RESHAPE");
337 static_assert(static_cast<int32_t>(OperationType::RESIZE_BILINEAR) ==
338                       ANEURALNETWORKS_RESIZE_BILINEAR,
339               "OperationType::RESIZE_BILINEAR != ANEURALNETWORKS_RESIZE_BILINEAR");
340 static_assert(static_cast<int32_t>(OperationType::RNN) == ANEURALNETWORKS_RNN,
341               "OperationType::RNN != ANEURALNETWORKS_RNN");
342 static_assert(static_cast<int32_t>(OperationType::SOFTMAX) == ANEURALNETWORKS_SOFTMAX,
343               "OperationType::SOFTMAX != ANEURALNETWORKS_SOFTMAX");
344 static_assert(static_cast<int32_t>(OperationType::SPACE_TO_DEPTH) == ANEURALNETWORKS_SPACE_TO_DEPTH,
345               "OperationType::SPACE_TO_DEPTH != ANEURALNETWORKS_SPACE_TO_DEPTH");
346 static_assert(static_cast<int32_t>(OperationType::SVDF) == ANEURALNETWORKS_SVDF,
347               "OperationType::SVDF != ANEURALNETWORKS_SVDF");
348 static_assert(static_cast<int32_t>(OperationType::TANH) == ANEURALNETWORKS_TANH,
349               "OperationType::TANH != ANEURALNETWORKS_TANH");
350 
351 static_assert(static_cast<int32_t>(FusedActivationFunc::NONE) == ANEURALNETWORKS_FUSED_NONE,
352               "FusedActivationFunc::NONE != ANEURALNETWORKS_FUSED_NONE");
353 static_assert(static_cast<int32_t>(FusedActivationFunc::RELU) == ANEURALNETWORKS_FUSED_RELU,
354               "FusedActivationFunc::RELU != ANEURALNETWORKS_FUSED_RELU");
355 static_assert(static_cast<int32_t>(FusedActivationFunc::RELU1) == ANEURALNETWORKS_FUSED_RELU1,
356               "FusedActivationFunc::RELU1 != ANEURALNETWORKS_FUSED_RELU1");
357 static_assert(static_cast<int32_t>(FusedActivationFunc::RELU6) == ANEURALNETWORKS_FUSED_RELU6,
358               "FusedActivationFunc::RELU6 != ANEURALNETWORKS_FUSED_RELU6");
359 
360 // Make sure that the constants are compatible with the values defined in
361 // hardware/interfaces/neuralnetworks/1.1/types.hal.
362 static_assert(static_cast<int32_t>(OperationType::BATCH_TO_SPACE_ND) ==
363                       ANEURALNETWORKS_BATCH_TO_SPACE_ND,
364               "OperationType::BATCH_TO_SPACE_ND != ANEURALNETWORKS_BATCH_TO_SPACE_ND");
365 static_assert(static_cast<int32_t>(OperationType::DIV) == ANEURALNETWORKS_DIV,
366               "OperationType::DIV != ANEURALNETWORKS_DIV");
367 static_assert(static_cast<int32_t>(OperationType::MEAN) == ANEURALNETWORKS_MEAN,
368               "OperationType::MEAN != ANEURALNETWORKS_MEAN");
369 static_assert(static_cast<int32_t>(OperationType::PAD) == ANEURALNETWORKS_PAD,
370               "OperationType::PAD != ANEURALNETWORKS_PAD");
371 static_assert(static_cast<int32_t>(OperationType::SPACE_TO_BATCH_ND) ==
372                       ANEURALNETWORKS_SPACE_TO_BATCH_ND,
373               "OperationType::SPACE_TO_BATCH_ND != ANEURALNETWORKS_SPACE_TO_BATCH_ND");
374 static_assert(static_cast<int32_t>(OperationType::SQUEEZE) == ANEURALNETWORKS_SQUEEZE,
375               "OperationType::SQUEEZE != ANEURALNETWORKS_SQUEEZE");
376 static_assert(static_cast<int32_t>(OperationType::STRIDED_SLICE) == ANEURALNETWORKS_STRIDED_SLICE,
377               "OperationType::STRIDED_SLICE != ANEURALNETWORKS_STRIDED_SLICE");
378 static_assert(static_cast<int32_t>(OperationType::SUB) == ANEURALNETWORKS_SUB,
379               "OperationType::SUB != ANEURALNETWORKS_SUB");
380 static_assert(static_cast<int32_t>(OperationType::TRANSPOSE) == ANEURALNETWORKS_TRANSPOSE,
381               "OperationType::TRANSPOSE != ANEURALNETWORKS_TRANSPOSE");
382 
383 // Make sure that the constants are compatible with the values defined in
384 // hardware/interfaces/neuralnetworks/1.2/types.hal.
385 static_assert(static_cast<int32_t>(OperandType::BOOL) == ANEURALNETWORKS_BOOL,
386               "BOOL != ANEURALNETWORKS_BOOL");
387 static_assert(static_cast<int32_t>(OperandType::TENSOR_QUANT16_SYMM) ==
388                       ANEURALNETWORKS_TENSOR_QUANT16_SYMM,
389               "TENSOR_QUANT16_SYMM != ANEURALNETWORKS_TENSOR_QUANT16_SYMM");
390 static_assert(static_cast<int32_t>(OperandType::TENSOR_FLOAT16) == ANEURALNETWORKS_TENSOR_FLOAT16,
391               "TENSOR_FLOAT16 != ANEURALNETWORKS_TENSOR_FLOAT16");
392 static_assert(static_cast<int32_t>(OperandType::TENSOR_BOOL8) == ANEURALNETWORKS_TENSOR_BOOL8,
393               "TENSOR_BOOL8 != ANEURALNETWORKS_TENSOR_BOOL8");
394 static_assert(static_cast<int32_t>(OperandType::FLOAT16) == ANEURALNETWORKS_FLOAT16,
395               "FLOAT16 != ANEURALNETWORKS_FLOAT16");
396 static_assert(static_cast<int32_t>(OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) ==
397                       ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL,
398               "TENSOR_QUANT8_SYMM_PER_CHANNEL != ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL");
399 static_assert(static_cast<int32_t>(OperandType::TENSOR_QUANT16_ASYMM) ==
400                       ANEURALNETWORKS_TENSOR_QUANT16_ASYMM,
401               "TENSOR_QUANT16_ASYMM != ANEURALNETWORKS_TENSOR_QUANT16_ASYMM");
402 static_assert(static_cast<int32_t>(OperandType::TENSOR_QUANT8_SYMM) ==
403                       ANEURALNETWORKS_TENSOR_QUANT8_SYMM,
404               "TENSOR_QUANT8_SYMM != ANEURALNETWORKS_TENSOR_QUANT8_SYMM");
405 
406 static_assert(static_cast<int32_t>(OperationType::ABS) == ANEURALNETWORKS_ABS,
407               "OperationType::ABS != ANEURALNETWORKS_ABS");
408 static_assert(static_cast<int32_t>(OperationType::ARGMAX) == ANEURALNETWORKS_ARGMAX,
409               "OperationType::ARGMAX != ANEURALNETWORKS_ARGMAX");
410 static_assert(static_cast<int32_t>(OperationType::ARGMIN) == ANEURALNETWORKS_ARGMIN,
411               "OperationType::ARGMIN != ANEURALNETWORKS_ARGMIN");
412 static_assert(static_cast<int32_t>(OperationType::AXIS_ALIGNED_BBOX_TRANSFORM) ==
413                       ANEURALNETWORKS_AXIS_ALIGNED_BBOX_TRANSFORM,
414               "OperationType::AXIS_ALIGNED_BBOX_TRANSFORM != "
415               "ANEURALNETWORKS_AXIS_ALIGNED_BBOX_TRANSFORM");
416 static_assert(static_cast<int32_t>(OperationType::BIDIRECTIONAL_SEQUENCE_LSTM) ==
417                       ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_LSTM,
418               "OperationType::BIDIRECTIONAL_SEQUENCE_LSTM != "
419               "ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_LSTM");
420 static_assert(
421         static_cast<int32_t>(OperationType::BIDIRECTIONAL_SEQUENCE_RNN) ==
422                 ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_RNN,
423         "OperationType::BIDIRECTIONAL_SEQUENCE_RNN != ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_RNN");
424 static_assert(static_cast<int32_t>(OperationType::BOX_WITH_NMS_LIMIT) ==
425                       ANEURALNETWORKS_BOX_WITH_NMS_LIMIT,
426               "OperationType::BOX_WITH_NMS_LIMIT != ANEURALNETWORKS_BOX_WITH_NMS_LIMIT");
427 static_assert(static_cast<int32_t>(OperationType::CAST) == ANEURALNETWORKS_CAST,
428               "OperationType::CAST != ANEURALNETWORKS_CAST");
429 static_assert(static_cast<int32_t>(OperationType::CHANNEL_SHUFFLE) ==
430                       ANEURALNETWORKS_CHANNEL_SHUFFLE,
431               "OperationType::CHANNEL_SHUFFLE != ANEURALNETWORKS_CHANNEL_SHUFFLE");
432 static_assert(
433         static_cast<int32_t>(OperationType::DETECTION_POSTPROCESSING) ==
434                 ANEURALNETWORKS_DETECTION_POSTPROCESSING,
435         "OperationType::DETECTION_POSTPROCESSING != ANEURALNETWORKS_DETECTION_POSTPROCESSING");
436 static_assert(static_cast<int32_t>(OperationType::EQUAL) == ANEURALNETWORKS_EQUAL,
437               "OperationType::EQUAL != ANEURALNETWORKS_EQUAL");
438 static_assert(static_cast<int32_t>(OperationType::EXP) == ANEURALNETWORKS_EXP,
439               "OperationType::EXP != ANEURALNETWORKS_EXP");
440 static_assert(static_cast<int32_t>(OperationType::EXPAND_DIMS) == ANEURALNETWORKS_EXPAND_DIMS,
441               "OperationType::EXPAND_DIMS != ANEURALNETWORKS_EXPAND_DIMS");
442 static_assert(static_cast<int32_t>(OperationType::GATHER) == ANEURALNETWORKS_GATHER,
443               "OperationType::GATHER != ANEURALNETWORKS_GATHER");
444 static_assert(static_cast<int32_t>(OperationType::GENERATE_PROPOSALS) ==
445                       ANEURALNETWORKS_GENERATE_PROPOSALS,
446               "OperationType::GENERATE_PROPOSALS != ANEURALNETWORKS_GENERATE_PROPOSALS");
447 static_assert(static_cast<int32_t>(OperationType::GREATER) == ANEURALNETWORKS_GREATER,
448               "OperationType::GREATER != ANEURALNETWORKS_GREATER");
449 static_assert(static_cast<int32_t>(OperationType::GREATER_EQUAL) == ANEURALNETWORKS_GREATER_EQUAL,
450               "OperationType::GREATER_EQUAL != ANEURALNETWORKS_GREATER_EQUAL");
451 static_assert(static_cast<int32_t>(OperationType::GROUPED_CONV_2D) ==
452                       ANEURALNETWORKS_GROUPED_CONV_2D,
453               "OperationType::GROUPED_CONV_2D != ANEURALNETWORKS_GROUPED_CONV_2D");
454 static_assert(static_cast<int32_t>(OperationType::HEATMAP_MAX_KEYPOINT) ==
455                       ANEURALNETWORKS_HEATMAP_MAX_KEYPOINT,
456               "OperationType::HEATMAP_MAX_KEYPOINT != ANEURALNETWORKS_HEATMAP_MAX_KEYPOINT");
457 static_assert(static_cast<int32_t>(OperationType::INSTANCE_NORMALIZATION) ==
458                       ANEURALNETWORKS_INSTANCE_NORMALIZATION,
459               "OperationType::INSTANCE_NORMALIZATION != ANEURALNETWORKS_INSTANCE_NORMALIZATION");
460 static_assert(static_cast<int32_t>(OperationType::LESS) == ANEURALNETWORKS_LESS,
461               "OperationType::LESS != ANEURALNETWORKS_LESS");
462 static_assert(static_cast<int32_t>(OperationType::LESS_EQUAL) == ANEURALNETWORKS_LESS_EQUAL,
463               "OperationType::LESS_EQUAL != ANEURALNETWORKS_LESS_EQUAL");
464 static_assert(static_cast<int32_t>(OperationType::LOG) == ANEURALNETWORKS_LOG,
465               "OperationType::LOG != ANEURALNETWORKS_LOG");
466 static_assert(static_cast<int32_t>(OperationType::LOGICAL_AND) == ANEURALNETWORKS_LOGICAL_AND,
467               "OperationType::LOGICAL_AND != ANEURALNETWORKS_LOGICAL_AND");
468 static_assert(static_cast<int32_t>(OperationType::LOGICAL_NOT) == ANEURALNETWORKS_LOGICAL_NOT,
469               "OperationType::LOGICAL_NOT != ANEURALNETWORKS_LOGICAL_NOT");
470 static_assert(static_cast<int32_t>(OperationType::LOGICAL_OR) == ANEURALNETWORKS_LOGICAL_OR,
471               "OperationType::LOGICAL_OR != ANEURALNETWORKS_LOGICAL_OR");
472 static_assert(static_cast<int32_t>(OperationType::LOG_SOFTMAX) == ANEURALNETWORKS_LOG_SOFTMAX,
473               "OperationType::LOG_SOFTMAX != ANEURALNETWORKS_LOG_SOFTMAX");
474 static_assert(static_cast<int32_t>(OperationType::MAXIMUM) == ANEURALNETWORKS_MAXIMUM,
475               "OperationType::MAXIMUM != ANEURALNETWORKS_MAXIMUM");
476 static_assert(static_cast<int32_t>(OperationType::MINIMUM) == ANEURALNETWORKS_MINIMUM,
477               "OperationType::MINIMUM != ANEURALNETWORKS_MINIMUM");
478 static_assert(static_cast<int32_t>(OperationType::NEG) == ANEURALNETWORKS_NEG,
479               "OperationType::NEG != ANEURALNETWORKS_NEG");
480 static_assert(static_cast<int32_t>(OperationType::NOT_EQUAL) == ANEURALNETWORKS_NOT_EQUAL,
481               "OperationType::NOT_EQUAL != ANEURALNETWORKS_NOT_EQUAL");
482 static_assert(static_cast<int32_t>(OperationType::PAD_V2) == ANEURALNETWORKS_PAD_V2,
483               "OperationType::PAD_V2 != ANEURALNETWORKS_PAD_V2");
484 static_assert(static_cast<int32_t>(OperationType::POW) == ANEURALNETWORKS_POW,
485               "OperationType::POW != ANEURALNETWORKS_POW");
486 static_assert(static_cast<int32_t>(OperationType::PRELU) == ANEURALNETWORKS_PRELU,
487               "OperationType::PRELU != ANEURALNETWORKS_PRELU");
488 static_assert(static_cast<int32_t>(OperationType::QUANTIZE) == ANEURALNETWORKS_QUANTIZE,
489               "OperationType::QUANTIZE != ANEURALNETWORKS_QUANTIZE");
490 static_assert(static_cast<int32_t>(OperationType::QUANTIZED_16BIT_LSTM) ==
491                       ANEURALNETWORKS_QUANTIZED_16BIT_LSTM,
492               "OperationType::QUANTIZED_16BIT_LSTM != ANEURALNETWORKS_QUANTIZED_16BIT_LSTM");
493 static_assert(static_cast<int32_t>(OperationType::RANDOM_MULTINOMIAL) ==
494                       ANEURALNETWORKS_RANDOM_MULTINOMIAL,
495               "OperationType::RANDOM_MULTINOMIAL != ANEURALNETWORKS_RANDOM_MULTINOMIAL");
496 static_assert(static_cast<int32_t>(OperationType::REDUCE_ALL) == ANEURALNETWORKS_REDUCE_ALL,
497               "OperationType::REDUCE_ALL != ANEURALNETWORKS_REDUCE_ALL");
498 static_assert(static_cast<int32_t>(OperationType::REDUCE_ANY) == ANEURALNETWORKS_REDUCE_ANY,
499               "OperationType::REDUCE_ANY != ANEURALNETWORKS_REDUCE_ANY");
500 static_assert(static_cast<int32_t>(OperationType::REDUCE_MAX) == ANEURALNETWORKS_REDUCE_MAX,
501               "OperationType::REDUCE_MAX != ANEURALNETWORKS_REDUCE_MAX");
502 static_assert(static_cast<int32_t>(OperationType::REDUCE_MIN) == ANEURALNETWORKS_REDUCE_MIN,
503               "OperationType::REDUCE_MIN != ANEURALNETWORKS_REDUCE_MIN");
504 static_assert(static_cast<int32_t>(OperationType::REDUCE_PROD) == ANEURALNETWORKS_REDUCE_PROD,
505               "OperationType::REDUCE_PROD != ANEURALNETWORKS_REDUCE_PROD");
506 static_assert(static_cast<int32_t>(OperationType::REDUCE_SUM) == ANEURALNETWORKS_REDUCE_SUM,
507               "OperationType::REDUCE_SUM != ANEURALNETWORKS_REDUCE_SUM");
508 static_assert(static_cast<int32_t>(OperationType::ROI_ALIGN) == ANEURALNETWORKS_ROI_ALIGN,
509               "OperationType::ROI_ALIGN != ANEURALNETWORKS_ROI_ALIGN");
510 static_assert(static_cast<int32_t>(OperationType::ROI_POOLING) == ANEURALNETWORKS_ROI_POOLING,
511               "OperationType::ROI_POOLING != ANEURALNETWORKS_ROI_POOLING");
512 static_assert(static_cast<int32_t>(OperationType::RSQRT) == ANEURALNETWORKS_RSQRT,
513               "OperationType::RSQRT != ANEURALNETWORKS_RSQRT");
514 static_assert(static_cast<int32_t>(OperationType::SELECT) == ANEURALNETWORKS_SELECT,
515               "OperationType::SELECT != ANEURALNETWORKS_SELECT");
516 static_assert(static_cast<int32_t>(OperationType::SIN) == ANEURALNETWORKS_SIN,
517               "OperationType::SIN != ANEURALNETWORKS_SIN");
518 static_assert(static_cast<int32_t>(OperationType::SLICE) == ANEURALNETWORKS_SLICE,
519               "OperationType::SLICE != ANEURALNETWORKS_SLICE");
520 static_assert(static_cast<int32_t>(OperationType::SPLIT) == ANEURALNETWORKS_SPLIT,
521               "OperationType::SPLIT != ANEURALNETWORKS_SPLIT");
522 static_assert(static_cast<int32_t>(OperationType::SQRT) == ANEURALNETWORKS_SQRT,
523               "OperationType::SQRT != ANEURALNETWORKS_SQRT");
524 static_assert(static_cast<int32_t>(OperationType::TILE) == ANEURALNETWORKS_TILE,
525               "OperationType::TILE != ANEURALNETWORKS_TILE");
526 static_assert(static_cast<int32_t>(OperationType::TOPK_V2) == ANEURALNETWORKS_TOPK_V2,
527               "OperationType::TOPK_V2 != ANEURALNETWORKS_TOPK_V2");
528 static_assert(static_cast<int32_t>(OperationType::TRANSPOSE_CONV_2D) ==
529                       ANEURALNETWORKS_TRANSPOSE_CONV_2D,
530               "OperationType::TRANSPOSE_CONV_2D != ANEURALNETWORKS_TRANSPOSE_CONV_2D");
531 static_assert(static_cast<int32_t>(OperationType::UNIDIRECTIONAL_SEQUENCE_LSTM) ==
532                       ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_LSTM,
533               "OperationType::UNIDIRECTIONAL_SEQUENCE_LSTM != "
534               "ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_LSTM");
535 static_assert(static_cast<int32_t>(OperationType::UNIDIRECTIONAL_SEQUENCE_RNN) ==
536                       ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_RNN,
537               "OperationType::UNIDIRECTIONAL_SEQUENCE_RNN != "
538               "ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_RNN");
539 static_assert(static_cast<int32_t>(OperationType::RESIZE_NEAREST_NEIGHBOR) ==
540                       ANEURALNETWORKS_RESIZE_NEAREST_NEIGHBOR,
541               "OperationType::RESIZE_NEAREST_NEIGHBOR != ANEURALNETWORKS_RESIZE_NEAREST_NEIGHBOR");
542 static_assert(static_cast<int32_t>(OperationType::QUANTIZED_LSTM) == ANEURALNETWORKS_QUANTIZED_LSTM,
543               "OperationType::QUANTIZED_LSTM != ANEURALNETWORKS_QUANTIZED_LSTM");
544 static_assert(static_cast<int32_t>(OperationType::IF) == ANEURALNETWORKS_IF,
545               "OperationType::IF != ANEURALNETWORKS_IF");
546 static_assert(static_cast<int32_t>(OperationType::WHILE) == ANEURALNETWORKS_WHILE,
547               "OperationType::WHILE != ANEURALNETWORKS_WHILE");
548 static_assert(static_cast<int32_t>(OperationType::ELU) == ANEURALNETWORKS_ELU,
549               "OperationType::ELU != ANEURALNETWORKS_ELU");
550 static_assert(static_cast<int32_t>(OperationType::HARD_SWISH) == ANEURALNETWORKS_HARD_SWISH,
551               "OperationType::HARD_SWISH != ANEURALNETWORKS_HARD_SWISH");
552 static_assert(static_cast<int32_t>(OperationType::FILL) == ANEURALNETWORKS_FILL,
553               "OperationType::FILL != ANEURALNETWORKS_FILL");
554 static_assert(static_cast<int32_t>(OperationType::RANK) == ANEURALNETWORKS_RANK,
555               "OperationType::RANK != ANEURALNETWORKS_RANK");
556 static_assert(static_cast<int32_t>(OperationType::BATCH_MATMUL) == ANEURALNETWORKS_BATCH_MATMUL,
557               "OperationType::BATCH_MATMUL != ANEURALNETWORKS_BATCH_MATMUL");
558 static_assert(static_cast<int32_t>(OperationType::PACK) == ANEURALNETWORKS_PACK,
559               "OperationType::PACK != ANEURALNETWORKS_PACK");
560 static_assert(static_cast<int32_t>(OperationType::MIRROR_PAD) == ANEURALNETWORKS_MIRROR_PAD,
561               "OperationType::MIRROR_PAD != ANEURALNETWORKS_MIRROR_PAD");
562 static_assert(static_cast<int32_t>(OperationType::REVERSE) == ANEURALNETWORKS_REVERSE,
563               "OperationType::REVERSE != ANEURALNETWORKS_REVERSE");
564 
565 static_assert(static_cast<int32_t>(DeviceType::OTHER) == ANEURALNETWORKS_DEVICE_OTHER,
566               "DeviceType::OTHER != ANEURALNETWORKS_DEVICE_OTHER");
567 static_assert(static_cast<int32_t>(DeviceType::CPU) == ANEURALNETWORKS_DEVICE_CPU,
568               "DeviceType::CPU != ANEURALNETWORKS_DEVICE_CPU");
569 static_assert(static_cast<int32_t>(DeviceType::GPU) == ANEURALNETWORKS_DEVICE_GPU,
570               "DeviceType::GPU != ANEURALNETWORKS_DEVICE_GPU");
571 static_assert(static_cast<int32_t>(DeviceType::ACCELERATOR) == ANEURALNETWORKS_DEVICE_ACCELERATOR,
572               "DeviceType::ACCELERATOR != ANEURALNETWORKS_DEVICE_ACCELERATOR");
573 
574 // Make sure that the constants are compatible with the values defined in
575 // hardware/interfaces/neuralnetworks/1.3/types.hal.
576 static_assert(android::nn::convertToCanonicalPriority(ANEURALNETWORKS_PRIORITY_LOW) ==
577                       Priority::LOW,
578               "ANEURALNETWORKS_PRIORITY_LOW does not map to Priority::LOW");
579 static_assert(android::nn::convertToCanonicalPriority(ANEURALNETWORKS_PRIORITY_MEDIUM) ==
580                       Priority::MEDIUM,
581               "ANEURALNETWORKS_PRIORITY_MEDIUM does not map to Priority::MEDIUM");
582 static_assert(android::nn::convertToCanonicalPriority(ANEURALNETWORKS_PRIORITY_HIGH) ==
583                       Priority::HIGH,
584               "ANEURALNETWORKS_PRIORITY_HIGH does not map to Priority::HIGH");
585 
586 // Asserts for ANeuralNetworksOperandType memory layout
587 static_assert(offsetof(ANeuralNetworksOperandType, type) == 0,
588               "ANeuralNetworksOperandType.type offset != 0");
589 static_assert(offsetof(ANeuralNetworksOperandType, dimensionCount) == 4,
590               "ANeuralNetworksOperandType.dimensionCount offset != 4");
591 static_assert(offsetof(ANeuralNetworksOperandType, dimensions) == 8,
592               "ANeuralNetworksOperandType.dimensions offset != 8");
593 static_assert(offsetof(ANeuralNetworksOperandType, scale) == 8 + sizeof(void*),
594               "ANeuralNetworksOperandType.scale offset != 8 + sizeof(void*)");
595 static_assert(offsetof(ANeuralNetworksOperandType, zeroPoint) == 12 + sizeof(void*),
596               "ANeuralNetworksOperandType.zeroPoint offset != 12 + sizeof(void*)");
597 static_assert(sizeof(ANeuralNetworksOperandType) == 16 + sizeof(void*),
598               "ANeuralNetworksOperandType size changed");
599 static_assert(alignof(ANeuralNetworksOperandType) == alignof(void*),
600               "ANeuralNetworksOperandType alignment changed");
601 
602 // Asserts for ANeuralNetworksSymmPerChannelQuantParams memory layout
603 static_assert(offsetof(ANeuralNetworksSymmPerChannelQuantParams, channelDim) == 0,
604               "ANeuralNetworksSymmPerChannelQuantParams.channelDim offset != 4 + sizeof(void*)");
605 static_assert(offsetof(ANeuralNetworksSymmPerChannelQuantParams, scaleCount) == 4,
606               "ANeuralNetworksSymmPerChannelQuantParams.scaleCount offset != 0");
607 static_assert(offsetof(ANeuralNetworksSymmPerChannelQuantParams, scales) == 8,
608               "ANeuralNetworksSymmPerChannelQuantParams.scales offset != 4");
609 static_assert(sizeof(ANeuralNetworksSymmPerChannelQuantParams) == 8 + sizeof(void*),
610               "ANeuralNetworksSymmPerChannelQuantParams size != 8 + sizeof(void*)");
611 static_assert(alignof(ANeuralNetworksSymmPerChannelQuantParams) == alignof(void*),
612               "ANeuralNetworksOperandType alignment changed");
613 
614 // Asserts for compilation caching
615 static_assert(ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN == 32,
616               "ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN has changed");
617 static_assert(ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN == kByteSizeOfCacheToken,
618               "ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN != kByteSizeOfCacheToken");
619 
620 // Asserts for compilation priority
621 static_assert(ANEURALNETWORKS_PRIORITY_LOW == 90, "ANEURALNETWORKS_PRIORITY_LOW has changed");
622 static_assert(ANEURALNETWORKS_PRIORITY_MEDIUM == 100,
623               "ANEURALNETWORKS_PRIORITY_MEDIUM has changed");
624 static_assert(ANEURALNETWORKS_PRIORITY_HIGH == 110, "ANEURALNETWORKS_PRIORITY_HIGH has changed");
625 static_assert(ANEURALNETWORKS_PRIORITY_DEFAULT == ANEURALNETWORKS_PRIORITY_MEDIUM,
626               "ANEURALNETWORKS_PRIORITY_DEFAULT has changed");
627 
628 // Asserts for feature levels
629 static_assert(ANEURALNETWORKS_FEATURE_LEVEL_1 == 27, "ANEURALNETWORKS_FEATURE_LEVEL_1 has changed");
630 static_assert(ANEURALNETWORKS_FEATURE_LEVEL_2 == 28, "ANEURALNETWORKS_FEATURE_LEVEL_2 has changed");
631 static_assert(ANEURALNETWORKS_FEATURE_LEVEL_3 == 29, "ANEURALNETWORKS_FEATURE_LEVEL_3 has changed");
632 static_assert(ANEURALNETWORKS_FEATURE_LEVEL_4 == 30, "ANEURALNETWORKS_FEATURE_LEVEL_4 has changed");
633 static_assert(ANEURALNETWORKS_FEATURE_LEVEL_5 == 31, "ANEURALNETWORKS_FEATURE_LEVEL_5 has changed");
634 static_assert(ANEURALNETWORKS_FEATURE_LEVEL_6 == 1000006,
635               "ANEURALNETWORKS_FEATURE_LEVEL_6 has changed");
636 static_assert(ANEURALNETWORKS_FEATURE_LEVEL_7 == 1000007,
637               "ANEURALNETWORKS_FEATURE_LEVEL_7 has changed");
638 static_assert(ANEURALNETWORKS_FEATURE_LEVEL_8 == 1000008,
639               "ANEURALNETWORKS_FEATURE_LEVEL_8 has changed");
640 
641 #ifdef NN_COMPATIBILITY_LIBRARY_BUILD
642 
643 static_assert(sizeof(SL_ANeuralNetworksPerformanceInfo) == sizeof(float) * 2,
644               "SL_ANeuralNetworksPerformanceInfo size changed");
645 static_assert(sizeof(SL_ANeuralNetworksOperandPerformanceInfo) ==
646                       sizeof(float) * 2 + sizeof(int32_t),
647               "SL_ANeuralNetworksOperandPerformanceInfo size changed");
648 static_assert(sizeof(SL_ANeuralNetworksExtensionOperandTypeInformation) == 8,
649               "SL_ANeuralNetworksExtensionOperandTypeInformation size changed");
650 
651 static_assert(SL_ANEURALNETWORKS_CAPABILITIES_PERFORMANCE_RELAXED_SCALAR == 0,
652               "SL_ANEURALNETWORKS_CAPABILITIES_PERFORMANCE_RELAXED_SCALAR has changed");
653 static_assert(SL_ANEURALNETWORKS_CAPABILITIES_PERFORMANCE_RELAXED_TENSOR == 1,
654               "SL_ANEURALNETWORKS_CAPABILITIES_PERFORMANCE_RELAXED_TENSOR has changed");
655 static_assert(SL_ANEURALNETWORKS_CAPABILITIES_PERFORMANCE_IF == 2,
656               "SL_ANEURALNETWORKS_CAPABILITIES_PERFORMANCE_IF has changed");
657 static_assert(SL_ANEURALNETWORKS_CAPABILITIES_PERFORMANCE_WHILE == 3,
658               "SL_ANEURALNETWORKS_CAPABILITIES_PERFORMANCE_WHILE has changed");
659 
660 #endif  // NN_COMPATIBILITY_LIBRARY_BUILD
661 
ANeuralNetworks_getDeviceCount(uint32_t * numDevices)662 int ANeuralNetworks_getDeviceCount(uint32_t* numDevices) {
663     if (numDevices == nullptr) {
664         LOG(ERROR) << "ANeuralNetworks_getDeviceCount passed a nullptr";
665         return ANEURALNETWORKS_UNEXPECTED_NULL;
666     }
667     *numDevices = DeviceManager::get()->getDrivers().size();
668     return ANEURALNETWORKS_NO_ERROR;
669 }
670 
ANeuralNetworks_getDevice(uint32_t devIndex,ANeuralNetworksDevice ** device)671 int ANeuralNetworks_getDevice(uint32_t devIndex, ANeuralNetworksDevice** device) {
672     if (device == nullptr) {
673         LOG(ERROR) << "ANeuralNetworks_getDevice passed a nullptr";
674         return ANEURALNETWORKS_UNEXPECTED_NULL;
675     }
676     const std::vector<std::shared_ptr<Device>>& devices = DeviceManager::get()->getDrivers();
677     if (devIndex >= devices.size()) {
678         LOG(ERROR) << "ANeuralNetworks_getDevice passed an invalid device index";
679         return ANEURALNETWORKS_BAD_DATA;
680     }
681     *device = reinterpret_cast<ANeuralNetworksDevice*>(devices.at(devIndex).get());
682     return ANEURALNETWORKS_NO_ERROR;
683 }
684 
ANeuralNetworksDevice_getName(const ANeuralNetworksDevice * device,const char ** name)685 int ANeuralNetworksDevice_getName(const ANeuralNetworksDevice* device, const char** name) {
686     if (device == nullptr || name == nullptr) {
687         LOG(ERROR) << "ANeuralNetworksDevice_getName passed a nullptr";
688         return ANEURALNETWORKS_UNEXPECTED_NULL;
689     }
690     const Device* d = reinterpret_cast<const Device*>(device);
691     *name = d->getName().c_str();
692     return ANEURALNETWORKS_NO_ERROR;
693 }
694 
ANeuralNetworksDevice_getVersion(const ANeuralNetworksDevice * device,const char ** version)695 int ANeuralNetworksDevice_getVersion(const ANeuralNetworksDevice* device, const char** version) {
696     if (device == nullptr || version == nullptr) {
697         LOG(ERROR) << "ANeuralNetworksDevice_getVersion passed a nullptr";
698         return ANEURALNETWORKS_UNEXPECTED_NULL;
699     }
700     const Device* d = reinterpret_cast<const Device*>(device);
701     *version = d->getVersionString().c_str();
702     return ANEURALNETWORKS_NO_ERROR;
703 }
704 
ANeuralNetworksDevice_getType(const ANeuralNetworksDevice * device,int32_t * type)705 int ANeuralNetworksDevice_getType(const ANeuralNetworksDevice* device, int32_t* type) {
706     if (device == nullptr || type == nullptr) {
707         LOG(ERROR) << "ANeuralNetworksDevice_getType passed a nullptr";
708         return ANEURALNETWORKS_UNEXPECTED_NULL;
709     }
710     const Device* d = reinterpret_cast<const Device*>(device);
711     int32_t dType = d->getType();
712     if (dType < 0) {
713         return ANEURALNETWORKS_OP_FAILED;
714     }
715     *type = d->getType();
716     return ANEURALNETWORKS_NO_ERROR;
717 }
718 
719 #ifdef NN_DEBUGGABLE
720 static int64_t sRuntimeFeatureLevel = 0;
forTest_setRuntimeFeatureLevel(int64_t level)721 void forTest_setRuntimeFeatureLevel(int64_t level) {
722     sRuntimeFeatureLevel = level;
723 }
724 #endif
725 
726 // Since ANeuralNetworks_getRuntimeFeatureLevel is new in 31 while libneuralnetwork targets
727 // "min_sdk_version: 30", calling it should be properly guarded (e.g. __builtin_available).
728 // But calling it within the same compilation unit is perfectly fine. Guarding it doesn't
729 // make any sense and is simply wrong. (It's available on a system where __builtin_available(30)
730 // evaluates to false.)
731 // To make the compiler happy we introduce getRuntimeFeatureLevelImpl() and call it within the
732 // library.
getRuntimeFeatureLevelImpl()733 static inline int64_t getRuntimeFeatureLevelImpl() {
734 #ifdef NN_DEBUGGABLE
735     if (sRuntimeFeatureLevel) {
736         return sRuntimeFeatureLevel;
737     }
738 #endif
739     return DeviceManager::get()->getRuntimeFeatureLevel();
740 }
741 
ANeuralNetworksDevice_getFeatureLevel(const ANeuralNetworksDevice * device,int64_t * featureLevel)742 int ANeuralNetworksDevice_getFeatureLevel(const ANeuralNetworksDevice* device,
743                                           int64_t* featureLevel) {
744     if (device == nullptr || featureLevel == nullptr) {
745         LOG(ERROR) << "ANeuralNetworksDevice_getFeatureLevel passed a nullptr";
746         return ANEURALNETWORKS_UNEXPECTED_NULL;
747     }
748     Device* d = reinterpret_cast<Device*>(const_cast<ANeuralNetworksDevice*>(device));
749     int64_t dFeatureLevel = DeviceManager::versionToFeatureLevel(d->getFeatureLevel().level);
750     if (dFeatureLevel < 0) {
751         return ANEURALNETWORKS_BAD_STATE;
752     }
753     *featureLevel = std::min(getRuntimeFeatureLevelImpl(), dFeatureLevel);
754     return ANEURALNETWORKS_NO_ERROR;
755 }
756 
ANeuralNetworksDevice_wait(const ANeuralNetworksDevice * device)757 int ANeuralNetworksDevice_wait(const ANeuralNetworksDevice* device) {
758     if (device == nullptr) {
759         LOG(ERROR) << "ANeuralNetworksDevice_wait passed a nullptr";
760         return ANEURALNETWORKS_UNEXPECTED_NULL;
761     }
762     const Device* d = reinterpret_cast<const Device*>(device);
763     return d->wait();
764 }
765 
ANeuralNetworksModel_getSupportedOperationsForDevices(const ANeuralNetworksModel * model,const ANeuralNetworksDevice * const * devices,uint32_t numDevices,bool * supportedOps)766 int ANeuralNetworksModel_getSupportedOperationsForDevices(
767         const ANeuralNetworksModel* model, const ANeuralNetworksDevice* const* devices,
768         uint32_t numDevices, bool* supportedOps) {
769     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksModel_getSupportedOperationsForDevices");
770     if (model == nullptr || devices == nullptr || supportedOps == nullptr) {
771         LOG(ERROR) << "ANeuralNetworksModel_getSupportedOperationsForDevices passed a nullptr";
772         return ANEURALNETWORKS_UNEXPECTED_NULL;
773     }
774     if (numDevices == 0) {
775         LOG(ERROR) << "ANeuralNetworksModel_getSupportedOperationsForDevices passed an empty "
776                       "device list";
777         return ANEURALNETWORKS_BAD_DATA;
778     }
779     const ModelBuilder* m = reinterpret_cast<const ModelBuilder*>(model);
780     if (!m->isFinished() || !m->isValid()) {
781         LOG(ERROR) << "ANeuralNetworksModel_getSupportedOperationsForDevices passed an unfinished "
782                       "or invalid Model";
783         return ANEURALNETWORKS_BAD_STATE;
784     }
785 
786     const Model canonicalModel = m->makeModel();
787     const std::vector<uint32_t>& opMap = m->getSortedOperationMapping();
788     // init the output array to false for all the operations.
789     std::fill(supportedOps, supportedOps + opMap.size(), false);
790     for (uint32_t i = 0; i < numDevices; i++) {
791         if (devices[i] == nullptr) {
792             LOG(ERROR) << "ANeuralNetworksModel_getSupportedOperationsForDevices passed a nullptr "
793                           "as a device";
794             return ANEURALNETWORKS_UNEXPECTED_NULL;
795         }
796         for (uint32_t j = i + 1; j < numDevices; j++) {
797             if (devices[i] == devices[j]) {
798                 LOG(ERROR) << "ANeuralNetworksModel_getSupportedOperationsForDevices passed "
799                               "duplicate devices";
800                 return ANEURALNETWORKS_BAD_DATA;
801             }
802         }
803 
804         Device* d = reinterpret_cast<Device*>(const_cast<ANeuralNetworksDevice*>(devices[i]));
805         const MetaModel metaModel(canonicalModel, DeviceManager::get()->strictSlicing());
806         const std::vector<bool> supportsByDevice = d->getSupportedOperations(metaModel);
807         for (uint32_t j = 0; j < supportsByDevice.size(); j++) {
808             uint32_t originalIdx = opMap[j];
809             supportedOps[originalIdx] |= supportsByDevice[j];
810         }
811     }
812     return ANEURALNETWORKS_NO_ERROR;
813 }
814 
ANeuralNetworksCompilation_createForDevices(ANeuralNetworksModel * model,const ANeuralNetworksDevice * const * devices,uint32_t numDevices,ANeuralNetworksCompilation ** compilation)815 int ANeuralNetworksCompilation_createForDevices(ANeuralNetworksModel* model,
816                                                 const ANeuralNetworksDevice* const* devices,
817                                                 uint32_t numDevices,
818                                                 ANeuralNetworksCompilation** compilation) {
819     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_createForDevices");
820     if (model == nullptr || devices == nullptr || compilation == nullptr) {
821         LOG(ERROR) << "ANeuralNetworksCompilation_createForDevices passed a nullptr";
822         return ANEURALNETWORKS_UNEXPECTED_NULL;
823     }
824 
825     if (numDevices == 0) {
826         LOG(ERROR) << "ANeuralNetworksCompilation_createForDevices passed an empty device list";
827         return ANEURALNETWORKS_BAD_DATA;
828     }
829 
830     std::vector<std::shared_ptr<Device>> selectedDevices;
831     for (uint32_t i = 0; i < numDevices; i++) {
832         if (devices[i] == nullptr) {
833             LOG(ERROR)
834                     << "ANeuralNetworksCompilation_createForDevices passed a nullptr as a device";
835             return ANEURALNETWORKS_UNEXPECTED_NULL;
836         }
837         for (uint32_t j = i + 1; j < numDevices; j++) {
838             if (devices[i] == devices[j]) {
839                 LOG(ERROR)
840                         << "ANeuralNetworksCompilation_createForDevices passed duplicate devices";
841                 return ANEURALNETWORKS_BAD_DATA;
842             }
843         }
844         for (auto& device : DeviceManager::get()->getDrivers()) {
845             if (device.get() == reinterpret_cast<const Device*>(devices[i])) {
846                 // Find a match
847                 selectedDevices.push_back(device);
848                 break;
849             }
850         }
851     }
852 
853     if (selectedDevices.size() != numDevices) {
854         LOG(ERROR) << "ANeuralNetworksCompilation_createForDevices passed an invalid device set";
855         return ANEURALNETWORKS_BAD_DATA;
856     }
857     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
858     CompilationBuilder* c = nullptr;
859     // No CPU fallback when user specifies the list of devices manually.
860     int result = m->createCompilation(&c, selectedDevices, /* explicitDeviceList */ true);
861     *compilation = reinterpret_cast<ANeuralNetworksCompilation*>(c);
862     return result;
863 }
864 
ANeuralNetworksExecution_compute(ANeuralNetworksExecution * execution)865 int ANeuralNetworksExecution_compute(ANeuralNetworksExecution* execution) {
866     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_compute");
867     if (!execution) {
868         LOG(ERROR) << "ANeuralNetworksExecution_compute passed a nullptr";
869         return ANEURALNETWORKS_UNEXPECTED_NULL;
870     }
871     // TODO validate the rest
872 
873     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
874     return r->computeSynchronously();
875 }
876 
ANeuralNetworksExecution_setMeasureTiming(ANeuralNetworksExecution * execution,bool measure)877 int ANeuralNetworksExecution_setMeasureTiming(ANeuralNetworksExecution* execution, bool measure) {
878     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_setMeasureTiming");
879     if (!execution) {
880         LOG(ERROR) << "ANeuralNetworksExecution_setMeasureTiming passed a nullptr";
881         return ANEURALNETWORKS_UNEXPECTED_NULL;
882     }
883     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
884     return r->setMeasureTiming(measure);
885 }
886 
ANeuralNetworksExecution_getDuration(const ANeuralNetworksExecution * execution,int32_t durationCode,uint64_t * duration)887 int ANeuralNetworksExecution_getDuration(const ANeuralNetworksExecution* execution,
888                                          int32_t durationCode, uint64_t* duration) {
889     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_getDuration");
890     if (!execution || !duration) {
891         LOG(ERROR) << "ANeuralNetworksExecution_getDuration passed a nullptr";
892         return ANEURALNETWORKS_UNEXPECTED_NULL;
893     }
894     switch (durationCode) {
895         case ANEURALNETWORKS_DURATION_ON_HARDWARE:
896         case ANEURALNETWORKS_DURATION_IN_DRIVER:
897         case ANEURALNETWORKS_FENCED_DURATION_ON_HARDWARE:
898         case ANEURALNETWORKS_FENCED_DURATION_IN_DRIVER:
899             break;
900         default:
901             LOG(ERROR) << "ANeuralNetworksExecution_getDuration passed a bad durationCode "
902                        << durationCode;
903             return ANEURALNETWORKS_BAD_DATA;
904     }
905     const ExecutionBuilder* r = reinterpret_cast<const ExecutionBuilder*>(execution);
906     return r->getDuration(durationCode, duration);
907 }
908 
ANeuralNetworksBurst_create(ANeuralNetworksCompilation * compilation,ANeuralNetworksBurst ** burst)909 int ANeuralNetworksBurst_create(ANeuralNetworksCompilation* compilation,
910                                 ANeuralNetworksBurst** burst) {
911     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksBurst_create");
912     if (!compilation || !burst) {
913         LOG(ERROR) << "ANeuralNetworksBurst_create passed a nullptr";
914         return ANEURALNETWORKS_UNEXPECTED_NULL;
915     }
916 
917     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
918     BurstBuilder* b = nullptr;
919     int result = c->createBurst(&b);
920     *burst = reinterpret_cast<ANeuralNetworksBurst*>(b);
921     return result;
922 }
923 
ANeuralNetworksBurst_free(ANeuralNetworksBurst * burst)924 void ANeuralNetworksBurst_free(ANeuralNetworksBurst* burst) {
925     NNTRACE_RT(NNTRACE_PHASE_TERMINATION, "ANeuralNetworksBurst_free");
926     // No validation.  Free of nullptr is valid.
927     BurstBuilder* b = reinterpret_cast<BurstBuilder*>(burst);
928     delete b;
929 }
930 
ANeuralNetworksExecution_burstCompute(ANeuralNetworksExecution * execution,ANeuralNetworksBurst * burst)931 int ANeuralNetworksExecution_burstCompute(ANeuralNetworksExecution* execution,
932                                           ANeuralNetworksBurst* burst) {
933     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_burstCompute");
934     if (!execution || !burst) {
935         LOG(ERROR) << "ANeuralNetworksExecution_burstCompute passed a nullptr";
936         return ANEURALNETWORKS_UNEXPECTED_NULL;
937     }
938 
939     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
940     BurstBuilder* b = reinterpret_cast<BurstBuilder*>(burst);
941 
942     if (r->getCompilation() != b->getCompilation()) {
943         LOG(ERROR) << "ANeuralNetworksBurst and ANeuralNetworksExecution "
944                       "used in ANeuralNetworksExecution_burstCompute must "
945                       "originate from the same ANeuralNetworksCompilation";
946         return ANEURALNETWORKS_BAD_DATA;
947     }
948 
949     const bool locked = b->tryLock();
950     if (!locked) {
951         LOG(ERROR) << "ANeuralNetworksBurst is already being used in another "
952                       "call to ANeuralNetworksExecution_burstCompute";
953         return ANEURALNETWORKS_BAD_STATE;
954     }
955 
956     const int n = r->burstCompute(b);
957     b->unlock();
958 
959     return n;
960 }
961 
ANeuralNetworksMemoryDesc_create(ANeuralNetworksMemoryDesc ** desc)962 int ANeuralNetworksMemoryDesc_create(ANeuralNetworksMemoryDesc** desc) {
963     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksMemoryDesc_create");
964     if (desc != nullptr) {
965         *desc = nullptr;
966     }
967     if (!desc) {
968         LOG(ERROR) << "ANeuralNetworksMemoryDesc_create passed a nullptr";
969         return ANEURALNETWORKS_UNEXPECTED_NULL;
970     }
971     auto mb = std::make_unique<MemoryBuilder>();
972     *desc = reinterpret_cast<ANeuralNetworksMemoryDesc*>(mb.release());
973     return ANEURALNETWORKS_NO_ERROR;
974 }
975 
ANeuralNetworksMemoryDesc_free(ANeuralNetworksMemoryDesc * desc)976 void ANeuralNetworksMemoryDesc_free(ANeuralNetworksMemoryDesc* desc) {
977     NNTRACE_RT(NNTRACE_PHASE_TERMINATION, "ANeuralNetworksMemoryDesc_free");
978     // No validation.  Free of nullptr is valid.
979     MemoryBuilder* mb = reinterpret_cast<MemoryBuilder*>(desc);
980     delete mb;
981 }
982 
ANeuralNetworksMemoryDesc_addInputRole(ANeuralNetworksMemoryDesc * desc,const ANeuralNetworksCompilation * compilation,uint32_t index,float frequency)983 int ANeuralNetworksMemoryDesc_addInputRole(ANeuralNetworksMemoryDesc* desc,
984                                            const ANeuralNetworksCompilation* compilation,
985                                            uint32_t index, float frequency) {
986     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksMemoryDesc_addInputRole");
987     if (!desc || !compilation) {
988         LOG(ERROR) << "ANeuralNetworksMemoryDesc_addInputRole passed a nullptr";
989         return ANEURALNETWORKS_UNEXPECTED_NULL;
990     }
991     MemoryBuilder* mb = reinterpret_cast<MemoryBuilder*>(desc);
992     const CompilationBuilder* c = reinterpret_cast<const CompilationBuilder*>(compilation);
993     return mb->addRole(*c, IOType::INPUT, index, frequency);
994 }
995 
ANeuralNetworksMemoryDesc_addOutputRole(ANeuralNetworksMemoryDesc * desc,const ANeuralNetworksCompilation * compilation,uint32_t index,float frequency)996 int ANeuralNetworksMemoryDesc_addOutputRole(ANeuralNetworksMemoryDesc* desc,
997                                             const ANeuralNetworksCompilation* compilation,
998                                             uint32_t index, float frequency) {
999     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksMemoryDesc_addOutputRole");
1000     if (!desc || !compilation) {
1001         LOG(ERROR) << "ANeuralNetworksMemoryDesc_addOutputRole passed a nullptr";
1002         return ANEURALNETWORKS_UNEXPECTED_NULL;
1003     }
1004     MemoryBuilder* mb = reinterpret_cast<MemoryBuilder*>(desc);
1005     const CompilationBuilder* c = reinterpret_cast<const CompilationBuilder*>(compilation);
1006     return mb->addRole(*c, IOType::OUTPUT, index, frequency);
1007 }
1008 
ANeuralNetworksMemoryDesc_setDimensions(ANeuralNetworksMemoryDesc * desc,uint32_t rank,const uint32_t * dimensions)1009 int ANeuralNetworksMemoryDesc_setDimensions(ANeuralNetworksMemoryDesc* desc, uint32_t rank,
1010                                             const uint32_t* dimensions) {
1011     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksMemoryDesc_setDimensions");
1012     if (!desc || (!dimensions && rank > 0)) {
1013         LOG(ERROR) << "ANeuralNetworksMemoryDesc_setDimensions passed a nullptr";
1014         return ANEURALNETWORKS_UNEXPECTED_NULL;
1015     }
1016     const std::vector<uint32_t> dims(dimensions, dimensions + rank);
1017     MemoryBuilder* mb = reinterpret_cast<MemoryBuilder*>(desc);
1018     return mb->setDimensions(dims);
1019 }
1020 
ANeuralNetworksMemoryDesc_finish(ANeuralNetworksMemoryDesc * desc)1021 int ANeuralNetworksMemoryDesc_finish(ANeuralNetworksMemoryDesc* desc) {
1022     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksMemoryDesc_finish");
1023     if (!desc) {
1024         LOG(ERROR) << "ANeuralNetworksMemoryDesc_finish passed a nullptr";
1025         return ANEURALNETWORKS_UNEXPECTED_NULL;
1026     }
1027     MemoryBuilder* mb = reinterpret_cast<MemoryBuilder*>(desc);
1028     return mb->finish();
1029 }
1030 
ANeuralNetworksMemory_createFromDesc(const ANeuralNetworksMemoryDesc * desc,ANeuralNetworksMemory ** memory)1031 int ANeuralNetworksMemory_createFromDesc(const ANeuralNetworksMemoryDesc* desc,
1032                                          ANeuralNetworksMemory** memory) {
1033     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksMemory_createFromDesc");
1034     if (memory != nullptr) {
1035         *memory = nullptr;
1036     }
1037     if (!desc || !memory) {
1038         LOG(ERROR) << "ANeuralNetworksMemory_createFromDesc passed a nullptr";
1039         return ANEURALNETWORKS_UNEXPECTED_NULL;
1040     }
1041     const MemoryBuilder* mb = reinterpret_cast<const MemoryBuilder*>(desc);
1042     auto [n, m] = mb->allocate();
1043     if (n != ANEURALNETWORKS_NO_ERROR) {
1044         return n;
1045     }
1046     *memory = reinterpret_cast<ANeuralNetworksMemory*>(m.release());
1047     return ANEURALNETWORKS_NO_ERROR;
1048 }
1049 
ANeuralNetworksMemory_copy(const ANeuralNetworksMemory * src,const ANeuralNetworksMemory * dst)1050 int ANeuralNetworksMemory_copy(const ANeuralNetworksMemory* src, const ANeuralNetworksMemory* dst) {
1051     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksMemory_copy");
1052     if (!src || !dst) {
1053         LOG(ERROR) << "ANeuralNetworksMemory_copy passed a nullptr";
1054         return ANEURALNETWORKS_UNEXPECTED_NULL;
1055     }
1056     const RuntimeMemory* s = reinterpret_cast<const RuntimeMemory*>(src);
1057     const RuntimeMemory* d = reinterpret_cast<const RuntimeMemory*>(dst);
1058     return RuntimeMemory::copy(*s, *d);
1059 }
1060 
ANeuralNetworksMemory_createFromFd(size_t size,int prot,int fd,size_t offset,ANeuralNetworksMemory ** memory)1061 int ANeuralNetworksMemory_createFromFd(size_t size, int prot, int fd, size_t offset,
1062                                        ANeuralNetworksMemory** memory) {
1063     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksMemory_createFromFd");
1064     if (memory != nullptr) {
1065         *memory = nullptr;
1066     }
1067     if (!memory) {
1068         LOG(ERROR) << "ANeuralNetworksMemory_createFromFd passed a nullptr";
1069         return ANEURALNETWORKS_UNEXPECTED_NULL;
1070     }
1071     int n = ANEURALNETWORKS_NO_ERROR;
1072     std::unique_ptr<MemoryFd> m;
1073     std::tie(n, m) = MemoryFd::create(size, prot, fd, offset);
1074     if (n != ANEURALNETWORKS_NO_ERROR) {
1075         return n;
1076     }
1077     *memory = reinterpret_cast<ANeuralNetworksMemory*>(m.release());
1078     return ANEURALNETWORKS_NO_ERROR;
1079 }
1080 
ANeuralNetworksMemory_createFromAHardwareBuffer(const AHardwareBuffer * ahwb,ANeuralNetworksMemory ** memory)1081 int ANeuralNetworksMemory_createFromAHardwareBuffer(const AHardwareBuffer* ahwb,
1082                                                     ANeuralNetworksMemory** memory) {
1083     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksMemory_createFromAHardwareBuffer");
1084     if (memory != nullptr) {
1085         *memory = nullptr;
1086     }
1087     if (!ahwb || !memory) {
1088         LOG(ERROR) << "ANeuralNetworksMemory_createFromAHardwareBuffer passed a nullptr";
1089         return ANEURALNETWORKS_UNEXPECTED_NULL;
1090     }
1091     int n = ANEURALNETWORKS_NO_ERROR;
1092     std::unique_ptr<MemoryAHWB> m;
1093     std::tie(n, m) = MemoryAHWB::create(*ahwb);
1094     if (n != ANEURALNETWORKS_NO_ERROR) {
1095         return n;
1096     }
1097     *memory = reinterpret_cast<ANeuralNetworksMemory*>(m.release());
1098     return ANEURALNETWORKS_NO_ERROR;
1099 }
1100 
ANeuralNetworksMemory_free(ANeuralNetworksMemory * memory)1101 void ANeuralNetworksMemory_free(ANeuralNetworksMemory* memory) {
1102     NNTRACE_RT(NNTRACE_PHASE_TERMINATION, "ANeuralNetworksMemory_free");
1103     // No validation.  Free of nullptr is valid.
1104     RuntimeMemory* m = reinterpret_cast<RuntimeMemory*>(memory);
1105     delete m;
1106 }
1107 
ANeuralNetworksModel_create(ANeuralNetworksModel ** model)1108 int ANeuralNetworksModel_create(ANeuralNetworksModel** model) {
1109     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_create");
1110     initVLogMask();
1111     if (!model) {
1112         LOG(ERROR) << "ANeuralNetworksModel_create passed a nullptr";
1113         return ANEURALNETWORKS_UNEXPECTED_NULL;
1114     }
1115     ModelBuilder* m = new (std::nothrow) ModelBuilder();
1116     if (m == nullptr) {
1117         *model = nullptr;
1118         return ANEURALNETWORKS_OUT_OF_MEMORY;
1119     }
1120     *model = reinterpret_cast<ANeuralNetworksModel*>(m);
1121     return ANEURALNETWORKS_NO_ERROR;
1122 }
1123 
ANeuralNetworksModel_free(ANeuralNetworksModel * model)1124 void ANeuralNetworksModel_free(ANeuralNetworksModel* model) {
1125     NNTRACE_RT(NNTRACE_PHASE_TERMINATION, "ANeuralNetworksModel_free");
1126     // No validation.  Free of nullptr is valid.
1127     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1128     delete m;
1129 }
1130 
ANeuralNetworksModel_finish(ANeuralNetworksModel * model)1131 int ANeuralNetworksModel_finish(ANeuralNetworksModel* model) {
1132     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_finish");
1133     if (!model) {
1134         LOG(ERROR) << "ANeuralNetworksModel_finish passed a nullptr";
1135         return ANEURALNETWORKS_UNEXPECTED_NULL;
1136     }
1137     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1138     return m->finish();
1139 }
1140 
ANeuralNetworksModel_addOperand(ANeuralNetworksModel * model,const ANeuralNetworksOperandType * type)1141 int ANeuralNetworksModel_addOperand(ANeuralNetworksModel* model,
1142                                     const ANeuralNetworksOperandType* type) {
1143     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_addOperand");
1144     if (!model || !type) {
1145         LOG(ERROR) << "ANeuralNetworksModel_addOperand passed a nullptr";
1146         return ANEURALNETWORKS_UNEXPECTED_NULL;
1147     }
1148     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1149     return m->addOperand(*type);
1150 }
1151 
ANeuralNetworksModel_setOperandValue(ANeuralNetworksModel * model,int32_t index,const void * buffer,size_t length)1152 int ANeuralNetworksModel_setOperandValue(ANeuralNetworksModel* model, int32_t index,
1153                                          const void* buffer, size_t length) {
1154     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_setOperandValue");
1155     if (!model || (!buffer && length != 0)) {
1156         LOG(ERROR) << "ANeuralNetworksModel_setOperandValue passed a nullptr";
1157         return ANEURALNETWORKS_UNEXPECTED_NULL;
1158     }
1159     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1160     return m->setOperandValue(index, buffer, length);
1161 }
1162 
ANeuralNetworksModel_setOperandValueFromMemory(ANeuralNetworksModel * model,int32_t index,const ANeuralNetworksMemory * memory,size_t offset,size_t length)1163 int ANeuralNetworksModel_setOperandValueFromMemory(ANeuralNetworksModel* model, int32_t index,
1164                                                    const ANeuralNetworksMemory* memory,
1165                                                    size_t offset, size_t length) {
1166     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_setOperandValueFromMemory");
1167     if (!model || !memory) {
1168         LOG(ERROR) << "ANeuralNetworksModel_setOperandValue passed a nullptr";
1169         return ANEURALNETWORKS_UNEXPECTED_NULL;
1170     }
1171     const RuntimeMemory* mem = reinterpret_cast<const RuntimeMemory*>(memory);
1172     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1173     return m->setOperandValueFromMemory(index, mem, offset, length);
1174 }
1175 
ANeuralNetworksModel_setOperandValueFromModel(ANeuralNetworksModel * model,int32_t index,const ANeuralNetworksModel * value)1176 int ANeuralNetworksModel_setOperandValueFromModel(ANeuralNetworksModel* model, int32_t index,
1177                                                   const ANeuralNetworksModel* value) {
1178     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_setOperandValueFromModel");
1179     if (!model || !value) {
1180         LOG(ERROR) << "ANeuralNetworksModel_setOperandValueFromModel passed a nullptr";
1181         return ANEURALNETWORKS_UNEXPECTED_NULL;
1182     }
1183     const ModelBuilder* val = reinterpret_cast<const ModelBuilder*>(value);
1184     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1185     return m->setOperandValueFromModel(index, val);
1186 }
1187 
ANeuralNetworksModel_addOperation(ANeuralNetworksModel * model,ANeuralNetworksOperationType type,uint32_t inputCount,const uint32_t * inputs,uint32_t outputCount,const uint32_t * outputs)1188 int ANeuralNetworksModel_addOperation(ANeuralNetworksModel* model,
1189                                       ANeuralNetworksOperationType type, uint32_t inputCount,
1190                                       const uint32_t* inputs, uint32_t outputCount,
1191                                       const uint32_t* outputs) {
1192     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_addOperation");
1193     if (!model || !inputs || !outputs) {
1194         LOG(ERROR) << "ANeuralNetworksModel_addOperation passed a nullptr";
1195         return ANEURALNETWORKS_UNEXPECTED_NULL;
1196     }
1197     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1198     return m->addOperation(type, inputCount, inputs, outputCount, outputs);
1199 }
1200 
ANeuralNetworksModel_setOperandSymmPerChannelQuantParams(ANeuralNetworksModel * model,int32_t index,const ANeuralNetworksSymmPerChannelQuantParams * channelQuant)1201 int ANeuralNetworksModel_setOperandSymmPerChannelQuantParams(
1202         ANeuralNetworksModel* model, int32_t index,
1203         const ANeuralNetworksSymmPerChannelQuantParams* channelQuant) {
1204     NNTRACE_RT(NNTRACE_PHASE_PREPARATION,
1205                "ANeuralNetworksModel_setOperandSymmPerChannelQuantParams");
1206     if (!model || !channelQuant) {
1207         LOG(ERROR) << "ANeuralNetworksModel_setOperandSymmPerChannelQuantParams passed a nullptr";
1208         return ANEURALNETWORKS_UNEXPECTED_NULL;
1209     }
1210     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1211     return m->setOperandSymmPerChannelQuantParams(index, *channelQuant);
1212 }
1213 
ANeuralNetworksModel_identifyInputsAndOutputs(ANeuralNetworksModel * model,uint32_t inputCount,const uint32_t * inputs,uint32_t outputCount,const uint32_t * outputs)1214 int ANeuralNetworksModel_identifyInputsAndOutputs(ANeuralNetworksModel* model, uint32_t inputCount,
1215                                                   const uint32_t* inputs, uint32_t outputCount,
1216                                                   const uint32_t* outputs) {
1217     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_identifyInputsAndOutputs");
1218     if (!model || !inputs || !outputs) {
1219         LOG(ERROR) << ("ANeuralNetworksModel_identifyInputsAndOutputs passed a nullptr");
1220         return ANEURALNETWORKS_UNEXPECTED_NULL;
1221     }
1222     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1223     return m->identifyInputsAndOutputs(inputCount, inputs, outputCount, outputs);
1224 }
1225 
ANeuralNetworksModel_relaxComputationFloat32toFloat16(ANeuralNetworksModel * model,bool allow)1226 int ANeuralNetworksModel_relaxComputationFloat32toFloat16(ANeuralNetworksModel* model, bool allow) {
1227     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_relaxComputationFloat32toFloat16");
1228     if (!model) {
1229         LOG(ERROR) << ("ANeuralNetworksModel_relaxComputationFloat32toFloat16 passed a nullptr");
1230         return ANEURALNETWORKS_UNEXPECTED_NULL;
1231     }
1232     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1233     return m->relaxComputationFloat32toFloat16(allow);
1234 }
1235 
ANeuralNetworksCompilation_create(ANeuralNetworksModel * model,ANeuralNetworksCompilation ** compilation)1236 int ANeuralNetworksCompilation_create(ANeuralNetworksModel* model,
1237                                       ANeuralNetworksCompilation** compilation) {
1238     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_create");
1239     if (!model || !compilation) {
1240         LOG(ERROR) << "ANeuralNetworksCompilation_create passed a nullptr";
1241         return ANEURALNETWORKS_UNEXPECTED_NULL;
1242     }
1243 
1244     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1245     CompilationBuilder* c = nullptr;
1246 
1247     const auto& drivers = DeviceManager::get()->getDrivers();
1248 
1249     int result = m->createCompilation(&c, drivers);
1250     *compilation = reinterpret_cast<ANeuralNetworksCompilation*>(c);
1251     return result;
1252 }
1253 
ANeuralNetworksCompilation_free(ANeuralNetworksCompilation * compilation)1254 void ANeuralNetworksCompilation_free(ANeuralNetworksCompilation* compilation) {
1255     NNTRACE_RT(NNTRACE_PHASE_TERMINATION, "ANeuralNetworksCompilation_free");
1256     // No validation.  Free of nullptr is valid.
1257     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1258     delete c;
1259 }
1260 
ANeuralNetworksCompilation_setPreference(ANeuralNetworksCompilation * compilation,int32_t preference)1261 int ANeuralNetworksCompilation_setPreference(ANeuralNetworksCompilation* compilation,
1262                                              int32_t preference) {
1263     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_setPreference");
1264     if (!compilation) {
1265         LOG(ERROR) << "ANeuralNetworksCompilation_setPreference passed a nullptr";
1266         return ANEURALNETWORKS_UNEXPECTED_NULL;
1267     }
1268     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1269     return c->setPreference(preference);
1270 }
1271 
ANeuralNetworksCompilation_setCaching(ANeuralNetworksCompilation * compilation,const char * cacheDir,const uint8_t * token)1272 int ANeuralNetworksCompilation_setCaching(ANeuralNetworksCompilation* compilation,
1273                                           const char* cacheDir, const uint8_t* token) {
1274     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_setCaching");
1275     if (!compilation || !cacheDir || !token) {
1276         LOG(ERROR) << "ANeuralNetworksCompilation_setCaching passed a nullptr";
1277         return ANEURALNETWORKS_UNEXPECTED_NULL;
1278     }
1279     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1280     return c->setCaching(cacheDir, token);
1281 }
1282 
ANeuralNetworksCompilation_finish(ANeuralNetworksCompilation * compilation)1283 int ANeuralNetworksCompilation_finish(ANeuralNetworksCompilation* compilation) {
1284     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_finish");
1285     if (!compilation) {
1286         LOG(ERROR) << "ANeuralNetworksCompilation_finish passed a nullptr";
1287         return ANEURALNETWORKS_UNEXPECTED_NULL;
1288     }
1289     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1290     int result = c->finish();
1291     telemetry::onCompilationFinish(c, result);
1292 
1293     return result;
1294 }
1295 
ANeuralNetworksCompilation_setPriority(ANeuralNetworksCompilation * compilation,int priority)1296 int ANeuralNetworksCompilation_setPriority(ANeuralNetworksCompilation* compilation, int priority) {
1297     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_setPriority");
1298     if (!compilation) {
1299         LOG(ERROR) << "ANeuralNetworksCompilation_setPriority passed a nullptr";
1300         return ANEURALNETWORKS_UNEXPECTED_NULL;
1301     }
1302     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1303     return c->setPriority(priority);
1304 }
1305 
ANeuralNetworksCompilation_setTimeout(ANeuralNetworksCompilation * compilation,uint64_t duration)1306 int ANeuralNetworksCompilation_setTimeout(ANeuralNetworksCompilation* compilation,
1307                                           uint64_t duration) {
1308     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_setTimeout");
1309     if (!compilation) {
1310         LOG(ERROR) << "ANeuralNetworksCompilation_setTimeout passed a nullptr";
1311         return ANEURALNETWORKS_UNEXPECTED_NULL;
1312     }
1313     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1314     return c->setTimeoutDuration(duration);
1315 }
1316 
ANeuralNetworksExecution_create(ANeuralNetworksCompilation * compilation,ANeuralNetworksExecution ** execution)1317 int ANeuralNetworksExecution_create(ANeuralNetworksCompilation* compilation,
1318                                     ANeuralNetworksExecution** execution) {
1319     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_create");
1320     if (!compilation || !execution) {
1321         LOG(ERROR) << "ANeuralNetworksExecution_create passed a nullptr";
1322         return ANEURALNETWORKS_UNEXPECTED_NULL;
1323     }
1324 
1325     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1326     ExecutionBuilder* r = nullptr;
1327     int result = c->createExecution(&r);
1328     *execution = reinterpret_cast<ANeuralNetworksExecution*>(r);
1329     return result;
1330 }
1331 
ANeuralNetworksExecution_free(ANeuralNetworksExecution * execution)1332 void ANeuralNetworksExecution_free(ANeuralNetworksExecution* execution) {
1333     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_free");
1334     // Free of nullptr is valid.
1335     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1336     if (r && r->inFlight()) {
1337         LOG(ERROR) << "ANeuralNetworksExecution_free passed an in-flight ANeuralNetworksExecution"
1338                    << " and is therefore ignored";
1339         return;
1340     }
1341     delete r;
1342 }
1343 
ANeuralNetworksExecution_getOutputOperandRank(ANeuralNetworksExecution * execution,int32_t index,uint32_t * rank)1344 int ANeuralNetworksExecution_getOutputOperandRank(ANeuralNetworksExecution* execution,
1345                                                   int32_t index, uint32_t* rank) {
1346     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_getOutputOperandRank");
1347     if (!execution || !rank) {
1348         LOG(ERROR) << "ANeuralNetworksExecution_getOutputOperandRank passed a nullptr";
1349         return ANEURALNETWORKS_UNEXPECTED_NULL;
1350     }
1351     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1352     return r->getOutputOperandRank(index, rank);
1353 }
1354 
ANeuralNetworksExecution_getOutputOperandDimensions(ANeuralNetworksExecution * execution,int32_t index,uint32_t * dimensions)1355 int ANeuralNetworksExecution_getOutputOperandDimensions(ANeuralNetworksExecution* execution,
1356                                                         int32_t index, uint32_t* dimensions) {
1357     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_getOutputOperandDimensions");
1358     if (!execution || !dimensions) {
1359         LOG(ERROR) << "ANeuralNetworksExecution_getOutputOperandDimensions passed a nullptr";
1360         return ANEURALNETWORKS_UNEXPECTED_NULL;
1361     }
1362     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1363     return r->getOutputOperandDimensions(index, dimensions);
1364 }
1365 
ANeuralNetworksExecution_setInput(ANeuralNetworksExecution * execution,int32_t index,const ANeuralNetworksOperandType * type,const void * buffer,size_t length)1366 int ANeuralNetworksExecution_setInput(ANeuralNetworksExecution* execution, int32_t index,
1367                                       const ANeuralNetworksOperandType* type, const void* buffer,
1368                                       size_t length) {
1369     NNTRACE_RT(NNTRACE_PHASE_INPUTS_AND_OUTPUTS, "ANeuralNetworksExecution_setInput");
1370     if (!execution || (!buffer && length != 0)) {
1371         LOG(ERROR) << "ANeuralNetworksExecution_setInput passed a nullptr";
1372         return ANEURALNETWORKS_UNEXPECTED_NULL;
1373     }
1374     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1375     return r->setInput(index, type, buffer, length);
1376 }
1377 
ANeuralNetworksExecution_setInputFromMemory(ANeuralNetworksExecution * execution,int32_t index,const ANeuralNetworksOperandType * type,const ANeuralNetworksMemory * memory,size_t offset,size_t length)1378 int ANeuralNetworksExecution_setInputFromMemory(ANeuralNetworksExecution* execution, int32_t index,
1379                                                 const ANeuralNetworksOperandType* type,
1380                                                 const ANeuralNetworksMemory* memory, size_t offset,
1381                                                 size_t length) {
1382     NNTRACE_RT(NNTRACE_PHASE_INPUTS_AND_OUTPUTS, "ANeuralNetworksExecution_setInputFromMemory");
1383     if (!execution || !memory) {
1384         LOG(ERROR) << "ANeuralNetworksExecution_setInputFromMemory passed a nullptr";
1385         return ANEURALNETWORKS_UNEXPECTED_NULL;
1386     }
1387 
1388     const RuntimeMemory* m = reinterpret_cast<const RuntimeMemory*>(memory);
1389     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1390     return r->setInputFromMemory(index, type, m, offset, length);
1391 }
1392 
ANeuralNetworksExecution_setOutput(ANeuralNetworksExecution * execution,int32_t index,const ANeuralNetworksOperandType * type,void * buffer,size_t length)1393 int ANeuralNetworksExecution_setOutput(ANeuralNetworksExecution* execution, int32_t index,
1394                                        const ANeuralNetworksOperandType* type, void* buffer,
1395                                        size_t length) {
1396     NNTRACE_RT(NNTRACE_PHASE_INPUTS_AND_OUTPUTS, "ANeuralNetworksExecution_setOutput");
1397     if (!execution || (!buffer && length != 0)) {
1398         LOG(ERROR) << "ANeuralNetworksExecution_setOutput passed a nullptr";
1399         return ANEURALNETWORKS_UNEXPECTED_NULL;
1400     }
1401     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1402     return r->setOutput(index, type, buffer, length);
1403 }
1404 
ANeuralNetworksExecution_setOutputFromMemory(ANeuralNetworksExecution * execution,int32_t index,const ANeuralNetworksOperandType * type,const ANeuralNetworksMemory * memory,size_t offset,size_t length)1405 int ANeuralNetworksExecution_setOutputFromMemory(ANeuralNetworksExecution* execution, int32_t index,
1406                                                  const ANeuralNetworksOperandType* type,
1407                                                  const ANeuralNetworksMemory* memory, size_t offset,
1408                                                  size_t length) {
1409     NNTRACE_RT(NNTRACE_PHASE_INPUTS_AND_OUTPUTS, "ANeuralNetworksExecution_setOutputFromMemory");
1410     if (!execution || !memory) {
1411         LOG(ERROR) << "ANeuralNetworksExecution_setOutputFromMemory passed a nullptr";
1412         return ANEURALNETWORKS_UNEXPECTED_NULL;
1413     }
1414 
1415     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1416     const RuntimeMemory* m = reinterpret_cast<const RuntimeMemory*>(memory);
1417     return r->setOutputFromMemory(index, type, m, offset, length);
1418 }
1419 
ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution * execution,ANeuralNetworksEvent ** event)1420 int ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution* execution,
1421                                           ANeuralNetworksEvent** event) {
1422     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_startCompute");
1423     if (!event) {
1424         LOG(ERROR) << "ANeuralNetworksExecution_startCompute passed a nullptr";
1425         return ANEURALNETWORKS_UNEXPECTED_NULL;
1426     }
1427     if (!execution) {
1428         LOG(ERROR) << "ANeuralNetworksExecution_startCompute passed a nullptr";
1429         *event = nullptr;
1430         return ANEURALNETWORKS_UNEXPECTED_NULL;
1431     }
1432     // TODO validate the rest
1433 
1434     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1435 
1436     std::shared_ptr<ExecutionCallback> callback;
1437     *event = nullptr;
1438 
1439     int n = r->computeAsynchronously(&callback);
1440     if (n != ANEURALNETWORKS_NO_ERROR) {
1441         return n;
1442     }
1443     auto e = std::make_unique<CallbackEvent>(std::move(callback));
1444     *event = reinterpret_cast<ANeuralNetworksEvent*>(e.release());
1445     return ANEURALNETWORKS_NO_ERROR;
1446 }
1447 
ANeuralNetworksExecution_setTimeout(ANeuralNetworksExecution * execution,uint64_t duration)1448 int ANeuralNetworksExecution_setTimeout(ANeuralNetworksExecution* execution, uint64_t duration) {
1449     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_setTimeout");
1450     if (!execution) {
1451         LOG(ERROR) << "ANeuralNetworksExecution_setTimeout passed a nullptr";
1452         return ANEURALNETWORKS_UNEXPECTED_NULL;
1453     }
1454 
1455     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1456     return r->setTimeoutDuration(duration);
1457 }
1458 
ANeuralNetworksEvent_wait(ANeuralNetworksEvent * event)1459 int ANeuralNetworksEvent_wait(ANeuralNetworksEvent* event) {
1460     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksEvent_wait");
1461     if (event == nullptr) {
1462         LOG(ERROR) << "ANeuralNetworksEvent_wait passed a nullptr";
1463         return ANEURALNETWORKS_UNEXPECTED_NULL;
1464     }
1465 
1466     IEvent* e = reinterpret_cast<IEvent*>(event);
1467     return convertErrorStatusToResultCode(e->wait());
1468 }
1469 
ANeuralNetworksEvent_free(ANeuralNetworksEvent * event)1470 void ANeuralNetworksEvent_free(ANeuralNetworksEvent* event) {
1471     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksEvent_free");
1472     // No validation.  Free of nullptr is valid.
1473     if (event) {
1474         IEvent* e = reinterpret_cast<IEvent*>(event);
1475         e->wait();
1476         delete e;
1477     }
1478 }
1479 
ANeuralNetworksExecution_setLoopTimeout(ANeuralNetworksExecution * execution,uint64_t duration)1480 int ANeuralNetworksExecution_setLoopTimeout(ANeuralNetworksExecution* execution,
1481                                             uint64_t duration) {
1482     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_setLoopTimeout");
1483     if (!execution) {
1484         LOG(ERROR) << "ANeuralNetworksExecution_setLoopTimeout passed a nullptr";
1485         return ANEURALNETWORKS_UNEXPECTED_NULL;
1486     }
1487 
1488     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1489     return r->setLoopTimeout(duration);
1490 }
1491 
ANeuralNetworks_getDefaultLoopTimeout()1492 uint64_t ANeuralNetworks_getDefaultLoopTimeout() {
1493     return operation_while::kTimeoutNsDefault;
1494 }
1495 
ANeuralNetworks_getMaximumLoopTimeout()1496 uint64_t ANeuralNetworks_getMaximumLoopTimeout() {
1497     return operation_while::kTimeoutNsMaximum;
1498 }
1499 
ANeuralNetworksDevice_getExtensionSupport(const ANeuralNetworksDevice * device,const char * extensionName,bool * isExtensionSupported)1500 int ANeuralNetworksDevice_getExtensionSupport(const ANeuralNetworksDevice* device,
1501                                               const char* extensionName,
1502                                               bool* isExtensionSupported) {
1503     if (device == nullptr || extensionName == nullptr || isExtensionSupported == nullptr) {
1504         LOG(ERROR) << "ANeuralNetworksDevice_getExtensionSupport passed a nullptr";
1505         return ANEURALNETWORKS_UNEXPECTED_NULL;
1506     }
1507 
1508     const Device* d = reinterpret_cast<const Device*>(device);
1509     const auto& supportedExtensions = d->getSupportedExtensions();
1510     *isExtensionSupported = std::any_of(supportedExtensions.begin(), supportedExtensions.end(),
1511                                         [extensionName](const auto& supportedExtension) {
1512                                             return supportedExtension.name == extensionName;
1513                                         });
1514 
1515     return ANEURALNETWORKS_NO_ERROR;
1516 }
1517 
ANeuralNetworksModel_getExtensionOperandType(ANeuralNetworksModel * model,const char * extensionName,uint16_t operandCodeWithinExtension,int32_t * type)1518 int ANeuralNetworksModel_getExtensionOperandType(ANeuralNetworksModel* model,
1519                                                  const char* extensionName,
1520                                                  uint16_t operandCodeWithinExtension,
1521                                                  int32_t* type) {
1522     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_getExtensionOperandType");
1523     if (!model || !extensionName || !type) {
1524         LOG(ERROR) << "ANeuralNetworksModel_getExtensionOperandType passed a nullptr";
1525         return ANEURALNETWORKS_UNEXPECTED_NULL;
1526     }
1527     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1528     return m->getExtensionType(extensionName, operandCodeWithinExtension, type);
1529 }
1530 
ANeuralNetworksModel_getExtensionOperationType(ANeuralNetworksModel * model,const char * extensionName,uint16_t operationCodeWithinExtension,ANeuralNetworksOperationType * type)1531 int ANeuralNetworksModel_getExtensionOperationType(ANeuralNetworksModel* model,
1532                                                    const char* extensionName,
1533                                                    uint16_t operationCodeWithinExtension,
1534                                                    ANeuralNetworksOperationType* type) {
1535     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_getExtensionOperationType");
1536     if (!model || !extensionName || !type) {
1537         LOG(ERROR) << "ANeuralNetworksModel_getExtensionOperationType passed a nullptr";
1538         return ANEURALNETWORKS_UNEXPECTED_NULL;
1539     }
1540     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1541     return m->getExtensionType(extensionName, operationCodeWithinExtension, type);
1542 }
1543 
ANeuralNetworksModel_setOperandExtensionData(ANeuralNetworksModel * model,int32_t index,const void * data,size_t length)1544 int ANeuralNetworksModel_setOperandExtensionData(ANeuralNetworksModel* model, int32_t index,
1545                                                  const void* data, size_t length) {
1546     NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_setOperandExtensionData");
1547     if (!model || (!data && length != 0)) {
1548         LOG(ERROR) << "ANeuralNetworksModel_setOperandExtensionData passed a nullptr";
1549         return ANEURALNETWORKS_UNEXPECTED_NULL;
1550     }
1551     ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1552     return m->setOperandExtensionData(index, data, length);
1553 }
1554 
ANeuralNetworksCompilation_addExtensionAttribute(ANeuralNetworksCompilation * compilation,const char * extensionName,uint16_t attributeCodeWithinExtension,const void * data,size_t length)1555 int ANeuralNetworksCompilation_addExtensionAttribute(ANeuralNetworksCompilation* compilation,
1556                                                      const char* extensionName,
1557                                                      uint16_t attributeCodeWithinExtension,
1558                                                      const void* data, size_t length) {
1559     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_addExtensionAttribute");
1560     if (!compilation || !extensionName || (!data && length != 0)) {
1561         LOG(ERROR) << "ANeuralNetworksCompilation_addExtensionAttribute passed a nullptr";
1562         return ANEURALNETWORKS_UNEXPECTED_NULL;
1563     }
1564     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1565     return c->addExtensionAttribute(extensionName, attributeCodeWithinExtension, data, length);
1566 }
1567 
ANeuralNetworksExecution_addExtensionAttribute(ANeuralNetworksExecution * execution,const char * extensionName,uint16_t attributeCodeWithinExtension,const void * data,size_t length)1568 int ANeuralNetworksExecution_addExtensionAttribute(ANeuralNetworksExecution* execution,
1569                                                    const char* extensionName,
1570                                                    uint16_t attributeCodeWithinExtension,
1571                                                    const void* data, size_t length) {
1572     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_addExtensionAttribute");
1573     if (!execution || !extensionName || (!data && length != 0)) {
1574         LOG(ERROR) << "ANeuralNetworksExecution_addExtensionAttribute passed a nullptr";
1575         return ANEURALNETWORKS_UNEXPECTED_NULL;
1576     }
1577     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1578     return r->addExtensionAttribute(extensionName, attributeCodeWithinExtension, data, length);
1579 }
1580 
ANeuralNetworksEvent_createFromSyncFenceFd(int syncFenceFd,ANeuralNetworksEvent ** event)1581 int ANeuralNetworksEvent_createFromSyncFenceFd(int syncFenceFd, ANeuralNetworksEvent** event) {
1582     if (event == nullptr) {
1583         LOG(ERROR) << "ANeuralNetworksEvent_createFromSyncFenceFd passed a nullptr";
1584         return ANEURALNETWORKS_UNEXPECTED_NULL;
1585     }
1586     if (syncFenceFd <= 0) {
1587         LOG(ERROR) << "ANeuralNetworksEvent_createFromSyncFenceFd passed an invalid fd: "
1588                    << syncFenceFd;
1589         *event = nullptr;
1590         return ANEURALNETWORKS_BAD_DATA;
1591     }
1592     std::unique_ptr<SyncFenceEvent> e =
1593             std::make_unique<SyncFenceEvent>(syncFenceFd, nullptr, nullptr);
1594     *event = reinterpret_cast<ANeuralNetworksEvent*>(e.release());
1595     return ANEURALNETWORKS_NO_ERROR;
1596 }
1597 
ANeuralNetworksEvent_getSyncFenceFd(const ANeuralNetworksEvent * event,int * syncFenceFd)1598 int ANeuralNetworksEvent_getSyncFenceFd(const ANeuralNetworksEvent* event, int* syncFenceFd) {
1599     if (syncFenceFd == nullptr) {
1600         LOG(ERROR) << "ANeuralNetworksEvent_getSyncFenceFd passed a nullptr";
1601         return ANEURALNETWORKS_UNEXPECTED_NULL;
1602     }
1603     *syncFenceFd = -1;
1604     if (event == nullptr) {
1605         LOG(ERROR) << "ANeuralNetworksEvent_getSyncFenceFd passed a nullptr";
1606         return ANEURALNETWORKS_UNEXPECTED_NULL;
1607     }
1608     const IEvent* e = reinterpret_cast<const IEvent*>(event);
1609     // The client owns the dupped fd, and is responsible for closing it.
1610     *syncFenceFd = e->getSyncFenceFd(/*shouldDup*/ true);
1611     if (*syncFenceFd <= 0) {
1612         LOG(ERROR) << "ANeuralNetworksEvent_getSyncFenceFd unable to get valid sync_fence fd";
1613         *syncFenceFd = -1;
1614         return ANEURALNETWORKS_BAD_DATA;
1615     }
1616     return ANEURALNETWORKS_NO_ERROR;
1617 }
1618 
ANeuralNetworksExecution_startComputeWithDependencies(ANeuralNetworksExecution * execution,const ANeuralNetworksEvent * const * dependencies,uint32_t numOfDependencies,uint64_t duration,ANeuralNetworksEvent ** event)1619 int ANeuralNetworksExecution_startComputeWithDependencies(
1620         ANeuralNetworksExecution* execution, const ANeuralNetworksEvent* const* dependencies,
1621         uint32_t numOfDependencies, uint64_t duration, ANeuralNetworksEvent** event) {
1622     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_startComputeWithDependencies");
1623     if (!event) {
1624         LOG(ERROR) << "ANeuralNetworksExecution_startComputeWithDependencies passed a nullptr";
1625         return ANEURALNETWORKS_UNEXPECTED_NULL;
1626     }
1627     if ((!dependencies && numOfDependencies != 0) || !execution) {
1628         LOG(ERROR) << "ANeuralNetworksExecution_startComputeWithDependencies passed a nullptr";
1629         *event = nullptr;
1630         return ANEURALNETWORKS_UNEXPECTED_NULL;
1631     }
1632     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1633 
1634     std::vector<int> waitForList;
1635     for (uint32_t i = 0; i < numOfDependencies; i++) {
1636         if (!dependencies[i]) {
1637             LOG(ERROR) << "ANeuralNetworksExecution_startComputeWithDependencies passed a nullptr";
1638             *event = nullptr;
1639             return ANEURALNETWORKS_UNEXPECTED_NULL;
1640         }
1641         const IEvent* e = reinterpret_cast<const IEvent*>(dependencies[i]);
1642         int syncFenceFd = e->getSyncFenceFd(/*should_dup*/ false);
1643         if (syncFenceFd < 0) {
1644             e->wait();
1645         } else {
1646             waitForList.push_back(syncFenceFd);
1647         }
1648     }
1649 
1650     if (r->getCompilation()->hasDynamicTemporaries()) {
1651         // The current implementation of fenced execution does not support
1652         // dynamic temporaries.  Fall back to non fenced execution.
1653         LOG(INFO) << "ANeuralNetworksExecution_startComputeWithDependencies falling back"
1654                   << " to ANeuralNetworksExecution_startCompute"
1655                   << " because of boundary operands of unknown size";
1656         for (int syncFenceFd : waitForList) {
1657             if (syncFenceFd > 0) {
1658                 auto w = syncWait(syncFenceFd, -1);
1659                 if (w != FenceState::SIGNALED) {
1660                     VLOG(EXECUTION) << "syncWait failed, fd: " << syncFenceFd;
1661                     *event = nullptr;
1662                     return ANEURALNETWORKS_OP_FAILED;
1663                 }
1664             }
1665         }
1666         return ANeuralNetworksExecution_startCompute(execution, event);
1667     }
1668 
1669     int syncFenceToSignal = -1;
1670     int n = r->computeFenced(waitForList, duration, &syncFenceToSignal);
1671     std::unique_ptr<SyncFenceEvent> e = std::make_unique<SyncFenceEvent>(
1672             syncFenceToSignal, r->getExecuteFencedInfoCallback(),
1673             // TODO(miaowang): support dynamic output shape only with memory domain.
1674             // For now just return empty output shapes.
1675             [r](ErrorStatus status) {
1676                 return r->finishComputation(status, {}, ExecutionMode::ASYNC_WITH_DEPS);
1677             });
1678     close(syncFenceToSignal);
1679     if (n != ANEURALNETWORKS_NO_ERROR) {
1680         *event = nullptr;
1681     } else {
1682         *event = reinterpret_cast<ANeuralNetworksEvent*>(e.release());
1683     }
1684     return n;
1685 }
1686 
ANeuralNetworks_getRuntimeFeatureLevel()1687 int64_t ANeuralNetworks_getRuntimeFeatureLevel() {
1688     return getRuntimeFeatureLevelImpl();
1689 }
1690 
ANeuralNetworksExecution_enableInputAndOutputPadding(ANeuralNetworksExecution * execution,bool enable)1691 int ANeuralNetworksExecution_enableInputAndOutputPadding(ANeuralNetworksExecution* execution,
1692                                                          bool enable) {
1693     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_enableInputAndOutputPadding");
1694     if (!execution) {
1695         LOG(ERROR) << "ANeuralNetworksExecution_enableInputAndOutputPadding passed a nullptr";
1696         return ANEURALNETWORKS_UNEXPECTED_NULL;
1697     }
1698     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1699     return r->enableInputAndOutputPadding(enable);
1700 }
1701 
ANeuralNetworksCompilation_getPreferredMemoryAlignmentForInput(const ANeuralNetworksCompilation * compilation,uint32_t index,uint32_t * alignment)1702 int ANeuralNetworksCompilation_getPreferredMemoryAlignmentForInput(
1703         const ANeuralNetworksCompilation* compilation, uint32_t index, uint32_t* alignment) {
1704     NNTRACE_RT(NNTRACE_PHASE_COMPILATION,
1705                "ANeuralNetworksCompilation_getPreferredMemoryAlignmentForInput");
1706     if (!compilation || !alignment) {
1707         LOG(ERROR) << "ANeuralNetworksCompilation_getPreferredMemoryAlignmentForInput passed a "
1708                       "nullptr";
1709         return ANEURALNETWORKS_UNEXPECTED_NULL;
1710     }
1711     const CompilationBuilder* c = reinterpret_cast<const CompilationBuilder*>(compilation);
1712     return c->getPreferredMemoryAlignmentForInput(index, alignment);
1713 }
1714 
ANeuralNetworksCompilation_getPreferredMemoryPaddingForInput(const ANeuralNetworksCompilation * compilation,uint32_t index,uint32_t * padding)1715 int ANeuralNetworksCompilation_getPreferredMemoryPaddingForInput(
1716         const ANeuralNetworksCompilation* compilation, uint32_t index, uint32_t* padding) {
1717     NNTRACE_RT(NNTRACE_PHASE_COMPILATION,
1718                "ANeuralNetworksCompilation_getPreferredMemoryPaddingForInput");
1719     if (!compilation || !padding) {
1720         LOG(ERROR) << "ANeuralNetworksCompilation_getPreferredMemoryPaddingForInput passed a "
1721                       "nullptr";
1722         return ANEURALNETWORKS_UNEXPECTED_NULL;
1723     }
1724     const CompilationBuilder* c = reinterpret_cast<const CompilationBuilder*>(compilation);
1725     return c->getPreferredMemoryPaddingForInput(index, padding);
1726 }
1727 
ANeuralNetworksCompilation_getPreferredMemoryAlignmentForOutput(const ANeuralNetworksCompilation * compilation,uint32_t index,uint32_t * alignment)1728 int ANeuralNetworksCompilation_getPreferredMemoryAlignmentForOutput(
1729         const ANeuralNetworksCompilation* compilation, uint32_t index, uint32_t* alignment) {
1730     NNTRACE_RT(NNTRACE_PHASE_COMPILATION,
1731                "ANeuralNetworksCompilation_getPreferredMemoryAlignmentForOutput");
1732     if (!compilation || !alignment) {
1733         LOG(ERROR) << "ANeuralNetworksCompilation_getPreferredMemoryAlignmentForOutput passed a "
1734                       "nullptr";
1735         return ANEURALNETWORKS_UNEXPECTED_NULL;
1736     }
1737     const CompilationBuilder* c = reinterpret_cast<const CompilationBuilder*>(compilation);
1738     return c->getPreferredMemoryAlignmentForOutput(index, alignment);
1739 }
1740 
ANeuralNetworksCompilation_getPreferredMemoryPaddingForOutput(const ANeuralNetworksCompilation * compilation,uint32_t index,uint32_t * padding)1741 int ANeuralNetworksCompilation_getPreferredMemoryPaddingForOutput(
1742         const ANeuralNetworksCompilation* compilation, uint32_t index, uint32_t* padding) {
1743     NNTRACE_RT(NNTRACE_PHASE_COMPILATION,
1744                "ANeuralNetworksCompilation_getPreferredMemoryPaddingForOutput");
1745     if (!compilation || !padding) {
1746         LOG(ERROR) << "ANeuralNetworksCompilation_getPreferredMemoryPaddingForOutput passed a "
1747                       "nullptr";
1748         return ANEURALNETWORKS_UNEXPECTED_NULL;
1749     }
1750     const CompilationBuilder* c = reinterpret_cast<const CompilationBuilder*>(compilation);
1751     return c->getPreferredMemoryPaddingForOutput(index, padding);
1752 }
1753 
ANeuralNetworksExecution_setReusable(ANeuralNetworksExecution * execution,bool reusable)1754 int ANeuralNetworksExecution_setReusable(ANeuralNetworksExecution* execution, bool reusable) {
1755     NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_setReusable");
1756     if (!execution) {
1757         LOG(ERROR) << "ANeuralNetworksExecution_setReusable passed a nullptr";
1758         return ANEURALNETWORKS_UNEXPECTED_NULL;
1759     }
1760     ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1761     return r->setReusable(reusable);
1762 }
1763 
1764 #ifdef NN_COMPATIBILITY_LIBRARY_BUILD
1765 
SL_ANeuralNetworksCompilation_setCachingFromFds(ANeuralNetworksCompilation * compilation,const int * modelCacheFds,const uint32_t numModelCacheFiles,const int * dataCacheFds,const uint32_t numDataCacheFiles,const uint8_t * token)1766 int SL_ANeuralNetworksCompilation_setCachingFromFds(ANeuralNetworksCompilation* compilation,
1767                                                     const int* modelCacheFds,
1768                                                     const uint32_t numModelCacheFiles,
1769                                                     const int* dataCacheFds,
1770                                                     const uint32_t numDataCacheFiles,
1771                                                     const uint8_t* token) {
1772     NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "SL_ANeuralNetworksCompilation_setCachingFromFds");
1773     if (!compilation || (numModelCacheFiles != 0 && !modelCacheFds) ||
1774         (numDataCacheFiles != 0 && !dataCacheFds) || !token) {
1775         LOG(ERROR) << "SL_ANeuralNetworksCompilation_setCachingFromFds passed a nullptr";
1776         return ANEURALNETWORKS_UNEXPECTED_NULL;
1777     }
1778     CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1779     return c->setCachingFromFds(modelCacheFds, numModelCacheFiles, dataCacheFds, numDataCacheFiles,
1780                                 token);
1781 }
1782 
SL_ANeuralNetworksDevice_getNumberOfCacheFilesNeeded(const ANeuralNetworksDevice * device,uint32_t * numModelCacheFiles,uint32_t * numDataCacheFiles)1783 int SL_ANeuralNetworksDevice_getNumberOfCacheFilesNeeded(const ANeuralNetworksDevice* device,
1784                                                          uint32_t* numModelCacheFiles,
1785                                                          uint32_t* numDataCacheFiles) {
1786     if (numModelCacheFiles) *numModelCacheFiles = 0;
1787     if (numDataCacheFiles) *numDataCacheFiles = 0;
1788 
1789     if (device == nullptr || numModelCacheFiles == nullptr || numDataCacheFiles == nullptr) {
1790         LOG(ERROR) << "SL_ANeuralNetworksDevice_getNumberOfCacheFilesNeeded passed a nullptr";
1791         return ANEURALNETWORKS_UNEXPECTED_NULL;
1792     }
1793 
1794     const Device* d = reinterpret_cast<const Device*>(device);
1795     std::tie(*numModelCacheFiles, *numDataCacheFiles) = d->getNumberOfCacheFilesNeeded();
1796     return ANEURALNETWORKS_NO_ERROR;
1797 }
1798 
SL_ANeuralNetworksDevice_getPerformanceInfo(const ANeuralNetworksDevice * device,int32_t performanceInfoKind,SL_ANeuralNetworksPerformanceInfo * performanceInfo)1799 int SL_ANeuralNetworksDevice_getPerformanceInfo(
1800         const ANeuralNetworksDevice* device, int32_t performanceInfoKind,
1801         SL_ANeuralNetworksPerformanceInfo* performanceInfo) {
1802     if (performanceInfo) *performanceInfo = {.execTime = 0.0f, .powerUsage = 0.0f};
1803 
1804     if (device == nullptr || performanceInfo == nullptr) {
1805         LOG(ERROR) << "SL_ANeuralNetworksDevice_getPerformanceInfo passed a nullptr";
1806         return ANEURALNETWORKS_UNEXPECTED_NULL;
1807     }
1808 
1809     constexpr auto conv = [](const Capabilities::PerformanceInfo& info) {
1810         return SL_ANeuralNetworksPerformanceInfo{.execTime = info.execTime,
1811                                                  .powerUsage = info.powerUsage};
1812     };
1813 
1814     const Device* d = reinterpret_cast<const Device*>(device);
1815     const Capabilities& capabilities = d->getCapabilities();
1816 
1817     switch (performanceInfoKind) {
1818         case SL_ANEURALNETWORKS_CAPABILITIES_PERFORMANCE_RELAXED_SCALAR:
1819             *performanceInfo = conv(capabilities.relaxedFloat32toFloat16PerformanceScalar);
1820             return ANEURALNETWORKS_NO_ERROR;
1821         case SL_ANEURALNETWORKS_CAPABILITIES_PERFORMANCE_RELAXED_TENSOR:
1822             *performanceInfo = conv(capabilities.relaxedFloat32toFloat16PerformanceTensor);
1823             return ANEURALNETWORKS_NO_ERROR;
1824         case SL_ANEURALNETWORKS_CAPABILITIES_PERFORMANCE_IF:
1825             *performanceInfo = conv(capabilities.ifPerformance);
1826             return ANEURALNETWORKS_NO_ERROR;
1827         case SL_ANEURALNETWORKS_CAPABILITIES_PERFORMANCE_WHILE:
1828             *performanceInfo = conv(capabilities.whilePerformance);
1829             return ANEURALNETWORKS_NO_ERROR;
1830     }
1831 
1832     LOG(ERROR) << "SL_ANeuralNetworksDevice_getPerformanceInfo passed unknown performanceInfoKind "
1833                << performanceInfoKind;
1834     return ANEURALNETWORKS_BAD_DATA;
1835 }
1836 
SL_ANeuralNetworksDevice_forEachOperandPerformanceInfo(const ANeuralNetworksDevice * device,void * context,void (* callback)(SL_ANeuralNetworksOperandPerformanceInfo,void *))1837 int SL_ANeuralNetworksDevice_forEachOperandPerformanceInfo(
1838         const ANeuralNetworksDevice* device, void* context,
1839         void (*callback)(SL_ANeuralNetworksOperandPerformanceInfo, void*)) {
1840     if (device == nullptr || context == nullptr || callback == nullptr) {
1841         LOG(ERROR) << "SL_ANeuralNetworksDevice_forEachOperandPerformanceInfo passed a nullptr";
1842         return ANEURALNETWORKS_UNEXPECTED_NULL;
1843     }
1844 
1845     constexpr auto conv = [](const Capabilities::OperandPerformance& operandPerformance) {
1846         return SL_ANeuralNetworksOperandPerformanceInfo{
1847                 .operandType = static_cast<int32_t>(operandPerformance.type),
1848                 .performanceInfo = {.execTime = operandPerformance.info.execTime,
1849                                     .powerUsage = operandPerformance.info.powerUsage},
1850         };
1851     };
1852 
1853     const Device* d = reinterpret_cast<const Device*>(device);
1854     const Capabilities& capabilities = d->getCapabilities();
1855 
1856     for (const auto& operandPerformance : capabilities.operandPerformance.asVector()) {
1857         const SL_ANeuralNetworksOperandPerformanceInfo opPerf = conv(operandPerformance);
1858         callback(opPerf, context);
1859     }
1860     return ANEURALNETWORKS_NO_ERROR;
1861 }
1862 
SL_ANeuralNetworksDevice_getVendorExtensionCount(const ANeuralNetworksDevice * device,uint32_t * vendorExtensionCount)1863 int SL_ANeuralNetworksDevice_getVendorExtensionCount(const ANeuralNetworksDevice* device,
1864                                                      uint32_t* vendorExtensionCount) {
1865     if (vendorExtensionCount) *vendorExtensionCount = 0;
1866 
1867     if (device == nullptr || vendorExtensionCount == nullptr) {
1868         LOG(ERROR) << "SL_ANeuralNetworksDevice_getVendorExtensionCount passed a nullptr";
1869         return ANEURALNETWORKS_UNEXPECTED_NULL;
1870     }
1871 
1872     const Device* d = reinterpret_cast<const Device*>(device);
1873     *vendorExtensionCount = d->getSupportedExtensions().size();
1874     return ANEURALNETWORKS_NO_ERROR;
1875 }
1876 
SL_ANeuralNetworksDevice_getVendorExtensionName(const ANeuralNetworksDevice * device,uint32_t vendorExtensionIndex,const char ** extensionName)1877 int SL_ANeuralNetworksDevice_getVendorExtensionName(const ANeuralNetworksDevice* device,
1878                                                     uint32_t vendorExtensionIndex,
1879                                                     const char** extensionName) {
1880     if (extensionName) *extensionName = nullptr;
1881 
1882     if (device == nullptr || extensionName == nullptr) {
1883         LOG(ERROR) << "SL_ANeuralNetworksDevice_getVendorExtensionName passed a nullptr";
1884         return ANEURALNETWORKS_UNEXPECTED_NULL;
1885     }
1886 
1887     const Device* d = reinterpret_cast<const Device*>(device);
1888     const auto& extensions = d->getSupportedExtensions();
1889 
1890     if (vendorExtensionIndex >= extensions.size()) {
1891         LOG(ERROR)
1892                 << "SL_ANeuralNetworksDevice_getVendorExtensionName passed a vendorExtensionIndex "
1893                    "that is out of range";
1894         return ANEURALNETWORKS_BAD_DATA;
1895     }
1896     const auto& extension = extensions[vendorExtensionIndex];
1897 
1898     *extensionName = extension.name.c_str();
1899     return ANEURALNETWORKS_NO_ERROR;
1900 }
1901 
SL_ANeuralNetworksDevice_forEachVendorExtensionOperandTypeInformation(const ANeuralNetworksDevice * device,uint32_t vendorExtensionIndex,void * context,void (* callback)(SL_ANeuralNetworksExtensionOperandTypeInformation,void *))1902 int SL_ANeuralNetworksDevice_forEachVendorExtensionOperandTypeInformation(
1903         const ANeuralNetworksDevice* device, uint32_t vendorExtensionIndex, void* context,
1904         void (*callback)(SL_ANeuralNetworksExtensionOperandTypeInformation, void*)) {
1905     if (device == nullptr || context == nullptr || callback == nullptr) {
1906         LOG(ERROR)
1907                 << "SL_ANeuralNetworksDevice_forEachVendorExtensionOperandTypeInformation passed a "
1908                    "nullptr";
1909         return ANEURALNETWORKS_UNEXPECTED_NULL;
1910     }
1911 
1912     const Device* d = reinterpret_cast<const Device*>(device);
1913     const auto& extensions = d->getSupportedExtensions();
1914 
1915     if (vendorExtensionIndex >= extensions.size()) {
1916         LOG(ERROR)
1917                 << "SL_ANeuralNetworksDevice_forEachVendorExtensionOperandTypeInformation passed a "
1918                    "vendorExtensionIndex that is out of range";
1919         return ANEURALNETWORKS_BAD_DATA;
1920     }
1921     const auto& operandTypes = extensions[vendorExtensionIndex].operandTypes;
1922 
1923     constexpr auto conv = [](const Extension::OperandTypeInformation& operandTypeInfo) {
1924         return SL_ANeuralNetworksExtensionOperandTypeInformation{
1925                 .byteSize = operandTypeInfo.byteSize,
1926                 .type = operandTypeInfo.type,
1927                 .isTensor = operandTypeInfo.isTensor,
1928         };
1929     };
1930 
1931     for (const auto& operandTypeInfo : operandTypes) {
1932         const SL_ANeuralNetworksExtensionOperandTypeInformation opTypeInfo = conv(operandTypeInfo);
1933         callback(opTypeInfo, context);
1934     }
1935     return ANEURALNETWORKS_NO_ERROR;
1936 }
1937 
1938 #define NNCL_FUNC(symbol) .symbol = symbol
1939 
1940 NnApiSLDriverImplFL8 slDriverImpl{
1941         .base{
1942                 .base{.implFeatureLevel = ANEURALNETWORKS_FEATURE_LEVEL_8},
1943                 NNCL_FUNC(ANeuralNetworksBurst_create),
1944                 NNCL_FUNC(ANeuralNetworksBurst_free),
1945                 NNCL_FUNC(ANeuralNetworksCompilation_createForDevices),
1946                 NNCL_FUNC(ANeuralNetworksCompilation_finish),
1947                 NNCL_FUNC(ANeuralNetworksCompilation_free),
1948                 NNCL_FUNC(ANeuralNetworksCompilation_getPreferredMemoryAlignmentForInput),
1949                 NNCL_FUNC(ANeuralNetworksCompilation_getPreferredMemoryAlignmentForOutput),
1950                 NNCL_FUNC(ANeuralNetworksCompilation_getPreferredMemoryPaddingForInput),
1951                 NNCL_FUNC(ANeuralNetworksCompilation_getPreferredMemoryPaddingForOutput),
1952                 NNCL_FUNC(ANeuralNetworksCompilation_setCaching),
1953                 NNCL_FUNC(ANeuralNetworksCompilation_setPreference),
1954                 NNCL_FUNC(ANeuralNetworksCompilation_setPriority),
1955                 NNCL_FUNC(ANeuralNetworksCompilation_setTimeout),
1956                 NNCL_FUNC(ANeuralNetworksDevice_getExtensionSupport),
1957                 NNCL_FUNC(ANeuralNetworksDevice_getFeatureLevel),
1958                 NNCL_FUNC(ANeuralNetworksDevice_getName),
1959                 NNCL_FUNC(ANeuralNetworksDevice_getType),
1960                 NNCL_FUNC(ANeuralNetworksDevice_getVersion),
1961                 NNCL_FUNC(ANeuralNetworksDevice_wait),
1962                 NNCL_FUNC(ANeuralNetworksEvent_createFromSyncFenceFd),
1963                 NNCL_FUNC(ANeuralNetworksEvent_free),
1964                 NNCL_FUNC(ANeuralNetworksEvent_getSyncFenceFd),
1965                 NNCL_FUNC(ANeuralNetworksEvent_wait),
1966                 NNCL_FUNC(ANeuralNetworksExecution_burstCompute),
1967                 NNCL_FUNC(ANeuralNetworksExecution_compute),
1968                 NNCL_FUNC(ANeuralNetworksExecution_create),
1969                 NNCL_FUNC(ANeuralNetworksExecution_enableInputAndOutputPadding),
1970                 NNCL_FUNC(ANeuralNetworksExecution_free),
1971                 NNCL_FUNC(ANeuralNetworksExecution_getDuration),
1972                 NNCL_FUNC(ANeuralNetworksExecution_getOutputOperandDimensions),
1973                 NNCL_FUNC(ANeuralNetworksExecution_getOutputOperandRank),
1974                 NNCL_FUNC(ANeuralNetworksExecution_setInput),
1975                 NNCL_FUNC(ANeuralNetworksExecution_setInputFromMemory),
1976                 NNCL_FUNC(ANeuralNetworksExecution_setLoopTimeout),
1977                 NNCL_FUNC(ANeuralNetworksExecution_setMeasureTiming),
1978                 NNCL_FUNC(ANeuralNetworksExecution_setOutput),
1979                 NNCL_FUNC(ANeuralNetworksExecution_setOutputFromMemory),
1980                 NNCL_FUNC(ANeuralNetworksExecution_setReusable),
1981                 NNCL_FUNC(ANeuralNetworksExecution_setTimeout),
1982                 NNCL_FUNC(ANeuralNetworksExecution_startComputeWithDependencies),
1983                 NNCL_FUNC(ANeuralNetworksMemoryDesc_addInputRole),
1984                 NNCL_FUNC(ANeuralNetworksMemoryDesc_addOutputRole),
1985                 NNCL_FUNC(ANeuralNetworksMemoryDesc_create),
1986                 NNCL_FUNC(ANeuralNetworksMemoryDesc_finish),
1987                 NNCL_FUNC(ANeuralNetworksMemoryDesc_free),
1988                 NNCL_FUNC(ANeuralNetworksMemoryDesc_setDimensions),
1989                 NNCL_FUNC(ANeuralNetworksMemory_copy),
1990                 NNCL_FUNC(ANeuralNetworksMemory_createFromAHardwareBuffer),
1991                 NNCL_FUNC(ANeuralNetworksMemory_createFromDesc),
1992                 NNCL_FUNC(ANeuralNetworksMemory_createFromFd),
1993                 NNCL_FUNC(ANeuralNetworksMemory_free),
1994                 NNCL_FUNC(ANeuralNetworksModel_addOperand),
1995                 NNCL_FUNC(ANeuralNetworksModel_addOperation),
1996                 NNCL_FUNC(ANeuralNetworksModel_create),
1997                 NNCL_FUNC(ANeuralNetworksModel_finish),
1998                 NNCL_FUNC(ANeuralNetworksModel_free),
1999                 NNCL_FUNC(ANeuralNetworksModel_getExtensionOperandType),
2000                 NNCL_FUNC(ANeuralNetworksModel_getExtensionOperationType),
2001                 NNCL_FUNC(ANeuralNetworksModel_getSupportedOperationsForDevices),
2002                 NNCL_FUNC(ANeuralNetworksModel_identifyInputsAndOutputs),
2003                 NNCL_FUNC(ANeuralNetworksModel_relaxComputationFloat32toFloat16),
2004                 NNCL_FUNC(ANeuralNetworksModel_setOperandExtensionData),
2005                 NNCL_FUNC(ANeuralNetworksModel_setOperandSymmPerChannelQuantParams),
2006                 NNCL_FUNC(ANeuralNetworksModel_setOperandValue),
2007                 NNCL_FUNC(ANeuralNetworksModel_setOperandValueFromMemory),
2008                 NNCL_FUNC(ANeuralNetworksModel_setOperandValueFromModel),
2009                 NNCL_FUNC(ANeuralNetworks_getDefaultLoopTimeout),
2010                 NNCL_FUNC(ANeuralNetworks_getDevice),
2011                 NNCL_FUNC(ANeuralNetworks_getDeviceCount),
2012                 NNCL_FUNC(ANeuralNetworks_getMaximumLoopTimeout),
2013                 NNCL_FUNC(ANeuralNetworks_getRuntimeFeatureLevel),
2014                 NNCL_FUNC(SL_ANeuralNetworksCompilation_setCachingFromFds),
2015                 NNCL_FUNC(SL_ANeuralNetworksDevice_getNumberOfCacheFilesNeeded),
2016                 NNCL_FUNC(SL_ANeuralNetworksDevice_getPerformanceInfo),
2017                 NNCL_FUNC(SL_ANeuralNetworksDevice_forEachOperandPerformanceInfo),
2018                 NNCL_FUNC(SL_ANeuralNetworksDevice_getVendorExtensionCount),
2019                 NNCL_FUNC(SL_ANeuralNetworksDevice_getVendorExtensionName),
2020                 NNCL_FUNC(SL_ANeuralNetworksDevice_forEachVendorExtensionOperandTypeInformation),
2021                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticCompilationInfo_getSessionId),
2022                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticCompilationInfo_getNnApiVersion),
2023                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticCompilationInfo_getModelArchHash),
2024                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticCompilationInfo_getDeviceIds),
2025                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticCompilationInfo_getErrorCode),
2026                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticCompilationInfo_getInputDataClass),
2027                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticCompilationInfo_getOutputDataClass),
2028                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticCompilationInfo_getCompilationTimeNanos),
2029                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticCompilationInfo_isCachingEnabled),
2030                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticCompilationInfo_isControlFlowUsed),
2031                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticCompilationInfo_areDynamicTensorsUsed),
2032                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticExecutionInfo_getSessionId),
2033                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticExecutionInfo_getNnApiVersion),
2034                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticExecutionInfo_getModelArchHash),
2035                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticExecutionInfo_getDeviceIds),
2036                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticExecutionInfo_getExecutionMode),
2037                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticExecutionInfo_getInputDataClass),
2038                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticExecutionInfo_getOutputDataClass),
2039                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticExecutionInfo_getErrorCode),
2040                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticExecutionInfo_getRuntimeExecutionTimeNanos),
2041                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticExecutionInfo_getDriverExecutionTimeNanos),
2042                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticExecutionInfo_getHardwareExecutionTimeNanos),
2043                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticExecutionInfo_isCachingEnabled),
2044                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticExecutionInfo_isControlFlowUsed),
2045                 NNCL_FUNC(SL_ANeuralNetworksDiagnosticExecutionInfo_areDynamicTensorsUsed),
2046                 NNCL_FUNC(SL_ANeuralNetworksDiagnostic_registerCallbacks),
2047         },
2048         NNCL_FUNC(ANeuralNetworksCompilation_addExtensionAttribute),
2049         NNCL_FUNC(ANeuralNetworksExecution_addExtensionAttribute),
2050 };
2051 
2052 #undef NNCL_FUNC
2053 
2054 __BEGIN_DECLS
ANeuralNetworks_getSLDriverImpl()2055 NnApiSLDriverImpl* ANeuralNetworks_getSLDriverImpl() {
2056     return reinterpret_cast<NnApiSLDriverImpl*>(&slDriverImpl);
2057 }
2058 __END_DECLS
2059 
2060 #endif  // NN_COMPATIBILITY_LIBRARY_BUILD
2061