自分用の覚え書きです。
llama.cpp
C/C++ で書かれた Meta 社の LLaMa モデル用のインターフェイス。
GitHub – ggerganov/llama.cpp: LLM inference in C/C++
https://github.com/ggerganov/llama.cpp
導入
Ubuntu 環境は次のページが参考になる。
Ubuntu 22.04 : Llama インストールと設定 : Server World
https://www.server-world.info/query?os=Ubuntu_22.04&p=llama
いろいろと検索してみると、割と Windows で動かしている人が多い印象。WSL さまさまだなぁ。
LMDE の頃、あれだけ苦労していた CUDA/cuDNN の Python まわりの環境導入はコマンド一発。NVIDIA GeForce RTX 3060 Ti を使っているので、
nvidia-smi
で状況確認。そして
pip3 install llama-cpp-python[server]
の部分でコケる。曰く、
ERROR: Could not build wheels for llama-cpp-python, which is required to install pyproject.toml-based projects
との事なので、
pip3 install pyproject-toml
するも改善しない。
調べていくと、llama-cpp-python にはいくつものバージョンがある模様。活発にアプデされていて、依存関係もバージョンごとに若干変わっているみたい。
こちらやこちらで言及されている Wheel 置場が次である。
- https://abetlen.github.io/llama-cpp-python/whl/cu122/
- https://jllllll.github.io/llama-cpp-python-cuBLAS-wheels/AVX2/
上記から拝借する。
pip3 install llama-cpp-python[server] --prefer-binary --extra-index-url=https://jllllll.github.io/llama-cpp-python-cuBLAS-wheels/AVX2/cu117
といった具合に導入。URL の末尾が cu117 だが上記配布の最新である cu122 でも可能。
一部のモデルを使用してサーバーを起動する際に
error loading model: unknown model architecture: 'qwen2'
と怒られる。やっぱり最新のを持ってくる必要があるみたい。よく考えたらこの Ubuntu、gcc-11 と g++-11 が入ってなかった。
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install gcc-11 g++-11
「MS Visual Studio の toolchain を入れろ」みたいな話を目にしてスルーしてた…。
気を取り直して、pip uninstall して install llama-cpp-python[server] しても同じエラー。迷走していたら、(llama) 状態のシェルから抜けて、
source llama/bin/activate
し、もう一度 activate 後に pip install したら通った。
モデル調達と起動
こちらから llama-2-13b-chat.Q4_K_M.gguf をダウンロードして、llama_cpp.server を起動する。
python3 -m llama_cpp.server --model ./llama-2-13b-chat.Q4_K_M.gguf --host 0.0.0.0 --port 8000
サーバーがリスン状態に入れば成功。
curl -s -XPOST -H 'Content-Type: application/json' localhost:8000/v1/chat/completions -d '{"messages": [{"role": "user", "content": "日本語で自己紹介してください。"}]}' | jq | grep "content"
JSON をぶん投げて回答を得る。結果は次。
"content": " Konnichiwa! Ohayou gozaimasu! *bows*\n\nMy name is (insert name here), and I am a (insert occupation or student status here) from (insert hometown or current location here). *smiles* I am excited to be here and learn more about the community. *nodding*\n\nI enjoy (insert hobbies or interests here) in my free time, and I am always eager to meet new people and make friends. *winks* If you have any questions or want to chat, feel free to ask me! *smiling widely*\n\nOh, and I almost forgot to mention that I love (insert favorite food here). *drools* It's so delicious! *noms on air* Do you have any recommendations for where to find the best (insert food here) in the area? *bouncy eyes*\n\nAnyway, it was nice meeting you! *waves goodbye* Hopefully, we can hang out again soon! *smiling*",
すごい (insert here) !
Hugging Face
huggingface-cli が使える。名前のとおり、CLI インターフェイス。
pip install --upgrade huggingface_hub
ここでアクセストークンを作成する。ダウンロードするだけなら Read 権限があればよい。
huggingface-cli login
トークンを聞いてくるので入力。
huggingface-cli download --repo-type model mmnga/cyberagent-DeepSeek-R1-Distill-Qwen-14B-Japanese-gguf
ただこれ、ワーキングディレクトリにダウンロードするかと思いきや ~/.cache/huggingface/hub 配下に保存する。/home を 512GB の SSD にマウントしているので 180GB とか入れられるとそこそこ困る。移動してシンボリックリンクを貼る。
参考追記
Cline+ローカル版DeepSeek R1でAIコーディングを使い放題にする(高スペックマシン向け)|しぴちゃん
https://note.com/cppp_cpchan/n/n92c7795f5939
WSL2でDeepSeek-R1-Distill-Qwen-32B-Japaneseをllama.cppで試してみる|noguchi-shoji
https://note.com/ngc_shj/n/nf09197c0a409
mmnga/cyberagent-DeepSeek-R1-Distill-Qwen-14B-Japanese-gguf at main
https://huggingface.co/mmnga/cyberagent-DeepSeek-R1-Distill-Qwen-14B-Japanese-gguf/tree/main
さらに追記
どうも GPU が有効になっていなかったらしくまあ遅い。古いドライバ nvidia-driver-535 と CUDA11.5 をぶち消して nvidia-driver-570 と CUDA11.8を入れ直した。最低でもCUDA11.7以降が必要みたい。
source ~/llama/bin/activate
で (llama) 環境に入ってから、llama-cpp-python もインストールし直し。
CMAKE_ARGS="-DLLAMA_CUDA=on" FORCE_CMAKE=1 pip install llama-cpp-python --upgrade --force-reinstall --no-cache-dir
起動。
python3 -m llama_cpp.server –host 0.0.0.0 –port 8000 –n_gpu_layers -1 –model PATH-TO-MODEL.guff
起動完了までに流れるログに
…
load_tensors: layer 0 assigned to device CUDA0
load_tensors: layer 1 assigned to device CUDA0
load_tensors: layer 2 assigned to device CUDA0
load_tensors: layer 3 assigned to device CUDA0
load_tensors: layer 4 assigned to device CUDA0
load_tensors: layer 5 assigned to device CUDA0
load_tensors: layer 6 assigned to device CUDA0
load_tensors: layer 7 assigned to device CUDA0
…
とある。CUDA が使えてなければ CPU になる箇所なので、これで多分OK。
DeepSeek-R1-Distill-Qwen-7B-Q6_K-00001-of-00001.gguf 動確。
cyberagent-DeepSeek-R1-Distill-Qwen-14B-Japanese-Q2_K.gguf 動確。