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 "Select.h"
20
21 #include "IndexedShapeWrapper.h"
22 #include "OperationResolver.h"
23 #include "OperationsExecutionUtils.h"
24
25 namespace android {
26 namespace nn {
27 namespace select_op {
28 namespace {
29
30 template <typename T>
compute(const bool8 * conditionData,const Shape & conditionShape,const T * aData,const Shape & aShape,const T * bData,const Shape & bShape,T * outputData,const Shape & outputShape)31 bool compute(const bool8* conditionData, const Shape& conditionShape, const T* aData,
32 const Shape& aShape, const T* bData, const Shape& bShape, T* outputData,
33 const Shape& outputShape) {
34 // The code assumes that condition has the same shape as all other tensors.
35 // This should be checked during preparation stage.
36 uint32_t size = getNumberOfElements(conditionShape);
37 for (uint32_t i = 0; i < size; ++i) {
38 T a = aData[i];
39 T b = bData[i];
40
41 if constexpr (std::is_same_v<T, uint8_t> || std::is_same_v<T, int8_t>) {
42 a = requantize<T>(a, aShape, outputShape);
43 b = requantize<T>(b, bShape, outputShape);
44 }
45 outputData[i] = conditionData[i] ? a : b;
46 }
47 return true;
48 }
49
50 template <typename T>
executeTyped(IOperationExecutionContext * context)51 bool executeTyped(IOperationExecutionContext* context) {
52 return compute<T>(
53 context->getInputBuffer<bool8>(kInputCondition),
54 context->getInputShape(kInputCondition), context->getInputBuffer<T>(kInputTensor1),
55 context->getInputShape(kInputTensor1), context->getInputBuffer<T>(kInputTensor2),
56 context->getInputShape(kInputTensor2), context->getOutputBuffer<T>(kOutputTensor),
57 context->getOutputShape(kOutputTensor));
58 }
59
60 } // namespace
61
prepare(IOperationExecutionContext * context)62 bool prepare(IOperationExecutionContext* context) {
63 Shape inputCondition = context->getInputShape(kInputCondition);
64 Shape input1 = context->getInputShape(kInputTensor1);
65 if (inputCondition.dimensions.size() != input1.dimensions.size()) {
66 LOG(ERROR) << "Condition and input tensor dimensions are not equal";
67 return false;
68 }
69 for (size_t i = 0; i < inputCondition.dimensions.size(); ++i) {
70 if (inputCondition.dimensions[i] != input1.dimensions[i]) {
71 LOG(ERROR) << "Condition and input tensor dimensions are not equal";
72 return false;
73 }
74 }
75
76 Shape input2 = context->getInputShape(kInputTensor2);
77 NN_RET_CHECK(SameShape(input1, input2));
78
79 Shape output = context->getOutputShape(kOutputTensor);
80 NN_RET_CHECK(SetShape(input1, &output));
81 return context->setOutputShape(kOutputTensor, output);
82 }
83
execute(IOperationExecutionContext * context)84 bool execute(IOperationExecutionContext* context) {
85 switch (context->getInputType(kInputTensor1)) {
86 case OperandType::TENSOR_FLOAT16:
87 return executeTyped<_Float16>(context);
88 case OperandType::TENSOR_FLOAT32:
89 return executeTyped<float>(context);
90 case OperandType::TENSOR_INT32:
91 return executeTyped<int32_t>(context);
92 case OperandType::TENSOR_QUANT8_ASYMM:
93 return executeTyped<uint8_t>(context);
94 case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
95 return executeTyped<int8_t>(context);
96 default:
97 NN_RET_CHECK_FAIL() << "Unsupported tensor type for SELECT op.";
98 }
99 }
100
101 } // namespace select_op
102
103 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(SELECT, select_op::prepare, select_op::execute);
104
105 } // namespace nn
106 } // namespace android
107