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