Set random seed for reproducability.
(seed)
| 564 | return True |
| 565 | |
| 566 | def set_random_seed(seed): |
| 567 | """Set random seed for reproducability.""" |
| 568 | if seed is not None: |
| 569 | global _GLOBAL_RANDOM_SEED |
| 570 | _GLOBAL_RANDOM_SEED = seed |
| 571 | assert seed > 0 |
| 572 | random.seed(seed) |
| 573 | np.random.seed(seed) |
| 574 | torch.manual_seed(seed) |
| 575 | torch.cuda.manual_seed(seed) |
| 576 | torch.cuda.manual_seed_all(seed) # if you are using multi-GPU. |
| 577 | torch.backends.cudnn.benchmark = True |
| 578 | torch.backends.cudnn.deterministic = True |
| 579 | torch.backends.cudnn.enabled = True # False |
| 580 | torch.backends.cuda.matmul.allow_tf32 = False # if set it to True will be much faster but not accurate |
| 581 | try: |
| 582 | import deepspeed |
| 583 | if deepspeed.checkpointing.is_configured(): |
| 584 | mpu.model_parallel_cuda_manual_seed(seed) |
| 585 | except ImportError: |
| 586 | pass |
| 587 | |
| 588 | def reset_random_seed(scale=1): |
| 589 | assert _GLOBAL_RANDOM_SEED is not None, "You have not set random seed. No need to reset it." |
no outgoing calls
no test coverage detected