dequantize.cc 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/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. #include "tensorflow/lite/micro/kernels/dequantize.h"
  21. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  22. #include "tensorflow/lite/micro/micro_error_reporter.h"
  23. namespace tflite {
  24. void* DequantizeInit(TfLiteContext* context, const char* buffer,
  25. size_t length) {
  26. TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
  27. return context->AllocatePersistentBuffer(context, sizeof(DequantizeOpData));
  28. }
  29. TfLiteStatus DequantizeEval(TfLiteContext* context, TfLiteNode* node) {
  30. TFLITE_DCHECK(node->user_data != nullptr);
  31. DequantizeOpData* data = static_cast<DequantizeOpData*>(node->user_data);
  32. const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0);
  33. TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0);
  34. if (output->type == kTfLiteFloat32) {
  35. switch (input->type) {
  36. case kTfLiteInt8:
  37. reference_ops::Dequantize(data->quantization_params,
  38. tflite::micro::GetTensorShape(input),
  39. tflite::micro::GetTensorData<int8_t>(input),
  40. tflite::micro::GetTensorShape(output),
  41. tflite::micro::GetTensorData<float>(output));
  42. break;
  43. case kTfLiteInt16:
  44. reference_ops::Dequantize(data->quantization_params,
  45. tflite::micro::GetTensorShape(input),
  46. tflite::micro::GetTensorData<int16_t>(input),
  47. tflite::micro::GetTensorShape(output),
  48. tflite::micro::GetTensorData<float>(output));
  49. break;
  50. case kTfLiteUInt8:
  51. reference_ops::Dequantize(data->quantization_params,
  52. tflite::micro::GetTensorShape(input),
  53. tflite::micro::GetTensorData<uint8_t>(input),
  54. tflite::micro::GetTensorShape(output),
  55. tflite::micro::GetTensorData<float>(output));
  56. break;
  57. default:
  58. MicroPrintf("Input %s, output %s not supported.",
  59. TfLiteTypeGetName(input->type),
  60. TfLiteTypeGetName(output->type));
  61. return kTfLiteError;
  62. }
  63. } else {
  64. MicroPrintf("Input %s, output %s not supported.",
  65. TfLiteTypeGetName(input->type),
  66. TfLiteTypeGetName(output->type));
  67. return kTfLiteError;
  68. }
  69. return kTfLiteOk;
  70. }
  71. TfLiteRegistration Register_DEQUANTIZE() {
  72. return tflite::micro::RegisterOp(DequantizeInit, DequantizePrepare,
  73. DequantizeEval);
  74. }
  75. } // namespace tflite