dequantize.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Copyright 2019 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. #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEQUANTIZE_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEQUANTIZE_H_
  14. #include <limits.h>
  15. #include <vector>
  16. #include "tensorflow/lite/kernels/internal/common.h"
  17. #include "tensorflow/lite/kernels/internal/types.h"
  18. namespace tflite {
  19. namespace reference_ops {
  20. // Dequantizes into a float without rounding.
  21. template <typename InputT, typename OutputT>
  22. inline void Dequantize(const tflite::DequantizationParams& op_params,
  23. const RuntimeShape& input_shape,
  24. const InputT* input_data,
  25. const RuntimeShape& output_shape, OutputT* output_data) {
  26. int32_t zero_point = op_params.zero_point;
  27. const double scale = op_params.scale;
  28. const int flat_size = MatchingFlatSize(input_shape, output_shape);
  29. for (int i = 0; i < flat_size; i++) {
  30. const int32_t val = input_data[i];
  31. const OutputT result = static_cast<OutputT>(scale * (val - zero_point));
  32. output_data[i] = result;
  33. }
  34. }
  35. // Dequantizes per-channel quantized tensor to float.
  36. template <typename T>
  37. inline void PerChannelDequantize(
  38. const tflite::PerChannelDequantizationParams& op_params,
  39. const RuntimeShape& input_shape, const T* input_data,
  40. const RuntimeShape& output_shape, float* output_data) {
  41. // Ensure flat size is same.
  42. MatchingFlatSize(input_shape, output_shape);
  43. const int32_t* zero_point = op_params.zero_point;
  44. const float* scale = op_params.scale;
  45. const int32_t quantized_dimension = op_params.quantized_dimension;
  46. const int32_t num_dims = input_shape.DimensionsCount();
  47. const int32_t* dims_data = input_shape.DimsData();
  48. std::vector<int> current_dim(num_dims, 0);
  49. do {
  50. size_t offset =
  51. ReducedOutputOffset(num_dims, reinterpret_cast<const int*>(dims_data),
  52. current_dim.data(), 0, nullptr);
  53. const int channel = current_dim[quantized_dimension];
  54. const int32_t val = input_data[offset];
  55. const float result =
  56. static_cast<float>(scale[channel] * (val - zero_point[channel]));
  57. output_data[offset] = result;
  58. } while (NextIndex(num_dims, reinterpret_cast<const int*>(dims_data),
  59. current_dim.data()));
  60. }
  61. } // namespace reference_ops
  62. } // namespace tflite
  63. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEQUANTIZE_H_