Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 "Operations" |
| 18 | |
Lev Proleev | 0648e03 | 2019-11-08 16:27:15 +0000 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <limits> |
| 21 | #include <vector> |
| 22 | |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 23 | #include "OperationResolver.h" |
| 24 | #include "OperationsUtils.h" |
| 25 | #include "Tracing.h" |
| 26 | |
Slava Shklyaev | 84829a9 | 2021-02-26 12:05:38 +0000 | [diff] [blame] | 27 | #ifdef NN_INCLUDE_CPU_IMPLEMENTATION |
Slava Shklyaev | a75fa2c | 2021-01-13 16:12:02 +0000 | [diff] [blame] | 28 | #include <tensorflow/lite/kernels/internal/reference/reference_ops.h> |
Slava Shklyaev | 84829a9 | 2021-02-26 12:05:38 +0000 | [diff] [blame] | 29 | #endif // NN_INCLUDE_CPU_IMPLEMENTATION |
Slava Shklyaev | a75fa2c | 2021-01-13 16:12:02 +0000 | [diff] [blame] | 30 | |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace nn { |
| 33 | namespace reduce { |
| 34 | |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 35 | constexpr uint32_t kNumInputs = 3; |
| 36 | constexpr uint32_t kInputTensor = 0; |
| 37 | constexpr uint32_t kInputAxes = 1; |
| 38 | constexpr uint32_t kInputKeepDims = 2; |
| 39 | |
| 40 | constexpr uint32_t kNumOutputs = 1; |
| 41 | constexpr uint32_t kOutputTensor = 0; |
| 42 | |
| 43 | // Values from |
| 44 | // https://en.wikipedia.org/wiki/Half-precision_floating-point_format#IEEE_754_half-precision_binary_floating-point_format:_binary16 |
| 45 | constexpr _Float16 kFloat16Max = 65504; |
| 46 | constexpr _Float16 kFloat16Lowest = -kFloat16Max; |
| 47 | |
Slava Shklyaev | 84829a9 | 2021-02-26 12:05:38 +0000 | [diff] [blame] | 48 | #ifdef NN_INCLUDE_CPU_IMPLEMENTATION |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 49 | namespace { |
| 50 | |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 51 | template <typename T> |
| 52 | inline bool compute(IOperationExecutionContext* context, T init, T func(T, T)) { |
| 53 | const Shape inputShape = context->getInputShape(kInputTensor); |
| 54 | const Shape axesShape = context->getInputShape(kInputAxes); |
| 55 | const Shape outputShape = context->getOutputShape(kOutputTensor); |
| 56 | const uint32_t inputRank = getNumberOfDimensions(inputShape); |
| 57 | const uint32_t numAxes = getNumberOfElements(axesShape); |
| 58 | std::vector<int> tempIndex(inputShape.dimensions.size()); |
| 59 | std::vector<int> tempAxes(numAxes); |
| 60 | return tflite::reference_ops::ReduceGeneric<T>( |
| 61 | context->getInputBuffer<T>(kInputTensor), |
| 62 | reinterpret_cast<const int32_t*>(inputShape.dimensions.data()), inputRank, |
| 63 | context->getOutputBuffer<T>(kOutputTensor), |
| 64 | reinterpret_cast<const int32_t*>(outputShape.dimensions.data()), |
| 65 | outputShape.dimensions.size(), context->getInputBuffer<int32_t>(kInputAxes), numAxes, |
| 66 | context->getInputValue<bool8>(kInputKeepDims), tempIndex.data(), tempAxes.data(), init, |
| 67 | func); |
| 68 | } |
| 69 | |
| 70 | } // namespace |
Slava Shklyaev | 84829a9 | 2021-02-26 12:05:38 +0000 | [diff] [blame] | 71 | #endif // NN_INCLUDE_CPU_IMPLEMENTATION |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 72 | |
Michael Butler | 274ff7b | 2020-11-02 23:17:11 -0800 | [diff] [blame] | 73 | Result<Version> validateProdSum(const IOperationValidationContext* context) { |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 74 | NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs); |
| 75 | NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs); |
| 76 | OperandType inputType = context->getInputType(kInputTensor); |
| 77 | NN_RET_CHECK(inputType == OperandType::TENSOR_FLOAT16 || |
| 78 | inputType == OperandType::TENSOR_FLOAT32) |
| 79 | << "Unsupported tensor type for REDUCE_PROD or REDUCE_SUM"; |
| 80 | NN_RET_CHECK( |
| 81 | validateInputTypes(context, {inputType, OperandType::TENSOR_INT32, OperandType::BOOL})); |
| 82 | NN_RET_CHECK(validateOutputTypes(context, {inputType})); |
Lev Proleev | 88a3bba | 2020-03-18 15:20:46 +0000 | [diff] [blame] | 83 | const Shape& input = context->getInputShape(kInputTensor); |
| 84 | if (hasKnownRank(input)) { |
| 85 | NN_RET_CHECK_LE(getNumberOfDimensions(input), 4); |
| 86 | } |
Michael Butler | 274ff7b | 2020-11-02 23:17:11 -0800 | [diff] [blame] | 87 | return Version::ANDROID_Q; |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Michael Butler | 274ff7b | 2020-11-02 23:17:11 -0800 | [diff] [blame] | 90 | Result<Version> validateMaxMin(const IOperationValidationContext* context) { |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 91 | NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs); |
| 92 | NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs); |
| 93 | OperandType inputType = context->getInputType(kInputTensor); |
| 94 | NN_RET_CHECK(inputType == OperandType::TENSOR_FLOAT16 || |
| 95 | inputType == OperandType::TENSOR_FLOAT32 || |
Lev Proleev | 0648e03 | 2019-11-08 16:27:15 +0000 | [diff] [blame] | 96 | inputType == OperandType::TENSOR_QUANT8_ASYMM || |
| 97 | inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 98 | << "Unsupported tensor type for REDUCE_MAX or REDUCE_MIN"; |
| 99 | NN_RET_CHECK( |
| 100 | validateInputTypes(context, {inputType, OperandType::TENSOR_INT32, OperandType::BOOL})); |
| 101 | NN_RET_CHECK(validateOutputTypes(context, {inputType})); |
Michael Butler | 25d5073 | 2020-11-01 23:47:40 -0800 | [diff] [blame] | 102 | auto minVersion = Version::ANDROID_Q; |
Lev Proleev | 0648e03 | 2019-11-08 16:27:15 +0000 | [diff] [blame] | 103 | if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) { |
Michael Butler | 25d5073 | 2020-11-01 23:47:40 -0800 | [diff] [blame] | 104 | minVersion = Version::ANDROID_R; |
Lev Proleev | 0648e03 | 2019-11-08 16:27:15 +0000 | [diff] [blame] | 105 | } |
Lev Proleev | 88a3bba | 2020-03-18 15:20:46 +0000 | [diff] [blame] | 106 | const Shape& input = context->getInputShape(kInputTensor); |
| 107 | if (hasKnownRank(input)) { |
| 108 | NN_RET_CHECK_LE(getNumberOfDimensions(input), 4); |
| 109 | } |
Michael Butler | 274ff7b | 2020-11-02 23:17:11 -0800 | [diff] [blame] | 110 | return minVersion; |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Michael Butler | 274ff7b | 2020-11-02 23:17:11 -0800 | [diff] [blame] | 113 | Result<Version> validateLogical(const IOperationValidationContext* context) { |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 114 | NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs); |
| 115 | NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs); |
| 116 | OperandType inputType = context->getInputType(kInputTensor); |
| 117 | NN_RET_CHECK(inputType == OperandType::TENSOR_BOOL8) |
| 118 | << "Unsupported tensor type for REDUCE_ANY or REDUCE_ALL"; |
| 119 | NN_RET_CHECK( |
| 120 | validateInputTypes(context, {inputType, OperandType::TENSOR_INT32, OperandType::BOOL})); |
| 121 | NN_RET_CHECK(validateOutputTypes(context, {inputType})); |
Lev Proleev | 88a3bba | 2020-03-18 15:20:46 +0000 | [diff] [blame] | 122 | const Shape& input = context->getInputShape(kInputTensor); |
| 123 | if (hasKnownRank(input)) { |
| 124 | NN_RET_CHECK_LE(getNumberOfDimensions(input), 4); |
| 125 | } |
Michael Butler | 274ff7b | 2020-11-02 23:17:11 -0800 | [diff] [blame] | 126 | return Version::ANDROID_Q; |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Slava Shklyaev | 84829a9 | 2021-02-26 12:05:38 +0000 | [diff] [blame] | 129 | #ifdef NN_INCLUDE_CPU_IMPLEMENTATION |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 130 | bool prepare(IOperationExecutionContext* context) { |
| 131 | Shape inputShape = context->getInputShape(kInputTensor); |
| 132 | const uint32_t inputRank = getNumberOfDimensions(inputShape); |
Lev Proleev | 88a3bba | 2020-03-18 15:20:46 +0000 | [diff] [blame] | 133 | NN_RET_CHECK_LE(inputRank, 4); |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 134 | |
| 135 | std::vector<bool> shouldReduce(inputRank); |
| 136 | const int32_t* axes = context->getInputBuffer<int32_t>(kInputAxes); |
| 137 | Shape axesShape = context->getInputShape(kInputAxes); |
| 138 | NN_RET_CHECK_EQ(getNumberOfDimensions(axesShape), 1u); |
| 139 | const uint32_t numAxes = getNumberOfElements(axesShape); |
| 140 | for (uint32_t i = 0; i < numAxes; ++i) { |
| 141 | int32_t axis = axes[i]; |
| 142 | NN_RET_CHECK(handleNegativeAxis(inputRank, &axis)); |
| 143 | shouldReduce[axis] = true; |
| 144 | } |
| 145 | |
| 146 | // Input and output must have the same quantization parameters, etc. |
| 147 | Shape outputShape = inputShape; |
| 148 | outputShape.dimensions.clear(); |
| 149 | bool keepDims = context->getInputValue<bool8>(kInputKeepDims); |
| 150 | for (uint32_t axis = 0; axis < inputRank; ++axis) { |
| 151 | if (shouldReduce[axis]) { |
| 152 | if (keepDims) { |
| 153 | outputShape.dimensions.push_back(1); |
| 154 | } |
| 155 | } else { |
| 156 | outputShape.dimensions.push_back(getSizeOfDimension(inputShape, axis)); |
| 157 | } |
| 158 | } |
| 159 | |
Lev Proleev | 8a78472 | 2020-05-05 17:26:26 +0100 | [diff] [blame] | 160 | // Handle the case when all dimensions are removed |
| 161 | if (outputShape.dimensions.empty()) { |
| 162 | outputShape.dimensions.push_back(1); |
| 163 | } |
| 164 | |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 165 | return context->setOutputShape(kOutputTensor, outputShape); |
| 166 | } |
| 167 | |
| 168 | bool executeProd(IOperationExecutionContext* context) { |
| 169 | switch (context->getInputType(kInputTensor)) { |
| 170 | case OperandType::TENSOR_FLOAT16: |
Xusong Wang | 3b0c7fb | 2020-03-11 17:53:08 -0700 | [diff] [blame] | 171 | return compute<_Float16>(context, 1, [](_Float16 a, _Float16 b) -> _Float16 { |
| 172 | // Handle the zero case because 0 * inf evaluates to nan. |
| 173 | if (a == 0 || b == 0) return 0; |
| 174 | return a * b; |
| 175 | }); |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 176 | case OperandType::TENSOR_FLOAT32: |
Xusong Wang | 3b0c7fb | 2020-03-11 17:53:08 -0700 | [diff] [blame] | 177 | return compute<float>(context, 1, [](float a, float b) -> float { |
| 178 | // Handle the zero case because 0 * inf evaluates to nan. |
| 179 | if (a == 0 || b == 0) return 0; |
| 180 | return a * b; |
| 181 | }); |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 182 | default: |
| 183 | NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation REDUCE_PROD"; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | bool executeSum(IOperationExecutionContext* context) { |
| 188 | switch (context->getInputType(kInputTensor)) { |
| 189 | case OperandType::TENSOR_FLOAT16: |
| 190 | return compute<_Float16>(context, 0, [](_Float16 a, _Float16 b) { return a + b; }); |
| 191 | case OperandType::TENSOR_FLOAT32: |
| 192 | return compute<float>(context, 0, [](float a, float b) { return a + b; }); |
| 193 | default: |
| 194 | NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation REDUCE_SUM"; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | bool executeMax(IOperationExecutionContext* context) { |
| 199 | switch (context->getInputType(kInputTensor)) { |
| 200 | case OperandType::TENSOR_FLOAT16: |
| 201 | return compute<_Float16>(context, kFloat16Lowest, |
| 202 | [](_Float16 a, _Float16 b) { return std::max(a, b); }); |
| 203 | case OperandType::TENSOR_FLOAT32: |
| 204 | return compute<float>(context, std::numeric_limits<float>::lowest(), |
| 205 | [](float a, float b) { return std::max(a, b); }); |
| 206 | case OperandType::TENSOR_QUANT8_ASYMM: |
| 207 | return compute<uint8_t>(context, std::numeric_limits<uint8_t>::lowest(), |
| 208 | [](uint8_t a, uint8_t b) { return std::max(a, b); }); |
Lev Proleev | 0648e03 | 2019-11-08 16:27:15 +0000 | [diff] [blame] | 209 | case OperandType::TENSOR_QUANT8_ASYMM_SIGNED: |
| 210 | return compute<int8_t>(context, std::numeric_limits<int8_t>::lowest(), |
| 211 | [](int8_t a, int8_t b) { return std::max(a, b); }); |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 212 | default: |
| 213 | NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation REDUCE_MAX"; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | bool executeMin(IOperationExecutionContext* context) { |
| 218 | switch (context->getInputType(kInputTensor)) { |
| 219 | case OperandType::TENSOR_FLOAT16: |
| 220 | return compute<_Float16>(context, kFloat16Max, |
| 221 | [](_Float16 a, _Float16 b) { return std::min(a, b); }); |
| 222 | case OperandType::TENSOR_FLOAT32: |
| 223 | return compute<float>(context, std::numeric_limits<float>::max(), |
| 224 | [](float a, float b) { return std::min(a, b); }); |
| 225 | case OperandType::TENSOR_QUANT8_ASYMM: |
| 226 | return compute<uint8_t>(context, std::numeric_limits<uint8_t>::max(), |
| 227 | [](uint8_t a, uint8_t b) { return std::min(a, b); }); |
Lev Proleev | 0648e03 | 2019-11-08 16:27:15 +0000 | [diff] [blame] | 228 | case OperandType::TENSOR_QUANT8_ASYMM_SIGNED: |
| 229 | return compute<int8_t>(context, std::numeric_limits<int8_t>::max(), |
| 230 | [](int8_t a, int8_t b) { return std::min(a, b); }); |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 231 | default: |
| 232 | NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation REDUCE_MIN"; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | bool executeAny(IOperationExecutionContext* context) { |
| 237 | switch (context->getInputType(kInputTensor)) { |
| 238 | case OperandType::TENSOR_BOOL8: |
| 239 | return compute<bool8>(context, false, |
| 240 | [](bool8 a, bool8 b) { return static_cast<bool8>(a || b); }); |
| 241 | default: |
| 242 | NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation REDUCE_ANY"; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | bool executeAll(IOperationExecutionContext* context) { |
| 247 | switch (context->getInputType(kInputTensor)) { |
| 248 | case OperandType::TENSOR_BOOL8: |
| 249 | return compute<bool8>(context, true, |
| 250 | [](bool8 a, bool8 b) { return static_cast<bool8>(a && b); }); |
| 251 | default: |
| 252 | NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation REDUCE_ALL"; |
| 253 | } |
| 254 | } |
Slava Shklyaev | 84829a9 | 2021-02-26 12:05:38 +0000 | [diff] [blame] | 255 | #endif // NN_INCLUDE_CPU_IMPLEMENTATION |
Slava Shklyaev | 145436e | 2018-11-30 22:50:15 +0000 | [diff] [blame] | 256 | |
| 257 | } // namespace reduce |
| 258 | |
| 259 | NN_REGISTER_OPERATION(REDUCE_PROD, "REDUCE_PROD", reduce::validateProdSum, reduce::prepare, |
| 260 | reduce::executeProd); |
| 261 | NN_REGISTER_OPERATION(REDUCE_SUM, "REDUCE_SUM", reduce::validateProdSum, reduce::prepare, |
| 262 | reduce::executeSum); |
| 263 | NN_REGISTER_OPERATION(REDUCE_MAX, "REDUCE_MAX", reduce::validateMaxMin, reduce::prepare, |
| 264 | reduce::executeMax); |
| 265 | NN_REGISTER_OPERATION(REDUCE_MIN, "REDUCE_MIN", reduce::validateMaxMin, reduce::prepare, |
| 266 | reduce::executeMin); |
| 267 | NN_REGISTER_OPERATION(REDUCE_ANY, "REDUCE_ANY", reduce::validateLogical, reduce::prepare, |
| 268 | reduce::executeAny); |
| 269 | NN_REGISTER_OPERATION(REDUCE_ALL, "REDUCE_ALL", reduce::validateLogical, reduce::prepare, |
| 270 | reduce::executeAll); |
| 271 | |
| 272 | } // namespace nn |
| 273 | } // namespace android |