Get different scores in different machine with the same torch version

narrowsnap: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/b/4b703e6d20b4c270db350241111b9f8f08353b31.png" data-download-href="https://discuss.pytorch.org/uploads/default/4b703e6d20b4c270db350241111b9f8f08353b31" title="Screen_Shot_2021-05-11_at_3_25_37_PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/b/4b703e6d20b4c270db350241111b9f8f08353b31_2_690x97.png" alt="Screen_Shot_2021-05-11_at_3_25_37_PM" data-base62-sha1="aLmi51kKJe7LV4jEAnKGTGqk8HD" width="690" height="97" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/b/4b703e6d20b4c270db350241111b9f8f08353b31_2_690x97.png, https://discuss.pytorch.org/uploads/default/optimized/3X/4/b/4b703e6d20b4c270db350241111b9f8f08353b31_2_1035x145.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/4/b/4b703e6d20b4c270db350241111b9f8f08353b31_2_1380x194.png 2x" data-dominant-color="F7F6F6"><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">Screen_Shot_2021-05-11_at_3_25_37_PM</span><span class="informations">3520×496 86.1 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>Cause the same model and code to get different output on different machines.</p>
<p>Who has encountered the same problem? Or told me why this happens. Thank you.</p>
<p>NumPy gets the right value.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/5/3509117c0a35725dea85dead70cb3dc9b72e92f2.png" data-download-href="https://discuss.pytorch.org/uploads/default/3509117c0a35725dea85dead70cb3dc9b72e92f2" title="Screen_Shot_2021-05-11_at_3_39_03_PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/5/3509117c0a35725dea85dead70cb3dc9b72e92f2_2_690x194.png" alt="Screen_Shot_2021-05-11_at_3_39_03_PM" data-base62-sha1="7zaJy5y6nujpk2NPNyQwjuXICkO" width="690" height="194" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/5/3509117c0a35725dea85dead70cb3dc9b72e92f2_2_690x194.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/5/3509117c0a35725dea85dead70cb3dc9b72e92f2_2_1035x291.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/5/3509117c0a35725dea85dead70cb3dc9b72e92f2_2_1380x388.png 2x" data-dominant-color="F5F5F5"><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">Screen_Shot_2021-05-11_at_3_39_03_PM</span><span class="informations">3418×964 246 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>

eqy: <p>This is interesting. I wonder if it could be due to different vectorization implementations being available on the different machines yielding different results.</p>
<p>Can you give us some information about the processor(s) of each machine (e.g., the output of <code>cat /proc/cpuinfo</code>)?<br>
I’m also curious if manually writing the sum to force it to proceed without vectorization produces different results (e.g.,</p>
<pre><code class="lang-auto">a = torch.ones(512, 512)
a = a/255
s = 0.0
for i in range(512):
    for j in range(512):
        s += a[i, j]
print(s)
</code></pre>

narrowsnap: <p>Thanks for your reply.</p>
<p>This code:</p>
<pre><code class="lang-auto">a = torch.ones(512, 512)
a = a/255
s = 0.0
for i in range(512):
    for j in range(512):
        s += a[i, j]
print(s)
</code></pre>
<p>gets the same value(1026.9807) in two machine，but different from <code>(512*512)/255</code>(1028.0156862745098)</p>
<p>The processor information of machine one is:</p>
<pre><code class="lang-auto">processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 79
model name	: Intel(R) Core(TM) i7-6850K CPU @ 3.60GHz
stepping	: 1
microcode	: 0xb000038
cpu MHz		: 1199.605
cache size	: 15360 KB
physical id	: 0
siblings	: 12
core id		: 0
cpu cores	: 6
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 20
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single pti intel_ppin ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap intel_pt xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts md_clear flush_l1d
bugs		: cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs taa itlb_multihit
bogomips	: 7195.69
clflush size	: 64
cache_alignment	: 64
address sizes	: 46 bits physical, 48 bits virtual
power management:
</code></pre>
<p>The second machine:</p>
<pre><code class="lang-auto">processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 85
model name	: Intel(R) Xeon(R) Silver 4114 CPU @ 2.20GHz
stepping	: 4
microcode	: 0x2006a08
cpu MHz		: 800.234
cache size	: 14080 KB
physical id	: 0
siblings	: 20
core id		: 0
cpu cores	: 10
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 22
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single pti intel_ppin ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku ospke md_clear flush_l1d
bugs		: cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs taa itlb_multihit
bogomips	: 4400.00
clflush size	: 64
cache_alignment	: 64
address sizes	: 46 bits physical, 48 bits virtual
power management:
</code></pre>

eqy: <p>Interesting, both CPUs have <code>avx2</code> support and I’m not sure if PyTorch has any <code>avx512</code> capabilities upstreamed (or in 1.6.0, for that matter). Can you try to see if using double precision initially is a usable workaround? <code>a = torch.ones(512, 512, dtype=torch.float64)</code></p>

narrowsnap: <p>Thanks, looks like a precision issue. The values are very close when using double precision to calculate. <code>1028.0156862745062</code> vs <code>1028.0156862745064</code>.</p>

narrowsnap: <p>There is still a problem. I trained my network with float32. The model output is different in the second decimal place in some image (e.g. <code>-0.7769964337348938</code> vs <code>-0.7643359303474426</code>).  Is there any other solution besides modifying the precision?</p>

eqy: <p>Are you applying the <a href="https://pytorch.org/docs/stable/notes/randomness.html?highlight=determinism" rel="noopener nofollow ugc">standard tricks</a> to try to make sure your model training is deterministic/starts from the same random seed etc. across the two machines?</p>

narrowsnap: <p>The model was trained once and inference in two different machines(GPU is P100 and K80). Most images have a close output but some of them are different. However, the output is the same when run multi times on the same machine. Does this mean that the reason is not a random seed？Is this caused by the GPU?</p>

eqy: <p>If the model is already trained, then it doesn’t sound like the difference is due to random seeds. Are the outputs far enough apart to affect model accuracy measurably? P100 and K80 are different enough that I would expect cuDNN to use different algorithms for operations like convolution which will likely have different numerical behavior.</p>

narrowsnap: <p>Thank you for all your assistance! I know why this happens.</p>