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 <vndk/hardware_buffer.h>
26
27 #include <algorithm>
28 #include <cstddef>
29 #include <memory>
30 #include <vector>
31
32 #include "BurstBuilder.h"
33 #include "Callbacks.h"
34 #include "CompilationBuilder.h"
35 #include "ControlFlow.h"
36 #include "Event.h"
37 #include "ExecutionBuilder.h"
38 #include "HalInterfaces.h"
39 #include "Manager.h"
40 #include "Memory.h"
41 #include "MetaModel.h"
42 #include "ModelBuilder.h"
43 #include "NeuralNetworksExtensions.h"
44 #include "NeuralNetworksOEM.h"
45 #include "Tracing.h"
46 #include "Utils.h"
47
48 using namespace android::nn;
49 using namespace android::nn::hal;
50
51 // Make sure the constants defined in the header files have not changed values.
52 // IMPORTANT: When adding new values, update kNumberOfDataTypes or kNumberOfDataTypesOEM
53 // in Utils.h.
54 static_assert(ANEURALNETWORKS_FLOAT32 == 0, "ANEURALNETWORKS_FLOAT32 has changed");
55 static_assert(ANEURALNETWORKS_INT32 == 1, "ANEURALNETWORKS_INT32 has changed");
56 static_assert(ANEURALNETWORKS_UINT32 == 2, "ANEURALNETWORKS_UINT32 has changed");
57 static_assert(ANEURALNETWORKS_TENSOR_FLOAT32 == 3, "ANEURALNETWORKS_TENSOR_FLOAT32 has changed");
58 static_assert(ANEURALNETWORKS_TENSOR_INT32 == 4, "ANEURALNETWORKS_TENSOR_INT32 has changed");
59 static_assert(ANEURALNETWORKS_TENSOR_QUANT8_ASYMM == 5,
60 "ANEURALNETWORKS_TENSOR_QUANT8_ASYMM has changed");
61 static_assert(ANEURALNETWORKS_BOOL == 6, "ANEURALNETWORKS_BOOL has changed");
62 static_assert(ANEURALNETWORKS_TENSOR_QUANT16_SYMM == 7,
63 "ANEURALNETWORKS_TENSOR_QUANT16_SYMM has changed");
64 static_assert(ANEURALNETWORKS_TENSOR_FLOAT16 == 8, "ANEURALNETWORKS_TENSOR_FLOAT16 has changed");
65 static_assert(ANEURALNETWORKS_TENSOR_BOOL8 == 9, "ANEURALNETWORKS_TENSOR_BOOL8 has changed");
66 static_assert(ANEURALNETWORKS_FLOAT16 == 10, "ANEURALNETWORKS_FLOAT16 has changed");
67 static_assert(ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL == 11,
68 "ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL has changed");
69 static_assert(ANEURALNETWORKS_TENSOR_QUANT16_ASYMM == 12,
70 "ANEURALNETWORKS_TENSOR_QUANT16_ASYMM has changed");
71 static_assert(ANEURALNETWORKS_TENSOR_QUANT8_SYMM == 13,
72 "ANEURALNETWORKS_TENSOR_QUANT8_SYMM has changed");
73 static_assert(ANEURALNETWORKS_OEM_SCALAR == 10000, "ANEURALNETWORKS_OEM_SCALAR has changed");
74 static_assert(ANEURALNETWORKS_TENSOR_OEM_BYTE == 10001,
75 "ANEURALNETWORKS_TENSOR_OEM_BYTE has changed");
76
77 // IMPORTANT: When adding new values, update kNumberOfOperationTypes or
78 // kNumberOfOperationTypesOEMin Utils.h.
79 static_assert(ANEURALNETWORKS_ADD == 0, "ANEURALNETWORKS_ADD has changed");
80 static_assert(ANEURALNETWORKS_AVERAGE_POOL_2D == 1, "ANEURALNETWORKS_AVERAGE_POOL_2D has changed");
81 static_assert(ANEURALNETWORKS_CONCATENATION == 2, "ANEURALNETWORKS_CONCATENATION has changed");
82 static_assert(ANEURALNETWORKS_CONV_2D == 3, "ANEURALNETWORKS_CONV_2D has changed");
83 static_assert(ANEURALNETWORKS_DEPTHWISE_CONV_2D == 4,
84 "ANEURALNETWORKS_DEPTHWISE_CONV_2D has changed");
85 static_assert(ANEURALNETWORKS_DEPTH_TO_SPACE == 5, "ANEURALNETWORKS_DEPTH_TO_SPACE has changed");
86 static_assert(ANEURALNETWORKS_DEQUANTIZE == 6, "ANEURALNETWORKS_DEQUANTIZE has changed");
87 static_assert(ANEURALNETWORKS_EMBEDDING_LOOKUP == 7,
88 "ANEURALNETWORKS_EMBEDDING_LOOKUP has changed");
89 static_assert(ANEURALNETWORKS_FLOOR == 8, "ANEURALNETWORKS_FLOOR has changed");
90 static_assert(ANEURALNETWORKS_FULLY_CONNECTED == 9, "ANEURALNETWORKS_FULLY_CONNECTED has changed");
91 static_assert(ANEURALNETWORKS_HASHTABLE_LOOKUP == 10,
92 "ANEURALNETWORKS_HASHTABLE_LOOKUP has changed");
93 static_assert(ANEURALNETWORKS_L2_NORMALIZATION == 11,
94 "ANEURALNETWORKS_L2_NORMALIZATION has changed");
95 static_assert(ANEURALNETWORKS_L2_POOL_2D == 12, "ANEURALNETWORKS_L2_POOL has changed");
96 static_assert(ANEURALNETWORKS_LOCAL_RESPONSE_NORMALIZATION == 13,
97 "ANEURALNETWORKS_LOCAL_RESPONSE_NORMALIZATION has changed");
98 static_assert(ANEURALNETWORKS_LOGISTIC == 14, "ANEURALNETWORKS_LOGISTIC has changed");
99 static_assert(ANEURALNETWORKS_LSH_PROJECTION == 15, "ANEURALNETWORKS_LSH_PROJECTION has changed");
100 static_assert(ANEURALNETWORKS_LSTM == 16, "ANEURALNETWORKS_LSTM has changed");
101 static_assert(ANEURALNETWORKS_MAX_POOL_2D == 17, "ANEURALNETWORKS_MAX_POOL has changed");
102 static_assert(ANEURALNETWORKS_MUL == 18, "ANEURALNETWORKS_MUL has changed");
103 static_assert(ANEURALNETWORKS_RELU == 19, "ANEURALNETWORKS_RELU has changed");
104 static_assert(ANEURALNETWORKS_RELU1 == 20, "ANEURALNETWORKS_RELU1 has changed");
105 static_assert(ANEURALNETWORKS_RELU6 == 21, "ANEURALNETWORKS_RELU6 has changed");
106 static_assert(ANEURALNETWORKS_RESHAPE == 22, "ANEURALNETWORKS_RESHAPE has changed");
107 static_assert(ANEURALNETWORKS_RESIZE_BILINEAR == 23, "ANEURALNETWORKS_RESIZE_BILINEAR has changed");
108 static_assert(ANEURALNETWORKS_RNN == 24, "ANEURALNETWORKS_RNN has changed");
109 static_assert(ANEURALNETWORKS_SOFTMAX == 25, "ANEURALNETWORKS_SOFTMAX has changed");
110 static_assert(ANEURALNETWORKS_SPACE_TO_DEPTH == 26, "ANEURALNETWORKS_SPACE_TO_DEPTH has changed");
111 static_assert(ANEURALNETWORKS_SVDF == 27, "ANEURALNETWORKS_SVDF has changed");
112 static_assert(ANEURALNETWORKS_TANH == 28, "ANEURALNETWORKS_TANH has changed");
113
114 static_assert(ANEURALNETWORKS_BATCH_TO_SPACE_ND == 29,
115 "ANEURALNETWORKS_BATCH_TO_SPACE_ND has changed");
116 static_assert(ANEURALNETWORKS_DIV == 30, "ANEURALNETWORKS_DIV has changed");
117 static_assert(ANEURALNETWORKS_MEAN == 31, "ANEURALNETWORKS_MEAN has changed");
118 static_assert(ANEURALNETWORKS_PAD == 32, "ANEURALNETWORKS_PAD has changed");
119 static_assert(ANEURALNETWORKS_SPACE_TO_BATCH_ND == 33,
120 "ANEURALNETWORKS_SPACE_TO_BATCH_ND has changed");
121 static_assert(ANEURALNETWORKS_SQUEEZE == 34, "ANEURALNETWORKS_SQUEEZE has changed");
122 static_assert(ANEURALNETWORKS_STRIDED_SLICE == 35, "ANEURALNETWORKS_STRIDED_SLICE has changed");
123 static_assert(ANEURALNETWORKS_SUB == 36, "ANEURALNETWORKS_TANH has changed");
124 static_assert(ANEURALNETWORKS_TRANSPOSE == 37, "ANEURALNETWORKS_TRANSPOSE has changed");
125
126 static_assert(ANEURALNETWORKS_ABS == 38, "ANEURALNETWORKS_ABS has changed");
127 static_assert(ANEURALNETWORKS_ARGMAX == 39, "ANEURALNETWORKS_ARGMAX has changed");
128 static_assert(ANEURALNETWORKS_ARGMIN == 40, "ANEURALNETWORKS_ARGMIN has changed");
129 static_assert(ANEURALNETWORKS_AXIS_ALIGNED_BBOX_TRANSFORM == 41,
130 "ANEURALNETWORKS_AXIS_ALIGNED_BBOX_TRANSFORM has changed");
131 static_assert(ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_LSTM == 42,
132 "ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_LSTM has changed");
133 static_assert(ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_RNN == 43,
134 "ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_RNN has changed");
135 static_assert(ANEURALNETWORKS_BOX_WITH_NMS_LIMIT == 44,
136 "ANEURALNETWORKS_BOX_WITH_NMS_LIMIT has changed");
137 static_assert(ANEURALNETWORKS_CAST == 45, "ANEURALNETWORKS_CAST has changed");
138 static_assert(ANEURALNETWORKS_CHANNEL_SHUFFLE == 46, "ANEURALNETWORKS_CHANNEL_SHUFFLE has changed");
139 static_assert(ANEURALNETWORKS_DETECTION_POSTPROCESSING == 47,
140 "ANEURALNETWORKS_DETECTION_POSTPROCESSING has changed");
141 static_assert(ANEURALNETWORKS_EQUAL == 48, "ANEURALNETWORKS_EQUAL has changed");
142 static_assert(ANEURALNETWORKS_EXP == 49, "ANEURALNETWORKS_EXP has changed");
143 static_assert(ANEURALNETWORKS_EXPAND_DIMS == 50, "ANEURALNETWORKS_EXPAND_DIMS has changed");
144 static_assert(ANEURALNETWORKS_GATHER == 51, "ANEURALNETWORKS_GATHER has changed");
145 static_assert(ANEURALNETWORKS_GENERATE_PROPOSALS == 52,
146 "ANEURALNETWORKS_GENERATE_PROPOSALS has changed");
147 static_assert(ANEURALNETWORKS_GREATER == 53, "ANEURALNETWORKS_GREATER has changed");
148 static_assert(ANEURALNETWORKS_GREATER_EQUAL == 54, "ANEURALNETWORKS_GREATER_EQUAL has changed");
149 static_assert(ANEURALNETWORKS_GROUPED_CONV_2D == 55, "ANEURALNETWORKS_GROUPED_CONV_2D has changed");
150 static_assert(ANEURALNETWORKS_HEATMAP_MAX_KEYPOINT == 56,
151 "ANEURALNETWORKS_HEATMAP_MAX_KEYPOINT has changed");
152 static_assert(ANEURALNETWORKS_INSTANCE_NORMALIZATION == 57,
153 "ANEURALNETWORKS_INSTANCE_NORMALIZATION has changed");
154 static_assert(ANEURALNETWORKS_LESS == 58, "ANEURALNETWORKS_LESS has changed");
155 static_assert(ANEURALNETWORKS_LESS_EQUAL == 59, "ANEURALNETWORKS_LESS_EQUAL has changed");
156 static_assert(ANEURALNETWORKS_LOG == 60, "ANEURALNETWORKS_LOG has changed");
157 static_assert(ANEURALNETWORKS_LOGICAL_AND == 61, "ANEURALNETWORKS_LOGICAL_AND has changed");
158 static_assert(ANEURALNETWORKS_LOGICAL_NOT == 62, "ANEURALNETWORKS_LOGICAL_NOT has changed");
159 static_assert(ANEURALNETWORKS_LOGICAL_OR == 63, "ANEURALNETWORKS_LOGICAL_OR has changed");
160 static_assert(ANEURALNETWORKS_LOG_SOFTMAX == 64, "ANEURALNETWORKS_LOG_SOFTMAX has changed");
161 static_assert(ANEURALNETWORKS_MAXIMUM == 65, "ANEURALNETWORKS_MAXIMUM has changed");
162 static_assert(ANEURALNETWORKS_MINIMUM == 66, "ANEURALNETWORKS_MINIMUM has changed");
163 static_assert(ANEURALNETWORKS_NEG == 67, "ANEURALNETWORKS_NEG has changed");
164 static_assert(ANEURALNETWORKS_NOT_EQUAL == 68, "ANEURALNETWORKS_NOT_EQUAL has changed");
165 static_assert(ANEURALNETWORKS_PAD_V2 == 69, "ANEURALNETWORKS_PAD_V2 has changed");
166 static_assert(ANEURALNETWORKS_POW == 70, "ANEURALNETWORKS_POW has changed");
167 static_assert(ANEURALNETWORKS_PRELU == 71, "ANEURALNETWORKS_PRELU has changed");
168 static_assert(ANEURALNETWORKS_QUANTIZE == 72, "ANEURALNETWORKS_QUANTIZE has changed");
169 static_assert(ANEURALNETWORKS_QUANTIZED_16BIT_LSTM == 73,
170 "ANEURALNETWORKS_QUANTIZED_16BIT_LSTM has changed");
171 static_assert(ANEURALNETWORKS_RANDOM_MULTINOMIAL == 74,
172 "ANEURALNETWORKS_RANDOM_MULTINOMIAL has changed");
173 static_assert(ANEURALNETWORKS_REDUCE_ALL == 75, "ANEURALNETWORKS_REDUCE_ALL has changed");
174 static_assert(ANEURALNETWORKS_REDUCE_ANY == 76, "ANEURALNETWORKS_REDUCE_ANY has changed");
175 static_assert(ANEURALNETWORKS_REDUCE_MAX == 77, "ANEURALNETWORKS_REDUCE_MAX has changed");
176 static_assert(ANEURALNETWORKS_REDUCE_MIN == 78, "ANEURALNETWORKS_REDUCE_MIN has changed");
177 static_assert(ANEURALNETWORKS_REDUCE_PROD == 79, "ANEURALNETWORKS_REDUCE_PROD has changed");
178 static_assert(ANEURALNETWORKS_REDUCE_SUM == 80, "ANEURALNETWORKS_REDUCE_SUM has changed");
179 static_assert(ANEURALNETWORKS_ROI_ALIGN == 81, "ANEURALNETWORKS_ROI_ALIGN has changed");
180 static_assert(ANEURALNETWORKS_ROI_POOLING == 82, "ANEURALNETWORKS_ROI_POOLING has changed");
181 static_assert(ANEURALNETWORKS_RSQRT == 83, "ANEURALNETWORKS_RSQRT has changed");
182 static_assert(ANEURALNETWORKS_SELECT == 84, "ANEURALNETWORKS_SELECT has changed");
183 static_assert(ANEURALNETWORKS_SIN == 85, "ANEURALNETWORKS_SIN has changed");
184 static_assert(ANEURALNETWORKS_SLICE == 86, "ANEURALNETWORKS_SLICE has changed");
185 static_assert(ANEURALNETWORKS_SPLIT == 87, "ANEURALNETWORKS_SPLIT has changed");
186 static_assert(ANEURALNETWORKS_SQRT == 88, "ANEURALNETWORKS_SQRT has changed");
187 static_assert(ANEURALNETWORKS_TILE == 89, "ANEURALNETWORKS_TILE has changed");
188 static_assert(ANEURALNETWORKS_TOPK_V2 == 90, "ANEURALNETWORKS_TOPK_V2 has changed");
189 static_assert(ANEURALNETWORKS_TRANSPOSE_CONV_2D == 91,
190 "ANEURALNETWORKS_TRANSPOSE_CONV_2D has changed");
191 static_assert(ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_LSTM == 92,
192 "ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_LSTM has changed");
193 static_assert(ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_RNN == 93,
194 "ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_RNN has changed");
195 static_assert(ANEURALNETWORKS_RESIZE_NEAREST_NEIGHBOR == 94,
196 "ANEURALNETWORKS_RESIZE_NEAREST_NEIGHBOR has changed");
197 static_assert(ANEURALNETWORKS_QUANTIZED_LSTM == 95, "ANEURALNETWORKS_QUANTIZED_LSTM has changed");
198 static_assert(ANEURALNETWORKS_IF == 96, "ANEURALNETWORKS_IF has changed");
199 static_assert(ANEURALNETWORKS_WHILE == 97, "ANEURALNETWORKS_WHILE has changed");
200 static_assert(ANEURALNETWORKS_ELU == 98, "ANEURALNETWORKS_ELU has changed");
201 static_assert(ANEURALNETWORKS_HARD_SWISH == 99, "ANEURALNETWORKS_HARD_SWISH has changed");
202 static_assert(ANEURALNETWORKS_FILL == 100, "ANEURALNETWORKS_FILL has changed");
203 static_assert(ANEURALNETWORKS_RANK == 101, "ANEURALNETWORKS_RANK has changed");
204
205 static_assert(ANEURALNETWORKS_OEM_OPERATION == 10000, "ANEURALNETWORKS_OEM_OPERATION has changed");
206
207 static_assert(ANEURALNETWORKS_FUSED_NONE == 0, "ANEURALNETWORKS_FUSED_NONE has changed");
208 static_assert(ANEURALNETWORKS_FUSED_RELU == 1, "ANEURALNETWORKS_FUSED_RELU has changed");
209 static_assert(ANEURALNETWORKS_FUSED_RELU1 == 2, "ANEURALNETWORKS_FUSED_RELU1 has changed");
210 static_assert(ANEURALNETWORKS_FUSED_RELU6 == 3, "ANEURALNETWORKS_FUSED_RELU6 has changed");
211
212 static_assert(ANEURALNETWORKS_PREFER_LOW_POWER == 0,
213 "ANEURALNETWORKS_PREFER_LOW_POWER has changed");
214 static_assert(ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER == 1,
215 "ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER has changed");
216 static_assert(ANEURALNETWORKS_PREFER_SUSTAINED_SPEED == 2,
217 "ANEURALNETWORKS_PREFER_SUSTAINED_SPEED has changed");
218
219 static_assert(ANEURALNETWORKS_NO_ERROR == 0, "ANEURALNETWORKS_NO_ERROR has changed");
220 static_assert(ANEURALNETWORKS_OUT_OF_MEMORY == 1, "ANEURALNETWORKS_OUT_OF_MEMORY has changed");
221 static_assert(ANEURALNETWORKS_INCOMPLETE == 2, "ANEURALNETWORKS_INCOMPLETE has changed");
222 static_assert(ANEURALNETWORKS_UNEXPECTED_NULL == 3, "ANEURALNETWORKS_UNEXPECTED_NULL has changed");
223 static_assert(ANEURALNETWORKS_BAD_DATA == 4, "ANEURALNETWORKS_BAD_DATA has changed");
224 static_assert(ANEURALNETWORKS_OP_FAILED == 5, "ANEURALNETWORKS_OP_FAILED has changed");
225 static_assert(ANEURALNETWORKS_BAD_STATE == 6, "ANEURALNETWORKS_BAD_STATE has changed");
226 static_assert(ANEURALNETWORKS_UNMAPPABLE == 7, "ANEURALNETWORKS_UNMAPPABLE has changed");
227 static_assert(ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE == 8,
228 "ANEURALNETWORKS_OUTPUT_INSUFFICIENT_SIZE has changed");
229 static_assert(ANEURALNETWORKS_UNAVAILABLE_DEVICE == 9,
230 "ANEURALNETWORKS_UNAVAILABLE_DEVICE has changed");
231 static_assert(ANEURALNETWORKS_MISSED_DEADLINE_TRANSIENT == 10,
232 "ANEURALNETWORKS_MISSED_DEADLINE_TRANSIENT has changed");
233 static_assert(ANEURALNETWORKS_MISSED_DEADLINE_PERSISTENT == 11,
234 "ANEURALNETWORKS_MISSED_DEADLINE_PERSISTENT has changed");
235 static_assert(ANEURALNETWORKS_RESOURCE_EXHAUSTED_TRANSIENT == 12,
236 "ANEURALNETWORKS_RESOURCE_EXHAUSTED_TRANSIENT has changed");
237 static_assert(ANEURALNETWORKS_RESOURCE_EXHAUSTED_PERSISTENT == 13,
238 "ANEURALNETWORKS_RESOURCE_EXHAUSTED_PERSISTENT has changed");
239 static_assert(ANEURALNETWORKS_DEAD_OBJECT == 14, "ANEURALNETWORKS_DEAD_OBJECT has changed");
240
241 static_assert(ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES == 128,
242 "ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES has changed");
243
244 static_assert(ANEURALNETWORKS_DEVICE_UNKNOWN == 0, "ANEURALNETWORKS_DEVICE_UNKNOWN has changed");
245 static_assert(ANEURALNETWORKS_DEVICE_OTHER == 1, "ANEURALNETWORKS_DEVICE_OTHER has changed");
246 static_assert(ANEURALNETWORKS_DEVICE_CPU == 2, "ANEURALNETWORKS_DEVICE_CPU has changed");
247 static_assert(ANEURALNETWORKS_DEVICE_GPU == 3, "ANEURALNETWORKS_DEVICE_GPU has changed");
248 static_assert(ANEURALNETWORKS_DEVICE_ACCELERATOR == 4,
249 "ANEURALNETWORKS_DEVICE_ACCELERATOR has changed");
250
251 static_assert(ANEURALNETWORKS_DURATION_ON_HARDWARE == 0,
252 "ANEURALNETWORKS_DURATION_ON_HARDWARE has changed");
253 static_assert(ANEURALNETWORKS_DURATION_IN_DRIVER == 1,
254 "ANEURALNETWORKS_DURATION_IN_DRIVER has changed");
255 static_assert(ANEURALNETWORKS_FENCED_DURATION_ON_HARDWARE == 2,
256 "ANEURALNETWORKS_FENCED_DURATION_ON_HARDWARE has changed");
257 static_assert(ANEURALNETWORKS_FENCED_DURATION_IN_DRIVER == 3,
258 "ANEURALNETWORKS_FENCED_DURATION_IN_DRIVER has changed");
259
260 // Make sure that the constants are compatible with the values defined in
261 // hardware/interfaces/neuralnetworks/1.0/types.hal.
262 static_assert(static_cast<int32_t>(OperandType::OEM) == ANEURALNETWORKS_OEM_SCALAR,
263 "OEM != ANEURALNETWORKS_OEM");
264 static_assert(static_cast<int32_t>(OperandType::FLOAT32) == ANEURALNETWORKS_FLOAT32,
265 "FLOAT32 != ANEURALNETWORKS_FLOAT32");
266 static_assert(static_cast<int32_t>(OperandType::INT32) == ANEURALNETWORKS_INT32,
267 "INT32 != ANEURALNETWORKS_INT32");
268 static_assert(static_cast<int32_t>(OperandType::UINT32) == ANEURALNETWORKS_UINT32,
269 "UINT32 != ANEURALNETWORKS_UINT32");
270 static_assert(static_cast<int32_t>(OperandType::TENSOR_OEM_BYTE) == ANEURALNETWORKS_TENSOR_OEM_BYTE,
271 "TENSOR_OEM_BYTE != ANEURALNETWORKS_TENSOR_OEM_BYTE");
272 static_assert(static_cast<int32_t>(OperandType::TENSOR_FLOAT32) == ANEURALNETWORKS_TENSOR_FLOAT32,
273 "TENSOR_FLOAT32 != ANEURALNETWORKS_TENSOR_FLOAT32");
274 static_assert(static_cast<int32_t>(OperandType::TENSOR_QUANT8_ASYMM) ==
275 ANEURALNETWORKS_TENSOR_QUANT8_ASYMM,
276 "TENSOR_QUANT8_ASYMM != ANEURALNETWORKS_TENSOR_QUANT8_ASYMM");
277
278 static_assert(static_cast<int32_t>(OperationType::ADD) == ANEURALNETWORKS_ADD,
279 "OperationType::ADD != ANEURALNETWORKS_ADD");
280 static_assert(static_cast<int32_t>(OperationType::AVERAGE_POOL_2D) ==
281 ANEURALNETWORKS_AVERAGE_POOL_2D,
282 "OperationType::AVERAGE_POOL_2D != ANEURALNETWORKS_AVERAGE_POOL_2D");
283 static_assert(static_cast<int32_t>(OperationType::CONV_2D) == ANEURALNETWORKS_CONV_2D,
284 "OperationType::CONV_2D != ANEURALNETWORKS_CONV_2D");
285 static_assert(static_cast<int32_t>(OperationType::DEPTHWISE_CONV_2D) ==
286 ANEURALNETWORKS_DEPTHWISE_CONV_2D,
287 "OperationType::DEPTHWISE_CONV_2D != ANEURALNETWORKS_DEPTHWISE_CONV_2D");
288 static_assert(static_cast<int32_t>(OperationType::DEPTH_TO_SPACE) == ANEURALNETWORKS_DEPTH_TO_SPACE,
289 "OperationType::DEPTH_TO_SPACE != ANEURALNETWORKS_DEPTH_TO_SPACE");
290 static_assert(static_cast<int32_t>(OperationType::DEQUANTIZE) == ANEURALNETWORKS_DEQUANTIZE,
291 "OperationType::DEQUANTIZE != ANEURALNETWORKS_DEQUANTIZE");
292 static_assert(static_cast<int32_t>(OperationType::EMBEDDING_LOOKUP) ==
293 ANEURALNETWORKS_EMBEDDING_LOOKUP,
294 "OperationType::EMBEDDING_LOOKUP != ANEURALNETWORKS_EMBEDDING_LOOKUP");
295 static_assert(static_cast<int32_t>(OperationType::FLOOR) == ANEURALNETWORKS_FLOOR,
296 "OperationType::FLOOR != ANEURALNETWORKS_FLOOR");
297 static_assert(static_cast<int32_t>(OperationType::FULLY_CONNECTED) ==
298 ANEURALNETWORKS_FULLY_CONNECTED,
299 "OperationType::FULLY_CONNECTED != ANEURALNETWORKS_FULLY_CONNECTED");
300 static_assert(static_cast<int32_t>(OperationType::HASHTABLE_LOOKUP) ==
301 ANEURALNETWORKS_HASHTABLE_LOOKUP,
302 "OperationType::HASHTABLE_LOOKUP != ANEURALNETWORKS_HASHTABLE_LOOKUP");
303 static_assert(static_cast<int32_t>(OperationType::L2_NORMALIZATION) ==
304 ANEURALNETWORKS_L2_NORMALIZATION,
305 "OperationType::L2_NORMALIZATION != ANEURALNETWORKS_L2_NORMALIZATION");
306 static_assert(static_cast<int32_t>(OperationType::L2_POOL_2D) == ANEURALNETWORKS_L2_POOL_2D,
307 "OperationType::L2_POOL_2D != ANEURALNETWORKS_L2_POOL_2D");
308 static_assert(static_cast<int32_t>(OperationType::LOCAL_RESPONSE_NORMALIZATION) ==
309 ANEURALNETWORKS_LOCAL_RESPONSE_NORMALIZATION,
310 "OperationType::LOCAL_RESPONSE_NORMALIZATION != "
311 "ANEURALNETWORKS_LOCAL_RESPONSE_NORMALIZATION");
312 static_assert(static_cast<int32_t>(OperationType::LOGISTIC) == ANEURALNETWORKS_LOGISTIC,
313 "OperationType::LOGISTIC != ANEURALNETWORKS_LOGISTIC");
314 static_assert(static_cast<int32_t>(OperationType::LSH_PROJECTION) == ANEURALNETWORKS_LSH_PROJECTION,
315 "OperationType::LSH_PROJECTION != ANEURALNETWORKS_LSH_PROJECTION");
316 static_assert(static_cast<int32_t>(OperationType::LSTM) == ANEURALNETWORKS_LSTM,
317 "OperationType::LSTM != ANEURALNETWORKS_LSTM");
318 static_assert(static_cast<int32_t>(OperationType::MAX_POOL_2D) == ANEURALNETWORKS_MAX_POOL_2D,
319 "OperationType::MAX_POOL_2D != ANEURALNETWORKS_MAX_POOL_2D");
320 static_assert(static_cast<int32_t>(OperationType::MUL) == ANEURALNETWORKS_MUL,
321 "OperationType::MUL != ANEURALNETWORKS_MUL");
322 static_assert(static_cast<int32_t>(OperationType::RELU) == ANEURALNETWORKS_RELU,
323 "OperationType::RELU != ANEURALNETWORKS_RELU");
324 static_assert(static_cast<int32_t>(OperationType::RELU1) == ANEURALNETWORKS_RELU1,
325 "OperationType::RELU1 != ANEURALNETWORKS_RELU1");
326 static_assert(static_cast<int32_t>(OperationType::RELU6) == ANEURALNETWORKS_RELU6,
327 "OperationType::RELU6 != ANEURALNETWORKS_RELU6");
328 static_assert(static_cast<int32_t>(OperationType::RESHAPE) == ANEURALNETWORKS_RESHAPE,
329 "OperationType::RESHAPE != ANEURALNETWORKS_RESHAPE");
330 static_assert(static_cast<int32_t>(OperationType::RESIZE_BILINEAR) ==
331 ANEURALNETWORKS_RESIZE_BILINEAR,
332 "OperationType::RESIZE_BILINEAR != ANEURALNETWORKS_RESIZE_BILINEAR");
333 static_assert(static_cast<int32_t>(OperationType::RNN) == ANEURALNETWORKS_RNN,
334 "OperationType::RNN != ANEURALNETWORKS_RNN");
335 static_assert(static_cast<int32_t>(OperationType::SOFTMAX) == ANEURALNETWORKS_SOFTMAX,
336 "OperationType::SOFTMAX != ANEURALNETWORKS_SOFTMAX");
337 static_assert(static_cast<int32_t>(OperationType::SPACE_TO_DEPTH) == ANEURALNETWORKS_SPACE_TO_DEPTH,
338 "OperationType::SPACE_TO_DEPTH != ANEURALNETWORKS_SPACE_TO_DEPTH");
339 static_assert(static_cast<int32_t>(OperationType::SVDF) == ANEURALNETWORKS_SVDF,
340 "OperationType::SVDF != ANEURALNETWORKS_SVDF");
341 static_assert(static_cast<int32_t>(OperationType::TANH) == ANEURALNETWORKS_TANH,
342 "OperationType::TANH != ANEURALNETWORKS_TANH");
343
344 static_assert(static_cast<int32_t>(FusedActivationFunc::NONE) == ANEURALNETWORKS_FUSED_NONE,
345 "FusedActivationFunc::NONE != ANEURALNETWORKS_FUSED_NONE");
346 static_assert(static_cast<int32_t>(FusedActivationFunc::RELU) == ANEURALNETWORKS_FUSED_RELU,
347 "FusedActivationFunc::RELU != ANEURALNETWORKS_FUSED_RELU");
348 static_assert(static_cast<int32_t>(FusedActivationFunc::RELU1) == ANEURALNETWORKS_FUSED_RELU1,
349 "FusedActivationFunc::RELU1 != ANEURALNETWORKS_FUSED_RELU1");
350 static_assert(static_cast<int32_t>(FusedActivationFunc::RELU6) == ANEURALNETWORKS_FUSED_RELU6,
351 "FusedActivationFunc::RELU6 != ANEURALNETWORKS_FUSED_RELU6");
352
353 // Make sure that the constants are compatible with the values defined in
354 // hardware/interfaces/neuralnetworks/1.1/types.hal.
355 static_assert(static_cast<int32_t>(OperationType::BATCH_TO_SPACE_ND) ==
356 ANEURALNETWORKS_BATCH_TO_SPACE_ND,
357 "OperationType::BATCH_TO_SPACE_ND != ANEURALNETWORKS_BATCH_TO_SPACE_ND");
358 static_assert(static_cast<int32_t>(OperationType::DIV) == ANEURALNETWORKS_DIV,
359 "OperationType::DIV != ANEURALNETWORKS_DIV");
360 static_assert(static_cast<int32_t>(OperationType::MEAN) == ANEURALNETWORKS_MEAN,
361 "OperationType::MEAN != ANEURALNETWORKS_MEAN");
362 static_assert(static_cast<int32_t>(OperationType::PAD) == ANEURALNETWORKS_PAD,
363 "OperationType::PAD != ANEURALNETWORKS_PAD");
364 static_assert(static_cast<int32_t>(OperationType::SPACE_TO_BATCH_ND) ==
365 ANEURALNETWORKS_SPACE_TO_BATCH_ND,
366 "OperationType::SPACE_TO_BATCH_ND != ANEURALNETWORKS_SPACE_TO_BATCH_ND");
367 static_assert(static_cast<int32_t>(OperationType::SQUEEZE) == ANEURALNETWORKS_SQUEEZE,
368 "OperationType::SQUEEZE != ANEURALNETWORKS_SQUEEZE");
369 static_assert(static_cast<int32_t>(OperationType::STRIDED_SLICE) == ANEURALNETWORKS_STRIDED_SLICE,
370 "OperationType::STRIDED_SLICE != ANEURALNETWORKS_STRIDED_SLICE");
371 static_assert(static_cast<int32_t>(OperationType::SUB) == ANEURALNETWORKS_SUB,
372 "OperationType::SUB != ANEURALNETWORKS_SUB");
373 static_assert(static_cast<int32_t>(OperationType::TRANSPOSE) == ANEURALNETWORKS_TRANSPOSE,
374 "OperationType::TRANSPOSE != ANEURALNETWORKS_TRANSPOSE");
375
376 // Make sure that the constants are compatible with the values defined in
377 // hardware/interfaces/neuralnetworks/1.2/types.hal.
378 static_assert(static_cast<int32_t>(OperandType::BOOL) == ANEURALNETWORKS_BOOL,
379 "BOOL != ANEURALNETWORKS_BOOL");
380 static_assert(static_cast<int32_t>(OperandType::TENSOR_QUANT16_SYMM) ==
381 ANEURALNETWORKS_TENSOR_QUANT16_SYMM,
382 "TENSOR_QUANT16_SYMM != ANEURALNETWORKS_TENSOR_QUANT16_SYMM");
383 static_assert(static_cast<int32_t>(OperandType::TENSOR_FLOAT16) == ANEURALNETWORKS_TENSOR_FLOAT16,
384 "TENSOR_FLOAT16 != ANEURALNETWORKS_TENSOR_FLOAT16");
385 static_assert(static_cast<int32_t>(OperandType::TENSOR_BOOL8) == ANEURALNETWORKS_TENSOR_BOOL8,
386 "TENSOR_BOOL8 != ANEURALNETWORKS_TENSOR_BOOL8");
387 static_assert(static_cast<int32_t>(OperandType::FLOAT16) == ANEURALNETWORKS_FLOAT16,
388 "FLOAT16 != ANEURALNETWORKS_FLOAT16");
389 static_assert(static_cast<int32_t>(OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) ==
390 ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL,
391 "TENSOR_QUANT8_SYMM_PER_CHANNEL != ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL");
392 static_assert(static_cast<int32_t>(OperandType::TENSOR_QUANT16_ASYMM) ==
393 ANEURALNETWORKS_TENSOR_QUANT16_ASYMM,
394 "TENSOR_QUANT16_ASYMM != ANEURALNETWORKS_TENSOR_QUANT16_ASYMM");
395 static_assert(static_cast<int32_t>(OperandType::TENSOR_QUANT8_SYMM) ==
396 ANEURALNETWORKS_TENSOR_QUANT8_SYMM,
397 "TENSOR_QUANT8_SYMM != ANEURALNETWORKS_TENSOR_QUANT8_SYMM");
398
399 static_assert(static_cast<int32_t>(OperationType::ABS) == ANEURALNETWORKS_ABS,
400 "OperationType::ABS != ANEURALNETWORKS_ABS");
401 static_assert(static_cast<int32_t>(OperationType::ARGMAX) == ANEURALNETWORKS_ARGMAX,
402 "OperationType::ARGMAX != ANEURALNETWORKS_ARGMAX");
403 static_assert(static_cast<int32_t>(OperationType::ARGMIN) == ANEURALNETWORKS_ARGMIN,
404 "OperationType::ARGMIN != ANEURALNETWORKS_ARGMIN");
405 static_assert(static_cast<int32_t>(OperationType::AXIS_ALIGNED_BBOX_TRANSFORM) ==
406 ANEURALNETWORKS_AXIS_ALIGNED_BBOX_TRANSFORM,
407 "OperationType::AXIS_ALIGNED_BBOX_TRANSFORM != "
408 "ANEURALNETWORKS_AXIS_ALIGNED_BBOX_TRANSFORM");
409 static_assert(static_cast<int32_t>(OperationType::BIDIRECTIONAL_SEQUENCE_LSTM) ==
410 ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_LSTM,
411 "OperationType::BIDIRECTIONAL_SEQUENCE_LSTM != "
412 "ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_LSTM");
413 static_assert(
414 static_cast<int32_t>(OperationType::BIDIRECTIONAL_SEQUENCE_RNN) ==
415 ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_RNN,
416 "OperationType::BIDIRECTIONAL_SEQUENCE_RNN != ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_RNN");
417 static_assert(static_cast<int32_t>(OperationType::BOX_WITH_NMS_LIMIT) ==
418 ANEURALNETWORKS_BOX_WITH_NMS_LIMIT,
419 "OperationType::BOX_WITH_NMS_LIMIT != ANEURALNETWORKS_BOX_WITH_NMS_LIMIT");
420 static_assert(static_cast<int32_t>(OperationType::CAST) == ANEURALNETWORKS_CAST,
421 "OperationType::CAST != ANEURALNETWORKS_CAST");
422 static_assert(static_cast<int32_t>(OperationType::CHANNEL_SHUFFLE) ==
423 ANEURALNETWORKS_CHANNEL_SHUFFLE,
424 "OperationType::CHANNEL_SHUFFLE != ANEURALNETWORKS_CHANNEL_SHUFFLE");
425 static_assert(
426 static_cast<int32_t>(OperationType::DETECTION_POSTPROCESSING) ==
427 ANEURALNETWORKS_DETECTION_POSTPROCESSING,
428 "OperationType::DETECTION_POSTPROCESSING != ANEURALNETWORKS_DETECTION_POSTPROCESSING");
429 static_assert(static_cast<int32_t>(OperationType::EQUAL) == ANEURALNETWORKS_EQUAL,
430 "OperationType::EQUAL != ANEURALNETWORKS_EQUAL");
431 static_assert(static_cast<int32_t>(OperationType::EXP) == ANEURALNETWORKS_EXP,
432 "OperationType::EXP != ANEURALNETWORKS_EXP");
433 static_assert(static_cast<int32_t>(OperationType::EXPAND_DIMS) == ANEURALNETWORKS_EXPAND_DIMS,
434 "OperationType::EXPAND_DIMS != ANEURALNETWORKS_EXPAND_DIMS");
435 static_assert(static_cast<int32_t>(OperationType::GATHER) == ANEURALNETWORKS_GATHER,
436 "OperationType::GATHER != ANEURALNETWORKS_GATHER");
437 static_assert(static_cast<int32_t>(OperationType::GENERATE_PROPOSALS) ==
438 ANEURALNETWORKS_GENERATE_PROPOSALS,
439 "OperationType::GENERATE_PROPOSALS != ANEURALNETWORKS_GENERATE_PROPOSALS");
440 static_assert(static_cast<int32_t>(OperationType::GREATER) == ANEURALNETWORKS_GREATER,
441 "OperationType::GREATER != ANEURALNETWORKS_GREATER");
442 static_assert(static_cast<int32_t>(OperationType::GREATER_EQUAL) == ANEURALNETWORKS_GREATER_EQUAL,
443 "OperationType::GREATER_EQUAL != ANEURALNETWORKS_GREATER_EQUAL");
444 static_assert(static_cast<int32_t>(OperationType::GROUPED_CONV_2D) ==
445 ANEURALNETWORKS_GROUPED_CONV_2D,
446 "OperationType::GROUPED_CONV_2D != ANEURALNETWORKS_GROUPED_CONV_2D");
447 static_assert(static_cast<int32_t>(OperationType::HEATMAP_MAX_KEYPOINT) ==
448 ANEURALNETWORKS_HEATMAP_MAX_KEYPOINT,
449 "OperationType::HEATMAP_MAX_KEYPOINT != ANEURALNETWORKS_HEATMAP_MAX_KEYPOINT");
450 static_assert(static_cast<int32_t>(OperationType::INSTANCE_NORMALIZATION) ==
451 ANEURALNETWORKS_INSTANCE_NORMALIZATION,
452 "OperationType::INSTANCE_NORMALIZATION != ANEURALNETWORKS_INSTANCE_NORMALIZATION");
453 static_assert(static_cast<int32_t>(OperationType::LESS) == ANEURALNETWORKS_LESS,
454 "OperationType::LESS != ANEURALNETWORKS_LESS");
455 static_assert(static_cast<int32_t>(OperationType::LESS_EQUAL) == ANEURALNETWORKS_LESS_EQUAL,
456 "OperationType::LESS_EQUAL != ANEURALNETWORKS_LESS_EQUAL");
457 static_assert(static_cast<int32_t>(OperationType::LOG) == ANEURALNETWORKS_LOG,
458 "OperationType::LOG != ANEURALNETWORKS_LOG");
459 static_assert(static_cast<int32_t>(OperationType::LOGICAL_AND) == ANEURALNETWORKS_LOGICAL_AND,
460 "OperationType::LOGICAL_AND != ANEURALNETWORKS_LOGICAL_AND");
461 static_assert(static_cast<int32_t>(OperationType::LOGICAL_NOT) == ANEURALNETWORKS_LOGICAL_NOT,
462 "OperationType::LOGICAL_NOT != ANEURALNETWORKS_LOGICAL_NOT");
463 static_assert(static_cast<int32_t>(OperationType::LOGICAL_OR) == ANEURALNETWORKS_LOGICAL_OR,
464 "OperationType::LOGICAL_OR != ANEURALNETWORKS_LOGICAL_OR");
465 static_assert(static_cast<int32_t>(OperationType::LOG_SOFTMAX) == ANEURALNETWORKS_LOG_SOFTMAX,
466 "OperationType::LOG_SOFTMAX != ANEURALNETWORKS_LOG_SOFTMAX");
467 static_assert(static_cast<int32_t>(OperationType::MAXIMUM) == ANEURALNETWORKS_MAXIMUM,
468 "OperationType::MAXIMUM != ANEURALNETWORKS_MAXIMUM");
469 static_assert(static_cast<int32_t>(OperationType::MINIMUM) == ANEURALNETWORKS_MINIMUM,
470 "OperationType::MINIMUM != ANEURALNETWORKS_MINIMUM");
471 static_assert(static_cast<int32_t>(OperationType::NEG) == ANEURALNETWORKS_NEG,
472 "OperationType::NEG != ANEURALNETWORKS_NEG");
473 static_assert(static_cast<int32_t>(OperationType::NOT_EQUAL) == ANEURALNETWORKS_NOT_EQUAL,
474 "OperationType::NOT_EQUAL != ANEURALNETWORKS_NOT_EQUAL");
475 static_assert(static_cast<int32_t>(OperationType::PAD_V2) == ANEURALNETWORKS_PAD_V2,
476 "OperationType::PAD_V2 != ANEURALNETWORKS_PAD_V2");
477 static_assert(static_cast<int32_t>(OperationType::POW) == ANEURALNETWORKS_POW,
478 "OperationType::POW != ANEURALNETWORKS_POW");
479 static_assert(static_cast<int32_t>(OperationType::PRELU) == ANEURALNETWORKS_PRELU,
480 "OperationType::PRELU != ANEURALNETWORKS_PRELU");
481 static_assert(static_cast<int32_t>(OperationType::QUANTIZE) == ANEURALNETWORKS_QUANTIZE,
482 "OperationType::QUANTIZE != ANEURALNETWORKS_QUANTIZE");
483 static_assert(static_cast<int32_t>(OperationType::QUANTIZED_16BIT_LSTM) ==
484 ANEURALNETWORKS_QUANTIZED_16BIT_LSTM,
485 "OperationType::QUANTIZED_16BIT_LSTM != ANEURALNETWORKS_QUANTIZED_16BIT_LSTM");
486 static_assert(static_cast<int32_t>(OperationType::RANDOM_MULTINOMIAL) ==
487 ANEURALNETWORKS_RANDOM_MULTINOMIAL,
488 "OperationType::RANDOM_MULTINOMIAL != ANEURALNETWORKS_RANDOM_MULTINOMIAL");
489 static_assert(static_cast<int32_t>(OperationType::REDUCE_ALL) == ANEURALNETWORKS_REDUCE_ALL,
490 "OperationType::REDUCE_ALL != ANEURALNETWORKS_REDUCE_ALL");
491 static_assert(static_cast<int32_t>(OperationType::REDUCE_ANY) == ANEURALNETWORKS_REDUCE_ANY,
492 "OperationType::REDUCE_ANY != ANEURALNETWORKS_REDUCE_ANY");
493 static_assert(static_cast<int32_t>(OperationType::REDUCE_MAX) == ANEURALNETWORKS_REDUCE_MAX,
494 "OperationType::REDUCE_MAX != ANEURALNETWORKS_REDUCE_MAX");
495 static_assert(static_cast<int32_t>(OperationType::REDUCE_MIN) == ANEURALNETWORKS_REDUCE_MIN,
496 "OperationType::REDUCE_MIN != ANEURALNETWORKS_REDUCE_MIN");
497 static_assert(static_cast<int32_t>(OperationType::REDUCE_PROD) == ANEURALNETWORKS_REDUCE_PROD,
498 "OperationType::REDUCE_PROD != ANEURALNETWORKS_REDUCE_PROD");
499 static_assert(static_cast<int32_t>(OperationType::REDUCE_SUM) == ANEURALNETWORKS_REDUCE_SUM,
500 "OperationType::REDUCE_SUM != ANEURALNETWORKS_REDUCE_SUM");
501 static_assert(static_cast<int32_t>(OperationType::ROI_ALIGN) == ANEURALNETWORKS_ROI_ALIGN,
502 "OperationType::ROI_ALIGN != ANEURALNETWORKS_ROI_ALIGN");
503 static_assert(static_cast<int32_t>(OperationType::ROI_POOLING) == ANEURALNETWORKS_ROI_POOLING,
504 "OperationType::ROI_POOLING != ANEURALNETWORKS_ROI_POOLING");
505 static_assert(static_cast<int32_t>(OperationType::RSQRT) == ANEURALNETWORKS_RSQRT,
506 "OperationType::RSQRT != ANEURALNETWORKS_RSQRT");
507 static_assert(static_cast<int32_t>(OperationType::SELECT) == ANEURALNETWORKS_SELECT,
508 "OperationType::SELECT != ANEURALNETWORKS_SELECT");
509 static_assert(static_cast<int32_t>(OperationType::SIN) == ANEURALNETWORKS_SIN,
510 "OperationType::SIN != ANEURALNETWORKS_SIN");
511 static_assert(static_cast<int32_t>(OperationType::SLICE) == ANEURALNETWORKS_SLICE,
512 "OperationType::SLICE != ANEURALNETWORKS_SLICE");
513 static_assert(static_cast<int32_t>(OperationType::SPLIT) == ANEURALNETWORKS_SPLIT,
514 "OperationType::SPLIT != ANEURALNETWORKS_SPLIT");
515 static_assert(static_cast<int32_t>(OperationType::SQRT) == ANEURALNETWORKS_SQRT,
516 "OperationType::SQRT != ANEURALNETWORKS_SQRT");
517 static_assert(static_cast<int32_t>(OperationType::TILE) == ANEURALNETWORKS_TILE,
518 "OperationType::TILE != ANEURALNETWORKS_TILE");
519 static_assert(static_cast<int32_t>(OperationType::TOPK_V2) == ANEURALNETWORKS_TOPK_V2,
520 "OperationType::TOPK_V2 != ANEURALNETWORKS_TOPK_V2");
521 static_assert(static_cast<int32_t>(OperationType::TRANSPOSE_CONV_2D) ==
522 ANEURALNETWORKS_TRANSPOSE_CONV_2D,
523 "OperationType::TRANSPOSE_CONV_2D != ANEURALNETWORKS_TRANSPOSE_CONV_2D");
524 static_assert(static_cast<int32_t>(OperationType::UNIDIRECTIONAL_SEQUENCE_LSTM) ==
525 ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_LSTM,
526 "OperationType::UNIDIRECTIONAL_SEQUENCE_LSTM != "
527 "ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_LSTM");
528 static_assert(static_cast<int32_t>(OperationType::UNIDIRECTIONAL_SEQUENCE_RNN) ==
529 ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_RNN,
530 "OperationType::UNIDIRECTIONAL_SEQUENCE_RNN != "
531 "ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_RNN");
532 static_assert(static_cast<int32_t>(OperationType::RESIZE_NEAREST_NEIGHBOR) ==
533 ANEURALNETWORKS_RESIZE_NEAREST_NEIGHBOR,
534 "OperationType::RESIZE_NEAREST_NEIGHBOR != ANEURALNETWORKS_RESIZE_NEAREST_NEIGHBOR");
535 static_assert(static_cast<int32_t>(OperationType::QUANTIZED_LSTM) == ANEURALNETWORKS_QUANTIZED_LSTM,
536 "OperationType::QUANTIZED_LSTM != ANEURALNETWORKS_QUANTIZED_LSTM");
537 static_assert(static_cast<int32_t>(OperationType::IF) == ANEURALNETWORKS_IF,
538 "OperationType::IF != ANEURALNETWORKS_IF");
539 static_assert(static_cast<int32_t>(OperationType::WHILE) == ANEURALNETWORKS_WHILE,
540 "OperationType::WHILE != ANEURALNETWORKS_WHILE");
541 static_assert(static_cast<int32_t>(OperationType::ELU) == ANEURALNETWORKS_ELU,
542 "OperationType::ELU != ANEURALNETWORKS_ELU");
543 static_assert(static_cast<int32_t>(OperationType::HARD_SWISH) == ANEURALNETWORKS_HARD_SWISH,
544 "OperationType::HARD_SWISH != ANEURALNETWORKS_HARD_SWISH");
545 static_assert(static_cast<int32_t>(OperationType::FILL) == ANEURALNETWORKS_FILL,
546 "OperationType::FILL != ANEURALNETWORKS_FILL");
547 static_assert(static_cast<int32_t>(OperationType::RANK) == ANEURALNETWORKS_RANK,
548 "OperationType::RANK != ANEURALNETWORKS_RANK");
549
550 static_assert(static_cast<int32_t>(DeviceType::OTHER) == ANEURALNETWORKS_DEVICE_OTHER,
551 "DeviceType::OTHER != ANEURALNETWORKS_DEVICE_OTHER");
552 static_assert(static_cast<int32_t>(DeviceType::CPU) == ANEURALNETWORKS_DEVICE_CPU,
553 "DeviceType::CPU != ANEURALNETWORKS_DEVICE_CPU");
554 static_assert(static_cast<int32_t>(DeviceType::GPU) == ANEURALNETWORKS_DEVICE_GPU,
555 "DeviceType::GPU != ANEURALNETWORKS_DEVICE_GPU");
556 static_assert(static_cast<int32_t>(DeviceType::ACCELERATOR) == ANEURALNETWORKS_DEVICE_ACCELERATOR,
557 "DeviceType::ACCELERATOR != ANEURALNETWORKS_DEVICE_ACCELERATOR");
558
559 // Make sure that the constants are compatible with the values defined in
560 // hardware/interfaces/neuralnetworks/1.3/types.hal.
561 static_assert(android::nn::convertToHalPriority(ANEURALNETWORKS_PRIORITY_LOW) == Priority::LOW,
562 "ANEURALNETWORKS_PRIORITY_LOW does not map to Priority::LOW");
563 static_assert(android::nn::convertToHalPriority(ANEURALNETWORKS_PRIORITY_MEDIUM) ==
564 Priority::MEDIUM,
565 "ANEURALNETWORKS_PRIORITY_MEDIUM does not map to Priority::MEDIUM");
566 static_assert(android::nn::convertToHalPriority(ANEURALNETWORKS_PRIORITY_HIGH) == Priority::HIGH,
567 "ANEURALNETWORKS_PRIORITY_HIGH does not map to Priority::HIGH");
568
569 // Asserts for ANeuralNetworksOperandType memory layout
570 static_assert(offsetof(ANeuralNetworksOperandType, type) == 0,
571 "ANeuralNetworksOperandType.type offset != 0");
572 static_assert(offsetof(ANeuralNetworksOperandType, dimensionCount) == 4,
573 "ANeuralNetworksOperandType.dimensionCount offset != 4");
574 static_assert(offsetof(ANeuralNetworksOperandType, dimensions) == 8,
575 "ANeuralNetworksOperandType.dimensions offset != 8");
576 static_assert(offsetof(ANeuralNetworksOperandType, scale) == 8 + sizeof(void*),
577 "ANeuralNetworksOperandType.scale offset != 8 + sizeof(void*)");
578 static_assert(offsetof(ANeuralNetworksOperandType, zeroPoint) == 12 + sizeof(void*),
579 "ANeuralNetworksOperandType.zeroPoint offset != 12 + sizeof(void*)");
580 static_assert(sizeof(ANeuralNetworksOperandType) == 16 + sizeof(void*),
581 "ANeuralNetworksOperandType size changed");
582 static_assert(alignof(ANeuralNetworksOperandType) == alignof(void*),
583 "ANeuralNetworksOperandType alignment changed");
584
585 // Asserts for ANeuralNetworksSymmPerChannelQuantParams memory layout
586 static_assert(offsetof(ANeuralNetworksSymmPerChannelQuantParams, channelDim) == 0,
587 "ANeuralNetworksSymmPerChannelQuantParams.channelDim offset != 4 + sizeof(void*)");
588 static_assert(offsetof(ANeuralNetworksSymmPerChannelQuantParams, scaleCount) == 4,
589 "ANeuralNetworksSymmPerChannelQuantParams.scaleCount offset != 0");
590 static_assert(offsetof(ANeuralNetworksSymmPerChannelQuantParams, scales) == 8,
591 "ANeuralNetworksSymmPerChannelQuantParams.scales offset != 4");
592 static_assert(sizeof(ANeuralNetworksSymmPerChannelQuantParams) == 8 + sizeof(void*),
593 "ANeuralNetworksSymmPerChannelQuantParams size != 8 + sizeof(void*)");
594 static_assert(alignof(ANeuralNetworksSymmPerChannelQuantParams) == alignof(void*),
595 "ANeuralNetworksOperandType alignment changed");
596
597 // Asserts for compilation caching
598 static_assert(ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN == 32,
599 "ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN has changed");
600 static_assert(static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN) ==
601 ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN,
602 "Constant::BYTE_SIZE_OF_CACHE_TOKEN != ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN");
603
604 // Asserts for compilation priority
605 static_assert(ANEURALNETWORKS_PRIORITY_LOW == 90, "ANEURALNETWORKS_PRIORITY_LOW has changed");
606 static_assert(ANEURALNETWORKS_PRIORITY_MEDIUM == 100,
607 "ANEURALNETWORKS_PRIORITY_MEDIUM has changed");
608 static_assert(ANEURALNETWORKS_PRIORITY_HIGH == 110, "ANEURALNETWORKS_PRIORITY_HIGH has changed");
609 static_assert(ANEURALNETWORKS_PRIORITY_DEFAULT == ANEURALNETWORKS_PRIORITY_MEDIUM,
610 "ANEURALNETWORKS_PRIORITY_DEFAULT has changed");
611
612 // Asserts for loop timeout duration
613 static_assert(static_cast<uint64_t>(LoopTimeoutDurationNs::DEFAULT) ==
614 operation_while::kTimeoutNsDefault,
615 "LoopTimeoutDurationNs::DEFAULT != operation_while::kTimeoutNsDefault");
616 static_assert(static_cast<uint64_t>(LoopTimeoutDurationNs::MAXIMUM) ==
617 operation_while::kTimeoutNsMaximum,
618 "LoopTimeoutDurationNs::MAXIMUM != operation_while::kTimeoutNsMaximum");
619
ANeuralNetworks_getDeviceCount(uint32_t * numDevices)620 int ANeuralNetworks_getDeviceCount(uint32_t* numDevices) {
621 if (numDevices == nullptr) {
622 LOG(ERROR) << "ANeuralNetworks_getDeviceCount passed a nullptr";
623 return ANEURALNETWORKS_UNEXPECTED_NULL;
624 }
625 *numDevices = DeviceManager::get()->getDrivers().size();
626 return ANEURALNETWORKS_NO_ERROR;
627 }
628
ANeuralNetworks_getDevice(uint32_t devIndex,ANeuralNetworksDevice ** device)629 int ANeuralNetworks_getDevice(uint32_t devIndex, ANeuralNetworksDevice** device) {
630 if (device == nullptr) {
631 LOG(ERROR) << "ANeuralNetworks_getDevice passed a nullptr";
632 return ANEURALNETWORKS_UNEXPECTED_NULL;
633 }
634 const std::vector<std::shared_ptr<Device>>& devices = DeviceManager::get()->getDrivers();
635 if (devIndex >= devices.size()) {
636 LOG(ERROR) << "ANeuralNetworks_getDevice passed an invalid device index";
637 return ANEURALNETWORKS_BAD_DATA;
638 }
639 *device = reinterpret_cast<ANeuralNetworksDevice*>(devices.at(devIndex).get());
640 return ANEURALNETWORKS_NO_ERROR;
641 }
642
ANeuralNetworksDevice_getName(const ANeuralNetworksDevice * device,const char ** name)643 int ANeuralNetworksDevice_getName(const ANeuralNetworksDevice* device, const char** name) {
644 if (device == nullptr || name == nullptr) {
645 LOG(ERROR) << "ANeuralNetworksDevice_getName passed a nullptr";
646 return ANEURALNETWORKS_UNEXPECTED_NULL;
647 }
648 const Device* d = reinterpret_cast<const Device*>(device);
649 *name = d->getName().c_str();
650 return ANEURALNETWORKS_NO_ERROR;
651 }
652
ANeuralNetworksDevice_getVersion(const ANeuralNetworksDevice * device,const char ** version)653 int ANeuralNetworksDevice_getVersion(const ANeuralNetworksDevice* device, const char** version) {
654 if (device == nullptr || version == nullptr) {
655 LOG(ERROR) << "ANeuralNetworksDevice_getVersion passed a nullptr";
656 return ANEURALNETWORKS_UNEXPECTED_NULL;
657 }
658 const Device* d = reinterpret_cast<const Device*>(device);
659 *version = d->getVersionString().c_str();
660 return ANEURALNETWORKS_NO_ERROR;
661 }
662
ANeuralNetworksDevice_getType(const ANeuralNetworksDevice * device,int32_t * type)663 int ANeuralNetworksDevice_getType(const ANeuralNetworksDevice* device, int32_t* type) {
664 if (device == nullptr || type == nullptr) {
665 LOG(ERROR) << "ANeuralNetworksDevice_getType passed a nullptr";
666 return ANEURALNETWORKS_UNEXPECTED_NULL;
667 }
668 const Device* d = reinterpret_cast<const Device*>(device);
669 int32_t dType = d->getType();
670 if (dType < 0) {
671 return ANEURALNETWORKS_OP_FAILED;
672 }
673 *type = d->getType();
674 return ANEURALNETWORKS_NO_ERROR;
675 }
676
ANeuralNetworksDevice_getFeatureLevel(const ANeuralNetworksDevice * device,int64_t * featureLevel)677 int ANeuralNetworksDevice_getFeatureLevel(const ANeuralNetworksDevice* device,
678 int64_t* featureLevel) {
679 if (device == nullptr || featureLevel == nullptr) {
680 LOG(ERROR) << "ANeuralNetworksDevice_getFeatureLevel passed a nullptr";
681 return ANEURALNETWORKS_UNEXPECTED_NULL;
682 }
683 Device* d = reinterpret_cast<Device*>(const_cast<ANeuralNetworksDevice*>(device));
684 int64_t dFeatureLevel = d->getFeatureLevel();
685 if (dFeatureLevel < 0) {
686 return ANEURALNETWORKS_BAD_STATE;
687 }
688 *featureLevel = dFeatureLevel;
689 return ANEURALNETWORKS_NO_ERROR;
690 }
691
ANeuralNetworksDevice_wait(const ANeuralNetworksDevice * device)692 int ANeuralNetworksDevice_wait(const ANeuralNetworksDevice* device) {
693 if (device == nullptr) {
694 LOG(ERROR) << "ANeuralNetworksDevice_wait passed a nullptr";
695 return ANEURALNETWORKS_UNEXPECTED_NULL;
696 }
697 const Device* d = reinterpret_cast<const Device*>(device);
698 return d->wait();
699 }
700
ANeuralNetworksModel_getSupportedOperationsForDevices(const ANeuralNetworksModel * model,const ANeuralNetworksDevice * const * devices,uint32_t numDevices,bool * supportedOps)701 int ANeuralNetworksModel_getSupportedOperationsForDevices(
702 const ANeuralNetworksModel* model, const ANeuralNetworksDevice* const* devices,
703 uint32_t numDevices, bool* supportedOps) {
704 NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksModel_getSupportedOperationsForDevices");
705 if (model == nullptr || devices == nullptr || supportedOps == nullptr) {
706 LOG(ERROR) << "ANeuralNetworksModel_getSupportedOperationsForDevices passed a nullptr";
707 return ANEURALNETWORKS_UNEXPECTED_NULL;
708 }
709 if (numDevices == 0) {
710 LOG(ERROR) << "ANeuralNetworksModel_getSupportedOperationsForDevices passed an empty "
711 "device list";
712 return ANEURALNETWORKS_BAD_DATA;
713 }
714 const ModelBuilder* m = reinterpret_cast<const ModelBuilder*>(model);
715 if (!m->isFinished() || !m->isValid()) {
716 LOG(ERROR) << "ANeuralNetworksModel_getSupportedOperationsForDevices passed an unfinished "
717 "or invalid Model";
718 return ANEURALNETWORKS_BAD_STATE;
719 }
720
721 const Model hidlModel = m->makeHidlModel();
722 const std::vector<uint32_t>& opMap = m->getSortedOperationMapping();
723 // init the output array to false for all the operations.
724 std::fill(supportedOps, supportedOps + opMap.size(), false);
725 for (uint32_t i = 0; i < numDevices; i++) {
726 if (devices[i] == nullptr) {
727 LOG(ERROR) << "ANeuralNetworksModel_getSupportedOperationsForDevices passed a nullptr "
728 "as a device";
729 return ANEURALNETWORKS_UNEXPECTED_NULL;
730 }
731 for (uint32_t j = i + 1; j < numDevices; j++) {
732 if (devices[i] == devices[j]) {
733 LOG(ERROR) << "ANeuralNetworksModel_getSupportedOperationsForDevices passed "
734 "duplicate devices";
735 return ANEURALNETWORKS_BAD_DATA;
736 }
737 }
738
739 Device* d = reinterpret_cast<Device*>(const_cast<ANeuralNetworksDevice*>(devices[i]));
740 const MetaModel metaModel(hidlModel, DeviceManager::get()->strictSlicing());
741 const std::vector<bool> supportsByDevice = d->getSupportedOperations(metaModel);
742 for (uint32_t j = 0; j < supportsByDevice.size(); j++) {
743 uint32_t originalIdx = opMap[j];
744 supportedOps[originalIdx] |= supportsByDevice[j];
745 }
746 }
747 return ANEURALNETWORKS_NO_ERROR;
748 }
749
ANeuralNetworksCompilation_createForDevices(ANeuralNetworksModel * model,const ANeuralNetworksDevice * const * devices,uint32_t numDevices,ANeuralNetworksCompilation ** compilation)750 int ANeuralNetworksCompilation_createForDevices(ANeuralNetworksModel* model,
751 const ANeuralNetworksDevice* const* devices,
752 uint32_t numDevices,
753 ANeuralNetworksCompilation** compilation) {
754 NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_createForDevices");
755 if (model == nullptr || devices == nullptr || compilation == nullptr) {
756 LOG(ERROR) << "ANeuralNetworksCompilation_createForDevices passed a nullptr";
757 return ANEURALNETWORKS_UNEXPECTED_NULL;
758 }
759
760 if (numDevices == 0) {
761 LOG(ERROR) << "ANeuralNetworksCompilation_createForDevices passed an empty device list";
762 return ANEURALNETWORKS_BAD_DATA;
763 }
764
765 std::vector<std::shared_ptr<Device>> selectedDevices;
766 for (uint32_t i = 0; i < numDevices; i++) {
767 if (devices[i] == nullptr) {
768 LOG(ERROR)
769 << "ANeuralNetworksCompilation_createForDevices passed a nullptr as a device";
770 return ANEURALNETWORKS_UNEXPECTED_NULL;
771 }
772 for (uint32_t j = i + 1; j < numDevices; j++) {
773 if (devices[i] == devices[j]) {
774 LOG(ERROR)
775 << "ANeuralNetworksCompilation_createForDevices passed duplicate devices";
776 return ANEURALNETWORKS_BAD_DATA;
777 }
778 }
779 for (auto& device : DeviceManager::get()->getDrivers()) {
780 if (device.get() == reinterpret_cast<const Device*>(devices[i])) {
781 // Find a match
782 selectedDevices.push_back(device);
783 break;
784 }
785 }
786 }
787
788 if (selectedDevices.size() != numDevices) {
789 LOG(ERROR) << "ANeuralNetworksCompilation_createForDevices passed an invalid device set";
790 return ANEURALNETWORKS_BAD_DATA;
791 }
792 ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
793 CompilationBuilder* c = nullptr;
794 // No CPU fallback when user specifies the list of devices manually.
795 int result = m->createCompilation(&c, selectedDevices, /* explicitDeviceList */ true);
796 *compilation = reinterpret_cast<ANeuralNetworksCompilation*>(c);
797 return result;
798 }
799
ANeuralNetworksExecution_compute(ANeuralNetworksExecution * execution)800 int ANeuralNetworksExecution_compute(ANeuralNetworksExecution* execution) {
801 NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_compute");
802 if (!execution) {
803 LOG(ERROR) << "ANeuralNetworksExecution_compute passed a nullptr";
804 return ANEURALNETWORKS_UNEXPECTED_NULL;
805 }
806 // TODO validate the rest
807
808 ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
809 return r->computeSynchronously();
810 }
811
ANeuralNetworksExecution_setMeasureTiming(ANeuralNetworksExecution * execution,bool measure)812 int ANeuralNetworksExecution_setMeasureTiming(ANeuralNetworksExecution* execution, bool measure) {
813 NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_setMeasureTiming");
814 if (!execution) {
815 LOG(ERROR) << "ANeuralNetworksExecution_setMeasureTiming passed a nullptr";
816 return ANEURALNETWORKS_UNEXPECTED_NULL;
817 }
818 ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
819 return r->setMeasureTiming(measure);
820 }
821
ANeuralNetworksExecution_getDuration(const ANeuralNetworksExecution * execution,int32_t durationCode,uint64_t * duration)822 int ANeuralNetworksExecution_getDuration(const ANeuralNetworksExecution* execution,
823 int32_t durationCode, uint64_t* duration) {
824 NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_getDuration");
825 if (!execution || !duration) {
826 LOG(ERROR) << "ANeuralNetworksExecution_getDuration passed a nullptr";
827 return ANEURALNETWORKS_UNEXPECTED_NULL;
828 }
829 switch (durationCode) {
830 case ANEURALNETWORKS_DURATION_ON_HARDWARE:
831 case ANEURALNETWORKS_DURATION_IN_DRIVER:
832 case ANEURALNETWORKS_FENCED_DURATION_ON_HARDWARE:
833 case ANEURALNETWORKS_FENCED_DURATION_IN_DRIVER:
834 break;
835 default:
836 LOG(ERROR) << "ANeuralNetworksExecution_getDuration passed a bad durationCode "
837 << durationCode;
838 return ANEURALNETWORKS_BAD_DATA;
839 }
840 const ExecutionBuilder* r = reinterpret_cast<const ExecutionBuilder*>(execution);
841 return r->getDuration(durationCode, duration);
842 }
843
ANeuralNetworksBurst_create(ANeuralNetworksCompilation * compilation,ANeuralNetworksBurst ** burst)844 int ANeuralNetworksBurst_create(ANeuralNetworksCompilation* compilation,
845 ANeuralNetworksBurst** burst) {
846 NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksBurst_create");
847 if (!compilation || !burst) {
848 LOG(ERROR) << "ANeuralNetworksBurst_create passed a nullptr";
849 return ANEURALNETWORKS_UNEXPECTED_NULL;
850 }
851
852 CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
853 BurstBuilder* b = nullptr;
854 int result = c->createBurst(&b);
855 *burst = reinterpret_cast<ANeuralNetworksBurst*>(b);
856 return result;
857 }
858
ANeuralNetworksBurst_free(ANeuralNetworksBurst * burst)859 void ANeuralNetworksBurst_free(ANeuralNetworksBurst* burst) {
860 NNTRACE_RT(NNTRACE_PHASE_TERMINATION, "ANeuralNetworksBurst_free");
861 // No validation. Free of nullptr is valid.
862 BurstBuilder* b = reinterpret_cast<BurstBuilder*>(burst);
863 delete b;
864 }
865
ANeuralNetworksExecution_burstCompute(ANeuralNetworksExecution * execution,ANeuralNetworksBurst * burst)866 int ANeuralNetworksExecution_burstCompute(ANeuralNetworksExecution* execution,
867 ANeuralNetworksBurst* burst) {
868 NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_burstCompute");
869 if (!execution || !burst) {
870 LOG(ERROR) << "ANeuralNetworksExecution_burstCompute passed a nullptr";
871 return ANEURALNETWORKS_UNEXPECTED_NULL;
872 }
873
874 ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
875 BurstBuilder* b = reinterpret_cast<BurstBuilder*>(burst);
876
877 if (r->getCompilation() != b->getCompilation()) {
878 LOG(ERROR) << "ANeuralNetworksBurst and ANeuralNetworksExecution "
879 "used in ANeuralNetworksExecution_burstCompute must "
880 "originate from the same ANeuralNetworksCompilation";
881 return ANEURALNETWORKS_BAD_DATA;
882 }
883
884 const bool locked = b->tryLock();
885 if (!locked) {
886 LOG(ERROR) << "ANeuralNetworksBurst is already being used in another "
887 "call to ANeuralNetworksExecution_burstCompute";
888 return ANEURALNETWORKS_BAD_STATE;
889 }
890
891 const int n = r->burstCompute(b);
892 b->unlock();
893
894 return n;
895 }
896
ANeuralNetworksMemoryDesc_create(ANeuralNetworksMemoryDesc ** desc)897 int ANeuralNetworksMemoryDesc_create(ANeuralNetworksMemoryDesc** desc) {
898 NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksMemoryDesc_create");
899 if (desc != nullptr) {
900 *desc = nullptr;
901 }
902 if (!desc) {
903 LOG(ERROR) << "ANeuralNetworksMemoryDesc_create passed a nullptr";
904 return ANEURALNETWORKS_UNEXPECTED_NULL;
905 }
906 auto mb = std::make_unique<MemoryBuilder>();
907 *desc = reinterpret_cast<ANeuralNetworksMemoryDesc*>(mb.release());
908 return ANEURALNETWORKS_NO_ERROR;
909 }
910
ANeuralNetworksMemoryDesc_free(ANeuralNetworksMemoryDesc * desc)911 void ANeuralNetworksMemoryDesc_free(ANeuralNetworksMemoryDesc* desc) {
912 NNTRACE_RT(NNTRACE_PHASE_TERMINATION, "ANeuralNetworksMemoryDesc_free");
913 // No validation. Free of nullptr is valid.
914 MemoryBuilder* mb = reinterpret_cast<MemoryBuilder*>(desc);
915 delete mb;
916 }
917
ANeuralNetworksMemoryDesc_addInputRole(ANeuralNetworksMemoryDesc * desc,const ANeuralNetworksCompilation * compilation,uint32_t index,float frequency)918 int ANeuralNetworksMemoryDesc_addInputRole(ANeuralNetworksMemoryDesc* desc,
919 const ANeuralNetworksCompilation* compilation,
920 uint32_t index, float frequency) {
921 NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksMemoryDesc_addInputRole");
922 if (!desc || !compilation) {
923 LOG(ERROR) << "ANeuralNetworksMemoryDesc_addInputRole passed a nullptr";
924 return ANEURALNETWORKS_UNEXPECTED_NULL;
925 }
926 MemoryBuilder* mb = reinterpret_cast<MemoryBuilder*>(desc);
927 const CompilationBuilder* c = reinterpret_cast<const CompilationBuilder*>(compilation);
928 return mb->addRole(*c, IOType::INPUT, index, frequency);
929 }
930
ANeuralNetworksMemoryDesc_addOutputRole(ANeuralNetworksMemoryDesc * desc,const ANeuralNetworksCompilation * compilation,uint32_t index,float frequency)931 int ANeuralNetworksMemoryDesc_addOutputRole(ANeuralNetworksMemoryDesc* desc,
932 const ANeuralNetworksCompilation* compilation,
933 uint32_t index, float frequency) {
934 NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksMemoryDesc_addOutputRole");
935 if (!desc || !compilation) {
936 LOG(ERROR) << "ANeuralNetworksMemoryDesc_addOutputRole passed a nullptr";
937 return ANEURALNETWORKS_UNEXPECTED_NULL;
938 }
939 MemoryBuilder* mb = reinterpret_cast<MemoryBuilder*>(desc);
940 const CompilationBuilder* c = reinterpret_cast<const CompilationBuilder*>(compilation);
941 return mb->addRole(*c, IOType::OUTPUT, index, frequency);
942 }
943
ANeuralNetworksMemoryDesc_setDimensions(ANeuralNetworksMemoryDesc * desc,uint32_t rank,const uint32_t * dimensions)944 int ANeuralNetworksMemoryDesc_setDimensions(ANeuralNetworksMemoryDesc* desc, uint32_t rank,
945 const uint32_t* dimensions) {
946 NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksMemoryDesc_setDimensions");
947 if (!desc || (!dimensions && rank > 0)) {
948 LOG(ERROR) << "ANeuralNetworksMemoryDesc_setDimensions passed a nullptr";
949 return ANEURALNETWORKS_UNEXPECTED_NULL;
950 }
951 const std::vector<uint32_t> dims(dimensions, dimensions + rank);
952 MemoryBuilder* mb = reinterpret_cast<MemoryBuilder*>(desc);
953 return mb->setDimensions(dims);
954 }
955
ANeuralNetworksMemoryDesc_finish(ANeuralNetworksMemoryDesc * desc)956 int ANeuralNetworksMemoryDesc_finish(ANeuralNetworksMemoryDesc* desc) {
957 NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksMemoryDesc_finish");
958 if (!desc) {
959 LOG(ERROR) << "ANeuralNetworksMemoryDesc_finish passed a nullptr";
960 return ANEURALNETWORKS_UNEXPECTED_NULL;
961 }
962 MemoryBuilder* mb = reinterpret_cast<MemoryBuilder*>(desc);
963 return mb->finish();
964 }
965
ANeuralNetworksMemory_createFromDesc(const ANeuralNetworksMemoryDesc * desc,ANeuralNetworksMemory ** memory)966 int ANeuralNetworksMemory_createFromDesc(const ANeuralNetworksMemoryDesc* desc,
967 ANeuralNetworksMemory** memory) {
968 NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksMemory_createFromDesc");
969 if (memory != nullptr) {
970 *memory = nullptr;
971 }
972 if (!desc || !memory) {
973 LOG(ERROR) << "ANeuralNetworksMemory_createFromDesc passed a nullptr";
974 return ANEURALNETWORKS_UNEXPECTED_NULL;
975 }
976 const MemoryBuilder* mb = reinterpret_cast<const MemoryBuilder*>(desc);
977 auto [n, m] = mb->allocate();
978 if (n != ANEURALNETWORKS_NO_ERROR) {
979 return n;
980 }
981 *memory = reinterpret_cast<ANeuralNetworksMemory*>(m.release());
982 return ANEURALNETWORKS_NO_ERROR;
983 }
984
ANeuralNetworksMemory_copy(const ANeuralNetworksMemory * src,const ANeuralNetworksMemory * dst)985 int ANeuralNetworksMemory_copy(const ANeuralNetworksMemory* src, const ANeuralNetworksMemory* dst) {
986 NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksMemory_copy");
987 if (!src || !dst) {
988 LOG(ERROR) << "ANeuralNetworksMemory_copy passed a nullptr";
989 return ANEURALNETWORKS_UNEXPECTED_NULL;
990 }
991 const Memory* s = reinterpret_cast<const Memory*>(src);
992 const Memory* d = reinterpret_cast<const Memory*>(dst);
993 return Memory::copy(*s, *d);
994 }
995
ANeuralNetworksMemory_createFromFd(size_t size,int prot,int fd,size_t offset,ANeuralNetworksMemory ** memory)996 int ANeuralNetworksMemory_createFromFd(size_t size, int prot, int fd, size_t offset,
997 ANeuralNetworksMemory** memory) {
998 NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksMemory_createFromFd");
999 *memory = nullptr; // WARNING: b/138965390
1000 int n = ANEURALNETWORKS_NO_ERROR;
1001 std::unique_ptr<MemoryFd> m;
1002 std::tie(n, m) = MemoryFd::create(size, prot, fd, offset);
1003 if (n != ANEURALNETWORKS_NO_ERROR) {
1004 return n;
1005 }
1006 *memory = reinterpret_cast<ANeuralNetworksMemory*>(m.release());
1007 return ANEURALNETWORKS_NO_ERROR;
1008 }
1009
ANeuralNetworksMemory_createFromAHardwareBuffer(const AHardwareBuffer * ahwb,ANeuralNetworksMemory ** memory)1010 int ANeuralNetworksMemory_createFromAHardwareBuffer(const AHardwareBuffer* ahwb,
1011 ANeuralNetworksMemory** memory) {
1012 NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksMemory_createFromAHardwareBuffer");
1013 *memory = nullptr; // WARNING: b/138965390
1014 int n = ANEURALNETWORKS_NO_ERROR;
1015 std::unique_ptr<MemoryAHWB> m;
1016 std::tie(n, m) = MemoryAHWB::create(*ahwb);
1017 if (n != ANEURALNETWORKS_NO_ERROR) {
1018 return n;
1019 }
1020 *memory = reinterpret_cast<ANeuralNetworksMemory*>(m.release());
1021 return ANEURALNETWORKS_NO_ERROR;
1022 }
1023
ANeuralNetworksMemory_free(ANeuralNetworksMemory * memory)1024 void ANeuralNetworksMemory_free(ANeuralNetworksMemory* memory) {
1025 NNTRACE_RT(NNTRACE_PHASE_TERMINATION, "ANeuralNetworksMemory_free");
1026 // No validation. Free of nullptr is valid.
1027 Memory* m = reinterpret_cast<Memory*>(memory);
1028 delete m;
1029 }
1030
ANeuralNetworksModel_create(ANeuralNetworksModel ** model)1031 int ANeuralNetworksModel_create(ANeuralNetworksModel** model) {
1032 NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_create");
1033 initVLogMask();
1034 if (!model) {
1035 LOG(ERROR) << "ANeuralNetworksModel_create passed a nullptr";
1036 return ANEURALNETWORKS_UNEXPECTED_NULL;
1037 }
1038 ModelBuilder* m = new (std::nothrow) ModelBuilder();
1039 if (m == nullptr) {
1040 *model = nullptr;
1041 return ANEURALNETWORKS_OUT_OF_MEMORY;
1042 }
1043 *model = reinterpret_cast<ANeuralNetworksModel*>(m);
1044 return ANEURALNETWORKS_NO_ERROR;
1045 }
1046
ANeuralNetworksModel_free(ANeuralNetworksModel * model)1047 void ANeuralNetworksModel_free(ANeuralNetworksModel* model) {
1048 NNTRACE_RT(NNTRACE_PHASE_TERMINATION, "ANeuralNetworksModel_free");
1049 // No validation. Free of nullptr is valid.
1050 ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1051 delete m;
1052 }
1053
ANeuralNetworksModel_finish(ANeuralNetworksModel * model)1054 int ANeuralNetworksModel_finish(ANeuralNetworksModel* model) {
1055 NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_finish");
1056 if (!model) {
1057 LOG(ERROR) << "ANeuralNetworksModel_finish passed a nullptr";
1058 return ANEURALNETWORKS_UNEXPECTED_NULL;
1059 }
1060 ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1061 return m->finish();
1062 }
1063
ANeuralNetworksModel_addOperand(ANeuralNetworksModel * model,const ANeuralNetworksOperandType * type)1064 int ANeuralNetworksModel_addOperand(ANeuralNetworksModel* model,
1065 const ANeuralNetworksOperandType* type) {
1066 NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_addOperand");
1067 if (!model || !type) {
1068 LOG(ERROR) << "ANeuralNetworksModel_addOperand passed a nullptr";
1069 return ANEURALNETWORKS_UNEXPECTED_NULL;
1070 }
1071 ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1072 return m->addOperand(*type);
1073 }
1074
ANeuralNetworksModel_setOperandValue(ANeuralNetworksModel * model,int32_t index,const void * buffer,size_t length)1075 int ANeuralNetworksModel_setOperandValue(ANeuralNetworksModel* model, int32_t index,
1076 const void* buffer, size_t length) {
1077 NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_setOperandValue");
1078 if (!model || (!buffer && length != 0)) {
1079 LOG(ERROR) << "ANeuralNetworksModel_setOperandValue passed a nullptr";
1080 return ANEURALNETWORKS_UNEXPECTED_NULL;
1081 }
1082 ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1083 return m->setOperandValue(index, buffer, length);
1084 }
1085
ANeuralNetworksModel_setOperandValueFromMemory(ANeuralNetworksModel * model,int32_t index,const ANeuralNetworksMemory * memory,size_t offset,size_t length)1086 int ANeuralNetworksModel_setOperandValueFromMemory(ANeuralNetworksModel* model, int32_t index,
1087 const ANeuralNetworksMemory* memory,
1088 size_t offset, size_t length) {
1089 NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_setOperandValueFromMemory");
1090 if (!model || !memory) {
1091 LOG(ERROR) << "ANeuralNetworksModel_setOperandValue passed a nullptr";
1092 return ANEURALNETWORKS_UNEXPECTED_NULL;
1093 }
1094 const Memory* mem = reinterpret_cast<const Memory*>(memory);
1095 ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1096 return m->setOperandValueFromMemory(index, mem, offset, length);
1097 }
1098
ANeuralNetworksModel_setOperandValueFromModel(ANeuralNetworksModel * model,int32_t index,const ANeuralNetworksModel * value)1099 int ANeuralNetworksModel_setOperandValueFromModel(ANeuralNetworksModel* model, int32_t index,
1100 const ANeuralNetworksModel* value) {
1101 NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_setOperandValueFromModel");
1102 if (!model || !value) {
1103 LOG(ERROR) << "ANeuralNetworksModel_setOperandValueFromModel passed a nullptr";
1104 return ANEURALNETWORKS_UNEXPECTED_NULL;
1105 }
1106 const ModelBuilder* val = reinterpret_cast<const ModelBuilder*>(value);
1107 ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1108 return m->setOperandValueFromModel(index, val);
1109 }
1110
ANeuralNetworksModel_addOperation(ANeuralNetworksModel * model,ANeuralNetworksOperationType type,uint32_t inputCount,const uint32_t * inputs,uint32_t outputCount,const uint32_t * outputs)1111 int ANeuralNetworksModel_addOperation(ANeuralNetworksModel* model,
1112 ANeuralNetworksOperationType type, uint32_t inputCount,
1113 const uint32_t* inputs, uint32_t outputCount,
1114 const uint32_t* outputs) {
1115 NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_addOperation");
1116 if (!model || !inputs || !outputs) {
1117 LOG(ERROR) << "ANeuralNetworksModel_addOperation passed a nullptr";
1118 return ANEURALNETWORKS_UNEXPECTED_NULL;
1119 }
1120 ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1121 return m->addOperation(type, inputCount, inputs, outputCount, outputs);
1122 }
1123
ANeuralNetworksModel_setOperandSymmPerChannelQuantParams(ANeuralNetworksModel * model,int32_t index,const ANeuralNetworksSymmPerChannelQuantParams * channelQuant)1124 int ANeuralNetworksModel_setOperandSymmPerChannelQuantParams(
1125 ANeuralNetworksModel* model, int32_t index,
1126 const ANeuralNetworksSymmPerChannelQuantParams* channelQuant) {
1127 NNTRACE_RT(NNTRACE_PHASE_PREPARATION,
1128 "ANeuralNetworksModel_setOperandSymmPerChannelQuantParams");
1129 if (!model || !channelQuant) {
1130 LOG(ERROR) << "ANeuralNetworksModel_setOperandSymmPerChannelQuantParams passed a nullptr";
1131 return ANEURALNETWORKS_UNEXPECTED_NULL;
1132 }
1133 ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1134 return m->setOperandSymmPerChannelQuantParams(index, *channelQuant);
1135 }
1136
ANeuralNetworksModel_identifyInputsAndOutputs(ANeuralNetworksModel * model,uint32_t inputCount,const uint32_t * inputs,uint32_t outputCount,const uint32_t * outputs)1137 int ANeuralNetworksModel_identifyInputsAndOutputs(ANeuralNetworksModel* model, uint32_t inputCount,
1138 const uint32_t* inputs, uint32_t outputCount,
1139 const uint32_t* outputs) {
1140 NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_identifyInputsAndOutputs");
1141 if (!model || !inputs || !outputs) {
1142 LOG(ERROR) << ("ANeuralNetworksModel_identifyInputsAndOutputs passed a nullptr");
1143 return ANEURALNETWORKS_UNEXPECTED_NULL;
1144 }
1145 ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1146 return m->identifyInputsAndOutputs(inputCount, inputs, outputCount, outputs);
1147 }
1148
ANeuralNetworksModel_relaxComputationFloat32toFloat16(ANeuralNetworksModel * model,bool allow)1149 int ANeuralNetworksModel_relaxComputationFloat32toFloat16(ANeuralNetworksModel* model, bool allow) {
1150 NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_relaxComputationFloat32toFloat16");
1151 if (!model) {
1152 LOG(ERROR) << ("ANeuralNetworksModel_relaxComputationFloat32toFloat16 passed a nullptr");
1153 return ANEURALNETWORKS_UNEXPECTED_NULL;
1154 }
1155 ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1156 return m->relaxComputationFloat32toFloat16(allow);
1157 }
1158
ANeuralNetworksCompilation_create(ANeuralNetworksModel * model,ANeuralNetworksCompilation ** compilation)1159 int ANeuralNetworksCompilation_create(ANeuralNetworksModel* model,
1160 ANeuralNetworksCompilation** compilation) {
1161 NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_create");
1162 if (!model || !compilation) {
1163 LOG(ERROR) << "ANeuralNetworksCompilation_create passed a nullptr";
1164 return ANEURALNETWORKS_UNEXPECTED_NULL;
1165 }
1166
1167 ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1168 CompilationBuilder* c = nullptr;
1169 int result = m->createCompilation(&c, DeviceManager::get()->getDrivers());
1170 *compilation = reinterpret_cast<ANeuralNetworksCompilation*>(c);
1171 return result;
1172 }
1173
ANeuralNetworksCompilation_free(ANeuralNetworksCompilation * compilation)1174 void ANeuralNetworksCompilation_free(ANeuralNetworksCompilation* compilation) {
1175 NNTRACE_RT(NNTRACE_PHASE_TERMINATION, "ANeuralNetworksCompilation_free");
1176 // No validation. Free of nullptr is valid.
1177 CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1178 delete c;
1179 }
1180
ANeuralNetworksCompilation_setPreference(ANeuralNetworksCompilation * compilation,int32_t preference)1181 int ANeuralNetworksCompilation_setPreference(ANeuralNetworksCompilation* compilation,
1182 int32_t preference) {
1183 NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_setPreference");
1184 if (!compilation) {
1185 LOG(ERROR) << "ANeuralNetworksCompilation_setPreference passed a nullptr";
1186 return ANEURALNETWORKS_UNEXPECTED_NULL;
1187 }
1188 CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1189 return c->setPreference(preference);
1190 }
1191
ANeuralNetworksCompilation_setCaching(ANeuralNetworksCompilation * compilation,const char * cacheDir,const uint8_t * token)1192 int ANeuralNetworksCompilation_setCaching(ANeuralNetworksCompilation* compilation,
1193 const char* cacheDir, const uint8_t* token) {
1194 NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_setCaching");
1195 if (!compilation || !cacheDir || !token) {
1196 LOG(ERROR) << "ANeuralNetworksCompilation_setCaching passed a nullptr";
1197 return ANEURALNETWORKS_UNEXPECTED_NULL;
1198 }
1199 CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1200 return c->setCaching(cacheDir, token);
1201 }
1202
ANeuralNetworksCompilation_finish(ANeuralNetworksCompilation * compilation)1203 int ANeuralNetworksCompilation_finish(ANeuralNetworksCompilation* compilation) {
1204 NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_finish");
1205 if (!compilation) {
1206 LOG(ERROR) << "ANeuralNetworksCompilation_finish passed a nullptr";
1207 return ANEURALNETWORKS_UNEXPECTED_NULL;
1208 }
1209 CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1210 return c->finish();
1211 }
1212
ANeuralNetworksCompilation_setPriority(ANeuralNetworksCompilation * compilation,int priority)1213 int ANeuralNetworksCompilation_setPriority(ANeuralNetworksCompilation* compilation, int priority) {
1214 NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_setPriority");
1215 if (!compilation) {
1216 LOG(ERROR) << "ANeuralNetworksCompilation_setPriority passed a nullptr";
1217 return ANEURALNETWORKS_UNEXPECTED_NULL;
1218 }
1219 CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1220 return c->setPriority(priority);
1221 }
1222
ANeuralNetworksCompilation_setTimeout(ANeuralNetworksCompilation * compilation,uint64_t duration)1223 int ANeuralNetworksCompilation_setTimeout(ANeuralNetworksCompilation* compilation,
1224 uint64_t duration) {
1225 NNTRACE_RT(NNTRACE_PHASE_COMPILATION, "ANeuralNetworksCompilation_setTimeout");
1226 if (!compilation) {
1227 LOG(ERROR) << "ANeuralNetworksCompilation_setTimeout passed a nullptr";
1228 return ANEURALNETWORKS_UNEXPECTED_NULL;
1229 }
1230 CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1231 return c->setTimeoutDuration(duration);
1232 }
1233
ANeuralNetworksExecution_create(ANeuralNetworksCompilation * compilation,ANeuralNetworksExecution ** execution)1234 int ANeuralNetworksExecution_create(ANeuralNetworksCompilation* compilation,
1235 ANeuralNetworksExecution** execution) {
1236 NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_create");
1237 if (!compilation || !execution) {
1238 LOG(ERROR) << "ANeuralNetworksExecution_create passed a nullptr";
1239 return ANEURALNETWORKS_UNEXPECTED_NULL;
1240 }
1241
1242 CompilationBuilder* c = reinterpret_cast<CompilationBuilder*>(compilation);
1243 ExecutionBuilder* r = nullptr;
1244 int result = c->createExecution(&r);
1245 *execution = reinterpret_cast<ANeuralNetworksExecution*>(r);
1246 return result;
1247 }
1248
ANeuralNetworksExecution_free(ANeuralNetworksExecution * execution)1249 void ANeuralNetworksExecution_free(ANeuralNetworksExecution* execution) {
1250 NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_free");
1251 // Free of nullptr is valid.
1252 ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1253 if (r && r->inFlight()) {
1254 LOG(ERROR) << "ANeuralNetworksExecution_free passed an in-flight ANeuralNetworksExecution"
1255 << " and is therefore ignored";
1256 return;
1257 }
1258 delete r;
1259 }
1260
ANeuralNetworksExecution_getOutputOperandRank(ANeuralNetworksExecution * execution,int32_t index,uint32_t * rank)1261 int ANeuralNetworksExecution_getOutputOperandRank(ANeuralNetworksExecution* execution,
1262 int32_t index, uint32_t* rank) {
1263 NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_getOutputOperandRank");
1264 if (!execution || !rank) {
1265 LOG(ERROR) << "ANeuralNetworksExecution_getOutputOperandRank passed a nullptr";
1266 return ANEURALNETWORKS_UNEXPECTED_NULL;
1267 }
1268 ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1269 return r->getOutputOperandRank(index, rank);
1270 }
1271
ANeuralNetworksExecution_getOutputOperandDimensions(ANeuralNetworksExecution * execution,int32_t index,uint32_t * dimensions)1272 int ANeuralNetworksExecution_getOutputOperandDimensions(ANeuralNetworksExecution* execution,
1273 int32_t index, uint32_t* dimensions) {
1274 NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_getOutputOperandDimensions");
1275 if (!execution || !dimensions) {
1276 LOG(ERROR) << "ANeuralNetworksExecution_getOutputOperandDimensions passed a nullptr";
1277 return ANEURALNETWORKS_UNEXPECTED_NULL;
1278 }
1279 ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1280 return r->getOutputOperandDimensions(index, dimensions);
1281 }
1282
ANeuralNetworksExecution_setInput(ANeuralNetworksExecution * execution,int32_t index,const ANeuralNetworksOperandType * type,const void * buffer,size_t length)1283 int ANeuralNetworksExecution_setInput(ANeuralNetworksExecution* execution, int32_t index,
1284 const ANeuralNetworksOperandType* type, const void* buffer,
1285 size_t length) {
1286 NNTRACE_RT(NNTRACE_PHASE_INPUTS_AND_OUTPUTS, "ANeuralNetworksExecution_setInput");
1287 if (!execution || (!buffer && length != 0)) {
1288 LOG(ERROR) << "ANeuralNetworksExecution_setInput passed a nullptr";
1289 return ANEURALNETWORKS_UNEXPECTED_NULL;
1290 }
1291 ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1292 return r->setInput(index, type, buffer, length);
1293 }
1294
ANeuralNetworksExecution_setInputFromMemory(ANeuralNetworksExecution * execution,int32_t index,const ANeuralNetworksOperandType * type,const ANeuralNetworksMemory * memory,size_t offset,size_t length)1295 int ANeuralNetworksExecution_setInputFromMemory(ANeuralNetworksExecution* execution, int32_t index,
1296 const ANeuralNetworksOperandType* type,
1297 const ANeuralNetworksMemory* memory, size_t offset,
1298 size_t length) {
1299 NNTRACE_RT(NNTRACE_PHASE_INPUTS_AND_OUTPUTS, "ANeuralNetworksExecution_setInputFromMemory");
1300 if (!execution || !memory) {
1301 LOG(ERROR) << "ANeuralNetworksExecution_setInputFromMemory passed a nullptr";
1302 return ANEURALNETWORKS_UNEXPECTED_NULL;
1303 }
1304
1305 const Memory* m = reinterpret_cast<const Memory*>(memory);
1306 ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1307 return r->setInputFromMemory(index, type, m, offset, length);
1308 }
1309
ANeuralNetworksExecution_setOutput(ANeuralNetworksExecution * execution,int32_t index,const ANeuralNetworksOperandType * type,void * buffer,size_t length)1310 int ANeuralNetworksExecution_setOutput(ANeuralNetworksExecution* execution, int32_t index,
1311 const ANeuralNetworksOperandType* type, void* buffer,
1312 size_t length) {
1313 NNTRACE_RT(NNTRACE_PHASE_INPUTS_AND_OUTPUTS, "ANeuralNetworksExecution_setOutput");
1314 if (!execution || (!buffer && length != 0)) {
1315 LOG(ERROR) << "ANeuralNetworksExecution_setOutput passed a nullptr";
1316 return ANEURALNETWORKS_UNEXPECTED_NULL;
1317 }
1318 ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1319 return r->setOutput(index, type, buffer, length);
1320 }
1321
ANeuralNetworksExecution_setOutputFromMemory(ANeuralNetworksExecution * execution,int32_t index,const ANeuralNetworksOperandType * type,const ANeuralNetworksMemory * memory,size_t offset,size_t length)1322 int ANeuralNetworksExecution_setOutputFromMemory(ANeuralNetworksExecution* execution, int32_t index,
1323 const ANeuralNetworksOperandType* type,
1324 const ANeuralNetworksMemory* memory, size_t offset,
1325 size_t length) {
1326 NNTRACE_RT(NNTRACE_PHASE_INPUTS_AND_OUTPUTS, "ANeuralNetworksExecution_setOutputFromMemory");
1327 if (!execution || !memory) {
1328 LOG(ERROR) << "ANeuralNetworksExecution_setOutputFromMemory passed a nullptr";
1329 return ANEURALNETWORKS_UNEXPECTED_NULL;
1330 }
1331
1332 ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1333 const Memory* m = reinterpret_cast<const Memory*>(memory);
1334 return r->setOutputFromMemory(index, type, m, offset, length);
1335 }
1336
ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution * execution,ANeuralNetworksEvent ** event)1337 int ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution* execution,
1338 ANeuralNetworksEvent** event) {
1339 NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_startCompute");
1340 if (!event) {
1341 LOG(ERROR) << "ANeuralNetworksExecution_startCompute passed a nullptr";
1342 return ANEURALNETWORKS_UNEXPECTED_NULL;
1343 }
1344 if (!execution) {
1345 LOG(ERROR) << "ANeuralNetworksExecution_startCompute passed a nullptr";
1346 *event = nullptr;
1347 return ANEURALNETWORKS_UNEXPECTED_NULL;
1348 }
1349 // TODO validate the rest
1350
1351 ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1352
1353 // Dynamically allocate an sp to wrap an ExecutionCallback, seen in the NN
1354 // API as an abstract event object. The sp<ExecutionCallback> object is
1355 // returned when the execution has been successfully launched, otherwise a
1356 // nullptr is returned. The sp is used for ref-counting purposes. Without
1357 // it, the HIDL service could attempt to communicate with a dead callback
1358 // object.
1359 std::unique_ptr<sp<ExecutionCallback>> callback = std::make_unique<sp<ExecutionCallback>>();
1360 *event = nullptr;
1361
1362 int n = r->computeAsynchronously(callback.get());
1363 if (n != ANEURALNETWORKS_NO_ERROR) {
1364 return n;
1365 }
1366 std::unique_ptr<CallbackEvent> e = std::make_unique<CallbackEvent>(*callback);
1367 *event = reinterpret_cast<ANeuralNetworksEvent*>(e.release());
1368 return ANEURALNETWORKS_NO_ERROR;
1369 }
1370
ANeuralNetworksExecution_setTimeout(ANeuralNetworksExecution * execution,uint64_t duration)1371 int ANeuralNetworksExecution_setTimeout(ANeuralNetworksExecution* execution, uint64_t duration) {
1372 NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_setTimeout");
1373 if (!execution) {
1374 LOG(ERROR) << "ANeuralNetworksExecution_setTimeout passed a nullptr";
1375 return ANEURALNETWORKS_UNEXPECTED_NULL;
1376 }
1377
1378 ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1379 return r->setTimeoutDuration(duration);
1380 }
1381
ANeuralNetworksEvent_wait(ANeuralNetworksEvent * event)1382 int ANeuralNetworksEvent_wait(ANeuralNetworksEvent* event) {
1383 NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksEvent_wait");
1384 if (event == nullptr) {
1385 LOG(ERROR) << "ANeuralNetworksEvent_wait passed a nullptr";
1386 return ANEURALNETWORKS_UNEXPECTED_NULL;
1387 }
1388
1389 IEvent* e = reinterpret_cast<IEvent*>(event);
1390 e->wait();
1391 return convertErrorStatusToResultCode(e->getStatus());
1392 }
1393
ANeuralNetworksEvent_free(ANeuralNetworksEvent * event)1394 void ANeuralNetworksEvent_free(ANeuralNetworksEvent* event) {
1395 NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksEvent_free");
1396 // No validation. Free of nullptr is valid.
1397 if (event) {
1398 IEvent* e = reinterpret_cast<IEvent*>(event);
1399 e->wait();
1400 delete e;
1401 }
1402 }
1403
ANeuralNetworksExecution_setLoopTimeout(ANeuralNetworksExecution * execution,uint64_t duration)1404 int ANeuralNetworksExecution_setLoopTimeout(ANeuralNetworksExecution* execution,
1405 uint64_t duration) {
1406 NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_setLoopTimeout");
1407 if (!execution) {
1408 LOG(ERROR) << "ANeuralNetworksExecution_setLoopTimeout passed a nullptr";
1409 return ANEURALNETWORKS_UNEXPECTED_NULL;
1410 }
1411
1412 ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1413 return r->setLoopTimeout(duration);
1414 }
1415
ANeuralNetworks_getDefaultLoopTimeout()1416 uint64_t ANeuralNetworks_getDefaultLoopTimeout() {
1417 return operation_while::kTimeoutNsDefault;
1418 }
1419
ANeuralNetworks_getMaximumLoopTimeout()1420 uint64_t ANeuralNetworks_getMaximumLoopTimeout() {
1421 return operation_while::kTimeoutNsMaximum;
1422 }
1423
ANeuralNetworksDevice_getExtensionSupport(const ANeuralNetworksDevice * device,const char * extensionName,bool * isExtensionSupported)1424 int ANeuralNetworksDevice_getExtensionSupport(const ANeuralNetworksDevice* device,
1425 const char* extensionName,
1426 bool* isExtensionSupported) {
1427 if (device == nullptr || extensionName == nullptr || isExtensionSupported == nullptr) {
1428 LOG(ERROR) << "ANeuralNetworksDevice_getExtensionSupport passed a nullptr";
1429 return ANEURALNETWORKS_UNEXPECTED_NULL;
1430 }
1431
1432 const Device* d = reinterpret_cast<const Device*>(device);
1433 const auto& supportedExtensions = d->getSupportedExtensions();
1434 *isExtensionSupported = std::any_of(supportedExtensions.begin(), supportedExtensions.end(),
1435 [extensionName](const auto& supportedExtension) {
1436 return supportedExtension.name == extensionName;
1437 });
1438
1439 return ANEURALNETWORKS_NO_ERROR;
1440 }
1441
ANeuralNetworksModel_getExtensionOperandType(ANeuralNetworksModel * model,const char * extensionName,uint16_t operandCodeWithinExtension,int32_t * type)1442 int ANeuralNetworksModel_getExtensionOperandType(ANeuralNetworksModel* model,
1443 const char* extensionName,
1444 uint16_t operandCodeWithinExtension,
1445 int32_t* type) {
1446 NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_getExtensionOperandType");
1447 if (!model || !extensionName || !type) {
1448 LOG(ERROR) << "ANeuralNetworksModel_getExtensionOperandType passed a nullptr";
1449 return ANEURALNETWORKS_UNEXPECTED_NULL;
1450 }
1451 ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1452 return m->getExtensionType(extensionName, operandCodeWithinExtension, type);
1453 }
1454
ANeuralNetworksModel_getExtensionOperationType(ANeuralNetworksModel * model,const char * extensionName,uint16_t operationCodeWithinExtension,ANeuralNetworksOperationType * type)1455 int ANeuralNetworksModel_getExtensionOperationType(ANeuralNetworksModel* model,
1456 const char* extensionName,
1457 uint16_t operationCodeWithinExtension,
1458 ANeuralNetworksOperationType* type) {
1459 NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_getExtensionOperationType");
1460 if (!model || !extensionName || !type) {
1461 LOG(ERROR) << "ANeuralNetworksModel_getExtensionOperationType passed a nullptr";
1462 return ANEURALNETWORKS_UNEXPECTED_NULL;
1463 }
1464 ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1465 return m->getExtensionType(extensionName, operationCodeWithinExtension, type);
1466 }
1467
ANeuralNetworksModel_setOperandExtensionData(ANeuralNetworksModel * model,int32_t index,const void * data,size_t length)1468 int ANeuralNetworksModel_setOperandExtensionData(ANeuralNetworksModel* model, int32_t index,
1469 const void* data, size_t length) {
1470 NNTRACE_RT(NNTRACE_PHASE_PREPARATION, "ANeuralNetworksModel_setOperandExtensionData");
1471 if (!model || (!data && length != 0)) {
1472 LOG(ERROR) << "ANeuralNetworksModel_setOperandExtensionData passed a nullptr";
1473 return ANEURALNETWORKS_UNEXPECTED_NULL;
1474 }
1475 ModelBuilder* m = reinterpret_cast<ModelBuilder*>(model);
1476 return m->setOperandExtensionData(index, data, length);
1477 }
1478
ANeuralNetworksEvent_createFromSyncFenceFd(int syncFenceFd,ANeuralNetworksEvent ** event)1479 int ANeuralNetworksEvent_createFromSyncFenceFd(int syncFenceFd, ANeuralNetworksEvent** event) {
1480 if (event == nullptr) {
1481 LOG(ERROR) << "ANeuralNetworksEvent_createFromSyncFenceFd passed a nullptr";
1482 return ANEURALNETWORKS_UNEXPECTED_NULL;
1483 }
1484 if (syncFenceFd <= 0) {
1485 LOG(ERROR) << "ANeuralNetworksEvent_createFromSyncFenceFd passed an invalid fd: "
1486 << syncFenceFd;
1487 *event = nullptr;
1488 return ANEURALNETWORKS_BAD_DATA;
1489 }
1490 std::unique_ptr<SyncFenceEvent> e = std::make_unique<SyncFenceEvent>(syncFenceFd, nullptr);
1491 *event = reinterpret_cast<ANeuralNetworksEvent*>(e.release());
1492 return ANEURALNETWORKS_NO_ERROR;
1493 }
1494
ANeuralNetworksEvent_getSyncFenceFd(const ANeuralNetworksEvent * event,int * syncFenceFd)1495 int ANeuralNetworksEvent_getSyncFenceFd(const ANeuralNetworksEvent* event, int* syncFenceFd) {
1496 if (syncFenceFd == nullptr) {
1497 LOG(ERROR) << "ANeuralNetworksEvent_getSyncFenceFd passed a nullptr";
1498 return ANEURALNETWORKS_UNEXPECTED_NULL;
1499 }
1500 *syncFenceFd = -1;
1501 if (event == nullptr) {
1502 LOG(ERROR) << "ANeuralNetworksEvent_getSyncFenceFd passed a nullptr";
1503 return ANEURALNETWORKS_UNEXPECTED_NULL;
1504 }
1505 const IEvent* e = reinterpret_cast<const IEvent*>(event);
1506 // The client owns the dupped fd, and is responsible for closing it.
1507 *syncFenceFd = e->getSyncFenceFd(/*shouldDup*/ true);
1508 if (*syncFenceFd <= 0) {
1509 LOG(ERROR) << "ANeuralNetworksEvent_getSyncFenceFd unable to get valid sync_fence fd";
1510 *syncFenceFd = -1;
1511 return ANEURALNETWORKS_OP_FAILED;
1512 }
1513 return ANEURALNETWORKS_NO_ERROR;
1514 }
1515
ANeuralNetworksExecution_startComputeWithDependencies(ANeuralNetworksExecution * execution,const ANeuralNetworksEvent * const * dependencies,uint32_t numOfDependencies,uint64_t duration,ANeuralNetworksEvent ** event)1516 int ANeuralNetworksExecution_startComputeWithDependencies(
1517 ANeuralNetworksExecution* execution, const ANeuralNetworksEvent* const* dependencies,
1518 uint32_t numOfDependencies, uint64_t duration, ANeuralNetworksEvent** event) {
1519 NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ANeuralNetworksExecution_startComputeWithDependencies");
1520 if (!event) {
1521 LOG(ERROR) << "ANeuralNetworksExecution_startComputeWithDependencies passed a nullptr";
1522 return ANEURALNETWORKS_UNEXPECTED_NULL;
1523 }
1524 if ((!dependencies && numOfDependencies != 0) || !execution) {
1525 LOG(ERROR) << "ANeuralNetworksExecution_startComputeWithDependencies passed a nullptr";
1526 *event = nullptr;
1527 return ANEURALNETWORKS_UNEXPECTED_NULL;
1528 }
1529 ExecutionBuilder* r = reinterpret_cast<ExecutionBuilder*>(execution);
1530
1531 std::vector<int> waitForList;
1532 for (uint32_t i = 0; i < numOfDependencies; i++) {
1533 if (!dependencies[i]) {
1534 LOG(ERROR) << "ANeuralNetworksExecution_startComputeWithDependencies passed a nullptr";
1535 *event = nullptr;
1536 return ANEURALNETWORKS_UNEXPECTED_NULL;
1537 }
1538 const IEvent* e = reinterpret_cast<const IEvent*>(dependencies[i]);
1539 int syncFenceFd = e->getSyncFenceFd(/*should_dup*/ false);
1540 if (syncFenceFd < 0) {
1541 e->wait();
1542 } else {
1543 waitForList.push_back(syncFenceFd);
1544 }
1545 }
1546 int syncFenceToSignal = -1;
1547 int n = r->computeFenced(waitForList, duration, &syncFenceToSignal);
1548 std::unique_ptr<SyncFenceEvent> e =
1549 std::make_unique<SyncFenceEvent>(syncFenceToSignal, r->getFencedExecutionCallback());
1550 if (n != ANEURALNETWORKS_NO_ERROR) {
1551 *event = nullptr;
1552 } else {
1553 *event = reinterpret_cast<ANeuralNetworksEvent*>(e.release());
1554 }
1555 return n;
1556 }
1557