Skip to content

Commit

Permalink
[Cleanup][C-14]Replace Program.random_seed (#61526)
Browse files Browse the repository at this point in the history
  • Loading branch information
co63oc authored Feb 3, 2024
1 parent 1291468 commit c7a3ee4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 32 deletions.
30 changes: 10 additions & 20 deletions test/legacy_test/test_multiprocess_dataloader_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ def __iter__(self):

class TestTensorDataset(unittest.TestCase):
def run_main(self, num_workers, places):
paddle.static.default_startup_program().random_seed = 1
paddle.static.default_main_program().random_seed = 1
paddle.seed(1)
place = paddle.CPUPlace()
with base.dygraph.guard(place):
input_np = np.random.random([16, 3, 4]).astype('float32')
Expand Down Expand Up @@ -98,8 +97,7 @@ def test_main(self):

class TestComposeDataset(unittest.TestCase):
def test_main(self):
paddle.static.default_startup_program().random_seed = 1
paddle.static.default_main_program().random_seed = 1
paddle.seed(1)

dataset1 = RandomDataset(10)
dataset2 = RandomDataset(10)
Expand All @@ -118,8 +116,7 @@ def test_main(self):

class TestRandomSplitApi(unittest.TestCase):
def test_main(self):
paddle.static.default_startup_program().random_seed = 1
paddle.static.default_main_program().random_seed = 1
paddle.seed(1)

dataset1, dataset2 = paddle.io.random_split(range(5), [1, 4])

Expand All @@ -139,8 +136,7 @@ def test_main(self):

class TestRandomSplitError(unittest.TestCase):
def test_errors(self):
paddle.static.default_startup_program().random_seed = 1
paddle.static.default_main_program().random_seed = 1
paddle.seed(1)

self.assertRaises(ValueError, paddle.io.random_split, range(5), [3, 8])
self.assertRaises(ValueError, paddle.io.random_split, range(5), [8])
Expand All @@ -149,8 +145,7 @@ def test_errors(self):

class TestSubsetDataset(unittest.TestCase):
def run_main(self, num_workers, places):
paddle.static.default_startup_program().random_seed = 1
paddle.static.default_main_program().random_seed = 1
paddle.seed(1)

input_np = np.random.random([5, 3, 4]).astype('float32')
input = paddle.to_tensor(input_np)
Expand Down Expand Up @@ -201,8 +196,7 @@ def assert_basic(input, label):
self.assertEqual(odd_list, elements_list)

def test_main(self):
paddle.static.default_startup_program().random_seed = 1
paddle.static.default_main_program().random_seed = 1
paddle.seed(1)

places = [paddle.CPUPlace()]
if paddle.is_compiled_with_cuda():
Expand All @@ -213,8 +207,7 @@ def test_main(self):

class TestChainDataset(unittest.TestCase):
def run_main(self, num_workers, places):
paddle.static.default_startup_program().random_seed = 1
paddle.static.default_main_program().random_seed = 1
paddle.seed(1)

dataset1 = RandomIterableDataset(10)
dataset2 = RandomIterableDataset(10)
Expand Down Expand Up @@ -259,8 +252,7 @@ def __getitem__(self, idx):

class TestNumpyMixTensorDataset(TestTensorDataset):
def run_main(self, num_workers, places):
paddle.static.default_startup_program().random_seed = 1
paddle.static.default_main_program().random_seed = 1
paddle.seed(1)
place = paddle.CPUPlace()
with base.dygraph.guard(place):
dataset = NumpyMixTensorDataset(16)
Expand Down Expand Up @@ -304,8 +296,7 @@ def __getitem__(self, idx):

class TestComplextDataset(unittest.TestCase):
def run_main(self, num_workers):
paddle.static.default_startup_program().random_seed = 1
paddle.static.default_main_program().random_seed = 1
paddle.seed(1)
place = paddle.CPUPlace()
with base.dygraph.guard(place):
dataset = ComplextDataset(16)
Expand Down Expand Up @@ -360,8 +351,7 @@ def init_dataset(self):
self.dataset = SingleFieldDataset(self.sample_num)

def run_main(self, num_workers):
paddle.static.default_startup_program().random_seed = 1
paddle.static.default_main_program().random_seed = 1
paddle.seed(1)
place = paddle.CPUPlace()
with base.dygraph.guard(place):
self.init_dataset()
Expand Down
6 changes: 2 additions & 4 deletions test/legacy_test/test_multiprocess_dataloader_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ def run_main(
collate_fn,
use_shared_memory,
):
base.default_startup_program().random_seed = 1
base.default_main_program().random_seed = 1
paddle.seed(1)
with base.dygraph.guard(places[0]):
fc_net = SimpleFCNet()
optimizer = paddle.optimizer.Adam(parameters=fc_net.parameters())
Expand Down Expand Up @@ -176,8 +175,7 @@ def run_main(
collate_fn,
use_shared_memory,
):
base.default_startup_program().random_seed = 1
base.default_main_program().random_seed = 1
paddle.seed(1)
with base.dygraph.guard(places[0]):
fc_net = SimpleFCNet()
optimizer = paddle.optimizer.Adam(parameters=fc_net.parameters())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ def forward(self, image):

class TestDygraphDataLoader(unittest.TestCase):
def run_main(self, num_workers, places, persistent_workers):
base.default_startup_program().random_seed = 1
base.default_main_program().random_seed = 1
paddle.seed(1)
with base.dygraph.guard(places[0]):
fc_net = SimpleFCNet()
optimizer = paddle.optimizer.Adam(parameters=fc_net.parameters())
Expand Down Expand Up @@ -146,8 +145,7 @@ def test_main(self):

class TestDygraphDataLoaderWithBatchedDataset(TestDygraphDataLoader):
def run_main(self, num_workers, places, persistent_workers):
base.default_startup_program().random_seed = 1
base.default_main_program().random_seed = 1
paddle.seed(1)
with base.dygraph.guard(places[0]):
fc_net = SimpleFCNet()
optimizer = paddle.optimizer.Adam(parameters=fc_net.parameters())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def __iter__(self):
def simple_fc_net_static():
startup_prog = base.Program()
main_prog = base.Program()
startup_prog.random_seed = 1
main_prog.random_seed = 1
paddle.seed(1)

with base.unique_name.guard():
with base.program_guard(main_prog, startup_prog):
Expand Down
3 changes: 1 addition & 2 deletions test/legacy_test/test_multiprocess_dataloader_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def __len__(self):
def simple_fc_net_static():
startup_prog = base.Program()
main_prog = base.Program()
startup_prog.random_seed = 1
main_prog.random_seed = 1
paddle.seed(1)

with base.unique_name.guard():
with base.program_guard(main_prog, startup_prog):
Expand Down

0 comments on commit c7a3ee4

Please sign in to comment.