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
19 #include <algorithm>
20 #include <utility>
21 #include <vector>
22
23 #include "OperationResolver.h"
24 #include "OperationsUtils.h"
25
26 namespace android {
27 namespace nn {
28 namespace topk_v2 {
29
30 constexpr uint32_t kNumInputs = 2;
31 constexpr uint32_t kInputTensor = 0;
32 constexpr uint32_t kTopKScalar = 1;
33
34 constexpr uint32_t kNumOutputs = 2;
35 constexpr uint32_t kOutputValuesTensor = 0;
36 constexpr uint32_t kOutputIndicesTensor = 1;
37
38 namespace {
39
40 template <typename T>
evalGeneric(const T * inputData,const Shape & inputShape,const int32_t k,T * valuesData,int32_t * indicesData)41 bool evalGeneric(const T* inputData, const Shape& inputShape, const int32_t k, T* valuesData,
42 int32_t* indicesData) {
43 const int rowSize = inputShape.dimensions.back();
44 const int totalSize = getNumberOfElements(inputShape);
45 std::vector<std::pair<T, int32_t>> values(rowSize);
46 T* curOutputValue = valuesData;
47 int32_t* curOutputIndex = indicesData;
48 for (int rowBegin = 0; rowBegin < totalSize; rowBegin += rowSize) {
49 for (int i = 0; i < rowSize; ++i) {
50 values[i] = std::make_pair(inputData[rowBegin + i], i);
51 }
52 std::nth_element(values.begin(), values.begin() + (rowSize - k), values.end());
53 std::sort(values.begin() + (rowSize - k), values.end());
54 std::reverse(values.begin(), values.end());
55 for (int i = 0; i < k; ++i) {
56 *curOutputValue = values[i].first;
57 *curOutputIndex = values[i].second;
58 curOutputValue++;
59 curOutputIndex++;
60 }
61 }
62 return true;
63 }
64
65 template <typename T>
executeTyped(IOperationExecutionContext * context)66 bool executeTyped(IOperationExecutionContext* context) {
67 return evalGeneric(context->getInputBuffer<T>(kInputTensor),
68 context->getInputShape(kInputTensor),
69 context->getInputValue<int32_t>(kTopKScalar),
70 context->getOutputBuffer<T>(kOutputValuesTensor),
71 context->getOutputBuffer<int32_t>(kOutputIndicesTensor));
72 }
73
74 } // namespace
75
validate(const IOperationValidationContext * context)76 Result<Version> validate(const IOperationValidationContext* context) {
77 NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
78 NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
79 OperandType inputType = context->getInputType(kInputTensor);
80 NN_RET_CHECK(inputType == OperandType::TENSOR_FLOAT16 ||
81 inputType == OperandType::TENSOR_FLOAT32 ||
82 inputType == OperandType::TENSOR_INT32 ||
83 inputType == OperandType::TENSOR_QUANT8_ASYMM ||
84 inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)
85 << "Unsupported input operand type for select op: " << inputType;
86 NN_RET_CHECK(validateInputTypes(context, {inputType, OperandType::INT32}));
87 NN_RET_CHECK(validateOutputTypes(context, {inputType, OperandType::TENSOR_INT32}));
88 Version minSupportedVersion = Version::ANDROID_Q;
89 if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
90 minSupportedVersion = Version::ANDROID_R;
91 }
92 return minSupportedVersion;
93 }
94
prepare(IOperationExecutionContext * context)95 bool prepare(IOperationExecutionContext* context) {
96 const Shape inputShape = context->getInputShape(kInputTensor);
97 const int32_t k = context->getInputValue<int32_t>(kTopKScalar);
98 NN_RET_CHECK_GT(k, 0);
99 NN_RET_CHECK_LE(k, inputShape.dimensions.back());
100
101 // Copy input shape to ensure that quantization parameters for the output
102 // values are the same as for the input tensor.
103 Shape outputValuesShape = inputShape;
104 outputValuesShape.dimensions.back() = k;
105 Shape outputIndicesShape;
106 outputIndicesShape.type = OperandType::TENSOR_INT32;
107 outputIndicesShape.dimensions = inputShape.dimensions;
108 outputIndicesShape.dimensions.back() = k;
109 return context->setOutputShape(kOutputValuesTensor, outputValuesShape) &&
110 context->setOutputShape(kOutputIndicesTensor, outputIndicesShape);
111 }
112
execute(IOperationExecutionContext * context)113 bool execute(IOperationExecutionContext* context) {
114 const Shape inputShape = context->getInputShape(kInputTensor);
115 switch (inputShape.type) {
116 case OperandType::TENSOR_FLOAT16: {
117 return executeTyped<_Float16>(context);
118 } break;
119 case OperandType::TENSOR_FLOAT32: {
120 return executeTyped<float>(context);
121 } break;
122 case OperandType::TENSOR_INT32: {
123 return executeTyped<int32_t>(context);
124 } break;
125 case OperandType::TENSOR_QUANT8_ASYMM: {
126 return executeTyped<uint8_t>(context);
127 } break;
128 case OperandType::TENSOR_QUANT8_ASYMM_SIGNED: {
129 return executeTyped<int8_t>(context);
130 } break;
131 default: {
132 LOG(ERROR) << "Unsupported data type: " << inputShape.type;
133 return false;
134 }
135 }
136 }
137
138 } // namespace topk_v2
139
140 NN_REGISTER_OPERATION(TOPK_V2, "TOPK_V2", topk_v2::validate, topk_v2::prepare, topk_v2::execute);
141
142 } // namespace nn
143 } // namespace android
144