Repository: pytorch
Issue: Pin memory in subprocess
Labels: module: dataloader,module: cuda,triaged

### 🚀 The feature, motivation and pitch

Hey!

We are implementing custom data transforms pipeline. It involves subprocesses for parallel data processing. The pipeline works great, passing pytorch tensors with multipricessing queues via shared memory.

However, problems appear when trying to use `pin_memory()` in subprocesses. Unexpectedly, CUDA OOM start to appear. While I do not know the direct cause of such behavior, I suppose that it's due to the pinned memory caching allocator.

Is it possible to pin tensors without using torch's pinned memory caching allocator? Pinning memory in the main process greatly increases data processing time, which is suboptimal as pinning could be done in parallel subprocess. 

### Alternatives

Trivial solution is to pin in the main process, but such solution increases GPU wait time.

Also, generic torch DataLoader is not compatible with our processing pipeline due to data transforms complexity.

### Additional context

```
PyTorch version: 2.2.0
Is debug build: False
CUDA used to build PyTorch: 12.3
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.3 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.22.1
Libc version: glibc-2.35

Python version: 3.11.9 (main, Apr  6 2024, 17:59:24) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-5.4.210-39.1-x86_64-with-glibc2.35
Is CUDA available: True
...

Nvidia driver version: 550.54.14
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.7
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.9.7
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.9.7
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.9.7
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.9.7
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.9.7
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.9.7
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:                   48 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          120
On-line CPU(s) list:             0-119
Vendor ID:                       AuthenticAMD
Model name:                      AMD EPYC 7662 64-Core Processor
CPU family:                      23
Model:                           49
Thread(s) per core:              1
Core(s) per socket:              1
Socket(s):                       120
Stepping:                        0
BogoMIPS:                        3992.43
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy svm cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw perfctr_core ssbd ibrs ibpb stibp vmmcall fsgsbase tsc_adjust bmi1 avx2 smep bmi2 rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 clzero xsaveerptr wbnoinvd arat npt nrip_save umip rdpid arch_capabilities
Virtualization:                  AMD-V
L1d cache:                       7.5 MiB (120 instances)
L1i cache:                       7.5 MiB (120 instances)
L2 cache:                        60 MiB (120 instances)
L3 cache:                        1.9 GiB (120 instances)
NUMA node(s):                    4
NUMA node0 CPU(s):               0-29
NUMA node1 CPU(s):               30-59
NUMA node2 CPU(s):               60-89
NUMA node3 CPU(s):               90-119
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   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 disabled, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected

Versions of relevant libraries:
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.24.3
[pip3] torch==2.2.0
[pip3] torch-tb-profiler==0.4.3
[pip3] triton==2.2.0
```

cc @andrewkho @gokulavasan @SsnL @VitalyFedyunin @dzhulgakov @ptrblck @msaroufim