Matmul failure after dtype change on mixed AMD setup
Labels: module: rocm, triaged

### 🐛 Describe the bug

`matmul` fails with torch.float64 after float32 on mixed devices.

I have a setup with AMD MI100 (gfx908) and  RADEONVII (gfx906) cards. torch is compiled from source (latest main) against rocm 5.6.0. I was trying some performance comparison and noticed `matmul` fails when trying to do this for different dtypes on both devices. The failing code is below. After doing float32, the float64 version fails (bogus result) on the first device the float32 version was run on. Switching the order of the float32 evaluation, makes the float64 fail on the other device, so it appears to not be a device specific issue. Similarly, doing the float64 evaluations first, then the float32 evalution will fail, so it's also not a dtype specific issue. It's using both devices and dtypes that causes the issue.

```python
from torch import tensor, cuda, float32, float64, allclose

t1 = tensor([[2., -1.], [-1., 2]])
t2 = tensor([[2., -1.], [-1., 2]])

def test(t1, t2, dtype, device, comparison=None):
    print(f"Device: {device}, type: {dtype}")
    t1 = t1.to(dtype).to(device)
    t2 = t2.to(dtype).to(device)
    result = t1.matmul(t2)
    result = result.to("cpu")
    if comparison is not None:
        print(f"Max diff: {abs(result - comparison).max():.8f}")
        assert allclose(result, comparison)
    return result

r32 = test(t1, t2, float32, "cpu")
r64 = test(t1, t2, float64, "cpu")

test(t1, t2, float32, "cuda:0", r32)
test(t1, t2, float32, "cuda:1", r32)

test(t1, t2, float64, "cuda:0", r64)    # FAILS WITH ASSERT ERROR DUE TO BOGUS RESULT
test(t1, t2, float64, "cuda:1", r64)


### Versions

Collecting environment information...
PyTorch version: 2.2.0a0+gita545905
Is debug build: False
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: 5.6.31061-8c743ae5d

OS: Linux Mint 21.2 (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.27.6
Libc version: glibc-2.35

Python version: 3.11.5 (main, Aug 29 2023, 09:38:28) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-6.2.0-1014-lowlatency-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: AMD Instinct MI100
Nvidia driver version: Could not collect
cuDNN version: Could not collect
HIP runtime version: 5.6.31061
MIOpen runtime version: 2.20.0
Is XNNPACK available: True

CPU:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Address sizes:                      46 bits physical, 48 bits virtual
Byte Order:                         Little Endian
CPU(s):                             32
On-line CPU(s) list:                0-31
Vendor ID:                          GenuineIntel
Model name:                         13th Gen Intel(R) Core(TM) i9-13900K
CPU family:                         6
Model:                              183
Thread(s) per core:                 2
Core(s) per socket:                 24
Socket(s):                          1
Stepping:                           1
CPU max MHz:                        5800.0000
CPU min MHz:                        800.0000
BogoMIPS:                           5990.40
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 tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi umip pku ospke waitpkg gfni vaes vpclmulqdq tme rdpid movdiri movdir64b fsrm md_clear serialize pconfig arch_lbr ibt flush_l1d arch_capabilities
Virtualization:                     VT-x
L1d cache:                          896 KiB (24 instances)
L1i cache:                          1.3 MiB (24 instances)
L2 cache:                           32 MiB (12 instances)
L3 cache:                           36 MiB (1 instance)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-31
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: Not affected
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; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] flake8==5.0.4
[pip3] flake8-black==0.3.6
[pip3] flake8-isort==6.1.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.24.4
[pip3] torch-rocm-5.6.0==2.2.0a0+gita545905
[conda] Could not collect


cc @jeffdaily @sunway513 @jithunnair-amd @pruthvistony @ROCmSupport @dllehr-amd @jataylo @hongxiayang