Skip to content

修改tensor.data的返回,返回的张量不包含梯度信息 #73246

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

Merged
merged 2 commits into from
Jun 12, 2025

Conversation

Qin-sx
Copy link
Contributor

@Qin-sx Qin-sx commented Jun 11, 2025

PR Category

User Experience

PR Types

Improvements

Description

tensor.data功能和pytorch对齐,返回不包含梯度信息的tensor
测试代码:

import numpy as np
import paddle
import torch

np_data = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float32)
paddle_tensor = paddle.to_tensor(np_data, stop_gradient=False)
paddle_tensor_data = paddle_tensor.data
print(paddle_tensor_data)

np_data = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float32)
torch_tensor = torch.tensor(np_data, requires_grad=True)
torch_tensor_data = torch_tensor.data
print(torch_tensor_data)

	modified:   paddle/fluid/pybind/eager_properties.cc
Copy link

paddle-bot bot commented Jun 11, 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 Jun 11, 2025
return reinterpret_cast<PyObject*>(self);
paddle::Tensor new_tensor(self->tensor.impl());

auto meta = std::make_shared<egr::AutogradMeta>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Tensor.data剥离了反向,应该不需要AutogradMeta了吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

嗯,收到,已修改。测试了一下,不需要重新设置stopgradient,新的tensor没有梯度信息。

	modified:   paddle/fluid/pybind/eager_properties.cc
@Qin-sx
Copy link
Contributor Author

Qin-sx commented Jun 12, 2025

CI / Mac-CPU / Build and test (pull_request)这个检查没有找到在哪里可以重新启动

@swgu98
Copy link
Contributor

swgu98 commented Jun 12, 2025

CI / Mac-CPU / Build and test (pull_request)这个检查没有找到在哪里可以重新启动

非required的失败不影响合入

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

@zhwesky2010 zhwesky2010 merged commit 137c06c into PaddlePaddle:develop Jun 12, 2025
49 of 50 checks passed
DrRyanHuang pushed a commit to DrRyanHuang/Paddle that referenced this pull request Jun 14, 2025
* modified tensor.data

	modified:   paddle/fluid/pybind/eager_properties.cc

* deleted meta

	modified:   paddle/fluid/pybind/eager_properties.cc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants