Skip to content

[Cherry-Pick][Operator Mechanism] Reject empty reduction dim and oversized input in quantile- #79842 - #79852

Merged
sneaxiy merged 4 commits into
PaddlePaddle:release/3.4from
feixi139:scratch_r34
Oct 10, 2026
Merged

sneaxiy merged 4 commits into
PaddlePaddle:release/3.4from
feixi139:scratch_r34

Conversation

@feixi139

@feixi139 feixi139 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

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 下计算 rank q*(n-1)。float32 仅 23 位尾数,超过 2^24 的整数无法精确表示,rank 算错 → gather 取到错误元素 → 结果静默错误,无报错。float16/bfloat16 的精度上限更低(2^11/2^8)。PyTorch(commit f66303fef70)的做法是把 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 报 ValueError
  • 新增 test_large_float32_reduction_not_rejected:float32 n=2^24+1 不报错且结果精确(回归守卫)
  • 全量 25 个测试通过

devPR:#79842

是否引起精度变化

是(正向对齐 PyTorch)。

  • midpoint 插值模式:约 4-5% 的 case 消除 1 ulp 差异,与 PyTorch 逐位一致
  • 大归约维场景(n > 2^24 float32):由静默错误变为正确结果
  • 其他插值模式和常规大小输入的数值结果不变

feixi139 and others added 4 commits October 9, 2026 20:41
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-Review-Bot

Paddle-Review-Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Paddle-Bot Review Board (review完成)

序号 位置 优先级 规则来源 状态
1 cherry-pick 源 develop PR #79842 尚未合入 P3 默认规则 🚧

Powered by Nyanpasu claude with Opus 4.8 默认推理级别, please check the suggestions carefully.

@Paddle-Review-Bot Paddle-Review-Bot 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.

P3 本 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-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (release/3.4@64f001f). Learn more about missing BASE report.

Files with missing lines Patch % Lines
python/paddle/tensor/stat.py 88.88% 1 Missing ⚠️

❌ 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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sneaxiy
sneaxiy merged commit a108087 into PaddlePaddle:release/3.4 Oct 10, 2026
140 of 149 checks passed
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.

6 participants