はじめに
以下でポストした、「Llama-3.2-11B-Vision-Instruct-4bit」と「MLX-VLM」を使ったローカルLLM に関する記事です。
具体的には環境をセットアップして、以下に記載の内容を試してみた感じなのですが、その手順のメモを残してみます。
●mlx-community/Llama-3.2-11B-Vision-Instruct-4bit · Hugging Face
https://huggingface.co/mlx-community/Llama-3.2-11B-Vision-Instruct-4bit
使われているコマンド的に、以下の「MLX-VLM」を活用したもののようです。
●Blaizzy/mlx-vlm: MLX-VLM is a package for inference and fine-tuning of Vision Language Models (VLMs) on your Mac using MLX.
https://github.com/Blaizzy/mlx-vlm
お試しで使ったマシン
今回のお試しで使ったマシンは、以下の記事を書いた時にも使っていた M4 の Mac mini です。
●M4 Mac mini で mlx-whisper を試す(pyenv・venv を使った環境準備も)【Python-4】 - Qiita
https://qiita.com/youtoy/items/c5b204c811f5d8b4e5fe
その時にも活用していた MLX も関連しています。
それまで所有していた Mac のプロセッサがどれも Intel なやつだったところで、以下の M4 の Mac mini を入手したため、それ以降に MLX を試し始めました。
今回試した内容
要点のみ書いたもの
細かな説明は省き、今回は手順の要点のみ抜粋したものを残します。
前提となる環境
今回使った Mac mini では、既に Python の 3.13系・3.12系を使えるようにしていました(※ pyenv を利用)
手順
当初、Python の 3.13系を使って試したら途中でエラーが出たようだったので、3.12系を使うようにしてセットアップを行いました。
以下は 3.12系で仮想環境を用意した時のコマンドのメモです(利用する Python のバージョンの変更は、今回のお試し用のフォルダ内のみに限定する形で進めました)。
pyenv local 3.12
python -m venv myenv
source myenv/bin/activate
上記で仮想環境の作成・アクティベートを行ってから、その中で pipコマンドを使い「MLX-VLM」を準備しました。
pip install mlx-vlm
以下の画像は、ここまでの流れを実行した画面のキャプチャです。
【追記】
コマンド実行時に「None of PyTorch, TensorFlow >= 2.0, or Flax have been found.」という出力が出ていました。
その部分の PyTorch の対応を行ってなくても動作はしていましたが、以下を実行して PyTorch を入れておおくと良さそうです。
pip install torch
画像の内容をテキストで得る
ここまでの準備をしていたディレクトリに、入力画像となるファイルを「dragon.jpg」という名前で置きました。あとは、以下のコマンドの実行を行ったのみです。
python -m mlx_vlm.generate --model mlx-community/Llama-3.2-11B-Vision-Instruct-4bit --image 'dragon.jpg' --prompt 'describe this image'
少し時間がかかった感じでしたが、コマンドの実行結果として、冒頭に掲載していたように以下のテキストを得られました。
The image depicts two dragons facing each other, with one dragon breathing fire and the other breathing ice. The dragon on the left is blue and has its wings spread wide, while the dragon on the right is red and has its wings folded against its back. The dragon on the left is breathing a stream of ice, while the dragon on the right is breathing a stream of fire. The background of the image is a mountain range, with lightning bolts illuminating the sky. The overall atmosphere of the image is one of intense conflict and power.
==========
Prompt: 14 tokens, 0.934 tokens-per-sec
Generation: 109 tokens, 2.654 tokens-per-sec
Peak memory: 16.481 GB
入力画像の内容を説明したテキストが得られているかと思います。
余談
今回のお試しで、当初は以下の「Qwen2-VL」の MLX版を使おうとしていました。
●QwenLM/Qwen2-VL: Qwen2-VL is the multimodal large language model series developed by Qwen team, Alibaba Cloud.
https://github.com/QwenLM/Qwen2-VL
MLX対応のものを、Hugging Face で以下のように検索して探し、試す対象を決めていました。
●mlx-community (MLX Community)
https://huggingface.co/mlx-community
●Full Text Search - Hugging Face
https://huggingface.co/search/full-text?q=mlx+Qwen2-VL&type=model
具体的には以下を選んで試していました。
●mlx-community/Qwen2-VL-2B-Instruct-4bit · Hugging Face
https://huggingface.co/mlx-community/Qwen2-VL-2B-Instruct-4bit
それでエラーが出たので、今回の Llama 3.2 のビジョンモデルも試してみて、Llama のほうはうまくいったという状況でした。
Qwen2-VL のほうは、引き続き試していければと思っています。