From 05d6be7ee13fdece11fdcfd29e1556e7dbb8fecd Mon Sep 17 00:00:00 2001 From: Weilong Wu Date: Fri, 29 Apr 2022 11:32:12 +0800 Subject: [PATCH] [Eager] Remove enable_legacy_dygraph setting (#42363) * [Eager] Remove enable_legacy_dygraph setting * Add more tests --- .../dygraph_to_static/test_simnet_v2.py | 3 --- .../fluid/tests/unittests/test_dropout_op.py | 20 ++++++++++++++++++- .../fluid/tests/unittests/test_lbfgs.py | 3 --- .../fluid/tests/unittests/test_nan_inf.py | 2 -- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_simnet_v2.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_simnet_v2.py index ab836b088b0..872d419ff89 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_simnet_v2.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_simnet_v2.py @@ -20,9 +20,6 @@ import unittest from simnet_dygraph_model_v2 import BOW, HingeLoss -from paddle.fluid.framework import _enable_legacy_dygraph -_enable_legacy_dygraph() - SEED = 102 random.seed(SEED) diff --git a/python/paddle/fluid/tests/unittests/test_dropout_op.py b/python/paddle/fluid/tests/unittests/test_dropout_op.py index 20abeaec726..e8d4fc260b8 100644 --- a/python/paddle/fluid/tests/unittests/test_dropout_op.py +++ b/python/paddle/fluid/tests/unittests/test_dropout_op.py @@ -23,7 +23,6 @@ import paddle.static as static import paddle.fluid as fluid from paddle.fluid import Program, program_guard from paddle.fluid.framework import _test_eager_guard, _enable_legacy_dygraph -_enable_legacy_dygraph() import os from paddle import _C_ops @@ -979,6 +978,7 @@ class TestDropoutBackward(unittest.TestCase): ), self.cal_grad_downscale_in_infer(mask.numpy()))) def test_backward_upscale_train(self): + _enable_legacy_dygraph() for place in self.places: with fluid.dygraph.guard(place): @@ -1010,6 +1010,7 @@ class TestDropoutBackward(unittest.TestCase): ), self.cal_grad_upscale_train(mask.numpy(), prob))) def test_backward_upscale_train_2(self): + _enable_legacy_dygraph() for place in self.places: with fluid.dygraph.guard(place): @@ -1025,6 +1026,23 @@ class TestDropoutBackward(unittest.TestCase): np.allclose(input.gradient( ), self.cal_grad_upscale_train(mask.numpy(), prob))) + def test_backward_upscale_train_2_eager(self): + for place in self.places: + with fluid.dygraph.guard(place): + with _test_eager_guard(): + + prob = 0.3 + input = paddle.uniform([40, 40], dtype="float32") + input.stop_gradient = False + out, mask = _C_ops.final_state_dropout( + input, None, 0.3, False, "upscale_in_train", 0, False) + + out.backward() + + self.assertTrue( + np.allclose(input.gradient( + ), self.cal_grad_upscale_train(mask.numpy(), prob))) + class TestRandomValue(unittest.TestCase): def test_fixed_random_number(self): diff --git a/python/paddle/fluid/tests/unittests/test_lbfgs.py b/python/paddle/fluid/tests/unittests/test_lbfgs.py index 2cad4822b28..bb381874760 100644 --- a/python/paddle/fluid/tests/unittests/test_lbfgs.py +++ b/python/paddle/fluid/tests/unittests/test_lbfgs.py @@ -21,9 +21,6 @@ import paddle.nn.functional as F from paddle.incubate.optimizer.functional.lbfgs import minimize_lbfgs -from paddle.fluid.framework import _enable_legacy_dygraph -_enable_legacy_dygraph() - np.random.seed(123) diff --git a/python/paddle/fluid/tests/unittests/test_nan_inf.py b/python/paddle/fluid/tests/unittests/test_nan_inf.py index 9b11f6711af..84559048a2b 100644 --- a/python/paddle/fluid/tests/unittests/test_nan_inf.py +++ b/python/paddle/fluid/tests/unittests/test_nan_inf.py @@ -20,8 +20,6 @@ import os import sys import subprocess import paddle -from paddle.fluid.framework import _enable_legacy_dygraph -_enable_legacy_dygraph() paddle.enable_static() -- GitLab