pythonpytorchhuggingface-transformers

ModuleNotFoundError for transformers.pipeline after installing PyTorch for CUDA


I'm a bit stumped on an issue that just popped up. My code, which uses the transformers library, was running perfectly fine until I tried to install a CUDA-compatible version of PyTorch.

Everything broke right after I ran this command to get PyTorch working with my CUDA 11.8 setup:

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

Now even I can not import pipeline from transformers

from transformers import pipeline

and this is the output:

Traceback (most recent call last):
  File "/talie-hdd/00/tasks/meysam/envs/my-env/lib/python3.12/site-packages/transformers/utils/import_utils.py", line 2292, in __getattr__
    module = self._get_module(self._class_to_module[name])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/talie-hdd/00/tasks/meysam/envs/my-env/lib/python3.12/site-packages/transformers/utils/import_utils.py", line 2322, in _get_module
    raise e
  File "/talie-hdd/00/tasks/meysam/envs/my-env/lib/python3.12/site-packages/transformers/utils/import_utils.py", line 2320, in _get_module
    return importlib.import_module("." + module_name, self.__name__)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 999, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/talie-hdd/00/tasks/meysam/envs/my-env/lib/python3.12/site-packages/transformers/pipelines/__init__.py", line 26, in <module>
    from ..image_processing_utils import BaseImageProcessor
  File "/talie-hdd/00/tasks/meysam/envs/my-env/lib/python3.12/site-packages/transformers/image_processing_utils.py", line 22, in <module>
    from .image_transforms import center_crop, normalize, rescale
  File "/talie-hdd/00/tasks/meysam/envs/my-env/lib/python3.12/site-packages/transformers/image_transforms.py", line 22, in <module>
    from .image_utils import (
  File "/talie-hdd/00/tasks/meysam/envs/my-env/lib/python3.12/site-packages/transformers/image_utils.py", line 59, in <module>
    from torchvision.transforms import InterpolationMode
  File "/talie-hdd/00/tasks/meysam/envs/my-env/lib/python3.12/site-packages/torchvision/__init__.py", line 9, in <module>
    from .extension import _HAS_OPS  # usort:skip
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/talie-hdd/00/tasks/meysam/envs/my-env/lib/python3.12/site-packages/torchvision/extension.py", line 92, in <module>
    _check_cuda_version()
  File "/talie-hdd/00/tasks/meysam/envs/my-env/lib/python3.12/site-packages/torchvision/extension.py", line 78, in _check_cuda_version
    raise RuntimeError(
RuntimeError: Detected that PyTorch and torchvision were compiled with different CUDA major versions. PyTorch has CUDA Version=11.8 and torchvision has CUDA Version=12.1. Please reinstall the torchvision that matches your PyTorch install.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/talie-hdd/00/tasks/meysam/AgenticChatbotOSS/oss.py", line 1, in <module>
    from transformers import pipeline
  File "/talie-hdd/00/tasks/meysam/envs/my-env/lib/python3.12/site-packages/transformers/utils/import_utils.py", line 2295, in __getattr__
    raise ModuleNotFoundError(
ModuleNotFoundError: Could not import module 'pipeline'. Are this object's requirements defined correctly?

Solution

  • as you can see right here, I thought you installed CUDA version 11.8 but your torchvision was 12.1

    RuntimeError: Detected that PyTorch and torchvision were compiled with different CUDA major versions. PyTorch has CUDA Version=11.8 and torchvision has CUDA Version=12.1. Please reinstall the torchvision that matches your PyTorch install.
    

    so, you need to reinstall torchvision including torchaudio with the correct CUDA 11.8

    pip uninstall -y torchvision torchaudio
    pip install torchvision==0.15.2+cu118 torchaudio==2.0.2+cu118 --index-url https://download.pytorch.org/whl/cu118
    

    you can adjust version if needed depending on your installed pytorch. You can check it with

    python -c "import torch; print(torch.__version__)"
    

    After that you can check once again to verify they already match (pytorch with CUDA)

    python -c "import torch, torchvision, torchaudio; print(torch.__version__, torchvision.__version__, torchaudio.__version__)"