1 /*
2  * Copyright (C) 2019 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 "Transpose.h"
20 
21 #include <vector>
22 
23 #include "OperationResolver.h"
24 #include "Tracing.h"
25 
26 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
27 #pragma clang diagnostic push
28 #pragma clang diagnostic ignored "-Wunused-parameter"
29 #pragma clang diagnostic ignored "-Wsign-compare"
30 #pragma clang diagnostic ignored "-Winvalid-partial-specialization"
31 #include <tensorflow/lite/kernels/internal/optimized/legacy_optimized_ops.h>
32 #include <tensorflow/lite/kernels/internal/reference/reference_ops.h>
33 #pragma clang diagnostic pop
34 
35 #include "CpuOperationUtils.h"
36 #endif  // NN_INCLUDE_CPU_IMPLEMENTATION
37 
38 namespace android {
39 namespace nn {
40 namespace transpose {
41 
42 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
43 namespace {
44 
45 template <typename T>
transposeGeneric(const T * inputData,const Shape & inputShape,const int32_t * perm,const Shape & permShape,T * outputData,const Shape & outputShape)46 bool transposeGeneric(const T* inputData, const Shape& inputShape, const int32_t* perm,
47                       const Shape& permShape, T* outputData, const Shape& outputShape) {
48     NNTRACE_TRANS("transposeGeneric");
49     // Reverse the permuted axes and convert to 4D due to the way Dims are
50     // constructed.
51     const int32_t kOutputDimensionNum = 4;
52 
53     // permData can be NO_VALUE representing a regular 2D matrix transpose
54     int32_t permSize = perm == nullptr ? 2 : static_cast<int32_t>(getSizeOfDimension(permShape, 0));
55     int32_t perm_tmp[2] = {1, 0};
56     if (perm == nullptr) {
57         perm = perm_tmp;
58     }
59     int32_t reversed_perm[kOutputDimensionNum];
60     for (int32_t output_k = 0, input_k = permSize - 1; output_k < permSize; ++output_k, --input_k) {
61         reversed_perm[output_k] = permSize - perm[input_k] - 1;
62     }
63     for (int32_t k = permSize; k < kOutputDimensionNum; ++k) {
64         reversed_perm[k] = k;
65     }
66     NNTRACE_COMP_SWITCH("reference_ops::Transpose");
67     tflite::reference_ops::Transpose(inputData, convertShapeToDims(inputShape), outputData,
68                                      convertShapeToDims(outputShape), reversed_perm);
69     return true;
70 }
71 
72 }  // namespace
73 
prepare(IOperationExecutionContext * context)74 bool prepare(IOperationExecutionContext* context) {
75     // Only the permutation tensor can be omitted.
76     NN_RET_CHECK(!context->isOmittedInput(kInputTensor));
77     NN_RET_CHECK(!context->isOmittedOutput(kOutputTensor));
78 
79     const Shape& input = context->getInputShape(kInputTensor);
80     uint32_t numInputDims = getNumberOfDimensions(input);
81     Shape output = context->getOutputShape(kOutputTensor);
82     output.type = input.type;
83     output.offset = input.offset;
84     output.scale = input.scale;
85 
86     // permData can be NO_VALUE representing a regular 2D matrix transpose
87     if (context->isOmittedInput(kPermTensor)) {
88         NN_RET_CHECK_EQ(numInputDims, 2u);
89         output.dimensions = {getSizeOfDimension(input, 1), getSizeOfDimension(input, 0)};
90     } else {
91         const Shape& permShape = context->getInputShape(kPermTensor);
92         const int32_t* permData = context->getInputBuffer<int32_t>(kPermTensor);
93 
94         // Transpose op only supports 1D-4D input arrays.
95         NN_RET_CHECK_LE(numInputDims, 4u);
96 
97         // perm need to be provided as a 1-D int32 tensor.
98         NN_RET_CHECK(permShape.type == OperandType::TENSOR_INT32);
99         NN_RET_CHECK_EQ(getNumberOfDimensions(permShape), 1u);
100         NN_RET_CHECK_EQ(numInputDims, getSizeOfDimension(permShape, 0));
101 
102         std::vector<uint32_t> outDims(numInputDims);
103         for (int32_t idx = 0; idx < static_cast<int32_t>(numInputDims); ++idx) {
104             NN_RET_CHECK(permData[idx] >= 0 && permData[idx] < static_cast<int32_t>(numInputDims));
105             outDims[idx] = getSizeOfDimension(input, permData[idx]);
106         }
107         output.dimensions = outDims;
108     }
109     return context->setOutputShape(kOutputTensor, output);
110 }
111 
execute(IOperationExecutionContext * context)112 bool execute(IOperationExecutionContext* context) {
113     // Bypass execution in the case of zero-sized input.
114     if (getNumberOfElements(context->getOutputShape(kOutputTensor)) == 0) return true;
115 
116     switch (context->getInputType(kInputTensor)) {
117         case OperandType::TENSOR_FLOAT32:
118             return transposeGeneric(context->getInputBuffer<float>(kInputTensor),
119                                     context->getInputShape(kInputTensor),
120                                     context->getInputBuffer<int32_t>(kPermTensor),
121                                     context->getInputShape(kPermTensor),
122                                     context->getOutputBuffer<float>(kOutputTensor),
123                                     context->getOutputShape(kOutputTensor));
124         case OperandType::TENSOR_FLOAT16:
125             return transposeGeneric(context->getInputBuffer<_Float16>(kInputTensor),
126                                     context->getInputShape(kInputTensor),
127                                     context->getInputBuffer<int32_t>(kPermTensor),
128                                     context->getInputShape(kPermTensor),
129                                     context->getOutputBuffer<_Float16>(kOutputTensor),
130                                     context->getOutputShape(kOutputTensor));
131         case OperandType::TENSOR_QUANT8_ASYMM:
132             return transposeGeneric(context->getInputBuffer<uint8_t>(kInputTensor),
133                                     context->getInputShape(kInputTensor),
134                                     context->getInputBuffer<int32_t>(kPermTensor),
135                                     context->getInputShape(kPermTensor),
136                                     context->getOutputBuffer<uint8_t>(kOutputTensor),
137                                     context->getOutputShape(kOutputTensor));
138         case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
139             return transposeGeneric(context->getInputBuffer<int8_t>(kInputTensor),
140                                     context->getInputShape(kInputTensor),
141                                     context->getInputBuffer<int32_t>(kPermTensor),
142                                     context->getInputShape(kPermTensor),
143                                     context->getOutputBuffer<int8_t>(kOutputTensor),
144                                     context->getOutputShape(kOutputTensor));
145         default:
146             NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
147     }
148 }
149 #endif  // NN_INCLUDE_CPU_IMPLEMENTATION
150 
151 }  // namespace transpose
152 
153 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(TRANSPOSE, transpose::prepare, transpose::execute,
154                                          .allowOmittedOperand = true, .allowZeroSizedInput = true);
155 
156 }  // namespace nn
157 }  // namespace android
158