Skip to content

[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
wangbingguang2026:fix/put_along_axis_grad_crash
Apr 9, 2026
Merged

[BugFix] Fix put_along_axis grad kernel crash for integer div-by-zero and 0-size input#78482
wanghuancoder merged 6 commits intoPaddlePaddle:developfrom
wangbingguang2026:fix/put_along_axis_grad_crash

Conversation

@wangbingguang2026
Copy link
Copy Markdown
Contributor

@wangbingguang2026 wangbingguang2026 commented Mar 25, 2026

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 / xself * (out / value) 在除数为 0 时未做保护。浮点类型产生 inf/nan 不会 crash,但整数类型会触发 SIGFPE 信号。

修复:在 CPU 和 GPU 的 4 处除法位置增加零值判断,除数为 0 时梯度直接赋 0。

修改文件:

  • paddle/phi/kernels/funcs/gather_scatter_functor.cccpu_scatter_mul_min_max_input_grad_kernelcpu_scatter_mul_min_max_value_grad_kernel
  • paddle/phi/kernels/funcs/gather_scatter_functor.cuScatterMulInputGradGPUKernelScatterMulValueGradGPUKernel

Bug 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 不 crash
  • TestPutAlongAxisZeroSizeInputGrad:覆盖 Bug B,测试 input 各维度为 0 时 assign/add/mul 前向不 crash

是否引起精度变化

否。修改仅在除数为零或 numel 为零的边界条件下生效(梯度赋 0),不影响正常数值计算路径。

case 复测结果

0_size_tensor_1_8_1.txt 中 put_along_axis 相关的 case 一共有123个,测试结果如下:

  • Passed cases:96
  • Failed cases:0
  • Skipped cases:27

0_size_tensor_1_8_invalid_1.txt 中 put_along_axis 相关的 case 一共有40个,测试结果如下:

  • Passed cases:27
  • Failed cases:10
  • Skipped cases:3

其中,Failed cases 都是 paddle error,Python 层的 shape 校验正确地拦截了越界输入并抛出 ValueError,这些 case 本身就是非法输入,应该报错。Skipped cases 都是 numpy error,PaddleAPITest 框架将它们标记为 numpy_error 并跳过,这是测试数据本身的边界问题。

… 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>
@paddle-bot
Copy link
Copy Markdown

paddle-bot Bot commented Mar 25, 2026

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 25, 2026

CLA assistant check
All committers have signed the CLA.

@paddle-bot paddle-bot Bot added the contributor External developers label Mar 25, 2026
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 25, 2026

Codecov Report

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

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.
📢 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.

- 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>
wanghuancoder
wanghuancoder previously approved these changes Mar 27, 2026
Copy link
Copy Markdown
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

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

LGTM

@wangbingguang2026
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

1 similar comment
@wangbingguang2026
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@wangbingguang2026
Copy link
Copy Markdown
Contributor Author

/coverage test_put_along_axis_op

From00
From00 previously approved these changes Mar 30, 2026
Copy link
Copy Markdown
Contributor

@From00 From00 left a comment

Choose a reason for hiding this comment

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

LGTM

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>
@wangbingguang2026 wangbingguang2026 dismissed stale reviews from From00 and wanghuancoder via 413ee3c March 30, 2026 12:48
From00
From00 previously approved these changes Mar 30, 2026
Copy link
Copy Markdown
Contributor

@From00 From00 left a comment

Choose a reason for hiding this comment

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

LGTM

wanghuancoder
wanghuancoder previously approved these changes Mar 31, 2026
Copy link
Copy Markdown
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

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

LGTM

@wangbingguang2026
Copy link
Copy Markdown
Contributor Author

@lugimzzz @Jiang-Jia-Jun 麻烦两位大佬帮忙审批一下~

Copy link
Copy Markdown
Contributor

@lugimzzz lugimzzz left a comment

Choose a reason for hiding this comment

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

lgtm

@wangbingguang2026
Copy link
Copy Markdown
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>
@wangbingguang2026 wangbingguang2026 dismissed stale reviews from wanghuancoder and From00 via 6cba785 March 31, 2026 10:47
wanghuancoder
wanghuancoder previously approved these changes Mar 31, 2026
Copy link
Copy Markdown
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

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

LGTM

wanghuancoder
wanghuancoder previously approved these changes Apr 1, 2026
Copy link
Copy Markdown
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

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

LGTM

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>
…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>
@wangbingguang2026
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

1 similar comment
@wangbingguang2026
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

Copy link
Copy Markdown
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

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

LGTM

@wangbingguang2026
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

Copy link
Copy Markdown
Contributor

@From00 From00 left a comment

Choose a reason for hiding this comment

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

LGTM

@wangbingguang2026
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@wangbingguang2026
Copy link
Copy Markdown
Contributor Author

@sneaxiy 帮忙审核并合入一下

@wanghuancoder wanghuancoder merged commit 5ae373f into PaddlePaddle:develop Apr 9, 2026
244 of 256 checks passed
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>
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.

7 participants