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

Question:

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.

Asked By: Fengfan Zhou

||

Answers:

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.

Answered By: Gulzar

Yes, torch.manual_seed() does include CUDA:

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

Answered By: iacob

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)

    ...
Answered By: C_T
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.