Repository: pytorch
Issue: If large enough tensor is being cloned, parallel dataloading hangs on M1 Mac
Labels: high priority,module: dataloader,triaged,module: macos,module: deadlock,module: arm

## Issue description
This is really strange issue. I drilled it down to the smallest reproducible example possible.

The dataloader hangs if:
- num_workers > 0
- large enough batches are being produced from a dataloader (see code)
- the large enough (smaller tensors like (100, 100) work ok) is being cloned some time during execution of a program; interestingly it may be any tensor, even the one not related to dataloading (see code)
- the program is being run on M1 Mac (it works ok on x86 machines)

## Code example

```
import torch
import itertools

def _samples_to_XY(samples):
    X = [x for x, _ in samples]
    X = torch.tensor(X)
    Y = torch.tensor([y for _, y in samples])
    return X, Y

def _gen(tensor_size):
    while True:
        yield (list(range(tensor_size)), 1)

def _gen_batch(tensor_size, batch_size):
    gen = _gen(tensor_size)
    while True:
        xys = list(itertools.islice(gen, batch_size))
        if not xys:
            break
        yield _samples_to_XY(xys)

class TheDataset(torch.utils.data.IterableDataset):
    def __init__(self, tensor_size, batch_size):
        self.batch_size = batch_size
        self.tensor_size = tensor_size
    def __iter__(self):
        return _gen_batch(self.tensor_size, self.batch_size)
    def into_dataloader(self, num_workers):
        mproc_config = {'multiprocessing_context': 'fork'}
        mproc_config = mproc_config if num_workers > 0 else {}
        return torch.utils.data.DataLoader(self,
                                           num_workers=num_workers,
                                           **mproc_config)

if __name__ == '__main__':

    some_tensor = torch.rand(10000, 50)
    
    # if this statement is commented, none of the code below hangs
    some_tensor = some_tensor.clone()

    # works ok
    if next(iter(TheDataset(tensor_size=64, batch_size=512).into_dataloader(num_workers=1))):
        print('ok')

    # works ok
    if next(iter(TheDataset(tensor_size=128, batch_size=256).into_dataloader(num_workers=1))):
        print('ok')

    # hangs
    if next(iter(TheDataset(tensor_size=128, batch_size=512).into_dataloader(num_workers=1))):
        print('ok')
```

Console output:
```
[W ParallelNative.cpp:229] Warning: Cannot set number of intraop threads after parallel work has started or after set_num_threads call when using native parallel backend (function set_num_threads)
ok
[W ParallelNative.cpp:229] Warning: Cannot set number of intraop threads after parallel work has started or after set_num_threads call when using native parallel backend (function set_num_threads)
ok
[W ParallelNative.cpp:229] Warning: Cannot set number of intraop threads after parallel work has started or after set_num_threads call when using native parallel backend (function set_num_threads)
```

Console output with clone statement commented out:
```
ok
ok
ok
```

## System Info
```
PyTorch version: 1.11.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 12.3.1 (arm64)
GCC version: Could not collect
Clang version: 13.1.6 (clang-1316.0.21.2.3)
CMake version: Could not collect
Libc version: N/A

Python version: 3.9.12 (main, Jun  1 2022, 06:34:44)  [Clang 12.0.0 ] (64-bit runtime)
Python platform: macOS-12.3.1-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
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.11.0
[pip3] torchvision==0.12.0
[conda] numpy                     1.22.4                   pypi_0    pypi
[conda] torch                     1.11.0                   pypi_0    pypi
[conda] torchvision               0.12.0                   pypi_0    pypi
```

cc @ezyang @gchanan @zou3519 @SsnL @VitalyFedyunin @ejguan @NivekT @malfet @albanD @kulinseth