| 86 | |
| 87 | |
| 88 | def set_seed(seed): |
| 89 | # seed init. |
| 90 | random.seed(seed) |
| 91 | np.random.seed(seed) |
| 92 | os.environ["PYTHONHASHSEED"] = str(seed) |
| 93 | |
| 94 | # torch seed init. |
| 95 | torch.manual_seed(seed) |
| 96 | torch.cuda.manual_seed(seed) |
| 97 | torch.cuda.manual_seed_all(seed) |
| 98 | |
| 99 | torch.backends.cudnn.deterministic = True |
| 100 | torch.backends.cudnn.benchmark = False |
| 101 | torch.backends.cudnn.enabled = True # train speed is slower after enabling this opts. |
| 102 | |
| 103 | # https://pytorch.org/docs/stable/generated/torch.use_deterministic_algorithms.html |
| 104 | os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":16:8" |
| 105 | |
| 106 | # avoiding nondeterministic algorithms (see https://pytorch.org/docs/stable/notes/randomness.html) |
| 107 | torch.use_deterministic_algorithms(True, warn_only=True) |
| 108 | |
| 109 | |
| 110 | with Engine(custom_parser=parser) as engine: |