[Fix] issue #79238 paddle.bincount output consumed by paddle.scatter_reduce fails under to_static full_graph#79288
Conversation
…reduce fails under to_static full_graph
|
/re-run all-failed |
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
/re-run all-failed |
PaddlePaddle-bot
left a comment
There was a problem hiding this comment.
🤖 Paddle-CI-Agent | pr_review |
2026-06-10 15:39:17
📋 Review 摘要
PR 概述:修复 paddle.bincount 动态长度输出接入 paddle.scatter_reduce 在 to_static(full_graph=True) 下的 shape 误判。
变更范围:python/paddle/tensor/manipulation.py 中 put_along_axis 非 broadcast 分支的 shape 校验。
影响面 Tag:User Experience
问题
| 级别 | 文件 | 概述 |
|---|---|---|
| 🔴 Bug | python/paddle/tensor/manipulation.py:7655 |
动态 shape 跳过条件过宽,会放过仍可静态判断的不兼容 indices/values 或 arr/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: |
There was a problem hiding this comment.
🔴 Bug 这里把“任一 shape 维度为动态”作为整维度跳过条件,会连带跳过仍然可静态判断的另一条约束。
put_along_axis 在 broadcast=False 时需要同时保证:非 axis 维上 indices 不超过 arr,以及每个维度上 indices 不超过 values。例如 axis=0 且 arr.shape[0] == -1、indices.shape[0] == 5、values.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 约束仍可静态判错”的非法输入。
|
/re-run all-failed |
PR Category
User Experience
PR Types
Bug fixes
Description
错误原因
在静态图模式(
paddle.jit.to_static)下,paddle.scatter_reduce配合paddle.bincount使用时抛出 RuntimeError在静态图下,
paddle.bincount的输出counts的shape被推断为 [-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中的代码能够通过相关 issue:
paddle.bincountoutput consumed bypaddle.scatter_reducefails under to_static full_graph #79238是否引起精度变化
否