Llama-2 (7B) をファインチューニングして ChatGPT を超える方法

Built for Speed: ~10ms Latency, Even Under Load
Blazingly fast way to build, track and deploy your models!
- Handles 350+ RPS on just 1 vCPU — no tuning needed
- Production-ready with full enterprise support
大規模言語モデル(LLM)はますます強力になっており、チャットボット、テキスト生成、質問応答など、さまざまなタスクに使用されています。しかし、LLM のトレーニングにはコストとリソースがかかる場合があります。このブログ記事では、より小さな LLM(7B)をファインチューニングして、ChatGPT 以上のパフォーマンスを発揮させる方法を紹介します。
ファインチューニングとは、特定のタスクにおけるパフォーマンスを向上させるために、特定のデータセットで LLM をトレーニングするプロセスです。今回は、2 つの数字を掛け算するように 7B LLM をファインチューニングします。
それではまず、簡単な掛け算タスクにおけるさまざまな大規模言語モデルのパフォーマンスから見てみましょう:458*987 = 452046。
最初に、Meta が最近リリースしたモデル Llama-2-7B を見てみます。 TrueFoundry 上に Llama-2 をデプロイし、TrueFoundry の LangChain 統合機能を使用して試してみました。その結果がこちらです。

一目瞭然ですが、このタスクではうまく機能していません(モデルのサイズを考えれば予想通りでもあります)。最先端のモデルが同じタスクでどのように機能するかを見てみましょう:
ChatGPT (GPT-3.5 Turbo) の結果を見てみましょう:

今回の回答(451,086)は実際の答えである 452,046 に非常に近いですが、正しくありません。
別のプロンプトを試してみましょう(ステップバイステップで計算させて、どうなるか確認します):
0:00/1×
掛け算用のカスタムプロンプトを使用した ChatGPT
しかし、やはり間違った結果(450,606)に到達してしまいました 🤨
最後に、最先端モデル(GPT-4)を試して、このタスクでどのように機能するかを確認してみましょう:

