Skip to content

Conversation

@LLSGYN
Copy link
Contributor

@LLSGYN LLSGYN commented Aug 8, 2025

PR Category

User Experience

PR Types

New features

Description

This PR adds a requires_grad property to Paddle Tensors to provide compatibility and improve user experience.

What's added:

  • Added requires_grad property to Tensor class across all execution modes (dynamic graph, static graph, and PIR)
  • The property provides a convenient way to check and set gradient computation requirements
  • requires_grad is the inverse of stop_gradient (requires_grad=True means stop_gradient=False)
  • Added comprehensive unit tests to verify the functionality

Key features:

  • Getter: Returns not self.stop_gradient, allowing users to check if gradient computation is enabled
  • Setter: Accepts boolean values and sets self.stop_gradient = not value
  • Type checking: Raises TypeError if non-boolean values are provided
  • Documentation: Includes detailed docstrings with usage examples

Files modified:

  • python/paddle/base/dygraph/math_op_patch.py - Dynamic graph mode support
  • python/paddle/base/layers/math_op_patch.py - Static graph mode support
  • python/paddle/pir/math_op_patch.py - PIR mode support
  • test/legacy_test/test_tensor_requires_grad.py - Comprehensive test suite
@paddle-bot
Copy link

paddle-bot bot commented Aug 8, 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.

@paddle-bot paddle-bot bot added the contributor External developers label Aug 8, 2025
Copy link
Contributor

@zhwesky2010 zhwesky2010 left a comment

Choose a reason for hiding this comment

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

LGTM

@PaddlePaddle PaddlePaddle locked and limited conversation to collaborators Aug 11, 2025
@PaddlePaddle PaddlePaddle unlocked this conversation Aug 11, 2025
@zhwesky2010 zhwesky2010 requested a review from SigureMo August 11, 2025 11:12
@zhwesky2010 zhwesky2010 changed the title [API-Compat] paddle.Tensor.requires_grad is added and tested [API-Compat] add paddle.Tensor.requires_grad is added and tested Aug 11, 2025
@codecov-commenter
Copy link

codecov-commenter commented Aug 11, 2025

Codecov Report

❌ Patch coverage is 79.16667% with 5 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@c917219). Learn more about missing BASE report.

Files with missing lines Patch % Lines
python/paddle/base/layers/math_op_patch.py 50.00% 4 Missing ⚠️
python/paddle/pir/math_op_patch.py 87.50% 1 Missing ⚠️
Additional details and impacted files
@@ Coverage Diff @@ ## develop #74491 +/- ## ========================================== Coverage ? 79.16% ========================================== Files ? 3 Lines ? 24 Branches ? 0 ========================================== Hits ? 19 Misses ? 5 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.
@SigureMo SigureMo changed the title [API-Compat] add paddle.Tensor.requires_grad is added and tested [API compatibility] add new API paddle.Tensor.requires_grad Aug 12, 2025
Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@jeff41404 jeff41404 left a comment

Choose a reason for hiding this comment

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

LGTM

@zhwesky2010 zhwesky2010 merged commit 2beec18 into PaddlePaddle:develop Aug 13, 2025
94 of 99 checks passed
maxiaolong001 pushed a commit to maxiaolong001/Paddle that referenced this pull request Aug 13, 2025
@LLSGYN LLSGYN deleted the prop_req_grad branch August 30, 2025 04:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment