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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions python/paddle/fluid/dygraph/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,11 @@ def forward(self, input):
out = mylayer(x)

"""
# global setting
framework._dygraph_tracer().train_mode()
# global setting in dygraph
# NOTE(chenweihang): nn.Layer also can be used in static mode,
# but _dygraph_tracer() can not be called in static mode
if in_dygraph_mode():
framework._dygraph_tracer().train_mode()
Comment on lines +138 to +139
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

# Layer-level setting
self.training = True
for layer in self.sublayers():
Expand Down Expand Up @@ -170,8 +173,11 @@ def forward(self, input):
print(out)

"""
# global setting
framework._dygraph_tracer().eval_mode()
# global setting in dygraph
# NOTE(chenweihang): nn.Layer also can be used in static mode,
# but _dygraph_tracer() can not be called in static mode
if in_dygraph_mode():
framework._dygraph_tracer().eval_mode()
# Layer-level setting
self.training = False
for layer in self.sublayers():
Expand Down
17 changes: 17 additions & 0 deletions python/paddle/fluid/tests/unittests/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3701,6 +3701,23 @@ def test_layer_parameter_set(self):
self.assertFalse(net.weight.trainable)


class TestLayerTrainingAttribute(unittest.TestCase):
def test_set_train_eval_in_dynamic_mode(self):
with fluid.dygraph.guard():
net = paddle.nn.Dropout()
net.train()
self.assertTrue(net.training)
net.eval()
self.assertFalse(net.training)

def test_set_train_eval_in_static_mode(self):
net = paddle.nn.Dropout()
net.train()
self.assertTrue(net.training)
net.eval()
self.assertFalse(net.training)


if __name__ == '__main__':
paddle.enable_static()
unittest.main()