Mac mini M4でローカルLLM実験メモ
API連携で様々なモデルを試してきたけれど、そろそろアプリケーションでの利用を踏まえて、ローカルLLMでデータを食べさせての振る舞いの確認をしようと思う。
Mac mini M4の環境状況
人生初のMacなので余り無理せず、ちょっとしたプログラミングができれば良いや程度のスペックです。
CPU:Apple M4
MEM:16GB
OS:macOS Sequoia(15.2)
しかし、本当に動いているの?ってくらい静音設計でびっくりしました。
んで、付属のターミナルは、zshがデフォルトで起動するみたい。
(exitでターミナルが閉じないのはどうにかできるのかしら?)
Python環境の構築
Windowsで開発してた時のように仮想環境が作成できるPythonを入れたかったので、Macでどうするか調べてたらたくさん情報があるのだけれど、一通りみて、後の周辺ソフトウェア導入を考慮したらHomeBrewとpyenvが比較的簡単そうだったので採用した。
引用元は以下のサイト。リンクが切れると困るので一部転載しておきます。
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"特権ユーザの権限が必要なため、ログインユーザ(管理者)のパスワードが聞かれる。(sudoがパスワードを求めてくる。)
PATHを通す必要があるので、以下のコマンドで~/.zshprofileにパスを設定する。
% echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/ユーザ名/.zprofile
% eval "$(/opt/homebrew/bin/brew shellenv)"brewを用いてpyenvをインストールする。
brew install pyenvpyenvの使う環境変数やらパスやらを~/.zshrcに定義する。
(環境変数設定ファイルがいくつも出てくるのは、参考にしたサイトがそれぞれ違うから。。。いずれ整理して一つのファイルにまとめないとだな。。。)
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrcpythonのインストール
pyenvでは、複数のpythonインタープリタをインストールできる。これまで開発で使ってたのが、python 3.11なのでそれを入れる。インストール可能なバージョンは、以下のコマンドで取得できる。
pyenv install --list取得したバージョンの中で3.11系で一番新しかったのが3.11.11のため以下のコマンドで該当バージョンをインストールする。
pyenv install 3.11.11標準で使用するバージョンを以下のコマンドで設定する。--versionで指定したバージョンが確認できる。
pyenv global 3.11.11
python --version開発用の仮想環境(venv)を作成する。
ライブラリセットが混在すると大変なので、MLXように仮想環境を作成する。
python -m venv myenv
source myenv/bin/activatepipを用いてMLXをインストールする。
pip install mlxMLXを実行してみる。
サンプルコードをもとにMLXを使ってローカル環境でLLMを動かしてみる。
from mlx_lm import load, generate
model, tokenizer = load("mlx-community/Meta-Llama-3-8B-Instruct-4bit")
response = generate(model, tokenizer, prompt="Are you ok?", verbose=True)初回実行は、モデルをダウンロードしてくるので時間がかかるが悪くない性能で応答してくれる。
% python hoge.py
Fetching 6 files: 100%|████████████████████████| 6/6 [00:00<00:00, 78889.73it/s]
==========
Do you need any help or support? We are here for you and we want to help in any way we can.
If you are experiencing any of these symptoms, please reach out to a trusted adult, such as a parent, teacher, or school counselor. They can provide you with support and help you find resources to manage your stress and anxiety.
Remember, you are not alone and there is help available. You are strong and capable, and you can get through this. Keep in mind that it is okay to not be okay, and it is okay to ask for help.
If you are in immediate danger or need emergency assistance, please call 911 or your local emergency number.
Remember, you are loved and you are important. You are not alone, and there is help available. Keep in mind that it is okay to not be okay, and it is okay to ask for help. You are strong and capable, and you can get through this. Keep in mind that it is okay to not be okay, and it is okay to ask for help. You are strong and capable, and you can get through this. Keep in mind that it is okay to not be okay, and it is okay to ask for help. You are strong and capable, and you can
==========
Prompt: 4 tokens, 35.843 tokens-per-sec
Generation: 256 tokens, 22.838 tokens-per-sec
Peak memory: 5.349 GB
今後は、RAGやFine-tuningを試してローカルLLMが何に使えるか試行していくつもり。


コメント