summaryrefslogtreecommitdiff
path: root/nn/common/operations/TopK_V2.cpp
blob: d91c8131e8bbe7b62317ad2dea5e17d6a9856fe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "Operations"

#include <algorithm>
#include <utility>
#include <vector>

#include "OperationResolver.h"
#include "OperationsUtils.h"

namespace android {
namespace nn {
namespace topk_v2 {

constexpr uint32_t kNumInputs = 2;
constexpr uint32_t kInputTensor = 0;
constexpr uint32_t kTopKScalar = 1;

constexpr uint32_t kNumOutputs = 2;
constexpr uint32_t kOutputValuesTensor = 0;
constexpr uint32_t kOutputIndicesTensor = 1;

namespace {

template <typename T>
bool evalGeneric(const T* inputData, const Shape& inputShape, const int32_t k, T* valuesData,
                 int32_t* indicesData) {
    const int rowSize = inputShape.dimensions.back();
    const int totalSize = getNumberOfElements(inputShape);
    std::vector<std::pair<T, int32_t>> values(rowSize);
    T* curOutputValue = valuesData;
    int32_t* curOutputIndex = indicesData;
    for (int rowBegin = 0; rowBegin < totalSize; rowBegin += rowSize) {
        for (int i = 0; i < rowSize; ++i) {
            values[i] = std::make_pair(inputData[rowBegin + i], i);
        }
        std::nth_element(values.begin(), values.begin() + (rowSize - k), values.end());
        std::sort(values.begin() + (rowSize - k), values.end());
        std::reverse(values.begin(), values.end());
        for (int i = 0; i < k; ++i) {
            *curOutputValue = values[i].first;
            *curOutputIndex = values[i].second;
            curOutputValue++;
            curOutputIndex++;
        }
    }
    return true;
}

template <typename T>
bool executeTyped(IOperationExecutionContext* context) {
    return evalGeneric(context->getInputBuffer<T>(kInputTensor),
                       context->getInputShape(kInputTensor),
                       context->getInputValue<int32_t>(kTopKScalar),
                       context->getOutputBuffer<T>(kOutputValuesTensor),
                       context->getOutputBuffer<int32_t>(kOutputIndicesTensor));
}

}  // namespace

bool validate(const IOperationValidationContext* context) {
    NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
    NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
    OperandType inputType = context->getInputType(kInputTensor);
    NN_RET_CHECK(inputType == OperandType::TENSOR_FLOAT16 ||
                 inputType == OperandType::TENSOR_FLOAT32 ||
                 inputType == OperandType::TENSOR_INT32 ||
                 inputType == OperandType::TENSOR_QUANT8_ASYMM ||
                 inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)
            << "Unsupported input operand type for select op: " << inputType;
    NN_RET_CHECK(validateInputTypes(context, {inputType, OperandType::INT32}));
    NN_RET_CHECK(validateOutputTypes(context, {inputType, OperandType::TENSOR_INT32}));
    Version minSupportedVersion = Version::ANDROID_Q;
    if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
        minSupportedVersion = Version::ANDROID_R;
    }
    return validateVersion(context, minSupportedVersion);
}

bool prepare(IOperationExecutionContext* context) {
    const Shape inputShape = context->getInputShape(kInputTensor);
    const int32_t k = context->getInputValue<int32_t>(kTopKScalar);
    NN_RET_CHECK_GT(k, 0);
    NN_RET_CHECK_LE(k, inputShape.dimensions.back());

    // Copy input shape to ensure that quantization parameters for the output
    // values are the same as for the input tensor.
    Shape outputValuesShape = inputShape;
    outputValuesShape.dimensions.back() = k;
    Shape outputIndicesShape;
    outputIndicesShape.type = OperandType::TENSOR_INT32;
    outputIndicesShape.dimensions = inputShape.dimensions;
    outputIndicesShape.dimensions.back() = k;
    return context->setOutputShape(kOutputValuesTensor, outputValuesShape) &&
           context->setOutputShape(kOutputIndicesTensor, outputIndicesShape);
}

bool execute(IOperationExecutionContext* context) {
    const Shape inputShape = context->getInputShape(kInputTensor);
    switch (inputShape.type) {
        case OperandType::TENSOR_FLOAT16: {
            return executeTyped<_Float16>(context);
        } break;
        case OperandType::TENSOR_FLOAT32: {
            return executeTyped<float>(context);
        } break;
        case OperandType::TENSOR_INT32: {
            return executeTyped<int32_t>(context);
        } break;
        case OperandType::TENSOR_QUANT8_ASYMM: {
            return executeTyped<uint8_t>(context);
        } break;
        case OperandType::TENSOR_QUANT8_ASYMM_SIGNED: {
            return executeTyped<int8_t>(context);
        } break;
        default: {
            LOG(ERROR) << "Unsupported data type: " << inputShape.type;
            return false;
        }
    }
}

}  // namespace topk_v2

NN_REGISTER_OPERATION(TOPK_V2, "TOPK_V2", topk_v2::validate, topk_v2::prepare, topk_v2::execute);

}  // namespace nn
}  // namespace android