Today(※最近) I Learned です
目次
LLMの量子化で知っていたbitsandbytes
過去の素振りから、bitsandbytesはtransformers.BitsAndBytesConfig
で知っていました。
LLMをロードする際のquantization_config
に指定します。
bnb_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16, )
This is a wrapper class about all possible attributes and features that you can play with a model that has been loaded using bitsandbytes.
pip install
しているbitsandbytesについてこれ以上を知らなかったのですが、もう少しできることがあるというのを最近知りました。
python -m bitsandbytes
GPU環境での訓練の試行錯誤の中で知りました。
bitsandbytesがこのコマンドをエラーメッセージの中に出していました。
Colabでの実行例
Google Colabの無料枠(T4)で叩いた例です。
- Python 3.10.12
- torch 2.4.0+cu121
- bitsandbytes 0.43.3
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++ BUG REPORT INFORMATION ++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++ OTHER +++++++++++++++++++++++++++ CUDA specs: CUDASpecs(highest_compute_capability=(7, 5), cuda_version_string='121', cuda_version_tuple=(12, 1)) PyTorch settings found: CUDA_VERSION=121, Highest Compute Capability: (7, 5). To manually override the PyTorch CUDA version please see: https://github.com/TimDettmers/bitsandbytes/blob/main/docs/source/nonpytorchcuda.mdx The directory listed in your path is found to be non-existent: /sys/fs/cgroup/memory.events /var/colab/cgroup/jupyter-children/memory.events The directory listed in your path is found to be non-existent: //172.28.0.1 The directory listed in your path is found to be non-existent: 8013 The directory listed in your path is found to be non-existent: //colab.research.google.com/tun/m/cc48301118ce562b961b3c22d803539adc1e0c19/gpu-t4-s-1ops9k4tl02er --tunnel_background_save_delay=10s --tunnel_periodic_background_save_frequency=30m0s --enable_output_coalescing=true --output_coalescing_required=true --log_code_content The directory listed in your path is found to be non-existent: /datalab/web/pyright/typeshed-fallback/stdlib,/usr/local/lib/python3.10/dist-packages The directory listed in your path is found to be non-existent: /env/python The directory listed in your path is found to be non-existent: //ipykernel.pylab.backend_inline 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!」、環境構築はなんかよさそうとちょっと自信が持てる(GPUまわり、ハードが絡むと途端によくわからなくなる勢)
- CUDAのバージョンも見える。12.1かな
実装
ソースコードを覗きましょう。
python -m bitsandbytes
は、__main__.py
を実行します1。
https://docs.python.org/ja/3/using/cmdline.html#cmdoption-m
通常のモジュールの代わりにパッケージ名が与えられた場合、インタプリタは
<pkg>.__main__
を main モジュールとして実行します。
__main__.py
を見ると、bitsandbytes.diagnostics.main.main()
関数が呼ばれています。
https://github.com/bitsandbytes-foundation/bitsandbytes/blob/0.43.3/bitsandbytes/__main__.py#L2
main()
関数の実装の中では
https://github.com/bitsandbytes-foundation/bitsandbytes/blob/0.43.3/bitsandbytes/diagnostics/main.py#L45-L85
- CUDAのスペックの取得・表示
torch.cuda.is_available()
の確認- CUDAの実行環境の診断
が行われています。
「おおっ」と思ったのがsanity_check()
https://github.com/bitsandbytes-foundation/bitsandbytes/blob/0.43.3/bitsandbytes/diagnostics/main.py#L15-L42
GPUでの演算を試してるんですね。
p = torch.nn.Parameter(torch.rand(10, 10).cuda()) a = torch.rand(10, 10).cuda() p1 = p.data.sum().item() adam = Adam([p]) out = a * p loss = out.sum() loss.backward() adam.step() p2 = p.data.sum().item() assert p1 != p2
これが通っていたら、bitsandbytesからはCUDA環境が使えるわけですよね。
python -m bitsandbytes
を叩いて診断が通ったら安心できるな〜
通らない場合
python -m bitsandbytes
が通らない場合も経験したのですが、その1つはCUDAのバージョンがまだサポートできていない場合。
https://github.com/bitsandbytes-foundation/bitsandbytes/issues/1315#issuecomment-2288926631
We're not yet distributing a CUDA 12.6 binary,
CUDAは全然分からないのですが、python -m bitsandbytes
で診断が通らなければ
- bitsandbytesのバージョンを上げられるか
- CUDAのバージョンをbitsandbytesのサポート範囲に変えられるか
という2つの点からアプローチできることを学びました。
GPUまわりは祈るしかなかった私としては、ごくちょっとですが前進です!
終わりに
bitsandbytesは
この2つができることを知りました。
CUDAのバージョンとそれ向けのbitsandbytesのバイナリの有無という確認ポイントも収穫です
- 詳しくは ↩