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 <algorithm>
20 #include <vector>
21 
22 #include "MaximumMinimum.h"
23 #include "HalInterfaces.h"
24 #include "IndexedShapeWrapper.h"
25 #include "OperationsUtils.h"
26 #include "Tracing.h"
27 
28 namespace android {
29 namespace nn {
30 namespace maximum_minimum {
31 
32 namespace {
33 
34 using namespace hal;
35 
36 template <typename T>
evalGeneric(const T * aData,const Shape & aShape,const T * bData,const Shape & bShape,bool isMinimum,T * outputData,const Shape & outputShape)37 bool evalGeneric(const T* aData, const Shape& aShape, const T* bData, const Shape& bShape,
38                  bool isMinimum, T* outputData, const Shape& outputShape) {
39     IndexedShapeWrapper aShapeIndexed(aShape);
40     IndexedShapeWrapper bShapeIndexed(bShape);
41     IndexedShapeWrapper outputShapeIndexed(outputShape);
42 
43     std::vector<uint32_t> curIndex(outputShape.dimensions.size(), 0);
44     bool lastIndex = false;
45     do {
46         uint32_t outputFlatIndex;
47         NN_CHECK(outputShapeIndexed.indexToFlatIndex(curIndex, &outputFlatIndex));
48         uint32_t aFlatIndex;
49         NN_CHECK(aShapeIndexed.broadcastedIndexToFlatIndex(curIndex, &aFlatIndex));
50         uint32_t bFlatIndex;
51         NN_CHECK(bShapeIndexed.broadcastedIndexToFlatIndex(curIndex, &bFlatIndex));
52 
53         outputData[outputFlatIndex] = isMinimum ? std::min(aData[aFlatIndex], bData[bFlatIndex])
54                                                 : std::max(aData[aFlatIndex], bData[bFlatIndex]);
55 
56         NN_CHECK(outputShapeIndexed.nextIndexInplace(&curIndex, &lastIndex));
57     } while (!lastIndex);
58 
59     return true;
60 }
61 
62 template <typename T>
evalQuant8(const T * aData,const Shape & aShape,const T * bData,const Shape & bShape,bool isMinimum,T * outputData,const Shape & outputShape)63 bool evalQuant8(const T* aData, const Shape& aShape, const T* bData, const Shape& bShape,
64                 bool isMinimum, T* outputData, const Shape& outputShape) {
65     IndexedShapeWrapper aShapeIndexed(aShape);
66     IndexedShapeWrapper bShapeIndexed(bShape);
67     IndexedShapeWrapper outputShapeIndexed(outputShape);
68 
69     std::vector<uint32_t> curIndex(outputShape.dimensions.size(), 0);
70     bool lastIndex = false;
71     do {
72         uint32_t outputFlatIndex;
73         NN_CHECK(outputShapeIndexed.indexToFlatIndex(curIndex, &outputFlatIndex));
74         uint32_t aFlatIndex;
75         NN_CHECK(aShapeIndexed.broadcastedIndexToFlatIndex(curIndex, &aFlatIndex));
76         uint32_t bFlatIndex;
77         NN_CHECK(bShapeIndexed.broadcastedIndexToFlatIndex(curIndex, &bFlatIndex));
78 
79         T aValue = requantize<T>(aData[aFlatIndex], aShape, outputShape);
80         T bValue = requantize<T>(bData[bFlatIndex], bShape, outputShape);
81         outputData[outputFlatIndex] =
82                 isMinimum ? std::min(aValue, bValue) : std::max(aValue, bValue);
83 
84         NN_CHECK(outputShapeIndexed.nextIndexInplace(&curIndex, &lastIndex));
85     } while (!lastIndex);
86 
87     return true;
88 }
89 
90 }  // namespace
91 
prepare(const Shape & in1,const Shape & in2,Shape * out)92 bool prepare(const Shape& in1, const Shape& in2, Shape* out) {
93     NN_CHECK(in1.type == in2.type);
94     return calculateBroadcastedShape(in1, in2, out);
95 }
96 
eval(const void * in1,const Shape & shape1,const void * in2,const Shape & shape2,bool isMinimum,void * output,const Shape & outputShape)97 bool eval(const void* in1, const Shape& shape1, const void* in2, const Shape& shape2,
98           bool isMinimum, void* output, const Shape& outputShape) {
99     NNTRACE_COMP("maximum_minimum::eval");
100     switch (shape1.type) {
101         case OperandType::TENSOR_FLOAT16: {
102             return evalGeneric(reinterpret_cast<const _Float16*>(in1), shape1,
103                                reinterpret_cast<const _Float16*>(in2), shape2, isMinimum,
104                                reinterpret_cast<_Float16*>(output), outputShape);
105         }
106         case OperandType::TENSOR_FLOAT32: {
107             return evalGeneric(reinterpret_cast<const float*>(in1), shape1,
108                                reinterpret_cast<const float*>(in2), shape2, isMinimum,
109                                reinterpret_cast<float*>(output), outputShape);
110         }
111         case OperandType::TENSOR_INT32: {
112             return evalGeneric(reinterpret_cast<const int32_t*>(in1), shape1,
113                                reinterpret_cast<const int32_t*>(in2), shape2, isMinimum,
114                                reinterpret_cast<int32_t*>(output), outputShape);
115         }
116         case OperandType::TENSOR_QUANT8_ASYMM: {
117             return evalQuant8(reinterpret_cast<const uint8_t*>(in1), shape1,
118                               reinterpret_cast<const uint8_t*>(in2), shape2, isMinimum,
119                               reinterpret_cast<uint8_t*>(output), outputShape);
120         }
121         case OperandType::TENSOR_QUANT8_ASYMM_SIGNED: {
122             return evalQuant8(reinterpret_cast<const int8_t*>(in1), shape1,
123                               reinterpret_cast<const int8_t*>(in2), shape2, isMinimum,
124                               reinterpret_cast<int8_t*>(output), outputShape);
125         }
126         default: {
127             LOG(ERROR) << "Unsupported data type: " << toString(shape1.type);
128             return false;
129         }
130     }
131 }
132 
133 }  // namespace maximum_minimum
134 }  // namespace nn
135 }  // namespace android
136