Skip to content

[Fix] issue #79238 paddle.bincount output consumed by paddle.scatter_reduce fails under to_static full_graph#79288

Open
Manfredss wants to merge 1 commit into
PaddlePaddle:developfrom
Manfredss:fix_issue79238
Open

[Fix] issue #79238 paddle.bincount output consumed by paddle.scatter_reduce fails under to_static full_graph#79288
Manfredss wants to merge 1 commit into
PaddlePaddle:developfrom
Manfredss:fix_issue79238

Conversation

@Manfredss

Copy link
Copy Markdown
Contributor

PR Category

User Experience

PR Types

Bug fixes

Description

错误原因
在静态图模式(paddle.jit.to_static)下,paddle.scatter_reduce 配合 paddle.bincount 使用时抛出 RuntimeError

RuntimeError: Size does not match at dimension 0 expected index paddle.Size([5]) 
to be smaller than self paddle.Size([3]) apart from dimension 0 and to be smaller 
size than values paddle.Size([-1])

在静态图下,paddle.bincount 的输出 countsshape 被推断为 [-1](动态 shape,表示长度在运行时才能确定)。此时检查条件 indices.shape[i] > values.shape[I]5 > -1,在 Python 中该比较返回 True,导致错误地抛出 RuntimeError。

根本问题:shape 检查逻辑没有处理静态图中的动态 shape(用负数表示),将 -1 当作实际长度进行比较。

修复点
python/paddle/tensor/manipulation.py 第 7650-7656 行,put_along_axis 函数的 shape 检查逻辑中,增加了对动态 shape(值为负数)的跳过处理。这样在静态图下遇到 paddle.Size([-1]) 这样的动态 shape 时,不会错误地触发 shape 不匹配报错。

重新编译后 issue 中的代码能够通过

λ /workspace python3.10 test.py 
grep: warning: GREP_OPTIONS is deprecated; please use an alias or script
RuntimeError: module compiled against ABI version 0x1000009 but this version of numpy is 0x2000000
paddle 3.5.0.dev20260609
W0609 13:12:59.821501 19576 gpu_resources.cc:116] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 12.6, Runtime API Version: 11.8
eager: [7.0, -2.0, 1.0]
I0609 13:12:59.964815 19576 pir_interpreter.cc:1605] New Executor is Running ...
I0609 13:12:59.965278 19576 pir_interpreter.cc:1629] pir interpreter is running by multi-thread mode ...
static: [7.0, -2.0, 1.0]

相关 issue:

是否引起精度变化

@paddle-bot paddle-bot Bot added the contributor External developers label Jun 9, 2026
@Manfredss

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (develop@e313387). Learn more about missing BASE report.

Additional details and impacted files
@@             Coverage Diff             @@
##             develop    #79288   +/-   ##
===========================================
  Coverage           ?   100.00%           
===========================================
  Files              ?         1           
  Lines              ?         5           
  Branches           ?         0           
===========================================
  Hits               ?         5           
  Misses             ?         0           
  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.

@Manfredss

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@PaddlePaddle-bot PaddlePaddle-bot 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.

🤖 Paddle-CI-Agent | pr_review | 2026-06-10 15:39:17

📋 Review 摘要

PR 概述:修复 paddle.bincount 动态长度输出接入 paddle.scatter_reduceto_static(full_graph=True) 下的 shape 误判。
变更范围python/paddle/tensor/manipulation.pyput_along_axis 非 broadcast 分支的 shape 校验。
影响面 TagUser Experience

问题

级别 文件 概述
🔴 Bug python/paddle/tensor/manipulation.py:7655 动态 shape 跳过条件过宽,会放过仍可静态判断的不兼容 indices/valuesarr/indices shape

📝 PR 规范检查

标题 Tag [Fix] 不在 checklist.md §D2 的 PR Category/PR Types 枚举中;描述结构符合模板。

标题建议(可直接复制):

  • [Bug fixes] Fix issue #79238: support bincount output in scatter_reduce under to_static full_graph
PR 描述建议(点击展开,可直接复制)
### PR Category
User Experience

### PR Types
Bug fixes

### Description
修复 `paddle.jit.to_static(full_graph=True)``paddle.scatter_reduce` 消费 `paddle.bincount` 输出时的 shape 校验误报。`paddle.bincount` 在静态图中输出长度可为动态 `-1``put_along_axis` 的非 broadcast shape 校验不应把 `-1` 当作实际长度比较。

验证方式:
- 作者在 PR 描述中提供了 issue 复现脚本,动态图与静态图输出均为 `[7.0, -2.0, 1.0]`### 是否引起精度变化

总体评价

修复方向正确,但当前实现把同一维上的所有检查一起跳过,扩大了 dynamic shape 例外范围。需要按每个比较项分别判断参与比较的维度是否已知,避免引入 shape 校验回退。

values_shape_i = values.shape[i]
arr_shape_i = arr.shape[i]
# Skip check for dynamic shapes (negative values in static graph)
if indices_shape_i < 0 or values_shape_i < 0 or arr_shape_i < 0:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 Bug 这里把“任一 shape 维度为动态”作为整维度跳过条件,会连带跳过仍然可静态判断的另一条约束。

put_along_axisbroadcast=False 时需要同时保证:非 axis 维上 indices 不超过 arr,以及每个维度上 indices 不超过 values。例如 axis=0arr.shape[0] == -1indices.shape[0] == 5values.shape[0] == 3 时,arr 的动态轴与 indices > values 无关,但当前 continue 会放过明显不兼容的 values。底层 PutAlongAxisInferMeta 只返回 arr 的 shape,不会补做这个检查。

建议按比较项分别跳过未知维度:

arr_index_known = arr_shape_i >= 0 and indices_shape_i >= 0
index_value_known = indices_shape_i >= 0 and values_shape_i >= 0
if (i != axis and arr_index_known and arr_shape_i < indices_shape_i) or (
    index_value_known and indices_shape_i > values_shape_i
):
    raise RuntimeError(...)

并补充 to_static(full_graph=True) 回归用例,覆盖 bincount 动态长度输出以及“部分维度动态但另一条 shape 约束仍可静态判错”的非法输入。

@PaddlePaddle-bot

PaddlePaddle-bot commented Jun 11, 2026

Copy link
Copy Markdown

🤖 Paddle-CI-Agent | ci_status_monitor | 2026-06-22 00:41:33 UTC+08:00

CI报告基于以下代码生成(30分钟更新一次):
PR commit: 4e4ae8b | Merge base: e313387 (branch: develop)


1 Required任务 : 32/32 通过

总执行(rerun次数) 总任务 ✅ 通过 ❌ 失败 ⏳ 运行中 ⏸️ 等待中 跳过
85(34) 51 50 0 0 0 1
任务 错误类型 置信度 日志

2 失败详情

@Manfredss

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor External developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants