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