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 // Contains the implementation of the operations.
18
19 #define LOG_TAG "Operations"
20
21 #include "ArgMinMax.h"
22
23 #include "CpuOperationUtils.h"
24 #include "Operations.h"
25 #include "Tracing.h"
26
27 namespace android {
28 namespace nn {
29
30 template <typename In, typename Out>
argMinMaxImpl(const In * inputData,const Shape & inputShape,int32_t axis,bool isArgMin,Out * outputData,const Shape &)31 static void argMinMaxImpl(const In* inputData, const Shape& inputShape, int32_t axis, bool isArgMin,
32 Out* outputData, const Shape& /*outputShape*/) {
33 const int outerSize = getNumberOfElements(inputShape, 0, axis);
34 const int axisSize = getSizeOfDimension(inputShape, axis);
35 const int innerSize =
36 getNumberOfElements(inputShape, axis + 1, getNumberOfDimensions(inputShape));
37 for (int outer = 0; outer < outerSize; ++outer) {
38 for (int inner = 0; inner < innerSize; ++inner) {
39 auto minMaxValue = inputData[outer * axisSize * innerSize + inner];
40 int minMaxIndex = 0;
41 for (int i = 1; i < axisSize; ++i) {
42 const auto& value = inputData[(outer * axisSize + i) * innerSize + inner];
43 if ((isArgMin && value < minMaxValue) || (!isArgMin && value > minMaxValue)) {
44 minMaxValue = value;
45 minMaxIndex = i;
46 }
47 }
48 outputData[outer * innerSize + inner] = minMaxIndex;
49 }
50 }
51 }
52
argMinMaxGeneric(const uint8_t * inputData,const Shape & inputShape,int32 axis,bool isArgMin,uint8_t * outputData,const Shape & outputShape)53 bool argMinMaxGeneric(const uint8_t* inputData, const Shape& inputShape, int32 axis, bool isArgMin,
54 uint8_t* outputData, const Shape& outputShape) {
55 NNTRACE_TRANS("argMinMaxGeneric");
56 NN_CHECK(handleNegativeAxis(inputShape, &axis));
57
58 #define NNAPI_IMPL_ARG_MIN_MAX(operandType, dataType) \
59 if (inputShape.type == operandType) { \
60 NNTRACE_COMP_SWITCH("argMinMaxImpl::" #dataType); \
61 argMinMaxImpl(reinterpret_cast<const dataType*>(inputData), inputShape, axis, isArgMin, \
62 reinterpret_cast<int32_t*>(outputData), outputShape); \
63 return true; \
64 }
65
66 NNAPI_IMPL_ARG_MIN_MAX(OperandType::TENSOR_FLOAT16, _Float16);
67 NNAPI_IMPL_ARG_MIN_MAX(OperandType::TENSOR_FLOAT32, float);
68 NNAPI_IMPL_ARG_MIN_MAX(OperandType::TENSOR_INT32, int32_t);
69 NNAPI_IMPL_ARG_MIN_MAX(OperandType::TENSOR_QUANT8_ASYMM, uint8_t);
70 NNAPI_IMPL_ARG_MIN_MAX(OperandType::TENSOR_QUANT8_ASYMM_SIGNED, int8_t);
71 #undef NNAPI_IMPL_ARG_MIN_MAX
72
73 LOG(ERROR) << "Unsupported data type";
74 return false;
75 }
76
77 } // namespace nn
78 } // namespace android
79