quantize.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_QUANTIZE_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_QUANTIZE_H_
  14. #include <algorithm>
  15. #include <limits>
  16. #include <vector>
  17. #include "tensorflow/lite/kernels/internal/common.h"
  18. #include "tensorflow/lite/kernels/internal/compatibility.h"
  19. #include "tensorflow/lite/kernels/internal/cppmath.h"
  20. #include "tensorflow/lite/kernels/internal/types.h"
  21. namespace tflite {
  22. namespace reference_ops {
  23. template <typename InputT, typename OutputT>
  24. inline void AffineQuantize(const tflite::QuantizationParams& op_params,
  25. const RuntimeShape& input_shape,
  26. const InputT* input_data,
  27. const RuntimeShape& output_shape,
  28. OutputT* output_data) {
  29. const int32_t zero_point = op_params.zero_point;
  30. const double scale = op_params.scale;
  31. const int flat_size = MatchingFlatSize(input_shape, output_shape);
  32. static constexpr int32_t min_val = std::numeric_limits<OutputT>::min();
  33. static constexpr int32_t max_val = std::numeric_limits<OutputT>::max();
  34. for (int i = 0; i < flat_size; i++) {
  35. const InputT val = input_data[i];
  36. int32_t unclamped =
  37. static_cast<int32_t>(TfLiteRound(val / static_cast<float>(scale))) +
  38. zero_point;
  39. int32_t clamped = std::min(std::max(unclamped, min_val), max_val);
  40. output_data[i] = clamped;
  41. }
  42. }
  43. // Quantizes per-channel.
  44. template <typename InputT, typename OutputT>
  45. inline void PerChannelQuantize(
  46. const tflite::PerChannelQuantizationParams& op_params,
  47. const RuntimeShape& input_shape, const InputT* input_data,
  48. const RuntimeShape& output_shape, OutputT* output_data) {
  49. // Ensure flat size is same.
  50. MatchingFlatSize(input_shape, output_shape);
  51. const int32_t* zero_point = op_params.zero_point;
  52. const float* scale = op_params.scale;
  53. const int32_t quantized_dimension = op_params.quantized_dimension;
  54. const int32_t num_dims = input_shape.DimensionsCount();
  55. const int32_t* dims_data = input_shape.DimsData();
  56. std::vector<int> current_dim(num_dims, 0);
  57. static constexpr int32_t min_val = std::numeric_limits<OutputT>::min();
  58. static constexpr int32_t max_val = std::numeric_limits<OutputT>::max();
  59. do {
  60. size_t offset =
  61. ReducedOutputOffset(num_dims, reinterpret_cast<const int*>(dims_data),
  62. current_dim.data(), 0, nullptr);
  63. const InputT val = input_data[offset];
  64. const int channel = current_dim[quantized_dimension];
  65. int32_t unclamped = static_cast<int32_t>(TfLiteRound(
  66. val / static_cast<float>(scale[channel]))) +
  67. zero_point[channel];
  68. int32_t clamped = std::min(std::max(unclamped, min_val), max_val);
  69. output_data[offset] = static_cast<OutputT>(clamped);
  70. } while (NextIndex(num_dims, reinterpret_cast<const int*>(dims_data),
  71. current_dim.data()));
  72. }
  73. } // namespace reference_ops
  74. } // namespace tflite
  75. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_QUANTIZE_H_