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