l2norm.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/portable_tensor.h"
  14. #include "tensorflow/lite/kernels/internal/reference/integer_ops/l2normalization.h"
  15. #include "tensorflow/lite/kernels/internal/reference/l2normalization.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. MicroContext* micro_context = GetMicroContext(context);
  40. TfLiteTensor* input =
  41. micro_context->AllocateTempInputTensor(node, kInputTensor);
  42. TF_LITE_ENSURE(context, input != nullptr);
  43. TfLiteTensor* output =
  44. micro_context->AllocateTempOutputTensor(node, kOutputTensor);
  45. TF_LITE_ENSURE(context, output != nullptr);
  46. TF_LITE_ENSURE(context, NumDimensions(input) <= 4);
  47. TF_LITE_ENSURE(context,
  48. output->type == kTfLiteFloat32 || output->type == kTfLiteInt8);
  49. TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type);
  50. if (output->type == kTfLiteInt8) {
  51. data->input_zero_point = input->params.zero_point;
  52. } else if (output->type == kTfLiteFloat32) {
  53. data->input_zero_point = 0;
  54. }
  55. // Our implementations don't currently support activations.
  56. TF_LITE_ENSURE_EQ(context, params->activation, kTfLiteActNone);
  57. micro_context->DeallocateTempTfLiteTensor(input);
  58. micro_context->DeallocateTempTfLiteTensor(output);
  59. return kTfLiteOk;
  60. }
  61. void* Init(TfLiteContext* context, const char* buffer, size_t length) {
  62. TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
  63. return context->AllocatePersistentBuffer(context,
  64. sizeof(L2NormalizationParams));
  65. }
  66. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  67. TFLITE_DCHECK(node->user_data != nullptr);
  68. const L2NormalizationParams& data =
  69. *(static_cast<const L2NormalizationParams*>(node->user_data));
  70. const TfLiteEvalTensor* input =
  71. tflite::micro::GetEvalInput(context, node, kInputTensor);
  72. TfLiteEvalTensor* output =
  73. tflite::micro::GetEvalOutput(context, node, kOutputTensor);
  74. // TODO(b/143912164): instead of hardcode the epsilon here, we should read it
  75. // from tensorflow, i.e., adding a params.
  76. // We don't compute epsilon for quantized kernel:
  77. //
  78. // epsilon_float = (epsilon_quant - zp) * scale
  79. // so
  80. // espsilon_quant = epsilon_float / scale + zp
  81. // We know epsilon_float is just a very small number to avoid division by
  82. // zero error, and scale is > 1, so the integer value of epsilon for quant
  83. // is just dominated by the zero point.
  84. // Also, GetInvSqrtQuantizedMultiplierExp handles the scenario where the sum
  85. // of input value squared is zero case well.
  86. // So we don't even need to do handle the epsilon for quantized kernel case.
  87. const float epsilon = 1e-6f;
  88. if (output->type == kTfLiteFloat32) {
  89. reference_ops::L2Normalization(data, tflite::micro::GetTensorShape(input),
  90. tflite::micro::GetTensorData<float>(input),
  91. tflite::micro::GetTensorShape(output),
  92. tflite::micro::GetTensorData<float>(output),
  93. epsilon);
  94. } else if (output->type == kTfLiteInt8) {
  95. const auto input_shape = tflite::micro::GetTensorShape(input);
  96. const auto output_shape = tflite::micro::GetTensorShape(output);
  97. const int trailing_dim = input_shape.DimensionsCount() - 1;
  98. const int depth =
  99. MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim);
  100. const int outer_size =
  101. MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape);
  102. reference_integer_ops::L2Normalization(
  103. data.input_zero_point, outer_size, depth,
  104. tflite::micro::GetTensorData<int8_t>(input),
  105. tflite::micro::GetTensorData<int8_t>(output));
  106. } else {
  107. TF_LITE_KERNEL_LOG(context, "Output type is %s, requires float.",
  108. TfLiteTypeGetName(output->type));
  109. return kTfLiteError;
  110. }
  111. return kTfLiteOk;
  112. }
  113. } // namespace l2norm
  114. TfLiteRegistration Register_L2NORM_REF() {
  115. return tflite::micro::RegisterOp(l2norm::Init, l2norm::Prepare, l2norm::Eval);
  116. }
  117. TfLiteRegistration Register_L2_NORMALIZATION() { return Register_L2NORM_REF(); }
  118. } // namespace micro
  119. } // namespace ops
  120. } // namespace tflite