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