Skip to content
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

add tensor clone #27953

Merged
merged 3 commits into from
Oct 15, 2020
Merged

Conversation

zhwesky2010
Copy link
Contributor

@zhwesky2010 zhwesky2010 commented Oct 14, 2020

PR types

New features

PR changes

APIs

Describe

  1. 增加了了Tensor的clone功能,clone之后的Tensor与原来Tensor不共享数据,一定会发生Tensor拷贝,且place/shape/dtype与之前完全一致,并且提供梯度回溯功能;
import paddle

x = paddle.to_tensor(1.0, stop_gradient=False)
clone_x = x.clone()
y = clone_x**2
y.backward()
print(clone_x.stop_gradient) # False
print(clone_x.grad)          # [2.0], support gradient propagation
print(x.stop_gradient)       # False
print(x.grad)                # [2.0], clone_x support gradient propagation for x

x = paddle.to_tensor(1.0)
clone_x = x.clone()
clone_x.stop_gradient = False
z = clone_x**3
z.backward()
print(clone_x.stop_gradient) # False
print(clone_x.grad)          # [3.0], support gradient propagation
print(x.stop_gradient) # True
print(x.grad)          # None

  1. 修改了assignOP的逻辑,分配out的Tensor时不再基于tracer的place,而是与input的Tensor位置保持一致,使得assign前后的place位置一致,因为2.0的Tensor是可以自由指定位置的,不一定与tracer相同;

与Tensor.detach的功能相关联,#27921

如果需要一个既分离计算图,又不共享数据内存的Tensor,请使用x.detach().clone()

import paddle
x = paddle.to_tensor(1.0, stop_gradient=False)
detach_clone_x = x.detach().clone()

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

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

@TCChenlong TCChenlong 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

@TCChenlong TCChenlong 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 bf412f4 into PaddlePaddle:develop Oct 15, 2020
chen-zhiyu pushed a commit to chen-zhiyu/Paddle that referenced this pull request Oct 15, 2020
* add tensor clone

* fix unittest test_var_base
zhwesky2010 added a commit to zhwesky2010/Paddle that referenced this pull request Oct 15, 2020
* add tensor clone

* fix unittest test_var_base
zhwesky2010 added a commit that referenced this pull request Oct 15, 2020
* add tensor clone (#27953)

* add tensor clone

* fix unittest test_var_base

* fix bug of tensor copy of CUDAPinnedPlace (#27966)
MingMingShangTian pushed a commit to MingMingShangTian/Paddle that referenced this pull request Oct 15, 2020
* add tensor clone

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

Successfully merging this pull request may close these issues.

None yet

4 participants