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