Repository navigation
[Cherry-Pick][Operator Mechanism] Reject empty reduction dim and oversized input in quantile- #79842 - #79852
Conversation
paddle.quantile / paddle.nanquantile silently returned a meaningless value for inputs that torch rejects, so they diverged from torch under PaddleAPITest comparison. Root cause is in _compute_quantile: the reduction-dim length feeds `last_index = x.shape[axis] - 1` and the rank `q*(n-1)` computed in x.dtype. When the reduction dim is empty, last_index becomes -1 and the gathered rank is negative, yielding a bogus result (e.g. 0.0). When the reduction dim exceeds 2**24, float32 can no longer represent the integer rank exactly, so take_along_axis gathers a wrong index and the quantile is numerically off for data with large gaps between sorted neighbors. Add two guards keyed on x.shape[axis] (the exact quantity feeding last_index), matching torch's non-empty and <=2**24 checks. The check is on the reduction dim rather than total numel, so legitimate empty-batch reductions such as quantile([2,0,3], axis=0) that align with numpy keep working. Test Plan: python -m pytest test/legacy_test/test_quantile_and_nanquantile.py Added assertRaises(ValueError) cases in TestError for empty-reduction quantile/nanquantile and for an input whose reduction dim exceeds 2**24. (cherry picked from commit 9d535c1)
The oversized-input guard added for quantile/nanquantile used a fixed
2**24 threshold, but the rank `q*(n-1)` and `last_index` are computed in
x.dtype and the gathered index is cast to int32. The fixed threshold is
wrong for every dtype except float32:
- float64 keeps producing exact results far past 2**24 (verified up to
tens of millions), so the fixed limit rejected legitimate inputs and
regressed documented float64 support.
- float16 / bfloat16 lose integer-rank precision well below 2**24
(2**11 / 2**8), so the fixed limit let silently-wrong results through.
Key the limit on x.dtype (2**(mantissa_bits+1) per dtype) and cap it by
the int32 gather-index range (2**31). quantile and nanquantile share this
single guard in _compute_quantile, so both are covered.
Add TestError cases rejecting oversized float16 / bfloat16 reductions and
a regression test confirming a float64 reduction dim above 2**24 is no
longer rejected and stays numerically exact.
(cherry picked from commit 118c3fc)
Replace per-dtype input size guards with a root-cause fix: promote the rank computation q*(n-1) from x.dtype to float64, which keeps integer precision up to 2^53 and removes the need for dtype-specific rejection thresholds (2^8/2^11/2^24). The empty-reduction guard is retained. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> (cherry picked from commit 3c023fa)
…lignment - Add 2^53 guard for float64 rank precision limit - Change gather index dtype from int32 to int64 to avoid overflow when reduction dim exceeds 2^31 - Align midpoint interpolation with PyTorch: use lerp(a, b, 0.5) instead of (a + b) / 2 to eliminate 1-ulp rounding differences (cherry picked from commit d7499ea)
Paddle-Bot Review Board (review完成)
Powered by Nyanpasu claude with Opus 4.8 默认推理级别, please check the suggestions carefully. |
Paddle-Review-Bot
left a comment
There was a problem hiding this comment.
本 PR 为向
release/3.4 的 cherry-pick,对应 develop 分支 PR #79842 目前仍处于 open 未合入状态。release 分支的 cherry-pick 通常应在源 develop PR 合入后再推进;若 #79842 在后续评审中调整实现(如错误信息文案、2**53 守卫阈值、midpoint 插值写法),本 cherry-pick 可能与最终合入版本产生偏差。建议待 #79842 合入后再推进本 PR,或保持两者同步。代码改动本身(float64 rank 精度提升、int64 gather 索引、lerp midpoint 对齐、空归约维守卫)正确且测试完备,无其他问题。
Codecov Report❌ Patch coverage is
❌ Your patch status has failed because the patch coverage (88.88%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## release/3.4 #79852 +/- ##
==============================================
Coverage ? 88.88%
==============================================
Files ? 1
Lines ? 9
Branches ? 0
==============================================
Hits ? 8
Misses ? 1
Partials ? 0 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
PR Category
Operator Mechanism
PR Types
Bug fixes
Description
本 PR 修复
paddle.quantile/paddle.nanquantile在 develop 分支上存在的四个问题:rank 计算精度不足、gather 索引溢出、midpoint 插值与 PyTorch 逐位不一致、空归约维缺少守卫。所有改动集中在_compute_quantile函数(python/paddle/tensor/stat.py)。1. rank 计算精度不足:float32 输入在归约维 > 2^24 时结果静默错误
develop 现状
q_num = paddle.to_tensor(q_num, dtype=x.dtype)强制在x.dtype下计算 rankq*(n-1)。float32 仅 23 位尾数,超过 2^24 的整数无法精确表示,rank 算错 → gather 取到错误元素 → 结果静默错误,无报错。float16/bfloat16 的精度上限更低(2^11/2^8)。PyTorch(commitf66303fef70)的做法是把 rank 计算提升到 float64,不拦截。修改内容
将
dtype=x.dtype改为dtype=paddle.float64。rank 在 float64 下精确到 2^53,覆盖所有实际场景。_compute_index中 weights 已有.astype(x.dtype)对齐回输入精度,无需额外改动。同时新增 2^53 上限守卫(float64 精度极限,物理上不可能触达,仅作防御)。2. gather 索引 int32 溢出:归约维 ≥ 2^31 时索引 cast 溢出
develop 现状
_compute_index中 3 处.astype(paddle.int32)将浮点 rank cast 为 int32 传给paddle.take_along_axis。当归约维 ≥ 2^31 时 int32 溢出,索引变为负数或乱值,gather 取到错误元素。PyTorch 使用 int64 索引,无此限制。修改内容
将 3 处
.astype(paddle.int32)改为.astype(paddle.int64)。paddle.take_along_axis已支持 int64 索引,无其他副作用。3. midpoint 插值与 PyTorch 逐位不一致
develop 现状
midpoint 插值用
(tensor_upper + tensor_below) / 2,PyTorch 用lerp(a, b, 0.5)。浮点中间舍入路径不同,约 4-5% 的 case 差 1 ulp。修改内容
改为
paddle.lerp(tensor_below, tensor_upper, 0.5),与 PyTorch 舍入路径一致。4. 空归约维缺少守卫
develop 现状
归约维长度为 0 时(如
paddle.quantile(paddle.to_tensor([]), q=0.5)),last_index = x.shape[axis] - 1得 -1,rank 为负 → gather 取到错误位置,结果无意义但不报错。PyTorch 报quantile() input tensor must be non-empty。修改内容
在 rank 计算前新增
if x.shape[axis] == 0: raise ValueError("quantile() input tensor must be non-empty"),对齐 PyTorch。5. 测试
test_empty_reduction_quantile/test_empty_reduction_nanquantile:空 tensor 报 ValueErrortest_large_float32_reduction_not_rejected:float32 n=2^24+1 不报错且结果精确(回归守卫)devPR:#79842
是否引起精度变化
是(正向对齐 PyTorch)。