Accumulation of precision loss due to sigmoid

pylearn: <p>I have a large model which involves sigmoid and logsigmoid functions. I am using float64 for better accuracy with <code>torch.set_default_dtype(torch.float64)</code>.</p>
<p>Running the same code twice, I got different training results. I printed the loss to see what happened. I found out the losses are the same for the first few hundred iterations. Then there are tiny difference (after the 10th decimal place). Eventually, the difference becomes so large that the training is not reproducible.</p>
<p>In my case, there are random generation. However, I don’t think that is the problem since I use random generator with a fixed seed.</p>
<p>Hence, I think the reason is the accumulation of precision loss. How can I control the precision loss? In general, is it true that the same code will produce different results even there is no randomness involved?</p>
<p>What confuse me is that shouldn’t the precision loss be deterministic (within the same machine)? and thus leading to the same result?</p>

ptrblck: <p>Yes, the code could result in small differences due to the limited floating point precision and since floating point operations are not associative. The used algorithm might not be deterministic (you can enable deterministic algorithms on the GPU for a potential performance penalty) and could thus result in these mismatches. Note that both results are still valid and your initial one is not closer to the theoretical output than the next run.</p>

pylearn: <p>The structure of the network I was using is a simple one, but quite deep. It contains mostly linear layers with the weight matrix masked by some buffered binary matrix. It seems like none of the operation could be random.</p>
<p>Well, MaxPooling is a simple operation that is random on GPU. So maybe linear layer is random as well? Is there a list showing all the random operations on GPU?</p>
<p>What about in CPU? Will the precision loss accumulate and lead to different result (on the same machine but different time)? I am trying to figure out the reason and avoid it. If the irreproducibility is due to accumulation of precision loss, I guess there is no way to get around it?</p>

ptrblck: <aside class="quote no-group" data-username="pylearn" data-post="3" data-topic="188367">
<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/pylearn/48/54463_2.png" class="avatar"> pylearn:</div>
<blockquote>
<p>It seems like none of the operation could be random.</p>
</blockquote>
</aside>
<p>Again, non-deterministic results are not necessarily caused by any random operation, but are due to the algorithmic implementation using floating point values in different orders.<br>
A simple example is:</p>
<pre data-code-wrap="python"><code class="lang-python">x = torch.randn(10, 10, 10)
x1 = x.sum()
x2 = x.sum(0).sum(0).sum(0)

print(x1 - x2)
# tensor(2.6226e-06)

x_64 = x.double().sum()
print(x1 - x_64)
# tensor(1.2807e-06, dtype=torch.float64)
print(x2 - x_64)
# tensor(-1.3419e-06, dtype=torch.float64)
</code></pre>
<p>As you can see a different order of operations already creates the expected mismatch for a simple <code>sum</code>.</p>
<aside class="quote no-group" data-username="pylearn" data-post="3" data-topic="188367">
<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/pylearn/48/54463_2.png" class="avatar"> pylearn:</div>
<blockquote>
<p>If the irreproducibility is due to accumulation of precision loss, I guess there is no way to get around it?</p>
</blockquote>
</aside>
<p>Check the <a href="https://pytorch.org/docs/stable/notes/randomness.html">Reproducibility docs</a> to see how deterministic algorithms can be enabled, if they are available.</p>

pylearn: <aside class="quote no-group" data-username="ptrblck" data-post="4" data-topic="188367" data-full="true">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>non-deterministic results are not necessarily caused by any random operation, but are due to the algorithmic implementation using floating point values in different orders.</p>
</blockquote>
</aside>
<p>Yes, that was what I meant. My bad for using the term “random operation” for nondeterministic implementation.</p>
<p>Back to my original question, suppose we control all operations such that only deterministic implementation is used, will the precision loss be different and accumulate to different results on the same machine across time?</p>
<p>For example, <code>a = f(...)</code>, the theoretical computation result for <code>a</code> is 1/3, by using the same code on the same machine, will we sometimes get <code>a = 0.33···111</code> and sometimes get <code>a=0.33···222</code>? (the different digits are due to precision loss), so that further computation carry the difference in precision loss and accumulate to significant different results?</p>

pylearn: <p>Based on my limited tests, if only deterministic implementation is used, then results will be the same across multiple executions, that is, the precision loss is deterministic and will not lead to different results across multiple executions (on the same machine). To use my toy example above, if <code>a = f(...)</code> results in <code>a=0.33···222</code>, then it will always be <code>a=0.33···222</code>.</p>
<p>Thank you <a class="mention" href="/u/ptrblck">@ptrblck</a> for your help.</p>
<p>Note that this conclusion is based on my limited tests, it could be wrong in general. To understand the behavior for sure, it requires more knowledge on floating number and hardware, which is out of the scope of this topic.</p>