l2norm.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "tensorflow/lite/c/common.h"
  13. #include "tensorflow/lite/kernels/internal/reference/integer_ops/l2normalization.h"
  14. #include "tensorflow/lite/kernels/internal/reference/l2normalization.h"
  15. #include "tensorflow/lite/kernels/internal/tensor.h"
  16. #include "tensorflow/lite/kernels/kernel_util.h"
  17. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  18. namespace tflite {
  19. namespace ops {
  20. namespace micro {
  21. namespace l2norm {
  22. namespace {
  23. // This file has two implementation of L2Norm.
  24. enum KernelType {
  25. kReference,
  26. kGenericOptimized,
  27. };
  28. constexpr int kInputTensor = 0;
  29. constexpr int kOutputTensor = 0;
  30. } // namespace
  31. TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
  32. TFLITE_DCHECK(node->user_data != nullptr);
  33. TFLITE_DCHECK(node->builtin_data != nullptr);
  34. auto* params = reinterpret_cast<TfLiteL2NormParams*>(node->builtin_data);
  35. L2NormalizationParams* data =
  36. static_cast<L2NormalizationParams*>(node->user_data);
  37. TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
  38. TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
  39. const TfLiteTensor* input = GetInput(context, node, kInputTensor);
  40. TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
  41. TF_LITE_ENSURE(context, NumDimensions(input) <= 4);
  42. TF_LITE_ENSURE(context, output->type == kTfLiteFloat32 ||
  43. output->type == kTfLiteUInt8 ||
  44. output->type == kTfLiteInt8);
  45. TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type);
  46. if (output->type == kTfLiteUInt8 || output->type == kTfLiteInt8) {
  47. data->input_zero_point = input->params.zero_point;
  48. } else if (output->type == kTfLiteFloat32) {
  49. data->input_zero_point = 0;
  50. }
  51. // TODO(ahentz): For some reason our implementations don't support
  52. // activations.
  53. TF_LITE_ENSURE_EQ(context, params->activation, kTfLiteActNone);
  54. return kTfLiteOk;
  55. }
  56. void* Init(TfLiteContext* context, const char* buffer, size_t length) {
  57. TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
  58. return context->AllocatePersistentBuffer(context,
  59. sizeof(L2NormalizationParams));
  60. }
  61. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  62. TFLITE_DCHECK(node->user_data != nullptr);
  63. const L2NormalizationParams& data =
  64. *(static_cast<const L2NormalizationParams*>(node->user_data));
  65. const TfLiteEvalTensor* input =
  66. tflite::micro::GetEvalInput(context, node, kInputTensor);
  67. TfLiteEvalTensor* output =
  68. tflite::micro::GetEvalOutput(context, node, kOutputTensor);
  69. // TODO(b/143912164): instead of hardcode the epsilon here, we should read it
  70. // from tensorflow, i.e., adding a params.
  71. // We don't compute epsilon for quantized kernel:
  72. //
  73. // epsilon_float = (epsilon_quant - zp) * scale
  74. // so
  75. // espsilon_quant = epsilon_float / scale + zp
  76. // We know epsilon_float is just a very small number to avoid division by
  77. // zero error, and scale is > 1, so the integer value of epsilon for quant
  78. // is just dominated by the zero point.
  79. // Also, GetInvSqrtQuantizedMultiplierExp handles the scenario where the sum
  80. // of input value squared is zero case well.
  81. // So we don't even need to do handle the epsilon for quantized kernel case.
  82. const float epsilon = 1e-6f;
  83. if (output->type == kTfLiteFloat32) {
  84. reference_ops::L2Normalization(data, tflite::micro::GetTensorShape(input),
  85. tflite::micro::GetTensorData<float>(input),
  86. tflite::micro::GetTensorShape(output),
  87. tflite::micro::GetTensorData<float>(output),
  88. epsilon);
  89. } else if (output->type == kTfLiteUInt8) {
  90. reference_ops::L2Normalization(
  91. data, tflite::micro::GetTensorShape(input),
  92. tflite::micro::GetTensorData<uint8_t>(input),
  93. tflite::micro::GetTensorShape(output),
  94. tflite::micro::GetTensorData<uint8_t>(output));
  95. } else if (output->type == kTfLiteInt8) {
  96. const auto input_shape = tflite::micro::GetTensorShape(input);
  97. const auto output_shape = tflite::micro::GetTensorShape(output);
  98. const int trailing_dim = input_shape.DimensionsCount() - 1;
  99. const int depth =
  100. MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim);
  101. const int outer_size =
  102. MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape);
  103. reference_integer_ops::L2Normalization(
  104. data.input_zero_point, outer_size, depth,
  105. tflite::micro::GetTensorData<int8_t>(input),
  106. tflite::micro::GetTensorData<int8_t>(output));
  107. } else {
  108. TF_LITE_KERNEL_LOG(context, "Output type is %s, requires float.",
  109. TfLiteTypeGetName(output->type));
  110. return kTfLiteError;
  111. }
  112. return kTfLiteOk;
  113. }
  114. } // namespace l2norm
  115. TfLiteRegistration Register_L2NORM_REF() {
  116. return {/*init=*/l2norm::Init,
  117. /*free=*/nullptr,
  118. /*prepare=*/l2norm::Prepare,
  119. /*invoke=*/l2norm::Eval,
  120. /*profiling_string=*/nullptr,
  121. /*builtin_code=*/0,
  122. /*custom_name=*/nullptr,
  123. /*version=*/0};
  124. }
  125. TfLiteRegistration Register_L2_NORMALIZATION() { return Register_L2NORM_REF(); }
  126. } // namespace micro
  127. } // namespace ops
  128. } // namespace tflite