l2norm.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #include "packages/TensorflowLiteMicro/tensorflow/lite/c/common.h"
  13. #include "packages/TensorflowLiteMicro/tensorflow/lite/kernels/internal/reference/integer_ops/l2normalization.h"
  14. #include "packages/TensorflowLiteMicro/tensorflow/lite/kernels/internal/reference/l2normalization.h"
  15. #include "packages/TensorflowLiteMicro/tensorflow/lite/kernels/internal/tensor.h"
  16. #include "packages/TensorflowLiteMicro/tensorflow/lite/kernels/kernel_util.h"
  17. namespace tflite {
  18. namespace ops {
  19. namespace micro {
  20. namespace l2norm {
  21. // This file has two implementation of L2Norm.
  22. enum KernelType {
  23. kReference,
  24. kGenericOptimized,
  25. };
  26. constexpr int kInputTensor = 0;
  27. constexpr int kOutputTensor = 0;
  28. TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
  29. #if defined(DEBUG)
  30. auto* params = reinterpret_cast<TfLiteL2NormParams*>(node->builtin_data);
  31. TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
  32. TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
  33. const TfLiteTensor* input = GetInput(context, node, kInputTensor);
  34. TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
  35. TF_LITE_ENSURE(context, NumDimensions(input) <= 4);
  36. TF_LITE_ENSURE(context, output->type == kTfLiteFloat32 ||
  37. output->type == kTfLiteUInt8 ||
  38. output->type == kTfLiteInt8);
  39. TF_LITE_ENSURE_EQ(context, input->type, output->type);
  40. if (output->type == kTfLiteUInt8 || output->type == kTfLiteInt8) {
  41. TF_LITE_ENSURE_EQ(context, output->params.scale, (1. / 128.));
  42. if (output->type == kTfLiteUInt8) {
  43. TF_LITE_ENSURE_EQ(context, output->params.zero_point, 128);
  44. }
  45. if (output->type == kTfLiteInt8) {
  46. TF_LITE_ENSURE_EQ(context, output->params.zero_point, 0);
  47. }
  48. }
  49. // TODO(ahentz): For some reason our implementations don't support
  50. // activations.
  51. TF_LITE_ENSURE_EQ(context, params->activation, kTfLiteActNone);
  52. #endif
  53. return kTfLiteOk;
  54. }
  55. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  56. const TfLiteTensor* input = GetInput(context, node, kInputTensor);
  57. TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
  58. // TODO(b/143912164): instead of hardcode the epsilon here, we should read it
  59. // from tensorflow, i.e., adding a params.
  60. // We don't compute epsilon for quantized kernel:
  61. //
  62. // epsilon_float = (epsilon_quant - zp) * scale
  63. // so
  64. // espsilon_quant = epsilon_float / scale + zp
  65. // We know epsilon_float is just a very small number to avoid division by
  66. // zero error, and scale is > 1, so the integer value of epsilon for quant
  67. // is just dominated by the zero point.
  68. // Also, GetInvSqrtQuantizedMultiplierExp handles the scenario where the sum
  69. // of input value squared is zero case well.
  70. // So we don't even need to do handle the epsilon for quantized kernel case.
  71. const float epsilon = 1e-6f;
  72. if (output->type == kTfLiteFloat32) {
  73. #define TF_LITE_L2NORM(type) \
  74. tflite::L2NormalizationParams op_params; \
  75. op_params.input_zero_point = 0; \
  76. type::L2Normalization(op_params, GetTensorShape(input), \
  77. GetTensorData<float>(input), GetTensorShape(output), \
  78. GetTensorData<float>(output), epsilon)
  79. TF_LITE_L2NORM(reference_ops);
  80. #undef TF_LITE_L2NORM
  81. } else if (output->type == kTfLiteUInt8) {
  82. #define TF_LITE_L2NORM(type) \
  83. tflite::L2NormalizationParams op_params; \
  84. op_params.input_zero_point = input->params.zero_point; \
  85. type::L2Normalization(op_params, GetTensorShape(input), \
  86. GetTensorData<uint8>(input), GetTensorShape(output), \
  87. GetTensorData<uint8>(output))
  88. TF_LITE_L2NORM(reference_ops);
  89. #undef TF_LITE_L2NORM
  90. } else if (output->type == kTfLiteInt8) {
  91. const auto input_shape = GetTensorShape(input);
  92. const auto output_shape = GetTensorShape(output);
  93. const int trailing_dim = input_shape.DimensionsCount() - 1;
  94. const int depth =
  95. MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim);
  96. const int outer_size =
  97. MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape);
  98. reference_integer_ops::L2Normalization(input->params.zero_point, outer_size,
  99. depth, GetTensorData<int8>(input),
  100. GetTensorData<int8>(output));
  101. } else {
  102. TF_LITE_KERNEL_LOG(context, "Output type is %d, requires float.",
  103. output->type);
  104. return kTfLiteError;
  105. }
  106. return kTfLiteOk;
  107. }
  108. } // namespace l2norm
  109. TfLiteRegistration* Register_L2NORM_REF() {
  110. static TfLiteRegistration r = {/*init=*/nullptr,
  111. /*free=*/nullptr,
  112. /*prepare=*/l2norm::Prepare,
  113. /*invoke=*/l2norm::Eval,
  114. /*profiling_string=*/nullptr,
  115. /*builtin_code=*/0,
  116. /*custom_name=*/nullptr,
  117. /*version=*/0};
  118. return &r;
  119. }
  120. TfLiteRegistration* Register_L2_NORMALIZATION() {
  121. return Register_L2NORM_REF();
  122. }
  123. } // namespace micro
  124. } // namespace ops
  125. } // namespace tflite