llama-cpp-python 〜 numpyバージョンアップの影響

はじめに

NumPy 2.0.0が6月16日にリリースされた。先日、lang-chainを使ってRAGを試してみようとして、dockerコンテナをビルドする際にエラーとなって初めて気付いた。その後、llama-cpp-pythonを組み込む際にCMakeでエラーが発生した。

この記事では、最近自分が体験した2つのエラーへの対応をまとめた。

NumPy 2.0.0に関するエラーへの対処

背景

最近、RAGをちゃんと学習しようと思い、LLMのファインチューニングとRAGという書籍を購入した。この書籍では、langchainを使っているので、langchainライブラリを組み込んだjupyterlabのdockerコンテナを作成することにした。

現象

Dockerfileに「pip install langchain-community faiss-gpu」を追加したjupyterlab上で、書籍のコードを実行してみた。すると、次のようなエラーメッセージが表示された。

A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.0.0 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.

簡単に解決するには、上記のメッセージの通り、これまでのバージョン1.26.4のnumpyをインストールすれば良いと考えた。しかし、自分のDockerfileでは、明示的に「pip install numpy」してはいない。どこで、「RUN pip install -U numpy==1.26.4」するか考え、インストール手順の最終段階で行うことにした。

Dockerfile

FROM nvidia/cuda:12.1.1-cudnn8-devel-ubuntu22.04

# Set bash as the default shell
ENV SHELL=/bin/bash

# Build with some basic utilities
RUN apt update \
        && apt install -y \
        wget \
        bzip2 \
        git \
        git-lfs \
        curl \
        unzip \
        file \
        xz-utils \
        sudo \
        python3 \
        python3-pip && \
        apt-get autoremove -y && \
        apt-get clean && \
        rm -rf /usr/local/src/*

# alias python='python3'
RUN ln -s /usr/bin/python3 /usr/bin/python

RUN pip install --upgrade pip setuptools \
        && pip install torch torchvision torchaudio \
        && pip install jupyterlab matplotlib pandas scikit-learn ipywidgets \
        && pip install transformers accelerate sentencepiece einops \
        && pip install langchain bitsandbytes protobuf \
        && pip install auto-gptq optimum \
        && pip install pypdf tiktoken sentence_transformers faiss-gpu trafilatura \
        && pip install langchain-community

# Install llama-cpp-python[server] with cuBLAS on
RUN CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 \
        pip install llama-cpp-python[server] --force-reinstall --no-cache-dir

RUN pip install -U numpy==1.26.4

# Create a working directory
WORKDIR /workdir

# Port number in container side
EXPOSE 8888

ENTRYPOINT ["jupyter-lab", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root", "--NotebookApp.token=''"]

CMD ["--notebook-dir=/workdir"]

後で分かったことだが、numpy 2.0.0をインストールしていたのでは、llama-cpp-pythonをCMakeしている箇所であった。

これで、解決かと思われたが、後日(正確には4日後)、別のマシンでもdocker buildすると、今度は別のエラーにぶち当たった。

CMake Error at vendor/llama.cpp/CMakeLists.txt:95

現象

タイトルの通り、docker build中(上記のDockerfileの「RUN CMAKE _ARGS=・・・」で)次のようなエラーとなった。

・・・
19.76       CMake Error at vendor/llama.cpp/CMakeLists.txt:95 (message):
19.76         LLAMA_CUBLAS is deprecated and will be removed in the future.
19.76
19.76         Use GGML_CUDA instead
・・・
19.76   note: This error originates from a subprocess, and is likely not a problem with pip.
19.76   ERROR: Failed building wheel for llama-cpp-python
19.76 Failed to build llama-cpp-python
19.76 ERROR: ERROR: Failed to build installable wheels for some pyproject.toml based \projects (llama-cpp-python)

対処その1

数日前まで動作していたコンテナを起動して、llama_cpp_pythonのバージョンを確認した。

# pip list | grep llama
llama_cpp_python          0.2.75

従って、上記のDockerfileを次のように修正すると、buildできるようになった。

# Install llama-cpp-python[server] with cuBLAS on
RUN CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 \
        pip install llama-cpp-python[server]==0.2.75 --force-reinstall --no-cache-dir

上記以外の部分は、上記引用のDockerfileと同じである。

対処その2

改めて、エラーメッセージを見ると、LLAMA_CUBLASは将来的には削除される非推奨であり、GGML_CUDAを使えと、とある。

そこで、Dockerfileを次のように修正した。その他の部分は変更なし。

RUN CMAKE_ARGS="-DGGML_CUDA=on" FORCE_CMAKE=1 \
        pip install llama-cpp-python[server] --force-reinstall --no-cache-dir

こちらの方が本質的な対応であると考え、上記を採用することにした。

まとめ

以上説明した2つのエラーに関しては、相互に関連は無いと思われる。

それら変更のタイミングでライブラリを引用する必要があったこと(docker buildしたこと)により、環境構築に手間取った。その分勉強にはなった。

RAGについてもそのうちまとめて投稿する。