1 /*
2 * Copyright (C) 2021 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 "Reverse.h"
20
21 #include "OperationResolver.h"
22 #include "OperationsExecutionUtils.h"
23
24 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
25
26 #pragma clang diagnostic push
27 #pragma clang diagnostic ignored "-Wunused-parameter"
28 #pragma clang diagnostic ignored "-Wsign-compare"
29 #include <tensorflow/lite/kernels/internal/reference/reference_ops.h>
30 #pragma clang diagnostic pop
31
32 #include "CpuOperationUtils.h"
33 #endif // NN_INCLUDE_CPU_IMPLEMENTATION
34
35 namespace android {
36 namespace nn {
37 namespace reverse_op {
38
39 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
prepare(IOperationExecutionContext * context)40 bool prepare(IOperationExecutionContext* context) {
41 const Shape inputShape = context->getInputShape(kInputTensor);
42
43 // Input tensor must be of rank 1..8.
44 const auto inputTensorRank = getNumberOfDimensions(inputShape);
45 NN_RET_CHECK_GE(inputTensorRank, 1U);
46 NN_RET_CHECK_LE(inputTensorRank, 8U);
47
48 // Check the axis dimension value.
49 const Shape axisShape = context->getInputShape(kInputAxisTensor);
50 NN_RET_CHECK_EQ(getNumberOfDimensions(axisShape), 1U);
51 NN_RET_CHECK_EQ(getNumberOfElements(axisShape), 1U);
52 const int32_t axisDimension = (context->getInputBuffer<int32_t>(kInputAxisTensor))[0];
53 NN_RET_CHECK_GE(axisDimension, 0);
54 NN_RET_CHECK_LT(uint32_t(axisDimension), inputTensorRank);
55
56 Shape outputShape = context->getOutputShape(kOutputTensor);
57 NN_RET_CHECK(SetShape(inputShape, &outputShape));
58 return context->setOutputShape(kOutputTensor, outputShape);
59 }
60
61 template <typename T>
reverse(IOperationExecutionContext * context)62 bool reverse(IOperationExecutionContext* context) {
63 // Note that the NNAPI REVERSE operation requires input and output tensor to
64 // have the same dimensions.
65 const tflite::RuntimeShape tensorShape =
66 convertShapeToTflshape(context->getInputShape(kInputTensor));
67
68 tflite::reference_ops::Reverse((context->getInputBuffer<int32_t>(kInputAxisTensor))[0],
69 tensorShape, context->getInputBuffer<T>(kInputTensor),
70 tensorShape, context->getOutputBuffer<T>(kOutputTensor));
71 return true;
72 }
73
execute(IOperationExecutionContext * context)74 bool execute(IOperationExecutionContext* context) {
75 switch (context->getInputType(kInputTensor)) {
76 case OperandType::TENSOR_FLOAT16:
77 return reverse<_Float16>(context);
78 case OperandType::TENSOR_FLOAT32:
79 return reverse<float>(context);
80 case OperandType::TENSOR_QUANT8_ASYMM:
81 return reverse<uint8_t>(context);
82 case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
83 return reverse<int8_t>(context);
84 case OperandType::TENSOR_INT32:
85 return reverse<int32_t>(context);
86 default:
87 NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
88 }
89 }
90 #endif // NN_INCLUDE_CPU_IMPLEMENTATION
91
92 } // namespace reverse_op
93
94 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(REVERSE, reverse_op::prepare, reverse_op::execute);
95
96 } // namespace nn
97 } // namespace android
98