Fix out-of-bounds memory access in SetKernel for 0-size tensor#78486
Merged
wanghuancoder merged 6 commits intoPaddlePaddle:developfrom Apr 1, 2026
Merged
Conversation
|
你的PR提交成功,感谢你对开源项目的贡献! |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #78486 +/- ##
==========================================
Coverage ? 94.59%
==========================================
Files ? 1
Lines ? 37
Branches ? 0
==========================================
Hits ? 35
Misses ? 2
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
Author
|
/re-run all-failed |
4 similar comments
Contributor
Author
|
/re-run all-failed |
Contributor
Author
|
/re-run all-failed |
Contributor
Author
|
/re-run all-failed |
Contributor
Author
|
/re-run all-failed |
When calling Tensor.set_(source, shape, stride, offset) with a 0-size source tensor and non-zero target shape, the original code had a missing branch in the conditional logic: when source.numel()==0 and x.numel()!=0, no branch was executed, leaving `out` with its original data holder but with the user-specified meta (shape/stride). This caused ContiguousKernel to read beyond allocated memory when converting the strided tensor to contiguous. The fix forces the output tensor to inherit the source's 0-size dims/strides when source has no elements, preventing out-of-bounds access. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
f021456 to
4b3ac5c
Compare
Contributor
Author
|
/re-run all-failed |
Contributor
Author
|
/re-run all-failed |
Contributor
Author
|
/re-run all-failed |
1 similar comment
Contributor
Author
|
/re-run all-failed |
3 tasks
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
修复 0-Size 报错问题
paddle.Tensor.set_(Tensor([20],"float32"), Tensor([0, 3],"float32"), list[20,], list[2,], 0, )
Summary
compute-sanitizer) when callingTensor.set_(source, shape, stride, offset)with a 0-size source tensor and non-zero target shape.source.numel() == 0, the output tensor now inherits the source's 0-size dims/strides instead of using user-specified shape/stride, preventing invalid memory access.TestSet_API_ZeroSizeand added 5 new test cases covering various 0-size scenarios.Root Cause
In
SetKernel(paddle/phi/kernels/set_kernel.cc), the conditional logic for handling 0-size tensors had a missing branch: whensource.numel() == 0andx.numel() != 0, no branch was executed. The output tensor retained its original data holder, butSetInferMetahad already set its meta to the user-specified shape/stride (e.g.,shape=[20], stride=[2]). WhenContiguousKernellater attempted to read 20 elements via stride=2 (requiring storage for indices 0–38), the underlying storage was empty (nullptr), causing CUDA illegal memory access.Before fix (missing branch):
Neither branch executes → out keeps stale holder with mismatched meta
Fix
When
source.numel() == 0, force the output tensor's dims/strides to match the source's 0-size shape, and rebind the holder to the source's (empty) storage. This ensuresnumel == 0, so downstream kernels (e.g.,ContiguousKernel) skip safely via theirnumel <= 0early-return guards.Test Plan
compute-sanitizer:ERROR SUMMARY: 0 errors(was 11 errors before fix)paddle.Tensor.set_(Tensor([20],"float32"), Tensor([0, 3],"float32"), list[20,], list[2,], 0, )TestSet_API_ZeroSize:test_zero_size_source_with_nonzero_shape— 0-size source + explicit non-zero shapetest_zero_size_source_default_args— 0-size source with default shape/stridetest_zero_size_x_nonzero_source— 0-size x with non-zero sourcetest_both_zero_size— both x and source are 0-sizetest_zero_size_source_no_crash_on_contiguous— no crash on.contiguous()after set_是否引起精度变化
否