Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reproducibility and performance in PyTorch

The documentation states:

Deterministic mode can have a performance impact, depending on your model.

My question is, what is meant by performance here. Processing speed or model quality (i.e. minimal loss)? In other words, when setting manual seeds and making the model perform in a deterministic way, does that cause longer training time until minimal loss is found, or is that minimal loss worse than when the model is non-deterministic?

For completeness' sake, I manually make the model deterministic by setting all of these properties:

def set_seed(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False
    np.random.seed(seed)
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
like image 518
Bram Vanroy Avatar asked May 29 '19 06:05

Bram Vanroy


People also ask

Are PyTorch models deterministic?

deterministic = True is set. The latter setting controls only this behavior, unlike torch. use_deterministic_algorithms() which will make other PyTorch operations behave deterministically, too.

What is seeding in PyTorch?

seed ()[source] Sets the seed for generating random numbers to a non-deterministic random number. Returns a 64 bit number used to seed the RNG.

Is PyTorch DataLoader deterministic?

DataLoader is not deterministic - vision - PyTorch Forums.

Does PyTorch guarantee reproducibility?

Reproducibility Completely reproducible results are not guaranteed across PyTorch releases, individual commits, or different platforms. Furthermore, results may not be reproducible between CPU and GPU executions, even when using identical seeds.

Are pre-trained models in PyTorch reproducible?

These benchmarks serve as a standard from which to start new projects or debug current implementations. But pre-trained models are already reproducible… right? In PyTorch, yes.

Are PyTorch results reproducible between CPU and GPU?

Furthermore, results may not be reproducible between CPU and GPU executions, even when using identical seeds. However, there are some steps you can take to limit the number of sources of nondeterministic behavior for a specific platform, device, and PyTorch release.

Is seed_everything() deterministic in PyTorch?

We also should notice: deterministic=True in trainer. However, if you only plan to set a random seed for python, numpy, pytorch. and you do not use pytorch lightning to train mode. seed_everything () is indeed.


2 Answers

Performance refers to the run time; CuDNN has several ways of implementations, when cudnn.deterministic is set to true, you're telling CuDNN that you only need the deterministic implementations (or what we believe they are). In a nutshell, when you are doing this, you should expect the same results on the CPU or the GPU on the same system when feeding the same inputs. Why would it affect the performance? CuDNN uses heuristics for the choice of the implementation. So, it actually depends on your model how CuDNN will behave; choosing it to be deterministic may affect the runtime because their could have been, let's say, faster way of choosing them at the same point of running.


Concerning your snippet, I do the exact seeding, it has been working good (in terms of reproducibility) for 100+ DL experiments.

like image 176
ndrwnaguib Avatar answered Sep 20 '22 12:09

ndrwnaguib


"performance" in this context refer to run-time

like image 37
Shai Avatar answered Sep 18 '22 12:09

Shai