Repository: pytorch
Issue: Multiprocessing DataLoader pickles multiprocessing.Queues incorrectly
Labels: module: dataloader,triaged

### 🐛 Describe the bug

If a `DataPipe` instance passed to a `DataLoader` contains a member of `multiprocessing.Queue` when `num_workers>0`, the `Queue` object is not correctly "deserialized" in worker processes, when the start method for sub-processes is "spawn" (the default on MacOS).

The example below works under Linux, but not MacOS.
```
import torchdata
from multiprocessing import Queue
import itertools
import torch

# Lead (worker 0) gives out data to followers. Lame, but to show that workers can 
# communicate.
class LeadFollowerDataPipe(torchdata.datapipes.iter.IterDataPipe):
  def __init__(self, group_size):
    self._group_size = group_size
    self._queues = [Queue() for _ in range(self._group_size)]

  def __iter__(self):
    worker_id = torch.utils.data.get_worker_info().id
    assert worker_id < self._group_size
    if worker_id != 0:
      q = self._queues[worker_id]
      while True:
        yield worker_id, q.get()
    else:
      for i in itertools.count(0):
        dst_worker = i % self._group_size
        if dst_worker == 0:
          yield 0, i
        self._queues[dst_worker].put(i)

if __name__ == '__main__':
  dp = LeadFollowerDataPipe(3)
  dl = torch.utils.data.DataLoader(dp, num_workers=3)
  for v, _ in zip(dl, range(100)):
    print(v)
```

On MacOS, the following occurs:

```
Original Traceback (most recent call last):
  File "/lib/python3.8/site-packages/torch/utils/data/datapipes/_typing.py", line 514, in wrap_generator
    response = gen.send(None)
  File "lead_follower.py", line 22, in __iter__
    yield worker_id, q.get()
  File "/opt/homebrew/Cellar/python@3.8/3.8.13_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/queues.py", line 97, in get
    res = self._recv_bytes()
  File "/opt/homebrew/Cellar/python@3.8/3.8.13_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/connection.py", line 216, in recv_bytes
    buf = self._recv_bytes(maxlength)
  File "/opt/homebrew/Cellar/python@3.8/3.8.13_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/connection.py", line 414, in _recv_bytes
    buf = self._recv(4)
  File "/opt/homebrew/Cellar/python@3.8/3.8.13_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/multiprocessing/connection.py", line 379, in _recv
    chunk = read(handle, remaining)
OSError: [Errno 9] Bad file descriptor
```

This is because Python's multiprocessing pickles the arguments to the multiprocessing target function, and it has special treatment when pickling sockets (thus, multiprocessing.Queues) [1]. However, DataPipes are wrapped in _DataPipeSerializationWrapper [2]. When pickling the wrapper, the default pickler is always used and that prevents the aforementioned special pickler from doing its job.

Removing these two lines line [3] makes the above code work under MacOS.

[1] https://github.com/python/cpython/blob/f4c03484da59049eb62a9bf7777b963e2267d187/Lib/multiprocessing/reduction.py#L33
[2] https://github.com/pytorch/pytorch/blob/1a7409c77199403153f1260e2281bae2f76745f6/torch/utils/data/datapipes/datapipe.py#L317
[3] https://github.com/pytorch/pytorch/blob/d29c8c0ffa68f11790fc2e9fd78778bb8e9bc281/torch/utils/data/dataloader.py#L259

### Versions

```
PyTorch version: 1.12.1
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 12.6 (arm64)
GCC version: Could not collect
Clang version: 14.0.0 (clang-1400.0.29.102)
CMake version: Could not collect
Libc version: N/A

Python version: 3.8.13 (default, May  8 2022, 17:48:02)  [Clang 13.1.6 (clang-1316.0.21.2)] (64-bit runtime)
Python platform: macOS-12.6-arm64-arm-64bit
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

Versions of relevant libraries:
[pip3] numpy==1.22.4
[pip3] torch==1.12.1
[pip3] torchdata==0.4.1
[conda] No relevant packages
```

cc @SsnL @VitalyFedyunin @ejguan @NivekT