Repository: pytorch
Issue: Unexpected modification to CPU affinity of Dataloader workers
Labels: module: multiprocessing,triaged,module: openmp

### 🐛 Describe the bug

In certain environments, pytorch (or a dependency) unexpectedly modifies CPU affinity of DataLoader workers. This may result in  training/inference throughput reduction.

Example script: 
```python
import os

from torch.utils.data import DataLoader, Dataset


class TestDataset(Dataset):
    def __init__(self):
        print('__init__', os.getpid(), len(os.sched_getaffinity(0)))

    def __len__(self):
        return 10

    def __getitem__(self, item):
        print('__getitem__', os.getpid(), len(os.sched_getaffinity(0)))
        return item


if __name__ == '__main__':
    print('__main__', os.getpid(), len(os.sched_getaffinity(0)))

    loader = DataLoader(TestDataset(), num_workers=2)
    for _ in loader:
        pass
```

### Environment 1 (expected)
`mamba create -n pytorch2 pytorch pytorch-cuda=11.7 -c pytorch -c nvidia`

Script output:
(numpy warnings removed)
```
__main__ 38262 64
__init__ 38262 64
__getitem__ 38264 64
__getitem__ 38263 64
__getitem__ 38264 64
__getitem__ 38263 64
__getitem__ 38263 64
__getitem__ 38264 64
__getitem__ 38263 64
__getitem__ 38264 64
__getitem__ 38263 64
__getitem__ 38264 64
```




### Environment 2 (unexpected)

`mamba create -n pytorch2-condaforge pytorch pytorch-cuda=11.7 -c pytorch -c nvidia -c conda-forge` 

Script output:
(numpy warnings removed)
```
__main__ 38397 64
__init__ 38397 64
__getitem__ 38399 2
__getitem__ 38399 2
__getitem__ 38398 2
__getitem__ 38398 2
__getitem__ 38398 2
__getitem__ 38399 2
__getitem__ 38399 2
__getitem__ 38398 2
__getitem__ 38398 2
__getitem__ 38399 2
```


### Versions

<details>
<summary>Environment 1</summary>

Collecting environment information...
PyTorch version: 2.0.1
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: CentOS Linux 7 (Core) (x86_64)
GCC version: (GCC) 10.2.1 20210130 (Red Hat 10.2.1-11)
Clang version: Could not collect
CMake version: version 3.26.3
Libc version: glibc-2.17

Python version: 3.11.3 (main, Apr 19 2023, 23:54:32) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-3.10.0-1127.8.2.el7.x86_64-x86_64-with-glibc2.17
Is CUDA available: True
CUDA runtime version: 11.3.109
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: Tesla V100-SXM2-32GB
GPU 1: Tesla V100-SXM2-32GB
GPU 2: Tesla V100-SXM2-32GB
GPU 3: Tesla V100-SXM2-32GB
GPU 4: Tesla V100-SXM2-32GB
GPU 5: Tesla V100-SXM2-32GB
GPU 6: Tesla V100-SXM2-32GB
GPU 7: Tesla V100-SXM2-32GB

Nvidia driver version: 460.73.01
cuDNN version: Probably one of the following:
/usr/lib64/libcudnn.so.8.9.1
/usr/lib64/libcudnn_adv_infer.so.8.9.1
/usr/lib64/libcudnn_adv_train.so.8.9.1
/usr/lib64/libcudnn_cnn_infer.so.8.9.1
/usr/lib64/libcudnn_cnn_train.so.8.9.1
/usr/lib64/libcudnn_ops_infer.so.8.9.1
/usr/lib64/libcudnn_ops_train.so.8.9.1
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
CPU(s):                64
On-line CPU(s) list:   0-63
Thread(s) per core:    2
Core(s) per socket:    16
Socket(s):             2
NUMA node(s):          2
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 79
Model name:            Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz
Stepping:              1
CPU MHz:               2398.779
CPU max MHz:           3600.0000
CPU min MHz:           1200.0000
BogoMIPS:              5200.24
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              40960K
NUMA node0 CPU(s):     0-15,32-47
NUMA node1 CPU(s):     16-31,48-63
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 aperfmperf eagerfpu pni pclmulqdq dtes64 monitor 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 epb cat_l3 cdp_l3 invpcid_single intel_ppin intel_pt ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts spec_ctrl intel_stibp

