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 using namespace hal;
41 
42 template <typename T>
eval(const T * inputData,const Shape & inputShape,int32_t axis,const int32_t * indicesData,const Shape & indicesShape,T * outputData)43 inline bool eval(const T* inputData, const Shape& inputShape, int32_t axis,
44                  const int32_t* indicesData, const Shape& indicesShape, T* outputData) {
45     const auto outerSize = getNumberOfElements(inputShape, 0, axis);
46     const auto axisSize = getSizeOfDimension(inputShape, axis);
47     const auto innerSize =
48             getNumberOfElements(inputShape, axis + 1, getNumberOfDimensions(inputShape));
49     const auto indicesCount = getNumberOfElements(indicesShape);
50     for (uint32_t outer = 0; outer < outerSize; ++outer) {
51         for (uint32_t outputIndex = 0; outputIndex < indicesCount; ++outputIndex) {
52             const auto inputIndex = static_cast<uint32_t>(indicesData[outputIndex]);
53             NN_RET_CHECK_LE(0u, inputIndex);
54             NN_RET_CHECK_LT(inputIndex, axisSize);
55             std::memcpy(outputData + (outer * indicesCount + outputIndex) * innerSize,
56                         inputData + (outer * axisSize + inputIndex) * innerSize,
57                         sizeof(T) * innerSize);
58         }
59     }
60     return true;
61 }
62 
63 }  // namespace
64 
validate(const IOperationValidationContext * context)65 bool validate(const IOperationValidationContext* context) {
66     NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
67     NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
68     OperandType inputType = context->getInputType(kInputTensor);
69     NN_RET_CHECK(inputType == OperandType::TENSOR_FLOAT16 ||
70                  inputType == OperandType::TENSOR_FLOAT32 ||
71                  inputType == OperandType::TENSOR_INT32 ||
72                  inputType == OperandType::TENSOR_QUANT8_ASYMM ||
73                  inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)
74             << "Unsupported tensor type for operation " << kOperationName;
75     NN_RET_CHECK(validateInputTypes(context,
76                                     {inputType, OperandType::INT32, OperandType::TENSOR_INT32}));
77     NN_RET_CHECK(validateOutputTypes(context, {inputType}));
78     if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
79         return validateHalVersion(context, HalVersion::V1_3);
80     } else {
81         return validateHalVersion(context, HalVersion::V1_2);
82     }
83 }
84 
prepare(IOperationExecutionContext * context)85 bool prepare(IOperationExecutionContext* context) {
86     Shape input = context->getInputShape(kInputTensor);
87     int32_t axis = context->getInputValue<int32_t>(kInputAxis);
88     NN_RET_CHECK(handleNegativeAxis(input, &axis));
89     Shape indices = context->getInputShape(kInputIndices);
90     Shape output = context->getOutputShape(kOutputTensor);
91 
92     output.dimensions.clear();
93     output.dimensions.reserve(getNumberOfDimensions(input) + getNumberOfDimensions(indices) - 1);
94     output.dimensions.insert(output.dimensions.end(), input.dimensions.begin(),
95                              input.dimensions.begin() + axis);
96     output.dimensions.insert(output.dimensions.end(), indices.dimensions.begin(),
97                              indices.dimensions.end());
98     output.dimensions.insert(output.dimensions.end(), input.dimensions.begin() + axis + 1,
99                              input.dimensions.end());
100 
101     return context->setOutputShape(kOutputTensor, output);
102 }
103 
execute(IOperationExecutionContext * context)104 bool execute(IOperationExecutionContext* context) {
105     int32_t axis = context->getInputValue<int32_t>(kInputAxis);
106     NN_RET_CHECK(handleNegativeAxis(context->getInputShape(kInputTensor), &axis));
107     switch (context->getInputType(kInputTensor)) {
108         case OperandType::TENSOR_FLOAT16:
109             return eval(context->getInputBuffer<_Float16>(kInputTensor),
110                         context->getInputShape(kInputTensor), axis,
111                         context->getInputBuffer<int32_t>(kInputIndices),
112                         context->getInputShape(kInputIndices),
113                         context->getOutputBuffer<_Float16>(kOutputTensor));
114         case OperandType::TENSOR_FLOAT32:
115             return eval(context->getInputBuffer<float>(kInputTensor),
116                         context->getInputShape(kInputTensor), axis,
117                         context->getInputBuffer<int32_t>(kInputIndices),
118                         context->getInputShape(kInputIndices),
119                         context->getOutputBuffer<float>(kOutputTensor));
120         case OperandType::TENSOR_INT32:
121             return eval(context->getInputBuffer<int32_t>(kInputTensor),
122                         context->getInputShape(kInputTensor), axis,
123                         context->getInputBuffer<int32_t>(kInputIndices),
124                         context->getInputShape(kInputIndices),
125                         context->getOutputBuffer<int32_t>(kOutputTensor));
126         case OperandType::TENSOR_QUANT8_ASYMM:
127             return eval(context->getInputBuffer<uint8_t>(kInputTensor),
128                         context->getInputShape(kInputTensor), axis,
129                         context->getInputBuffer<int32_t>(kInputIndices),
130                         context->getInputShape(kInputIndices),
131                         context->getOutputBuffer<uint8_t>(kOutputTensor));
132         case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
133             return eval(context->getInputBuffer<int8_t>(kInputTensor),
134                         context->getInputShape(kInputTensor), axis,
135                         context->getInputBuffer<int32_t>(kInputIndices),
136                         context->getInputShape(kInputIndices),
137                         context->getOutputBuffer<int8_t>(kOutputTensor));
138         default:
139             NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
140     }
141 }
142 
143 }  // namespace gather
144 
145 NN_REGISTER_OPERATION(GATHER, gather::kOperationName, gather::validate, gather::prepare,
146                       gather::execute);
147 
148 }  // namespace nn
149 }  // namespace android
150