Skip to content

Commit

Permalink
try to fix test_paddle_save_load unknown timeout (PaddlePaddle#27536)
Browse files Browse the repository at this point in the history
* try to fix paddle save load test

* open paddle save load

* replace dataloader

* remove dataloader
  • Loading branch information
chenwhql authored Sep 25, 2020
1 parent 6fc74bb commit c143326
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
3 changes: 0 additions & 3 deletions python/paddle/fluid/tests/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,6 @@ list(REMOVE_ITEM TEST_OPS test_conv3d_transpose_op)
# disable this unittest temporarily
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_paddle_save_load)



if (APPLE OR WIN32)
list(REMOVE_ITEM TEST_OPS test_dataset)
Expand Down
49 changes: 23 additions & 26 deletions python/paddle/fluid/tests/unittests/test_paddle_save_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,23 @@
CLASS_NUM = 10


# define a random dataset
class RandomDataset(paddle.io.Dataset):
def __init__(self, num_samples):
self.num_samples = num_samples

def __getitem__(self, idx):
def random_batch_reader():
def _get_random_inputs_and_labels():
np.random.seed(SEED)
image = np.random.random([IMAGE_SIZE]).astype('float32')
label = np.random.randint(0, CLASS_NUM - 1, (1, )).astype('int64')
image = np.random.random([BATCH_SIZE, IMAGE_SIZE]).astype('float32')
label = np.random.randint(0, CLASS_NUM - 1, (
BATCH_SIZE,
1, )).astype('int64')
return image, label

def __len__(self):
return self.num_samples
def __reader__():
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):
Expand All @@ -66,8 +70,7 @@ def train(layer, loader, loss_fn, opt):
class TestSaveLoad(unittest.TestCase):
def setUp(self):
# enable dygraph mode
self.place = paddle.CPUPlace()
paddle.disable_static(self.place)
paddle.disable_static()

# config seed
paddle.manual_seed(SEED)
Expand All @@ -81,14 +84,8 @@ def build_and_train_model(self):
adam = opt.Adam(learning_rate=0.001, parameters=layer.parameters())

# create data loader
dataset = RandomDataset(BATCH_NUM * BATCH_SIZE)
loader = paddle.io.DataLoader(
dataset,
places=self.place,
batch_size=BATCH_SIZE,
shuffle=True,
drop_last=True,
num_workers=2)
# TODO: using new DataLoader cause unknown Timeout on windows, replace it
loader = random_batch_reader()

# train
train(layer, loader, loss_fn, adam)
Expand All @@ -103,8 +100,8 @@ def test_save_load(self):
layer, opt = self.build_and_train_model()

# save
layer_save_path = "linear.pdparams"
opt_save_path = "linear.pdopt"
layer_save_path = "test_paddle_save_load.linear.pdparams"
opt_save_path = "test_paddle_save_load.linear.pdopt"
layer_state_dict = layer.state_dict()
opt_state_dict = opt.state_dict()

Expand All @@ -120,7 +117,7 @@ def test_save_load(self):

# test save load in static mode
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)
load_static_state_dict = paddle.load(static_save_path)
self.check_load_state_dict(layer_state_dict, load_static_state_dict)
Expand All @@ -133,15 +130,15 @@ def test_save_load(self):

# 2. test save path format error
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
with self.assertRaises(ValueError):
paddle.load("linear.params")
paddle.load("test_paddle_save_load.linear.params")

# 4. test load old save path error
with self.assertRaises(ValueError):
paddle.load("linear")
paddle.load("test_paddle_save_load.linear")


if __name__ == '__main__':
Expand Down

0 comments on commit c143326

Please sign in to comment.