Repository: pytorch
Issue: Failures in test_nn depending on test order
Labels: module: nn,triaged,module: testing

### 🐛 Describe the bug

I found that some CUDA tests in `test_nn` are very sensible to input values and fail when run in a different order but pass in another order.

In particular running `python test_nn.py` fails those tests:
- test_Conv1d_pad_same_cuda_tf32
- test_RNN_cudnn_weight_norm
- test_cudnn_weight_tying
- test_partial_flat_weights

Running `python test_nn.py -k test_Conv1d_pad_same_cuda_tf32` makes the test pass but `python test_nn.py -k test_Conv1d` fails as another test runs first. E.g.: `python test_nn.py -v -- TestNN.test_Conv1d_pad_same TestNN.test_Conv1d_pad_same_cuda_tf32`

Similar for `test_Conv2d_cuda_tf32` which fails when run alone, but passes with `python test_nn.py -v -k test_Conv2d` which runs other tests first

Investigating `test_Conv1d_pad_same_cuda_tf32` I found when running it alone, the input tensor starts with values `[-0.0883,  0.3420,  0.4112,  1.0051, -0.1117, -0.5988, -0.0982, -0.3511,  0.7209, -0.2169]` while running it after another Conv1d test it starts with `[-0.7140, -2.5363,  0.3875, -1.8925, -1.0344, -0.2548, -0.3988, 0.0979,  0.5487,  1.0110]` which I traced to input tensor caching at https://github.com/pytorch/pytorch/blob/d6b556bd981421acbfdaf1de2d392aaf5f7cd58d/torch/testing/_internal/common_nn.py#L3365

This also explains failures in other tests when run alone as (I assume) the input values will also be different.

The error differences as reported are:
```
FAIL: test_Conv1d_pad_same_cuda_tf32 (__main__.TestNN)
----------------------------------------------------------------------
Mismatched elements: 2 / 60 (3.3%)
Greatest absolute difference: 0.007216689253091602 at index (1, 0, 0) (up to 0.005 allowed)
Greatest relative difference: 0.0016201659975987861 at index (3, 0, 0) (up to 0 allowed)

To execute this test, run the following from the base repo dir:
     python test/test_nn.py -k test_Conv1d_pad_same_cuda_tf32

======================================================================
FAIL: test_RNN_cudnn_weight_norm (__main__.TestNN)
----------------------------------------------------------------------
Mismatched elements: 91 / 252 (36.1%)
Greatest absolute difference: 7.40252435207367e-05 at index (5, 3, 4) (up to 1e-05 allowed)
Greatest relative difference: 0.012152512557804585 at index (3, 4, 5) (up to 1.3e-06 allowed)

To execute this test, run the following from the base repo dir:
     python test/test_nn.py -k test_RNN_cudnn_weight_norm

======================================================================
FAIL: test_cudnn_weight_tying (__main__.TestNN)
----------------------------------------------------------------------
Mismatched elements: 486 / 800 (60.8%)
Greatest absolute difference: 0.00012448430061340332 at index (2, 1, 8) (up to 1e-05 allowed)
Greatest relative difference: 0.0016339612193405628 at index (3, 0, 12) (up to 1.3e-06 allowed)

To execute this test, run the following from the base repo dir:
     python test/test_nn.py -k test_cudnn_weight_tying

======================================================================
FAIL: test_partial_flat_weights (__main__.TestNN)
----------------------------------------------------------------------
Mismatched elements: 9 / 36 (25.0%)
Greatest absolute difference: 3.0122697353363037e-05 at index (2, 0, 4) (up to 1e-05 allowed)
Greatest relative difference: 0.003078237408772111 at index (2, 0, 4) (up to 1.3e-06 allowed)

To execute this test, run the following from the base repo dir:
     python test/test_nn.py -k test_partial_flat_weights
```

### Versions

PyTorch version: 2.1.2
Is debug build: False
CUDA used to build PyTorch: 12.0
ROCM used to build PyTorch: N/A

OS: Rocky Linux release 8.7 (Green Obsidian) (x86_64)
GCC version: (GCC) 12.2.0
Clang version: Could not collect
CMake version: version 3.24.3
Libc version: glibc-2.28

Python version: 3.10.8 (main, Dec  7 2023, 16:03:11) [GCC 12.2.0] (64-bit runtime)
Python platform: Linux-4.18.0-425.19.2.el8_7.x86_64-x86_64-with-glibc2.28
Is CUDA available: True
CUDA runtime version: 12.0.76
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA A100-SXM4-40GB
GPU 1: NVIDIA A100-SXM4-40GB
GPU 2: NVIDIA A100-SXM4-40GB
GPU 3: NVIDIA A100-SXM4-40GB
GPU 4: NVIDIA A100-SXM4-40GB
GPU 5: NVIDIA A100-SXM4-40GB
GPU 6: NVIDIA A100-SXM4-40GB
GPU 7: NVIDIA A100-SXM4-40GB

Nvidia driver version: 545.23.08
cuDNN version: Could not collect  (Note: It is  cuDNN/8.8.0.121)
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:                   43 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          96
On-line CPU(s) list:             0-95
Vendor ID:                       AuthenticAMD
Model name:                      AMD EPYC 7352 24-Core Processor
CPU family:                      23
Model:                           49
Thread(s) per core:              2
Core(s) per socket:              24
Socket(s):                       2
Stepping:                        0
Frequency boost:                 enabled
CPU(s) scaling MHz:              78%
CPU max MHz:                     2300.0000
CPU min MHz:                     1500.0000
BogoMIPS:                        4599.85
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 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 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 wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif v_spec_ctrl umip rdpid overflow_recov succor smca sme sev sev_es
Virtualization:                  AMD-V
L1d cache:                       1.5 MiB (48 instances)
L1i cache:                       1.5 MiB (48 instances)
L2 cache:                        24 MiB (48 instances)
L3 cache:                        256 MiB (16 instances)
NUMA node(s):                    4
NUMA node0 CPU(s):               0-11,48-59
NUMA node1 CPU(s):               12-23,60-71
NUMA node2 CPU(s):               24-35,72-83
NUMA node3 CPU(s):               36-47,84-95
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:          Mitigation; untrained return thunk; SMT enabled with STIBP protection
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, 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.24.2
[pip3] torch==2.1.2
[conda] Could not collect


cc @albanD @mruberry @jbschlosser @walterddr @mikaylagawarecki