ArgMinMax.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // Contains the implementation of the operations.
  17. #define LOG_TAG "Operations"
  18. #include "Operations.h"
  19. #include "CpuOperationUtils.h"
  20. #include "Tracing.h"
  21. namespace android {
  22. namespace nn {
  23. template <typename In, typename Out>
  24. static void argMinMaxImpl(const In* inputData, const Shape& inputShape,
  25. int32_t axis, bool isArgMin,
  26. Out* outputData, const Shape& outputShape) {
  27. const int outerSize = getNumberOfElements(inputShape, 0, axis);
  28. const int axisSize = getSizeOfDimension(inputShape, axis);
  29. const int innerSize = getNumberOfElements(
  30. inputShape, axis + 1, getNumberOfDimensions(inputShape));
  31. for (int outer = 0; outer < outerSize; ++outer) {
  32. for (int inner = 0; inner < innerSize; ++inner) {
  33. auto minMaxValue = inputData[outer * axisSize * innerSize + inner];
  34. int minMaxIndex = 0;
  35. for (int i = 1; i < axisSize; ++i) {
  36. const auto& value =
  37. inputData[(outer * axisSize + i) * innerSize + inner];
  38. if ((isArgMin && value < minMaxValue) ||
  39. (!isArgMin && value > minMaxValue)) {
  40. minMaxValue = value;
  41. minMaxIndex = i;
  42. }
  43. }
  44. outputData[outer * innerSize + inner] = minMaxIndex;
  45. }
  46. }
  47. }
  48. bool argMinMaxGeneric(const uint8_t* inputData, const Shape& inputShape,
  49. int32 axis, bool isArgMin,
  50. uint8_t* outputData, const Shape& outputShape) {
  51. NNTRACE_TRANS("argMinMaxGeneric");
  52. NN_CHECK(handleNegativeAxis(inputShape, &axis));
  53. #define NNAPI_IMPL_ARG_MIN_MAX(operandType, dataType) \
  54. if (inputShape.type == operandType) { \
  55. NNTRACE_COMP_SWITCH("argMinMaxImpl::" #dataType); \
  56. argMinMaxImpl( \
  57. reinterpret_cast<const dataType*>(inputData), \
  58. inputShape, \
  59. axis, \
  60. isArgMin, \
  61. reinterpret_cast<int32_t*>(outputData), \
  62. outputShape); \
  63. return true; \
  64. }
  65. NNAPI_IMPL_ARG_MIN_MAX(OperandType::TENSOR_FLOAT16, _Float16);
  66. NNAPI_IMPL_ARG_MIN_MAX(OperandType::TENSOR_FLOAT32, float);
  67. NNAPI_IMPL_ARG_MIN_MAX(OperandType::TENSOR_INT32, int32_t);
  68. NNAPI_IMPL_ARG_MIN_MAX(OperandType::TENSOR_QUANT8_ASYMM, uint8_t);
  69. #undef NNAPI_IMPL_ARG_MIN_MAX
  70. LOG(ERROR) << "Unsupported data type";
  71. return false;
  72. }
  73. } // namespace nn
  74. } // namespace android