Slice.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "CpuOperationUtils.h"
  17. #include "IndexedShapeWrapper.h"
  18. #include "OperationResolver.h"
  19. #include <vector>
  20. namespace android {
  21. namespace nn {
  22. namespace slice {
  23. constexpr char kOperationName[] = "SLICE";
  24. constexpr uint32_t kNumInputs = 3;
  25. constexpr uint32_t kInputTensor = 0;
  26. constexpr uint32_t kBeginTensor = 1;
  27. constexpr uint32_t kSizeTensor = 2;
  28. constexpr uint32_t kNumOutputs = 1;
  29. constexpr uint32_t kOutputTensor = 0;
  30. namespace {
  31. template <typename T>
  32. void addVectors(const std::vector<T>& a, const std::vector<T>& b, std::vector<T>* res) {
  33. for (int i = 0; i < res->size(); ++i) {
  34. res->at(i) = a[i] + b[i];
  35. }
  36. }
  37. template <typename T>
  38. bool evalGeneric(const T* inputData, const Shape& inputShape, const int32_t* beginData,
  39. const Shape& beginShape, const int32_t* sizeData, const Shape& sizeShape,
  40. T* outputData, const Shape& outputShape) {
  41. const int outputSize = getNumberOfElements(outputShape);
  42. const IndexedShapeWrapper indexedOutput = IndexedShapeWrapper(outputShape);
  43. const IndexedShapeWrapper indexedInput = IndexedShapeWrapper(inputShape);
  44. std::vector<uint32_t> outputIndex(getNumberOfDimensions(outputShape), 0);
  45. std::vector<uint32_t> beginIndex(getSizeOfDimension(beginShape, 0));
  46. std::vector<uint32_t> inputIndex(getNumberOfDimensions(inputShape));
  47. for (int i = 0; i < beginIndex.size(); ++i) {
  48. beginIndex[i] = static_cast<uint32_t>(beginData[i]);
  49. }
  50. bool lastIndex = false;
  51. uint32_t outputOffset;
  52. uint32_t inputOffset;
  53. do {
  54. addVectors(outputIndex, beginIndex, &inputIndex);
  55. NN_RET_CHECK(indexedOutput.indexToFlatIndex(outputIndex, &outputOffset));
  56. NN_RET_CHECK(indexedInput.indexToFlatIndex(inputIndex, &inputOffset));
  57. outputData[outputOffset] = inputData[inputOffset];
  58. NN_RET_CHECK(indexedOutput.nextIndexInplace(&outputIndex, &lastIndex));
  59. } while (!lastIndex);
  60. return true;
  61. }
  62. } // namespace
  63. bool validate(const IOperationValidationContext* context) {
  64. NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
  65. NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
  66. const OperandType inputType = context->getInputType(kInputTensor);
  67. NN_RET_CHECK(
  68. inputType == OperandType::TENSOR_FLOAT16 || inputType == OperandType::TENSOR_FLOAT32 ||
  69. inputType == OperandType::TENSOR_INT32 || inputType == OperandType::TENSOR_QUANT8_ASYMM)
  70. << "Unsupported tensor type for operation " << kOperationName;
  71. NN_RET_CHECK(validateHalVersion(context, HalVersion::V1_2));
  72. return validateInputTypes(context,
  73. {inputType, OperandType::TENSOR_INT32, OperandType::TENSOR_INT32}) &&
  74. validateOutputTypes(context, {inputType});
  75. }
  76. bool prepare(IOperationExecutionContext* context) {
  77. const Shape& inputShape = context->getInputShape(kInputTensor);
  78. const int32_t n_dims = getNumberOfDimensions(inputShape);
  79. NN_RET_CHECK(n_dims > 0);
  80. const Shape& beginShape = context->getInputShape(kBeginTensor);
  81. NN_RET_CHECK_EQ(getNumberOfDimensions(beginShape), 1);
  82. NN_RET_CHECK_EQ(getSizeOfDimension(beginShape, 0), n_dims);
  83. const Shape& sizeShape = context->getInputShape(kSizeTensor);
  84. NN_RET_CHECK_EQ(getNumberOfDimensions(sizeShape), 1);
  85. NN_RET_CHECK_EQ(getSizeOfDimension(sizeShape, 0), n_dims);
  86. const int32_t* beginData = context->getInputBuffer<int32_t>(kBeginTensor);
  87. const int32_t* sizeData = context->getInputBuffer<int32_t>(kSizeTensor);
  88. Shape outputShape = context->getOutputShape(kOutputTensor);
  89. outputShape.dimensions.resize(n_dims);
  90. for (int i = 0; i < n_dims; ++i) {
  91. const int32_t sliceBegin = beginData[i];
  92. int32_t sliceSize = sizeData[i];
  93. if (sliceSize == -1) {
  94. sliceSize = getSizeOfDimension(inputShape, i) - sliceBegin;
  95. }
  96. NN_RET_CHECK_LE(beginData[i], getSizeOfDimension(inputShape, i));
  97. NN_RET_CHECK_GE(sliceSize, 0);
  98. NN_RET_CHECK_LE(sliceBegin + sliceSize, getSizeOfDimension(inputShape, i));
  99. outputShape.dimensions[i] = sliceSize;
  100. }
  101. return context->setOutputShape(kOutputTensor, outputShape);
  102. }
  103. bool execute(IOperationExecutionContext* context) {
  104. // Bypass execution in the case of zero-sized input.
  105. if (getNumberOfElements(context->getOutputShape(kOutputTensor)) == 0) return true;
  106. switch (context->getInputType(kInputTensor)) {
  107. case OperandType::TENSOR_FLOAT16:
  108. return evalGeneric(context->getInputBuffer<_Float16>(kInputTensor),
  109. context->getInputShape(kInputTensor),
  110. context->getInputBuffer<int32_t>(kBeginTensor),
  111. context->getInputShape(kBeginTensor),
  112. context->getInputBuffer<int32_t>(kSizeTensor),
  113. context->getInputShape(kSizeTensor),
  114. context->getOutputBuffer<_Float16>(kOutputTensor),
  115. context->getOutputShape(kOutputTensor));
  116. case OperandType::TENSOR_FLOAT32:
  117. return evalGeneric(context->getInputBuffer<float>(kInputTensor),
  118. context->getInputShape(kInputTensor),
  119. context->getInputBuffer<int32_t>(kBeginTensor),
  120. context->getInputShape(kBeginTensor),
  121. context->getInputBuffer<int32_t>(kSizeTensor),
  122. context->getInputShape(kSizeTensor),
  123. context->getOutputBuffer<float>(kOutputTensor),
  124. context->getOutputShape(kOutputTensor));
  125. case OperandType::TENSOR_INT32:
  126. return evalGeneric(context->getInputBuffer<int32_t>(kInputTensor),
  127. context->getInputShape(kInputTensor),
  128. context->getInputBuffer<int32_t>(kBeginTensor),
  129. context->getInputShape(kBeginTensor),
  130. context->getInputBuffer<int32_t>(kSizeTensor),
  131. context->getInputShape(kSizeTensor),
  132. context->getOutputBuffer<int32_t>(kOutputTensor),
  133. context->getOutputShape(kOutputTensor));
  134. case OperandType::TENSOR_QUANT8_ASYMM:
  135. return evalGeneric(context->getInputBuffer<uint8_t>(kInputTensor),
  136. context->getInputShape(kInputTensor),
  137. context->getInputBuffer<int32_t>(kBeginTensor),
  138. context->getInputShape(kBeginTensor),
  139. context->getInputBuffer<int32_t>(kSizeTensor),
  140. context->getInputShape(kSizeTensor),
  141. context->getOutputBuffer<uint8_t>(kOutputTensor),
  142. context->getOutputShape(kOutputTensor));
  143. default:
  144. NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
  145. }
  146. }
  147. } // namespace slice
  148. NN_REGISTER_OPERATION(SLICE, slice::kOperationName, slice::validate, slice::prepare, slice::execute,
  149. .allowZeroSizedInput = true);
  150. } // namespace nn
  151. } // namespace android