Unreproducible pseudorandomness using CPU

Amos_Haviv_Hason: <p>We have a cluster of GPU nodes at the university, so consequent runs are most probably executed on different nodes, but all have the same software and package versions.</p>
<p>One of my models started to produce inconsistent results between runs after a recent change.</p>
<p>A fix in one line finally solved the issue, but I have no idea what the issue actually was.</p>
<p>The line prior to the fix:</p>
<pre><code class="lang-auto">a = torch.randn(n, m).to(b)
</code></pre>
<p>The line after the fix:</p>
<pre><code class="lang-auto">a = torch.randn(n, m, dtype=b.dtype, device=b.device)
</code></pre>
<p>(b is a float64 tensor on CUDA.)</p>
<p>In both cases I use a seed for everything, but only the second version guarantees reproducible results.</p>
<p>What is going on here? Shouldn’t seeded pseudorandomness on different CPUs yield the same results? Or does it have to do with the casting to float64?</p>

ptrblck: <aside class="quote no-group" data-username="Amos_Haviv_Hason" data-post="1" data-topic="206412">
<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/amos_haviv_hason/48/61652_2.png" class="avatar"> Amos_Haviv_Hason:</div>
<blockquote>
<p>Shouldn’t seeded pseudorandomness on different CPUs yield the same results?</p>
</blockquote>
</aside>
<p>I don’t know if there is a guarantee for different CPU architectures to generate the same random numbers (I would guess not, but we would need to wait for colleagues working on CPU backends to confirm). At least for NVIDIA GPUs, we do not guarantee different devices will create the same random values.<br>
Which CPUs are you using btw.?</p>

Amos_Haviv_Hason: <p>I’m not sure how to get this info. I’m guessing all CPUs are x86-64.</p>
<p>However, it seems like this change didn’t really fix the issue; it was probably a coincidence. What happens is that there are some periods in which reproductions happen and some periods in which reproductions don’t happen (even if runs are on the same GPU model but on different nodes), <strong>without any change in code in between</strong>. Thus, it is most likely an environment-related issue.</p>
<p>What kinds of environment details should I log in order to debug this issue?</p>
<p>In the meantime, I’ll log <a href="https://gist.github.com/AmosHason/78662524eda9aead7e220ab49e69df9e" rel="noopener nofollow ugc">these details</a>.</p>

ptrblck: <aside class="quote no-group" data-username="Amos_Haviv_Hason" data-post="3" data-topic="206412">
<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/amos_haviv_hason/48/61652_2.png" class="avatar"> Amos_Haviv_Hason:</div>
<blockquote>
<p>What happens is that there are some periods in which reproductions happen and some periods in which reproductions don’t happen (even if runs are on the same GPU model but on different nodes), <strong>without any change in code in between</strong>.</p>
</blockquote>
</aside>
<p>This points to a script issue where calls into the PRNG are made without you being aware of it. You could try to re-seed the code at various places to see which section of the code might have created random values behind your back.</p>

Amos_Haviv_Hason: <p>Let me rephrase that:</p>
<p>I’m able to reproduce results multiple times. Then, out of nowhere, and without any change in the code, I’m no longer able to reproduce them. Then, after a few hours, I’m able to reproduce them again. In the periods of failed reproductions, the metrics are different from the git-go.</p>

Amos_Haviv_Hason: <p>So now I experienced it again, but I have more information. The environments are similar in all aspects except for the GPU. In one run it is NVIDIA GeForce RTX 2080 Ti, and in the other it is NVIDIA GeForce RTX 3090 (both are using CUDA 12.1).</p>
<p>Is it possible that different GPUs produce different random sequences for the same seed?</p>

ptrblck: <aside class="quote no-group" data-username="Amos_Haviv_Hason" data-post="6" data-topic="206412">
<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/amos_haviv_hason/48/61652_2.png" class="avatar"> Amos_Haviv_Hason:</div>
<blockquote>
<p>Is it possible that different GPUs produce different random sequences for the same seed?</p>
</blockquote>
</aside>
<p>Yes, there is no guarantee to see the same random numbers between different platforms, CPUs, GPUs, etc. as described in the <a href="https://pytorch.org/docs/stable/notes/randomness.html">docs</a>.</p>

Amos_Haviv_Hason: <p>Yeah, I read this a number of times, but there is an <a href="https://discuss.pytorch.org/t/manual-seed-non-deterministic-on-different-gpus/44549/2">old answer</a> by Sam Gross, which states: “You should get consistent random numbers if you’re using the same seed, PyTorch version, and CUDA version even if it’s run on a different physical GPU.” Is this statement incorrect then?</p>

ptrblck: <p>Yes, the comment from 2019 is invalid as also seen in <a href="https://discuss.pytorch.org/t/reproducing-results-across-different-machines-cpu-dropout-layers/208180">this recent post</a> describing different behavior on the CPU.</p>

Amos_Haviv_Hason: <p>Thanks for the clarification. Is it specific to PyTorch, or also related to Python’s standard PRNG?</p>