llama.cpp で CPU で Qwen2-VL を試す(72B 0.4 toks/sec)
llama.cpp に qwen2-vl 対応が入りました.
llava example を拡張して対応しているため, 実装がちょっとややこしいです(vision encoder が clip 扱いになっているなど)
実行には, llm part と vision encoder part(mmproj) と二つモデルを指定する必要があります
Qwen2-VL では Vision Encoder はどれも同じ(Transformer アーキ. サイズは 600M くらい)で, LLM 部分で大きさが変わるので, mmproj は同じものを使えそうですが, 一応それぞれの LLM の大きさに対応するのを使わないとダメなようです.
環境
- Ryzen 3950X
- 128 GB mem
- OpenBLAS 有効
2B
Alicia Vikander 先生の画像を与えてみます.
画像サイズは 1000x1056
./llama-qwen2vl-cli -m ../../models/Qwen2-VL-2B-Instruct-f16.gguf --mmproj ../../models/mmproj-Qwen2-VL-2B-Instruct-f32.gguf --image ../../images/alicia.jpg -p "Describe the image"
vision encoder で 30 秒くらいかかります.
encode_image_with_clip: image encoded in 32568.78 ms by CLIP ( 23.81 ms per image patch)
The image shows a person wearing a light pink, sleeveless dress with a sweetheart neckline. They have long, wavy brown hair. The person is also wearing a necklace with red gemstones and matching earrings. The background features a backdrop with text, although the text is not fully legible in the image.
ふむ!
text 生成は 9 toks/sec でいい感じです.
7B
./llama-qwen2vl-cli -m ../../models/Qwen2-VL-7B-Instruct-f16.gguf --mmproj ../../models/mmproj-Qwen2-VL-7B-Instruct-f32.gguf --image ../../images/alicia.jpg -p "Describe the image"
f16 版使います(15 GB + mmproj 2 GB = 18 GB くらい)
画像の encode に 2B と同じく 30 秒くらいかかります.
encode_image_with_clip: image encoded in 32593.30 ms by CLIP ( 23.83 ms per image patch)
その後 30 秒 ~ 1 分くらい待つと, text が出てきます.
The image shows a woman with long, wavy hair styled in loose waves. She is wearing a sleeveless, light pink dress with intricate lace detailing. The dress features a sweet heart neckline and is fitted at the bodice, accentuating her figure. She is accessorized with a matching pink necklace and earrings that have a circular design with small jewels. The background appears to be a white or light-colored backdrop with some text and logos, suggesting that this might be a promotional or event photograph.
ほほー.
でも 2 tokens/sec くらいでした. ちょっと遅いカナ...
まあ Ryzen DDR4 のメモリ速度はよくて 20GB/s くらいなので, 妥当なところでしょうかね...
72B
f16 は 128 GB に入りきらないので, q8 使いました. 総メモリ 75 GB くらいです.
./llama-qwen2vl-cli -m ../../models/Qwen2-VL-72B-Instruct-Q8_0-00001-of-00002.gguf --mmproj ../../models/mmproj-Qwen2-VL-72B-Instruct-f32.gguf --image ../../images/alicia.jpg -p "Describe the image"
とくに意図はないですが mmproj は f32 を使いました(llama.cpp(ggml) では内部処理(内積計算など)は f16 がメインっぽいので, f16 も f32 もあまり違いはなさそう).
vision encoder は同じく 30 秒ほど.
encode_image_with_clip: image encoded in 32694.19 ms by CLIP ( 23.90 ms per image patch)
5 分ほど待って, text が出てきます.
The image shows a person wearing an elegant, light pink, sleeveless dress with delicate embroidery. They are accessorized with a matching necklace and earrings that appear to be made of precious stones. The background features a sign that reads "RESORT DUBAI."
背景の文字を推論できているのは良き!
llama_perf_context_print: load time = 423797.48 ms
llama_perf_context_print: prompt eval time = 383244.96 ms / 1392 tokens ( 275.32 ms per token, 3.63 tokens per second)
llama_perf_context_print: eval time = 133094.21 ms / 52 runs ( 2559.50 ms per token, 0.39 tokens per second)
llama_perf_context_print: total time = 559463.27 ms / 1444 tokens
0.4 toks/sec とめちゃ遅だけど... 😅
Q4 とかにしたら二倍は早くなるだろうけど, 精度は落ちそう.
Q6 あたりがいいかしらん?
とりままとめ
CPU だとやはりメモリ律速. DDR5 にしたとして(だいたい 50 GB/s), Q8 72B がよくて 1 toks/sec と推測される.
Mac の場合は mlx を使うのも手か. 72B で Q5 くらいなら mac mini M4 64 GB でも動きそう.
GPU 対応
clip(vision encoder) はまだ GPU 対応していません.
LLM part のみ.
3070 8GB(bandwidth 448 GB/s)で少し試しました
- 2B Q8 が 90 toks/sec
- 7B Q8(ngl 26) が 17 toks/sec
- 72B Q8(ngl 4) が 0.41 toks/sec
GPU VRAM にフィットする場合は CPU(DDR4 20 GB/s)の 10 倍は出る感じですかね.
72B は GPU 処理できるレイヤー少ないのであまり速度改善はなし.
Vision encoder(CLIP)も GPU で動くようになったら, 3 秒くらいで encode できると思われます.
TODO
- llama.cpp での qwen2-vl 実装はややこしいので, 自前の C++ VLM(vision-language.cpp . 絶賛開発中)で Qwen2-VL model を動かす
- GPU full offload を試す
* 5090 x 3(96 GB. メモリ帯域 1.5 TB/s) で Q8 72B の full GPU 実行試す.- 3090(24GB VRAM)でも 3 枚ならほぼ Q8 72B いけそ?
- AI エージェントヴームの波に乗り https://note.com/breer/n/nda8ba5c62822 , OCR エージェントやチャート解析トレーディング bot, 画像認識オートメーションエージェント作ってがっぽがっぽしたい.
Discussion