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 "Operations.h"
22 #include "CpuOperationUtils.h"
23
24 #include "Tracing.h"
25
26 namespace android {
27 namespace nn {
28
29 template <typename In, typename Out>
argMinMaxImpl(const In * inputData,const Shape & inputShape,int32_t axis,bool isArgMin,Out * outputData,const Shape & outputShape)30 static void argMinMaxImpl(const In* inputData, const Shape& inputShape,
31 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 = getNumberOfElements(
36 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 =
43 inputData[(outer * axisSize + i) * innerSize + inner];
44 if ((isArgMin && value < minMaxValue) ||
45 (!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,
56 int32 axis, bool isArgMin,
57 uint8_t* outputData, const Shape& outputShape) {
58 NNTRACE_TRANS("argMinMaxGeneric");
59 NN_CHECK(handleNegativeAxis(inputShape, &axis));
60
61 #define NNAPI_IMPL_ARG_MIN_MAX(operandType, dataType) \
62 if (inputShape.type == operandType) { \
63 NNTRACE_COMP_SWITCH("argMinMaxImpl::" #dataType); \
64 argMinMaxImpl( \
65 reinterpret_cast<const dataType*>(inputData), \
66 inputShape, \
67 axis, \
68 isArgMin, \
69 reinterpret_cast<int32_t*>(outputData), \
70 outputShape); \
71 return true; \
72 }
73
74 NNAPI_IMPL_ARG_MIN_MAX(OperandType::TENSOR_FLOAT16, _Float16);
75 NNAPI_IMPL_ARG_MIN_MAX(OperandType::TENSOR_FLOAT32, float);
76 NNAPI_IMPL_ARG_MIN_MAX(OperandType::TENSOR_INT32, int32_t);
77 NNAPI_IMPL_ARG_MIN_MAX(OperandType::TENSOR_QUANT8_ASYMM, uint8_t);
78 #undef NNAPI_IMPL_ARG_MIN_MAX
79
80 LOG(ERROR) << "Unsupported data type";
81 return false;
82 }
83
84 } // namespace nn
85 } // namespace android
86