4

Does torch.manual_seed include the operation of torch.cuda.manual_seed_all?

If yes, we can just use torch.manual_seed to set the seed. Otherwise we should call both functions.

iacob
  • 20,084
  • 6
  • 92
  • 119
Fengfan Zhou
  • 75
  • 1
  • 4

3 Answers3

3

Yes, torch.manual_seed() does include CUDA:

You can use torch.manual_seed() to seed the RNG for all devices (both CPU and CUDA):

iacob
  • 20,084
  • 6
  • 92
  • 119
1

See Pytorch lightning's seed_everything:

random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)

Makes me believe these are all and only the required seeds.

Gulzar
  • 23,452
  • 27
  • 113
  • 201
  • 1
    There may be [other sources of randomness](https://stackoverflow.com/questions/67511658/training-pytorch-models-on-different-machines-leads-to-different-results/67511954#67511954) in PyTorch models, depending on the specific modules/functions used. – iacob May 18 '21 at 12:23
1

Yes, torch.manual_seed calls torch.cuda.manual_seed_all internally.

Additional evidence other than @iacob's answer can be found in the PyTorch source code

def manual_seed(seed) -> torch._C.Generator:
    ...

    if not torch.cuda._is_in_bad_fork():
        torch.cuda.manual_seed_all(seed)

    ...
C_T
  • 11
  • 1