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 #ifndef ANDROID_PACKAGES_MODULES_NEURALNETWORKS_COMMON_TYPES_OPERATIONS_UTILS_H 18 #define ANDROID_PACKAGES_MODULES_NEURALNETWORKS_COMMON_TYPES_OPERATIONS_UTILS_H 19 20 #include <string> 21 #include <vector> 22 23 #include "nnapi/TypeUtils.h" 24 #include "nnapi/Types.h" 25 26 namespace android::nn { 27 28 // DEPRECATED. Use NN_RET_CHECK instead. 29 #define NN_CHECK(x) NN_RET_CHECK(x) 30 #define NN_OPS_CHECK(x) NN_RET_CHECK(x) 31 32 // DEPRECATED. Use NN_RET_CHECK_EQ instead. 33 #define NN_CHECK_EQ(x, y) NN_RET_CHECK_EQ(x, y) 34 35 #ifdef NN_EXPERIMENTAL_FEATURE 36 #define NN_FOR_EACH_EXPERIMENTAL_OPERATION_IF_ENABLED(CALL) CALL(DENSIFY) 37 #else // NN_EXPERIMENTAL_FEATURE 38 #define NN_FOR_EACH_EXPERIMENTAL_OPERATION_IF_ENABLED(CALL) 39 #endif // NN_EXPERIMENTAL_FEATURE 40 41 // TODO(b/213798075): Generate this operation list in a macro with the API generator. 42 #define NN_FOR_EACH_OPERATION(CALL) \ 43 CALL(ADD) \ 44 CALL(AVERAGE_POOL_2D) \ 45 CALL(CONCATENATION) \ 46 CALL(CONV_2D) \ 47 CALL(DEPTHWISE_CONV_2D) \ 48 CALL(DEPTH_TO_SPACE) \ 49 CALL(DEQUANTIZE) \ 50 CALL(EMBEDDING_LOOKUP) \ 51 CALL(FLOOR) \ 52 CALL(FULLY_CONNECTED) \ 53 CALL(HASHTABLE_LOOKUP) \ 54 CALL(L2_NORMALIZATION) \ 55 CALL(L2_POOL_2D) \ 56 CALL(LOCAL_RESPONSE_NORMALIZATION) \ 57 CALL(LOGISTIC) \ 58 CALL(LSH_PROJECTION) \ 59 CALL(LSTM) \ 60 CALL(MAX_POOL_2D) \ 61 CALL(MUL) \ 62 CALL(RELU) \ 63 CALL(RELU1) \ 64 CALL(RELU6) \ 65 CALL(RESHAPE) \ 66 CALL(RESIZE_BILINEAR) \ 67 CALL(RNN) \ 68 CALL(SOFTMAX) \ 69 CALL(SPACE_TO_DEPTH) \ 70 CALL(SVDF) \ 71 CALL(TANH) \ 72 CALL(BATCH_TO_SPACE_ND) \ 73 CALL(DIV) \ 74 CALL(MEAN) \ 75 CALL(PAD) \ 76 CALL(SPACE_TO_BATCH_ND) \ 77 CALL(SQUEEZE) \ 78 CALL(STRIDED_SLICE) \ 79 CALL(SUB) \ 80 CALL(TRANSPOSE) \ 81 CALL(ABS) \ 82 CALL(ARGMAX) \ 83 CALL(ARGMIN) \ 84 CALL(AXIS_ALIGNED_BBOX_TRANSFORM) \ 85 CALL(BIDIRECTIONAL_SEQUENCE_LSTM) \ 86 CALL(BIDIRECTIONAL_SEQUENCE_RNN) \ 87 CALL(BOX_WITH_NMS_LIMIT) \ 88 CALL(CAST) \ 89 CALL(CHANNEL_SHUFFLE) \ 90 CALL(DETECTION_POSTPROCESSING) \ 91 CALL(EQUAL) \ 92 CALL(EXP) \ 93 CALL(EXPAND_DIMS) \ 94 CALL(GATHER) \ 95 CALL(GENERATE_PROPOSALS) \ 96 CALL(GREATER) \ 97 CALL(GREATER_EQUAL) \ 98 CALL(GROUPED_CONV_2D) \ 99 CALL(HEATMAP_MAX_KEYPOINT) \ 100 CALL(INSTANCE_NORMALIZATION) \ 101 CALL(LESS) \ 102 CALL(LESS_EQUAL) \ 103 CALL(LOG) \ 104 CALL(LOGICAL_AND) \ 105 CALL(LOGICAL_NOT) \ 106 CALL(LOGICAL_OR) \ 107 CALL(LOG_SOFTMAX) \ 108 CALL(MAXIMUM) \ 109 CALL(MINIMUM) \ 110 CALL(NEG) \ 111 CALL(NOT_EQUAL) \ 112 CALL(PAD_V2) \ 113 CALL(POW) \ 114 CALL(PRELU) \ 115 CALL(QUANTIZE) \ 116 CALL(QUANTIZED_16BIT_LSTM) \ 117 CALL(RANDOM_MULTINOMIAL) \ 118 CALL(REDUCE_ALL) \ 119 CALL(REDUCE_ANY) \ 120 CALL(REDUCE_MAX) \ 121 CALL(REDUCE_MIN) \ 122 CALL(REDUCE_PROD) \ 123 CALL(REDUCE_SUM) \ 124 CALL(ROI_ALIGN) \ 125 CALL(ROI_POOLING) \ 126 CALL(RSQRT) \ 127 CALL(SELECT) \ 128 CALL(SIN) \ 129 CALL(SLICE) \ 130 CALL(SPLIT) \ 131 CALL(SQRT) \ 132 CALL(TILE) \ 133 CALL(TOPK_V2) \ 134 CALL(TRANSPOSE_CONV_2D) \ 135 CALL(UNIDIRECTIONAL_SEQUENCE_LSTM) \ 136 CALL(UNIDIRECTIONAL_SEQUENCE_RNN) \ 137 CALL(RESIZE_NEAREST_NEIGHBOR) \ 138 CALL(QUANTIZED_LSTM) \ 139 CALL(IF) \ 140 CALL(WHILE) \ 141 CALL(ELU) \ 142 CALL(HARD_SWISH) \ 143 CALL(FILL) \ 144 CALL(RANK) \ 145 CALL(BATCH_MATMUL) \ 146 CALL(PACK) \ 147 CALL(MIRROR_PAD) \ 148 CALL(REVERSE) \ 149 CALL(OEM_OPERATION) \ 150 NN_FOR_EACH_EXPERIMENTAL_OPERATION_IF_ENABLED(CALL) 151 152 // An 8-bit boolean type (sizeof(bool) is implementation-defined). 153 typedef uint8_t bool8; 154 155 // Stores operand type information. "Shape" is a historical name. 156 struct Shape { 157 OperandType type = OperandType::FLOAT32; 158 std::vector<uint32_t> dimensions; 159 float scale = 0.0f; 160 int32_t offset = 0; 161 Operand::ExtraParams extraParams; 162 }; 163 164 // Verifies that the two shapes are the same. 165 bool SameShape(const Shape& in1, const Shape& in2); 166 167 // Sets out to the same shape as in. 168 bool SetShape(const Shape& in, Shape* out); 169 170 // Return the total number of elements, i.e. all the dimensions multiplied 171 // together. For a scalar, returns one. 172 uint32_t getNumberOfElements(const Shape& shape); 173 uint32_t getNumberOfElements(const Shape& shape, size_t firstAxisInclusive, 174 size_t lastAxisExclusive); 175 176 uint32_t getNumberOfDimensions(const Shape& shape); 177 178 uint32_t getSizeOfDimension(const Shape& shape, uint32_t dimensionIdx); 179 180 uint32_t hasKnownRank(const Shape& shape); 181 182 } // namespace android::nn 183 184 #endif // ANDROID_PACKAGES_MODULES_NEURALNETWORKS_COMMON_TYPES_OPERATIONS_UTILS_H 185