WSL-Ubuntu で bitsandbytes のインストールに失敗するとき

WSL-Ubuntu で bitsandbytes のインストールに失敗するとき
Photo by Gabriel Heinzer / Unsplash

bitsandbytes を pip install しようとしたときに、以下のようなエラーがでたときの対処方法です

Could not load bitsandbytes native library: libcusparse.so.11: cannot open shared object file: No such file or directory
Traceback (most recent call last):
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/cextension.py", line 109, in <module>
    lib = get_native_library()
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/cextension.py", line 96, in get_native_library
    dll = ct.cdll.LoadLibrary(str(binary_path))
  File "/usr/lib/python3.10/ctypes/__init__.py", line 452, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python3.10/ctypes/__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: libcusparse.so.11: cannot open shared object file: No such file or directory

CUDA Setup failed despite CUDA being available. Please run the following command to get more information:

python -m bitsandbytes

Inspect the output of the command and see if you can locate CUDA libraries. You might need to add them
to your LD_LIBRARY_PATH. If you suspect a bug, please take the information from python -m bitsandbytes
and open an issue at: https://github.com/TimDettmers/bitsandbytes/issues

デバッグ情報の表示と読み解き

Pythonのモジュール実行機能を使って、bitsandbytesをモジュール実行してデバッグ情報を表示してみます。

python -m bitsandbytes

Could not load bitsandbytes native library: libcusparse.so.11: cannot open shared object file: No such file or directory
Traceback (most recent call last):
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/cextension.py", line 109, in <module>
    lib = get_native_library()
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/cextension.py", line 96, in get_native_library
    dll = ct.cdll.LoadLibrary(str(binary_path))
  File "/usr/lib/python3.10/ctypes/__init__.py", line 452, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python3.10/ctypes/__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: libcusparse.so.11: cannot open shared object file: No such file or directory

CUDA Setup failed despite CUDA being available. Please run the following command to get more information:

python -m bitsandbytes

Inspect the output of the command and see if you can locate CUDA libraries. You might need to add them
to your LD_LIBRARY_PATH. If you suspect a bug, please take the information from python -m bitsandbytes
and open an issue at: https://github.com/TimDettmers/bitsandbytes/issues
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++ BUG REPORT INFORMATION ++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++ OTHER +++++++++++++++++++++++++++
CUDA specs: CUDASpecs(highest_compute_capability=(8, 6), cuda_version_string='117', cuda_version_tuple=(11, 7))
PyTorch settings found: CUDA_VERSION=117, Highest Compute Capability: (8, 6).
To manually override the PyTorch CUDA version please see: https://github.com/TimDettmers/bitsandbytes/blob/main/docs/source/nonpytorchcuda.mdx
CUDA SETUP: WARNING! CUDA runtime files not found in any environmental path.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++ DEBUG INFO END ++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Checking that the library is importable and CUDA is callable...
Couldn't load the bitsandbytes library, likely due to missing binaries.
Please ensure bitsandbytes is properly installed.

For source installations, compile the binaries with `cmake -DCOMPUTE_BACKEND=cuda -S .`.
See the documentation for more details if needed.

