Option to provide seed to random generators to ensure reproducibility (#1572)

* Option to provide seed to random generators to ensure reproducibility

I added small function in utilities which imports torch, numpy, python
random and sets seed for all of the libraries to ensure reproducibility
of results.

* Apply recommendations from core contributors on seeding

1. Moved the seeding code to another file
2. Make deterministic as a parameter for trainer class
3. Add assertions for seeding numpy
4. Added warnings
5. torch.manual_seed should be enough for seeding torch

* Revert "Apply recommendations from core contributors on seeding"

This reverts commit a213c8e6882eec8a9e7408b9418926d2db7c5461.

* Revert "Revert "Apply recommendations from core contributors on seeding""

This reverts commit 59b2da53c62878de7aab0aa3feb3115e105eea06.

* Change in test, for correct seeding

* Allow seed equal to 0

* Allow seed to be uint32.max

* Added deterministic to benchmarks

* Cuda manual seed as in benchmark seeding

* Seeding should be done before model initialization

* cuda manual_seed is not necessary

* Fixing seed test_cpu_lbfgs

On some seeds seems like lbfgs doesn't converge.
So I fixed the seed during testing.

* rebasing issue with old reproducibility.py

* Improved documentation and ability to seed before initializing Train
class

* Change in docs

* Removed seed from trainer, update for documentation

* Typo in the docs

* Added seed_everything to _all_

* Fixing old changes

* Model initialization should be earlier then Trainer

* Update pytorch_lightning/trainer/__init__.py

From Example to testcode

Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>

* Fixing according to the contributors suggestions

* Moving horovod deterministic to Trainer class

* deterministic flag affects horovod docs update

* Improved static typing

* Added deterministic to test runners of horovod

It is failing on some versions, not very predictable

* static seeds for horovod tests

* Change for reset_seed function in tests

* Seeding horovod using reset_seed from tutils

* Update pytorch_lightning/trainer/__init__.py

* chlog

* Update trainer.py

* change "testcode" to "Example" in trainer init documentation

* Update pytorch_lightning/trainer/seed.py, first line in comment

Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>

Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
Co-authored-by: Jirka <jirka.borovec@seznam.cz>
Co-authored-by: William Falcon <waf2107@columbia.edu>
This commit is contained in:
kumuji
2020-05-12 07:53:20 -04:00
committed by GitHub
co-authored by Jirka Borovec Jirka William Falcon
parent 7af4505519
commit 619f984c36
9 changed files with 116 additions and 31 deletions
+6 -11
View File
@@ -8,7 +8,7 @@ import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
import tests.base.utils as tutils
from pytorch_lightning import Trainer, LightningModule
from pytorch_lightning import Trainer, LightningModule, seed_everything
class AverageDataset(Dataset):
@@ -68,13 +68,6 @@ def test_pytorch_parity(tmpdir):
tutils.assert_speed_parity(pl_times, pt_times, num_epochs)
def set_seed(seed):
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
def vanilla_loop(MODEL, num_runs=10, num_epochs=10):
"""
Returns an array with the last loss from each epoch for each run
@@ -83,12 +76,13 @@ def vanilla_loop(MODEL, num_runs=10, num_epochs=10):
errors = []
times = []
torch.backends.cudnn.deterministic = True
for i in range(num_runs):
time_start = time.perf_counter()
# set seed
seed = i
set_seed(seed)
seed_everything(seed)
# init model parts
model = MODEL()
@@ -134,10 +128,10 @@ def lightning_loop(MODEL, num_runs=10, num_epochs=10):
# set seed
seed = i
set_seed(seed)
seed_everything(seed)
model = MODEL()
# init model parts
model = MODEL()
trainer = Trainer(
max_epochs=num_epochs,
progress_bar_refresh_rate=0,
@@ -146,6 +140,7 @@ def lightning_loop(MODEL, num_runs=10, num_epochs=10):
early_stop_callback=False,
checkpoint_callback=False,
distributed_backend='dp',
deterministic=True,
)
trainer.fit(model)
+7 -12
View File
@@ -10,7 +10,7 @@ from torch.utils.data import DataLoader
from torchvision import transforms
import tests.base.utils as tutils
from pytorch_lightning import Trainer, LightningModule
from pytorch_lightning import Trainer, LightningModule, seed_everything
from tests.base.datasets import TrialMNIST
@@ -69,13 +69,6 @@ def test_pytorch_parity(tmpdir):
tutils.assert_speed_parity(pl_times[1:], pt_times[1:], num_epochs)
def _set_seed(seed):
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
def vanilla_loop(MODEL, num_runs=10, num_epochs=10):
"""
Returns an array with the last loss from each epoch for each run
@@ -84,12 +77,13 @@ def vanilla_loop(MODEL, num_runs=10, num_epochs=10):
errors = []
times = []
torch.backends.cudnn.deterministic = True
for i in range(num_runs):
time_start = time.perf_counter()
# set seed
seed = i
_set_seed(seed)
seed_everything(seed)
# init model parts
model = MODEL()
@@ -135,17 +129,18 @@ def lightning_loop(MODEL, num_runs=10, num_epochs=10):
# set seed
seed = i
_set_seed(seed)
seed_everything(seed)
# init model parts
model = MODEL()
# init model parts
trainer = Trainer(
max_epochs=num_epochs,
progress_bar_refresh_rate=0,
weights_summary=None,
gpus=1,
early_stop_callback=False,
checkpoint_callback=False
checkpoint_callback=False,
deterministic=True,
)
trainer.fit(model)