dequantize.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Copyright 2018 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/kernels/internal/reference/dequantize.h"
  13. #include "tensorflow/lite/c/builtin_op_data.h"
  14. #include "tensorflow/lite/c/common.h"
  15. #include "tensorflow/lite/kernels/internal/quantization_util.h"
  16. #include "tensorflow/lite/kernels/internal/reference/quantize.h"
  17. #include "tensorflow/lite/kernels/internal/reference/requantize.h"
  18. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  19. #include "tensorflow/lite/kernels/kernel_util.h"
  20. namespace tflite {
  21. namespace ops {
  22. namespace micro {
  23. namespace dequantize {
  24. TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
  25. TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
  26. TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
  27. // TODO(b/140515557): Add cached dequant to improve hybrid model performance.
  28. const TfLiteTensor* input = GetInput(context, node, 0);
  29. TfLiteTensor* output = GetOutput(context, node, 0);
  30. TF_LITE_ENSURE(context, input->type == kTfLiteUInt8 ||
  31. input->type == kTfLiteInt8 ||
  32. input->type == kTfLiteInt16);
  33. TF_LITE_ENSURE(
  34. context, output->type == kTfLiteFloat32 || output->type == kTfLiteInt32);
  35. return kTfLiteOk;
  36. }
  37. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  38. const TfLiteTensor* input = GetInput(context, node, 0);
  39. TfLiteTensor* output = GetOutput(context, node, 0);
  40. if (output->type == kTfLiteFloat32) {
  41. tflite::DequantizationParams op_params;
  42. op_params.zero_point = input->params.zero_point;
  43. op_params.scale = static_cast<double>(input->params.scale);
  44. switch (input->type) {
  45. case kTfLiteUInt8:
  46. reference_ops::Dequantize(
  47. op_params, GetTensorShape(input), GetTensorData<uint8_t>(input),
  48. GetTensorShape(output), GetTensorData<float>(output));
  49. break;
  50. case kTfLiteInt8:
  51. reference_ops::Dequantize(
  52. op_params, GetTensorShape(input), GetTensorData<int8_t>(input),
  53. GetTensorShape(output), GetTensorData<float>(output));
  54. break;
  55. case kTfLiteInt16:
  56. reference_ops::Dequantize(
  57. op_params, GetTensorShape(input), GetTensorData<int16_t>(input),
  58. GetTensorShape(output), GetTensorData<float>(output));
  59. break;
  60. default:
  61. TF_LITE_KERNEL_LOG(context, "Input %s, output %s not supported.",
  62. TfLiteTypeGetName(input->type),
  63. TfLiteTypeGetName(output->type));
  64. return kTfLiteError;
  65. }
  66. } else if (output->type == kTfLiteInt32) {
  67. int32_t output_multiplier;
  68. int output_shift;
  69. const double effective_output_scale =
  70. static_cast<double>(input->params.scale) /
  71. static_cast<double>(output->params.scale);
  72. QuantizeMultiplier(effective_output_scale, &output_multiplier,
  73. &output_shift);
  74. int flat_size =
  75. MatchingFlatSize(GetTensorShape(input), GetTensorShape(output));
  76. switch (input->type) {
  77. case kTfLiteInt16: {
  78. reference_ops::Requantize(
  79. GetTensorData<int16_t>(input), flat_size, output_multiplier,
  80. output_shift, input->params.zero_point, output->params.zero_point,
  81. GetTensorData<int32_t>(output));
  82. break;
  83. }
  84. case kTfLiteInt8: {
  85. reference_ops::Requantize(
  86. GetTensorData<int8_t>(input), flat_size, output_multiplier,
  87. output_shift, input->params.zero_point, output->params.zero_point,
  88. GetTensorData<int32_t>(output));
  89. break;
  90. }
  91. default:
  92. TF_LITE_KERNEL_LOG(context, "Input %s, output %s not supported.",
  93. TfLiteTypeGetName(input->type),
  94. TfLiteTypeGetName(output->type));
  95. return kTfLiteError;
  96. }
  97. } else {
  98. TF_LITE_KERNEL_LOG(context, "Input %s, output %s not supported.",
  99. TfLiteTypeGetName(input->type),
  100. TfLiteTypeGetName(output->type));
  101. return kTfLiteError;
  102. }
  103. return kTfLiteOk;
  104. }
  105. } // namespace dequantize
  106. TfLiteRegistration* Register_DEQUANTIZE() {
  107. static TfLiteRegistration r = {/*init=*/nullptr,
  108. /*free=*/nullptr,
  109. /*prepare=*/dequantize::Prepare,
  110. /*invoke=*/dequantize::Eval,
  111. /*profiling_string=*/nullptr,
  112. /*builtin_code=*/0,
  113. /*custom_name=*/nullptr,
  114. /*version=*/0};
  115. return &r;
  116. }
  117. } // namespace micro
  118. } // namespace ops
  119. } // namespace tflite