未验证 提交 16ce33b0 编写于 作者: A Aurelius84 提交者: GitHub

[Dy2Stat]Modify all jit.save path into tempfile under dygraph_to_static directory (#42842)

* [Dy2Stat]Modify all jit.save path into tempfile

* [Dy2Stat]Modify all jit.save path into tempfile
上级 a51817d7
...@@ -12,10 +12,12 @@ ...@@ -12,10 +12,12 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import os
import math import math
import numpy as np import numpy as np
import unittest import unittest
import paddle import paddle
import tempfile
from paddle.jit import to_static from paddle.jit import to_static
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid import ParamAttr from paddle.fluid import ParamAttr
...@@ -422,11 +424,6 @@ class Args(object): ...@@ -422,11 +424,6 @@ class Args(object):
prop_boundary_ratio = 0.5 prop_boundary_ratio = 0.5
num_sample = 2 num_sample = 2
num_sample_perbin = 2 num_sample_perbin = 2
model_save_dir = "./inference"
model_save_prefix = "./inference/bmn"
model_filename = "bmn" + INFER_MODEL_SUFFIX
params_filename = "bmn" + INFER_PARAMS_SUFFIX
dy_param_path = './bmn_dy_param'
def optimizer(cfg, parameter_list): def optimizer(cfg, parameter_list):
...@@ -559,7 +556,63 @@ def fake_data_reader(args, mode='train'): ...@@ -559,7 +556,63 @@ def fake_data_reader(args, mode='train'):
return reader return reader
def train_bmn(args, place, to_static): # Validation
def val_bmn(model, args):
val_reader = fake_data_reader(args, 'valid')
loss_data = []
for batch_id, data in enumerate(val_reader()):
video_feat = np.array([item[0] for item in data]).astype(DATATYPE)
gt_iou_map = np.array([item[1] for item in data]).astype(DATATYPE)
gt_start = np.array([item[2] for item in data]).astype(DATATYPE)
gt_end = np.array([item[3] for item in data]).astype(DATATYPE)
x_data = to_variable(video_feat)
gt_iou_map = to_variable(gt_iou_map)
gt_start = to_variable(gt_start)
gt_end = to_variable(gt_end)
gt_iou_map.stop_gradient = True
gt_start.stop_gradient = True
gt_end.stop_gradient = True
pred_bm, pred_start, pred_end = model(x_data)
loss, tem_loss, pem_reg_loss, pem_cls_loss = bmn_loss_func(
pred_bm, pred_start, pred_end, gt_iou_map, gt_start, gt_end, args)
avg_loss = fluid.layers.mean(loss)
loss_data += [
avg_loss.numpy()[0], tem_loss.numpy()[0], pem_reg_loss.numpy()[0],
pem_cls_loss.numpy()[0]
]
print('[VALID] iter {} '.format(batch_id)
+ '\tLoss = {}, \ttem_loss = {}, \tpem_reg_loss = {}, \tpem_cls_loss = {}'.format(
'%f' % avg_loss.numpy()[0], '%f' % tem_loss.numpy()[0], \
'%f' % pem_reg_loss.numpy()[0], '%f' % pem_cls_loss.numpy()[0]))
if batch_id == args.valid_batch_num:
break
return loss_data
class TestTrain(unittest.TestCase):
def setUp(self):
self.args = Args()
self.place = fluid.CPUPlace() if not fluid.is_compiled_with_cuda() \
else fluid.CUDAPlace(0)
self.temp_dir = tempfile.TemporaryDirectory()
self.model_save_dir = os.path.join(self.temp_dir.name, 'inference')
self.model_save_prefix = os.path.join(self.model_save_dir, 'bmn')
self.model_filename = "bmn" + INFER_MODEL_SUFFIX
self.params_filename = "bmn" + INFER_PARAMS_SUFFIX
self.dy_param_path = os.path.join(self.temp_dir.name, 'bmn_dy_param')
def tearDown(self):
self.temp_dir.cleanup()
def train_bmn(self, args, place, to_static):
program_translator.enable(to_static) program_translator.enable(to_static)
loss_data = [] loss_data = []
...@@ -580,8 +633,10 @@ def train_bmn(args, place, to_static): ...@@ -580,8 +633,10 @@ def train_bmn(args, place, to_static):
[item[0] for item in data]).astype(DATATYPE) [item[0] for item in data]).astype(DATATYPE)
gt_iou_map = np.array( gt_iou_map = np.array(
[item[1] for item in data]).astype(DATATYPE) [item[1] for item in data]).astype(DATATYPE)
gt_start = np.array([item[2] for item in data]).astype(DATATYPE) gt_start = np.array(
gt_end = np.array([item[3] for item in data]).astype(DATATYPE) [item[2] for item in data]).astype(DATATYPE)
gt_end = np.array(
[item[3] for item in data]).astype(DATATYPE)
x_data = to_variable(video_feat) x_data = to_variable(video_feat)
gt_iou_map = to_variable(gt_iou_map) gt_iou_map = to_variable(gt_iou_map)
...@@ -594,8 +649,8 @@ def train_bmn(args, place, to_static): ...@@ -594,8 +649,8 @@ def train_bmn(args, place, to_static):
pred_bm, pred_start, pred_end = bmn(x_data) pred_bm, pred_start, pred_end = bmn(x_data)
loss, tem_loss, pem_reg_loss, pem_cls_loss = bmn_loss_func( loss, tem_loss, pem_reg_loss, pem_cls_loss = bmn_loss_func(
pred_bm, pred_start, pred_end, gt_iou_map, gt_start, gt_end, pred_bm, pred_start, pred_end, gt_iou_map, gt_start,
args) gt_end, args)
avg_loss = fluid.layers.mean(loss) avg_loss = fluid.layers.mean(loss)
avg_loss.backward() avg_loss.backward()
...@@ -623,64 +678,17 @@ def train_bmn(args, place, to_static): ...@@ -623,64 +678,17 @@ def train_bmn(args, place, to_static):
if batch_id == args.train_batch_num: if batch_id == args.train_batch_num:
if to_static: if to_static:
fluid.dygraph.jit.save(bmn, args.model_save_prefix) fluid.dygraph.jit.save(bmn, self.model_save_prefix)
else: else:
fluid.dygraph.save_dygraph(bmn.state_dict(), fluid.dygraph.save_dygraph(bmn.state_dict(),
args.dy_param_path) self.dy_param_path)
break break
return np.array(loss_data) return np.array(loss_data)
# Validation
def val_bmn(model, args):
val_reader = fake_data_reader(args, 'valid')
loss_data = []
for batch_id, data in enumerate(val_reader()):
video_feat = np.array([item[0] for item in data]).astype(DATATYPE)
gt_iou_map = np.array([item[1] for item in data]).astype(DATATYPE)
gt_start = np.array([item[2] for item in data]).astype(DATATYPE)
gt_end = np.array([item[3] for item in data]).astype(DATATYPE)
x_data = to_variable(video_feat)
gt_iou_map = to_variable(gt_iou_map)
gt_start = to_variable(gt_start)
gt_end = to_variable(gt_end)
gt_iou_map.stop_gradient = True
gt_start.stop_gradient = True
gt_end.stop_gradient = True
pred_bm, pred_start, pred_end = model(x_data)
loss, tem_loss, pem_reg_loss, pem_cls_loss = bmn_loss_func(
pred_bm, pred_start, pred_end, gt_iou_map, gt_start, gt_end, args)
avg_loss = fluid.layers.mean(loss)
loss_data += [
avg_loss.numpy()[0], tem_loss.numpy()[0], pem_reg_loss.numpy()[0],
pem_cls_loss.numpy()[0]
]
print('[VALID] iter {} '.format(batch_id)
+ '\tLoss = {}, \ttem_loss = {}, \tpem_reg_loss = {}, \tpem_cls_loss = {}'.format(
'%f' % avg_loss.numpy()[0], '%f' % tem_loss.numpy()[0], \
'%f' % pem_reg_loss.numpy()[0], '%f' % pem_cls_loss.numpy()[0]))
if batch_id == args.valid_batch_num:
break
return loss_data
class TestTrain(unittest.TestCase):
def setUp(self):
self.args = Args()
self.place = fluid.CPUPlace() if not fluid.is_compiled_with_cuda() \
else fluid.CUDAPlace(0)
def test_train(self): def test_train(self):
static_res = train_bmn(self.args, self.place, to_static=True) static_res = self.train_bmn(self.args, self.place, to_static=True)
dygraph_res = train_bmn(self.args, self.place, to_static=False) dygraph_res = self.train_bmn(self.args, self.place, to_static=False)
self.assertTrue( self.assertTrue(
np.allclose(dygraph_res, static_res), np.allclose(dygraph_res, static_res),
"dygraph_res: {},\n static_res: {}".format( "dygraph_res: {},\n static_res: {}".format(
...@@ -726,8 +734,7 @@ class TestTrain(unittest.TestCase): ...@@ -726,8 +734,7 @@ class TestTrain(unittest.TestCase):
with fluid.dygraph.guard(self.place): with fluid.dygraph.guard(self.place):
bmn = BMN(self.args) bmn = BMN(self.args)
# load dygraph trained parameters # load dygraph trained parameters
model_dict, _ = fluid.load_dygraph(self.args.dy_param_path + model_dict, _ = fluid.load_dygraph(self.dy_param_path + ".pdparams")
".pdparams")
bmn.set_dict(model_dict) bmn.set_dict(model_dict)
bmn.eval() bmn.eval()
...@@ -743,10 +750,10 @@ class TestTrain(unittest.TestCase): ...@@ -743,10 +750,10 @@ class TestTrain(unittest.TestCase):
# load inference model # load inference model
[inference_program, feed_target_names, [inference_program, feed_target_names,
fetch_targets] = fluid.io.load_inference_model( fetch_targets] = fluid.io.load_inference_model(
self.args.model_save_dir, self.model_save_dir,
executor=exe, executor=exe,
model_filename=self.args.model_filename, model_filename=self.model_filename,
params_filename=self.args.params_filename) params_filename=self.params_filename)
pred_res = exe.run(inference_program, pred_res = exe.run(inference_program,
feed={feed_target_names[0]: data}, feed={feed_target_names[0]: data},
fetch_list=fetch_targets) fetch_list=fetch_targets)
...@@ -755,7 +762,7 @@ class TestTrain(unittest.TestCase): ...@@ -755,7 +762,7 @@ class TestTrain(unittest.TestCase):
def predict_dygraph_jit(self, data): def predict_dygraph_jit(self, data):
with fluid.dygraph.guard(self.place): with fluid.dygraph.guard(self.place):
bmn = fluid.dygraph.jit.load(self.args.model_save_prefix) bmn = fluid.dygraph.jit.load(self.model_save_prefix)
bmn.eval() bmn.eval()
x = to_variable(data) x = to_variable(data)
...@@ -765,9 +772,8 @@ class TestTrain(unittest.TestCase): ...@@ -765,9 +772,8 @@ class TestTrain(unittest.TestCase):
return pred_res return pred_res
def predict_analysis_inference(self, data): def predict_analysis_inference(self, data):
output = PredictorTools(self.args.model_save_dir, output = PredictorTools(self.model_save_dir, self.model_filename,
self.args.model_filename, self.params_filename, [data])
self.args.params_filename, [data])
out = output() out = output()
return out return out
......
...@@ -12,9 +12,11 @@ ...@@ -12,9 +12,11 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import os
import paddle import paddle
import unittest import unittest
import numpy as np import numpy as np
import tempfile
class BufferLayers(paddle.nn.Layer): class BufferLayers(paddle.nn.Layer):
...@@ -66,11 +68,15 @@ class TestSequential(unittest.TestCase): ...@@ -66,11 +68,15 @@ class TestSequential(unittest.TestCase):
def setUp(self): def setUp(self):
paddle.set_device('cpu') paddle.set_device('cpu')
self.seed = 2021 self.seed = 2021
self.temp_dir = tempfile.TemporaryDirectory()
self._init_config() self._init_config()
def _init_config(self): def _init_config(self):
self.net = SequentialNet(BufferLayers, 10, 3) self.net = SequentialNet(BufferLayers, 10, 3)
self.model_path = './sequential_net' self.model_path = os.path.join(self.temp_dir.name, 'sequential_net')
def tearDown(self):
self.temp_dir.cleanup()
def _init_seed(self): def _init_seed(self):
paddle.seed(self.seed) paddle.seed(self.seed)
...@@ -108,7 +114,8 @@ class TestSequential(unittest.TestCase): ...@@ -108,7 +114,8 @@ class TestSequential(unittest.TestCase):
class TestNestSequential(TestSequential): class TestNestSequential(TestSequential):
def _init_config(self): def _init_config(self):
self.net = NestSequentialNet() self.net = NestSequentialNet()
self.model_path = './nested_sequential_net' self.model_path = os.path.join(self.temp_dir.name,
'nested_sequential_net')
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -14,7 +14,8 @@ ...@@ -14,7 +14,8 @@
import numpy as np import numpy as np
import unittest import unittest
import os
import tempfile
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.static import InputSpec from paddle.static import InputSpec
...@@ -100,7 +101,11 @@ class TestStaticFunctionInstance(unittest.TestCase): ...@@ -100,7 +101,11 @@ class TestStaticFunctionInstance(unittest.TestCase):
class TestInputSpec(unittest.TestCase): class TestInputSpec(unittest.TestCase):
def setUp(self): def setUp(self):
pass self.temp_dir = tempfile.TemporaryDirectory()
self.model_path = os.path.join(self.temp_dir.name, 'simple_net')
def tearDown(self):
self.temp_dir.cleanup()
def test_with_input_spec(self): def test_with_input_spec(self):
with fluid.dygraph.guard(fluid.CPUPlace()): with fluid.dygraph.guard(fluid.CPUPlace()):
...@@ -116,8 +121,8 @@ class TestInputSpec(unittest.TestCase): ...@@ -116,8 +121,8 @@ class TestInputSpec(unittest.TestCase):
# 2. test save load # 2. test save load
net.inner_function(x) net.inner_function(x)
jit.save(net, './simple_net') jit.save(net, self.model_path)
infer_net = fluid.dygraph.jit.load('./simple_net') infer_net = fluid.dygraph.jit.load(self.model_path)
pred = infer_net(x) pred = infer_net(x)
self.assertTrue(np.allclose(out.numpy(), pred.numpy())) self.assertTrue(np.allclose(out.numpy(), pred.numpy()))
...@@ -438,12 +443,19 @@ class SetBuffersNet2(paddle.nn.Layer): ...@@ -438,12 +443,19 @@ class SetBuffersNet2(paddle.nn.Layer):
class TestSetBuffers(unittest.TestCase): class TestSetBuffers(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.model_path = os.path.join(self.temp_dir.name, 'SetBuffersNet1')
def tearDown(self):
self.temp_dir.cleanup()
def test_set_buffers1(self): def test_set_buffers1(self):
paddle.disable_static() paddle.disable_static()
net = SetBuffersNet1() net = SetBuffersNet1()
out = net() out = net()
self.assertEqual(out.numpy().tolist(), [2]) self.assertEqual(out.numpy().tolist(), [2])
paddle.jit.save(net, './SetBuffersNet1') paddle.jit.save(net, self.model_path)
paddle.enable_static() paddle.enable_static()
def test_set_buffers2(self): def test_set_buffers2(self):
......
...@@ -16,6 +16,8 @@ from __future__ import print_function ...@@ -16,6 +16,8 @@ from __future__ import print_function
import numpy as np import numpy as np
import unittest import unittest
import os
import tempfile
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
...@@ -532,12 +534,20 @@ class TestForOriginalTuple(TestTransformForOriginalList): ...@@ -532,12 +534,20 @@ class TestForOriginalTuple(TestTransformForOriginalList):
class TestForZip(unittest.TestCase): class TestForZip(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
def test_for_zip_error(self): def test_for_zip_error(self):
with self.assertRaises(RuntimeError): with self.assertRaises(RuntimeError):
paddle.jit.save(for_zip_error, './for_zip_error') model_path = os.path.join(self.temp_dir.name, 'for_zip_error')
paddle.jit.save(for_zip_error, model_path)
def test_for_zip(self): def test_for_zip(self):
paddle.jit.save(for_zip, './for_zip') model_path = os.path.join(self.temp_dir.name, 'for_zip')
paddle.jit.save(for_zip, model_path)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -17,6 +17,8 @@ from __future__ import print_function ...@@ -17,6 +17,8 @@ from __future__ import print_function
import numpy as np import numpy as np
import paddle import paddle
import unittest import unittest
import os
import tempfile
class GradLayer(paddle.nn.Layer): class GradLayer(paddle.nn.Layer):
...@@ -88,8 +90,15 @@ class TestGradLinear(TestGrad): ...@@ -88,8 +90,15 @@ class TestGradLinear(TestGrad):
self.func = GradLinearLayer() self.func = GradLinearLayer()
self.x = paddle.ones(shape=[10, 2, 5], dtype='float32') self.x = paddle.ones(shape=[10, 2, 5], dtype='float32')
self.x.stop_gradient = False self.x.stop_gradient = False
self.infer_model_path = "double_grad_infer_model"
self.train_model_path = "double_grad_train_model" self.temp_dir = tempfile.TemporaryDirectory()
self.infer_model_path = os.path.join(self.temp_dir.name,
'double_grad_infer_model')
self.train_model_path = os.path.join(self.temp_dir.name,
'double_grad_train_model')
def tearDown(self):
self.temp_dir.cleanup()
def test_save_infer_program(self): def test_save_infer_program(self):
input_spec = [ input_spec = [
...@@ -113,7 +122,7 @@ class TestGradLinear(TestGrad): ...@@ -113,7 +122,7 @@ class TestGradLinear(TestGrad):
avg_loss = paddle.mean(paddle.abs(out - 1)) avg_loss = paddle.mean(paddle.abs(out - 1))
avg_loss.backward() avg_loss.backward()
optimizer.minimize(avg_loss) optimizer.minimize(avg_loss)
print(self.x.grad.mean())
self.func.clear_gradients() self.func.clear_gradients()
paddle.jit.save(self.func, self.train_model_path) paddle.jit.save(self.func, self.train_model_path)
...@@ -129,8 +138,15 @@ class TestNoGradLinear(TestGradLinear): ...@@ -129,8 +138,15 @@ class TestNoGradLinear(TestGradLinear):
self.func = NoGradLinearLayer() self.func = NoGradLinearLayer()
self.x = paddle.ones(shape=[10, 2, 5], dtype='float32') self.x = paddle.ones(shape=[10, 2, 5], dtype='float32')
self.x.stop_gradient = False self.x.stop_gradient = False
self.infer_model_path = "no_grad_infer_model"
self.train_model_path = "no_grad_train_model" self.temp_dir = tempfile.TemporaryDirectory()
self.infer_model_path = os.path.join(self.temp_dir.name,
'no_grad_infer_model')
self.train_model_path = os.path.join(self.temp_dir.name,
'no_grad_train_model')
def tearDown(self):
self.temp_dir.cleanup()
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -19,6 +19,7 @@ import numpy as np ...@@ -19,6 +19,7 @@ import numpy as np
import unittest import unittest
import os import os
import tempfile
os.environ["CUDA_VISIBLE_DEVICES"] = "2" os.environ["CUDA_VISIBLE_DEVICES"] = "2"
import paddle import paddle
...@@ -406,11 +407,6 @@ class Args(object): ...@@ -406,11 +407,6 @@ class Args(object):
base_learning_rate = 0.01 base_learning_rate = 0.01
bigru_num = 2 bigru_num = 2
print_steps = 1 print_steps = 1
model_save_dir = "./inference"
model_save_prefix = "./inference/lac"
model_filename = "lac" + INFER_MODEL_SUFFIX
params_filename = "lac" + INFER_PARAMS_SUFFIX
dy_param_path = "./lac_dy_param"
def get_random_input_data(batch_size, vocab_size, num_labels, max_seq_len=64): def get_random_input_data(batch_size, vocab_size, num_labels, max_seq_len=64):
...@@ -458,7 +454,19 @@ def create_dataloader(reader, place): ...@@ -458,7 +454,19 @@ def create_dataloader(reader, place):
return data_loader return data_loader
def do_train(args, to_static): class TestLACModel(unittest.TestCase):
def setUp(self):
self.args = Args()
self.place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda(
) else fluid.CPUPlace()
self.temp_dir = tempfile.TemporaryDirectory()
self.model_save_dir = os.path.join(self.temp_dir.name, 'inference')
self.model_save_prefix = os.path.join(self.model_save_dir, 'lac')
self.model_filename = "lac" + INFER_MODEL_SUFFIX
self.params_filename = "lac" + INFER_PARAMS_SUFFIX
self.dy_param_path = os.path.join(self.temp_dir.name, 'lac_dy_param')
def train(self, args, to_static):
program_translator.enable(to_static) program_translator.enable(to_static)
place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda( place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda(
) else fluid.CPUPlace() ) else fluid.CPUPlace()
...@@ -514,28 +522,18 @@ def do_train(args, to_static): ...@@ -514,28 +522,18 @@ def do_train(args, to_static):
if to_static: if to_static:
fluid.dygraph.jit.save( fluid.dygraph.jit.save(
layer=model, layer=model,
path=args.model_save_prefix, path=self.model_save_prefix,
input_spec=[input_specs[0], input_specs[-1]], input_spec=[input_specs[0], input_specs[-1]],
output_spec=[crf_decode]) output_spec=[crf_decode])
else: else:
fluid.dygraph.save_dygraph(model.state_dict(), args.dy_param_path) fluid.dygraph.save_dygraph(model.state_dict(),
self.dy_param_path)
return np.array(loss_data) return np.array(loss_data)
class TestLACModel(unittest.TestCase):
def setUp(self):
self.args = Args()
self.place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda(
) else fluid.CPUPlace()
def train(self, to_static):
out = do_train(self.args, to_static)
return out
def test_train(self): def test_train(self):
st_out = self.train(to_static=True) st_out = self.train(self.args, to_static=True)
dy_out = self.train(to_static=False) dy_out = self.train(self.args, to_static=False)
self.assertTrue( self.assertTrue(
np.allclose(dy_out, st_out), np.allclose(dy_out, st_out),
msg="dygraph output:\n{},\nstatic output:\n {}.".format(dy_out, msg="dygraph output:\n{},\nstatic output:\n {}.".format(dy_out,
...@@ -565,8 +563,7 @@ class TestLACModel(unittest.TestCase): ...@@ -565,8 +563,7 @@ class TestLACModel(unittest.TestCase):
with fluid.dygraph.guard(self.place): with fluid.dygraph.guard(self.place):
model = LexNet(self.args) model = LexNet(self.args)
# load dygraph trained parameters # load dygraph trained parameters
model_dict, _ = fluid.load_dygraph(self.args.dy_param_path + model_dict, _ = fluid.load_dygraph(self.dy_param_path + ".pdparams")
".pdparams")
model.set_dict(model_dict) model.set_dict(model_dict)
model.eval() model.eval()
...@@ -585,10 +582,10 @@ class TestLACModel(unittest.TestCase): ...@@ -585,10 +582,10 @@ class TestLACModel(unittest.TestCase):
# load inference model # load inference model
[inference_program, feed_target_names, [inference_program, feed_target_names,
fetch_targets] = fluid.io.load_inference_model( fetch_targets] = fluid.io.load_inference_model(
self.args.model_save_dir, self.model_save_dir,
executor=exe, executor=exe,
model_filename=self.args.model_filename, model_filename=self.model_filename,
params_filename=self.args.params_filename) params_filename=self.params_filename)
words, targets, length = batch words, targets, length = batch
pred_res = exe.run( pred_res = exe.run(
...@@ -601,7 +598,7 @@ class TestLACModel(unittest.TestCase): ...@@ -601,7 +598,7 @@ class TestLACModel(unittest.TestCase):
def predict_dygraph_jit(self, batch): def predict_dygraph_jit(self, batch):
words, targets, length = batch words, targets, length = batch
with fluid.dygraph.guard(self.place): with fluid.dygraph.guard(self.place):
model = fluid.dygraph.jit.load(self.args.model_save_prefix) model = fluid.dygraph.jit.load(self.model_save_prefix)
model.eval() model.eval()
pred_res = model(to_variable(words), to_variable(length)) pred_res = model(to_variable(words), to_variable(length))
......
...@@ -14,8 +14,9 @@ ...@@ -14,8 +14,9 @@
import unittest import unittest
import paddle import paddle
import os
import numpy as np import numpy as np
import tempfile
def forward_post_hook1(layer, input, output): def forward_post_hook1(layer, input, output):
...@@ -54,7 +55,11 @@ class TestNestLayerHook(unittest.TestCase): ...@@ -54,7 +55,11 @@ class TestNestLayerHook(unittest.TestCase):
def setUp(self): def setUp(self):
paddle.seed(2022) paddle.seed(2022)
self.x = paddle.randn([4, 10]) self.x = paddle.randn([4, 10])
self.path = "./net_hook" self.temp_dir = tempfile.TemporaryDirectory()
self.path = os.path.join(self.temp_dir.name, 'net_hook')
def tearDown(self):
self.temp_dir.cleanup()
def train_net(self, to_static=False): def train_net(self, to_static=False):
paddle.seed(2022) paddle.seed(2022)
......
...@@ -16,6 +16,8 @@ import numpy as np ...@@ -16,6 +16,8 @@ import numpy as np
import paddle import paddle
import unittest import unittest
from paddle import nn from paddle import nn
import os
import tempfile
class LSTMLayer(nn.Layer): class LSTMLayer(nn.Layer):
...@@ -40,6 +42,12 @@ class Net(nn.Layer): ...@@ -40,6 +42,12 @@ class Net(nn.Layer):
class TestLstm(unittest.TestCase): class TestLstm(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
def run_lstm(self, to_static): def run_lstm(self, to_static):
paddle.jit.ProgramTranslator().enable(to_static) paddle.jit.ProgramTranslator().enable(to_static)
...@@ -78,11 +86,12 @@ class TestLstm(unittest.TestCase): ...@@ -78,11 +86,12 @@ class TestLstm(unittest.TestCase):
x = paddle.randn((2, 10, 12)) x = paddle.randn((2, 10, 12))
net = paddle.jit.to_static( net = paddle.jit.to_static(
net, input_spec=[paddle.static.InputSpec(shape=[-1, 10, 12])]) net, input_spec=[paddle.static.InputSpec(shape=[-1, 10, 12])])
paddle.jit.save(net, 'simple_lstm') model_path = os.path.join(self.temp_dir.name, 'simple_lstm')
paddle.jit.save(net, model_path)
dygraph_out = net(x) dygraph_out = net(x)
# load saved model # load saved model
load_net = paddle.jit.load('simple_lstm') load_net = paddle.jit.load(model_path)
static_out = load_net(x) static_out = load_net(x)
self.assertTrue( self.assertTrue(
...@@ -115,6 +124,12 @@ class LinearNet(nn.Layer): ...@@ -115,6 +124,12 @@ class LinearNet(nn.Layer):
class TestSaveInEvalMode(unittest.TestCase): class TestSaveInEvalMode(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
def test_save_in_eval(self): def test_save_in_eval(self):
paddle.jit.ProgramTranslator().enable(True) paddle.jit.ProgramTranslator().enable(True)
net = LinearNet() net = LinearNet()
...@@ -131,9 +146,11 @@ class TestSaveInEvalMode(unittest.TestCase): ...@@ -131,9 +146,11 @@ class TestSaveInEvalMode(unittest.TestCase):
# save directly # save directly
net = paddle.jit.to_static( net = paddle.jit.to_static(
net, input_spec=[paddle.static.InputSpec(shape=[-1, 10])]) net, input_spec=[paddle.static.InputSpec(shape=[-1, 10])])
paddle.jit.save(net, 'linear_net')
model_path = os.path.join(self.temp_dir.name, 'linear_net')
paddle.jit.save(net, model_path)
# load saved model # load saved model
load_net = paddle.jit.load('linear_net') load_net = paddle.jit.load(model_path)
x = paddle.randn((2, 10)) x = paddle.randn((2, 10))
eval_out = net(x) eval_out = net(x)
...@@ -146,6 +163,12 @@ class TestSaveInEvalMode(unittest.TestCase): ...@@ -146,6 +163,12 @@ class TestSaveInEvalMode(unittest.TestCase):
class TestEvalAfterSave(unittest.TestCase): class TestEvalAfterSave(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
def test_eval_after_save(self): def test_eval_after_save(self):
x = paddle.randn((2, 10, 12)).astype('float32') x = paddle.randn((2, 10, 12)).astype('float32')
net = Net(12, 2) net = Net(12, 2)
...@@ -159,8 +182,9 @@ class TestEvalAfterSave(unittest.TestCase): ...@@ -159,8 +182,9 @@ class TestEvalAfterSave(unittest.TestCase):
x = paddle.randn((2, 10, 12)).astype('float32') x = paddle.randn((2, 10, 12)).astype('float32')
dy_out = net(x) dy_out = net(x)
# save model # save model
paddle.jit.save(net, 'jit.save/lstm', input_spec=[x]) model_path = os.path.join(self.temp_dir.name, 'jit.save/lstm')
load_net = paddle.jit.load('jit.save/lstm') paddle.jit.save(net, model_path, input_spec=[x])
load_net = paddle.jit.load(model_path)
load_out = load_net(x) load_out = load_net(x)
self.assertTrue(np.allclose(dy_out.numpy(), load_out.numpy())) self.assertTrue(np.allclose(dy_out.numpy(), load_out.numpy()))
# eval # eval
......
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
from __future__ import print_function from __future__ import print_function
import unittest import unittest
import os
import tempfile
from time import time from time import time
import numpy as np import numpy as np
...@@ -134,6 +136,10 @@ class TestMNIST(unittest.TestCase): ...@@ -134,6 +136,10 @@ class TestMNIST(unittest.TestCase):
paddle.dataset.mnist.train(), paddle.dataset.mnist.train(),
batch_size=self.batch_size, batch_size=self.batch_size,
drop_last=True) drop_last=True)
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
class TestMNISTWithToStatic(TestMNIST): class TestMNISTWithToStatic(TestMNIST):
...@@ -227,9 +233,10 @@ class TestMNISTWithToStatic(TestMNIST): ...@@ -227,9 +233,10 @@ class TestMNISTWithToStatic(TestMNIST):
def check_jit_save_load(self, model, inputs, input_spec, to_static, gt_out): def check_jit_save_load(self, model, inputs, input_spec, to_static, gt_out):
if to_static: if to_static:
infer_model_path = "./test_mnist_inference_model_by_jit_save" infer_model_path = os.path.join(
model_save_dir = "./inference" self.temp_dir.name, 'test_mnist_inference_model_by_jit_save')
model_save_prefix = "./inference/mnist" model_save_dir = os.path.join(self.temp_dir.name, 'inference')
model_save_prefix = os.path.join(model_save_dir, 'mnist')
model_filename = "mnist" + INFER_MODEL_SUFFIX model_filename = "mnist" + INFER_MODEL_SUFFIX
params_filename = "mnist" + INFER_PARAMS_SUFFIX params_filename = "mnist" + INFER_PARAMS_SUFFIX
fluid.dygraph.jit.save( fluid.dygraph.jit.save(
......
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import os
import tempfile
import time import time
import numpy as np import numpy as np
import paddle import paddle
...@@ -439,11 +441,11 @@ class Args(object): ...@@ -439,11 +441,11 @@ class Args(object):
train_step = 10 train_step = 10
place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda( place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda(
) else fluid.CPUPlace() ) else fluid.CPUPlace()
model_save_dir = "./inference" model_save_dir = None
model_save_prefix = "./inference/" + model model_save_prefix = None
model_filename = model + INFER_MODEL_SUFFIX model_filename = None
params_filename = model + INFER_PARAMS_SUFFIX params_filename = None
dy_state_dict_save_path = model + ".dygraph" dy_state_dict_save_path = None
def train_mobilenet(args, to_static): def train_mobilenet(args, to_static):
...@@ -571,13 +573,21 @@ def predict_analysis_inference(args, data): ...@@ -571,13 +573,21 @@ def predict_analysis_inference(args, data):
class TestMobileNet(unittest.TestCase): class TestMobileNet(unittest.TestCase):
def setUp(self): def setUp(self):
self.args = Args() self.args = Args()
self.temp_dir = tempfile.TemporaryDirectory()
self.args.model_save_dir = os.path.join(self.temp_dir.name,
"./inference")
def tearDown(self):
self.temp_dir.cleanup()
def train(self, model_name, to_static): def train(self, model_name, to_static):
self.args.model = model_name self.args.model = model_name
self.args.model_save_prefix = "./inference/" + model_name self.args.model_save_prefix = os.path.join(self.temp_dir.name,
"./inference/" + model_name)
self.args.model_filename = model_name + INFER_MODEL_SUFFIX self.args.model_filename = model_name + INFER_MODEL_SUFFIX
self.args.params_filename = model_name + INFER_PARAMS_SUFFIX self.args.params_filename = model_name + INFER_PARAMS_SUFFIX
self.args.dy_state_dict_save_path = model_name + ".dygraph" self.args.dy_state_dict_save_path = os.path.join(
self.temp_dir.name, model_name + ".dygraph")
out = train_mobilenet(self.args, to_static) out = train_mobilenet(self.args, to_static)
return out return out
...@@ -590,10 +600,12 @@ class TestMobileNet(unittest.TestCase): ...@@ -590,10 +600,12 @@ class TestMobileNet(unittest.TestCase):
def assert_same_predict(self, model_name): def assert_same_predict(self, model_name):
self.args.model = model_name self.args.model = model_name
self.args.model_save_prefix = "./inference/" + model_name self.args.model_save_prefix = os.path.join(self.temp_dir.name,
"./inference/" + model_name)
self.args.model_filename = model_name + INFER_MODEL_SUFFIX self.args.model_filename = model_name + INFER_MODEL_SUFFIX
self.args.params_filename = model_name + INFER_PARAMS_SUFFIX self.args.params_filename = model_name + INFER_PARAMS_SUFFIX
self.args.dy_state_dict_save_path = model_name + ".dygraph" self.args.dy_state_dict_save_path = os.path.join(
self.temp_dir.name, model_name + ".dygraph")
local_random = np.random.RandomState(SEED) local_random = np.random.RandomState(SEED)
image = local_random.random_sample([1, 3, 224, 224]).astype('float32') image = local_random.random_sample([1, 3, 224, 224]).astype('float32')
dy_pre = predict_dygraph(self.args, image) dy_pre = predict_dygraph(self.args, image)
......
...@@ -19,6 +19,7 @@ os.environ["FLAGS_enable_eager_mode"] = "0" ...@@ -19,6 +19,7 @@ os.environ["FLAGS_enable_eager_mode"] = "0"
import math import math
import time import time
import unittest import unittest
import tempfile
import numpy as np import numpy as np
...@@ -37,11 +38,6 @@ epoch_num = 1 ...@@ -37,11 +38,6 @@ epoch_num = 1
place = paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda() \ place = paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda() \
else paddle.CPUPlace() else paddle.CPUPlace()
MODEL_SAVE_DIR = "./inference"
MODEL_SAVE_PREFIX = "./inference/resnet_v2"
MODEL_FILENAME = "resnet_v2" + paddle.fluid.dygraph.io.INFER_MODEL_SUFFIX
PARAMS_FILENAME = "resnet_v2" + paddle.fluid.dygraph.io.INFER_PARAMS_SUFFIX
DY_STATE_DICT_SAVE_PATH = "./resnet_v2.dygraph"
program_translator = paddle.jit.ProgramTranslator() program_translator = paddle.jit.ProgramTranslator()
if paddle.is_compiled_with_cuda(): if paddle.is_compiled_with_cuda():
...@@ -210,7 +206,22 @@ def reader_decorator(reader): ...@@ -210,7 +206,22 @@ def reader_decorator(reader):
return __reader__ return __reader__
def train(to_static): class TestResnet(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.model_save_dir = os.path.join(self.temp_dir.name, "./inference")
self.model_save_prefix = os.path.join(self.temp_dir.name,
"./inference/resnet_v2")
self.model_filename = "resnet_v2" + paddle.fluid.dygraph.io.INFER_MODEL_SUFFIX
self.params_filename = "resnet_v2" + paddle.fluid.dygraph.io.INFER_PARAMS_SUFFIX
self.dy_state_dict_save_path = os.path.join(self.temp_dir.name,
"./resnet_v2.dygraph")
def tearDown(self):
self.temp_dir.cleanup()
def do_train(self, to_static):
""" """
Tests model decorated by `dygraph_to_static_output` in static mode. For users, the model is defined in dygraph mode and trained in static mode. Tests model decorated by `dygraph_to_static_output` in static mode. For users, the model is defined in dygraph mode and trained in static mode.
""" """
...@@ -223,7 +234,8 @@ def train(to_static): ...@@ -223,7 +234,8 @@ def train(to_static):
reader_decorator(paddle.dataset.flowers.train(use_xmap=False)), reader_decorator(paddle.dataset.flowers.train(use_xmap=False)),
batch_size=batch_size, batch_size=batch_size,
drop_last=True) drop_last=True)
data_loader = paddle.io.DataLoader.from_generator(capacity=5, iterable=True) data_loader = paddle.io.DataLoader.from_generator(
capacity=5, iterable=True)
data_loader.set_sample_list_generator(train_reader) data_loader.set_sample_list_generator(train_reader)
resnet = ResNet() resnet = ResNet()
...@@ -240,7 +252,8 @@ def train(to_static): ...@@ -240,7 +252,8 @@ def train(to_static):
img, label = data img, label = data
pred = resnet(img) pred = resnet(img)
loss = paddle.nn.functional.cross_entropy(input=pred, label=label) loss = paddle.nn.functional.cross_entropy(
input=pred, label=label)
avg_loss = paddle.mean(x=loss) avg_loss = paddle.mean(x=loss)
acc_top1 = paddle.metric.accuracy(input=pred, label=label, k=1) acc_top1 = paddle.metric.accuracy(input=pred, label=label, k=1)
acc_top5 = paddle.metric.accuracy(input=pred, label=label, k=5) acc_top5 = paddle.metric.accuracy(input=pred, label=label, k=5)
...@@ -261,10 +274,10 @@ def train(to_static): ...@@ -261,10 +274,10 @@ def train(to_static):
total_acc1.numpy() / total_sample, total_acc5.numpy() / total_sample, end_time-start_time)) total_acc1.numpy() / total_sample, total_acc5.numpy() / total_sample, end_time-start_time))
if batch_id == 10: if batch_id == 10:
if to_static: if to_static:
paddle.jit.save(resnet, MODEL_SAVE_PREFIX) paddle.jit.save(resnet, self.model_save_prefix)
else: else:
paddle.fluid.dygraph.save_dygraph(resnet.state_dict(), paddle.fluid.dygraph.save_dygraph(
DY_STATE_DICT_SAVE_PATH) resnet.state_dict(), self.dy_state_dict_save_path)
# avoid dataloader throw abort signaal # avoid dataloader throw abort signaal
data_loader._reset() data_loader._reset()
break break
...@@ -272,13 +285,13 @@ def train(to_static): ...@@ -272,13 +285,13 @@ def train(to_static):
return total_loss.numpy() return total_loss.numpy()
def predict_dygraph(self, data):
def predict_dygraph(data):
program_translator.enable(False) program_translator.enable(False)
paddle.disable_static(place) paddle.disable_static(place)
resnet = ResNet() resnet = ResNet()
model_dict, _ = paddle.fluid.dygraph.load_dygraph(DY_STATE_DICT_SAVE_PATH) model_dict, _ = paddle.fluid.dygraph.load_dygraph(
self.dy_state_dict_save_path)
resnet.set_dict(model_dict) resnet.set_dict(model_dict)
resnet.eval() resnet.eval()
...@@ -290,15 +303,14 @@ def predict_dygraph(data): ...@@ -290,15 +303,14 @@ def predict_dygraph(data):
paddle.enable_static() paddle.enable_static()
return ret return ret
def predict_static(self, data):
def predict_static(data):
exe = paddle.static.Executor(place) exe = paddle.static.Executor(place)
[inference_program, feed_target_names, [inference_program, feed_target_names,
fetch_targets] = paddle.static.load_inference_model( fetch_targets] = paddle.static.load_inference_model(
MODEL_SAVE_DIR, self.model_save_dir,
executor=exe, executor=exe,
model_filename=MODEL_FILENAME, model_filename=self.model_filename,
params_filename=PARAMS_FILENAME) params_filename=self.params_filename)
pred_res = exe.run(inference_program, pred_res = exe.run(inference_program,
feed={feed_target_names[0]: data}, feed={feed_target_names[0]: data},
...@@ -306,10 +318,9 @@ def predict_static(data): ...@@ -306,10 +318,9 @@ def predict_static(data):
return pred_res[0] return pred_res[0]
def predict_dygraph_jit(self, data):
def predict_dygraph_jit(data):
paddle.disable_static(place) paddle.disable_static(place)
resnet = paddle.jit.load(MODEL_SAVE_PREFIX) resnet = paddle.jit.load(self.model_save_prefix)
resnet.eval() resnet.eval()
pred_res = resnet(data) pred_res = resnet(data)
...@@ -318,25 +329,22 @@ def predict_dygraph_jit(data): ...@@ -318,25 +329,22 @@ def predict_dygraph_jit(data):
paddle.enable_static() paddle.enable_static()
return ret return ret
def predict_analysis_inference(self, data):
def predict_analysis_inference(data): output = PredictorTools(self.model_save_dir, self.model_filename,
output = PredictorTools(MODEL_SAVE_DIR, MODEL_FILENAME, PARAMS_FILENAME, self.params_filename, [data])
[data])
out = output() out = output()
return out return out
class TestResnet(unittest.TestCase):
def train(self, to_static): def train(self, to_static):
program_translator.enable(to_static) program_translator.enable(to_static)
return train(to_static) return self.do_train(to_static)
def verify_predict(self): def verify_predict(self):
image = np.random.random([1, 3, 224, 224]).astype('float32') image = np.random.random([1, 3, 224, 224]).astype('float32')
dy_pre = predict_dygraph(image) dy_pre = self.predict_dygraph(image)
st_pre = predict_static(image) st_pre = self.predict_static(image)
dy_jit_pre = predict_dygraph_jit(image) dy_jit_pre = self.predict_dygraph_jit(image)
predictor_pre = predict_analysis_inference(image) predictor_pre = self.predict_analysis_inference(image)
self.assertTrue( self.assertTrue(
np.allclose(dy_pre, st_pre), np.allclose(dy_pre, st_pre),
msg="dy_pre:\n {}\n, st_pre: \n{}.".format(dy_pre, st_pre)) msg="dy_pre:\n {}\n, st_pre: \n{}.".format(dy_pre, st_pre))
...@@ -361,7 +369,7 @@ class TestResnet(unittest.TestCase): ...@@ -361,7 +369,7 @@ class TestResnet(unittest.TestCase):
paddle.fluid.set_flags({'FLAGS_use_mkldnn': True}) paddle.fluid.set_flags({'FLAGS_use_mkldnn': True})
try: try:
if paddle.fluid.core.is_compiled_with_mkldnn(): if paddle.fluid.core.is_compiled_with_mkldnn():
train(to_static=True) self.train(to_static=True)
finally: finally:
paddle.fluid.set_flags({'FLAGS_use_mkldnn': False}) paddle.fluid.set_flags({'FLAGS_use_mkldnn': False})
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
from __future__ import print_function from __future__ import print_function
import os import os
import tempfile
import unittest import unittest
import numpy as np import numpy as np
...@@ -48,6 +49,12 @@ class SimpleFcLayer(fluid.dygraph.Layer): ...@@ -48,6 +49,12 @@ class SimpleFcLayer(fluid.dygraph.Layer):
class TestDyToStaticSaveInferenceModel(unittest.TestCase): class TestDyToStaticSaveInferenceModel(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
def test_save_inference_model(self): def test_save_inference_model(self):
fc_size = 20 fc_size = 20
x_data = np.random.random((fc_size, fc_size)).astype('float32') x_data = np.random.random((fc_size, fc_size)).astype('float32')
...@@ -66,8 +73,10 @@ class TestDyToStaticSaveInferenceModel(unittest.TestCase): ...@@ -66,8 +73,10 @@ class TestDyToStaticSaveInferenceModel(unittest.TestCase):
adam.minimize(loss) adam.minimize(loss)
layer.clear_gradients() layer.clear_gradients()
# test for saving model in dygraph.guard # test for saving model in dygraph.guard
infer_model_prefix = "./test_dy2stat_inference_in_guard/model" infer_model_prefix = os.path.join(
infer_model_dir = "./test_dy2stat_inference_in_guard" self.temp_dir.name, "test_dy2stat_inference_in_guard/model")
infer_model_dir = os.path.join(self.temp_dir.name,
"test_dy2stat_inference_in_guard")
fluid.dygraph.jit.save( fluid.dygraph.jit.save(
layer=layer, layer=layer,
path=infer_model_prefix, path=infer_model_prefix,
...@@ -90,8 +99,10 @@ class TestDyToStaticSaveInferenceModel(unittest.TestCase): ...@@ -90,8 +99,10 @@ class TestDyToStaticSaveInferenceModel(unittest.TestCase):
expected_persistable_vars = set([p.name for p in model.parameters()]) expected_persistable_vars = set([p.name for p in model.parameters()])
infer_model_prefix = "./test_dy2stat_inference/model" infer_model_prefix = os.path.join(self.temp_dir.name,
infer_model_dir = "./test_dy2stat_inference" "test_dy2stat_inference/model")
infer_model_dir = os.path.join(self.temp_dir.name,
"test_dy2stat_inference")
model_filename = "model" + INFER_MODEL_SUFFIX model_filename = "model" + INFER_MODEL_SUFFIX
params_filename = "model" + INFER_PARAMS_SUFFIX params_filename = "model" + INFER_PARAMS_SUFFIX
fluid.dygraph.jit.save( fluid.dygraph.jit.save(
......
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
from __future__ import print_function from __future__ import print_function
import unittest import unittest
import os
import tempfile
import numpy as np import numpy as np
import paddle.fluid as fluid import paddle.fluid as fluid
...@@ -30,6 +32,14 @@ place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace( ...@@ -30,6 +32,14 @@ place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace(
class TestDyToStaticSaveLoad(unittest.TestCase): class TestDyToStaticSaveLoad(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.model_path = os.path.join(self.temp_dir.name,
"test_dy2stat_save_load")
def tearDown(self):
self.temp_dir.cleanup()
def test_save_load_same_result(self): def test_save_load_same_result(self):
program_translator = ProgramTranslator() program_translator = ProgramTranslator()
x_data = np.random.randn(30, 10, 32).astype('float32') x_data = np.random.randn(30, 10, 32).astype('float32')
...@@ -50,7 +60,8 @@ class TestDyToStaticSaveLoad(unittest.TestCase): ...@@ -50,7 +60,8 @@ class TestDyToStaticSaveLoad(unittest.TestCase):
adam.minimize(static_loss) adam.minimize(static_loss)
net.clear_gradients() net.clear_gradients()
# Save parameters # Save parameters
fluid.save_dygraph(net.state_dict(), "./test_dy2stat_save_load")
fluid.save_dygraph(net.state_dict(), self.model_path)
# minimize() will update parameter, call net() to get output and avg_loss. # minimize() will update parameter, call net() to get output and avg_loss.
# Switch into eval mode. # Switch into eval mode.
net.eval() net.eval()
...@@ -61,7 +72,7 @@ class TestDyToStaticSaveLoad(unittest.TestCase): ...@@ -61,7 +72,7 @@ class TestDyToStaticSaveLoad(unittest.TestCase):
dygraph_net = Linear(32, 64) dygraph_net = Linear(32, 64)
# Load parameters # Load parameters
model_dict, _ = fluid.load_dygraph("./test_dy2stat_save_load") model_dict, _ = fluid.load_dygraph(self.model_path)
dygraph_net.set_dict(model_dict) dygraph_net.set_dict(model_dict)
# Switch into eval mode. # Switch into eval mode.
dygraph_net.eval() dygraph_net.eval()
......
...@@ -16,6 +16,8 @@ import logging ...@@ -16,6 +16,8 @@ import logging
import math import math
import time import time
import unittest import unittest
import os
import tempfile
import numpy as np import numpy as np
import paddle import paddle
...@@ -35,11 +37,6 @@ BATCH_SIZE = 8 ...@@ -35,11 +37,6 @@ BATCH_SIZE = 8
EPOCH_NUM = 1 EPOCH_NUM = 1
PRINT_STEP = 2 PRINT_STEP = 2
STEP_NUM = 10 STEP_NUM = 10
MODEL_SAVE_DIR = "./inference"
MODEL_SAVE_PREFIX = "./inference/se_resnet"
MODEL_FILENAME = "se_resnet" + INFER_MODEL_SUFFIX
PARAMS_FILENAME = "se_resnet" + INFER_PARAMS_SUFFIX
DY_STATE_DICT_SAVE_PATH = "./se_resnet.dygraph"
place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() \ place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() \
else fluid.CPUPlace() else fluid.CPUPlace()
...@@ -327,7 +324,27 @@ class SeResNeXt(fluid.dygraph.Layer): ...@@ -327,7 +324,27 @@ class SeResNeXt(fluid.dygraph.Layer):
return out, avg_loss, acc_top1, acc_top5 return out, avg_loss, acc_top1, acc_top5
def train(train_reader, to_static): class TestSeResnet(unittest.TestCase):
def setUp(self):
self.train_reader = paddle.batch(
paddle.dataset.flowers.train(
use_xmap=False, cycle=True),
batch_size=BATCH_SIZE,
drop_last=True)
self.temp_dir = tempfile.TemporaryDirectory()
self.model_save_dir = os.path.join(self.temp_dir.name, "inference")
self.model_save_prefix = os.path.join(self.temp_dir.name,
"inference/se_resnet")
self.model_filename = "se_resnet" + INFER_MODEL_SUFFIX
self.params_filename = "se_resnet" + INFER_PARAMS_SUFFIX
self.dy_state_dict_save_path = os.path.join(self.temp_dir.name,
"se_resnet.dygraph")
def tearDown(self):
self.temp_dir.cleanup()
def train(self, train_reader, to_static):
program_translator = ProgramTranslator() program_translator = ProgramTranslator()
program_translator.enable(to_static) program_translator.enable(to_static)
...@@ -337,7 +354,8 @@ def train(train_reader, to_static): ...@@ -337,7 +354,8 @@ def train(train_reader, to_static):
paddle.seed(SEED) paddle.seed(SEED)
paddle.framework.random._manual_program_seed(SEED) paddle.framework.random._manual_program_seed(SEED)
se_resnext = SeResNeXt() se_resnext = SeResNeXt()
optimizer = optimizer_setting(train_parameters, se_resnext.parameters()) optimizer = optimizer_setting(train_parameters,
se_resnext.parameters())
for epoch_id in range(EPOCH_NUM): for epoch_id in range(EPOCH_NUM):
total_loss = 0.0 total_loss = 0.0
...@@ -347,9 +365,11 @@ def train(train_reader, to_static): ...@@ -347,9 +365,11 @@ def train(train_reader, to_static):
step_idx = 0 step_idx = 0
speed_list = [] speed_list = []
for step_id, data in enumerate(train_reader()): for step_id, data in enumerate(train_reader()):
dy_x_data = np.array([x[0].reshape(3, 224, 224) dy_x_data = np.array(
[x[0].reshape(3, 224, 224)
for x in data]).astype('float32') for x in data]).astype('float32')
y_data = np.array([x[1] for x in data]).astype('int64').reshape( y_data = np.array(
[x[1] for x in data]).astype('int64').reshape(
BATCH_SIZE, 1) BATCH_SIZE, 1)
img = to_variable(dy_x_data) img = to_variable(dy_x_data)
...@@ -388,23 +408,24 @@ def train(train_reader, to_static): ...@@ -388,23 +408,24 @@ def train(train_reader, to_static):
if to_static: if to_static:
fluid.dygraph.jit.save( fluid.dygraph.jit.save(
se_resnext, se_resnext,
MODEL_SAVE_PREFIX, [img], self.model_save_prefix, [img],
output_spec=[pred]) output_spec=[pred])
else: else:
fluid.dygraph.save_dygraph(se_resnext.state_dict(), fluid.dygraph.save_dygraph(
DY_STATE_DICT_SAVE_PATH) se_resnext.state_dict(),
self.dy_state_dict_save_path)
break break
return pred.numpy(), avg_loss.numpy(), acc_top1.numpy(), acc_top5.numpy( return pred.numpy(), avg_loss.numpy(), acc_top1.numpy(
) ), acc_top5.numpy()
def predict_dygraph(data): def predict_dygraph(self, data):
program_translator = ProgramTranslator() program_translator = ProgramTranslator()
program_translator.enable(False) program_translator.enable(False)
with fluid.dygraph.guard(place): with fluid.dygraph.guard(place):
se_resnext = SeResNeXt() se_resnext = SeResNeXt()
model_dict, _ = fluid.dygraph.load_dygraph(DY_STATE_DICT_SAVE_PATH) model_dict, _ = fluid.dygraph.load_dygraph(
self.dy_state_dict_save_path)
se_resnext.set_dict(model_dict) se_resnext.set_dict(model_dict)
se_resnext.eval() se_resnext.eval()
...@@ -415,16 +436,15 @@ def predict_dygraph(data): ...@@ -415,16 +436,15 @@ def predict_dygraph(data):
return pred_res.numpy() return pred_res.numpy()
def predict_static(self, data):
def predict_static(data):
paddle.enable_static() paddle.enable_static()
exe = fluid.Executor(place) exe = fluid.Executor(place)
[inference_program, feed_target_names, [inference_program, feed_target_names,
fetch_targets] = fluid.io.load_inference_model( fetch_targets] = fluid.io.load_inference_model(
MODEL_SAVE_DIR, self.model_save_dir,
executor=exe, executor=exe,
model_filename=MODEL_FILENAME, model_filename=self.model_filename,
params_filename=PARAMS_FILENAME) params_filename=self.params_filename)
pred_res = exe.run(inference_program, pred_res = exe.run(inference_program,
feed={feed_target_names[0]: data}, feed={feed_target_names[0]: data},
...@@ -432,38 +452,27 @@ def predict_static(data): ...@@ -432,38 +452,27 @@ def predict_static(data):
return pred_res[0] return pred_res[0]
def predict_dygraph_jit(self, data):
def predict_dygraph_jit(data):
with fluid.dygraph.guard(place): with fluid.dygraph.guard(place):
se_resnext = fluid.dygraph.jit.load(MODEL_SAVE_PREFIX) se_resnext = fluid.dygraph.jit.load(self.model_save_prefix)
se_resnext.eval() se_resnext.eval()
pred_res = se_resnext(data) pred_res = se_resnext(data)
return pred_res.numpy() return pred_res.numpy()
def predict_analysis_inference(self, data):
def predict_analysis_inference(data): output = PredictorTools(self.model_save_dir, self.model_filename,
output = PredictorTools(MODEL_SAVE_DIR, MODEL_FILENAME, PARAMS_FILENAME, self.params_filename, [data])
[data])
out = output() out = output()
return out return out
class TestSeResnet(unittest.TestCase):
def setUp(self):
self.train_reader = paddle.batch(
paddle.dataset.flowers.train(
use_xmap=False, cycle=True),
batch_size=BATCH_SIZE,
drop_last=True)
def verify_predict(self): def verify_predict(self):
image = np.random.random([1, 3, 224, 224]).astype('float32') image = np.random.random([1, 3, 224, 224]).astype('float32')
dy_pre = predict_dygraph(image) dy_pre = self.predict_dygraph(image)
st_pre = predict_static(image) st_pre = self.predict_static(image)
dy_jit_pre = predict_dygraph_jit(image) dy_jit_pre = self.predict_dygraph_jit(image)
predictor_pre = predict_analysis_inference(image) predictor_pre = self.predict_analysis_inference(image)
self.assertTrue( self.assertTrue(
np.allclose(dy_pre, st_pre), np.allclose(dy_pre, st_pre),
msg="dy_pre:\n {}\n, st_pre: \n{}.".format(dy_pre, st_pre)) msg="dy_pre:\n {}\n, st_pre: \n{}.".format(dy_pre, st_pre))
...@@ -483,9 +492,9 @@ class TestSeResnet(unittest.TestCase): ...@@ -483,9 +492,9 @@ class TestSeResnet(unittest.TestCase):
flat_predictor_pre[i], flat_st_pre[i])) flat_predictor_pre[i], flat_st_pre[i]))
def test_check_result(self): def test_check_result(self):
pred_1, loss_1, acc1_1, acc5_1 = train( pred_1, loss_1, acc1_1, acc5_1 = self.train(
self.train_reader, to_static=False) self.train_reader, to_static=False)
pred_2, loss_2, acc1_2, acc5_2 = train( pred_2, loss_2, acc1_2, acc5_2 = self.train(
self.train_reader, to_static=True) self.train_reader, to_static=True)
self.assertTrue( self.assertTrue(
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
# limitations under the License. # limitations under the License.
import os import os
import tempfile
import time import time
import unittest import unittest
...@@ -22,7 +23,7 @@ from paddle.fluid.clip import GradientClipByGlobalNorm ...@@ -22,7 +23,7 @@ from paddle.fluid.clip import GradientClipByGlobalNorm
from paddle.fluid.dygraph.dygraph_to_static import ProgramTranslator from paddle.fluid.dygraph.dygraph_to_static import ProgramTranslator
from seq2seq_dygraph_model import BaseModel, AttentionModel from seq2seq_dygraph_model import BaseModel, AttentionModel
from seq2seq_utils import Seq2SeqModelHyperParams as args from seq2seq_utils import Seq2SeqModelHyperParams
from seq2seq_utils import get_data_iter from seq2seq_utils import get_data_iter
place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace( place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace(
) )
...@@ -43,7 +44,7 @@ def prepare_input(batch): ...@@ -43,7 +44,7 @@ def prepare_input(batch):
return inputs, np.sum(tar_mask) return inputs, np.sum(tar_mask)
def train(attn_model=False): def train(args, attn_model=False):
with fluid.dygraph.guard(place): with fluid.dygraph.guard(place):
fluid.default_startup_program().random_seed = 2020 fluid.default_startup_program().random_seed = 2020
fluid.default_main_program().random_seed = 2020 fluid.default_main_program().random_seed = 2020
...@@ -117,7 +118,7 @@ def train(attn_model=False): ...@@ -117,7 +118,7 @@ def train(attn_model=False):
return loss.numpy() return loss.numpy()
def infer(attn_model=False): def infer(args, attn_model=False):
with fluid.dygraph.guard(place): with fluid.dygraph.guard(place):
if attn_model: if attn_model:
...@@ -160,19 +161,32 @@ def infer(attn_model=False): ...@@ -160,19 +161,32 @@ def infer(attn_model=False):
class TestSeq2seq(unittest.TestCase): class TestSeq2seq(unittest.TestCase):
def setUp(self):
self.args = Seq2SeqModelHyperParams
self.temp_dir = tempfile.TemporaryDirectory()
self.args.base_model_path = os.path.join(self.temp_dir.name,
self.args.base_model_path)
self.args.attn_model_path = os.path.join(self.temp_dir.name,
self.args.attn_model_path)
self.args.reload_model = os.path.join(self.temp_dir.name,
self.args.reload_model)
def tearDown(self):
self.temp_dir.cleanup()
def run_dygraph(self, mode="train", attn_model=False): def run_dygraph(self, mode="train", attn_model=False):
program_translator.enable(False) program_translator.enable(False)
if mode == "train": if mode == "train":
return train(attn_model) return train(self.args, attn_model)
else: else:
return infer(attn_model) return infer(self.args, attn_model)
def run_static(self, mode="train", attn_model=False): def run_static(self, mode="train", attn_model=False):
program_translator.enable(True) program_translator.enable(True)
if mode == "train": if mode == "train":
return train(attn_model) return train(self.args, attn_model)
else: else:
return infer(attn_model) return infer(self.args, attn_model)
def _test_train(self, attn_model=False): def _test_train(self, attn_model=False):
dygraph_loss = self.run_dygraph(mode="train", attn_model=attn_model) dygraph_loss = self.run_dygraph(mode="train", attn_model=attn_model)
......
...@@ -13,7 +13,8 @@ ...@@ -13,7 +13,8 @@
# limitations under the License. # limitations under the License.
from __future__ import print_function from __future__ import print_function
import os
import tempfile
import unittest import unittest
import numpy as np import numpy as np
...@@ -166,15 +167,20 @@ class TestSetValue(TestSliceWithoutControlFlow): ...@@ -166,15 +167,20 @@ class TestSetValue(TestSliceWithoutControlFlow):
class TestSetValueWithLayerAndSave(unittest.TestCase): class TestSetValueWithLayerAndSave(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.model_path = os.path.join(self.temp_dir.name,
"layer_use_set_value")
def tearDown(self):
self.temp_dir.cleanup()
def test_set_value_with_save(self): def test_set_value_with_save(self):
prog_trans.enable(True) prog_trans.enable(True)
model = LayerWithSetValue(input_dim=10, hidden=1) model = LayerWithSetValue(input_dim=10, hidden=1)
x = paddle.full(shape=[5, 10], fill_value=5.0, dtype="float32") x = paddle.full(shape=[5, 10], fill_value=5.0, dtype="float32")
paddle.jit.save( paddle.jit.save(
layer=model, layer=model, path=self.model_path, input_spec=[x], output_spec=None)
path="./layer_use_set_value",
input_spec=[x],
output_spec=None)
class TestSliceSupplementSpecialCase(unittest.TestCase): class TestSliceSupplementSpecialCase(unittest.TestCase):
......
...@@ -11,7 +11,8 @@ ...@@ -11,7 +11,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import os
import tempfile
import paddle import paddle
import unittest import unittest
import numpy as np import numpy as np
...@@ -72,11 +73,16 @@ class TestTyping(unittest.TestCase): ...@@ -72,11 +73,16 @@ class TestTyping(unittest.TestCase):
self.x = paddle.randn([4, 16]) self.x = paddle.randn([4, 16])
self.spec = [paddle.static.InputSpec(shape=[None, 16], dtype='float32')] self.spec = [paddle.static.InputSpec(shape=[None, 16], dtype='float32')]
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
def build_net(self): def build_net(self):
return LinearNetWithTuple(self.in_num, self.out_num) return LinearNetWithTuple(self.in_num, self.out_num)
def save_and_load(self, suffix=''): def save_and_load(self, suffix=''):
path = './layer_typing_' + suffix path = os.path.join(self.temp_dir.name, 'layer_typing_' + suffix)
paddle.jit.save(self.net, path, input_spec=self.spec) paddle.jit.save(self.net, path, input_spec=self.spec)
return paddle.jit.load(path) return paddle.jit.load(path)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册