1 /*
2  * Copyright (C) 2017 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 <tensorflow/lite/kernels/internal/optimized/legacy_optimized_ops.h>
22 #include <tensorflow/lite/kernels/internal/reference/legacy_reference_ops.h>
23 
24 #include <vector>
25 
26 #include "CpuOperationUtils.h"
27 #include "Operations.h"
28 #include "Tracing.h"
29 
30 namespace android {
31 namespace nn {
32 
meanFloat16(_Float16 * inputData,const Shape & inputShape,const int32_t * axis,const Shape & axisShape,bool keepDims,_Float16 * outputData,const Shape & outputShape)33 bool meanFloat16(_Float16* inputData, const Shape& inputShape, const int32_t* axis,
34                  const Shape& axisShape, bool keepDims, _Float16* outputData,
35                  const Shape& outputShape) {
36     NNTRACE_TRANS("meanFloat16");
37     std::vector<float> inputDataFloat32(getNumberOfElements(inputShape));
38     convertFloat16ToFloat32(inputData, &inputDataFloat32);
39 
40     std::vector<float> outputDataFloat32(getNumberOfElements(outputShape));
41     meanGeneric<float, float>(inputDataFloat32.data(), inputShape, axis, axisShape, keepDims,
42                               outputDataFloat32.data(), outputShape);
43     convertFloat32ToFloat16(outputDataFloat32, outputData);
44     return true;
45 }
46 
47 template <typename T, typename U>
meanGeneric(T * inputData,const Shape & inputShape,const int32_t * axis,const Shape & axisShape,bool keepDims,T * outputData,const Shape & outputShape)48 bool meanGeneric(T* inputData, const Shape& inputShape, const int32_t* axis, const Shape& axisShape,
49                  bool keepDims, T* outputData, const Shape& outputShape) {
50     NNTRACE_TRANS("meanGeneric");
51     // Creates a temp index to iterate through input data.
52     int32_t* scratchBuffer = new int32_t[getNumberOfDimensions(inputShape)];
53 
54     // Creates a temp tensor to store resolved axis given input data.
55     int32_t axisSize = static_cast<int32_t>(getSizeOfDimension(axisShape, 0));
56     int32_t* resolvedAxis = new int32_t[axisSize];
57 
58     bool result = true;
59     U* tempSumBuffer = new (std::nothrow) U[getNumberOfElements(outputShape)];
60     if (!tempSumBuffer) {
61         LOG(ERROR) << "Failed to allocate tempSumBuffer for MEAN";
62         result = false;
63     } else {
64         NNTRACE_COMP_SWITCH("optimized_ops::Mean");
65         tflite::reference_ops::Mean<T, U>(
66                 inputData, reinterpret_cast<const int*>(inputShape.dimensions.data()),
67                 getNumberOfDimensions(inputShape), outputData,
68                 reinterpret_cast<const int*>(outputShape.dimensions.data()),
69                 getNumberOfDimensions(outputShape), axis, axisSize, keepDims, scratchBuffer,
70                 resolvedAxis, tempSumBuffer);
71         delete[] tempSumBuffer;
72     }
73     delete[] scratchBuffer;
74     delete[] resolvedAxis;
75     return result;
76 }
77 template bool meanGeneric<float, float>(float* inputData, const Shape& inputShape,
78                                         const int32_t* axis, const Shape& axisShape, bool keepDims,
79                                         float* outputData, const Shape& outputShape);
80 template bool meanGeneric<uint8_t, int32_t>(uint8_t* inputData, const Shape& inputShape,
81                                             const int32_t* axis, const Shape& axisShape,
82                                             bool keepDims, uint8_t* outputData,
83                                             const Shape& outputShape);
84 template bool meanGeneric<int8_t, int32_t>(int8_t* inputData, const Shape& inputShape,
85                                            const int32_t* axis, const Shape& axisShape,
86                                            bool keepDims, int8_t* outputData,
87                                            const Shape& outputShape);
88 
89 }  // namespace nn
90 }  // namespace android
91