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