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 new inplace api Tensor.fill_, Tensor.zero_ #33829

Merged
merged 1 commit into from
Sep 14, 2021
Merged

add new inplace api Tensor.fill_, Tensor.zero_ #33829

merged 1 commit into from
Sep 14, 2021

Conversation

zhiboniu
Copy link
Contributor

@zhiboniu zhiboniu commented Jun 29, 2021

PR types

New features

PR changes

APIs

Describe

add new inplace api Tensor.fill_, Tensor.zero_

        #paddle.Tensor.fill_()示例:
        import paddle
        tensor = paddle.to_tensor([0, 1, 2, 3, 4])
        tensor.fill_(0)
        print(tensor.tolist())   #[0, 0, 0, 0, 0]


        #paddle.Tensor.zero_()示例:
        import paddle
        tensor = paddle.to_tensor([0, 1, 2, 3, 4])
        tensor.zero_()
        print(tensor.tolist())   #[0, 0, 0, 0, 0]

@paddle-bot-old
Copy link

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

@zhiboniu zhiboniu changed the title add new inplace api Tensor.fill_ and Tensor.zero_ add new inplace api Tensor.fill_, Tensor.zero_ and Tensor.mul_ Jul 2, 2021
@zhiboniu zhiboniu changed the title add new inplace api Tensor.fill_, Tensor.zero_ and Tensor.mul_ add new inplace api Tensor.fill_, Tensor.zero_ Jul 5, 2021
jeff41404
jeff41404 previously approved these changes Jul 9, 2021
Copy link
Contributor

@jeff41404 jeff41404 left a comment

Choose a reason for hiding this comment

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

lgtm

jzhang533
jzhang533 previously approved these changes Jul 10, 2021
Copy link
Contributor

@jzhang533 jzhang533 left a comment

Choose a reason for hiding this comment

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

lgtm

@paddle-bot-old
Copy link

Sorry to inform you that a82597a's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

@zhiboniu zhiboniu dismissed stale reviews from jzhang533 and jeff41404 via a0e661b July 21, 2021 06:47
@@ -37,6 +38,78 @@
__all__ = []


@dygraph_only
def fill_(x, fill_value):
Copy link
Contributor

Choose a reason for hiding this comment

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

这里value不需要区分,比较容易理解,建议直接用value
fill_value -> value

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

namespace paddle {
namespace operators {

class Fill_OpMaker : public framework::OpProtoAndCheckerMaker {
Copy link
Contributor

Choose a reason for hiding this comment

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

这种命名方式比较奇怪,用Fill_OpMaker来区分FillOpMaker,区分度太小,
建议用FillInplaceOpMaker这种方式来区分FillOpMaker
可以确认下其他InplaceOp的写法是什么样的

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,原来的区分的方式容易跟分隔符混淆,已修改为FillInplace的命名。

if not (isinstance(fill_value, Variable)):
fill_value = paddle.to_tensor(fill_value, dtype=x.dtype)
assert (fill_value.size == 1), "var should be size 1"
return core.ops.fill_inplace_(x, fill_value)
Copy link
Contributor

Choose a reason for hiding this comment

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

这里已经有inplace关键字了,不需要再加下划线
fill_inplace_ -> fill_inplace

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里实现inplace版本沿用之前的方式,通过注册DECLARE_INPLACE_OP_INFERER的方式。
所以实际会生成正常和inplace两个版本,只是我们这个api只需要inplace版本,所以调用op还是需要下划线

paddle/fluid/operators/fill_any_op.cc Outdated Show resolved Hide resolved
paddle/fluid/operators/fill_any_op.cc Outdated Show resolved Hide resolved
paddle/fluid/operators/fill_any_op.cc Outdated Show resolved Hide resolved
paddle/fluid/operators/fill_any_op.cc Outdated Show resolved Hide resolved
paddle/fluid/operators/fill_any_op.cc Outdated Show resolved Hide resolved
paddle/fluid/operators/fill_any_op.cc Outdated Show resolved Hide resolved
paddle/fluid/operators/fill_any_op.cc Outdated Show resolved Hide resolved
paddle/fluid/operators/fill_any_op.cc Outdated Show resolved Hide resolved
paddle/fluid/operators/fill_any_op.cc Outdated Show resolved Hide resolved
python/paddle/__init__.py Show resolved Hide resolved
python/paddle/fluid/tests/unittests/test_fill_any_op.py Outdated Show resolved Hide resolved
python/paddle/fluid/tests/unittests/test_fill_any_op.py Outdated Show resolved Hide resolved
python/paddle/tensor/manipulation.py Outdated Show resolved Hide resolved
tools/static_mode_white_list.py Show resolved Hide resolved
python/paddle/tensor/manipulation.py Show resolved Hide resolved
Xreki
Xreki previously approved these changes Sep 13, 2021
Copy link
Contributor

@Xreki Xreki left a comment

Choose a reason for hiding this comment

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

LGTM

XiaoguangHu01
XiaoguangHu01 previously approved these changes Sep 13, 2021
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

import paddle.fluid as fluid
fluid.set_flags({'FLAGS_eager_delete_tensor_gb': 1.0})
import paddle
paddle.set_flags({'FLAGS_eager_delete_tensor_gb': 1.0})
Copy link
Contributor

Choose a reason for hiding this comment

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

可以在set_flagsget_flags的docstring里加上对
https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/guides/flags/flags_cn.html
的链接。

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


Args:
x(Tensor): ``x`` is the Tensor we want to filled data inplace
value(Scale): ``value`` is the value to fill in x
Copy link
Contributor

Choose a reason for hiding this comment

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

to be filled

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


import paddle

tensor = paddle.to_tensor([0,1,2,3,4])
Copy link
Contributor

Choose a reason for hiding this comment

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

PEP8 规范的话, 逗号后面要有空格吧。

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

@@ -37,6 +38,74 @@
__all__ = []


@dygraph_only
def fill_(x, value):
Copy link
Contributor

Choose a reason for hiding this comment

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

out-place 版本我记得是paddle.full
这个为什么没用paddle.full_,而是用了paddle.fill_

Copy link
Contributor Author

Choose a reason for hiding this comment

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

paddle.full底层使用op fill_constant,没有反向传播,只能作为叶子节点。torch.fill_是可以反向的,所以新增了op,这样可以跟torch对齐



@dygraph_only
def zero_(x):
Copy link
Contributor

Choose a reason for hiding this comment

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

对应的outplace版本是paddle.zeros ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

是的

@zhiboniu zhiboniu dismissed stale reviews from XiaoguangHu01 and Xreki via d945633 September 13, 2021 10:41
Copy link
Contributor

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

@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

@jeff41404 jeff41404 left a comment

Choose a reason for hiding this comment

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

lgtm

@jeff41404 jeff41404 merged commit efeec79 into PaddlePaddle:develop Sep 14, 2021
AnnaTrainingG pushed a commit to AnnaTrainingG/Paddle that referenced this pull request Sep 29, 2021
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.

5 participants