Gather.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #define LOG_TAG "Operations"
  17. #include "HalInterfaces.h"
  18. #include "OperationResolver.h"
  19. #include "OperationsUtils.h"
  20. #include "Tracing.h"
  21. namespace android {
  22. namespace nn {
  23. namespace gather {
  24. constexpr char kOperationName[] = "GATHER";
  25. constexpr uint32_t kNumInputs = 3;
  26. constexpr uint32_t kInputTensor = 0;
  27. constexpr uint32_t kInputAxis = 1;
  28. constexpr uint32_t kInputIndices = 2;
  29. constexpr uint32_t kNumOutputs = 1;
  30. constexpr uint32_t kOutputTensor = 0;
  31. namespace {
  32. template <typename T>
  33. inline bool eval(const T* inputData, const Shape& inputShape, int32_t axis,
  34. const int32_t* indicesData, const Shape& indicesShape, T* outputData) {
  35. const auto outerSize = getNumberOfElements(inputShape, 0, axis);
  36. const auto axisSize = getSizeOfDimension(inputShape, axis);
  37. const auto innerSize =
  38. getNumberOfElements(inputShape, axis + 1, getNumberOfDimensions(inputShape));
  39. const auto indicesCount = getNumberOfElements(indicesShape);
  40. for (uint32_t outer = 0; outer < outerSize; ++outer) {
  41. for (uint32_t outputIndex = 0; outputIndex < indicesCount; ++outputIndex) {
  42. const auto inputIndex = static_cast<uint32_t>(indicesData[outputIndex]);
  43. NN_RET_CHECK_LE(0u, inputIndex);
  44. NN_RET_CHECK_LT(inputIndex, axisSize);
  45. std::memcpy(outputData + (outer * indicesCount + outputIndex) * innerSize,
  46. inputData + (outer * axisSize + inputIndex) * innerSize,
  47. sizeof(T) * innerSize);
  48. }
  49. }
  50. return true;
  51. }
  52. } // namespace
  53. bool validate(const IOperationValidationContext* context) {
  54. NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
  55. NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
  56. OperandType inputType = context->getInputType(kInputTensor);
  57. NN_RET_CHECK(
  58. inputType == OperandType::TENSOR_FLOAT16 || inputType == OperandType::TENSOR_FLOAT32 ||
  59. inputType == OperandType::TENSOR_INT32 || inputType == OperandType::TENSOR_QUANT8_ASYMM)
  60. << "Unsupported tensor type for operation " << kOperationName;
  61. NN_RET_CHECK(validateInputTypes(context,
  62. {inputType, OperandType::INT32, OperandType::TENSOR_INT32}));
  63. NN_RET_CHECK(validateOutputTypes(context, {inputType}));
  64. return validateHalVersion(context, HalVersion::V1_2);
  65. }
  66. bool prepare(IOperationExecutionContext* context) {
  67. Shape input = context->getInputShape(kInputTensor);
  68. int32_t axis = context->getInputValue<int32_t>(kInputAxis);
  69. NN_RET_CHECK(handleNegativeAxis(input, &axis));
  70. Shape indices = context->getInputShape(kInputIndices);
  71. Shape output = context->getOutputShape(kOutputTensor);
  72. output.dimensions.clear();
  73. output.dimensions.reserve(getNumberOfDimensions(input) + getNumberOfDimensions(indices) - 1);
  74. output.dimensions.insert(output.dimensions.end(), input.dimensions.begin(),
  75. input.dimensions.begin() + axis);
  76. output.dimensions.insert(output.dimensions.end(), indices.dimensions.begin(),
  77. indices.dimensions.end());
  78. output.dimensions.insert(output.dimensions.end(), input.dimensions.begin() + axis + 1,
  79. input.dimensions.end());
  80. return context->setOutputShape(kOutputTensor, output);
  81. }
  82. bool execute(IOperationExecutionContext* context) {
  83. int32_t axis = context->getInputValue<int32_t>(kInputAxis);
  84. NN_RET_CHECK(handleNegativeAxis(context->getInputShape(kInputTensor), &axis));
  85. switch (context->getInputType(kInputTensor)) {
  86. case OperandType::TENSOR_FLOAT16:
  87. return eval(context->getInputBuffer<_Float16>(kInputTensor),
  88. context->getInputShape(kInputTensor), axis,
  89. context->getInputBuffer<int32_t>(kInputIndices),
  90. context->getInputShape(kInputIndices),
  91. context->getOutputBuffer<_Float16>(kOutputTensor));
  92. case OperandType::TENSOR_FLOAT32:
  93. return eval(context->getInputBuffer<float>(kInputTensor),
  94. context->getInputShape(kInputTensor), axis,
  95. context->getInputBuffer<int32_t>(kInputIndices),
  96. context->getInputShape(kInputIndices),
  97. context->getOutputBuffer<float>(kOutputTensor));
  98. case OperandType::TENSOR_INT32:
  99. return eval(context->getInputBuffer<int32_t>(kInputTensor),
  100. context->getInputShape(kInputTensor), axis,
  101. context->getInputBuffer<int32_t>(kInputIndices),
  102. context->getInputShape(kInputIndices),
  103. context->getOutputBuffer<int32_t>(kOutputTensor));
  104. case OperandType::TENSOR_QUANT8_ASYMM:
  105. return eval(context->getInputBuffer<uint8_t>(kInputTensor),
  106. context->getInputShape(kInputTensor), axis,
  107. context->getInputBuffer<int32_t>(kInputIndices),
  108. context->getInputShape(kInputIndices),
  109. context->getOutputBuffer<uint8_t>(kOutputTensor));
  110. default:
  111. NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
  112. }
  113. }
  114. } // namespace gather
  115. NN_REGISTER_OPERATION(GATHER, gather::kOperationName, gather::validate, gather::prepare,
  116. gather::execute);
  117. } // namespace nn
  118. } // namespace android