Skip to content

GH-12681: [C++] Work around FP rounding issues in quantile tests - #55744

Merged
pitrou merged 1 commit into
apache:mainfrom
pitrou:gh12681-quantile-precision
Nov 24, 2026
Merged

pitrou merged 1 commit into
apache:mainfrom
pitrou:gh12681-quantile-precision

Conversation

@pitrou

@pitrou pitrou commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Rationale for this change

On some architectures (such as Risc-V) and/or with some optimizations enabled (such as -fma on x86), the quantile compute kernel may not output results rigorously equal to those from a textbook quantile computation as used in the tests.

Thanks to PukNgae Cryolitia (GH id Cryolitia) for diagnosing the issue and suggesting the fix.

What changes are included in this PR?

Allow for some slight divergence (1 ULP) between the results.

Are these changes tested?

By definition.

Are there any user-facing changes?

No.

Was AI used for this PR?

In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

On some architectures (such as Risc-V) and/or with some optimizations enabled (such as `-fma` on x86), the quantile compute kernel may not output results rigorously equal to those from a textbook quantile computation as used in the tests.

Allow for some slight divergence (1 ULP) between the results.
@pitrou

pitrou commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

FTR @andishgar :)

@pitrou

pitrou commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Also cc @cyb70289 @lidavidm

@cyb70289 cyb70289 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@pitrou
pitrou merged commit 391be25 into apache:main Nov 24, 2026
63 of 86 checks passed
@pitrou
pitrou deleted the gh12681-quantile-precision branch November 24, 2026 08:02
@andishgar

Copy link
Copy Markdown
Contributor

@ptrou A few notes:

  1. If this PR is related to an existing issue, I think it would be helpful to include a link to the original issue in the PR description. This would also make it clear where the potential one-ULP error in the current implementation was identified.

  2. I think we should clarify which interpolation methods are expected to have one-ULP accuracy: LINEAR, MIDPOINT, or both.

  3. If the compiler can optimize the following operations into FMA instructions, would it be better to use std::fma and std::midpoint directly instead?

if (interpolation == QuantileOptions::LINEAR) {
// more stable than naive linear interpolation
return fraction * higher_value + (1 - fraction) * lower_value;
} else if (interpolation == QuantileOptions::MIDPOINT) {
return lower_value / 2 + higher_value / 2;
} else {
DCHECK(false);
return NAN;
}
}
};

@Cryolitia Cryolitia left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirm that this PR fixes the build on Arch Linux RISC-V, and x86_64 in general with the -mfma parameter (although it may be a little late)

Tested-by: Cryolitia PukNgae <cryolitia.pukngae@linux.dev>

Comment on lines +3852 to +3854
// The results may not be rigorously equal to the "naive" results
// on some architecture and/or with some compiler flags (GH-12681).
AssertWithinUlp(quantiles[j], numeric_scalar->value, 1);

@andishgar andishgar Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pitrou , I forgot to mention one more thing: ULP-based comparison is inappropriate for LOWER, HIGHER, and NEAREST, since these interpolation methods are expected to return an unchanged input value.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it's not inappropriate, but you're right it's less strict than ideal :)

@pitrou

pitrou commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

@andishgar

  • I think we should clarify which interpolation methods are expected to have one-ULP accuracy: LINEAR, MIDPOINT, or both.

Well, I don't think we want to guarantee accuracy, because it can vary depending on the exact algorithm but also the machine/compiler/optimization options.

  • If the compiler can optimize the following operations into FMA instructions, would it be better to use std::fma and std::midpoint directly instead?

I don't know, that sounds a bit overkill to me. I'll let others discuss this if there is a desire to do so :)

felixonmars pushed a commit to felixonmars/archriscv-packages that referenced this pull request Nov 26, 2026
Backport the upstream quantile test fix to allow 1 ULP rounding
differences from FMA contraction. Replace the flag-stripping
workaround so _GLIBCXX_ASSERTIONS stays enabled for the build.

Refresh the PKGBUILD overlay and retain the 600-second CTest timeout.

All 345 aggregate tests pass with assertions enabled on riscv64 under
QEMU with LTO and on x86_64 with -mfma; the unpatched builds fail the
same four tests. Overlay application, prepare() and the upstream
patch checksum were also validated.

apache/arrow#55744
apache/arrow@391be25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants