Skip to content

[Bug Fix] Fix view grad when out_grad is not contiguous#76679

Merged
Eddie-Wang1120 merged 4 commits intoPaddlePaddle:developfrom
LittleHeroZZZX:add_view_shape_double_grad
Dec 3, 2025
Merged

[Bug Fix] Fix view grad when out_grad is not contiguous#76679
Eddie-Wang1120 merged 4 commits intoPaddlePaddle:developfrom
LittleHeroZZZX:add_view_shape_double_grad

Conversation

@LittleHeroZZZX
Copy link
Contributor

PR Category

User Experience

PR Types

Bug fixes

Description

修改 view_shape_grad,当 out_grad 不能原地 view 为目标 shape 时,创建一份副本再 reshape

This pull request updates the ViewShapeGradKernel implementation in view_grad_kernel.cc to improve how gradients are handled for tensors with strided memory layouts. The main change is replacing the previous kernel call with logic that attempts to reshape the stride directly, and if that's not possible, falls back to making the tensor contiguous. This enhances support for strided tensors in gradient computations.

Improvements to strided tensor gradient handling:

  • Replaced the call to ViewShapeStridedKernel with logic that first tries to reshape the tensor's stride using ReshapeStride. If successful, it sets the metadata, shape, stride, offset, holder, and version counter directly from out_grad to input_grad. If not, it creates a contiguous copy of the tensor using StridedTensorContiguous, ensuring correct gradient computation for non-contiguous tensors.

Dependency and utility updates:

  • Added includes for strided_reshape_utils.h and strided_utils.h to support the new stride handling logic.

@paddle-bot
Copy link

paddle-bot bot commented Nov 28, 2025

你的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.

@LittleHeroZZZX LittleHeroZZZX changed the title [Bug Fix]Fix view grad when out_grad is not contiguous [Bug Fix] Fix view grad when out_grad is not contiguous Nov 28, 2025
Eddie-Wang1120
Eddie-Wang1120 previously approved these changes Nov 28, 2025
wanghuancoder
wanghuancoder previously approved these changes Nov 28, 2025
Copy link
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

@LittleHeroZZZX
Copy link
Contributor Author

/re-run all-failed

@codecov-commenter
Copy link

codecov-commenter commented Nov 28, 2025

Codecov Report

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

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

Add `no_need_buffer: input` to the view_shape_grad backward operator configuration. This indicates that the gradient computation for view_shape does not require the input tensor buffer, optimizing memory usage during backward passes.
Add a new test case `TestViewGrad.test_dygraph` to verify gradient correctness when using `view` followed by `transpose` in dynamic graph mode. The test ensures that the computed gradients match expected values, improving coverage for tensor manipulation operations.
@LittleHeroZZZX
Copy link
Contributor Author

/re-run all-failed

2 similar comments
@LittleHeroZZZX
Copy link
Contributor Author

/re-run all-failed

@LittleHeroZZZX
Copy link
Contributor Author

/re-run all-failed

@LittleHeroZZZX
Copy link
Contributor Author

/re-run approval

@Eddie-Wang1120 Eddie-Wang1120 merged commit f729aff into PaddlePaddle:develop Dec 3, 2025
107 of 120 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants