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 channel_shuffle {
27
28 using namespace hal;
29
30 constexpr char kOperationName[] = "CHANNEL_SHUFFLE";
31
32 constexpr uint32_t kNumInputs = 3;
33 constexpr uint32_t kInputTensor = 0;
34 constexpr uint32_t kNumGroups = 1;
35 constexpr uint32_t kInputAxis = 2;
36
37 constexpr uint32_t kNumOutputs = 1;
38 constexpr uint32_t kOutputTensor = 0;
39
40 template <typename T>
eval(const T * inputData,const Shape & inputShape,int32_t numGroups,int32_t axis,T * outputData)41 inline bool eval(const T* inputData, const Shape& inputShape, int32_t numGroups, int32_t axis,
42 T* outputData) {
43 const uint32_t outerSize = getNumberOfElements(inputShape, 0, axis);
44 const uint32_t axisSize = getSizeOfDimension(inputShape, axis);
45 const uint32_t innerSize =
46 getNumberOfElements(inputShape, axis + 1, getNumberOfDimensions(inputShape));
47 const uint32_t groupSize = axisSize / numGroups;
48 for (uint32_t outer = 0; outer < outerSize; ++outer) {
49 for (uint32_t inner = 0; inner < innerSize; ++inner) {
50 const T* inputBase = inputData + outer * axisSize * innerSize + inner;
51 T* outputBase = outputData + outer * axisSize * innerSize + inner;
52 for (uint32_t i = 0; i < groupSize; i++) {
53 for (uint32_t j = 0; j < static_cast<uint32_t>(numGroups);
54 j++, outputBase += innerSize) {
55 *outputBase = inputBase[innerSize * (i + j * groupSize)];
56 }
57 }
58 }
59 }
60 return true;
61 }
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 auto inputType = context->getInputType(kInputTensor);
67 NN_RET_CHECK(inputType == OperandType::TENSOR_FLOAT16 ||
68 inputType == OperandType::TENSOR_FLOAT32 ||
69 inputType == OperandType::TENSOR_QUANT8_ASYMM ||
70 inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)
71 << "Unsupported tensor type for operation " << kOperationName;
72 const Shape& inputShape = context->getInputShape(kInputTensor);
73 if (hasKnownRank(inputShape)) {
74 NN_RET_CHECK_LE(getNumberOfDimensions(inputShape), 4);
75 }
76 NN_RET_CHECK(validateInputTypes(context, {inputType, OperandType::INT32, OperandType::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 numGroups = context->getInputValue<int32_t>(kNumGroups);
88 int32_t axis = context->getInputValue<int32_t>(kInputAxis);
89 NN_RET_CHECK(handleNegativeAxis(input, &axis));
90 NN_RET_CHECK(numGroups > 0);
91 NN_RET_CHECK(getSizeOfDimension(input, axis) % numGroups == 0);
92 return context->setOutputShape(kOutputTensor, input);
93 }
94
execute(IOperationExecutionContext * context)95 bool execute(IOperationExecutionContext* context) {
96 int32_t numGroups = context->getInputValue<int32_t>(kNumGroups);
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), numGroups, axis,
103 context->getOutputBuffer<_Float16>(kOutputTensor));
104 case OperandType::TENSOR_FLOAT32:
105 return eval(context->getInputBuffer<float>(kInputTensor),
106 context->getInputShape(kInputTensor), numGroups, axis,
107 context->getOutputBuffer<float>(kOutputTensor));
108 case OperandType::TENSOR_QUANT8_ASYMM:
109 return eval(context->getInputBuffer<uint8_t>(kInputTensor),
110 context->getInputShape(kInputTensor), numGroups, axis,
111 context->getOutputBuffer<uint8_t>(kOutputTensor));
112 case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
113 return eval(context->getInputBuffer<int8_t>(kInputTensor),
114 context->getInputShape(kInputTensor), numGroups, axis,
115 context->getOutputBuffer<int8_t>(kOutputTensor));
116 default:
117 NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
118 }
119 }
120
121 } // namespace channel_shuffle
122
123 NN_REGISTER_OPERATION(CHANNEL_SHUFFLE, channel_shuffle::kOperationName, channel_shuffle::validate,
124 channel_shuffle::prepare, channel_shuffle::execute);
125
126 } // namespace nn
127 } // namespace android
128