dequantize.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_DEQUANTIZE_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_DEQUANTIZE_H_
  14. #include "tflite/kernels/internal/common.h"
  15. #include "tflite/kernels/internal/types.h"
  16. namespace tflite {
  17. namespace reference_integer_ops {
  18. template <typename T>
  19. inline void Dequantize(const tflite::DequantizationParams& op_params,
  20. const RuntimeShape& input_shape, const T* input_data,
  21. const RuntimeShape& output_shape, float* output_data) {
  22. const int32 zero_point = op_params.zero_point;
  23. const double scale = op_params.scale;
  24. const int flat_size = MatchingFlatSize(input_shape, output_shape);
  25. for (int i = 0; i < flat_size; i++) {
  26. const int32 val = static_cast<int32>(input_data[i]);
  27. const float result = static_cast<float>(scale * (val - zero_point));
  28. output_data[i] = result;
  29. }
  30. }
  31. } // namespace reference_integer_ops
  32. } // namespace tflite
  33. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_DEQUANTIZE_H_