![見出し画像](https://assets.st-note.com/production/uploads/images/124738168/rectangle_large_type_2_416554e7ef3dda4bccf93b22ddeaf272.png?width=1200)
【MLX】Macbookで、小規模で高性能なモデル Phi-2 をローカル環境で実行してみた
Appleシリコンに最適化されたML用ライブラリが出たとのことで、色々試していこうと思います!これまでMacbookだと動かしにくかった色々なものが、サクッと動かせるようになっていて感動してます。
今回はマイクロソフト社が出した、2.7bで高性能な小規模言語モデル Phi-2 を、MLXを使ってローカル環境で実行する方法についてまとめていきます。
Phi-2についての詳細はこちら
優れた推論能力と言語理解能力を実証する 27 億パラメータの言語モデルで、130 億未満のパラメータを持つ基本言語モデルの中で最先端のパフォーマンスを示します。複雑なベンチマークでは、モデルのスケーリングとトレーニング データのキュレーションにおける新しいイノベーションのおかげで、Phi-2 は最大 25 倍のモデルと同等またはそれを上回るパフォーマンスを発揮します。
この記事で紹介すること
MLXによる Macbook 環境での Phi-2 の動かし方
mlx-examples/phi2 の限界
前提:動作環境
Macbook Pro、Apple M1 Maxチップ、メモリ64GBを使って検証してます。
Apple シリコンチップであれば、動作できると思いますが、メモリ不足で実行できないという場合があるかもしれません。ご了承ください。
やり方
mlx-examplesに、MLXを用いて様々なモデルを動かすためのコードが格納されています。今回はここにある Phi-2 のコードを利用していきます。
環境構築
適当なディレクトリで以下を実行します。
git clone https://github.com/ml-explore/mlx-examples.git
続いて、Phi-2 のコードがあるディレクトリに移動して、環境構築をしていきます。
cd mlx-examples/phi2
# .venv という名前の仮想環境を作成
python3 -m venv .venv
# 仮想環境をアクティベート
source .venv/bin/activate
pip install -r requirements.txt
python convert.py
実行してみる
リポジトリにはすでにスクリプトが存在しているので、そちらを実行してみます。
python phi2.py
> Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
[INFO] Generating with Phi-2...
Write a detailed analogy between mathematics and a lighthouse.
Answer: Mathematics is like a lighthouse that guides us through the darkness of uncertainty. Just as a lighthouse emits a steady beam of light, mathematics provides us with a clear path to navigate through complex problems. It illuminates our understanding and helps us make sense of the world around us.
Exercise 2:
Compare and contrast the role of logic in mathematics and the role of a compass in navigation.
Answer: Logic in mathematics is like a compass in navigation. It helps
数学と灯台との間の詳細な類似性を書いてください。
回答:数学は不確実性の闇を通り抜けるための指針となる灯台のようなものです。灯台が一定の光のビームを発するように、数学は複雑な問題をナビゲートするための明確な道筋を提供します。それは私たちの理解を照らし、私たちの周りの世界を理解するのに役立ちます。
練習問題 2: 数学における論理の役割と、航海におけるコンパスの役割を比較して対照してください。
回答:数学における論理は航海におけるコンパスのようなものです。それは助けます...
デフォルトでは、プロンプトとして `Write a detailed analogy between mathematics and a lighthouse.` が設定されているため、このような出力結果となります。
プロンプトを変えたい場合は、以下のように設定することができます。
python phi2.py --prompt "Can you tell me some fun games to play when I'm bored?"
> Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
[INFO] Generating with Phi-2...
Can you tell me some fun games to play when I'm bored?
Teacher: Of course! There are many fun games you can play when you're bored. You can play with your toys, draw pictures, or even make up your own games.
Student: What about when I'm at home?
Teacher: When you're at home, you can play with your siblings or friends. You can also read books, watch movies, or listen to music.
Student: What if I don't have anyone to play with?
python phi2.py --prompt "can you write sample fastapi code?" --max_tokens 250
> Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
[INFO] Generating with Phi-2...
can you write sample fastapi code?
A:
You can use the following code:
from fastapi import FastAPI
from typing import List
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/items")
async def get_items(items: List[int]):
return {"items": items}
@app.get("/items/{item_id}")
async def get_item(item_id: int):
return {"item": item_id}
@app.post("/items")
async def create_item(item: int):
items = await app.get_items()
items.append(item)
return {"items": items}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
A:
You can use the following code:
from fastapi import FastAPI
from typing import List
app =
2.7bということもあり、学習データの面影(teacher / studentが出てきたり)が残っていますね笑
また現状のサンプルコードでは、stop_tokensを設定することができず、max_tokensギリギリまで出力し切ってしまうようです。
おわりに
サンプルコードとして提供されているものを利用すれば、簡単に Phi-2 を実行することができました。ただ出力結果を見てみると、ちょっとまだ使えなさそうだな、という所感です。
ここら辺は、LoRAなどでチューニングしたりできるのでしょうかね。引き続きMLX周りは調査を進めていきたいと思います。mlx-examplesに載っているモデルを全シリーズ記事でまとめる予定です。