1 /*
2  * Copyright (C) 2020 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 #define LOG_TAG "Operations"
18 
19 #include "LocalResponseNormalization.h"
20 
21 #include <algorithm>
22 #include <vector>
23 
24 #include "OperationResolver.h"
25 #include "Tracing.h"
26 
27 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
28 #pragma clang diagnostic push
29 #pragma clang diagnostic ignored "-Wunused-parameter"
30 #pragma clang diagnostic ignored "-Wsign-compare"
31 #pragma clang diagnostic ignored "-Winvalid-partial-specialization"
32 #include <tensorflow/lite/kernels/internal/optimized/optimized_ops.h>
33 #pragma clang diagnostic pop
34 
35 #include "CpuOperationUtils.h"
36 #endif  // NN_INCLUDE_CPU_IMPLEMENTATION
37 
38 namespace android {
39 namespace nn {
40 namespace local_response_norm {
41 
42 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
43 namespace {
44 
localResponseNormFloat32Impl(const float * inputData,const Shape & inputShape,int32_t radius,float bias,float alpha,float beta,int32_t axis,float * outputData,const Shape &)45 inline bool localResponseNormFloat32Impl(const float* inputData, const Shape& inputShape,
46                                          int32_t radius, float bias, float alpha, float beta,
47                                          int32_t axis, float* outputData,
48                                          const Shape& /*outputShape*/) {
49     NNTRACE_TRANS("localResponseNormFloat32");
50     const uint32_t outerSize = getNumberOfElements(inputShape, 0, axis);
51     const uint32_t axisSize = getSizeOfDimension(inputShape, axis);
52     const uint32_t innerSize =
53             getNumberOfElements(inputShape, axis + 1, getNumberOfDimensions(inputShape));
54     for (uint32_t outer = 0; outer < outerSize; ++outer) {
55         const float* inputBase = inputData + outer * axisSize * innerSize;
56         float* outputBase = outputData + outer * axisSize * innerSize;
57         for (uint32_t inner = 0; inner < innerSize; ++inner, ++inputBase, ++outputBase) {
58             for (int32_t i = 0; i < static_cast<int32_t>(axisSize); i++) {
59                 const int32_t dBegin = std::max(0, i - radius);
60                 // Add 1 on dEnd to comply with optimized_ops in TFLite
61                 const int32_t dEnd = std::min(static_cast<int32_t>(axisSize), i + radius + 1);
62                 float sum = 0.0f;
63                 for (int32_t d = dBegin; d < dEnd; d++) {
64                     float val = inputBase[d * innerSize];
65                     sum += val * val;
66                 }
67                 float multiplier = std::pow(bias + alpha * sum, -beta);
68                 outputBase[i * innerSize] = inputBase[i * innerSize] * multiplier;
69             }
70         }
71     }
72     return true;
73 }
74 
75 template <typename T>
76 bool localResponseNorm(const T* inputData, const Shape& inputShape, int32_t radius, T bias, T alpha,
77                        T beta, int32_t axis, T* outputData, const Shape& outputShape);
78 
79 template <>
localResponseNorm(const float * inputData,const Shape & inputShape,int32_t radius,float bias,float alpha,float beta,int32_t axis,float * outputData,const Shape & outputShape)80 bool localResponseNorm<float>(const float* inputData, const Shape& inputShape, int32_t radius,
81                               float bias, float alpha, float beta, int32_t axis, float* outputData,
82                               const Shape& outputShape) {
83     int32_t ndim = getNumberOfDimensions(inputShape);
84     NN_CHECK(handleNegativeAxis(inputShape, &axis));
85     radius = std::min(radius, static_cast<int32_t>(inputShape.dimensions[axis]));
86     // TFLite optimized implementation only supports computation along the last axis
87     if (axis == ndim - 1) {
88         NNTRACE_COMP("optimized_ops::LocalResponseNormalization::float");
89         tflite::LocalResponseNormalizationParams param = {
90                 .range = radius, .bias = bias, .alpha = alpha, .beta = beta};
91         tflite::optimized_ops::LocalResponseNormalization(
92                 param, convertShapeToTflshape(inputShape), inputData,
93                 convertShapeToTflshape(outputShape), outputData);
94         return true;
95     } else {
96         return localResponseNormFloat32Impl(inputData, inputShape, radius, bias, alpha, beta, axis,
97                                             outputData, outputShape);
98     }
99 }
100 
101 template <>
localResponseNorm(const _Float16 * inputData,const Shape & inputShape,int32_t radius,_Float16 bias,_Float16 alpha,_Float16 beta,int32_t axis,_Float16 * outputData,const Shape & outputShape)102 bool localResponseNorm<_Float16>(const _Float16* inputData, const Shape& inputShape, int32_t radius,
103                                  _Float16 bias, _Float16 alpha, _Float16 beta, int32_t axis,
104                                  _Float16* outputData, const Shape& outputShape) {
105     NNTRACE_TRANS("localResponseNormFloat16");
106     std::vector<float> inputDataFloat32(getNumberOfElements(inputShape));
107     convertFloat16ToFloat32(inputData, &inputDataFloat32);
108     std::vector<float> outputDataFloat32(getNumberOfElements(outputShape));
109 
110     localResponseNorm<float>(inputDataFloat32.data(), inputShape, radius, bias, alpha, beta, axis,
111                              outputDataFloat32.data(), outputShape);
112     convertFloat32ToFloat16(outputDataFloat32, outputData);
113 
114     return true;
115 }
116 
117 template <typename T>
executeTyped(IOperationExecutionContext * context)118 bool executeTyped(IOperationExecutionContext* context) {
119     int32_t axis = context->getNumInputs() == kNumInputs
120                            ? context->getInputValue<int32_t>(kAxisScalar)
121                            : -1;
122     NN_RET_CHECK(handleNegativeAxis(context->getInputShape(kInputTensor), &axis));
123     return localResponseNorm<T>(
124             context->getInputBuffer<T>(kInputTensor), context->getInputShape(kInputTensor),
125             context->getInputValue<int32_t>(kRadiusScalar), context->getInputValue<T>(kBiasScalar),
126             context->getInputValue<T>(kAlphaScalar), context->getInputValue<T>(kBetaScalar), axis,
127             context->getOutputBuffer<T>(kOutputTensor), context->getOutputShape(kOutputTensor));
128 }
129 
130 }  // namespace
131 
prepare(IOperationExecutionContext * context)132 bool prepare(IOperationExecutionContext* context) {
133     const Shape& input = context->getInputShape(kInputTensor);
134     int32_t numDimensions = getNumberOfDimensions(input);
135     int32_t axis = context->getNumInputs() == kNumInputs
136                            ? context->getInputValue<int32_t>(kAxisScalar)
137                            : -1;
138     NN_RET_CHECK_LE(numDimensions, 4);
139     NN_RET_CHECK_GE(axis, -numDimensions);
140     NN_RET_CHECK_LT(axis, numDimensions);
141     const int32_t radius = context->getInputValue<int32_t>(kRadiusScalar);
142     NN_RET_CHECK_GE(radius, 0);
143     return context->setOutputShape(kOutputTensor, input);
144 }
145 
execute(IOperationExecutionContext * context)146 bool execute(IOperationExecutionContext* context) {
147     switch (context->getInputType(kInputTensor)) {
148         case OperandType::TENSOR_FLOAT32:
149             return executeTyped<float>(context);
150         case OperandType::TENSOR_FLOAT16:
151             return executeTyped<_Float16>(context);
152         default:
153             NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
154     }
155 }
156 #endif  // NN_INCLUDE_CPU_IMPLEMENTATION
157 
158 }  // namespace local_response_norm
159 
160 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(LOCAL_RESPONSE_NORMALIZATION, local_response_norm::prepare,
161                                          local_response_norm::execute);
162 
163 }  // namespace nn
164 }  // namespace android
165