1 /*
2  * Copyright (C) 2022 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 #define LOG_TAG "OperationConverterResolver"
18 
19 #include "OperationConverterResolver.h"
20 
21 #include "OperationsUtils.h"
22 
23 namespace android {
24 namespace nn {
25 
26 #define NN_FORWARD_DECLARE_OPERATION_CONVERTER_REGISTRATION_FUNCTION(opType) \
27     const IOperationConverter* registerConverter_##opType();
28 
29 NN_FOR_EACH_OPERATION(NN_FORWARD_DECLARE_OPERATION_CONVERTER_REGISTRATION_FUNCTION)
30 
31 #undef NN_FORWARD_DECLARE_OPERATION_CONVERTER_REGISTRATION_FUNCTION
32 
33 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(AVERAGE_POOL_2D);
34 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(CONCATENATION);
35 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(DEPTH_TO_SPACE);
36 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(DEQUANTIZE);
37 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(EMBEDDING_LOOKUP);
38 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(FLOOR);
39 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(FULLY_CONNECTED);
40 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(HASHTABLE_LOOKUP);
41 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(L2_NORMALIZATION);
42 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(L2_POOL_2D);
43 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(LOCAL_RESPONSE_NORMALIZATION);
44 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(LSH_PROJECTION);
45 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(LSTM);
46 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(MAX_POOL_2D);
47 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(MUL);
48 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(RELU);
49 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(RELU1);
50 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(RELU6);
51 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(RESHAPE);
52 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(RESIZE_BILINEAR);
53 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(RNN);
54 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(SOFTMAX);
55 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(SPACE_TO_DEPTH);
56 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(SVDF);
57 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(TANH);
58 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(BATCH_TO_SPACE_ND);
59 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(DIV);
60 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(MEAN);
61 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(PAD);
62 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(SPACE_TO_BATCH_ND);
63 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(SQUEEZE);
64 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(STRIDED_SLICE);
65 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(SUB);
66 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(TRANSPOSE);
67 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(ABS);
68 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(ARGMAX);
69 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(ARGMIN);
70 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(AXIS_ALIGNED_BBOX_TRANSFORM);
71 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(BIDIRECTIONAL_SEQUENCE_LSTM);
72 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(BIDIRECTIONAL_SEQUENCE_RNN);
73 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(BOX_WITH_NMS_LIMIT);
74 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(CAST);
75 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(CHANNEL_SHUFFLE);
76 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(DENSIFY);
77 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(DETECTION_POSTPROCESSING);
78 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(EQUAL);
79 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(EXP);
80 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(EXPAND_DIMS);
81 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(GATHER);
82 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(GENERATE_PROPOSALS);
83 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(GREATER);
84 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(GREATER_EQUAL);
85 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(GROUPED_CONV_2D);
86 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(HEATMAP_MAX_KEYPOINT);
87 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(INSTANCE_NORMALIZATION);
88 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(LESS);
89 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(LESS_EQUAL);
90 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(LOG);
91 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(LOGICAL_AND);
92 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(LOGICAL_NOT);
93 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(LOGICAL_OR);
94 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(LOG_SOFTMAX);
95 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(MAXIMUM);
96 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(MINIMUM);
97 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(NEG);
98 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(NOT_EQUAL);
99 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(PAD_V2);
100 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(POW);
101 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(PRELU);
102 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(QUANTIZE);
103 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(QUANTIZED_16BIT_LSTM);
104 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(RANDOM_MULTINOMIAL);
105 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(REDUCE_ALL);
106 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(REDUCE_ANY);
107 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(REDUCE_MAX);
108 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(REDUCE_MIN);
109 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(REDUCE_PROD);
110 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(REDUCE_SUM);
111 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(ROI_ALIGN);
112 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(ROI_POOLING);
113 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(RSQRT);
114 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(SELECT);
115 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(SIN);
116 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(SLICE);
117 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(SPLIT);
118 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(SQRT);
119 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(TILE);
120 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(TOPK_V2);
121 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(TRANSPOSE_CONV_2D);
122 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(UNIDIRECTIONAL_SEQUENCE_LSTM);
123 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(UNIDIRECTIONAL_SEQUENCE_RNN);
124 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(RESIZE_NEAREST_NEIGHBOR);
125 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(QUANTIZED_LSTM);
126 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(IF);
127 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(WHILE);
128 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(ELU);
129 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(HARD_SWISH);
130 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(FILL);
131 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(RANK);
132 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(BATCH_MATMUL);
133 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(PACK);
134 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(MIRROR_PAD);
135 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(REVERSE);
136 NN_OPERATION_CONVERTER_NOT_IMPLEMENTED(OEM_OPERATION);
137 
OperationConverterResolver()138 OperationConverterResolver::OperationConverterResolver() {
139 #define NN_REGISTER_OPERATION_CONVERTER_TO_RESOLVER(operationType) \
140     registerOperationConverter(registerConverter_##operationType(), OperationType::operationType);
141     NN_FOR_EACH_OPERATION(NN_REGISTER_OPERATION_CONVERTER_TO_RESOLVER)
142 #undef NN_REGISTER_OPERATION_CONVERTER_TO_RESOLVER
143 }
144 
findOperationConverter(OperationType operationType) const145 const IOperationConverter* OperationConverterResolver::findOperationConverter(
146         OperationType operationType) const {
147     int32_t index = static_cast<int32_t>(operationType);
148     if (index >= 0 && index < kNumberOfOperationTypes) {
149         return mConverters[index];
150     }
151     return nullptr;
152 }
153 
registerOperationConverter(const IOperationConverter * operationConverter,OperationType operationType)154 void OperationConverterResolver::registerOperationConverter(
155         const IOperationConverter* operationConverter, OperationType operationType) {
156     if (operationConverter == nullptr) {
157         return;
158     }
159 
160     int32_t index = static_cast<int32_t>(operationType);
161     CHECK(mConverters[index] == nullptr);
162     mConverters[index] = operationConverter;
163 }
164 
165 }  // namespace nn
166 }  // namespace android