logistic.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_LOGISTIC_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LOGISTIC_H_
  14. #include <cmath>
  15. #include "fixedpoint/fixedpoint.h"
  16. #include "tensorflow/lite/kernels/internal/common.h"
  17. #include "tensorflow/lite/kernels/internal/cppmath.h"
  18. #include "tensorflow/lite/kernels/internal/quantization_util.h"
  19. #include "tensorflow/lite/kernels/internal/types.h"
  20. #include "tensorflow/lite/kernels/op_macros.h"
  21. namespace tflite {
  22. namespace reference_ops {
  23. inline void Logistic(const RuntimeShape& input_shape, const float* input_data,
  24. const RuntimeShape& output_shape, float* output_data) {
  25. const float cutoff_upper = 16.619047164916992188f;
  26. const float cutoff_lower = -9.f;
  27. const int flat_size = MatchingFlatSize(input_shape, output_shape);
  28. // Rational for using approximation in reference kernel.
  29. // 0. This approximation gives enough precision for float.
  30. // 1. This works around an issue on an embedded chipset where exp() does not
  31. // return correctly as expected - exp(x) should return inf when overflown
  32. // not 1.701417 IEEE 754 defines representation for inf.
  33. // 2. This will speed up calculation and is matching the behavior in the
  34. // optimized kernels. (check the definition of scalar_logistic_op<float>)
  35. for (int i = 0; i < flat_size; i++) {
  36. float val = input_data[i];
  37. float result;
  38. if (val > cutoff_upper) {
  39. result = 1.0f;
  40. } else if (val < cutoff_lower) {
  41. result = std::exp(val);
  42. } else {
  43. result = 1.f / (1.f + std::exp(-val));
  44. }
  45. output_data[i] = result;
  46. }
  47. }
  48. // Convenience version that allows, for example, generated-code calls to be
  49. // uniform between data types.
  50. inline void Logistic(const LogisticParams&, const RuntimeShape& input_shape,
  51. const float* input_data, const RuntimeShape& output_shape,
  52. float* output_data) {
  53. // Drop params: not needed.
  54. Logistic(input_shape, input_data, output_shape, output_data);
  55. }
  56. inline void Logistic(const LogisticParams& params,
  57. const RuntimeShape& input_shape, const int16_t* input_data,
  58. const RuntimeShape& output_shape, int16_t* output_data) {
  59. const int flat_size = MatchingFlatSize(input_shape, output_shape);
  60. for (int i = 0; i < flat_size; i++) {
  61. // F0 uses 0 integer bits, range [-1, 1].
  62. // This is the return type of math functions such as tanh, logistic,
  63. // whose range is in [-1, 1].
  64. using F0 = gemmlowp::FixedPoint<std::int16_t, 0>;
  65. // F3 uses 3 integer bits, range [-8, 8], the input range expected here.
  66. using F3 = gemmlowp::FixedPoint<std::int16_t, 3>;
  67. const F3 input = F3::FromRaw(input_data[i]);
  68. F0 output = gemmlowp::logistic(input);
  69. output_data[i] = output.raw();
  70. }
  71. }
  72. // Quantized int8_t logistic activation. Cheats by dequantizing and
  73. // requantizing around the floating point logistic method. This implementation
  74. // is slow on platforms without a floating point unit.
  75. // TODO(b/141211002): Delete this int8_t implementation once we can reuse the
  76. // approach used in TFLite for int8_t Logistic.
  77. inline void Logistic(const RuntimeShape& input_shape, const int8_t* input_data,
  78. float input_scale, int input_zero_point,
  79. const RuntimeShape& output_shape, int8_t* output_data,
  80. float output_scale, int output_zero_point) {
  81. const float cutoff_upper = 16.619047164916992188f;
  82. const float cutoff_lower = -9.f;
  83. const int flat_size = MatchingFlatSize(input_shape, output_shape);
  84. // Rational for using approximation in reference kernel.
  85. // 0. This approximation gives enough precision for float.
  86. // 1. This works around an issue on an embedded chipset where exp() does not
  87. // return correctly as expected - exp(x) should return inf when overflown
  88. // not 1.701417 IEEE 754 defines representation for inf.
  89. // 2. This will speed up calculation and is matching the behavior in the
  90. // optimized kernels. (check the definition of scalar_logistic_op<float>)
  91. for (int i = 0; i < flat_size; i++) {
  92. // Dequantize.
  93. float val =
  94. static_cast<float>((input_data[i] - input_zero_point) * input_scale);
  95. float result;
  96. if (val > cutoff_upper) {
  97. result = 1.0f;
  98. } else if (val < cutoff_lower) {
  99. result = std::exp(val);
  100. } else {
  101. result = 1.f / (1.f + std::exp(-val));
  102. }
  103. // Requantize
  104. int8_t output =
  105. static_cast<int8_t>(result / output_scale + output_zero_point);
  106. output_data[i] = output;
  107. }
  108. }
  109. } // namespace reference_ops
  110. } // namespace tflite
  111. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LOGISTIC_H_