intel / intel-extension-for-pytorch

A Python package for extending the official PyTorch that can easily obtain performance on Intel platform
Apache License 2.0
1.54k stars 234 forks source link

[torch]: ImportError: cannot import name 'packaging' from 'pkg_resources' #662

Closed raj-ritu17 closed 1 month ago

raj-ritu17 commented 2 months ago

Describe the bug

created a conda env with python 3.11, have setup with all required library. ran sanity test, it fails to import pkg_resources:

python -c "import torch; import intel_extension_for_pytorch as ipex; print(torch.__version__); print(ipex.__version__); [print(f'[{i}]: {torch.xpu.get_device_properties(i)}') for i in range(torch.xpu.device_count())];"

error:

(envIPEX):~/ritu$ python -c "import torch; import intel_extension_for_pytorch as ipex; print(torch.__version__); print(ipex.__version__); [print(f'[{i}]: {torch.xpu.get_device_properties(i)}') for i in range(torch.xpu.device_count())];"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/rajritu/miniforge3/envs/envIPEX/lib/python3.11/site-packages/intel_extension_for_pytorch/__init__.py", line 111, in <module>
    from . import xpu
  File "/home/rajritu/miniforge3/envs/envIPEX/lib/python3.11/site-packages/intel_extension_for_pytorch/xpu/__init__.py", line 27, in <module>
    from .cpp_extension import *
  File "/home/rajritu/miniforge3/envs/envIPEX/lib/python3.11/site-packages/intel_extension_for_pytorch/xpu/cpp_extension.py", line 16, in <module>
    from torch.utils.cpp_extension import _TORCH_PATH
  File "/home/rajritu/miniforge3/envs/envIPEX/lib/python3.11/site-packages/torch/utils/cpp_extension.py", line 28, in <module>
    from pkg_resources import packaging  # type: ignore[attr-defined]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: cannot import name 'packaging' from 'pkg_resources' (/xxx/xxx/miniforge3/envs/envIPEX/lib/python3.11/site-packages/pkg_resources/__init__.py)

resolution: it comes from the setup tools, downgrade the tools: python -m pip install setuptools==69.5.1

output after downgrading:

 python -c "import torch; import intel_extension_for_pytorch as ipex; print(torch.__version__); print(ipex.__version__); [print(f'[{i}]: {torch.xpu.get_device_properties(i)}') for i in range(torch.xpu.device_count())];"
2.1.0.post2+cxx11.abi
2.1.30+xpu
[0]: _DeviceProperties(name='Intel(R) Arc(TM) A770 Graphics', platform_name='Intel(R) Level-Zero', dev_type='gpu', driver_version='1.3.29138', has_fp64=0, total_memory=15473MB, max_compute_units=512, gpu_eu_count=512)
[1]: _DeviceProperties(name='Intel(R) Arc(TM) A770 Graphics', platform_name='Intel(R) Level-Zero', dev_type='gpu', driver_version='1.3.29138', has_fp64=0, total_memory=15473MB, max_compute_units=512, gpu_eu_count=512)

Versions

Collecting environment information...
PyTorch version: 2.1.0.post2+cxx11.abi
PyTorch CXX11 ABI: Yes
IPEX version: 2.1.30+xpu
IPEX commit: 474a6b3cb
Build type: Release

OS: Ubuntu 22.04.4 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: N/A
IGC version: 2024.1.0 (2024.1.0.20240308)
CMake version: version 3.22.1
Libc version: glibc-2.35

Python version: 3.11.9 | packaged by conda-forge | (main, Apr 19 2024, 18:36:13) [GCC 12.3.0] (64-bit runtime)
Python platform: Linux-6.5.0-35-generic-x86_64-with-glibc2.35
Is XPU available: True
DPCPP runtime version: 2024.1
MKL version: 2024.1
GPU models and configuration:
[0] _DeviceProperties(name='Intel(R) Arc(TM) A770 Graphics', platform_name='Intel(R) Level-Zero', dev_type='gpu', driver_version='1.3.29138', has_fp64=0, total_memory=15473MB, max_compute_units=512, gpu_eu_count=512)
[1] _DeviceProperties(name='Intel(R) Arc(TM) A770 Graphics', platform_name='Intel(R) Level-Zero', dev_type='gpu', driver_version='1.3.29138', has_fp64=0, total_memory=15473MB, max_compute_units=512, gpu_eu_count=512)
Intel OpenCL ICD version: 24.13.29138.29-881~22.04
Level Zero version: 1.3.29138.29-881~22.04

