Repository: pytorch
Issue: Torch Filename Storage hangs on "file_system" sharing strategy after in-place fill
Labels: module: multiprocessing,triaged,module: mps

### 🐛 Describe the bug

On MacOS M-series machines (AND linux machines using 'file_system' sharing) if you use an in-place fill operation on a Linear layer before launching torch multi-processing, then shared filename storage will fail to allocate on sufficiently large batches of data. The problem also appears if you move a normal tensor into shared memory on the host. This is a problem because the torch DataLoader relies on torch Queues which in turn rely on the ForkingPickler which in turn relies on shared filename storage.

```python
import torch
import torch.nn as nn
from torch.multiprocessing import Process, set_start_method, set_sharing_strategy


def _do_pickle(batch_size):
    print(f"starting batch size: {batch_size}")
    x = torch.ones((batch_size, 28, 28))
    storage = x._typed_storage()
    storage._share_filename_cpu_()


if __name__ == "__main__":
    set_start_method("fork", force=True)
    set_sharing_strategy('file_system')  # This is the default/only option on Mac, but this problem also exists for linux when using this file sharing strategy

    t = nn.Linear(28 * 28, 300)
    t.weight.data.fill_(0.01)  # If you comment this out then both batch sizes succeed

    # An alternative way to reproduce this error:
    # t = torch.ones((128, 28, 28))  # Smaller tensors don't freeze, not sure if this is the exact limit or not
    # t.share_memory_()

    w = Process(target=_do_pickle, args=(1, ))
    w.daemon = True
    w.start()
    w.join()
    print("batch size 1 succeeded")

    w = Process(target=_do_pickle, args=(128, ))
    w.daemon = True
    w.start()
    w.join()
    print("batch size 128 succeeded")
```

The expected output here would be to see:
```
starting batch size: 1
batch size 1 succeeded
starting batch size: 128
batch size 128 succeeded
```

The actual output is as follows, with the program then hanging forever rather than exiting:
```
starting batch size: 1
batch size 1 succeeded
starting batch size: 128
```


### Versions

**MacOS**

Collecting environment information...
PyTorch version: 2.0.1
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 13.3 (arm64)
GCC version: Could not collect
Clang version: 14.0.3 (clang-1403.0.22.14.1)
CMake version: Could not collect
Libc version: N/A

Python version: 3.8.16 | packaged by conda-forge | (default, Feb  1 2023, 16:01:13)  [Clang 14.0.6 ] (64-bit runtime)
Python platform: macOS-13.3-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

CPU:
Apple M2 Max

Versions of relevant libraries:
[pip3] numpy==1.24.2
[pip3] torch==2.0.1
[pip3] torchaudio==2.0.2
[pip3] torchinfo==1.7.2
[pip3] torchview==0.2.6
[pip3] torchvision==0.15.2
[conda] numpy                     1.24.2                   pypi_0    pypi
[conda] torch                     2.0.1                    pypi_0    pypi
[conda] torchaudio                2.0.2                    pypi_0    pypi
[conda] torchinfo                 1.7.2                    pypi_0    pypi
[conda] torchview                 0.2.6                    pypi_0    pypi
[conda] torchvision               0.15.2                   pypi_0    pypi

---

**Linux**

Collecting environment information...
PyTorch version: 2.0.0+cu118
Is debug build: False
CUDA used to build PyTorch: 11.8
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: version 3.26.1
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.4.0-148-generic-x86_64-with-glibc2.29
Is CUDA available: True
CUDA runtime version: 11.8.89
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: Tesla V100-DGXS-16GB
Nvidia driver version: 470.182.03
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.6.0
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:                   46 bits physical, 48 bits virtual
CPU(s):                          40
On-line CPU(s) list:             0-39
Thread(s) per core:              2
Core(s) per socket:              20
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           79
Model name:                      Intel(R) Xeon(R) CPU E5-2698 v4 @ 2.20GHz
Stepping:                        1
CPU MHz:                         1199.199
CPU max MHz:                     3600.0000
CPU min MHz:                     1200.0000
BogoMIPS:                        4397.07
Virtualization:                  VT-x
L1d cache:                       640 KiB
L1i cache:                       640 KiB
L2 cache:                        5 MiB
L3 cache:                        50 MiB
NUMA node0 CPU(s):               0-39
Vulnerability Itlb multihit:     KVM: Vulnerable
Vulnerability L1tf:              Mitigation; PTE Inversion
Vulnerability Mds:               Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown:          Mitigation; PTI
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed:          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; Retpolines, IBPB conditional, IBRS_FW, STIBP conditional, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Mitigation; Clear CPU buffers; SMT vulnerable
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single pti intel_ppin ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap intel_pt xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts md_clear flush_l1d

Versions of relevant libraries:
[pip3] numpy==1.24.2
[pip3] torch==2.0.0+cu118
[pip3] torchaudio==2.0.1+cu118
[pip3] torchvision==0.15.1+cu118
[pip3] triton==2.0.0
[conda] Could not collect

---

cc @VitalyFedyunin @kulinseth @albanD @malfet @DenisVieriu97 @razarmehr @abhudev