importing of transformers 4.29.2 slows down PyToch DataLoader's multi-processing significantly
Author: TYTTYTTYT
Pull request: no

### System Info

- `transformers` version: 4.29.2
- Platform: Linux-5.15.0-70-generic-x86_64-with-glibc2.35
- Python version: 3.10.11
- Huggingface_hub version: 0.14.1
- Safetensors version: not installed
- PyTorch version (GPU?): 1.13.1 (True)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: <no> no
- Using distributed or parallel set-up in script?: <yes> yes



### Who can help?

_No response_

### Information

- [ ] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

The issue is firstly report to ```PyTorch```, then I found it's caused by ```transformers``` [Original Issue](https://github.com/pytorch/pytorch/issues/102494)

The codes below take 23.6 seconds with only 2 CPU cores fully used, even though I didn't really use the transformers.

``` python
import transformers    # imported but not used

import torch
import torchvision.datasets as datasets
import torchvision.transforms as transforms

trans = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])

dataset = datasets.FakeData(size=10000, transform=trans)


loader = torch.utils.data.DataLoader(
    dataset, batch_size=128, shuffle=True,
    num_workers=12, sampler=None)

i = 0
for d in loader:
    print("Batch {}".format(i))
    i += 1
# takes 23.6 seconds
```

And by importing ```torch``` before ```transformers```, the CPU is fully used and only takes 5.4 seconds.

``` python
import torch
import torchvision.datasets as datasets
import torchvision.transforms as transforms

import transformers

trans = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])

dataset = datasets.FakeData(size=10000, transform=trans)


loader = torch.utils.data.DataLoader(
    dataset, batch_size=128, shuffle=True,
    num_workers=12, sampler=None)

i = 0
for d in loader:
    print("Batch {}".format(i))
    i += 1
# take only 5.4 seconds
```

### Expected behavior

The aforementioned issue happens to ```transformers 4.29.2```. I tested 4.26.1 as well and it works fine.

I expect the multi-processing DataLoader can fully use my CPU so the data processing could be faster.

Comments:
Both take the same time on my side, so it's not just Transformers but some external library causing the problem. Could you share your full env?

> Both take the same time on my side, so it's not just Transformers but some external library causing the problem. Could you share your full env?

Thanks for your reply! Here is the env generated by Pytorch env script:

```
PyTorch version: 2.0.1
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.2 LTS (x86_64)
GCC version: (Ubuntu 11.3.0-1ubuntu1~22.04.1) 11.3.0
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.35

Python version: 3.10.11 (main, Apr 20 2023, 19:02:41) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.0-70-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3090 Ti
Nvidia driver version: 515.65.01
cuDNN version: Could not collect
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:                   43 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          16
On-line CPU(s) list:             0-15
Vendor ID:                       AuthenticAMD
Model name:                      AMD Ryzen 7 3700X 8-Core Processor
CPU family:                      23
Model:                           113
Thread(s) per core:              2
Core(s) per socket:              8
Socket(s):                       1
Stepping:                        0
Frequency boost:                 enabled
CPU max MHz:                     3600.0000
CPU min MHz:                     2200.0000
BogoMIPS:                        7199.26
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr rdpru wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif v_spec_ctrl umip rdpid overflow_recov succor smca sme sev sev_es
Virtualisation:                  AMD-V
L1d cache:                       256 KiB (8 instances)
L1i cache:                       256 KiB (8 instances)
L2 cache:                        4 MiB (8 instances)
L3 cache:                        32 MiB (2 instances)
NUMA node(s):                    1
NUMA node0 CPU(s):               0-15
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Not affected
Vulnerability Retbleed:          Mitigation; untrained return thunk; SMT enabled with STIBP protection
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, STIBP always-on, 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.0.1
[pip3] torchaudio==2.0.2
[pip3] torchvision==0.15.2
[pip3] triton==2.0.0
[conda] blas                      1.0                         mkl  
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] mkl                       2023.1.0         h6d00ec8_46342  
[conda] mkl-service               2.4.0           py310h5eee18b_1  
[conda] mkl_fft                   1.3.6           py310h1128e8f_1  
[conda] mkl_random                1.2.2           py310h1128e8f_1  
[conda] numpy                     1.24.3          py310h5f9d8c6_1  
[conda] numpy-base                1.24.3          py310hb5e798b_1  
[conda] pytorch                   2.0.1           py3.10_cuda11.7_cudnn8.5.0_0    pytorch
[conda] pytorch-cuda              11.7                 h778d358_5    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchaudio                2.0.2               py310_cu117    pytorch
[conda] torchtriton               2.0.0                     py310    pytorch
[conda] torchvision               0.15.2              py310_cu117    pytorch
```
 
