1 /* 2 * Copyright (C) 2021 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 #include <aidl/android/hardware/neuralnetworks/DeviceType.h> 18 #include <aidl/android/hardware/neuralnetworks/ErrorStatus.h> 19 #include <aidl/android/hardware/neuralnetworks/ExecutionPreference.h> 20 #include <aidl/android/hardware/neuralnetworks/FusedActivationFunc.h> 21 #include <aidl/android/hardware/neuralnetworks/IDevice.h> 22 #include <aidl/android/hardware/neuralnetworks/OperandLifeTime.h> 23 #include <aidl/android/hardware/neuralnetworks/OperandType.h> 24 #include <aidl/android/hardware/neuralnetworks/OperationType.h> 25 #include <aidl/android/hardware/neuralnetworks/Priority.h> 26 27 #include <ControlFlow.h> 28 #include <nnapi/OperandTypes.h> 29 #include <nnapi/OperationTypes.h> 30 #include <nnapi/Types.h> 31 #include <type_traits> 32 33 namespace { 34 35 #define COMPARE_ENUMS_TYPES(lhsType, rhsType) \ 36 static_assert( \ 37 std::is_same_v< \ 38 std::underlying_type_t<::aidl::android::hardware::neuralnetworks::lhsType>, \ 39 std::underlying_type_t<::android::nn::rhsType>>, \ 40 "::aidl::android::hardware::neuralnetworks::" #lhsType \ 41 " does not have the same underlying type as ::android::nn::" #rhsType) 42 43 COMPARE_ENUMS_TYPES(OperandType, OperandType); 44 COMPARE_ENUMS_TYPES(OperationType, OperationType); 45 COMPARE_ENUMS_TYPES(Priority, Priority); 46 COMPARE_ENUMS_TYPES(OperandLifeTime, Operand::LifeTime); 47 COMPARE_ENUMS_TYPES(ErrorStatus, ErrorStatus); 48 49 #undef COMPARE_ENUMS_TYPES 50 51 #define COMPARE_ENUMS_FULL(lhsSymbol, rhsSymbol, lhsType, rhsType) \ 52 static_assert( \ 53 static_cast< \ 54 std::underlying_type_t<::aidl::android::hardware::neuralnetworks::lhsType>>( \ 55 ::aidl::android::hardware::neuralnetworks::lhsType::lhsSymbol) == \ 56 static_cast<std::underlying_type_t<::android::nn::rhsType>>( \ 57 ::android::nn::rhsType::rhsSymbol), \ 58 "::aidl::android::hardware::neuralnetworks::" #lhsType "::" #lhsSymbol \ 59 " does not match ::android::nn::" #rhsType "::" #rhsSymbol) 60 61 #define COMPARE_ENUMS(symbol) COMPARE_ENUMS_FULL(symbol, symbol, OperandType, OperandType) 62 63 COMPARE_ENUMS(FLOAT32); 64 COMPARE_ENUMS(INT32); 65 COMPARE_ENUMS(UINT32); 66 COMPARE_ENUMS(TENSOR_FLOAT32); 67 COMPARE_ENUMS(TENSOR_INT32); 68 COMPARE_ENUMS(TENSOR_QUANT8_ASYMM); 69 COMPARE_ENUMS(BOOL); 70 COMPARE_ENUMS(TENSOR_QUANT16_SYMM); 71 COMPARE_ENUMS(TENSOR_FLOAT16); 72 COMPARE_ENUMS(TENSOR_BOOL8); 73 COMPARE_ENUMS(FLOAT16); 74 COMPARE_ENUMS(TENSOR_QUANT8_SYMM_PER_CHANNEL); 75 COMPARE_ENUMS(TENSOR_QUANT16_ASYMM); 76 COMPARE_ENUMS(TENSOR_QUANT8_SYMM); 77 COMPARE_ENUMS(TENSOR_QUANT8_ASYMM_SIGNED); 78 COMPARE_ENUMS(SUBGRAPH); 79 80 #undef COMPARE_ENUMS 81 82 #define COMPARE_ENUMS(symbol) COMPARE_ENUMS_FULL(symbol, symbol, OperationType, OperationType) 83 84 COMPARE_ENUMS(ADD); 85 COMPARE_ENUMS(AVERAGE_POOL_2D); 86 COMPARE_ENUMS(CONCATENATION); 87 COMPARE_ENUMS(CONV_2D); 88 COMPARE_ENUMS(DEPTHWISE_CONV_2D); 89 COMPARE_ENUMS(DEPTH_TO_SPACE); 90 COMPARE_ENUMS(DEQUANTIZE); 91 COMPARE_ENUMS(EMBEDDING_LOOKUP); 92 COMPARE_ENUMS(FLOOR); 93 COMPARE_ENUMS(FULLY_CONNECTED); 94 COMPARE_ENUMS(HASHTABLE_LOOKUP); 95 COMPARE_ENUMS(L2_NORMALIZATION); 96 COMPARE_ENUMS(L2_POOL_2D); 97 COMPARE_ENUMS(LOCAL_RESPONSE_NORMALIZATION); 98 COMPARE_ENUMS(LOGISTIC); 99 COMPARE_ENUMS(LSH_PROJECTION); 100 COMPARE_ENUMS(LSTM); 101 COMPARE_ENUMS(MAX_POOL_2D); 102 COMPARE_ENUMS(MUL); 103 COMPARE_ENUMS(RELU); 104 COMPARE_ENUMS(RELU1); 105 COMPARE_ENUMS(RELU6); 106 COMPARE_ENUMS(RESHAPE); 107 COMPARE_ENUMS(RESIZE_BILINEAR); 108 COMPARE_ENUMS(RNN); 109 COMPARE_ENUMS(SOFTMAX); 110 COMPARE_ENUMS(SPACE_TO_DEPTH); 111 COMPARE_ENUMS(SVDF); 112 COMPARE_ENUMS(TANH); 113 COMPARE_ENUMS(BATCH_TO_SPACE_ND); 114 COMPARE_ENUMS(DIV); 115 COMPARE_ENUMS(MEAN); 116 COMPARE_ENUMS(PAD); 117 COMPARE_ENUMS(SPACE_TO_BATCH_ND); 118 COMPARE_ENUMS(SQUEEZE); 119 COMPARE_ENUMS(STRIDED_SLICE); 120 COMPARE_ENUMS(SUB); 121 COMPARE_ENUMS(TRANSPOSE); 122 COMPARE_ENUMS(ABS); 123 COMPARE_ENUMS(ARGMAX); 124 COMPARE_ENUMS(ARGMIN); 125 COMPARE_ENUMS(AXIS_ALIGNED_BBOX_TRANSFORM); 126 COMPARE_ENUMS(BIDIRECTIONAL_SEQUENCE_LSTM); 127 COMPARE_ENUMS(BIDIRECTIONAL_SEQUENCE_RNN); 128 COMPARE_ENUMS(BOX_WITH_NMS_LIMIT); 129 COMPARE_ENUMS(CAST); 130 COMPARE_ENUMS(CHANNEL_SHUFFLE); 131 COMPARE_ENUMS(DETECTION_POSTPROCESSING); 132 COMPARE_ENUMS(EQUAL); 133 COMPARE_ENUMS(EXP); 134 COMPARE_ENUMS(EXPAND_DIMS); 135 COMPARE_ENUMS(GATHER); 136 COMPARE_ENUMS(GENERATE_PROPOSALS); 137 COMPARE_ENUMS(GREATER); 138 COMPARE_ENUMS(GREATER_EQUAL); 139 COMPARE_ENUMS(GROUPED_CONV_2D); 140 COMPARE_ENUMS(HEATMAP_MAX_KEYPOINT); 141 COMPARE_ENUMS(INSTANCE_NORMALIZATION); 142 COMPARE_ENUMS(LESS); 143 COMPARE_ENUMS(LESS_EQUAL); 144 COMPARE_ENUMS(LOG); 145 COMPARE_ENUMS(LOGICAL_AND); 146 COMPARE_ENUMS(LOGICAL_NOT); 147 COMPARE_ENUMS(LOGICAL_OR); 148 COMPARE_ENUMS(LOG_SOFTMAX); 149 COMPARE_ENUMS(MAXIMUM); 150 COMPARE_ENUMS(MINIMUM); 151 COMPARE_ENUMS(NEG); 152 COMPARE_ENUMS(NOT_EQUAL); 153 COMPARE_ENUMS(PAD_V2); 154 COMPARE_ENUMS(POW); 155 COMPARE_ENUMS(PRELU); 156 COMPARE_ENUMS(QUANTIZE); 157 COMPARE_ENUMS(QUANTIZED_16BIT_LSTM); 158 COMPARE_ENUMS(RANDOM_MULTINOMIAL); 159 COMPARE_ENUMS(REDUCE_ALL); 160 COMPARE_ENUMS(REDUCE_ANY); 161 COMPARE_ENUMS(REDUCE_MAX); 162 COMPARE_ENUMS(REDUCE_MIN); 163 COMPARE_ENUMS(REDUCE_PROD); 164 COMPARE_ENUMS(REDUCE_SUM); 165 COMPARE_ENUMS(ROI_ALIGN); 166 COMPARE_ENUMS(ROI_POOLING); 167 COMPARE_ENUMS(RSQRT); 168 COMPARE_ENUMS(SELECT); 169 COMPARE_ENUMS(SIN); 170 COMPARE_ENUMS(SLICE); 171 COMPARE_ENUMS(SPLIT); 172 COMPARE_ENUMS(SQRT); 173 COMPARE_ENUMS(TILE); 174 COMPARE_ENUMS(TOPK_V2); 175 COMPARE_ENUMS(TRANSPOSE_CONV_2D); 176 COMPARE_ENUMS(UNIDIRECTIONAL_SEQUENCE_LSTM); 177 COMPARE_ENUMS(UNIDIRECTIONAL_SEQUENCE_RNN); 178 COMPARE_ENUMS(RESIZE_NEAREST_NEIGHBOR); 179 COMPARE_ENUMS(QUANTIZED_LSTM); 180 COMPARE_ENUMS(IF); 181 COMPARE_ENUMS(WHILE); 182 COMPARE_ENUMS(ELU); 183 COMPARE_ENUMS(HARD_SWISH); 184 COMPARE_ENUMS(FILL); 185 COMPARE_ENUMS(RANK); 186 187 #undef COMPARE_ENUMS 188 189 #define COMPARE_ENUMS(symbol) COMPARE_ENUMS_FULL(symbol, symbol, Priority, Priority) 190 191 COMPARE_ENUMS(LOW); 192 COMPARE_ENUMS(MEDIUM); 193 COMPARE_ENUMS(HIGH); 194 195 #undef COMPARE_ENUMS 196 197 #define COMPARE_ENUMS(lhsSymbol, rhsSymbol) \ 198 COMPARE_ENUMS_FULL(lhsSymbol, rhsSymbol, OperandLifeTime, Operand::LifeTime) 199 200 COMPARE_ENUMS(TEMPORARY_VARIABLE, TEMPORARY_VARIABLE); 201 COMPARE_ENUMS(SUBGRAPH_INPUT, SUBGRAPH_INPUT); 202 COMPARE_ENUMS(SUBGRAPH_OUTPUT, SUBGRAPH_OUTPUT); 203 COMPARE_ENUMS(CONSTANT_COPY, CONSTANT_COPY); 204 COMPARE_ENUMS(CONSTANT_POOL, CONSTANT_REFERENCE); 205 COMPARE_ENUMS(NO_VALUE, NO_VALUE); 206 COMPARE_ENUMS(SUBGRAPH, SUBGRAPH); 207 208 #undef COMPARE_ENUMS 209 210 #define COMPARE_ENUMS(symbol) COMPARE_ENUMS_FULL(symbol, symbol, ErrorStatus, ErrorStatus) 211 212 COMPARE_ENUMS(NONE); 213 COMPARE_ENUMS(DEVICE_UNAVAILABLE); 214 COMPARE_ENUMS(GENERAL_FAILURE); 215 COMPARE_ENUMS(OUTPUT_INSUFFICIENT_SIZE); 216 COMPARE_ENUMS(INVALID_ARGUMENT); 217 COMPARE_ENUMS(MISSED_DEADLINE_TRANSIENT); 218 COMPARE_ENUMS(MISSED_DEADLINE_PERSISTENT); 219 COMPARE_ENUMS(RESOURCE_EXHAUSTED_TRANSIENT); 220 COMPARE_ENUMS(RESOURCE_EXHAUSTED_PERSISTENT); 221 222 #undef COMPARE_ENUMS 223 224 #define COMPARE_ENUMS(symbol) \ 225 COMPARE_ENUMS_FULL(symbol, symbol, ExecutionPreference, ExecutionPreference) 226 227 COMPARE_ENUMS(LOW_POWER); 228 COMPARE_ENUMS(FAST_SINGLE_ANSWER); 229 COMPARE_ENUMS(SUSTAINED_SPEED); 230 231 #undef COMPARE_ENUMS 232 233 #define COMPARE_ENUMS(symbol) COMPARE_ENUMS_FULL(symbol, symbol, DeviceType, DeviceType) 234 235 COMPARE_ENUMS(OTHER); 236 COMPARE_ENUMS(CPU); 237 COMPARE_ENUMS(GPU); 238 COMPARE_ENUMS(ACCELERATOR); 239 240 #undef COMPARE_ENUMS 241 242 #define COMPARE_ENUMS(symbol) \ 243 COMPARE_ENUMS_FULL(symbol, symbol, FusedActivationFunc, FusedActivationFunc) 244 245 COMPARE_ENUMS(NONE); 246 COMPARE_ENUMS(RELU); 247 COMPARE_ENUMS(RELU1); 248 COMPARE_ENUMS(RELU6); 249 250 #undef COMPARE_ENUMS 251 252 #undef COMPARE_ENUMS_FULL 253 254 #define COMPARE_CONSTANTS(halSymbol, canonicalSymbol) \ 255 static_assert(::aidl::android::hardware::neuralnetworks::halSymbol == \ 256 ::android::nn::canonicalSymbol); 257 258 COMPARE_CONSTANTS(IDevice::BYTE_SIZE_OF_CACHE_TOKEN, kByteSizeOfCacheToken); 259 COMPARE_CONSTANTS(IDevice::MAX_NUMBER_OF_CACHE_FILES, kMaxNumberOfCacheFiles); 260 COMPARE_CONSTANTS(IDevice::EXTENSION_TYPE_HIGH_BITS_PREFIX, kExtensionPrefixBits - 1); 261 COMPARE_CONSTANTS(IDevice::EXTENSION_TYPE_LOW_BITS_TYPE, kExtensionTypeBits); 262 COMPARE_CONSTANTS(IPreparedModel::DEFAULT_LOOP_TIMEOUT_DURATION_NS, 263 operation_while::kTimeoutNsDefault); 264 COMPARE_CONSTANTS(IPreparedModel::MAXIMUM_LOOP_TIMEOUT_DURATION_NS, 265 operation_while::kTimeoutNsMaximum); 266 267 #undef COMPARE_CONSTANTS 268 269 } // anonymous namespace 270