Gymnastics with nudged zero point is to ensure that real zero maps to an integer, which is required for e.g. zero-padding in convolutional layers. Outputs nudged_min, nudged_max, nudged_scale.
| 39 | // an integer, which is required for e.g. zero-padding in convolutional layers. |
| 40 | // Outputs nudged_min, nudged_max, nudged_scale. |
| 41 | EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void Nudge( |
| 42 | const float min, const float max, const int quant_min, const int quant_max, |
| 43 | float* nudged_min, float* nudged_max, float* scale) { |
| 44 | const float quant_min_float = static_cast<float>(quant_min); |
| 45 | const float quant_max_float = static_cast<float>(quant_max); |
| 46 | *scale = (max - min) / (quant_max_float - quant_min_float); |
| 47 | const float zero_point_from_min = quant_min_float - min / *scale; |
| 48 | const uint16 nudged_zero_point = [zero_point_from_min, quant_min, |
| 49 | quant_min_float, quant_max, |
| 50 | quant_max_float] { |
| 51 | if (zero_point_from_min < quant_min_float) { |
| 52 | return static_cast<uint16>(quant_min); |
| 53 | } |
| 54 | if (zero_point_from_min > quant_max_float) { |
| 55 | return static_cast<uint16>(quant_max); |
| 56 | } |
| 57 | return static_cast<uint16>(StdRound(zero_point_from_min)); |
| 58 | }(); |
| 59 | *nudged_min = (quant_min_float - nudged_zero_point) * (*scale); |
| 60 | *nudged_max = (quant_max_float - nudged_zero_point) * (*scale); |
| 61 | } |
| 62 | |
| 63 | template <typename T> |
| 64 | using ConstScalar = typename tensorflow::TTypes<T>::ConstScalar; |
no test coverage detected