CPU:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Address sizes:                      52 bits physical, 57 bits virtual
Byte Order:                         Little Endian
CPU(s):                             112
On-line CPU(s) list:                0-111
Vendor ID:                          GenuineIntel
Model name:                         Intel(R) Xeon(R) w9-3495X
CPU family:                         6
Model:                              143
Thread(s) per core:                 2
Core(s) per socket:                 56
Socket(s):                          1
Stepping:                           8
CPU max MHz:                        4800.0000
CPU min MHz:                        800.0000
BogoMIPS:                           3792.00
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 art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq 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 cpuid_fault epb cat_l3 cat_l2 cdp_l3 invpcid_single intel_ppin cdp_l2 ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect avx_vnni avx512_bf16 wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req vnmi avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid bus_lock_detect cldemote movdiri movdir64b enqcmd fsrm md_clear serialize tsxldtrk pconfig arch_lbr ibt amx_bf16 avx512_fp16 amx_tile amx_int8 flush_l1d arch_capabilities
Virtualization:                     VT-x
L1d cache:                          2.6 MiB (56 instances)
L1i cache:                          1.8 MiB (56 instances)
L2 cache:                           112 MiB (56 instances)
L3 cache:                           105 MiB (1 instance)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-111
Vulnerability Gather data sampling: Not affected
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:             Not affected
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] intel-extension-for-pytorch==2.1.30+xpu
[pip3] numpy==1.26.0
[pip3] torch==2.1.0.post2+cxx11.abi
[pip3] torchaudio==2.1.0.post2+cxx11.abi
[pip3] torchvision==0.16.0.post2+cxx11.abi
[conda] intel-extension-for-pytorch 2.1.30              py311_xpu_0    intel
[conda] mkl-include               2024.1.0              intel_691    intel
[conda] mkl-static                2024.1.0              intel_691    intel
[conda] numpy                     1.26.0                   pypi_0    pypi
[conda] pytorch                   2.1.0               py311_xpu_3    intel
[conda] torchaudio                2.1.0.post2+cxx11.abi          pypi_0    pypi
[conda] torchvision               0.16.0.post2+cxx11.abi          pypi_0    pypi
ZhaoqiongZ commented 2 months ago

Hi @raj-ritu17 , this is not a ipex issue and as you said it is related to setuptools package version installed by miniforge3. and yes you can downgrade your setuptools by python -m pip install setuptools==69.5.1 to fix it.

martinmCGG commented 2 months ago

This also happens in a freshly installed Conda when creating a new environment from scratch, specifically conda create --name ipex3 python=3.10 intel-extension-for-pytorch=2.1.30 pytorch=2.1.0 -c intel -c conda-forge yields an environment where importing ipex fails with the ImportError: cannot import name 'packaging' from 'pkg_resources' error.

Workaround: running conda install 'setuptools<70.0.0' in the environment to downgrades setuptools to a compatible version and IPEX works again.

Fix: consider adding a "<" version dependency for setuptools, perhaps here for pip users. Conda package does not seem to list a dependency on setuptools (based on looking at "depends" in miniconda3/pkgs/intel-extension-for-pytorch-2.1.30-py310_xpu_0/info/index.json after installing the package) so it may need to be added there as well. (note that I don't have much experience with publishing packages so there might be a better place to specify dependencies) Alternatively, when IPEX gets upgraded to PyTorch > 2.1.x, this might be automatically resolved since the offending import was removed in PyTorch v2.2.0.

ZhaoqiongZ commented 1 month ago

Hi @martinmCGG , thanks for advice, we add pip install setuptools==69.5.1 in our installation document. https://intel.github.io/intel-extension-for-pytorch/index.html#installation?platform=gpu&version=v2.1.30%2bxpu&os=linux%2fwsl2&package=pip

MordragT commented 3 weeks ago

Hi @raj-ritu17 , this is not a ipex issue and as you said it is related to setuptools package version installed by miniforge3. and yes you can downgrade your setuptools by python -m pip install setuptools==69.5.1 to fix it.

It seems like this is not a bug in setuptools but a breaking change: https://github.com/pypa/setuptools/issues/4385#issuecomment-2126286321

Couldnt just importing packaging directly fix it for newer setuptools while still being compatible with older setuptools ?