Skip to content

[API compatibility] add tensor.requires_grad_ method#76439

Merged
zhwesky2010 merged 5 commits intoPaddlePaddle:developfrom
blacksheep-Aristotle:tensor_requires_grad
Nov 21, 2025
Merged

[API compatibility] add tensor.requires_grad_ method#76439
zhwesky2010 merged 5 commits intoPaddlePaddle:developfrom
blacksheep-Aristotle:tensor_requires_grad

Conversation

@blacksheep-Aristotle
Copy link
Contributor

PR Category

User Experience

PR Types

New features

Description

add tensor.requires_grad_ method , API corresponding to PyTorch:[https://docs.pytorch.org/docs/stable/generated/torch.Tensor.requires_grad_.html#torch.Tensor.requires_grad_]

@paddle-bot
Copy link

paddle-bot bot commented Nov 17, 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.

@zhwesky2010 zhwesky2010 changed the title add tensor.requires_grad_ method [API compatibility] add tensor.requires_grad_ method Nov 17, 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.

加单测跑一下吧,这个需要实际运行下。同事覆盖率CI也需要实际跑到了才行

self.stop_gradient = not value

@requires_grad.setter
def requires_grad_(self, value: bool) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

infoflow 2025-11-17 15-51-18

统一加到这三个文件里去吧,和def requires_grad(放一块

这个是一个function,不是一个属性,所以不需要@requires_grad.setter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

Choose a reason for hiding this comment

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

这里少了默认参数True,是漏掉了么

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.

eager_method.cc、framework.py这两个文件不需要改动

self.desc.set_stop_gradient(s)

@property
def requires_grad(self) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

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

需要删去

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

return not self.desc.stop_gradient()

@requires_grad.setter
def requires_grad_(self, value) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

需要删去

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -0,0 +1,63 @@
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
Copy link
Contributor

Choose a reason for hiding this comment

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

单测里还需要测 dygraph、老IR分支下的。

统一放到test/legacy_test/test_tensor_requires_grad.py里来测吧。

对于老IR:需要加with paddle.pir_utils.OldIrGuard(),才能测试到layers/math_op_patch.py
对于新IR:需要加with paddle.pir_utils.IrGuard(), 才能测到pir/math_op_patch.py
对于动态图:直接测试

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

"""Test requires_grad behavior in static mode"""
paddle.enable_static()

try:
Copy link
Contributor

Choose a reason for hiding this comment

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

这个不是检测异常抛出,不需要try finally

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

EAGER_CATCH_AND_THROW_RETURN_NULL
}

PyDoc_STRVAR(tensor_requires_grad___doc__, // NOLINT
Copy link
Contributor

Choose a reason for hiding this comment

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

eager_mothod.cc不需要改动了,这个的逻辑会被dygraph/math_op_patch.py覆盖掉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

self.assertFalse(y.requires_grad)
self.assertTrue(y.stop_gradient)

def test_requires_grad_static_mode(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

静态图pir跑到了,老IR应该没跑到吧,这样layers/match_op_patch.py里面的代码是没有覆盖到的

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@codecov-commenter
Copy link

codecov-commenter commented Nov 19, 2025

Codecov Report

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

Files with missing lines Patch % Lines
python/paddle/base/layers/math_op_patch.py 75.00% 1 Missing ⚠️
python/paddle/pir/math_op_patch.py 75.00% 1 Missing ⚠️

❌ Your patch status has failed because the patch coverage (83.33%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #76439   +/-   ##
==========================================
  Coverage           ?   83.33%           
==========================================
  Files              ?        3           
  Lines              ?       12           
  Branches           ?        0           
==========================================
  Hits               ?       10           
  Misses             ?        2           
  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.

@zhwesky2010 zhwesky2010 requested a review from SigureMo November 20, 2025 06:41
@zhwesky2010
Copy link
Contributor

zhwesky2010 commented Nov 20, 2025

infoflow 2025-11-20 23-09-29

这里没过覆盖率的原因是:老IR没有测异常抛出,这里新增的每行代码都需要单测跑到,不然覆盖率CI过不了。

@blacksheep-Aristotle
Copy link
Contributor Author

/re-run all-failed

@zhwesky2010 zhwesky2010 merged commit daa67ab into PaddlePaddle:develop Nov 21, 2025
118 of 128 checks passed
@blacksheep-Aristotle blacksheep-Aristotle deleted the tensor_requires_grad branch November 26, 2025 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants