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