1 /*
2  * Copyright (C) 2020 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 <android/hardware/neuralnetworks/1.0/types.h>
18 #include <nnapi/OperandTypes.h>
19 #include <nnapi/OperationTypes.h>
20 #include <nnapi/Types.h>
21 #include <type_traits>
22 
23 namespace {
24 
25 #define COMPARE_ENUMS_TYPES(lhsType, rhsType)                                                   \
26     static_assert(                                                                              \
27             std::is_same_v<                                                                     \
28                     std::underlying_type_t<::android::hardware::neuralnetworks::V1_0::lhsType>, \
29                     std::underlying_type_t<::android::nn::rhsType>>,                            \
30             "::android::hardware::neuralnetworks::V1_0::" #lhsType                              \
31             " does not have the same underlying type as ::android::nn::" #rhsType)
32 
33 COMPARE_ENUMS_TYPES(OperandType, OperandType);
34 COMPARE_ENUMS_TYPES(OperationType, OperationType);
35 COMPARE_ENUMS_TYPES(ErrorStatus, ErrorStatus);
36 COMPARE_ENUMS_TYPES(OperandLifeTime, Operand::LifeTime);
37 
38 #undef COMPARE_ENUMS_TYPES
39 
40 #define COMPARE_ENUMS_FULL(lhsSymbol, rhsSymbol, lhsType, rhsType)                               \
41     static_assert(                                                                               \
42             static_cast<                                                                         \
43                     std::underlying_type_t<::android::hardware::neuralnetworks::V1_0::lhsType>>( \
44                     ::android::hardware::neuralnetworks::V1_0::lhsType::lhsSymbol) ==            \
45                     static_cast<std::underlying_type_t<::android::nn::rhsType>>(                 \
46                             ::android::nn::rhsType::rhsSymbol),                                  \
47             "::android::hardware::neuralnetworks::V1_0::" #lhsType "::" #lhsSymbol               \
48             " does not match ::android::nn::" #rhsType "::" #rhsSymbol)
49 
50 #define COMPARE_ENUMS(symbol) COMPARE_ENUMS_FULL(symbol, symbol, OperandType, OperandType)
51 
52 COMPARE_ENUMS(FLOAT32);
53 COMPARE_ENUMS(INT32);
54 COMPARE_ENUMS(UINT32);
55 COMPARE_ENUMS(TENSOR_FLOAT32);
56 COMPARE_ENUMS(TENSOR_INT32);
57 COMPARE_ENUMS(TENSOR_QUANT8_ASYMM);
58 COMPARE_ENUMS(OEM);
59 COMPARE_ENUMS(TENSOR_OEM_BYTE);
60 
61 #undef COMPARE_ENUMS
62 
63 #define COMPARE_ENUMS(symbol) COMPARE_ENUMS_FULL(symbol, symbol, OperationType, OperationType)
64 
65 COMPARE_ENUMS(ADD);
66 COMPARE_ENUMS(AVERAGE_POOL_2D);
67 COMPARE_ENUMS(CONCATENATION);
68 COMPARE_ENUMS(CONV_2D);
69 COMPARE_ENUMS(DEPTHWISE_CONV_2D);
70 COMPARE_ENUMS(DEPTH_TO_SPACE);
71 COMPARE_ENUMS(DEQUANTIZE);
72 COMPARE_ENUMS(EMBEDDING_LOOKUP);
73 COMPARE_ENUMS(FLOOR);
74 COMPARE_ENUMS(FULLY_CONNECTED);
75 COMPARE_ENUMS(HASHTABLE_LOOKUP);
76 COMPARE_ENUMS(L2_NORMALIZATION);
77 COMPARE_ENUMS(L2_POOL_2D);
78 COMPARE_ENUMS(LOCAL_RESPONSE_NORMALIZATION);
79 COMPARE_ENUMS(LOGISTIC);
80 COMPARE_ENUMS(LSH_PROJECTION);
81 COMPARE_ENUMS(LSTM);
82 COMPARE_ENUMS(MAX_POOL_2D);
83 COMPARE_ENUMS(MUL);
84 COMPARE_ENUMS(RELU);
85 COMPARE_ENUMS(RELU1);
86 COMPARE_ENUMS(RELU6);
87 COMPARE_ENUMS(RESHAPE);
88 COMPARE_ENUMS(RESIZE_BILINEAR);
89 COMPARE_ENUMS(RNN);
90 COMPARE_ENUMS(SOFTMAX);
91 COMPARE_ENUMS(SPACE_TO_DEPTH);
92 COMPARE_ENUMS(SVDF);
93 COMPARE_ENUMS(TANH);
94 COMPARE_ENUMS(OEM_OPERATION);
95 
96 #undef COMPARE_ENUMS
97 
98 #define COMPARE_ENUMS(symbol) COMPARE_ENUMS_FULL(symbol, symbol, ErrorStatus, ErrorStatus)
99 
100 COMPARE_ENUMS(NONE);
101 COMPARE_ENUMS(DEVICE_UNAVAILABLE);
102 COMPARE_ENUMS(GENERAL_FAILURE);
103 COMPARE_ENUMS(OUTPUT_INSUFFICIENT_SIZE);
104 COMPARE_ENUMS(INVALID_ARGUMENT);
105 
106 #undef COMPARE_ENUMS
107 
108 #define COMPARE_ENUMS(lhsSymbol, rhsSymbol) \
109     COMPARE_ENUMS_FULL(lhsSymbol, rhsSymbol, OperandLifeTime, Operand::LifeTime)
110 
111 COMPARE_ENUMS(TEMPORARY_VARIABLE, TEMPORARY_VARIABLE);
112 COMPARE_ENUMS(MODEL_INPUT, SUBGRAPH_INPUT);
113 COMPARE_ENUMS(MODEL_OUTPUT, SUBGRAPH_OUTPUT);
114 COMPARE_ENUMS(CONSTANT_COPY, CONSTANT_COPY);
115 COMPARE_ENUMS(CONSTANT_REFERENCE, CONSTANT_REFERENCE);
116 COMPARE_ENUMS(NO_VALUE, NO_VALUE);
117 
118 #undef COMPARE_ENUMS
119 
120 #undef COMPARE_ENUMS_FULL
121 
122 }  // anonymous namespace
123