Llama3(Hugging Face)のモデルの重みをmlxに変換し、Apple Silicon Macで動かす
1. GitHubリポジトリのクローン
git clone https://huggingface.co/pfnet/Llama3-Preferred-MedSwallow-70B
で任意の場所にクローン。
2. モデルの重みファイルをダウンロード
hf_transferを使うと高速にダウンロードできます。デフォルトの状態でダウンロードすると、70Bクラスのモデルファイルのダウンロードにはかなり時間がかかり、何回かタイムアウトを経験することになります。
export HF_HUB_ENABLE_HF_TRANSFER=1
pip install hf_transfer
huggingface-cli download pfnet/Llama3-Preferred-MedSwallow-70B --include "*.safetensors"
ダウンロードが終わったので、容量を確認してみます。
du -sh models--pfnet--Llama3-Preferred-MedSwallow-70B/
132G models--pfnet--Llama3-Preferred-MedSwallow-70B/
132GBもあるんですね。
3. safetensorsファイルをコピーあるいは移動
ここで、先ほどgit cloneを実行したディレクトリ上dえ
mlx_lm.convert --hf-path ./Llama3-Preferred-MedSwallow-70B --mlx-path Llama3-Preferred-MedSwallow-70B-mlx --quantize
を実行すると、
FileNotFoundError: No safetensors found in Llama3-Preferred-MedSwallow-70B
というエラーが出ます。Macなら
/Users/yukik/.cache/huggingface/hub/models--pfnet--Llama3-Preferred-MedSwallow-70B/snapshots
の中を見ると、以下のように*.safetensors
のファイルがたくさんあります。
model-00001-of-00030.safetensors@ model-00004-of-00030.safetensors@ model-00007-of-00030.safetensors@ model-00012-of-00030.safetensors@ model-00016-of-00030.safetensors@ model-00020-of-00030.safetensors@
model-00002-of-00030.safetensors@ model-00005-of-00030.safetensors@ model-00008-of-00030.safetensors@ model-00013-of-00030.safetensors@ model-00017-of-00030.safetensors@ model-00022-of-00030.safetensors@
model-00003-of-00030.safetensors@ model-00006-of-00030.safetensors@ model-00010-of-00030.safetensors@ model-00014-of-00030.safetensors@ model-00018-of-00030.safetensors@
先ほどの*.safetensors
のファイルをgit cloneしたフォルダ内にコピーあるいは移動してあげます。今回はコピーします。
cd Llama3-Preferred-MedSwallow-70B
cp -r /Users/yukik/.cache/huggingface/hub/models--pfnet--Llama3-Preferred-MedSwallow-70B/snapshots ./
mv snapshots/9f0de3173362c4013a175da7ee41dfb19dba42d5/* ./
rm -r snapshots/
4. mlx_lm.convertを実行
満を辞して
mlx_lm.convert --hf-path ./Llama3-Preferred-MedSwallow-70B --mlx-path Llama3-Preferred-MedSwallow-70B-mlx --quantize --q-bits 8
とすると、8bit量子化されたモデルが作成されます。
これは
du -sh Llama3-Preferred-MedSwallow-70B-mlx/
70G Llama3-Preferred-MedSwallow-70B-mlx/
70GBで、元の132GBよりはまだマシな大きさです。
Discussion