Training loss is different when using different pytorch version

mf-giwoong-lee: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/f/3fb5dca2befd3810e05b7f9da6493627d9b3d0f2.png" data-download-href="https://discuss.pytorch.org/uploads/default/3fb5dca2befd3810e05b7f9da6493627d9b3d0f2" title="Untitled"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/f/3fb5dca2befd3810e05b7f9da6493627d9b3d0f2_2_690x298.png" alt="Untitled" data-base62-sha1="95BIvvaChqRqJ0rNJ39b5xYsYHo" width="690" height="298" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/f/3fb5dca2befd3810e05b7f9da6493627d9b3d0f2_2_690x298.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/f/3fb5dca2befd3810e05b7f9da6493627d9b3d0f2_2_1035x447.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/f/3fb5dca2befd3810e05b7f9da6493627d9b3d0f2_2_1380x596.png 2x" data-dominant-color="FCFBFC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Untitled</span><span class="informations">2138×924 81.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I want to upgrade pytorch version from 1.7.1 to 1.11.0.</p>
<p>Before do that, I want to generate same result from 1.7.1 and 1.11.0</p>
<p>I tested my network based on gpt2 with various pytorch version but the results are like above.</p>
<p>I set same random seed value (=0). But the loss graphs are slightly different.</p>
<p>I used same code to test and use cuda version for pytorch as 11.x (1.7.1-cuda11.0, 1.8.0&amp;1.9.0-cuda11.1, 1.11.0-cuda11.3)</p>
<p>Base docker image for testing is cuda11.3-ubuntu 18.04.</p>
<p>I tried to find some differences between 1.7.1 and 1.11.0 but I could not find any significant differences between them…</p>
<p>Is there any possible solution or any advise to solve this problem?</p>

Samuel_Bachorik: <p>Hi, I am not sure what your network architecture is but in my opinion, maybe if you are not using any weight initialization for your layers then from version to version there may be other basic weights in layers. Also some other functions can be improved.</p>

mf-giwoong-lee: <p>I used xavier_uniform, xavier_normal in custom attention layer and reset_parameter function in pre-defined pytorch layers e.g: nn.Linear, nn.LayerNorm, nn.Dropout.</p>
<p>Your answer is related to reset_parameter function in torch layers.</p>
<p>Thanks to reply my question. I try it!</p>
<p>Also if some guys have a solution or advise, then please reply your answer!</p>

mxahan: <p>I was just wondering if you have tried the seed control as discussed <a href="https://pytorch.org/docs/stable/notes/randomness.html" rel="noopener nofollow ugc">here</a>.</p>

mf-giwoong-lee: <p>I just used torch.manual_seed, random.seed, np.random.seed, torch.backends.cudnn.deterministic = True, torch.backends.cudnn.benchmark = False.</p>
<p>Your link describes additional methods for reproducibility.</p>
<p>Thanks! I will try it!</p>

mf-giwoong-lee: <p>I have not gotten a meaningful solution…</p>
<p>I guess that cuda versions of each pytorch version are different so the models are trained in slightly different manner.</p>
<p>I want more advise or possible solutions nowadays…</p>

InnovArul: <blockquote>
<p>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.</p>
</blockquote>
<p>There are numerous factors affecting reproducibility.<br>
What about the quantitative results in terms of accuracy?<br>
You haven’t talked about it. Can you pl. share if they are more or less equal?</p>

mf-giwoong-lee: <p>Your saying is that if I train models in same machine environment (like same CPU and GPU), then completely reproducible results are not guaranteed across PyTorch releases?</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/4/2493cac0f24cd219d864b08ba3ef70654b19cd09.png" data-download-href="https://discuss.pytorch.org/uploads/default/2493cac0f24cd219d864b08ba3ef70654b19cd09" title="val_rmse"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/2493cac0f24cd219d864b08ba3ef70654b19cd09_2_690x318.png" alt="val_rmse" data-base62-sha1="5dzPzD9wvDg7MAMYjleItQ2NDtf" width="690" height="318" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/2493cac0f24cd219d864b08ba3ef70654b19cd09_2_690x318.png, https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/2493cac0f24cd219d864b08ba3ef70654b19cd09_2_1035x477.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/4/2493cac0f24cd219d864b08ba3ef70654b19cd09_2_1380x636.png 2x" data-dominant-color="FCFDFD"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">val_rmse</span><span class="informations">2157×996 88.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>This is validation result (validation  RMSE)</p>

mf-giwoong-lee: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/5/f5a4ffa32a60882331cff9adf26936f291345ef3.png" data-download-href="https://discuss.pytorch.org/uploads/default/f5a4ffa32a60882331cff9adf26936f291345ef3" title="val_loss"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/5/f5a4ffa32a60882331cff9adf26936f291345ef3_2_690x301.png" alt="val_loss" data-base62-sha1="z34ugeDxtWxG8iNbZyTwRZPdGVl" width="690" height="301" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/5/f5a4ffa32a60882331cff9adf26936f291345ef3_2_690x301.png, https://discuss.pytorch.org/uploads/default/optimized/3X/f/5/f5a4ffa32a60882331cff9adf26936f291345ef3_2_1035x451.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/5/f5a4ffa32a60882331cff9adf26936f291345ef3_2_1380x602.png 2x" data-dominant-color="FBFCFC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">val_loss</span><span class="informations">2151×940 104 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>This is validation loss.</p>

InnovArul: <aside class="quote no-group" data-username="mf-giwoong-lee" data-post="8" data-topic="148534">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mf-giwoong-lee/48/48393_2.png" class="avatar"> mf-giwoong-lee:</div>
<blockquote>
<p>our saying is that if I train models in same machine environment (like same CPU and GPU), then completely reproducible results are not guaranteed across PyTorch releases?</p>
</blockquote>
</aside>
<p>There are two points:</p>
<ol>
<li>
<p>Reproducibility between different PyTorch releases, individual commits, or different platforms is not guaranteed regardless of the same CPU/GPU usage.</p>
</li>
<li>
<p>If you run the same code in CPU mode vs. GPU mode, still the reproducibility is not guaranteed.</p>
</li>
</ol>
<p>You can verify if the quantitative results are close enough (RMSE, Accuracy, mAP, F1 score etc.,) in all the versions.</p>