Skip to content

[API-Compat] paddle.Tensor.requires_grad is added and tested #74491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

LLSGYN
Copy link

@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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants