fix: percentile_cont interpolation causes NaN for f16 input#20208
fix: percentile_cont interpolation causes NaN for f16 input#20208alamb merged 5 commits intoapache:mainfrom
Conversation
Jefffrey
left a comment
There was a problem hiding this comment.
• Remove the unused interpolation precision constant and associated wrapping-math code paths/comments.
Could we please ensure we keep the PR body consistent with the latest changes
| /// is computed as: `lower + ((upper - lower) * (fraction * PRECISION)) / PRECISION` | ||
| /// to avoid floating-point operations on integer types while maintaining precision. | ||
| /// Interpolation is performed in f64 and then cast back to the native type to | ||
| /// avoid overflowing Float16 intermediates. |
There was a problem hiding this comment.
I'd still prefer we keep the old documentation as it was and just add on any amendments; I don't like how it reads about a "previous implementation" considering there is no information about such implementation (without looking at the git history)
| .div_wrapping(T::Native::usize_as(INTERPOLATION_PRECISION)), | ||
| ); | ||
| Some(interpolated) | ||
| let scaled = (fraction * INTERPOLATION_PRECISION as f64) as usize; |
There was a problem hiding this comment.
If we always cast INTERPOLATION_PRECISION to f64 at all its usages we should just define it as f64
| // Linear interpolation. | ||
| // | ||
| // We quantize the fractional component (via `INTERPOLATION_PRECISION`) to | ||
| // minimize output changes for Float32/Float64 compared to the previous |
There was a problem hiding this comment.
What previous implementation are we referring to here?
|
Thanks @kumarUjjawal and @Jefffrey |
…0208) ## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes apache#123` indicates that this PR will close issue apache#123. --> - Closes apache#18945 ## Rationale for this change percentile_cont interpolation for Float16 could overflow f16 intermediates (e.g. when scaling the fractional component), producing inf/NaN and incorrect results. This PR makes interpolation numerically safe for f16. <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? • Perform percentile interpolation in f64 and cast back to the input float type (f16/f32/f64) to avoid f16 overflow. • Add a regression unit test covering Float16 interpolation near the maximum finite value. <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? Yes <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? Yes. percentile_cont on Float16 inputs no longer returns NaN due to interpolation overflow and will produce correct finite results for valid finite f16 data <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
Which issue does this PR close?
NaNfor f16 input #18945Rationale for this change
percentile_cont interpolation for Float16 could overflow f16 intermediates (e.g. when scaling the fractional component), producing inf/NaN and incorrect results. This PR makes interpolation numerically safe for f16.
What changes are included in this PR?
• Perform percentile interpolation in f64 and cast back to the input float type (f16/f32/f64) to avoid f16 overflow.
• Add a regression unit test covering Float16 interpolation near the maximum finite value.
Are these changes tested?
Yes
Are there any user-facing changes?
Yes. percentile_cont on Float16 inputs no longer returns NaN due to interpolation overflow and will produce correct
finite results for valid finite f16 data