KEMBAR78
[API compatibility] add the param name for paddle.Tensor.copy_ by zeroRains · Pull Request #74768 · PaddlePaddle/Paddle · GitHub
Skip to content

Conversation

@zeroRains
Copy link
Contributor

PR Category

User Experience

PR Types

New features

Description

对齐paddle.Tensor.copy_与torch.Tensor.copy_。

当前paddle.Tensor.copy_已经存在,并且在功能上与torch.Tensor.copy_对齐,但是在paddle内是基于pybind实现的方法,在参数处理上没有处理kwargs的逻辑并且没有返回值(torch里有返回值),本PR进行了补充与完善。

在PaConvert下自测,paddle.Tensor.copy_与torch.Tenso.copy_对齐。

pcard-71500

image

@paddle-bot
Copy link

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

"sure you are on the right way. "
"The expected arguments as follow: ("
"other, non_blocking)"));
PADDLE_ENFORCE_EQ(
Copy link
Contributor

Choose a reason for hiding this comment

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

前面判断了flag,这里应该不用再判断了?

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

common::errors::PreconditionNotMet(
"Must provide the `other: Tensor` params for paddle.Tensor.copy_"));

paddle::Tensor& src_tensor = CastPyArg2Tensor(other, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

这个命名成other_tensor合理一点

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_TRY
paddle::Tensor& src_tensor = CastPyArg2Tensor(PyTuple_GET_ITEM(args, 0), 0);
PyObject* other = nullptr;
bool blocking = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

之前使用paddle并指定blocking的代码会挂掉,这个要兼容的话 就是在blocking后面加1个non_blocking参数,只有torch的non_blocking未指定关键字时被误当做blocking,这个影响应比较小。

other, blocking, non_blocking

Copy link
Contributor Author

Choose a reason for hiding this comment

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

嗯?是指.copy_(a, blocking=True)这个调用会报错吗,如果是的话应该是因为之前的copy_里面只对args解析了,没有对kwargs进行解析所以会报错。如果是.copy_(a, True, non_blocking=True),这种情况,以non_blocking为主吗?

nullptr};
bool flag = PyArg_ParseTupleAndKeywords(
args, kwargs, "|Obb", kwlist, &other_tensor, &blocking, &non_blocking);
blocking = !blocking || non_blocking ? false : true;
Copy link
Contributor Author

@zeroRains zeroRains Aug 22, 2025

Choose a reason for hiding this comment

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

这个blocking设置的逻辑是,默认采用blocking的方式执行,只blocking参数为False或者non_blocking设置为True时,就使用non_blocking的方式执行

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

@zeroRains
Copy link
Contributor Author

/re-run all-failed

@zhwesky2010 zhwesky2010 merged commit 78b75d0 into PaddlePaddle:develop Aug 25, 2025
79 of 86 checks passed
@zeroRains zeroRains deleted the copy branch August 25, 2025 03:17
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.

4 participants