Repository: pytorch
Issue: Deep-copied model cannot be sent through multiprocessing `Pipe`
Labels: module: multiprocessing,triaged

### 🐛 Describe the bug

If we create a not-too-small model, clone it by `copy.deepcopy`, and send the clone to a new process, then the cloned model cannot be sent back through `torch.multiprocessing.Pipe` connection (it will block forever):

```python
import time
from copy import deepcopy

import torch
# Replacing torch.multiprocessing with multiprocessing will NOT solve the problem
import torch.multiprocessing as mp


def process_target(sender, model):
    print('Start sending')
    sender.send(model)  # Bug: will block forever
    print('Finish sending')
    time.sleep(1.0)  # Necessary to avoid multiprocessing errors


def main():

    # Either removing deepcopy or replacing 1024 with 32 will solve the problem
    model = deepcopy(torch.nn.Linear(1024, 1024))

    receiver, sender = mp.Pipe(duplex=False)

    # Replacing mp.Process with mp.get_context('spawn').Process will solve the problem
    process = mp.Process(target=process_target, args=(sender, model))

    process.start()
    print(receiver.recv())
    process.join()


if __name__ == '__main__':
    main()
```

The above snippet will output:

```
Start sending
```

The expected output:

```
Start sending
Finish sending
Linear(in_features=1024, out_features=1024, bias=True)
```

### Versions

Collecting environment information...
PyTorch version: 2.2.1+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.4 LTS (x86_64)
GCC version: Could not collect
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.35

Python version: 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-6.5.0-25-generic-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
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:                      45 bits physical, 48 bits virtual
Byte Order:                         Little Endian
CPU(s):                             2
On-line CPU(s) list:                0,1
Vendor ID:                          AuthenticAMD
Model name:                         AMD Ryzen 9 5900HX with Radeon Graphics
CPU family:                         25
Model:                              80
Thread(s) per core:                 1
Core(s) per socket:                 2
Socket(s):                          1
Stepping:                           0
BogoMIPS:                           6587.63
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 tsc_reliable nonstop_tsc cpuid extd_apicid tsc_known_freq pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw topoext ibpb vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves clzero wbnoinvd arat umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor fsrm
Hypervisor vendor:                  VMware
Virtualization type:                full
L1d cache:                          64 KiB (2 instances)
L1i cache:                          64 KiB (2 instances)
L2 cache:                           1 MiB (2 instances)
L3 cache:                           16 MiB (1 instance)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0,1
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:    Vulnerable
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Retpolines, IBPB conditional, STIBP disabled, 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.2.1
[pip3] triton==2.2.0
[conda] Could not collect

(Note by author: the output above was from a VMWare virtual machine, but the same bug has been reproduced in another Ubuntu physical machine.)

cc @VitalyFedyunin