dequantize_common.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* Copyright 2021 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/builtin_op_data.h"
  13. #include "tensorflow/lite/c/common.h"
  14. #include "tensorflow/lite/kernels/internal/quantization_util.h"
  15. #include "tensorflow/lite/kernels/internal/reference/dequantize.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. #include "tensorflow/lite/micro/kernels/dequantize.h"
  21. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  22. namespace tflite {
  23. TfLiteStatus DequantizePrepare(TfLiteContext* context, TfLiteNode* node) {
  24. TFLITE_DCHECK(node->user_data != nullptr);
  25. DequantizeOpData* data = static_cast<DequantizeOpData*>(node->user_data);
  26. TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
  27. TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
  28. MicroContext* micro_context = GetMicroContext(context);
  29. // TODO(b/140515557): Add cached dequant to improve hybrid model performance.
  30. TfLiteTensor* input = micro_context->AllocateTempInputTensor(node, 0);
  31. TF_LITE_ENSURE(context, input != nullptr);
  32. TfLiteTensor* output = micro_context->AllocateTempOutputTensor(node, 0);
  33. TF_LITE_ENSURE(context, output != nullptr);
  34. TF_LITE_ENSURE(context,
  35. input->type == kTfLiteInt8 || input->type == kTfLiteInt16);
  36. TF_LITE_ENSURE(context, output->type == kTfLiteFloat32);
  37. if (output->type == kTfLiteInt32) {
  38. const double effective_output_scale =
  39. static_cast<double>(input->params.scale) /
  40. static_cast<double>(output->params.scale);
  41. QuantizeMultiplier(effective_output_scale, &data->output_multiplier,
  42. &data->output_shift);
  43. }
  44. data->quantization_params.zero_point = input->params.zero_point;
  45. data->quantization_params.scale = static_cast<double>(input->params.scale);
  46. data->output_zero_point = output->params.zero_point;
  47. micro_context->DeallocateTempTfLiteTensor(input);
  48. micro_context->DeallocateTempTfLiteTensor(output);
  49. return kTfLiteOk;
  50. }
  51. } // namespace tflite