Repository navigation
[Cherry-Pick][Operator Mechanism] Fix topk returning NaN for an empty reduction axis - #79818
Merged
Merged
Conversation
When the topk axis has size 0 while k >= 1, the CPU/GPU/XPU kernels short-circuited the empty input to a NaN-filled output and returned before the k-range check, silently producing NaN instead of raising. Align with torch's "selected index k out of range" semantics by throwing InvalidArgument in the empty-input branch of all three kernels.
The empty-reduction-axis assertion ran on the default place, so on the GPU coverage build the CPU kernel throw branch was never executed and reported as uncovered. Force CPUPlace (always available) plus GPU/XPU when compiled so each backend's throw branch is exercised, and keep the legit empty-output control on every place.
risemeup1111
approved these changes
Sep 28, 2026
On CUDA builds topk registers TopkKernelCuda, not TopkKernel, so its own empty-input branch still filled the output with NaN when the reduction axis had size 0 and k >= 2 (k == 1 already delegates to TopkKernel). Throw InvalidArgument there as well so CPU/GPU/XPU and ROCm all report the same "selected index k out of range" error.
risemeup1111
approved these changes
Sep 28, 2026
Codecov Report❌ Patch coverage is
❌ Your patch status has failed because the patch coverage (0.00%) 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 #79818 +/- ##
==============================================
Coverage ? 0.00%
==============================================
Files ? 1
Lines ? 1
Branches ? 0
==============================================
Hits ? 0
Misses ? 1
Partials ? 0 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The empty-reduction-axis assertion only checked for ValueError, which the k-range PADDLE_ENFORCE_GE fallback also raises for k >= 1. Match the "axis of size 0" message so removing the dedicated empty-input throw no longer slips through as a generic "x has only 0 element" error.
Paddle-Review-Bot
approved these changes
Oct 9, 2026
Paddle-Review-Bot
approved these changes
Oct 14, 2026
wanghuancoder
approved these changes
Oct 14, 2026
Contributor
Author
|
/re-run all-failed |
Member
|
cpu部分覆盖率 |
sneaxiy
approved these changes
Oct 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Category
Operator Mechanism
PR Types
Bug fixes
Description
本 PR 修复
topk在规约轴长度为 0 且k >= 1时静默返回全NaN的问题,覆盖 CPU / GPU / XPU 三个TopkKernel(topk与topk_v1共用同一实现)。核心问题和结果可以概括为:paddle.empty([1024, 0])沿axis=-1取k=10)时,输出在该轴上的期望长度为k >= 1,输出并非空张量。三个 kernel 在真正的k越界校验(PADDLE_ENFORCE_GE(x.numel(), k))之前,先用x.numel() == 0短路,直接把输出填成NaN、索引填成0并返回,导致非法请求被静默接受、返回无意义结果,而不是报错。selected index k out of range。Paddle 前向的这类"轴无法提供 k 个元素"应当报错,而不是产出 NaN。paddle.empty([0, 5])沿axis=-1取k=3,输出形状[0, 3]、numel == 0)走的是更早的out->numel() == 0空输出分支,本 PR 不触碰该路径。1. 空规约轴静默返回 NaN
问题
三个 kernel 的开头顺序都是:先处理"输出为空"(
out->numel() == 0)与 0-D 输入,再处理x.numel() == 0,最后才做k的范围校验。问题出在x.numel() == 0这一分支:能走到
x.numel() == 0这一步,说明输出已通过前面的out->numel() == 0判断(即输出非空)。输入为空而输出非空,只可能是 topk 轴长度为 0——此时该轴根本无法提供k >= 1个元素,属于非法请求。旧实现却把它当成"空输入"用Full(NAN)兜底:CPU kernel 的
FullTopK里其实有正确的PADDLE_ENFORCE_LE(k, input_width),但对空输入这条分支到不了;GPU/XPU 也在该分支之后才有k的范围校验。三处是同一根因。改动
TopkKernel中x.numel() == 0的 NaN 兜底分支替换为显式报错:topk_v1复用TopkKernel,无需单独改动。修改后结果
k >= 1的请求在进入计算前明确抛出InvalidArgument,与 torch 的selected index k out of range语义一致;NaN的"看似成功"结果;修改后的总体行为
InvalidArgument,对齐 torch[0,5]沿axis=-1取k=3,输出[0,3])合法输入的结果不变;修改只影响原本被静默接受、返回 NaN 的非法边界请求。
devPR:#79799
是否引起精度变化
否