Not quite deterministic behavior after seeding

CDhere: <p>I am using seeds to control the reproducibility of my experiments; specifically, I am using:</p>
<pre><code class="lang-auto">np.random.seed(seed)
torch.manual_seed(seed)
random.seed(seed)
</code></pre>
<p>However, the results each round are not completely the same: the losses I get are almost the same (within 1e-4 precision) but not quite; the classification accuracies are also slightly off by about 1e-1 each time. For example:</p>
<pre><code class="lang-auto">Epoch 1 Batch 100/782, loss: 4.536677837371826
Epoch 1 Batch 200/782, loss: 4.429199695587158
Epoch 1 Batch 300/782, loss: 4.34957218170166
Epoch 1 Batch 400/782, loss: 4.270653247833252
Epoch 1 Batch 500/782, loss: 4.134552478790283
Epoch 1 Batch 600/782, loss: 4.002155303955078
Epoch 1 Batch 700/782, loss: 3.957610607147217
Start evaluating...
eval step: 0
eval step: 100
Finished running on val
After Epoch 1, top 1 val acc: 37.74
After Epoch 1, top 5 val acc: 68.59
</code></pre>
<p>In one round and:</p>
<pre><code class="lang-auto">Epoch 1 Batch 100/782, loss: 4.536672115325928                                                                                                                                             
Epoch 1 Batch 200/782, loss: 4.4291839599609375                                                                                                                                            
Epoch 1 Batch 300/782, loss: 4.3493218421936035                                                                                                                                            
Epoch 1 Batch 400/782, loss: 4.270333290100098                                                                                                                                             
Epoch 1 Batch 500/782, loss: 4.133965969085693                                                                                                                                             
Epoch 1 Batch 600/782, loss: 4.00153112411499                                                                                                                                              
Epoch 1 Batch 700/782, loss: 3.9571311473846436                                                                                                                                            
Start evaluating...                                                                                                                                                                        
eval step: 0                                                                                                                                                                               
eval step: 100                                                                                                                                                                             
Finished running on val                                                                                                                                                                    
After Epoch 1, top 1 val acc: 37.9                                                                                                                                                         
After Epoch 1, top 5 val acc: 68.88
</code></pre>
<p>In the other. I’m simply fine-tuning a classification network on CIFAR100 which is very straightforward so I don’t think I’ve left any other bugs that could lead to this issue.</p>
<p>Is this possible to have slightly off results even after setting a fixed seed? Are those seeds that I used enough? Thanks!</p>

mmisiur: <p>I strongly advise you to read <a href="https://pytorch.org/docs/stable/notes/randomness.html" rel="noopener nofollow ugc">Pytorch reproducibility</a></p>
<p>In my case what helped was not only setting torch and numpy seed but also setting cudnn.benchmark to false and torch.set_deterministic:</p>
<pre><code class="lang-auto">torch.manual_seed(seed)
np.random.seed(seed)
torch.backends.cudnn.benchmark = False
torch.set_deterministic(True)
</code></pre>
<p>But there is a chance that it will be not enough. In that case follow <a href="https://pytorch.org/docs/stable/notes/randomness.html" rel="noopener nofollow ugc">Pytorch reproducibility</a></p>
<p>Hope that helps!</p>

CDhere: <p>Thank you for the reference! Yes this is super helpful. It worked for me to add these two lines:</p>
<pre><code class="lang-auto">torch.backends.cudnn.benchmark = False
torch.set_deterministic(True)
</code></pre>
<p>So just to summarize, there are several sources of randomness when using Pytorch:</p>
<ol>
<li>
<strong>Application-side randomness</strong>: this happens at our application level and includes: parameter initialization, data loader sampling, augmentation, and any other sampling wrote by ourselves in our own algorithms. Most often we are controlling this randomness.</li>
</ol>
<pre><code class="lang-auto">np.random.seed(seed)
torch.manual_seed(seed)
random.seed(seed)
</code></pre>
<p>take care of this aspect.</p>
<p>Note: To go one step further, in DDP the <code>DistributedSampler</code> (which is used to split the batch across processes) uses a separate generator with its own seed to do random sampling at each epoch. It is written in a way that we (developers) usually don’t have to touch the seed. So, rest assured without tweaking the sampler!</p>
<ol start="2">
<li>
<strong>Benchmarking randomness</strong>: this is brought by the fact a CUDA convolution operation usually has several implementations, and each might result in slightly different values due to the approximation they might use. So if <code>benchmark</code> option is <code>True</code>, our application might end up choosing different implementations across runs according to the state of our machine at that time, which leads to possible discrepancy.</li>
</ol>
<p>To disable this part of randomness we need this line:</p>
<pre><code class="lang-auto">torch.backends.cudnn.benchmark = False
</code></pre>
<ol start="3">
<li>
<p><strong>CUDA algorithmic randomness</strong>: even if we have fixed our choice of CUDA implementation of the operations, the implementation itself might have randomness. I’m not familiar with the exact algorithm but it seems some algorithms would rely on certain sampling strategies or approximation, somewhat like the application-level randomness but we will not be able to touch it. This part includes:</p>
<ul>
<li>
<strong>CUDA convolution randomness</strong> that happens in CUDA convolutions, and can be disabled by setting <code>torch.backends.cudnn.deterministic = True</code>; and</li>
<li>
<strong>other operations’ randomness</strong> such as <code>torch.bmm()</code> (<a href="https://pytorch.org/docs/stable/generated/torch.set_deterministic.html#torch.set_deterministic" rel="noopener nofollow ugc">full list here</a>), and can be disabled by calling <code>torch.set_deterministic(True)</code>. However, this also takes care of the CUDA convolution randomness, so it is enough to use this line to turn off all CUDA algorithmic randomness.</li>
</ul>
</li>
</ol>
<p><strong>To summarize</strong>, we would need the following lines to make completely reproducible (not across Pytorch versions &amp; platforms, only our own runs on fixed machines) experiments:</p>
<pre><code class="lang-python"># Application-side randomness
np.random.seed(seed)
torch.manual_seed(seed)
random.seed(seed)

# Benchmarking randomness
torch.backends.cudnn.benchmark = False

# CUDA algorithmic randomness
torch.set_deterministic(True)
</code></pre>
<p>I still want to make two notes here in case people get confused:</p>
<ol>
<li>
<p><code>torch.set_deterministic(True)</code> shuts off algorithmic randomness for <strong>all</strong> CUDA operations, whereas <code>torch.backends.cudnn.deterministic = True</code> only shuts off the CUDA convolution randomness. <strong>So it is enough to just use the former one.</strong></p>
</li>
<li>
<p><code>torch.backends.cudnn.deterministic</code> and <code>torch.backends.cudnn.benchmark</code> mean <strong>totally different things</strong>:</p>
<ul>
<li>
<code>...deterministic = True</code> disables the CUDA convolution randomness mentioned above, whereas</li>
<li>
<code>...benchmark = False</code> disables the algorithm selection.</li>
</ul>
</li>
</ol>
<p>And, as <a class="mention" href="/u/mmisiur">@mmisiur</a> suggested, the <a href="https://pytorch.org/docs/stable/notes/randomness.html" rel="noopener nofollow ugc">Reproducibility Doc</a> is always a good reference.</p>