Normalization.cpp 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include "CpuOperationUtils.h"
  17. #include "Operations.h"
  18. #include <algorithm>
  19. #include <cmath>
  20. #include "tensorflow/lite/kernels/internal/optimized/optimized_ops.h"
  21. #include "Tracing.h"
  22. namespace android {
  23. namespace nn {
  24. inline bool localResponseNormFloat32Impl(const float* inputData, const Shape& inputShape,
  25. int32_t radius, float bias, float alpha, float beta,
  26. int32_t axis, float* outputData,
  27. const Shape& outputShape) {
  28. NNTRACE_TRANS("localResponseNormFloat32");
  29. const uint32_t outerSize = getNumberOfElements(inputShape, 0, axis);
  30. const uint32_t axisSize = getSizeOfDimension(inputShape, axis);
  31. const uint32_t innerSize =
  32. getNumberOfElements(inputShape, axis + 1, getNumberOfDimensions(inputShape));
  33. for (uint32_t outer = 0; outer < outerSize; ++outer) {
  34. const float* inputBase = inputData + outer * axisSize * innerSize;
  35. float* outputBase = outputData + outer * axisSize * innerSize;
  36. for (uint32_t inner = 0; inner < innerSize; ++inner, ++inputBase, ++outputBase) {
  37. for (int32_t i = 0; i < axisSize; i++) {
  38. const int32_t dBegin = std::max(0, i - radius);
  39. // Add 1 on dEnd to comply with optimized_ops in TFLite
  40. const int32_t dEnd = std::min(static_cast<int32_t>(axisSize), i + radius + 1);
  41. float sum = 0.0f;
  42. for (int32_t d = dBegin; d < dEnd; d++) {
  43. float val = inputBase[d * innerSize];
  44. sum += val * val;
  45. }
  46. float multiplier = std::pow(bias + alpha * sum, -beta);
  47. outputBase[i * innerSize] = inputBase[i * innerSize] * multiplier;
  48. }
  49. }
  50. }
  51. return true;
  52. }
  53. bool localResponseNormFloat16(const _Float16* inputData, const Shape& inputShape, int32_t radius,
  54. float bias, float alpha, float beta, int32_t axis,
  55. _Float16* outputData, const Shape& outputShape) {
  56. NNTRACE_TRANS("localResponseNormFloat16");
  57. std::vector<float> inputDataFloat32(getNumberOfElements(inputShape));
  58. convertFloat16ToFloat32(inputData, &inputDataFloat32);
  59. std::vector<float> outputDataFloat32(getNumberOfElements(outputShape));
  60. localResponseNormFloat32(inputDataFloat32.data(), inputShape, radius, bias, alpha, beta, axis,
  61. outputDataFloat32.data(), outputShape);
  62. convertFloat32ToFloat16(outputDataFloat32, outputData);
  63. return true;
  64. }
  65. bool localResponseNormFloat32(const float* inputData, const Shape& inputShape, int32_t radius,
  66. float bias, float alpha, float beta, int32_t axis, float* outputData,
  67. const Shape& outputShape) {
  68. int32_t ndim = getNumberOfDimensions(inputShape);
  69. NN_CHECK(handleNegativeAxis(inputShape, &axis));
  70. // TFLite optimized implementation only supports computation along the last axis
  71. if (axis == ndim - 1) {
  72. NNTRACE_COMP("optimized_ops::LocalResponseNormalization::float");
  73. tflite::LocalResponseNormalizationParams param = {
  74. .range = radius, .bias = bias, .alpha = alpha, .beta = beta};
  75. tflite::optimized_ops::LocalResponseNormalization(
  76. param, convertShapeToTflshape(inputShape), inputData,
  77. convertShapeToTflshape(outputShape), outputData);
  78. return true;
  79. } else {
  80. return localResponseNormFloat32Impl(inputData, inputShape, radius, bias, alpha, beta, axis,
  81. outputData, outputShape);
  82. }
  83. }
  84. } // namespace nn
  85. } // namespace android