[BugFix] Fix put_along_axis grad kernel crash for integer div-by-zero and 0-size input#78482
Merged
wanghuancoder merged 6 commits intoPaddlePaddle:developfrom Apr 9, 2026
Conversation
… and 0-size input Fix two crash bugs in put_along_axis backward: 1. SIGFPE in reduce="mul" grad when integer divisor is zero (CPU & GPU) 2. Out-of-bounds memory access in CPU grad kernel when input has 0-size dim Add unit tests covering both scenarios. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
你的PR提交成功,感谢你对开源项目的贡献! |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #78482 +/- ##
===========================================
Coverage ? 100.00%
===========================================
Files ? 2
Lines ? 16
Branches ? 0
===========================================
Hits ? 16
Misses ? 0
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
- clang-format: break long line in gather_scatter_functor.cu - ruff-format: reformat test_put_along_axis_op.py - Fix test_uint8_cpu: integer types do not support autograd (sum_grad kernel not registered for uint8/int32/int64), change to forward-only verification to avoid NotFoundError in CI Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
Author
|
/re-run all-failed |
1 similar comment
Contributor
Author
|
/re-run all-failed |
Contributor
Author
|
/coverage test_put_along_axis_op |
Add TestPutAlongAxisMulFloat32DivByZeroGrad to exercise the new zero-divisor guards in gather_scatter_functor.cc/.cu: - test_x_grad_with_zero_in_x: covers x==0 branch in input_grad kernel - test_value_grad_with_zero_in_value: covers value==0 branch in value_grad kernel Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
413ee3c
Contributor
Author
|
@lugimzzz @Jiang-Jia-Jun 麻烦两位大佬帮忙审批一下~ |
Jiang-Jia-Jun
approved these changes
Mar 31, 2026
Contributor
Author
|
/re-run all-failed |
…erage The TestPutAlongAxisMulFloat32DivByZeroGrad test was not covering gather_scatter_functor.cc because tensors defaulted to GPU on CI machines. Add explicit place=paddle.CPUPlace() to ensure the CPU backward kernel path is exercised. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
6cba785
1. gather_scatter_functor.cc: split multi-line expression into separate statements so gcov can attribute execution counts to each line. 2. test_put_along_axis_op.py: add place=CPUPlace() and loss.backward() to TestPutAlongAxisZeroSizeInputGrad so the CPU grad kernel's numel==0 early-return path is exercised in Coverage CI. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
100bb7c to
b1d52bc
Compare
…coverage The existing zero-size tests all had indices.numel()==0, which triggers a Python-level early return and never reaches the C++ grad kernel. Add a test case with x_shape=[2,0,3] (x.numel()==0) but idx_shape=[2,1,3] (indices.numel()!=0) so the C++ kernel's numel==0 early-return guard (lines 38-45) is actually exercised. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
Author
|
/re-run all-failed |
1 similar comment
Contributor
Author
|
/re-run all-failed |
Contributor
Author
|
/re-run all-failed |
Contributor
Author
|
/re-run all-failed |
Contributor
Author
|
@sneaxiy 帮忙审核并合入一下 |
wangbingguang2026
added a commit
to wangbingguang2026/Paddle
that referenced
this pull request
Apr 9, 2026
… and 0-size input (PaddlePaddle#78482) * [BugFix] Fix put_along_axis grad kernel crash for integer div-by-zero and 0-size input Fix two crash bugs in put_along_axis backward: 1. SIGFPE in reduce="mul" grad when integer divisor is zero (CPU & GPU) 2. Out-of-bounds memory access in CPU grad kernel when input has 0-size dim Add unit tests covering both scenarios. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * [BugFix] Fix codestyle and test issues in put_along_axis fix - clang-format: break long line in gather_scatter_functor.cu - ruff-format: reformat test_put_along_axis_op.py - Fix test_uint8_cpu: integer types do not support autograd (sum_grad kernel not registered for uint8/int32/int64), change to forward-only verification to avoid NotFoundError in CI Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * [BugFix] Add float32 backward test for put_along_axis div-by-zero guard Add TestPutAlongAxisMulFloat32DivByZeroGrad to exercise the new zero-divisor guards in gather_scatter_functor.cc/.cu: - test_x_grad_with_zero_in_x: covers x==0 branch in input_grad kernel - test_value_grad_with_zero_in_value: covers value==0 branch in value_grad kernel Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * [BugFix] Force CPU placement in float32 div-by-zero grad test for coverage The TestPutAlongAxisMulFloat32DivByZeroGrad test was not covering gather_scatter_functor.cc because tensors defaulted to GPU on CI machines. Add explicit place=paddle.CPUPlace() to ensure the CPU backward kernel path is exercised. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * [BugFix] Improve patch coverage for put_along_axis grad fix 1. gather_scatter_functor.cc: split multi-line expression into separate statements so gcov can attribute execution counts to each line. 2. test_put_along_axis_op.py: add place=CPUPlace() and loss.backward() to TestPutAlongAxisZeroSizeInputGrad so the CPU grad kernel's numel==0 early-return path is exercised in Coverage CI. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * [BugFix] Add zero-size x with non-empty indices test for grad kernel coverage The existing zero-size tests all had indices.numel()==0, which triggers a Python-level early return and never reaches the C++ grad kernel. Add a test case with x_shape=[2,0,3] (x.numel()==0) but idx_shape=[2,1,3] (indices.numel()!=0) so the C++ kernel's numel==0 early-return guard (lines 38-45) is actually exercised. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
wangbingguang2026
added a commit
to wangbingguang2026/Paddle
that referenced
this pull request
Apr 16, 2026
… and 0-size input (PaddlePaddle#78482) * [BugFix] Fix put_along_axis grad kernel crash for integer div-by-zero and 0-size input Fix two crash bugs in put_along_axis backward: 1. SIGFPE in reduce="mul" grad when integer divisor is zero (CPU & GPU) 2. Out-of-bounds memory access in CPU grad kernel when input has 0-size dim Add unit tests covering both scenarios. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * [BugFix] Fix codestyle and test issues in put_along_axis fix - clang-format: break long line in gather_scatter_functor.cu - ruff-format: reformat test_put_along_axis_op.py - Fix test_uint8_cpu: integer types do not support autograd (sum_grad kernel not registered for uint8/int32/int64), change to forward-only verification to avoid NotFoundError in CI Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * [BugFix] Add float32 backward test for put_along_axis div-by-zero guard Add TestPutAlongAxisMulFloat32DivByZeroGrad to exercise the new zero-divisor guards in gather_scatter_functor.cc/.cu: - test_x_grad_with_zero_in_x: covers x==0 branch in input_grad kernel - test_value_grad_with_zero_in_value: covers value==0 branch in value_grad kernel Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * [BugFix] Force CPU placement in float32 div-by-zero grad test for coverage The TestPutAlongAxisMulFloat32DivByZeroGrad test was not covering gather_scatter_functor.cc because tensors defaulted to GPU on CI machines. Add explicit place=paddle.CPUPlace() to ensure the CPU backward kernel path is exercised. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * [BugFix] Improve patch coverage for put_along_axis grad fix 1. gather_scatter_functor.cc: split multi-line expression into separate statements so gcov can attribute execution counts to each line. 2. test_put_along_axis_op.py: add place=CPUPlace() and loss.backward() to TestPutAlongAxisZeroSizeInputGrad so the CPU grad kernel's numel==0 early-return path is exercised in Coverage CI. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * [BugFix] Add zero-size x with non-empty indices test for grad kernel coverage The existing zero-size tests all had indices.numel()==0, which triggers a Python-level early return and never reaches the C++ grad kernel. Add a test case with x_shape=[2,0,3] (x.numel()==0) but idx_shape=[2,1,3] (indices.numel()!=0) so the C++ kernel's numel==0 early-return guard (lines 38-45) is actually exercised. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
sneaxiy
pushed a commit
that referenced
this pull request
Apr 20, 2026
… and 0-size input (#78482) (#78621) * [BugFix] Fix put_along_axis grad kernel crash for integer div-by-zero and 0-size input Fix two crash bugs in put_along_axis backward: 1. SIGFPE in reduce="mul" grad when integer divisor is zero (CPU & GPU) 2. Out-of-bounds memory access in CPU grad kernel when input has 0-size dim Add unit tests covering both scenarios. * [BugFix] Fix codestyle and test issues in put_along_axis fix - clang-format: break long line in gather_scatter_functor.cu - ruff-format: reformat test_put_along_axis_op.py - Fix test_uint8_cpu: integer types do not support autograd (sum_grad kernel not registered for uint8/int32/int64), change to forward-only verification to avoid NotFoundError in CI * [BugFix] Add float32 backward test for put_along_axis div-by-zero guard Add TestPutAlongAxisMulFloat32DivByZeroGrad to exercise the new zero-divisor guards in gather_scatter_functor.cc/.cu: - test_x_grad_with_zero_in_x: covers x==0 branch in input_grad kernel - test_value_grad_with_zero_in_value: covers value==0 branch in value_grad kernel * [BugFix] Force CPU placement in float32 div-by-zero grad test for coverage The TestPutAlongAxisMulFloat32DivByZeroGrad test was not covering gather_scatter_functor.cc because tensors defaulted to GPU on CI machines. Add explicit place=paddle.CPUPlace() to ensure the CPU backward kernel path is exercised. * [BugFix] Improve patch coverage for put_along_axis grad fix 1. gather_scatter_functor.cc: split multi-line expression into separate statements so gcov can attribute execution counts to each line. 2. test_put_along_axis_op.py: add place=CPUPlace() and loss.backward() to TestPutAlongAxisZeroSizeInputGrad so the CPU grad kernel's numel==0 early-return path is exercised in Coverage CI. * [BugFix] Add zero-size x with non-empty indices test for grad kernel coverage The existing zero-size tests all had indices.numel()==0, which triggers a Python-level early return and never reaches the C++ grad kernel. Add a test case with x_shape=[2,0,3] (x.numel()==0) but idx_shape=[2,1,3] (indices.numel()!=0) so the C++ kernel's numel==0 early-return guard (lines 38-45) is actually exercised. --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
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
修复
paddle.put_along_axis反向传播中的两个 crash bug:Bug A:reduce="mul" 整数除零 SIGFPE
当
reduce="mul"且输入为整数类型(uint8/int32/int64)时,如果 x 或 value 中包含零值,反向传播中的梯度计算会触发整数除零(SIGFPE),导致进程崩溃。根因:梯度公式
grad * out / x和self * (out / value)在除数为 0 时未做保护。浮点类型产生 inf/nan 不会 crash,但整数类型会触发 SIGFPE 信号。修复:在 CPU 和 GPU 的 4 处除法位置增加零值判断,除数为 0 时梯度直接赋 0。
修改文件:
paddle/phi/kernels/funcs/gather_scatter_functor.cc:cpu_scatter_mul_min_max_input_grad_kernel和cpu_scatter_mul_min_max_value_grad_kernelpaddle/phi/kernels/funcs/gather_scatter_functor.cu:ScatterMulInputGradGPUKernel和ScatterMulValueGradGPUKernelBug B:0-size input CPU 反向传播越界访问
当 input tensor 含 0-size 维度(如 shape
[0, 60])但 index 在该维度非零时,CPU 反向传播会越界写入内存导致崩溃。GPU grad kernel 已有x.numel() == 0的 early return 保护,但 CPU 版本遗漏了。修复:在
put_along_axis_grad_kernel.cc入口处增加与 GPU 版本相同的 0-size 保护。修改文件:
paddle/phi/kernels/cpu/put_along_axis_grad_kernel.cc单测补充
在
test/legacy_test/test_put_along_axis_op.py中新增两个测试类:TestPutAlongAxisMulIntegerDivByZero:覆盖 Bug A,测试 uint8/int32/int64 在 CPU/GPU 上 reduce="mul" backward 不 crashTestPutAlongAxisZeroSizeInputGrad:覆盖 Bug B,测试 input 各维度为 0 时 assign/add/mul 前向不 crash是否引起精度变化
否。修改仅在除数为零或 numel 为零的边界条件下生效(梯度赋 0),不影响正常数值计算路径。
case 复测结果
0_size_tensor_1_8_1.txt 中 put_along_axis 相关的 case 一共有123个,测试结果如下:
0_size_tensor_1_8_invalid_1.txt 中 put_along_axis 相关的 case 一共有40个,测试结果如下:
其中,Failed cases 都是 paddle error,Python 层的 shape 校验正确地拦截了越界输入并抛出 ValueError,这些 case 本身就是非法输入,应该报错。Skipped cases 都是 numpy error,PaddleAPITest 框架将它们标记为 numpy_error 并跳过,这是测试数据本身的边界问题。