Versions of relevant libraries:
[pip3] torch==2.0.1
[pip3] triton==2.0.0
[conda] blas                      1.0                         mkl
[conda] mkl                       2023.1.0         h6d00ec8_46342
[conda] pytorch                   2.0.1           py3.11_cuda11.7_cudnn8.5.0_0    pytorch
[conda] pytorch-cuda              11.7                 h778d358_5    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchtriton               2.0.0                     py311    pytorch

</details>

<details>
<summary>Environment 2</summary>

Collecting environment information...
PyTorch version: 2.0.1
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: CentOS Linux 7 (Core) (x86_64)
GCC version: (GCC) 10.2.1 20210130 (Red Hat 10.2.1-11)
Clang version: Could not collect
CMake version: version 3.26.3
Libc version: glibc-2.17

Python version: 3.11.3 | packaged by conda-forge | (main, Apr  6 2023, 08:57:19) [GCC 11.3.0] (64-bit runtime)
Python platform: Linux-3.10.0-1127.8.2.el7.x86_64-x86_64-with-glibc2.17
Is CUDA available: True
CUDA runtime version: 11.3.109
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: Tesla V100-SXM2-32GB
GPU 1: Tesla V100-SXM2-32GB
GPU 2: Tesla V100-SXM2-32GB
GPU 3: Tesla V100-SXM2-32GB
GPU 4: Tesla V100-SXM2-32GB
GPU 5: Tesla V100-SXM2-32GB
GPU 6: Tesla V100-SXM2-32GB
GPU 7: Tesla V100-SXM2-32GB

Nvidia driver version: 460.73.01
cuDNN version: Probably one of the following:
/usr/lib64/libcudnn.so.8.9.1
/usr/lib64/libcudnn_adv_infer.so.8.9.1
/usr/lib64/libcudnn_adv_train.so.8.9.1
/usr/lib64/libcudnn_cnn_infer.so.8.9.1
/usr/lib64/libcudnn_cnn_train.so.8.9.1
/usr/lib64/libcudnn_ops_infer.so.8.9.1
/usr/lib64/libcudnn_ops_train.so.8.9.1
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
CPU(s):                64
On-line CPU(s) list:   0-63
Thread(s) per core:    2
Core(s) per socket:    16
Socket(s):             2
NUMA node(s):          2
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 79
Model name:            Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz
Stepping:              1
CPU MHz:               1200.500
CPU max MHz:           3600.0000
CPU min MHz:           1200.0000
BogoMIPS:              5200.24
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              40960K
NUMA node0 CPU(s):     0-15,32-47
NUMA node1 CPU(s):     16-31,48-63
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 aperfmperf eagerfpu pni pclmulqdq dtes64 monitor 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 epb cat_l3 cdp_l3 invpcid_single intel_ppin intel_pt ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts spec_ctrl intel_stibp

Versions of relevant libraries:
[pip3] torch==2.0.1
[pip3] triton==2.0.0
[conda] blas                      2.116                       mkl    conda-forge
[conda] blas-devel                3.9.0            16_linux64_mkl    conda-forge
[conda] libblas                   3.9.0            16_linux64_mkl    conda-forge
[conda] libcblas                  3.9.0            16_linux64_mkl    conda-forge
[conda] liblapack                 3.9.0            16_linux64_mkl    conda-forge
[conda] liblapacke                3.9.0            16_linux64_mkl    conda-forge
[conda] mkl                       2022.1.0           h84fe81f_915    conda-forge
[conda] mkl-devel                 2022.1.0           ha770c72_916    conda-forge
[conda] mkl-include               2022.1.0           h84fe81f_915    conda-forge
[conda] pytorch                   2.0.1           py3.11_cuda11.7_cudnn8.5.0_0    pytorch
[conda] pytorch-cuda              11.7                 h778d358_5    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchtriton               2.0.0                     py311    pytorch
</details>


cc @SsnL @VitalyFedyunin @ejguan @NivekT @dzhulgakov