2020-08-20 02:03:22 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2020-05-12 11:53:20 +00:00
|
|
|
"""Helper functions to help with reproducibility of models. """
|
|
|
|
|
2021-03-02 09:47:55 +00:00
|
|
|
import logging
|
2020-05-12 11:53:20 +00:00
|
|
|
import os
|
2020-08-07 22:33:51 +00:00
|
|
|
import random
|
|
|
|
from typing import Optional
|
2020-05-12 11:53:20 +00:00
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import torch
|
2021-01-23 23:52:04 +00:00
|
|
|
|
2021-01-12 04:30:27 +00:00
|
|
|
from pytorch_lightning.utilities import rank_zero_warn
|
2020-05-12 11:53:20 +00:00
|
|
|
|
2021-03-02 09:47:55 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2020-05-12 11:53:20 +00:00
|
|
|
|
|
|
|
def seed_everything(seed: Optional[int] = None) -> int:
|
2020-09-20 23:42:58 +00:00
|
|
|
"""
|
|
|
|
Function that sets seed for pseudo-random number generators in:
|
2020-09-30 12:38:24 +00:00
|
|
|
pytorch, numpy, python.random
|
2020-09-20 23:42:58 +00:00
|
|
|
In addition, sets the env variable `PL_GLOBAL_SEED` which will be passed to
|
|
|
|
spawned subprocesses (e.g. ddp_spawn backend).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
seed: the integer value seed for global random state in Lightning.
|
|
|
|
If `None`, will read seed from `PL_GLOBAL_SEED` env variable
|
|
|
|
or select it randomly.
|
2020-05-12 11:53:20 +00:00
|
|
|
"""
|
|
|
|
max_seed_value = np.iinfo(np.uint32).max
|
|
|
|
min_seed_value = np.iinfo(np.uint32).min
|
|
|
|
|
|
|
|
try:
|
2020-06-12 15:23:18 +00:00
|
|
|
if seed is None:
|
2021-01-12 04:30:27 +00:00
|
|
|
seed = os.environ.get("PL_GLOBAL_SEED")
|
2020-09-20 23:42:58 +00:00
|
|
|
seed = int(seed)
|
2020-05-12 11:53:20 +00:00
|
|
|
except (TypeError, ValueError):
|
|
|
|
seed = _select_seed_randomly(min_seed_value, max_seed_value)
|
2021-01-12 04:30:27 +00:00
|
|
|
rank_zero_warn(f"No correct seed found, seed set to {seed}")
|
2020-05-12 11:53:20 +00:00
|
|
|
|
2021-01-12 04:30:27 +00:00
|
|
|
if not (min_seed_value <= seed <= max_seed_value):
|
|
|
|
rank_zero_warn(f"{seed} is not in bounds, numpy accepts from {min_seed_value} to {max_seed_value}")
|
2020-05-12 11:53:20 +00:00
|
|
|
seed = _select_seed_randomly(min_seed_value, max_seed_value)
|
|
|
|
|
2021-04-07 11:17:48 +00:00
|
|
|
# using `log.info` instead of `rank_zero_info`,
|
|
|
|
# so users can verify the seed is properly set in distributed training.
|
2021-01-12 04:30:27 +00:00
|
|
|
log.info(f"Global seed set to {seed}")
|
2020-09-20 23:42:58 +00:00
|
|
|
os.environ["PL_GLOBAL_SEED"] = str(seed)
|
2020-05-12 11:53:20 +00:00
|
|
|
random.seed(seed)
|
|
|
|
np.random.seed(seed)
|
|
|
|
torch.manual_seed(seed)
|
2020-08-24 13:22:05 +00:00
|
|
|
torch.cuda.manual_seed_all(seed)
|
2020-05-12 11:53:20 +00:00
|
|
|
return seed
|
|
|
|
|
|
|
|
|
|
|
|
def _select_seed_randomly(min_seed_value: int = 0, max_seed_value: int = 255) -> int:
|
2021-01-12 04:30:27 +00:00
|
|
|
return random.randint(min_seed_value, max_seed_value)
|