A6000 produce wrong results

Tonis: <p>I use a server to run Stable Video diffusion models on RTX A6000, then I find that with different batch sizes, the generated videos are slightly different (all the random seeds are fixed). Then I run the same sampling on my RTX 3090, the results are the same for different batch sizes, and the results are different with the results from A6000 (random seeds are also fixed).</p>
<p>I perform a simple experiment on A6000, I input an image to a CLIP image encoder, with batch size 1 and 2. The two results are slightly different as shown in the figure below. This experiment gives exactly the same results on my 3090 card, what happens to A6000? The datatypes are both torch.float32, and I also checked that there are no BatchNorm or other random processes in CLIP image encoder.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/e/ceac6e70b506a90cc4ed70c41f97d77b774b4f36.png" data-download-href="https://discuss.pytorch.org/uploads/default/ceac6e70b506a90cc4ed70c41f97d77b774b4f36" title="WechatIMG682"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/e/ceac6e70b506a90cc4ed70c41f97d77b774b4f36_2_690x252.png" alt="WechatIMG682" data-base62-sha1="tujN4wjZO5ykclwB7hBerfndoou" width="690" height="252" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/e/ceac6e70b506a90cc4ed70c41f97d77b774b4f36_2_690x252.png, https://discuss.pytorch.org/uploads/default/optimized/3X/c/e/ceac6e70b506a90cc4ed70c41f97d77b774b4f36_2_1035x378.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/c/e/ceac6e70b506a90cc4ed70c41f97d77b774b4f36_2_1380x504.png 2x" data-dominant-color="151616"><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">WechatIMG682</span><span class="informations">1508×552 49.3 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>

Tonis: <p>And I also try other models, with different batch sizes, the outputs always have a difference. And each element may have different errors (0.0005 or 0.001) or no errors. It’s really weird!</p>

tom: <p>Results are well-defined only up to numerical accuracy. Even when things are deterministic on a system, if you run it on a different one, you might get different results.<br>
Ultimately, this is more or less a result of elementary floating-point operations not being associative, so subtle changes in how a computation is organized suddenly matter.<br>
Here is a very example that I can run my computer (with a 3090 card and recent PyTorch from git) to relatively reliably get such an effect:</p>
<pre><code class="lang-auto">In [1]: import torch
In [2]: a = torch.randn(4096, 4096, device="cuda")
In [3]: a[:100, :100].sum().item()
Out[3]: 51.92887878417969
In [4]: a[:100, :100].clone().sum().item()
Out[4]: 51.928890228271484
</code></pre>
<p>If I were to run lines 3 and 4 repeatedly, I would get the exact same results for them, but changing the layout of the array from a contiguous one (with the clone) to one that has gaps (without the clone) changes the value by a tiny bit, as the computation is organized differently.</p>
<p>Of course, here the difference is 1e-5ish but you might imagine that when you have a complex computation, in particular when involving things like normalization (which tends to amplify errors when the inputs are small), this easily gets to 1e-4 or 1e-3ish.<br>
Interestingly, integer computations don’t suffer from this as much, so a fully quantized computation might have better reproducibility.</p>
<p>Best regards</p>
<p>Thomas</p>
<p>P.S.: Of course, the real answer is to not bother with the A6000 and send it to me instead. <img src="https://discuss.pytorch.org/images/emoji/apple/stuck_out_tongue.png?v=12" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20"></p>

Tonis: <p>Hi, Thomas, thanks for your reply! I could appreciate that the computation may not be consistent when the input or computation process is slightly different. But I am still unsure why the same code could run into different results on different machines. So I test the following code on 3090:</p>
<pre><code class="lang-auto">import torch
import random, os
import numpy as np

seed = 1234    
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True

a = torch.randn(4096, 4096, device="cuda")
a[:100, :100].sum().item()

Out: -4.960655212402344
</code></pre>
<p>And same code on A6000, I get:</p>
<pre><code class="lang-auto">Out: 116.35237121582031
</code></pre>
<p>This should not be like this. After seeding everything, we should get exactly the same results across different machines. I remembered that when I copied some code from Google colab notebook, I could reproduce the results with stable diffusion, whose results should be fully dependent on the initial random noise.</p>
<p>To confirm this, I found another server running with 3090 and got result:</p>
<pre><code class="lang-auto">-4.960655212402344
</code></pre>

Tonis: <p>I further test the same code on three machines with numpy and torch(device=‘cpu’), all three machines output exactly the same results. So there must be some problem on the card A6000.</p>

smth: <p><a class="mention" href="/u/tonis">@Tonis</a> one thing you should keep in mind is that we don’t guarantee the RNG to be consistent across GPU models.<br>
So, for the same seed, you get different random numbers across A6000 versus 3090.</p>
<p>One test you can do is to change the code to generate randn on CPU, and then move it over to GPU, so that you use the same numbers across all devices.</p>
<pre><code class="lang-auto">a = torch.randn(4096, 4096, device="cpu").cuda()
</code></pre>

ptrblck: <aside class="quote no-group" data-username="Tonis" data-post="4" data-topic="194977">
<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/tonis/48/66773_2.png" class="avatar"> Tonis:</div>
<blockquote>
<p>After seeding everything, we should get exactly the same results across different machines.</p>
</blockquote>
</aside>
<p>That’s not the case as there is no guarantee the same algorithms (even for random number generation) will be used as already explained.</p>

Tonis: <p><a class="mention" href="/u/smth">@smth</a> Thanks for your reply and solution! I can now reproduce the results across the two cards with an error around 1e-5.</p>

Tonis: <p>Thanks! I got it now!</p>