未验证 提交 c143326d 编写于 作者: C Chen Weihang 提交者: GitHub

try to fix test_paddle_save_load unknown timeout (#27536)

* try to fix paddle save load test

* open paddle save load

* replace dataloader

* remove dataloader
上级 6fc74bba
...@@ -335,9 +335,6 @@ list(REMOVE_ITEM TEST_OPS test_conv3d_transpose_op) ...@@ -335,9 +335,6 @@ list(REMOVE_ITEM TEST_OPS test_conv3d_transpose_op)
# disable this unittest temporarily # disable this unittest temporarily
list(REMOVE_ITEM TEST_OPS test_imperative_data_loader_exception) list(REMOVE_ITEM TEST_OPS test_imperative_data_loader_exception)
list(REMOVE_ITEM TEST_OPS test_sampling_id_op) list(REMOVE_ITEM TEST_OPS test_sampling_id_op)
list(REMOVE_ITEM TEST_OPS test_paddle_save_load)
if (APPLE OR WIN32) if (APPLE OR WIN32)
list(REMOVE_ITEM TEST_OPS test_dataset) list(REMOVE_ITEM TEST_OPS test_dataset)
......
...@@ -29,19 +29,23 @@ IMAGE_SIZE = 784 ...@@ -29,19 +29,23 @@ IMAGE_SIZE = 784
CLASS_NUM = 10 CLASS_NUM = 10
# define a random dataset def random_batch_reader():
class RandomDataset(paddle.io.Dataset): def _get_random_inputs_and_labels():
def __init__(self, num_samples):
self.num_samples = num_samples
def __getitem__(self, idx):
np.random.seed(SEED) np.random.seed(SEED)
image = np.random.random([IMAGE_SIZE]).astype('float32') image = np.random.random([BATCH_SIZE, IMAGE_SIZE]).astype('float32')
label = np.random.randint(0, CLASS_NUM - 1, (1, )).astype('int64') label = np.random.randint(0, CLASS_NUM - 1, (
BATCH_SIZE,
1, )).astype('int64')
return image, label return image, label
def __len__(self): def __reader__():
return self.num_samples for _ in range(BATCH_NUM):
batch_image, batch_label = _get_random_inputs_and_labels()
batch_image = paddle.to_tensor(batch_image)
batch_label = paddle.to_tensor(batch_label)
yield batch_image, batch_label
return __reader__
class LinearNet(nn.Layer): class LinearNet(nn.Layer):
...@@ -66,8 +70,7 @@ def train(layer, loader, loss_fn, opt): ...@@ -66,8 +70,7 @@ def train(layer, loader, loss_fn, opt):
class TestSaveLoad(unittest.TestCase): class TestSaveLoad(unittest.TestCase):
def setUp(self): def setUp(self):
# enable dygraph mode # enable dygraph mode
self.place = paddle.CPUPlace() paddle.disable_static()
paddle.disable_static(self.place)
# config seed # config seed
paddle.manual_seed(SEED) paddle.manual_seed(SEED)
...@@ -81,14 +84,8 @@ class TestSaveLoad(unittest.TestCase): ...@@ -81,14 +84,8 @@ class TestSaveLoad(unittest.TestCase):
adam = opt.Adam(learning_rate=0.001, parameters=layer.parameters()) adam = opt.Adam(learning_rate=0.001, parameters=layer.parameters())
# create data loader # create data loader
dataset = RandomDataset(BATCH_NUM * BATCH_SIZE) # TODO: using new DataLoader cause unknown Timeout on windows, replace it
loader = paddle.io.DataLoader( loader = random_batch_reader()
dataset,
places=self.place,
batch_size=BATCH_SIZE,
shuffle=True,
drop_last=True,
num_workers=2)
# train # train
train(layer, loader, loss_fn, adam) train(layer, loader, loss_fn, adam)
...@@ -103,8 +100,8 @@ class TestSaveLoad(unittest.TestCase): ...@@ -103,8 +100,8 @@ class TestSaveLoad(unittest.TestCase):
layer, opt = self.build_and_train_model() layer, opt = self.build_and_train_model()
# save # save
layer_save_path = "linear.pdparams" layer_save_path = "test_paddle_save_load.linear.pdparams"
opt_save_path = "linear.pdopt" opt_save_path = "test_paddle_save_load.linear.pdopt"
layer_state_dict = layer.state_dict() layer_state_dict = layer.state_dict()
opt_state_dict = opt.state_dict() opt_state_dict = opt.state_dict()
...@@ -120,7 +117,7 @@ class TestSaveLoad(unittest.TestCase): ...@@ -120,7 +117,7 @@ class TestSaveLoad(unittest.TestCase):
# test save load in static mode # test save load in static mode
paddle.enable_static() paddle.enable_static()
static_save_path = "static_mode_test/linear.pdparams" static_save_path = "static_mode_test/test_paddle_save_load.linear.pdparams"
paddle.save(layer_state_dict, static_save_path) paddle.save(layer_state_dict, static_save_path)
load_static_state_dict = paddle.load(static_save_path) load_static_state_dict = paddle.load(static_save_path)
self.check_load_state_dict(layer_state_dict, load_static_state_dict) self.check_load_state_dict(layer_state_dict, load_static_state_dict)
...@@ -133,15 +130,15 @@ class TestSaveLoad(unittest.TestCase): ...@@ -133,15 +130,15 @@ class TestSaveLoad(unittest.TestCase):
# 2. test save path format error # 2. test save path format error
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
paddle.save(layer_state_dict, "linear.model/") paddle.save(layer_state_dict, "test_paddle_save_load.linear.model/")
# 3. test load path not exist error # 3. test load path not exist error
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
paddle.load("linear.params") paddle.load("test_paddle_save_load.linear.params")
# 4. test load old save path error # 4. test load old save path error
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
paddle.load("linear") paddle.load("test_paddle_save_load.linear")
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册