DataParallel interfering with TorchDispatchMode
Labels: high priority, oncall: distributed, triaged, module: data parallel, module: __torch_dispatch__, oncall: pt2

### 🐛 Describe the bug

TorchDispatchMode doesn't get all the ops that are used when used in conjunction with DDP. 

[Code Sample (Gist)](https://gist.github.com/jimwu6/5509839df956a0d495c02102c6760bf3) which is a slimmed down version of [official PyTorch Code for ResNet](https://github.com/pytorch/examples/blob/main/imagenet/main.py) with an additional `CustomDispatchMode` class being used.

Expected behaviour can be found using `python custom_dispatch.py --gpu 0`, where ops like these will be printed:

```
torch._ops.aten.relu_.default
torch._ops.aten.detach.default
torch._ops.aten.convolution.default
torch._ops.aten.add_.Tensor
torch._ops.aten.cudnn_batch_norm.default
torch._ops.aten.convolution.default
torch._ops.aten.add_.Tensor
torch._ops.aten.cudnn_batch_norm.default
torch._ops.aten.add_.Tensor
```

The behaviour seen is that when running using `python custom_dispatch.py`, which uses DDP (if you have more than one GPU visible/on the system, which is needed to reproduce - I've seen that only a multiple GPU system with `CUDA_VISIBLE_DEVICES=0` will not trigger this error), the output doesn't contain convolution or add, but instead contains mostly ops such as

```
torch._ops.aten.slice.Tensor
torch._ops.aten.view.default
torch._ops.aten.detach.default
```

where we would expect the output to contain items from the first code block.

I've been able to reproduce this on the latest PyTorch nightly (`2.1.0.dev20230615+cu121`) and a built-from-source version of `2.0.0`.


### Versions

```
Collecting environment information...
PyTorch version: 2.1.0.dev20230615+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.5 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.31

Python version: 3.8.16 | packaged by conda-forge | (default, Feb  1 2023, 16:01:55)  [GCC 11.3.0] (64-bit runtime)
Python platform: Linux-5.4.0-64-generic-x86_64-with-glibc2.10
Is CUDA available: True
CUDA runtime version: 12.0.140
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: NVIDIA A100-SXM-80GB
GPU 1: NVIDIA A100-SXM-80GB
GPU 2: NVIDIA A100-SXM-80GB
GPU 3: NVIDIA A100-SXM-80GB
GPU 4: NVIDIA A100-SXM-80GB
GPU 5: NVIDIA A100-SXM-80GB
GPU 6: NVIDIA A100-SXM-80GB
GPU 7: NVIDIA A100-SXM-80GB

Nvidia driver version: 470.57.02
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
Byte Order:                      Little Endian
Address sizes:                   48 bits physical, 48 bits virtual
CPU(s):                          128
On-line CPU(s) list:             0-127
Thread(s) per core:              1
Core(s) per socket:              64
Socket(s):                       2
NUMA node(s):                    8
Vendor ID:                       AuthenticAMD
CPU family:                      25
Model:                           1
Model name:                      AMD EPYC 7763 64-Core Processor
Stepping:                        1
Frequency boost:                 enabled
CPU MHz:                         3250.104
CPU max MHz:                     2450.0000
CPU min MHz:                     1500.0000
BogoMIPS:                        4900.16
Virtualization:                  AMD-V
L1d cache:                       4 MiB
L1i cache:                       4 MiB
L2 cache:                        64 MiB
L3 cache:                        512 MiB
NUMA node0 CPU(s):               0-15
NUMA node1 CPU(s):               16-31
NUMA node2 CPU(s):               32-47
NUMA node3 CPU(s):               48-63
NUMA node4 CPU(s):               64-79
NUMA node5 CPU(s):               80-95
NUMA node6 CPU(s):               96-111
NUMA node7 CPU(s):               112-127
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Full AMD retpoline, IBPB conditional, IBRS_FW, STIBP disabled, RSB filling
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected
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 pcid sse4_1 sse4_2 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 invpcid_single hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 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 wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold v_vmsave_vmload vgif umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor smca

Versions of relevant libraries:
[pip3] numpy==1.24.3
[pip3] pytorch-triton==2.1.0+440fd1bf20
[pip3] torch==2.1.0.dev20230615+cu121
[pip3] torchaudio==2.1.0.dev20230615+cu121
[pip3] torchvision==0.16.0.dev20230615+cu121
[conda] numpy                     1.24.3                   pypi_0    pypi
[conda] torchvision               0.16.0.dev20230615+cu121          pypi_0    pypi
```

cc @ezyang @gchanan @zou3519 @mrshenli @pritamdamania87 @zhaojuanmao @satgera @rohan-varma @gqchen @aazzolini @osalpekar @jiayisuse @H-Huang @kwen2501 @awgu @penguinwu @Chillee @albanD @samdow @msaroufim @wconstab @bdhirsh @anijain2305