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

[Dy2Stat]Support convert sublayers in Sequential Container #32978

Merged
merged 3 commits into from
May 20, 2021

Conversation

Aurelius84
Copy link
Contributor

@Aurelius84 Aurelius84 commented May 19, 2021

PR types

New features

PR changes

Others

Describe

1. PR功能

支持 Paddle.nn.Sequential中的sublayer的代码转换。

之前对于paddle.nn.Sequential容器内的layers,由于在convert_call_func时,识别为Paddle内部API,因此会跳过转换。此Layer为一个容器,内部常包含很多用户定义的Layer,因此需要进行转换。

Sequential包含了Paddle自身的Layer,由于convert_call_func会递归判断,因此不会对Paddle内部的Layer误处理。

2. 代码样例

class SequentialNet(paddle.nn.Layer):
    def __init__(self, sub_layer, in_channel, out_channel):
        super(SequentialNet, self).__init__()
        self.layer = paddle.nn.Sequential(
            ('l1', paddle.nn.Linear(in_channel, in_channel)),  
            ('l2', paddle.nn.Linear(in_channel, out_channel)),
            ('l3', sub_layer(out_channel)))  # 仅会对此sub layer进行动转静

    def forward(self, x):
        out = self.layer(x)
        return out

class BufferLayers(paddle.nn.Layer):
    def __init__(self, out_channel):
        super(BufferLayers, self).__init__()
        self.out_channel = out_channel

    def forward(self, x):
        mean = paddle.mean(x)
        if mean < 0.:
            x = x * self._mask()

        out = x - mean
        return out

    def _mask(self):
        return paddle.to_tensor(np.zeros([self.out_channel], 'float32'))

@paddle-bot-old
Copy link

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

@Aurelius84 Aurelius84 requested review from liym27 and zhhsplendid May 19, 2021 02:41
liym27
liym27 previously approved these changes May 19, 2021
Copy link
Contributor

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

@liym27 liym27 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 e409c7c into PaddlePaddle:develop May 20, 2021
Aurelius84 added a commit to Aurelius84/Paddle that referenced this pull request May 24, 2021
…dle#32978)

* Support convert sublayers in Sequential Container

* remove paddle.jit.set_code_level
lanxianghit pushed a commit that referenced this pull request May 26, 2021
…ner (#32978) (#33065)

* Support convert sublayers in Sequential Container

* remove paddle.jit.set_code_level
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