Here is my conda environment:

```
name: pt2hfpy310
channels:
  - pytorch
  - huggingface
  - nvidia
  - conda-forge
  - defaults
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - abseil-cpp=20211102.0=h27087fc_1
  - aiosignal=1.3.1=pyhd8ed1ab_0
  - anyio=3.5.0=py310h06a4308_0
  - argon2-cffi=21.3.0=pyhd3eb1b0_0
  - argon2-cffi-bindings=21.2.0=py310h7f8727e_0
  - arrow-cpp=11.0.0=py310h7516544_0
  - asttokens=2.0.5=pyhd3eb1b0_0
  - async-timeout=4.0.2=pyhd8ed1ab_0
  - attrs=23.1.0=pyh71513ae_1
  - aws-c-common=0.4.57=he6710b0_1
  - aws-c-event-stream=0.1.6=h2531618_5
  - aws-checksums=0.1.9=he6710b0_0
  - aws-sdk-cpp=1.8.185=hce553d0_0
  - babel=2.11.0=py310h06a4308_0
  - backcall=0.2.0=pyhd3eb1b0_0
  - beautifulsoup4=4.12.2=py310h06a4308_0
  - blas=1.0=mkl
  - bleach=4.1.0=pyhd3eb1b0_0
  - boost-cpp=1.65.1=0
  - bottleneck=1.3.5=py310ha9d4c09_0
  - brotli=1.0.9=he6710b0_2
  - brotlipy=0.7.0=py310h7f8727e_1002
  - bzip2=1.0.8=h7b6447c_0
  - c-ares=1.19.0=h5eee18b_0
  - ca-certificates=2023.01.10=h06a4308_0
  - certifi=2023.5.7=py310h06a4308_0
  - cffi=1.15.1=py310h5eee18b_3
  - charset-normalizer=2.0.4=pyhd3eb1b0_0
  - click=8.0.4=py310h06a4308_0
  - comm=0.1.2=py310h06a4308_0
  - contourpy=1.0.5=py310hdb19cb5_0
  - cryptography=39.0.1=py310h9ce1e76_0
  - cuda-cudart=11.7.99=0
  - cuda-cupti=11.7.101=0
  - cuda-libraries=11.7.1=0
  - cuda-nvrtc=11.7.99=0
  - cuda-nvtx=11.7.91=0
  - cuda-runtime=11.7.1=0
  - cycler=0.11.0=pyhd3eb1b0_0
  - dataclasses=0.8=pyh6d0b6a4_7
  - datasets=2.12.0=py_0
  - dbus=1.13.18=hb2f20db_0
  - debugpy=1.5.1=py310h295c915_0
  - decorator=5.1.1=pyhd3eb1b0_0
  - defusedxml=0.7.1=pyhd3eb1b0_0
  - dill=0.3.6=pyhd8ed1ab_1
  - entrypoints=0.4=py310h06a4308_0
  - executing=0.8.3=pyhd3eb1b0_0
  - expat=2.4.9=h6a678d5_0
  - ffmpeg=4.3=hf484d3e_0
  - filelock=3.9.0=py310h06a4308_0
  - fontconfig=2.14.1=h52c9d5c_1
  - fonttools=4.25.0=pyhd3eb1b0_0
  - freetype=2.12.1=h4a9f257_0
  - frozenlist=1.3.3=py310h5eee18b_0
  - fsspec=2023.5.0=pyh1a96a4e_0
  - gflags=2.2.2=he1b5a44_1004
  - giflib=5.2.1=h5eee18b_3
  - glib=2.69.1=he621ea3_2
  - glog=0.5.0=h48cff8f_0
  - gmp=6.2.1=h295c915_3
  - gmpy2=2.1.2=py310heeb90bb_0
  - gnutls=3.6.15=he1e5248_0
  - grpc-cpp=1.46.1=h33aed49_1
  - gst-plugins-base=1.14.1=h6a678d5_1
  - gstreamer=1.14.1=h5eee18b_1
  - huggingface_hub=0.14.1=py_0
  - icu=58.2=hf484d3e_1000
  - idna=3.4=py310h06a4308_0
  - importlib-metadata=6.0.0=py310h06a4308_0
  - importlib_metadata=6.0.0=hd3eb1b0_0
  - intel-openmp=2023.1.0=hdb19cb5_46305
  - ipykernel=6.19.2=py310h2f386ee_0
  - ipython=8.12.0=py310h06a4308_0
  - ipython_genutils=0.2.0=pyhd3eb1b0_1
  - ipywidgets=8.0.4=py310h06a4308_0
  - jedi=0.18.1=py310h06a4308_1
  - jinja2=3.1.2=py310h06a4308_0
  - joblib=1.1.1=py310h06a4308_0
  - jpeg=9e=h5eee18b_1
  - json5=0.9.6=pyhd3eb1b0_0
  - jsonschema=4.17.3=py310h06a4308_0
  - jupyter=1.0.0=py310h06a4308_8
  - jupyter_client=8.1.0=py310h06a4308_0
  - jupyter_console=6.6.3=py310h06a4308_0
  - jupyter_core=5.3.0=py310h06a4308_0
  - jupyter_server=1.23.4=py310h06a4308_0
  - jupyterlab=3.5.3=py310h06a4308_0
  - jupyterlab_pygments=0.1.2=py_0
  - jupyterlab_server=2.22.0=py310h06a4308_0
  - jupyterlab_widgets=3.0.5=py310h06a4308_0
  - keyutils=1.6.1=h166bdaf_0
  - kiwisolver=1.4.4=py310h6a678d5_0
  - krb5=1.19.3=h3790be6_0
  - lame=3.100=h7b6447c_0
  - lcms2=2.12=h3be6417_0
  - ld_impl_linux-64=2.38=h1181459_1
  - lerc=3.0=h295c915_0
  - libbrotlicommon=1.0.9=h166bdaf_7
  - libbrotlidec=1.0.9=h166bdaf_7
  - libbrotlienc=1.0.9=h166bdaf_7
  - libclang=10.0.1=default_hb85057a_2
  - libcublas=11.10.3.66=0
  - libcufft=10.7.2.124=h4fbf590_0
  - libcufile=1.6.1.9=0
  - libcurand=10.3.2.106=0
  - libcurl=7.87.0=h91b91d3_0
  - libcusolver=11.4.0.1=0
  - libcusparse=11.7.4.91=0
  - libdeflate=1.17=h5eee18b_0
  - libedit=3.1.20191231=he28a2e2_2
  - libev=4.33=h516909a_1
  - libevent=2.1.12=h8f2d780_0
  - libffi=3.4.4=h6a678d5_0
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libiconv=1.16=h7f8727e_2
  - libidn2=2.3.4=h5eee18b_0
  - libllvm10=10.0.1=hbcb73fb_5
  - libnghttp2=1.46.0=hce63b2e_0
  - libnpp=11.7.4.75=0
  - libnvjpeg=11.8.0.2=0
  - libpng=1.6.39=h5eee18b_0
  - libpq=12.9=h16c4e8d_3
  - libprotobuf=3.20.3=he621ea3_0
  - libsodium=1.0.18=h7b6447c_0
  - libssh2=1.10.0=ha56f1ee_2
  - libstdcxx-ng=11.2.0=h1234567_1
  - libtasn1=4.19.0=h5eee18b_0
  - libthrift=0.15.0=hcc01f38_0
  - libtiff=4.5.0=h6a678d5_2
  - libunistring=0.9.10=h27cfd23_0
  - libuuid=1.41.5=h5eee18b_0
  - libwebp=1.2.4=h11a3e52_1
  - libwebp-base=1.2.4=h5eee18b_1
  - libxcb=1.15=h7f8727e_0
  - libxkbcommon=1.0.1=hfa300c1_0
  - libxml2=2.9.14=h74e7548_0
  - libxslt=1.1.35=h4e12654_0
  - lxml=4.9.1=py310h1edc446_0
  - lz4-c=1.9.4=h6a678d5_0
  - markupsafe=2.1.1=py310h7f8727e_0
  - matplotlib=3.7.1=py310h06a4308_1
  - matplotlib-base=3.7.1=py310h1128e8f_1
  - matplotlib-inline=0.1.6=py310h06a4308_0
  - mistune=0.8.4=py310h7f8727e_1000
  - mkl=2023.1.0=h6d00ec8_46342
  - mkl-service=2.4.0=py310h5eee18b_1
  - mkl_fft=1.3.6=py310h1128e8f_1
  - mkl_random=1.2.2=py310h1128e8f_1
  - mpc=1.1.0=h10f8cd9_1
  - mpfr=4.0.2=hb69a4c5_1
  - multidict=6.0.2=py310h5eee18b_0
  - multiprocess=0.70.14=py310h06a4308_0
  - munkres=1.1.4=py_0
  - nbclassic=0.5.5=py310h06a4308_0
  - nbclient=0.5.13=py310h06a4308_0
  - nbconvert=6.5.4=py310h06a4308_0
  - nbformat=5.7.0=py310h06a4308_0
  - ncurses=6.4=h6a678d5_0
  - nest-asyncio=1.5.6=py310h06a4308_0
  - nettle=3.7.3=hbbd107a_1
  - networkx=2.8.4=py310h06a4308_1
  - notebook=6.5.4=py310h06a4308_0
  - notebook-shim=0.2.2=py310h06a4308_0
  - nspr=4.33=h295c915_0
  - nss=3.74=h0370c37_0
  - numexpr=2.8.4=py310h85018f9_1
  - numpy=1.24.3=py310h5f9d8c6_1
  - numpy-base=1.24.3=py310hb5e798b_1
  - openh264=2.1.1=h4ff587b_0
  - openssl=1.1.1t=h7f8727e_0
  - orc=1.7.4=hb3bc3d3_1
  - packaging=23.0=py310h06a4308_0
  - pandas=1.5.3=py310h1128e8f_0
  - pandocfilters=1.5.0=pyhd3eb1b0_0
  - parso=0.8.3=pyhd3eb1b0_0
  - pcre=8.45=h295c915_0
  - pexpect=4.8.0=pyhd3eb1b0_3
  - pickleshare=0.7.5=pyhd3eb1b0_1003
  - pillow=9.4.0=py310h6a678d5_0
  - pip=23.0.1=py310h06a4308_0
  - platformdirs=2.5.2=py310h06a4308_0
  - ply=3.11=py310h06a4308_0
  - prometheus_client=0.14.1=py310h06a4308_0
  - prompt-toolkit=3.0.36=py310h06a4308_0
  - prompt_toolkit=3.0.36=hd3eb1b0_0
  - protobuf=3.20.3=py310h6a678d5_0
  - psutil=5.9.0=py310h5eee18b_0
  - ptyprocess=0.7.0=pyhd3eb1b0_2
  - pure_eval=0.2.2=pyhd3eb1b0_0
  - pyarrow=11.0.0=py310h468efa6_0
  - pycparser=2.21=pyhd3eb1b0_0
  - pygments=2.15.1=py310h06a4308_1
  - pyopenssl=23.0.0=py310h06a4308_0
  - pyparsing=3.0.9=py310h06a4308_0
  - pyqt=5.15.7=py310h6a678d5_1
  - pyrsistent=0.18.0=py310h7f8727e_0
  - pysocks=1.7.1=py310h06a4308_0
  - python=3.10.11=h7a1cb2a_2
  - python-dateutil=2.8.2=pyhd8ed1ab_0
  - python-fastjsonschema=2.16.2=py310h06a4308_0
  - python-xxhash=3.0.0=py310h5764c6d_1
  - python_abi=3.10=2_cp310
  - pytorch=2.0.1=py3.10_cuda11.7_cudnn8.5.0_0
  - pytorch-cuda=11.7=h778d358_5
  - pytorch-mutex=1.0=cuda
  - pytz=2023.3=pyhd8ed1ab_0
  - pyyaml=6.0=py310h5eee18b_1
  - pyzmq=25.0.2=py310h6a678d5_0
  - qt-main=5.15.2=h327a75a_7
  - qt-webengine=5.15.9=hd2b0992_4
  - qtconsole=5.4.2=py310h06a4308_0
  - qtpy=2.2.0=py310h06a4308_0
  - qtwebkit=5.212=h4eab89a_4
  - re2=2022.04.01=h27087fc_0
  - readline=8.2=h5eee18b_0
  - regex=2022.7.9=py310h5eee18b_0
  - requests=2.29.0=py310h06a4308_0
  - sacremoses=master=py_0
  - send2trash=1.8.0=pyhd3eb1b0_1
  - sentencepiece=0.1.99=py310hdb19cb5_0
  - setuptools=66.0.0=py310h06a4308_0
  - sip=6.6.2=py310h6a678d5_0
  - six=1.16.0=pyhd3eb1b0_1
  - snappy=1.1.9=h295c915_0
  - sniffio=1.2.0=py310h06a4308_1
  - soupsieve=2.4=py310h06a4308_0
  - sqlite=3.41.2=h5eee18b_0
  - stack_data=0.2.0=pyhd3eb1b0_0
  - sympy=1.11.1=py310h06a4308_0
  - tbb=2021.8.0=hdb19cb5_0
  - terminado=0.17.1=py310h06a4308_0
  - tinycss2=1.2.1=py310h06a4308_0
  - tk=8.6.12=h1ccaba5_0
  - tokenizers=0.11.4=py310h3dcd8bd_1
  - toml=0.10.2=pyhd3eb1b0_0
  - tomli=2.0.1=py310h06a4308_0
  - torchaudio=2.0.2=py310_cu117
  - torchtriton=2.0.0=py310
  - torchvision=0.15.2=py310_cu117
  - tornado=6.2=py310h5eee18b_0
  - tqdm=4.65.0=py310h2f386ee_0
  - traitlets=5.7.1=py310h06a4308_0
  - typing-extensions=4.5.0=py310h06a4308_0
  - typing_extensions=4.5.0=py310h06a4308_0
  - tzdata=2023c=h04d1e81_0
  - urllib3=1.26.15=py310h06a4308_0
  - utf8proc=2.6.1=h27cfd23_0
  - wcwidth=0.2.5=pyhd3eb1b0_0
  - webencodings=0.5.1=py310h06a4308_1
  - websocket-client=0.58.0=py310h06a4308_4
  - wheel=0.38.4=py310h06a4308_0
  - widgetsnbextension=4.0.5=py310h06a4308_0
  - xxhash=0.8.0=h7f98852_3
  - xz=5.4.2=h5eee18b_0
  - yaml=0.2.5=h7b6447c_0
  - yarl=1.7.2=py310h5764c6d_2
  - zeromq=4.3.4=h2531618_0
  - zipp=3.11.0=py310h06a4308_0
  - zlib=1.2.13=h5eee18b_0
  - zstd=1.5.5=hc292b87_0
  - pip:
      - aiohttp==3.8.4
      - dataclasses-json==0.5.7
      - greenlet==2.0.2
      - langchain==0.0.180
      - marshmallow==3.19.0
      - marshmallow-enum==1.5.1
      - mpmath==1.2.1
      - mypy-extensions==1.0.0
      - openai==0.27.7
      - openapi-schema-pydantic==1.2.4
      - pydantic==1.10.8
      - pyqt5-sip==12.11.0
      - sqlalchemy==2.0.15
      - tenacity==8.2.2
      - transformers==4.29.2
      - typing-inspect==0.9.0
prefix: /home/tai/miniconda3/envs/pt2hfpy310
```


This is really puzzling as `import transformers` does not really do anything (it's when you import a specific object that the code of a module is actually executed), so I don't see what could cause this slowdown.

@sgugger Yeah, it's really puzzling. I think ```import transformers``` would run the codes inside the ```transformers/__init__.py``` before actually using it.

ZailiWang said it may be because "that transformers have another openmp dependency and the new openmp lib flushed llvm-openmp invoked by torch" in [anohter issue](https://github.com/pytorch/pytorch/issues/102494#issuecomment-1568727409).

We do not have an openmp dependency. And if you look at the transformers __init__ you will see that nothing is done there.

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.