正解(452,046)に非常に近く、一見すると正しいように見えます。しかし明らかに回答は間違っています。
なぜ LLM は簡単な掛け算タスクで不十分なパフォーマンスしか出せないのでしょうか?
この質問に対する答えは非常にシンプルです。それに向けて「トレーニング」されていないからです。
LLM はテキストとコードの膨大なデータセットでトレーニングされていますが、このデータは必ずしも数学的推理に役立つ方法で構造化されているわけではありません。たとえば、データには加算、減算、乗算、除算などの数学的概念の明示的な表現が含まれていない場合があります。これにより、LLM がこれらの演算を正確に実行する方法を学習するのが困難になる可能性があります。
そこで私たちは、LLM をトレーニングして数学的演算を実行させることは可能だろうかと考えました。実験を行ったところ、非常に有望な結果が得られました!
掛け算を学習させるための Llama-2 7B のファインチューニング
データセットのキュレーション
まず手始めに、40,000 行のプロンプトと指示文で構成されるデータセットをキュレーションしました。筆算による掛け算の手順を正確に模倣しようとしました。これには、被乗数と乗数を個々の桁に分解し、被乗数の各桁に乗数の各桁を掛け、位取りに基づいて適切な位置に結果を配置することが含まれます。最後に、個々の積をすべて足し合わせて最終結果を得ます。
プロンプトのサンプルはこちらです:
You are a helpful assistant providing detailed answers to multiplication questions. Ensure you provide a thorough explanation using the long multiplication method for the math problem given below.
If you don't know the answer to a question, please don't share false information. What is 5244 * 1932?
そしてレスポンスのサンプルはこちらです:
Here are the steps for multiplying 5244 * 1932 using the long multiplication method.
Step 1: Break 5244 into its individual digits: 5 (thousands place), 2 (hundreds place), 4 (tens place), and 4 (ones place).
Step 2: Similarly, break 1932 into its individual digits: 1 (thousands place), 9 (hundreds place), 3 (tens place), and 2 (ones place).
- 4 (from 1 place of first number) * 2 (from 1 place of second number) = 8 -> Shift it 0 places to the left (because we are multiplying with the 1 place of the second number) to get 8
- 4 (from 1 place of first number) * 3 (from 10 place of second number) = 12 -> Shift it 1 places to the left (because we are multiplying with the 10 place of the second number) to get 120
- 4 (from 1 place of first number) * 9 (from 100 place of second number) = 36 -> Shift it 2 places to the left (because we are multiplying with the 100 place of the second number) to get 3600
- 4 (from 1 place of first number) * 1 (from 1000 place of second number) = 4 -> Shift it 3 places to the left (because we are multiplying with the 1000 place of the second number) to get 4000
- 4 (from 10 place of first number) * 2 (from 1 place of second number) = 8 -> Shift it 1 places to the left (because we are multiplying with the 1 place of the second number) to get 80
- 4 (from 10 place of first number) * 3 (from 10 place of second number) = 12 -> Shift it 2 places to the left (because we are multiplying with the 10 place of the second number) to get 1200
- 4 (from 10 place of first number) * 9 (from 100 place of second number) = 36 -> Shift it 3 places to the left (because we are multiplying with the 100 place of the second number) to get 36000
- 4 (from 10 place of first number) * 1 (from 1000 place of second number) = 4 -> Shift it 4 places to the left (because we are multiplying with the 1000 place of the second number) to get 40000
- 2 (from 100 place of first number) * 2 (from 1 place of second number) = 4 -> Shift it 2 places to the left (because we are multiplying with the 1 place of the second number) to get 400
- 2 (from 100 place of first number) * 3 (from 10 place of second number) = 6 -> Shift it 3 places to the left (because we are multiplying with the 10 place of the second number) to get 6000
- 2 (from 100 place of first number) * 9 (from 100 place of second number) = 18 -> Shift it 4 places to the left (because we are multiplying with the 100 place of the second number) to get 180000
- 2 (from 100 place of first number) * 1 (from 1000 place of second number) = 2 -> Shift it 5 places to the left (because we are multiplying with the 1000 place of the second number) to get 200000
- 5 (from 1000 place of first number) * 2 (from 1 place of second number) = 10 -> Shift it 3 places to the left (because we are multiplying with the 1 place of the second number) to get 10000
- 5 (from 1000 place of first number) * 3 (from 10 place of second number) = 15 -> Shift it 4 places to the left (because we are multiplying with the 10 place of the second number) to get 150000
- 5 (from 1000 place of first number) * 9 (from 100 place of second number) = 45 -> Shift it 5 places to the left (because we are multiplying with the 100 place of the second number) to get 4500000
- 5 (from 1000 place of first number) * 1 (from 1000 place of second number) = 5 -> Shift it 6 places to the left (because we are multiplying with the 1000 place of the second number) to get 5000000
Now, add all these results together:
The sum of 8 + 120 + 3600 + 4000 + 80 + 1200 + 36000 + 40000 + 400 + 6000 + 180000 + 200000 + 10000 + 150000 + 4500000 + 5000000 = 10131408.
次に、掛け算用の 2 つの数字をランダムに選択してプロンプトとレスポンスのペアを生成する Python スクリプトを作成しました。これにより 40,000 行のデータセットを準備しました。
モデルのファインチューニング
データセットの準備ができたら、モデルをファインチューニングする必要があります。
ベースモデルとして、Meta のファインチューニング済みチャットバリアント(70 億パラメータ)の Llama-2 を使用しています。
BitsAndBytes と PEFT ライブラリを使用して QLoRA ファインチューニングを実施しました。使用した LoRA 設定は次のとおりです:
LoraConfig(
lora_alpha=16,
lora_dropout=0.1,
r=64,
bias="none",
task_type="CAUSAL_LM",
target_modules=[
"q_proj",
"k_proj",
"v_proj",
"o_proj",
],
)
40,000 行のデータセットに対し、A100 40GB GPU マシンでトレーニングに約 8 時間かかりました。
最後に、ファインチューニングしたモデルを再び TrueFoundry にデプロイしました。結果は以下のとおりです:

ついに成功しました!!ファインチューニングされたモデルが結果を正しく計算できていることが確認できます。
まとめ
算術は LLM を使用する本来の目的ではないかもしれませんが、この例は特定のタスク用に適切にファインチューニングされた「小さな」LLM(7B パラメータ)が、特定のタスクにおいて「大きな」LLM(GPT-3.5 Turbo の 175B パラメータや GPT-4 など)を凌駕できることを実証しています。
ファインチューニングされたより小さなモデルは、推論コストが安く、専門的なタスクに優れており、自社のクラウド上に簡単にデプロイできます!
Llama 2 のファインチューニングに関する詳細なブログ記事を執筆しました
TrueFoundry AI Gateway delivers ~3–4 ms latency, handles 350+ RPS on 1 vCPU, scales horizontally with ease, and is production-ready, while LiteLLM suffers from high latency, struggles beyond moderate RPS, lacks built-in scaling, and is best for light or prototype workloads.


















.webp)





.webp)
.webp)
.png)

.png)
.png)
.png)
.png)
.png)
.webp)
.webp)






