提交 4760fd96 编写于 作者: P phlrain

enable eager model; test=develop

上级 eaacf8bf
......@@ -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()
......
......@@ -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()
......@@ -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()
......
......@@ -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
......@@ -314,7 +315,7 @@ use_py_reader = False
sync = False
# how many batches we use
batch_num = 5
batch_num = 2
np.random.seed(90)
src_word_np = np.arange(1, TrainTaskConfig.batch_size * seq_len + 1).reshape(
......@@ -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()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册