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

Fix Layer train eval setting failed in static mode #29540

Conversation

chenwhql
Copy link
Contributor

@chenwhql chenwhql commented Dec 10, 2020

PR types

Bug fixes

PR changes

APIs

Describe

Fix Layer train eval setting failed in static mode

By our design, paddlle.nn.Layer also can be used in static graph mode, but Layer.train() and Layer.eval() can only be set in dygraph mode, if set in static mode, it willl throw like:

Traceback (most recent call last):
  File "train_mode.py", line 5, in <module>
    l.train()
  File "/usr/local/python2.7.15/lib/python2.7/site-packages/paddle/fluid/dygraph/layers.py", line 135, in train
    framework._dygraph_tracer().train_mode()
AttributeError: 'NoneType' object has no attribute 'train_mode'

related issue: #29534

This may cause some problem. For example, the behavior of paddle.nn.Dropoutin Layer.train() and Layer.eval() are diffferent, the 2.0 paddle.nn.Dropout implement as:

def forward(self, input):
        out = F.dropout(
            input,
            p=self.p,
            axis=self.axis,
            training=self.training,
            mode=self.mode,
            name=self.name)
        return out

It need self.training as argument of original dropout, if user want use paddle.nn.Dropout for test in static mode, it need to set Layer.eval() firstly.

So this PR fix this problem, only set framework._dygraph_tracer().train_mode() in dygraph mode.

@paddle-bot-old
Copy link

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

Comment on lines +138 to +139
if in_dygraph_mode():
framework._dygraph_tracer().train_mode()
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, _dygraph_tracer().train_mode() can be removed.

Copy link
Contributor Author

@chenwhql chenwhql Dec 10, 2020

Choose a reason for hiding this comment

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

agree, and need to revert dropout(is_test=None) to dropout(is_test=False) changed in #29064

Copy link
Contributor Author

@chenwhql chenwhql Dec 10, 2020

Choose a reason for hiding this comment

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

for compatibility, removing it in next PR is better, this PR need to be chery-pick into 2.0 to fix this bug

@chenwhql chenwhql merged commit c1a26e2 into PaddlePaddle:develop Dec 11, 2020
chenwhql added a commit to chenwhql/Paddle that referenced this pull request Dec 11, 2020
lanxianghit pushed a commit that referenced this pull request Dec 17, 2020
… (#29571)

Fix Layer train eval setting failed in static mode, more details please see #29540
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