Repository: pytorch
Issue: Excution difference between Mac and Linux
Labels: needs reproduction,triaged,module: macos,module: arm

### 🐛 Describe the bug

Hi,
TLDR, running the same network on the different linux machines lead to same results, running the network on different mac machines lead to same results but different from linux. The problem is that the results obtained on the linux machines are order of magnitude different from the mac ones ~10^4 when the same weights are use.

I have a very simple network:
```python
class NNModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.hidden1 = nn.Linear(12, 256)
        self.act1 = nn.LeakyReLU()
        self.hidden2 = nn.Linear(256, 256)
        self.act2 = nn.LeakyReLU()
        self.output = nn.Linear(256, 6)
        self.act_output = nn.Sigmoid()
 
    def forward(self, x):
        x = self.act1(self.hidden1(x))
        x = self.act2(self.hidden2(x))
        x = self.output(x)
        return x
```

The network doesn't learn anything on the linux machines whilst being extremely performant on the mac machines. 
At first we thought of some library difference. We tried this with pytorch going from v 2.0 -> 2.3 and the results are consistent. I have the weights, the code, some date uploaded on my [github](https://github.com/NicolayP/Torch-Mac-Linux-Error?tab=readme-ov-file) as well as the expected results on both OS. Unfortunately this is the smallest snippet we have that can replicate the results. 

At first we thought the issue was with the backend blas library (https://discuss.pytorch.org/t/matmul-gives-different-results-depending-on-operating-system/56303) however testing on mac with [mkl](https://www.intel.com/content/www/us/en/docs/onemkl/get-started-guide/2023-0/overview.html) and [accelerate](https://gitlab.aei.uni-hannover.de/cmake/accelerate-lapacke) leads to same results. We tried on different CPUs, different linux versions and the results are consistent yet still different between mac and linux. 

To give a metric of the difference, using the same input we get:

The input tensor is 
```bash
tensor([ 0.2476,  0.1170,  0.0054, -0.3450, -0.4259, -2.4299], dtype=torch.float64) 
```

The output on mac is
```bash
tensor([-0.0719, -0.0403, -0.0102, -0.0413,  0.1258, -0.0873], dtype=torch.float64) 
```

The output on linux with the same weights:
```bash
tensor([-192.7449, 255.3847, 153.4133, 605.0937, 1240.0849, 2499.9711], dtype=torch.float64)
```
I know those values do not make a lot a sense without the context but the magnitude of the difference is just too large for simple matrix multiplications... 

I can give out more specific details about every machine but given how that we managed to replicate the results across multiple machines with different configuration I don't know how much that would help. Just in case this I still linked the environment from my linux machine.

### Versions

Collecting environment information...
PyTorch version: 2.3.0
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.4 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.22.1
Libc version: glibc-2.35

Python version: 3.12.3 | packaged by conda-forge | (main, Apr 15 2024, 18:38:13) [GCC 12.3.0] (64-bit runtime)
Python platform: Linux-6.5.0-28-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.1.105
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3070 Ti Laptop GPU
Nvidia driver version: 525.147.05
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Address sizes:                      48 bits physical, 48 bits virtual
Byte Order:                         Little Endian
CPU(s):                             16
On-line CPU(s) list:                0-15
Vendor ID:                          AuthenticAMD
Model name:                         AMD Ryzen 7 6800H with Radeon Graphics
CPU family:                         25
Model:                              68
Thread(s) per core:                 2
Core(s) per socket:                 8
Socket(s):                          1
Stepping:                           1
CPU max MHz:                        4785.0000
CPU min MHz:                        400.0000
BogoMIPS:                           6388.02
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr rdpru wbnoinvd cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif v_spec_ctrl umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor smca fsrm
Virtualisation:                     AMD-V
L1d cache:                          256 KiB (8 instances)
L1i cache:                          256 KiB (8 instances)
L2 cache:                           4 MiB (8 instances)
L3 cache:                           16 MiB (1 instance)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-15
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit:        Not affected
Vulnerability L1tf:                 Not affected
Vulnerability Mds:                  Not affected
Vulnerability Meltdown:             Not affected
Vulnerability Mmio stale data:      Not affected
Vulnerability Retbleed:             Not affected
Vulnerability Spec rstack overflow: Vulnerable: Safe RET, no microcode
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Retpolines, IBPB conditional, IBRS_FW, STIBP always-on, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] numpy==1.26.4
[pip3] torch==2.3.0
[pip3] torchaudio==2.3.0
[pip3] torchvision==0.18.0
[conda] blas                      1.0                         mkl    conda-forge
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] libblas                   3.9.0            16_linux64_mkl    conda-forge
[conda] libcblas                  3.9.0            16_linux64_mkl    conda-forge
[conda] liblapack                 3.9.0            16_linux64_mkl    conda-forge
[conda] mkl                       2022.2.1         h84fe81f_16997    conda-forge
[conda] numpy                     1.26.4          py312heda63a1_0    conda-forge
[conda] pytorch                   2.3.0           py3.12_cuda11.8_cudnn8.7.0_0    pytorch
[conda] pytorch-cuda              11.8                 h7e8668a_5    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchaudio                2.3.0               py312_cu118    pytorch
[conda] torchvision               0.18.0              py312_cu118    pytorch

cc @malfet @albanD @snadampal