0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「Qwen2.5-VL」の量子化された MLX版のモデルと「MLX-VLM」を使ったローカルLLM(Image to Text、M4 Mac mini を利用)

Last updated at Posted at 2025-02-01

はじめに

↓こちらに関する記事です。

タイトルにも書いている「Qwen2.5-VL」のモデル(量子化された MLX版のうちの 1つ)と「MLX-VLM」を使ったローカルLLM、処理的には Image to Text になる話です。

MLX-VLM を使った Image to Text

MLX-VLM を使った Image to Text は、前に Llama-3.2-11B-Vision-Instruct-4bit の MLX対応版(mlx-community/Llama-3.2-11B-Vision-Instruct-4bit)を使ったお試しをやっていたことがありました。

その時の話は以下の記事に書いています。

●「Llama-3.2-11B-Vision-Instruct-4bit」と「MLX-VLM」を使ったローカルLLM + 画像入力を M4 Mac mini で試した時のメモ - Qiita
 https://qiita.com/youtoy/items/90b80eb1a3b3cac4c184

そして前に行った時の結果は以下です。

ちなみに、今回のお試しで用いる入力画像は、前に使ったのと同じ画像(上記で掲載している画像)になります。
しかし今回は、モデルとして「mlx-community/Qwen2.5-VL-7B-Instruct-8bit」を使ってみます。

今回用いるモデルの元になっている Qwen2.5-VL は、最近リリースされたばかりのモデルです。

基本的な流れ

今回のお試しのやり方は、当初の想定では「mlx-community/Llama-3.2-11B-Vision-Instruct-4bit」を試した時の手順でできると思っていました(前回も今回も、どちらもモデルが MLX-VLM に対応しているものであったため)。

対応が必要なのは、モデル名を「mlx-community/Qwen2.5-VL-7B-Instruct-8bit」のものに変えるだけ、という想定でした。

最終的な流れ

最終的には、前に「MLX-VLM」を試した流れとほぼ同じで、途中に以下を加えた手順で成功させられました。

pip install git+https://github.com/huggingface/transformers

