diff --git a/python/paddle/fluid/tests/unittests/test_imperative_ocr_attention_model.py b/python/paddle/fluid/tests/unittests/test_imperative_ocr_attention_model.py index 973c559857974a66ee1c06fc02305adf29c8f0ad..09868520b4c286c0d6b1f7c1cae160be5796199e 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_ocr_attention_model.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_ocr_attention_model.py @@ -22,6 +22,7 @@ from paddle.fluid import core from paddle.fluid.dygraph.nn import Conv2D, Pool2D, Linear, BatchNorm, Embedding, GRUUnit from paddle.fluid.dygraph.base import to_variable from test_imperative_base import new_program_scope +from paddle.fluid.framework import _test_eager_guard class Config(object): @@ -371,7 +372,7 @@ class OCRAttention(fluid.dygraph.Layer): class TestDygraphOCRAttention(unittest.TestCase): - def test_while_op(self): + def test_ocr_test(self): seed = 90 epoch_num = 1 if core.is_compiled_with_cuda(): @@ -400,7 +401,7 @@ class TestDygraphOCRAttention(unittest.TestCase): i * Config.max_length, dtype='int64').reshape([1, Config.max_length]))) - with fluid.dygraph.guard(): + def run_dygraph(): fluid.set_flags({'FLAGS_sort_sum_gradient': True}) paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -452,6 +453,16 @@ class TestDygraphOCRAttention(unittest.TestCase): for param in ocr_attention.parameters(): dy_param_value[param.name] = param.numpy() + return dy_out, dy_param_init_value, dy_param_value + + with fluid.dygraph.guard(): + dy_out, dy_param_init_value, dy_param_value = run_dygraph() + + with fluid.dygraph.guard(): + with _test_eager_guard(): + eager_out, eager_param_init_value, eager_param_value = run_dygraph( + ) + with new_program_scope(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -537,6 +548,17 @@ class TestDygraphOCRAttention(unittest.TestCase): for key, value in six.iteritems(static_param_value): self.assertTrue(np.allclose(value, dy_param_value[key], rtol=1e-05)) + # check eager here + self.assertTrue(np.allclose(static_out, eager_out)) + + for key, value in six.iteritems(static_param_init_value): + self.assertTrue(np.array_equal(value, eager_param_init_value[key])) + + for key, value in six.iteritems(static_param_value): + self.assertTrue( + np.allclose( + value, eager_param_value[key], rtol=1e-05)) + if __name__ == '__main__': paddle.enable_static() diff --git a/python/paddle/fluid/tests/unittests/test_imperative_reinforcement.py b/python/paddle/fluid/tests/unittests/test_imperative_reinforcement.py index a89628c594de9f7babd6a82ba178ba00829fccfe..08320d04d99964e61378076f05d71154940cd578 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_reinforcement.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_reinforcement.py @@ -27,6 +27,7 @@ from paddle.fluid.dygraph.nn import Conv2D, Pool2D, Linear import paddle.fluid.dygraph.nn as nn from paddle.fluid.dygraph.base import to_variable from test_imperative_base import new_program_scope +from paddle.fluid.framework import _test_eager_guard class Policy(fluid.dygraph.Layer): @@ -63,7 +64,7 @@ class TestImperativeMnist(unittest.TestCase): mask_list = [[0, 1]] mask = np.array(mask_list).astype("float32") - with fluid.dygraph.guard(): + def run_dygraph(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -104,6 +105,16 @@ class TestImperativeMnist(unittest.TestCase): for param in policy.parameters(): dy_param_value[param.name] = param.numpy() + return dy_out, dy_param_init_value, dy_param_value + + with fluid.dygraph.guard(): + dy_out, dy_param_init_value, dy_param_value = run_dygraph() + + with fluid.dygraph.guard(): + with _test_eager_guard(): + eager_out, eager_param_init_value, eager_param_value = run_dygraph( + ) + with new_program_scope(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -171,6 +182,16 @@ class TestImperativeMnist(unittest.TestCase): for key, value in six.iteritems(static_param_value): self.assertTrue(np.equal(value, dy_param_value[key]).all()) + # check eager + for key, value in six.iteritems(static_param_init_value): + self.assertTrue(np.equal(value, eager_param_init_value[key]).all()) + + self.assertTrue(np.equal(static_out, eager_out).all()) + + for key, value in six.iteritems(static_param_value): + self.assertTrue(np.equal(value, eager_param_value[key]).all()) + if __name__ == '__main__': + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_imperative_se_resnext.py b/python/paddle/fluid/tests/unittests/test_imperative_se_resnext.py index 8f8890557ad12328c88eedc25cb95a91f885eaf4..3fbb7f4cf7bb734892d55ff6440b51dd71ca2f3e 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_se_resnext.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_se_resnext.py @@ -24,6 +24,7 @@ from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear from paddle.fluid.dygraph.base import to_variable from test_imperative_base import new_program_scope +from paddle.fluid.framework import _test_eager_guard if fluid.is_compiled_with_cuda(): fluid.set_flags({'FLAGS_cudnn_deterministic': True}) @@ -310,7 +311,8 @@ class TestImperativeResneXt(unittest.TestCase): batch_size = train_parameters["batch_size"] batch_num = 1 epoch_num = 1 - with fluid.dygraph.guard(): + + def run_dygraph(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -371,6 +373,17 @@ class TestImperativeResneXt(unittest.TestCase): for param in se_resnext.parameters(): dy_param_value[param.name] = param.numpy() + return dy_out, dy_param_init_value, dy_param_value, dy_grad_value + + with fluid.dygraph.guard(): + dy_out, dy_param_init_value, dy_param_value, dy_grad_value = run_dygraph( + ) + + with fluid.dygraph.guard(): + with _test_eager_guard(): + eager_out, eager_param_init_value, eager_param_value, eager_grad_value = run_dygraph( + ) + with new_program_scope(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -479,6 +492,32 @@ class TestImperativeResneXt(unittest.TestCase): self.assertTrue(np.isfinite(value.all())) self.assertFalse(np.isnan(value.any())) + # check eager + self.assertTrue( + np.allclose(static_out, eager_out), + "\nstatic_out: {}\neager_out: {}".format(static_out, eager_out)) + + self.assertEqual( + len(eager_param_init_value), len(static_param_init_value)) + + for key, value in six.iteritems(static_param_init_value): + self.assertTrue(np.allclose(value, eager_param_init_value[key])) + + self.assertEqual(len(eager_grad_value), len(static_grad_value)) + + for key, value in six.iteritems(static_grad_value): + self.assertTrue( + np.allclose(value, eager_grad_value[key]), + "\nstatic_grad_value: {}\neager_grad_value: {}".format( + value, eager_grad_value[key])) + + self.assertEqual(len(eager_param_value), len(static_param_value)) + for key, value in six.iteritems(static_param_value): + self.assertTrue( + np.allclose(value, eager_param_value[key]), + "\nstatic_param_value: {}\neagear_param_value: {}".format( + value, eager_param_value[key])) + if __name__ == '__main__': paddle.enable_static() diff --git a/python/paddle/fluid/tests/unittests/test_imperative_transformer_sorted_gradient.py b/python/paddle/fluid/tests/unittests/test_imperative_transformer_sorted_gradient.py index 3f129cae44abb6e8e9b4d329558aea5167c96675..010c8aeccacd6550a5b8963c63f3d28af898550e 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_transformer_sorted_gradient.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_transformer_sorted_gradient.py @@ -21,6 +21,7 @@ from paddle.fluid import Embedding, LayerNorm, Linear, Layer from paddle.fluid.dygraph import to_variable, guard from paddle.fluid.dygraph import TracedLayer from test_imperative_base import new_program_scope +from paddle.fluid.framework import _test_eager_guard from paddle.fluid import core import numpy as np import six @@ -949,8 +950,7 @@ class TestDygraphTransformerSortGradient(unittest.TestCase): def transformer_sort_gradient_float32(self, is_sparse): seed = 90 - with guard(): - fluid.set_flags({'FLAGS_sort_sum_gradient': True}) + def run_dygraph(): # NOTE(xiongkun03): In new executor, the inplace strategy is on by default, which will cause result of sumop have some differences. So we disable inplace. fluid.set_flags({'FLAGS_new_executor_use_inplace': False}) paddle.seed(seed) @@ -998,7 +998,7 @@ class TestDygraphTransformerSortGradient(unittest.TestCase): for i in range(batch_num): enc_inputs, dec_inputs, label, weights = create_data() - if i % 2 == 0: + if False: outs, traced_layer = TracedLayer.trace( transformer, [enc_inputs, dec_inputs, label, weights]) @@ -1036,6 +1036,14 @@ class TestDygraphTransformerSortGradient(unittest.TestCase): dy_predict_value = dy_predict.numpy() dy_token_num_value = dy_token_num.numpy() + return dy_avg_cost_value, dy_sum_cost_value, dy_predict_value, dy_token_num_value, \ + dy_param_init, dy_param_updated + + with guard(): + fluid.set_flags({'FLAGS_sort_sum_gradient': True}) + dy_avg_cost_value, dy_sum_cost_value, dy_predict_value, dy_token_num_value, \ + dy_param_init, dy_param_updated = run_dygraph() + with new_program_scope(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -1122,6 +1130,28 @@ class TestDygraphTransformerSortGradient(unittest.TestCase): for key, value in six.iteritems(static_param_updated): self.assertTrue(np.array_equal(value, dy_param_updated[key])) + # check eager result + with guard(): + fluid.set_flags({'FLAGS_sort_sum_gradient': False}) + dy_avg_cost_value, dy_sum_cost_value, dy_predict_value, dy_token_num_value, \ + dy_param_init, dy_param_updated = run_dygraph() + + with guard(): + with _test_eager_guard(): + eager_avg_cost_value, eager_sum_cost_value, eager_predict_value, eager_token_num_value, \ + eager_param_init, eager_param_updated = run_dygraph() + self.assertTrue(np.allclose(dy_avg_cost_value, eager_avg_cost_value)) + self.assertTrue(np.allclose(dy_sum_cost_value, eager_sum_cost_value)) + + self.assertTrue(np.allclose(dy_predict_value, eager_predict_value)) + self.assertTrue(np.allclose(dy_token_num_value, eager_token_num_value)) + + for key, value in six.iteritems(static_param_init): + self.assertTrue(np.array_equal(value, eager_param_init[key])) + for key, value in six.iteritems(dy_param_updated): + self.assertTrue(np.allclose(value, eager_param_updated[key])) + if __name__ == '__main__': + paddle.enable_static() unittest.main()