![見出し画像](https://assets.st-note.com/production/uploads/images/124757706/rectangle_large_type_2_fec48124153276c6f1f98ab266d1bda8.png?width=1200)
M3 MacbookProでフレームワーク「MLX」をつかってローカルLLM Llama-7b
上の記事で紹介されているMLXを試してます。
昨日の記事で、conda環境 mlx をつくって、examplesまでmacbookのなかにとりこみました。
その続きとなります。
こちらですね。
ドキュメントは以下ですが、すっとばします。
conda activate mlx でconda環境を立ち上げて、LLamaのdirectoryまで移動します。
Last login: Sat Dec 16 16:15:29 on ttys001
~ % conda activate mlx
(mlx) ~ % ls
AI Documents Library Pictures
Applications Downloads Movies Public
Desktop Dropbox Music miniconda3
(mlx) ~ % cd ai
(mlx) ai % ls
mlx
(mlx) ai % cd mlx
(mlx) mlx % ls
bin include man share
conda-meta lib mlx-examples ssl
(mlx) mlx % cd mlx-examples
(mlx) mlx-examples % ls
CODE_OF_CONDUCT.md gcn phi2
CONTRIBUTING.md llama stable_diffusion
LICENSE lora transformer_lm
README.md mistral whisper
bert mixtral
cifar mnist
(mlx) mlx-examples % cd llama
(mlx) llama %
ここで、下記のコードを実行
pip install -r requirements.txt
conversionするステップは飛ばして、できてるモデルをダウンロードすることにします。
上から以下のモデルを選択すると、実行コードも書かれていました。
最初に関係ありそうな以下のコードのみをまず実行。
pip install huggingface_hub hf_transfer
このコマンドは二つのPythonパッケージ、huggingface_hubとhf_transferをインストールします。huggingface_hubはHugging Faceのモデルハブと対話するためのライブラリで、AIモデルやデータセットをHugging Faceのモデルハブにアップロードしたり、そこからダウンロードするために使用されます。hf_transferについては2021年の知識カットオフ時点では詳細は不明ですが、名前からするとHugging Face関連のデータ転送ツールである可能性があります。
次にモデルをダウンロード
# Download model
export HF_HUB_ENABLE_HF_TRANSFER=1
huggingface-cli download --local-dir Llama-2-7b-chat-mlx mlx-llama/Llama-2-7b-chat-mlx
このスクリプトはHugging Face Hubからモデルをダウンロードするためのコマンドを含んでいます。ここでは環境変数の設定とCLI(コマンドラインインターフェース)ツールを使用してのダウンロードが行われています。それぞれの行の説明は以下の通りです。
1. `export HF_HUB_ENABLE_HF_TRANSFER=1`
この行では、環境変数`HF_HUB_ENABLE_HF_TRANSFER`を`1`に設定しています。これはおそらく`huggingface_hub`ライブラリの特定の機能を有効にするためのフラグです。`hf_transfer`というツールや機能が関係していると思われますが、これは2021年の情報では詳細が不明です。この環境変数が具体的にどのような影響を与えるかは、`huggingface_hub`のドキュメントやその時点でのリリースノートを参照する必要があります。
2. `huggingface-cli download --local-dir Llama-2-7b-chat-mlx mlx-llama/Llama-2-7b-chat-mlx`
この行は`huggingface-cli`というコマンドラインツールを使用して、Hugging Face Hubにホストされている`mlx-llama/Llama-2-7b-chat-mlx`という特定のモデルをダウンロードするためのコマンドです。`--local-dir Llama-2-7b-chat-mlx`オプションはダウンロードしたモデルをローカルマシンの`Llama-2-7b-chat-mlx`というディレクトリに保存することを指示しています。これにより、Hugging Face Hubから指定されたモデルがローカルにダウンロードされます。
要するに、このスクリプトは環境変数を設定してから`huggingface-cli`を使って特定のモデルをダウンロードしています。これにより、モデルを使用するための準備が整います。
環境設定の中身はわからなかったですが、このまま実行しました。
環境設定は通ったみたいです。
ダウンロードはエラーがでました。
エラーをコピペしてChatGPTに尋ねたところ、.cache/huggingface/hub に書き込み権限がないということでした。それで、以下のように書き込み権限をsudoで与えて、再度実行したら、ダウンロードが開始されました。
sudo chown -R 自分のユーザー名 /Users/自分のユーザー名/.cache/huggingface/hub
下記を。llamaのdirectoryで実行したら、走りました。
python llama.py Llama-2-7b-chat-mlx/ Llama-2-7b-chat-mlx/tokenizer.model "My name is "
(mlx) llama % python llama.py Llama-2-7b-chat-mlx/ Llama-2-7b-chat-mlx/tokenizer.model "My name is "
[INFO] Loading model from disk.
Press enter to start generation
------
�� ��明 (Yu Hui Ming) and I am a researcher at the University of California, Los Angeles (UCLA). My research interests are primarily in the field of cognitive science, with a focus on how people perceive and understand time, as well as how emotions and cognitive biases influence time perception.
In my research, I use a combination of behavioral experiments and brain imaging techniques, such as functional magnetic reson
------
[INFO] Prompt processing: 0.459 s
[INFO] Full generation: 4.892 s
(mlx) llama %
とか
(mlx) llama % python llama.py Llama-2-7b-chat-mlx/ Llama-2-7b-chat-mlx/tokenizer.model "Hello "
[INFO] Loading model from disk.
Press enter to start generation
------
2019! What a wonderful thing it is to be alive and to have another chance to experience all the beauty and wonder that life has to offer. Here are some of the things I am looking forward to in the new year:
1. Travel: I love exploring new places and experiencing different cultures. This year, I am planning to visit some of the places I have always wanted to see, such as Japan, New Zealand, and Iceland.
2
------
[INFO] Prompt processing: 0.269 s
[INFO] Full generation: 4.684 s
(mlx) llama %
とか
(mlx) llama % python llama.py Llama-2-7b-chat-mlx/ Llama-2-7b-chat-mlx/tokenizer.model "こんにちは"
[INFO] Loading model from disk.
Press enter to start generation
------
。
I am a Japanese student studying abroad in the United States. I am currently taking an English course to improve my language skills. I am interested in learning more about American culture and society, and I hope to make many new friends while I am here. I enjoy playing sports, watching movies, and listening to music in my free time. I am excited to experience a new culture and make many new memories while I am here.sammie
selena
------
[INFO] Prompt processing: 0.285 s
[INFO] Full generation: 4.697 s
(mlx) llama %
こんな感じでした。
(mlx) llama % python llama.py Llama-2-7b-chat-mlx/ Llama-2-7b-chat-mlx/tokenizer.model "tell me a joke"
[INFO] Loading model from disk.
Press enter to start generation
------
, i'm feeling down
I'm so sorry to hear that you're feeling down. Here's a joke that might cheer you up:
Why don't scientists trust atoms?
Because they make up everything!
I hope that made you smile! If you want to talk about what's bothering you, I'm here to listen.tell me a joke, i'm feeling down
I'm
------
[INFO] Prompt processing: 0.278 s
[INFO] Full generation: 4.697 s
(mlx) llama %
ヘルプ画面は以下のようでした。
(mlx) llama % python llama.py --help
usage: llama.py [-h] [--few-shot] [--num-tokens NUM_TOKENS]
[--write-every WRITE_EVERY] [--temp TEMP] [--seed SEED]
model tokenizer prompt
Llama inference script
positional arguments:
model Path to the model directory containing the MLX weights
tokenizer The sentencepiece tokenizer
prompt The message to be processed by the model
options:
-h, --help show this help message and exit
--few-shot Read a few shot prompt from a file (as in
`sample_prompt.txt`).
--num-tokens NUM_TOKENS, -n NUM_TOKENS
How many tokens to generate
--write-every WRITE_EVERY
After how many tokens to detokenize
--temp TEMP The sampling temperature
--seed SEED The PRNG seed
(mlx) llama %
few shotの例があったので、下記を実行すると、Q & A形式でした。
python llama.py --few-shot Llama-2-7b-chat-mlx/ Llama-2-7b-chat-mlx/tokenizer.model sample_prompt.txt
python llama.py --few-shot Llama-2-7b-chat-mlx/ Llama-2-7b-chat-mlx/tokenizer.model sample_prompt.txt
ctrl + C で終了しました。
とりあえず、こんな感じでどうにか実行できました。
残念ながら、chat形式でやり取りする方法を私は見つけられませんでした。
いいなと思ったら応援しよう!
![Lucas](https://assets.st-note.com/production/uploads/images/107879020/profile_1a18442658444d51682a0e9f99ec2cbd.jpeg?width=600&crop=1:1,smart)