TopK_V2.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "TopK_V2.h"
  17. #include "OperationsUtils.h"
  18. #include <algorithm>
  19. namespace android {
  20. namespace nn {
  21. namespace topk_v2 {
  22. namespace {
  23. template <typename T>
  24. bool evalGeneric(const T* inputData, const Shape& inputShape, const int32_t k, T* valuesData,
  25. const Shape& /*valuesShape*/, int32_t* indicesData,
  26. const Shape& /*indicesShape*/) {
  27. const int rowSize = inputShape.dimensions.back();
  28. const int totalSize = getNumberOfElements(inputShape);
  29. std::vector<std::pair<T, int32_t>> values(rowSize);
  30. T* curOutputValue = valuesData;
  31. int32_t* curOutputIndex = indicesData;
  32. for (int rowBegin = 0; rowBegin < totalSize; rowBegin += rowSize) {
  33. for (int i = 0; i < rowSize; ++i) {
  34. values[i] = std::make_pair(inputData[rowBegin + i], i);
  35. }
  36. std::nth_element(values.begin(), values.begin() + (rowSize - k), values.end());
  37. std::sort(values.begin() + (rowSize - k), values.end());
  38. std::reverse(values.begin(), values.end());
  39. for (int i = 0; i < k; ++i) {
  40. *curOutputValue = values[i].first;
  41. *curOutputIndex = values[i].second;
  42. curOutputValue++;
  43. curOutputIndex++;
  44. }
  45. }
  46. return true;
  47. }
  48. } // namespace
  49. bool prepare(const Shape& input, int32_t k, Shape* values, Shape* indices) {
  50. NN_CHECK(k > 0);
  51. NN_CHECK(k <= input.dimensions.back());
  52. values->dimensions = input.dimensions;
  53. values->dimensions.back() = k;
  54. indices->dimensions = input.dimensions;
  55. indices->dimensions.back() = k;
  56. return true;
  57. }
  58. bool eval(const void* inputData, const Shape& inputShape, const int32_t k, void* valuesData,
  59. const Shape& valuesShape, void* indicesData, const Shape& indicesShape) {
  60. switch (inputShape.type) {
  61. case OperandType::TENSOR_FLOAT16: {
  62. return evalGeneric(reinterpret_cast<const _Float16*>(inputData), inputShape, k,
  63. reinterpret_cast<_Float16*>(valuesData), valuesShape,
  64. reinterpret_cast<int32_t*>(indicesData), indicesShape);
  65. } break;
  66. case OperandType::TENSOR_FLOAT32: {
  67. return evalGeneric(reinterpret_cast<const float*>(inputData), inputShape, k,
  68. reinterpret_cast<float*>(valuesData), valuesShape,
  69. reinterpret_cast<int32_t*>(indicesData), indicesShape);
  70. } break;
  71. case OperandType::TENSOR_INT32: {
  72. return evalGeneric(reinterpret_cast<const int32_t*>(inputData), inputShape, k,
  73. reinterpret_cast<int32_t*>(valuesData), valuesShape,
  74. reinterpret_cast<int32_t*>(indicesData), indicesShape);
  75. } break;
  76. case OperandType::TENSOR_QUANT8_ASYMM: {
  77. return evalGeneric(reinterpret_cast<const uint8_t*>(inputData), inputShape, k,
  78. reinterpret_cast<uint8_t*>(valuesData), valuesShape,
  79. reinterpret_cast<int32_t*>(indicesData), indicesShape);
  80. } break;
  81. default: {
  82. LOG(ERROR) << "Unsupported data type: " << toString(inputShape.type);
  83. return false;
  84. }
  85. }
  86. }
  87. } // namespace topk_v2
  88. } // namespace nn
  89. } // namespace android