[Python] python error with faiss on GPU with cuda despite successful installation

Discussão em 'Python' iniciado por Stack, Fevereiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a Google Cloud VM with CUDA 12. I tried to install either faiss-gpu-cu12 or faiss-gpu-cu12[fix_cuda] using either a venv or pyenv virtual environment, under python 3.12.4. For the application we also need langchain packages as below:

    python --version
    Python 3.12.4


    python -m pip install langchain langchain-huggingface langchain-community faiss-gpu-cu12[fix_cuda]


    pip gives no error and everything looks fine, but when I run the minimal example below:

    from langchain_community.vectorstores import FAISS
    from langchain_huggingface import HuggingFaceEmbeddings
    from sentence_transformers import SentenceTransformer

    docs = [ 'sefsl;fk lskdf;lk s', 'ewrl kwelklekfl ls ;' ]
    embedder = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')

    f = FAISS.from_texts(docs, embedding=embedder)
    embeddings = embedder.encode(docs)


    I get this error:

    Traceback (most recent call last):
    File "/home/jupyterlab_user/erwan/RAG/.venv/lib/python3.12/site-packages/langchain_community/vectorstores/faiss.py", line 55, in dependable_faiss_import
    import faiss
    File "/home/jupyterlab_user/erwan/RAG/.venv/lib/python3.12/site-packages/faiss/__init__.py", line 16, in <module>
    from .loader import *
    File "/home/jupyterlab_user/erwan/RAG/.venv/lib/python3.12/site-packages/faiss/loader.py", line 111, in <module>
    from .swigfaiss import *
    File "/home/jupyterlab_user/erwan/RAG/.venv/lib/python3.12/site-packages/faiss/swigfaiss.py", line 10, in <module>
    from . import _swigfaiss
    ImportError: /home/jupyterlab_user/erwan/RAG/.venv/lib/python3.12/site-packages/faiss/_swigfaiss.cpython-312-x86_64-linux-gnu.so: ELF load command address/offset not properly aligned

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "/home/jupyterlab_user/erwan/RAG/minimal-faiss-expl2.py", line 9, in <module>
    f = FAISS.from_texts(docs, embedding=embedder)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/jupyterlab_user/erwan/RAG/.venv/lib/python3.12/site-packages/langchain_community/vectorstores/faiss.py", line 1042, in from_texts
    return cls.__from(
    ^^^^^^^^^^^
    File "/home/jupyterlab_user/erwan/RAG/.venv/lib/python3.12/site-packages/langchain_community/vectorstores/faiss.py", line 994, in __from
    faiss = dependable_faiss_import()
    ^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/jupyterlab_user/erwan/RAG/.venv/lib/python3.12/site-packages/langchain_community/vectorstores/faiss.py", line 57, in dependable_faiss_import
    raise ImportError(
    ImportError: Could not import faiss python package. Please install it with `pip install faiss-gpu` (for CUDA supported GPU) or `pip install faiss-cpu` (depending on Python version).


    It looks like the Faiss library is not installed properly, even though pip gave no error when installing package faiss-gpu-cu12. Is there anything I missed? For instance, is there any known incompatibility between the packages above?

    For now I'm restricted to use faiss-cpu, so any suggestion welcome!


    Additional info:

    $ pip freeze | grep faiss
    faiss-gpu-cu12==1.8.0.2


    $ pip freeze | grep langchain
    langchain==0.3.3
    langchain-community==0.3.2
    langchain-core==0.3.12
    langchain-huggingface==0.1.0
    langchain-text-splitters==0.3.0


    $ nvcc --version
    nvcc: NVIDIA (R) Cuda compiler driver
    Copyright (c) 2005-2020 NVIDIA Corporation
    Built on Wed_Jul_22_19:09:09_PDT_2020
    Cuda compilation tools, release 11.0, V11.0.221
    Build cuda_11.0_bu.TC445_37.28845127_0

    Continue reading...

Compartilhe esta Página