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 <functional>
20 #include <vector>
21
22 #include "HalInterfaces.h"
23 #include "IndexedShapeWrapper.h"
24 #include "OperationResolver.h"
25 #include "OperationsUtils.h"
26
27 namespace android {
28 namespace nn {
29 namespace comparisons {
30
31 constexpr uint32_t kNumInputs = 2;
32 constexpr uint32_t kInputTensor1 = 0;
33 constexpr uint32_t kInputTensor2 = 1;
34
35 constexpr uint32_t kNumOutputs = 1;
36 constexpr uint32_t kOutputTensor = 0;
37
38 namespace {
39
40 using namespace hal;
41
42 template <typename DataType, typename ComparisonType>
compute(const std::function<bool (ComparisonType,ComparisonType)> & func,const DataType * aData,const Shape & aShape,const DataType * bData,const Shape & bShape,bool8 * outputData,const Shape & outputShape)43 bool compute(const std::function<bool(ComparisonType, ComparisonType)>& func, const DataType* aData,
44 const Shape& aShape, const DataType* bData, const Shape& bShape, bool8* outputData,
45 const Shape& outputShape) {
46 IndexedShapeWrapper aShapeIndexed(aShape);
47 IndexedShapeWrapper bShapeIndexed(bShape);
48 IndexedShapeWrapper outputShapeIndexed(outputShape);
49 std::vector<uint32_t> curIndex(outputShape.dimensions.size(), 0);
50 bool lastIndex = false;
51 do {
52 uint32_t outputFlatIndex;
53 NN_RET_CHECK(outputShapeIndexed.indexToFlatIndex(curIndex, &outputFlatIndex));
54 uint32_t aFlatIndex;
55 NN_RET_CHECK(aShapeIndexed.broadcastedIndexToFlatIndex(curIndex, &aFlatIndex));
56 uint32_t bFlatIndex;
57 NN_RET_CHECK(bShapeIndexed.broadcastedIndexToFlatIndex(curIndex, &bFlatIndex));
58
59 if (aShape.type == OperandType::TENSOR_QUANT8_ASYMM ||
60 aShape.type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
61 const float realA = (aData[aFlatIndex] - aShape.offset) * aShape.scale;
62 const float realB = (bData[bFlatIndex] - bShape.offset) * bShape.scale;
63 outputData[outputFlatIndex] = func(realA, realB);
64 } else {
65 outputData[outputFlatIndex] = func(aData[aFlatIndex], bData[bFlatIndex]);
66 }
67
68 NN_RET_CHECK(outputShapeIndexed.nextIndexInplace(&curIndex, &lastIndex));
69 } while (!lastIndex);
70 return true;
71 }
72
73 template <typename DataType, typename ComparisonType>
executeLessTyped(IOperationExecutionContext * context)74 bool executeLessTyped(IOperationExecutionContext* context) {
75 return compute<DataType, ComparisonType>(
76 std::less<ComparisonType>(), context->getInputBuffer<DataType>(kInputTensor1),
77 context->getInputShape(kInputTensor1), context->getInputBuffer<DataType>(kInputTensor2),
78 context->getInputShape(kInputTensor2), context->getOutputBuffer<bool8>(kOutputTensor),
79 context->getOutputShape(kOutputTensor));
80 }
81
82 template <typename DataType, typename ComparisonType>
executeLessEqualTyped(IOperationExecutionContext * context)83 bool executeLessEqualTyped(IOperationExecutionContext* context) {
84 return compute<DataType, ComparisonType>(
85 std::less_equal<ComparisonType>(), context->getInputBuffer<DataType>(kInputTensor1),
86 context->getInputShape(kInputTensor1), context->getInputBuffer<DataType>(kInputTensor2),
87 context->getInputShape(kInputTensor2), context->getOutputBuffer<bool8>(kOutputTensor),
88 context->getOutputShape(kOutputTensor));
89 }
90
91 template <typename DataType, typename ComparisonType>
executeEqualTyped(IOperationExecutionContext * context)92 bool executeEqualTyped(IOperationExecutionContext* context) {
93 return compute<DataType, ComparisonType>(
94 std::equal_to<ComparisonType>(), context->getInputBuffer<DataType>(kInputTensor1),
95 context->getInputShape(kInputTensor1), context->getInputBuffer<DataType>(kInputTensor2),
96 context->getInputShape(kInputTensor2), context->getOutputBuffer<bool8>(kOutputTensor),
97 context->getOutputShape(kOutputTensor));
98 }
99
100 template <typename DataType, typename ComparisonType>
executeNotEqualTyped(IOperationExecutionContext * context)101 bool executeNotEqualTyped(IOperationExecutionContext* context) {
102 return compute<DataType, ComparisonType>(
103 std::not_equal_to<ComparisonType>(), context->getInputBuffer<DataType>(kInputTensor1),
104 context->getInputShape(kInputTensor1), context->getInputBuffer<DataType>(kInputTensor2),
105 context->getInputShape(kInputTensor2), context->getOutputBuffer<bool8>(kOutputTensor),
106 context->getOutputShape(kOutputTensor));
107 }
108
109 template <typename DataType, typename ComparisonType>
executeGreaterEqualTyped(IOperationExecutionContext * context)110 bool executeGreaterEqualTyped(IOperationExecutionContext* context) {
111 return compute<DataType, ComparisonType>(
112 std::greater_equal<ComparisonType>(), context->getInputBuffer<DataType>(kInputTensor1),
113 context->getInputShape(kInputTensor1), context->getInputBuffer<DataType>(kInputTensor2),
114 context->getInputShape(kInputTensor2), context->getOutputBuffer<bool8>(kOutputTensor),
115 context->getOutputShape(kOutputTensor));
116 }
117
118 template <typename DataType, typename ComparisonType>
executeGreaterTyped(IOperationExecutionContext * context)119 bool executeGreaterTyped(IOperationExecutionContext* context) {
120 return compute<DataType, ComparisonType>(
121 std::greater<ComparisonType>(), context->getInputBuffer<DataType>(kInputTensor1),
122 context->getInputShape(kInputTensor1), context->getInputBuffer<DataType>(kInputTensor2),
123 context->getInputShape(kInputTensor2), context->getOutputBuffer<bool8>(kOutputTensor),
124 context->getOutputShape(kOutputTensor));
125 }
126
127 } // namespace
128
validate(const IOperationValidationContext * context)129 bool validate(const IOperationValidationContext* context) {
130 NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
131 NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
132 OperandType inputType = context->getInputType(kInputTensor1);
133 NN_RET_CHECK(
134 inputType == OperandType::TENSOR_BOOL8 || inputType == OperandType::TENSOR_FLOAT16 ||
135 inputType == OperandType::TENSOR_FLOAT32 || inputType == OperandType::TENSOR_INT32 ||
136 inputType == OperandType::TENSOR_QUANT8_ASYMM ||
137 inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)
138 << "Unsupported input operand type for comparison op: " << toString(inputType);
139 NN_RET_CHECK(validateInputTypes(context, {inputType, inputType}));
140 NN_RET_CHECK(validateOutputTypes(context, {OperandType::TENSOR_BOOL8}));
141 if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
142 return validateHalVersion(context, HalVersion::V1_3);
143 } else {
144 return validateHalVersion(context, HalVersion::V1_2);
145 }
146 }
147
prepare(IOperationExecutionContext * context)148 bool prepare(IOperationExecutionContext* context) {
149 Shape input1 = context->getInputShape(kInputTensor1);
150 Shape input2 = context->getInputShape(kInputTensor2);
151 Shape output = context->getOutputShape(kOutputTensor);
152 NN_RET_CHECK(calculateBroadcastedShape(input1, input2, &output));
153 return context->setOutputShape(kOutputTensor, output);
154 }
155
executeLess(IOperationExecutionContext * context)156 bool executeLess(IOperationExecutionContext* context) {
157 switch (context->getInputType(kInputTensor1)) {
158 case OperandType::TENSOR_FLOAT16:
159 return executeLessTyped<_Float16, _Float16>(context);
160 case OperandType::TENSOR_FLOAT32:
161 return executeLessTyped<float, float>(context);
162 case OperandType::TENSOR_INT32:
163 return executeLessTyped<int32_t, int32_t>(context);
164 case OperandType::TENSOR_QUANT8_ASYMM:
165 return executeLessTyped<uint8_t, float>(context);
166 case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
167 return executeLessTyped<int8_t, float>(context);
168 case OperandType::TENSOR_BOOL8:
169 return executeLessTyped<bool8, bool8>(context);
170 default:
171 NN_RET_CHECK_FAIL() << "Unsupported tensor type for comparison";
172 }
173 }
174
executeLessEqual(IOperationExecutionContext * context)175 bool executeLessEqual(IOperationExecutionContext* context) {
176 switch (context->getInputType(kInputTensor1)) {
177 case OperandType::TENSOR_FLOAT16:
178 return executeLessEqualTyped<_Float16, _Float16>(context);
179 case OperandType::TENSOR_FLOAT32:
180 return executeLessEqualTyped<float, float>(context);
181 case OperandType::TENSOR_INT32:
182 return executeLessEqualTyped<int32_t, int32_t>(context);
183 case OperandType::TENSOR_QUANT8_ASYMM:
184 return executeLessEqualTyped<uint8_t, float>(context);
185 case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
186 return executeLessEqualTyped<int8_t, float>(context);
187 case OperandType::TENSOR_BOOL8:
188 return executeLessEqualTyped<bool8, bool8>(context);
189 default:
190 NN_RET_CHECK_FAIL() << "Unsupported tensor type for comparison";
191 }
192 }
193
executeEqual(IOperationExecutionContext * context)194 bool executeEqual(IOperationExecutionContext* context) {
195 switch (context->getInputType(kInputTensor1)) {
196 case OperandType::TENSOR_FLOAT16:
197 return executeEqualTyped<_Float16, _Float16>(context);
198 case OperandType::TENSOR_FLOAT32:
199 return executeEqualTyped<float, float>(context);
200 case OperandType::TENSOR_INT32:
201 return executeEqualTyped<int32_t, int32_t>(context);
202 case OperandType::TENSOR_QUANT8_ASYMM:
203 return executeEqualTyped<uint8_t, float>(context);
204 case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
205 return executeEqualTyped<int8_t, float>(context);
206 case OperandType::TENSOR_BOOL8:
207 return executeEqualTyped<bool8, bool8>(context);
208 default:
209 NN_RET_CHECK_FAIL() << "Unsupported tensor type for comparison";
210 }
211 }
212
executeNotEqual(IOperationExecutionContext * context)213 bool executeNotEqual(IOperationExecutionContext* context) {
214 switch (context->getInputType(kInputTensor1)) {
215 case OperandType::TENSOR_FLOAT16:
216 return executeNotEqualTyped<_Float16, _Float16>(context);
217 case OperandType::TENSOR_FLOAT32:
218 return executeNotEqualTyped<float, float>(context);
219 case OperandType::TENSOR_INT32:
220 return executeNotEqualTyped<int32_t, int32_t>(context);
221 case OperandType::TENSOR_QUANT8_ASYMM:
222 return executeNotEqualTyped<uint8_t, float>(context);
223 case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
224 return executeNotEqualTyped<int8_t, float>(context);
225 case OperandType::TENSOR_BOOL8:
226 return executeNotEqualTyped<bool8, bool8>(context);
227 default:
228 NN_RET_CHECK_FAIL() << "Unsupported tensor type for comparison";
229 }
230 }
231
executeGreaterEqual(IOperationExecutionContext * context)232 bool executeGreaterEqual(IOperationExecutionContext* context) {
233 switch (context->getInputType(kInputTensor1)) {
234 case OperandType::TENSOR_FLOAT16:
235 return executeGreaterEqualTyped<_Float16, _Float16>(context);
236 case OperandType::TENSOR_FLOAT32:
237 return executeGreaterEqualTyped<float, float>(context);
238 case OperandType::TENSOR_INT32:
239 return executeGreaterEqualTyped<int32_t, int32_t>(context);
240 case OperandType::TENSOR_QUANT8_ASYMM:
241 return executeGreaterEqualTyped<uint8_t, float>(context);
242 case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
243 return executeGreaterEqualTyped<int8_t, float>(context);
244 case OperandType::TENSOR_BOOL8:
245 return executeGreaterEqualTyped<bool8, bool8>(context);
246 default:
247 NN_RET_CHECK_FAIL() << "Unsupported tensor type for comparison";
248 }
249 }
250
executeGreater(IOperationExecutionContext * context)251 bool executeGreater(IOperationExecutionContext* context) {
252 switch (context->getInputType(kInputTensor1)) {
253 case OperandType::TENSOR_FLOAT16:
254 return executeGreaterTyped<_Float16, _Float16>(context);
255 case OperandType::TENSOR_FLOAT32:
256 return executeGreaterTyped<float, float>(context);
257 case OperandType::TENSOR_INT32:
258 return executeGreaterTyped<int32_t, int32_t>(context);
259 case OperandType::TENSOR_QUANT8_ASYMM:
260 return executeGreaterTyped<uint8_t, float>(context);
261 case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
262 return executeGreaterTyped<int8_t, float>(context);
263 case OperandType::TENSOR_BOOL8:
264 return executeGreaterTyped<bool8, bool8>(context);
265 default:
266 NN_RET_CHECK_FAIL() << "Unsupported tensor type for comparison";
267 }
268 }
269
270 } // namespace comparisons
271
272 NN_REGISTER_OPERATION(LESS, "LESS", comparisons::validate, comparisons::prepare,
273 comparisons::executeLess);
274 NN_REGISTER_OPERATION(LESS_EQUAL, "LESS_EQUAL", comparisons::validate, comparisons::prepare,
275 comparisons::executeLessEqual);
276 NN_REGISTER_OPERATION(EQUAL, "EQUAL", comparisons::validate, comparisons::prepare,
277 comparisons::executeEqual);
278 NN_REGISTER_OPERATION(NOT_EQUAL, "NOT_EQUAL", comparisons::validate, comparisons::prepare,
279 comparisons::executeNotEqual);
280 NN_REGISTER_OPERATION(GREATER_EQUAL, "GREATER_EQUAL", comparisons::validate, comparisons::prepare,
281 comparisons::executeGreaterEqual);
282 NN_REGISTER_OPERATION(GREATER, "GREATER", comparisons::validate, comparisons::prepare,
283 comparisons::executeGreater);
284
285 } // namespace nn
286 } // namespace android
287