ざっくり書くと、こんな感じです。

  • 前提
    • Python の環境(自分は仮想環境を準備)、MLX-VLM はセットアップ済み
    • MLX版のモデルを使うので Apple Silicon の Mac を利用(自分は M4 Mac mini)
    • マシンでローカルLLM用に使えるメモリが 9GB強以上(今回使うモデルのサイズの関係
  • 手順
    1. Python の 3.12系で MLX-VLM のバージョン 0.1.12 をインストール(※ Python のバージョンに関して、前に作った環境を今回は流用しており、前にセットアップした際は Python 3.13系を使って試したら途中でエラーが出た)
    2. 上記の transformers のインストール(デフォルトで入るものと異なるバージョンを上書きする形)
    3. PyTorch のインストール( pip install torch を実行)
    4. MLX-VLM のコマンドを実行

得られた出力に関する画像・テキスト

得られた出力に関する画像・テキストを掲載してみます。

2025-02-01_23-26-39.jpg

The image depicts a dramatic scene featuring two dragons engaged in a fierce battle. The dragon on the left is blue and appears to be breathing a stream of icy blue energy, while the dragon on the right is red and is breathing a stream of fiery orange energy. The background is a dynamic sky with swirling clouds and lightning bolts, adding to the intensity of the scene. The dragons are positioned on opposite sides of a mountain, with the blue dragon on the left and the red dragon on the right. The overall atmosphere is one of conflict and power, with the two dragons locked in a battle of elements.
==========
Prompt: 234 tokens, 131.349 tokens-per-sec
Generation: 121 tokens, 13.426 tokens-per-sec
Peak memory: 9.142 GB

【追記】PyTorch のインストール

当初、出力の中で「None of PyTorch, TensorFlow >= 2.0, or Flax have been found.」という内容が出ていました。

それで、上記手順には追記済みですが PyTorch のインストールも行っておくのが良さそうでした。

対応後の状態

PyTorch をインストール後は、以下のように先ほどのメッセージがでなくなりm

image.png

【追記2】Python のプログラムでも試してみた

コマンドでの利用だけでなく、Python のプログラムでの利用も少しだけ試してみました。

2025-02-02_00-05-40.jpg

エラー対応の話

以下は、上記を試していく中でエラーに対応した話など、補足となる内容です。

今回のお試しを進める中で 2回エラーが発生していたのですが、その内容をメモとして書き残してみます。

エラーが出た話1

最初に試そうとした時は、以下のエラーが出ていました。

image.png

エラーの原因は、この時点で「MLX-VLM」が「Qwen2.5-VL」に対応していない状態だったためです。

上記のエラーの解決

その後、以下のように MLX-VLM がアップデートされ、Qwen2.5-VL への対応が追加されました。

これで以下のコマンドを実行することにより、Qwen2.5-VL に対応した MLX-VLM へとアップデートできる状態になりました。

pip install -U mlx-vlm

実際、以下のように 0.1.12 へのアップデートは無事成功し、上で書いていたエラーは解消しました。

2025-01-30_23-09-36.jpg

しかしその後、もう 1つエラーが発生しました。

エラーが出た話2

次に発生していたエラーは以下のものです。

2025-01-30_23-15-26.jpg

エラーの原因

上記の原因について GitHub に Issue にあがってそう、と思って見てみたところ、以下の内容がでてきました。

●Qwen2.5-VL failing · Issue #192 · Blaizzy/mlx-vlm
 https://github.com/Blaizzy/mlx-vlm/issues/192

そして、以下の記載を見つけました。

2025-02-01_23-18-46.jpg

エラーの解決法

どうやら、transformers のバージョンが原因のようです。

また、uvコマンドを使って対処した例の話と組み合わせて考えると、最低限 uvコマンドを使った例の中の transformers関連部分の対応が必要そうでした。

それで、上で書いていた pip install git+https://github.com/huggingface/transformers を実行するというのを加えてみました。
他は対応が必要なものが特になく、これで無事に冒頭に掲載していたような結果を得られました。

「Qwen2.5-VL」の量子化された MLX版のモデルの一部抜粋

最後に、「Qwen2.5-VL」の量子化された MLX版のモデルで、自分が試そうと思ったモデルを列挙してみます。

どれも自分の M4 Mac mini のメモリで対応可能なものになります(※ 搭載されているメモリは 24GB で、ローカルLLM用にデフォルトで使用されるのが 16GB)。

●mlx-community/Qwen2.5-VL-7B-Instruct-8bit at main
 https://huggingface.co/mlx-community/Qwen2.5-VL-7B-Instruct-8bit/tree/main

●mlx-community/Qwen2.5-VL-7B-Instruct-6bit at main
 https://huggingface.co/mlx-community/Qwen2.5-VL-7B-Instruct-6bit/tree/main

●mlx-community/Qwen2.5-VL-3B-Instruct-bf16 at main
 https://huggingface.co/mlx-community/Qwen2.5-VL-3B-Instruct-bf16/tree/main

●mlx-community/Qwen2.5-VL-3B-Instruct-8bit at main
 https://huggingface.co/mlx-community/Qwen2.5-VL-3B-Instruct-8bit/tree/main

●mlx-community/Qwen2.5-VL-3B-Instruct-6bit at main
 https://huggingface.co/mlx-community/Qwen2.5-VL-3B-Instruct-6bit/tree/main

●mlx-community/Qwen2.5-VL-3B-Instruct-3bit at main
 https://huggingface.co/mlx-community/Qwen2.5-VL-3B-Instruct-3bit/tree/main

この中で、モデルサイズの大きめのものを 1つ選んだという流れでした。

余談

今回、MLX-VLM を使ったお試しでしたが、以下の記事で使っている MLX-LM も便利です。

●MLX でローカルLLM:「mlx-community/phi-4-4bit」を試す(M4 Mac mini、mlx-lm を利用) - Qiita
 https://qiita.com/youtoy/items/a12ee071dd8290fc0715

●mlx-community の「Llama-3-ELYZA-JP-8B-8bit」「Swallow-13b-instruct-v0.1-8bit」を mlx-lm で試す - Qiita
 https://qiita.com/youtoy/items/973910dada605ab7e259

Image to Text に MLX-VLM を使い、Text to Text に MLX-LM を使っているような使い分けになっています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?