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