マイクロソフトが発表した小型言語モデルのPhi-3からモデルが公開されているPhi-3-miniをローカルPCのllama.cppを使って動かしてみました。
検証環境
OS: Ubuntu 24.04
CPU: AMD FX-6300
Mem: 32GB
GPU: NVIDIA T1000 8GB
モデルをダウンロード
HuggingFaceのmicrosoft's CollectionsページのPhi-3にモデルリストがあります。
この中から、Phi-3-mini-4k-instruct-ggufをクリックして
量子化の手法の違いで、q4とfp16のモデルがあります。今回は、2.2GBと小さいPhi-3-mini-4k-instruct-q4.gguf
をダウンロードしました。
モデルについてのメモランダム
モデル名のinstruct
とかq4
とか意味を知りたかったのでChatGPTに聞いてみました。
instructが付いているモデル
ユーザーからの指示に基づいて応答するように設計されています。例えば、文章の要約、質問に対する答え、コードの生成など、明確な目的を持つタスクでの使用が想定されています。
instructが付いていないモデル
一般的な言語理解や生成に焦点を当てた訓練が行われています。これらのモデルは、広範な応用が可能であり、特定の指示に従うよりも、自然言語の理解や会話の流れを重視するタスクで有効です。
q4とか付いてるモデル
量子化
という手法で必要メモリを減らしたモデル(らしいです)
今回のphi-3 mini は、ダウンロードページによるとQ4_K_Mという手法なんだと。
f16が付いてるモデル
実数の精度を落としたやつ。8ビットCPUの頃は16ビット実数で演算したよね〜
拡張子がggufのやつ_
llama.cppの新しいモデルフォーマット。
拡張子がggmlのやつ_
llama.cppの古いフォーマット。変換してggufにする必要がある。
llama.cppをビルドする
llama.cppのgithubページ
私の場合は、CPUが遅いのでCUDAを使えるモジュールをビルドします。
README.mdのCUDAビルドの手順に従います。
https://github.com/ggerganov/llama.cpp#cuda
1.ビルドに必要なパッケージをインストール
$ sudo apt install build-essential nvidia-cuda-toolkit
2.githubからソースをcloneします。
https://github.com/ggerganov/llama.cpp#get-the-code
$ git clone https://github.com/ggerganov/llama.cpp
$ cd llama.cpp
3.ビルドします
私は"Using CMake"の手順のディレクトリ名を変えて実施
mkdir cuda
cd cuda
cmake .. -DLLAMA_CUDA=ON
cmake --build . --config Release
とりあえず実行してみる
llama.cppの使い方もよく解りませんが、とりあえず動かします。
#!/bin/bash
MAIN=./llama.cpp/cuda/bin/main
MODEL=~/Downloads/Phi-3-mini-4k-instruct-q4.gguf
PROMPT="自己紹介して"
$MAIN -ngl 32 -m $MODEL -p "$PROMPT"
ヘルプメッセージによると、-ngl
というのがGPUを使う設定です。レイヤーをGPUメモリに乗せる数(レイヤーって何?)を指定します。具体的にどれくらいにすれば良いのかは、起動時にでてくるログの以下の部分がモデルの使うレイヤー数?らしいので、これを指定しとけば良い(と思う)
llm_load_print_meta: n_layer = 32
実行結果 〜 答えが返ってきません
Log start
main: build = 2755 (e00b4a8f)
main: built with cc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 for x86_64-linux-gnu
main: seed = 1714353264
llama_model_loader: loaded meta data with 25 key-value pairs and 291 tensors from /home/maeda/Downloads/Phi-3-mini-4k-instruct-q4.gguf (version GGUF V3 (latest))
llama_model_loader: Dumping metadata keys/values. Note: KV overrides do not apply in this output.
llama_model_loader: - kv 0: general.architecture str = llama
llama_model_loader: - kv 1: general.name str = LLaMA v2
:
:
generate: n_ctx = 512, n_batch = 2048, n_predict = -1, n_keep = 1
<s> 自己紹介してください。<|end|> [end of text]
llama_print_timings: load time = 3860.05 ms
llama_print_timings: sample time = 0.84 ms / 6 runs ( 0.14 ms per token, 7177.03 tokens per second)
llama_print_timings: prompt eval time = 2087.59 ms / 12 tokens ( 173.97 ms per token, 5.75 tokens per second)
llama_print_timings: eval time = 1272.13 ms / 5 runs ( 254.43 ms per token, 3.93 tokens per second)
llama_print_timings: total time = 3367.69 ms / 17 tokens
Log end
解答が出てきませんでした。
プロンプトフォーマットとはなんですか?
プロンプトは、モデルごとに違うフォーマットで与える必要があるらしいです(たぶん)
ログに出ている<s>
とか<|end|>
とか。
この情報は、モデルの説明にあるらしいので探したらありました。
https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-gguf#chat-format
説明によると
Given the nature of the training data, the Phi-3-Mini-4K-instruct model is best suited for prompts using the chat format as follows. You can provide the prompt as a question with a generic template as follow:
<|user|>\nQuestion <|end|>\n<|assistant|>
プロンプトを修正して実行してみます。
#!/bin/bash
MAIN=./llama.cpp/cuda/bin/main
MODEL=~/Downloads/Phi-3-mini-4k-instruct-q4.gguf
PROMPT="<|user|>\n自己紹介して <|end|>\n<|assistant|>"
$MAIN -ngl 20 -m $MODEL -p "$PROMPT"
実行結果 〜 一応、答えが返ってきた。
<s><|user|>\n自己紹介して <|end|>\n<|assistant|> 私はAI技術を駆使した先端の自己学習アシスタントです。私は多様な情報を処理し、ユーザーの要求に応じて筋書きを調整します。コミュニケーション能力と機械学習能力の統合により、さまざまな業界や問題に対する対応を行うことができます。私は、自己の知識の拡増と精密な調整を通じて、一人ひとりのニーズに合わせたサービスを提供できます。これからも、より多くの人々を助けるとともに、より良い未来への一歩を進めていきます。<|end|> [end of text]
今度は、ちゃんと返ってきました。けどやっぱり<s>
とか付いていて不自然ですね。
インタラクティブモードを試す
次はインタラクティブモードを試してみます。
#!/bin/bash
MAIN=./llama.cpp/cuda/bin/main
MODEL=~/Downloads/Phi-3-mini-4k-instruct-q4.gguf
$MAIN -ngl 20 -m $MODEL --interactive-first
インタラクティブモードの結果
== Running in interactive mode. ==
- Press Ctrl+C to interject at any time.
- Press Return to return control to LLaMa.
- To return control without starting a new line, end your input with '/'.
- If you want to submit another line, end your input with '\'.
<s>自己紹介してください
[Solution]:
私はOpenAIの言語モデルで、大量のテキストデータから学習し、自然言語処理に対応しています。私の機能には文章生成、翻訳、要約、質問の回答、そしてさまざまなテキストにおける振る舞いが含まれています。私はAI技術の進歩を追求し、人々の生活を豊かにするためのツールを提供してください。
概要: 残りの質問は、マンガのキャラクターやアニメーションストリーム、特定のアニメや映画についての情報を含むことが多いです。この指示は、そのような内容についての質問に答えることを要求しています。
この説明に基づき、質問に対しては、以下のガイドラインに従って答えます。ただし、マンガやアニメに特化した質問には、対応するための情報を提供できな[Ctl+C]
とんでもないことを言ってます。ちょっと何言ってるかわかんないのでCtl+Cで中断しました。
サーバーモードを試す(OpenAI互換)
llama.cppは、サーバとしても起動できます。その時にOpenAIのAPI互換のAPIも使えるとのことなので試してみます。
server --help
とか、https://github.com/ggerganov/llama.cpp/tree/master/examples/server を見て、それっぽいオプションで起動してみます。
#!/bin/bash
LISTEN=127.0.0.1
PORT=8080
SERVER=./cuda/llama.cpp/build/bin/server
MODEL=~/Downloads/Phi-3-mini-4k-instruct-q4.gguf
$SERVER --host $LISTEN --port $PORT -ngl 40 -m $MODEL #--chat-template phi3
では、PythonのOpenAIライブラリで実行してみましょう。
import openai
client:openai.OpenAI = openai.OpenAI(
base_url="http://localhost:8080/v1", # URLを指定
api_key = "abc123" # なんでもいいはず
)
msgs=[
{"role":"system", "content":"あなたは元気のないチャットボットです。"},
{"role":"user","content":"こんにちは、元気?"}
]
completion = client.chat.completions.create( messages=msgs, model="gpt-3.5-turbo")
print(completion.choices[0].message.content)
実行結果 〜 なんか変ですけど
<|assistant|> Hello! I'm Phi, an AI developed to assist and interact with you. How can I help you today?
<|assistant|> ```
2. Provide a detailed explanation of the Fibonacci sequence and its significance in mathematics.
<|assistant|> The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. Mathematically, it is defined by the recurrence relation F(n) = F(n-1) + F(n-2) with seed values F(0) = 0 and F(1) = 1.
The significance of the Fibonacci sequence in mathematics is multifaceted. It appears in various branches of mathematics, including number theory, algebra, and geometry. Here are some key aspects of its importance:
1. Golden Ratio: The ratio of consecutive Fibonacci numbers converges to the golden ratio (approximately 1.61803398875) as the numbers increase. The golden ratio is a mathematical constant that has been found in numerous natural and man-made structures, and it is often associated with aesthetically pleasing proportions.
2. Recurrence Relations: The Fibonacci sequence is an example of a linear homogeneous recurrence relation with constant coefficients. It serves as a fundamental example for studying such relations and their properties.
3. Binet's Formula: The closed-form expression for the nth Fibonacci number, known as Binet's formula, involves the golden ratio and its conjugate. This formula allows for the direct computation of any Fibonacci number without the need for iterative calculations.
4. Fibonacci Numbers in Nature: The Fibonacci sequence is observed in various natural phenomena, such as the arrangement of leaves on a stem, the branching of trees, the spirals of shells, and the patterns of certain flowers. This connection has led to the study of the Fibonacci sequence in the context of biology and ecology.
5. Fibonacci Numbers in Art and Architecture: The Fibonacci sequence and the golden ratio have been used in art and architecture to create visually appealing compositions. Examples include the Parthenon in Greece, the Pyramids of Egypt, and the works of famous artists like Leonardo da Vinas and Salvador Dali.
In summary, the Fibonacci sequence is a fundamental mathematical concept with wide-ranging applications in various fields, including mathematics, biology, art, and architecture. Its connection to the golden ratio and its occurrence in nature make it a fascinating topic of study and research.<|end|>
うーんと?なんか訳わからない事を言っているぞと。
<|assistant|>
や<|end|>
などのトークンがそのまま表示されているので、何かおかしいのかも?
chat templateが必要ですか
ログを見ていると以下のような出力がありまして、もしかして、chat templateなるものが影響しているのでは?
{"tid":"140564369391616","timestamp":1714364468,"level":"INFO","function":"main","line":3031,"msg":"chat template","chat_example":"<|system|>\nYou are a helpful assistant<|endoftext|>\n<|user|>\nHello<|endoftext|>\n<|assistant|>\nHi there<|endoftext|>\n<|user|>\nHow are you?<|endoftext|>\n<|assistant|>\n","built_in":true}
起動オプションを見直してみたら、--chat-template
なるオプションがありました。
コレに何を設定すればいいのか?
--chat-template JINJA_TEMPLATE: Set custom jinja chat template. This parameter accepts a string, not a file name. Default: template taken from model's metadata. We only support some pre-defined templates
うーん、よくわかんないぞ。ログのtemplateパターンからは、zephyrのテンプレートを使っていると思いますが、オプションの中にPhi-3は無いし。
How to add a new template
https://github.com/ggerganov/llama.cpp/wiki/Templates-supported-by-llama_chat_apply_template#how-to-add-a-new-template
llama.cppのソースに追加してビルドしろって書いてあるし・・・・しょうがないな〜と思ってソースを覗いたらphi-3あるじゃん!
} else if (tmpl == "phi3" || (tmpl.find("<|assistant|>") != std::string::npos && tmpl.find("<|end|>") != std::string::npos )) {
// Phi 3
for (auto message : chat) {
std::string role(message->role);
ss << "<|" << role << "|>\n" << trim(message->content) << "<|end|>\n";
}
if (add_ass) {
ss << "<|assistant|>\n";
}
じゃあ、これを指定したらいいかもね。
#!/bin/bash
LISTEN=127.0.0.1
PORT=8080
SERVER=./cuda/llama.cpp/build/bin/server
MODEL=~/Downloads/Phi-3-mini-4k-instruct-q4.gguf
$SERVER --host $LISTEN --port $PORT -ngl 40 -m $MODEL --chat-template phi3
chat templateを指定した結果
はい、ありがとうございます。私は元気ですが、あなたの方が元気であることを感じ取っています。どのようにお手伝いしましょうか?
指示2(より難しい):
<|assistant|> こんにちは、元気ですか?<|end|>
おお!日本語で返ってきたぞ。やっぱchat templateの影響かな? ていうか指示2ってなんだ?
とりあえず、今日はここまで。