Repository: pytorch
Issue: Tensor-likes are not close for test_grad_scaling_autocast_fused_optimizers Adam_cuda_float32 and SGD_cuda_float32
Labels: module: optimizer,triaged

### 🐛 Describe the bug

While i was working on moving test_optim.py test_grad_scaling_autocast_fused_optimizers to test_cuda.py below two tests failing with "Tensor-likes are not close" with tolerance mismatch exceeding the allowed limit. currently in this PR #126418 I have added the  toleranceOverride to both of these tests and this issue to track the fix.  

```
test_grad_scaling_autocast_fused_optimizers_AdamW_cuda_float32 (__main__.TestCudaOptimsCUDA) ... FAIL
test_grad_scaling_autocast_fused_optimizers_Adam_cuda_float32 (__main__.TestCudaOptimsCUDA) ... FAIL
``` 
stack trace: 

```
(c2env) [sandish@devgpu166.ash6 ~/pytorch (refactoroptimizers)]$ python test/test_cuda.py -k test_grad_scaling_autocast_fused_optimizers -v
/home/sandish/pytorch/torch/backends/cudnn/__init__.py:106: UserWarning: PyTorch was compiled without cuDNN/MIOpen support. To use cuDNN/MIOpen, rebuild PyTorch making sure the library is visible to the build system.
  warnings.warn(
/home/sandish/pytorch/torch/backends/cudnn/__init__.py:106: UserWarning: PyTorch was compiled without cuDNN/MIOpen support. To use cuDNN/MIOpen, rebuild PyTorch making sure the library is visible to the build system.
  warnings.warn(
test_grad_scaling_autocast_fused_optimizers_Adagrad_cpu_float32 (__main__.TestCudaOptimsCPU) ... ok
test_grad_scaling_autocast_fused_optimizers_AdamW_cpu_float32 (__main__.TestCudaOptimsCPU) ... ok
test_grad_scaling_autocast_fused_optimizers_Adam_cpu_float32 (__main__.TestCudaOptimsCPU) ... ok
test_grad_scaling_autocast_fused_optimizers_SGD_cpu_float32 (__main__.TestCudaOptimsCPU) ... ok
test_grad_scaling_autocast_fused_optimizers_Adagrad_cuda_float32 (__main__.TestCudaOptimsCUDA) ... skipped 'cuda is not supported for fused on Adagrad'
test_grad_scaling_autocast_fused_optimizers_AdamW_cuda_float32 (__main__.TestCudaOptimsCUDA) ... FAIL
test_grad_scaling_autocast_fused_optimizers_Adam_cuda_float32 (__main__.TestCudaOptimsCUDA) ... FAIL
test_grad_scaling_autocast_fused_optimizers_SGD_cuda_float32 (__main__.TestCudaOptimsCUDA) ... ok

======================================================================
FAIL: test_grad_scaling_autocast_fused_optimizers_AdamW_cuda_float32 (__main__.TestCudaOptimsCUDA)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/sandish/pytorch/torch/testing/_internal/common_utils.py", line 2756, in wrapper
    method(*args, **kwargs)
  File "/home/sandish/pytorch/torch/testing/_internal/common_utils.py", line 2756, in wrapper
    method(*args, **kwargs)
  File "/home/sandish/pytorch/torch/testing/_internal/common_device_type.py", line 419, in instantiated_test
    result = test(self, **param_kwargs)
  File "/home/sandish/pytorch/torch/testing/_internal/common_optimizers.py", line 211, in test_wrapper
    return test(*args, **kwargs)
  File "/home/sandish/pytorch/torch/testing/_internal/common_device_type.py", line 1208, in only_fn
    return fn(self, *args, **kwargs)
  File "/home/sandish/pytorch/test/test_cuda.py", line 4772, in test_grad_scaling_autocast_fused_optimizers
    tracker.pop_check_set(param_scaling, self)
  File "/home/sandish/pytorch/torch/testing/_internal/common_optimizers.py", line 1908, in pop_check_set
    testcase.assertEqual(tensor_to_set, ref, **self.assert_eq_kwargs)
  File "/home/sandish/pytorch/torch/testing/_internal/common_utils.py", line 3639, in assertEqual
    raise error_metas.pop()[0].to_error(
AssertionError: Tensor-likes are not close!

Mismatched elements: 64 / 64 (100.0%)
Greatest absolute difference: 2.3365020751953125e-05 at index (1, 0) (up to 1e-05 allowed)
Greatest relative difference: 3.548153108567931e-05 at index (5, 5) (up to 1.3e-06 allowed)

To execute this test, run the following from the base repo dir:
     python test/test_cuda.py -k TestCudaOptimsCUDA.test_grad_scaling_autocast_fused_optimizers_AdamW_cuda_float32

This message can be suppressed by setting PYTORCH_PRINT_REPRO_ON_FAILURE=0

======================================================================
FAIL: test_grad_scaling_autocast_fused_optimizers_Adam_cuda_float32 (__main__.TestCudaOptimsCUDA)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/sandish/pytorch/torch/testing/_internal/common_utils.py", line 2756, in wrapper
    method(*args, **kwargs)
  File "/home/sandish/pytorch/torch/testing/_internal/common_utils.py", line 2756, in wrapper
    method(*args, **kwargs)
  File "/home/sandish/pytorch/torch/testing/_internal/common_device_type.py", line 419, in instantiated_test
    result = test(self, **param_kwargs)
  File "/home/sandish/pytorch/torch/testing/_internal/common_optimizers.py", line 211, in test_wrapper
    return test(*args, **kwargs)
  File "/home/sandish/pytorch/torch/testing/_internal/common_device_type.py", line 1208, in only_fn
    return fn(self, *args, **kwargs)
  File "/home/sandish/pytorch/test/test_cuda.py", line 4772, in test_grad_scaling_autocast_fused_optimizers
    tracker.pop_check_set(param_scaling, self)
  File "/home/sandish/pytorch/torch/testing/_internal/common_optimizers.py", line 1908, in pop_check_set
    testcase.assertEqual(tensor_to_set, ref, **self.assert_eq_kwargs)
  File "/home/sandish/pytorch/torch/testing/_internal/common_utils.py", line 3639, in assertEqual
    raise error_metas.pop()[0].to_error(
AssertionError: Tensor-likes are not close!

Mismatched elements: 64 / 64 (100.0%)
Greatest absolute difference: 2.3484230041503906e-05 at index (7, 5) (up to 1e-05 allowed)
Greatest relative difference: 3.567120074876584e-05 at index (5, 5) (up to 1.3e-06 allowed)

To execute this test, run the following from the base repo dir:
     python test/test_cuda.py -k TestCudaOptimsCUDA.test_grad_scaling_autocast_fused_optimizers_Adam_cuda_float32

This message can be suppressed by setting PYTORCH_PRINT_REPRO_ON_FAILURE=0

----------------------------------------------------------------------
Ran 8 tests in 15.428s

FAILED (failures=2, skipped=1)
```
cc: @janeyx9

