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

Pythonの落とし穴:__len__メソッドを実装したらオブジェクトの真偽値判定が変わってしまった話

Pythonの落とし穴:__len__メソッドを実装したらオブジェクトの真偽値判定が変わってしまった話

こんにちは! Pythonでカスタムクラスを作成していて、 「オブジェクトは存在するのにif文でFalseと判定される」 という不可解な現象に遭遇したことはありませんか? この記事では、__len__メソッドを実装することで生じる、予期しない真偽値判定の挙動について解説いたします! 実際に遭遇したバグ ユーザーの投稿を管理するクラスを実装していたときのことです class PostManager: """ブログ投稿を管理するクラス""" def __init__(self, user_id): self.user_id = user_id self._posts = [] self._cache = {} def __len__(self): """投稿数を返す""" return len(self._posts) def add_post(

By Qualiteg プロダクト開発部
CEATEC 2025に出展します!フォトリアルAIアバター「MotionVox🄬」の最新版を実体験いただけます

CEATEC 2025に出展します!フォトリアルAIアバター「MotionVox🄬」の最新版を実体験いただけます

株式会社Qualitegは、2025年10月14日(火)~17日(金)に幕張メッセで開催される「CEATEC 2025」に出展いたします。今回の出展では、当社が開発したフォトリアリスティックAIアバター技術「MotionVox🄬」をはじめ、最新のAI技術とビジネスイノベーションソリューションをご紹介いたします。 出展概要 * 会期:2025年10月14日(火)~10月17日(金) * 会場:幕張メッセ * 出展エリア:ネクストジェネレーションパーク * ブース番号:ホール6 6H207 * CEATEC内特設サイト:https://www.ceatec.com/nj/exhibitor_detail_ja?id=1915 見どころ:最先端AI技術を体感できる特別展示 1. フォトリアルAIアバター「MotionVox🄬」 テキスト入力だけで、まるで本物の人間のような動画を生成できる革新的なAIアバターシステムです。 MotionVox🄬は自社開発している「Expression Aware🄬」技術により日本人の演者データを基に開発された、

By Qualiteg ニュース
その処理、GPUじゃなくて勝手にCPUで実行されてるかも  ~ONNX RuntimeのcuDNN 警告と対策~

その処理、GPUじゃなくて勝手にCPUで実行されてるかも ~ONNX RuntimeのcuDNN 警告と対策~

こんにちは! 本日は、ONNX RuntimeでGPU推論時の「libcudnn.so.9: cannot open shared object file」エラーの解決方法についての内容となります。 ONNX Runtimeを使用してGPU推論を行う際、CUDAプロバイダの初期化エラーに遭遇することがありますので、このエラーの原因と解決方法を解説いたします。 エラーメッセージの詳細 [E:onnxruntime:Default, provider_bridge_ort.cc:2195 TryGetProviderInfo_CUDA] /onnxruntime_src/onnxruntime/core/session/provider_bridge_ort.cc:1778 onnxruntime::Provider& onnxruntime::ProviderLibrary::Get() [ONNXRuntimeError] : 1 : FAIL : Failed to load

By Qualiteg プロダクト開発部
大企業のAIセキュリティを支える基盤技術 - 今こそ理解するActive Directory 第3回 クライアントとサーバーのドメイン参加

大企業のAIセキュリティを支える基盤技術 - 今こそ理解するActive Directory 第3回 クライアントとサーバーのドメイン参加

こんにちは、今回はシリーズ第3回クライアントとサーバーのドメイン参加について解説いたします! はじめに こんにちは!シリーズ第3回「クライアントとサーバーのドメイン参加」へようこそ。 前回(第2回)では、Active Directoryドメイン環境の構築手順について、ドメインコントローラーのセットアップからDNS設定まで詳しく解説しました。ドメイン環境の「土台」が整ったところで、今回はいよいよ実際にコンピューターをドメインに参加させる手順に進みます。 「ドメインユーザーアカウントを作ったのに、なぜかログインできない」「新しいPCを追加したけど、ドメイン認証が使えない」といった経験はありませんか?実は、Active Directoryの世界では、ユーザーアカウントを作成しただけでは不十分で、そのユーザーが使用するコンピューター自体もドメインに「参加」させる必要があるのです。 本記事では、このドメイン参加について、単なる手順の説明にとどまらず、「なぜドメイン参加が必要なのか」「裏側で何が起きているのか」という本質的な仕組みまで、初心者の方にも分かりやすく解説していきます。Win

By Qualiteg コンサルティング