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] Fix Error when generating train_program in eval mode #27975

Merged
merged 5 commits into from
Oct 16, 2020

Conversation

Aurelius84
Copy link
Contributor

@Aurelius84 Aurelius84 commented Oct 15, 2020

PR types

Function optimization

PR changes

Others

Describe

Fix Error when generating train_program in eval mode. See following example code:

class Net(nn.Layer):
    def __init__(self, in_channels, hidden_size):
        super(Net, self).__init__()
        self.lstm = nn.LSTM(
            in_channels, hidden_size, direction='bidirectional', num_layers=2)

    @paddle.jit.to_static
    def forward(self, x):
        x, _ = self.lstm(x)
        return x

net = Net(12, 2)
net.eval() # switch into eval mode firstly
# Here, we will get infer_program defaultly after converting dygraph layers into static.
# When initializing ParitalLayer, a train_program will be generated by `append_backward`
# from converted program(here is infer_program) with `is_test=True`, which will raise a error.
net = paddle.jit.to_static(
    net, input_spec=[paddle.static.InputSpec(shape=[-1, 10, 12])])
paddle.jit.save(net, 'simple_lstm')

Solution:

  1. Add change_is_test_status to make sure all is_test=True before append_backward
  2. Introduce lazy initialization mechanism to create program while only we need it.

@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 changed the title [Dy2Stat] Support jit.save in eval mode [Dy2Stat] Fix Error of generating train_program in eval mode Oct 16, 2020
@Aurelius84 Aurelius84 changed the title [Dy2Stat] Fix Error of generating train_program in eval mode [Dy2Stat] Fix Error when generating train_program in eval mode Oct 16, 2020
Copy link
Member

@zhhsplendid zhhsplendid 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 ffcc117 into PaddlePaddle:develop Oct 16, 2020
Aurelius84 added a commit that referenced this pull request Oct 16, 2020
* Fix save in eval mode

* remove assert statement

* fix test_partial_program failed

* add more test

* modify back into _train_program
Aurelius84 added a commit that referenced this pull request Oct 16, 2020
… (#28019)

* Fix save in eval mode

* remove assert statement

* fix test_partial_program failed

* add more test

* modify back into _train_program
@Aurelius84 Aurelius84 deleted the fix_save_in_eval_mode branch April 12, 2021 03:36
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