Repository: pytorch
Issue: Observed regress in DataLoader spawn from PyTorch1.13 to PyTorch2.0
Labels: high priority,module: performance,triaged,module: regression,module: data

## Issue description

Observed a performance regression between PyTorch 1.13.1 and PyTorch 2.0.0 when using DataLoader to spawn processes.

With the example code below, I have observed 37.5% performance regress.

## Code example

```python
import time

from torch.utils.data import Dataset, IterableDataset, DataLoader
from torchvision.datasets import MNIST


def one_experiment(data_train):
    dataloader = DataLoader(
        dataset = data_train,
        batch_size=2,
        num_workers=8,
        drop_last=True,
        multiprocessing_context="spawn"
    )
    start_time = time.perf_counter()
    dataloader._get_iterator()
    end_time = time.perf_counter()
    elapsed_time = end_time - start_time
    print("DataLoader Create Process Time = ", elapsed_time, " seconds")
    return elapsed_time


def main() -> None:
    num_tries = 5
    total_time = 0
    results = []
    data_train = MNIST('/tmp/mnist_data', train=True, download=True)
    for _ in range(num_tries):
        elapsed_time = one_experiment(data_train)
        results.append(elapsed_time)
    total_time = sum(results)
    averaged_time = total_time / float(num_tries)
    print("Number of tries = ", num_tries)
    print("Total time = ", total_time)
    print("Averaged time = ", averaged_time)
    print("Individual time = ", results)


if __name__ == "__main__":
    main()
```

If I run the above binary with PyTorch1.13, the averaged time of spawning the processes is about 6 seconds. On the other hand, if I run the same code with PyTorch2.0, the averaged time of spawning the processes becomes 9 seconds.

## System Info

Collecting environment information...
PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.6 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.10 (default, Mar 13 2023, 10:26:41)  [GCC 9.4.0] (64-bit runtime)
Python platform: Linux-5.15.0-1035-gcp-x86_64-with-glibc2.29
Is CUDA available: N/A
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: N/A

CPU:
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   46 bits physical, 48 bits virtual
CPU(s):                          8
On-line CPU(s) list:             0-7
Thread(s) per core:              2
Core(s) per socket:              4
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           63
Model name:                      Intel(R) Xeon(R) CPU @ 2.30GHz
Stepping:                        0
CPU MHz:                         2299.998
BogoMIPS:                        4599.99
Virtualization:                  VT-x
Hypervisor vendor:               KVM
Virtualization type:             full
L1d cache:                       128 KiB
L1i cache:                       128 KiB
L2 cache:                        1 MiB
L3 cache:                        45 MiB
NUMA node0 CPU(s):               0-7
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Mitigation; PTE Inversion; VMX flush not necessary, SMT vulnerable
Vulnerability Mds:               Mitigation; Clear CPU buffers; SMT Host state unknown
Vulnerability Meltdown:          Mitigation; PTI
Vulnerability Mmio stale data:   Vulnerable: Clear CPU buffers attempted, no microcode; SMT Host state unknown
Vulnerability Retbleed:          Mitigation; IBRS
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; IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS Not affected
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 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt arat md_clear arch_capabilities

Versions of relevant libraries:
[pip3] No relevant packages
[conda] Could not collect


- PyTorch or Caffe2:
  - PyTorch
- How you installed PyTorch (conda, pip, source):
  - Source
- Build command you used (if compiling from source):
  - Internal build system 
- PyTorch version: 
  - 1.13 and 2.0


cc @ezyang @gchanan @zou3519 @VitalyFedyunin @ejguan @dzhulgakov