Trying a simple check anyway, but this will likely fail...
Traceback (most recent call last):
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/diagnostics/main.py", line 66, in main
    sanity_check()
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/diagnostics/main.py", line 40, in sanity_check
    adam.step()
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/torch/optim/optimizer.py", line 280, in wrapper
    out = func(*args, **kwargs)
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/optim/optimizer.py", line 287, in step
    self.update_step(group, p, gindex, pindex)
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/optim/optimizer.py", line 496, in update_step
    F.optimizer_update_32bit(
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/functional.py", line 1584, in optimizer_update_32bit
    optim_func = str2optimizer32bit[optimizer_name][0]
NameError: name 'str2optimizer32bit' is not defined
Above we output some debug information.
Please provide this info when creating an issue via https://github.com/TimDettmers/bitsandbytes/issues/new/choose
WARNING: Please be sure to sanitize sensitive info from the output before posting it.

実行時のエラー情報とともに、 BUG REPORT INFORMATION が表示されます。

ここには以下2つの重要な情報が含まれています

  • LD_LIBRARY_PATH に CUDA のパスを入れたほうがいいかも
  • 現在の venv にある PyTorch が使用している CUDA バージョンは PyTorch settings found: CUDA_VERSION=117 と表示されていることから 11.7

現在の WSL-Ubuntu にインストールされている CUDA バージョンを確認する

現在の WSL2 Ubuntu にインストールされている CUDA バージョンを確認します。

ls /usr/local/cuda* -d

でCUDAのインストールディレクトリ一覧をみてみると、

/usr/local/cuda /usr/local/cuda-12 /usr/local/cuda-12.4

とありましたので、 CUDA バージョン 12.4 がこのubuntuにインストールされていることがわかりました。

モジュール単位でより詳しいバージョン番号までみたいときは

cat /usr/local/cuda/version.json

で表示させることも可能です。

エラーメッセージの原因

このエラーメッセージの原因がみえてきました。

venv にインストールされている PyTorch は CUDA_VERSION=117 用のインストールであったにもかかわらず、実際に ubuntu にインストールされている CUDAバージョンが12.4 だったため、 11.7をさがしにいったbitsandbytes がCUDA ライブラリを見つけられなかった、 ことが原因でした。

対処法

対処法としては3つあります

  1. ubuntu の CUDA ライブラリを venv とおなじ CUDA 11.7 にする
  2. venv にあるライブラリを ubuntu とおなじ CUDA 12.4 にする
  3. bitsandbytes に CUDA 12.4 を使うように教え込む

ここでは、いちばん簡単にできる 3 でやってみます。

(これで、アプリケーションのテストが通れば問題ない。テストが通らなければ、1や2で対策することになるでしょう)

Bitsandbytes に CUDA バージョンを教える

bitsandbytes に CUDA 12.4 ライブラリのパスを指定するには、以下のようにします

export BNB_CUDA_VERSION=124

さて、これで、もういちどbitsadnbytesをモジュール実行してみます。

python -m bitsandbytes

WARNING: BNB_CUDA_VERSION=124 environment variable detected; loading libbitsandbytes_cuda124.so.
This can be used to load a bitsandbytes version that is different from the PyTorch CUDA version.
If this was unintended set the BNB_CUDA_VERSION variable to an empty string: export BNB_CUDA_VERSION=
If you use the manual override make sure the right libcudart.so is in your LD_LIBRARY_PATH
For example by adding the following to your .bashrc: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<path_to_cuda_dir/lib64

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++ BUG REPORT INFORMATION ++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++ OTHER +++++++++++++++++++++++++++
CUDA specs: CUDASpecs(highest_compute_capability=(8, 6), cuda_version_string='117', cuda_version_tuple=(11, 7))
PyTorch settings found: CUDA_VERSION=117, Highest Compute Capability: (8, 6).
WARNING: BNB_CUDA_VERSION=124 environment variable detected; loading libbitsandbytes_cuda124.so.
This can be used to load a bitsandbytes version that is different from the PyTorch CUDA version.
If this was unintended set the BNB_CUDA_VERSION variable to an empty string: export BNB_CUDA_VERSION=
If you use the manual override make sure the right libcudart.so is in your LD_LIBRARY_PATH
For example by adding the following to your .bashrc: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<path_to_cuda_dir/lib64

To manually override the PyTorch CUDA version please see: https://github.com/TimDettmers/bitsandbytes/blob/main/docs/source/nonpytorchcuda.mdx
CUDA SETUP: WARNING! CUDA runtime files not found in any environmental path.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++ DEBUG INFO END ++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Checking that the library is importable and CUDA is callable...
SUCCESS!
Installation was successful!

Installation was successful!

ということで、今度は、無事に bitsandbytes が CUDAライブラリをみつけることができうまく動いてくれそうです。

環境変数が有効になるように設定する

BNB_CUDA_VERSION が有効になるようにしましょう

.bashrc に入れる場合

ターミナルからPythonアプリを実行するときのために .bashrc に BNB_CUDA_VERSION 環境変数を追加しておきます

.bashrc を開いたら、最後の行に

export BNB_CUDA_VERSION=124

を記述しておきます。

コードで直接指定する場合

以下のようにすれば、pythonコードから直接指定することもできます

import os

# 環境変数 BNB_CUDA_VERSION を設定
os.environ['BNB_CUDA_VERSION'] = '124'

Read more

【解説】Tekken トークナイザーとは何か? 〜 Mistral が採用する新世代トークナイザーの特徴

【解説】Tekken トークナイザーとは何か? 〜 Mistral が採用する新世代トークナイザーの特徴

こんにちは! 本日は、Tekkenについて解説いたします! 皆さま Tekken と聞いて何を思い浮かべますか? 格ゲーの鉄拳でしょうか? 私は、昔プレイした Age of Empires に登場する鉄剣戦士を思い浮かべました🤗 ちょっと古いかもしれませんが、名作です! さてつかみはこのくらいにして、、 LLMはご存じのとおり驚異的なスピードで進化しています。そんな中でひそかに注目されているのが、トークナイザーの改善です。 たとえば、Meta の Llama 系モデルのトークナイザーは Sentence Piece から BPE系へ進化するなど、LLM業界では従来よりも高効率なトークナイズ(テキスト分割)の方法を導入し始めています。 そして Mistral AI もまた、新たに「Tekken トークナイザー」という仕組みを採用し、大規模言語モデルの性能を底上げしています。 本記事では、Tekken トークナイザーの登場背景や技術的特徴、他のトークナイザーとの違い、さらには Mistral との関係などをわかりやすく解説していきます。 1. Tekken トーク

By Qualiteg プロダクト開発部
[AI新規事業創出]Qualitegオリジナル、アイディア評価、事業アイディア選定方法

[AI新規事業創出]Qualitegオリジナル、アイディア評価、事業アイディア選定方法

Qualiteg blogを訪問してくださった皆様、こんにちは。Micheleです。AIを活用した新規事業やマーケティングを手がけている私には、クライアントからよく寄せられる質問があります。AIを用いた事業展開を検討されている方々が共通して直面するであろう課題に対して、このブログを通じて私なりの解答をご提供したいと思います。 はじめに AI技術の急速な発展は、スタートアップから大企業まで、あらゆるビジネスに新たな可能性をもたらしています。クライアントとの会話の中でも、AIを活用した革新的な事業アイディアに関する相談が増えています。 しかし、多くの企業が「素晴らしいアイディアを思いついた!」と興奮しながらも、そのアイディアを具体化し、成功に導くための方法論に悩んでいるのも事実です。特にAIを用いた事業展開においては、従来のビジネスモデルとは異なる視点が必要となるため、その難しさはさらに増します。 本記事では、Qualitegオリジナルのアイディア評価、事業アイディア選定方法について解説します。特に、AIを用いた事業展開を検討されている方々が共通して直面するであろう課題に対して、

By Join us, Michele on Qualiteg's adventure to innovation
日本語対応!Mistral Small v3 解説

日本語対応!Mistral Small v3 解説

こんにちは! Mistral AIは2025年1月30日、新しい言語モデル「Mistral Small v3」を発表しました。このモデルは、24Bという比較的小規模なパラメータ数ながら、70B以上の大規模モデルに匹敵する性能を実現しています。また日本語対応も謳われており期待の高い小型モデルです! https://huggingface.co/mistralai/Mistral-Small-24B-Instruct-2501 動画 こちら本ブログの解説動画もご覧いただけます😊 きわだってるのは、レイテンシー最適化 Mistral Small 3のめだった特徴は、その処理性能とレイテンシーの絶妙なバランスではないでしょうか。 公開されている以下の性能評価のグラフによると、トークンあたり約11ミリ秒という業界最速レベルのレイテンシーを達成しています。これは、Qwen-2.5 32Bの約15ミリ秒やGemma-2 27Bの約14ミリ秒と比較して、明確な優位性を示しています。さらに注目すべきは、GPT-4o Miniと比較しても、より低いレイテンシーで同等以上の性能を実現し

By Qualiteg プロダクト開発部
[vLLM] To use CUDA with multiprocessing, you must use the 'spawn' start method の対処法

[vLLM] To use CUDA with multiprocessing, you must use the 'spawn' start method の対処法

WSLで vLLM を使用するとき、 tensor parallel を使って複数枚のGPUで1つのLLMをサーブしようとしたとき以下のようなエラーが発生しがちです RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the 'spawn' start method 遭遇するシーンとしてはvLLMの起動オプションに以下のようにテンソル並列化オプションを指定したときです。 --tensor-parallel-size 2 つまり、マルチプロセッシングでCUDA使うときは、 "fork"じゃなくて"spawn" 使ってね、というエラーです。 これを vLLM に教えるために、以下の2行目のように環境変数を設定してあげるとvLLMが "spawn" を使ってくれるようになります。 export

By Qualiteg プロダクト開発部