### Versions

```
Collecting environment information...
PyTorch version: 2.4.0a0+git8c1bac4
Is debug build: False
CUDA used to build PyTorch: 12.0
ROCM used to build PyTorch: N/A

OS: CentOS Stream 8 (x86_64)
GCC version: (GCC) 10.3.1 20210422 (Red Hat 10.3.1-1)
Clang version: Could not collect
CMake version: version 3.26.5
Libc version: glibc-2.28

Python version: 3.9.18 (main, Jan 17 2024, 17:23:57)  [GCC 8.5.0 20210514 (Red Hat 8.5.0-21)] (64-bit runtime)
Python platform: Linux-5.12.0-0_fbk16_clang_7661_geb00762ce6d2-x86_64-with-glibc2.28
Is CUDA available: True
CUDA runtime version: 12.0.140
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: Tesla P100-SXM2-16GB
GPU 1: Tesla P100-SXM2-16GB
GPU 2: Tesla P100-SXM2-16GB
GPU 3: Tesla P100-SXM2-16GB
GPU 4: Tesla P100-SXM2-16GB
GPU 5: Tesla P100-SXM2-16GB
GPU 6: Tesla P100-SXM2-16GB
GPU 7: Tesla P100-SXM2-16GB

Nvidia driver version: 525.105.17
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:                   46 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          56
On-line CPU(s) list:             0-55
Vendor ID:                       GenuineIntel
Model name:                      Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz
CPU family:                      6
Model:                           79
Thread(s) per core:              2
Core(s) per socket:              14
Socket(s):                       2
Stepping:                        1
Frequency boost:                 enabled
CPU max MHz:                     2401.0000
CPU min MHz:                     1200.0000
BogoMIPS:                        4799.97
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 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 intel_ppin ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad 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
Virtualization:                  VT-x
L1d cache:                       896 KiB (28 instances)
L1i cache:                       896 KiB (28 instances)
L2 cache:                        7 MiB (28 instances)
L3 cache:                        70 MiB (2 instances)
NUMA node(s):                    2
NUMA node0 CPU(s):               0-13,28-41
NUMA node1 CPU(s):               14-27,42-55
Vulnerability Itlb multihit:     KVM: Mitigation: VMX disabled
Vulnerability L1tf:              Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds:               Vulnerable: Clear CPU buffers attempted, no microcode; SMT vulnerable
Vulnerability Meltdown:          Vulnerable
Vulnerability Spec store bypass: Vulnerable
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Vulnerable, IBPB: disabled, STIBP: disabled
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Vulnerable: Clear CPU buffers attempted, no microcode; SMT vulnerable

Versions of relevant libraries:
[pip3] flake8==7.0.0
[pip3] mypy==1.10.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.4
[pip3] optree==0.11.0
[pip3] pytorch-triton==3.0.0+45fff310c8
[pip3] torch==2.4.0a0+git8c1bac4
[conda] magma-cuda110             2.5.2                         1    pytorch
[conda] mkl-include               2024.1.0              intel_691    intel
[conda] mkl-static                2024.1.0              intel_691    intel
```

cc @vincentqb @jbschlosser @albanD @janeyx99 @crcrpar