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 logical {
27
28 constexpr uint32_t kNumInputs = 2;
29 constexpr uint32_t kInputTensor1 = 0;
30 constexpr uint32_t kInputTensor2 = 1;
31
32 constexpr uint32_t kNumOutputs = 1;
33 constexpr uint32_t kOutputTensor = 0;
34
35 namespace {
36
37 using namespace hal;
38
compute(const std::function<bool (bool,bool)> & func,const bool8 * aData,const Shape & aShape,const bool8 * bData,const Shape & bShape,bool8 * outputData,const Shape & outputShape)39 bool compute(const std::function<bool(bool, bool)>& func, const bool8* aData, const Shape& aShape,
40 const bool8* bData, const Shape& bShape, bool8* outputData, const Shape& outputShape) {
41 IndexedShapeWrapper aShapeIndexed(aShape);
42 IndexedShapeWrapper bShapeIndexed(bShape);
43 IndexedShapeWrapper outputShapeIndexed(outputShape);
44 std::vector<uint32_t> curIndex(outputShape.dimensions.size(), 0);
45 bool lastIndex = false;
46 do {
47 uint32_t outputFlatIndex;
48 NN_RET_CHECK(outputShapeIndexed.indexToFlatIndex(curIndex, &outputFlatIndex));
49 uint32_t aFlatIndex;
50 NN_RET_CHECK(aShapeIndexed.broadcastedIndexToFlatIndex(curIndex, &aFlatIndex));
51 uint32_t bFlatIndex;
52 NN_RET_CHECK(bShapeIndexed.broadcastedIndexToFlatIndex(curIndex, &bFlatIndex));
53
54 outputData[outputFlatIndex] = func(aData[aFlatIndex], bData[bFlatIndex]);
55
56 NN_RET_CHECK(outputShapeIndexed.nextIndexInplace(&curIndex, &lastIndex));
57 } while (!lastIndex);
58 return true;
59 }
60
61 } // namespace
62
validate(const IOperationValidationContext * context)63 bool validate(const IOperationValidationContext* context) {
64 NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
65 NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
66 OperandType inputType = context->getInputType(kInputTensor1);
67 NN_RET_CHECK(inputType == OperandType::TENSOR_BOOL8)
68 << "Unsupported tensor type for a logical operation";
69 NN_RET_CHECK(validateInputTypes(context, {inputType, inputType}));
70 NN_RET_CHECK(validateOutputTypes(context, {inputType}));
71 return validateHalVersion(context, HalVersion::V1_2);
72 }
73
prepare(IOperationExecutionContext * context)74 bool prepare(IOperationExecutionContext* context) {
75 Shape input1 = context->getInputShape(kInputTensor1);
76 Shape input2 = context->getInputShape(kInputTensor2);
77 Shape output = context->getOutputShape(kOutputTensor);
78 NN_RET_CHECK(calculateBroadcastedShape(input1, input2, &output));
79 return context->setOutputShape(kOutputTensor, output);
80 }
81
executeAnd(IOperationExecutionContext * context)82 bool executeAnd(IOperationExecutionContext* context) {
83 return compute(
84 std::logical_and<bool>(), context->getInputBuffer<bool8>(kInputTensor1),
85 context->getInputShape(kInputTensor1), context->getInputBuffer<bool8>(kInputTensor2),
86 context->getInputShape(kInputTensor2), context->getOutputBuffer<bool8>(kOutputTensor),
87 context->getOutputShape(kOutputTensor));
88 }
89
executeOr(IOperationExecutionContext * context)90 bool executeOr(IOperationExecutionContext* context) {
91 return compute(
92 std::logical_or<bool>(), context->getInputBuffer<bool8>(kInputTensor1),
93 context->getInputShape(kInputTensor1), context->getInputBuffer<bool8>(kInputTensor2),
94 context->getInputShape(kInputTensor2), context->getOutputBuffer<bool8>(kOutputTensor),
95 context->getOutputShape(kOutputTensor));
96 }
97
98 } // namespace logical
99
100 NN_REGISTER_OPERATION(LOGICAL_AND, "LOGICAL_AND", logical::validate, logical::prepare,
101 logical::executeAnd);
102 NN_REGISTER_OPERATION(LOGICAL_OR, "LOGICAL_OR", logical::validate, logical::prepare,
103 logical::executeOr);
104
105 } // namespace nn
106 } // namespace android
107