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

[Dy2st]Fix error when set buffer in forward #38540

Merged
merged 8 commits into from
Jan 4, 2022

Conversation

0x45f
Copy link
Contributor

@0x45f 0x45f commented Dec 28, 2021

PR types

Bug fixes

PR changes

Others

Describe

#37296 合入后,下面的示例代码中存在两个问题

  1. 对于subnet的self.a,在动转静中是一个buffer变量,在forward中使用语句self.a = self.a + 10进行set时,由于取消了Layer.__call__中的param_guard操作,Layer._buffers变量不会被转为Variable,那么在Layer.__setattr__函数中最后会进入下图中的if中,assign产生一个新的variable,_buffers[name]也有原来的tensor变为了assig的variable,同时jit.save时会报错。
    image
    image

  2. 动转静中self.b变量被设置为None后重新被赋值为一个tensor,会产生1中类似错误,我们在代码中禁止了类似操作。

import paddle

class SubNet(paddle.nn.Layer):
    def __init__(self):
        super(SubNet, self).__init__()
        self.a = paddle.to_tensor([1])
        self.b = paddle.to_tensor([2])
    
    def forward(self, x):
        self.a = self.a + 10
        self.b = None
        self.b = paddle.to_tensor([3])
        return x

class Net(paddle.nn.Layer):
    def __init__(self):
        super(Net, self).__init__()
        self.sub_net = SubNet()

    @paddle.jit.to_static
    def forward(self, x):
        out = self.sub_net(x)
        return out

@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

@Aurelius84 Aurelius84 left a comment

Choose a reason for hiding this comment

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

LGTM

@Aurelius84 Aurelius84 merged commit 1e3f01e into PaddlePaddle:develop Jan 4, 2022
@0x45f 0x45f deleted the dy2st_fix_buffers_set branch January 4, 2022 06:29
0x45f added a commit to 0x45f/Paddle that referenced this pull request Jan 4, 2022
* fix error when set buffer in forward

* add unittest

* refine class name

* refine not framework.in_dygraph_mode() in if

* fix UT error

* add comment

* refine code

* remove useless import
lanxianghit pushed a commit that referenced this pull request Jan 5, 2022
… in dy2stat (#38418)

Fix error when calling sublayer's non-forward func in dy2stat
cherrypick: #37713#37759#37296#38540#37888
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.

2 participants