Phi Silica向けLoRAファインチューニング
特定のユースケースに合わせてパフォーマンスを向上させるために、Low Rank Adaptation (LoRA) を使用してPhi Silicaモデルをファインチューニングできます。Phi Silica(Microsoft Windowsローカル言語モデル)をLoRAで最適化することで、より正確な結果を得ることができます。このプロセスには、LoRAアダプターをトレーニングし、推論中に適用してモデルの精度を向上させるという流れが含まれます。
Phi Silicaの機能は中国では利用できません。
前提条件
- Phi Silicaの応答を強化するユースケースを特定しました。
- 「良い応答」が何かを判断するための評価基準を選択しました。
- Phi Silica APIを試しましたが、評価基準を満たしていません。
アダプターのトレーニング
Windows 11でPhi SilicaモデルをファインチューニングするためのLoRAアダプターをトレーニングするには、まずトレーニングプロセスで使用するデータセットを生成する必要があります。
LoRAアダプターで使用するデータセットの生成
データセットを生成するには、データを2つのファイルに分割する必要があります。
train.json: アダプターのトレーニングに使用します。test.json: トレーニング中およびトレーニング後のアダプターのパフォーマンス評価に使用します。
両方のファイルはJSON形式を使用する必要があります。各行は単一のサンプルを表す別個のJSONオブジェクトです。各サンプルには、ユーザーとアシスタントの間で交わされたメッセージのリストを含める必要があります。
すべてのメッセージオブジェクトには2つのフィールドが必要です。
content: メッセージのテキスト。role: 送信者を示す"user"または"assistant"のいずれか。
次の例を参照してください。
{"messages": [{"content": "Hello, how do I reset my password?", "role": "user"}, {"content": "To reset your password, go to the settings page and click 'Reset Password'.", "role": "assistant"}]}
{"messages": [{"content": "Can you help me find nearby restaurants?", "role": "user"}, {"content": "Sure! Here are some restaurants near your location: ...", "role": "assistant"}]}
{"messages": [{"content": "What is the weather like today?", "role": "user"}, {"content": "Today's forecast is sunny with a high of 25°C.", "role": "assistant"}]}
トレーニングのヒント
- 各サンプル行の最後にカンマは必要ありません。
- できるだけ多くの高品質で多様な例を含めてください。最良の結果を得るには、
train.jsonファイルに数千のトレーニングサンプルを集めてください。 test.jsonファイルは小さくても構いませんが、モデルが処理すると予想される対話のタイプをカバーする必要があります。train.jsonとtest.jsonファイルを、1行につき1つのJSONオブジェクトで作成し、それぞれにユーザーとアシスタントの間の短いやり取りを含めます。データの品質と量は、LoRAアダプターの有効性に大きく影響します。
LoRAアダプターのトレーニング
LoRAアダプターをトレーニングするには、以下の前提条件が必要です。
- Azure Container Appsで利用可能なクォータを持つAzureサブスクリプション。
- ファインチューニングジョブを効率的に実行するには、A100 GPU以上を使用することをお勧めします。
- Azure Portalで利用可能なクォータがあることを確認してください。クォータを見つけるのに役が必要な場合は、クォータの表示を参照してください。
ワークスペースを作成し、ファインチューニングジョブを開始するには、次の手順に従います。
-
Model Tools > Fine-tuning に移動し、New Project を選択します。
-
モデルカタログから「microsoft/phi-silica」を選択し、Next を選択します。
-
ダイアログで、プロジェクトフォルダーを選択し、プロジェクト名を入力します。プロジェクト用に新しいVS Codeウィンドウが開きます。

-
メソッドリストから「LoRA」を選択します。

-
Data > Training Dataset name および Test Dataset name で、
train.jsonおよびtest.jsonファイルを選択します。 -
Run with Cloud を選択します。

-
ダイアログで、AzureサブスクリプションにアクセスするためのMicrosoftアカウントを選択します。
-
アカウントが選択されたら、サブスクリプションドロップダウンメニューからリソースグループを選択します。
-
ファインチューニングジョブが正常に開始され、ジョブステータスが表示されることに注意してください。
Refresh ボタンを使用してステータスを手動で更新します。ファインチューニングジョブの完了には通常45~60分かかります。
-
ジョブが完了したら、Download を選択して新しくトレーニングされたLoRAアダプターをダウンロードするか、Show Metrics を選択してファインチューニングメトリックを確認することができます。

LoRAファインチューニングの推奨事項
ハイパーパラメーターの選択
LoRAファインチューニング用に設定されたデフォルトのハイパーパラメーターは、比較対象として妥当なベースラインのファインチューニングを提供します。ほとんどのユースケースとデータセットでうまく機能するデフォルト値を見つけるために最善を尽くしました。
ただし、必要に応じてパラメーターを調整できる柔軟性を残しています。
トレーニングハイパーパラメーター
標準的なパラメーター検索空間は次のとおりです。
| パラメーター名 | 最小値 | 最大値 | 分布 |
|---|---|---|---|
| learning_rate (学習率) | 1e-4 | 1e-2 | 対数一様分布 |
| weight_decay (重み減衰) | 1e-5 | 1e-1 | 対数一様分布 |
| adam_beta1 | 0.9 | 0.99 | 一様分布 |
| adam_beta2 | 0.9 | 0.999 | 一様分布 |
| adam_epsilon | 1e-9 | 1e-6 | 対数一様分布 |
| num_warmup_steps (ウォームアップステップ数) | 0 | 10000 | 一様分布 |
| lora_dropout (LoRAドロップアウト) | 0 | 0.5 | 一様分布 |
学習率スケジューラーについても検索し、linear_with_warmupまたはcosine_with_warmupのいずれかを選択します。num_warmup_stepsパラメーターが0に設定されている場合、線形またはコサインオプションを同等に使用できます。
学習率、学習率スケジューラー、ウォームアップステップ数はすべて互いに作用します。2つを固定し、3つ目を変更することで、データセットでのトレーニング出力がどのように変化するかについて、より良い洞察が得られます。
重み減衰とLoRAドロップアウトパラメーターは、過学習を制御するのに役立ちます。アダプターがトレーニングセットから評価セットにうまく汎化できていない場合は、これらのパラメーターの値を増やしてみてください。
adam_ parametersは、トレーニングステップ中のAdamオプティマイザーの動作に影響を与えます。このオプティマイザーの詳細については、例えばPyTorchのドキュメントを参照してください。
公開されている他の多くのパラメーターは、PEFTライブラリの同等の名前の対応物と類似しています。詳細については、transformersのドキュメントを参照してください。
データハイパーパラメーター
データハイパーパラメーターtrain_nsamplesとtest_nsamplesは、それぞれトレーニングとテストに取得するサンプル数を制御します。トレーニングセットからより多くのサンプルを使用することは通常良いアイデアです。より多くのテストサンプルを使用すると、テストメトリックのノイズが少なくなりますが、各評価実行には時間がかかります。
train_batch_sizeとtest_batch_sizeパラメーターは、それぞれトレーニングとテストの各バッチで使用するサンプル数を制御します。テスト例の実行はトレーニング例よりもGPUメモリを消費しないため、通常、トレーニングよりもテストに多くのバッチを使用できます。
train_seqlenとtest_seqlenパラメーターは、トレーニングおよびテストシーケンスの長さを制御します。一般的に、GPUメモリの制限に達するまでは長いほど良いです。デフォルト値は良好なバランスを提供します。
システムプロンプトの選択
トレーニングに使用するシステムプロンプトを選択する際に効果的であることがわかった戦略は、モデルに目的の形式で出力を生成させることを奨励しつつも、プロンプトをかなりシンプルに保つ(1文または2文)ことです。また、トレーニングと推論でわずかに異なるシステムプロンプトを使用すると、結果が向上することもわかっています。
目的の出力がベースモデルと異なれば異なるほど、システムプロンプトはより役立ちます。
例えば、若年層の読者にアピールするために簡略化された言語を使用するなど、ベースモデルのわずかなスタイル変更のみを目的としてトレーニングする場合、システムプロンプトはまったく必要ないかもしれません。
ただし、目的の出力により多くの構造がある場合は、システムプロンプトを使用してモデルをその方向に導く必要があります。したがって、特定のキーを持つJSONテーブルが必要な場合、システムプロンプトの最初の文で、モデルが平易な言語で応答した場合にどのように見えるべきかを記述できます。そして、2番目の文でJSONテーブルの形式がどのように見えるべきかをより具体的に指定できます。トレーニングで最初の文を使用し、推論で両方の文を使用することで、目的の結果が得られる可能性があります。
パラメーター
ファインチューニング可能なすべてのパラメーターのリストをここに追記します。パラメーターがワークフローページのUIに表示されない場合は、<your_project_path>/<model_name>/lora/lora.yamlに手動で追加してください。
[
################## Basic config settings ##################
{
"groupId": "data",
"fields": [
{
"name": "system_prompt",
"type": "Optional",
"defaultValue": null,
"info": "Optional system prompt. If specified, the system prompt given here will be prepended to each example in the dataset as the system prompt when training the LoRA adapter. When running inference the same (or a very similar) system prompt should be used. Note: if a system prompt is specified in the training data, giving a system prompt here will overwrite the system prompt in the dataset.",
"label": "System prompt"
},
{
"name": "varied_seqlen",
"type": "bool",
"defaultValue": false,
"info": "Varied sequence lengths in the calibration data. If False (default), training examples will be concatenated together until they are finetune_[train/test]_seqlen tokens long. This makes memory usage more consistent and predictable. If True, each individual example will be truncated to finetune_[train/test]_seqlen tokens. This can sometimes give better training performance, but also gives unpredictable memory usage. It can cause `out of memory` errors mid training, if there are long training examples in your dataset.",
"label": "Allow varied sequence length in data"
},
{
"name": "finetune_dataset",
"type": "str",
"defaultValue": "wikitext2",
"info": "Dataset to finetune on.",
"label": "Dataset name or path"
},
{
"name": "finetune_train_nsamples",
"type": "int",
"defaultValue": 4096,
"info": "Number of samples to load from the train set for finetuning.",
"label": "Number of finetuning samples"
},
{
"name": "finetune_test_nsamples",
"type": "int",
"defaultValue": 128,
"info": "Number of samples to load from the test set for finetuning.",
"label": "Number of test samples"
},
{
"name": "finetune_train_batch_size",
"type": "int",
"defaultValue": 4,
"info": "Batch size for finetuning training.",
"label": "Training batch size"
},
{
"name": "finetune_test_batch_size",
"type": "int",
"defaultValue": 8,
"info": "Batch size for finetuning testing.",
"label": "Test batch size"
},
{
"name": "finetune_train_seqlen",
"type": "int",
"defaultValue": 2048,
"info": "Maximum sequence length for finetuning training. Longer sequences will be truncated.",
"label": "Max training sequence length"
},
{
"name": "finetune_test_seqlen",
"type": "int",
"defaultValue": 2048,
"info": "Maximum sequence length for finetuning testing. Longer sequences will be truncated.",
"label": "Max test sequence length"
}
]
},
{
"groupId": "finetuning",
"fields": [
{
"name": "early_stopping_patience",
"type": "int",
"defaultValue": 5,
"info": "Number of evaluations with no improvement after which training will be stopped.",
"label": "Early stopping patience"
},
{
"name": "epochs",
"type": "float",
"defaultValue": 1,
"info": "Number of total epochs to run.",
"label": "Epochs"
},
{
"name": "eval_steps",
"type": "int",
"defaultValue": 64,
"info": "Number of training steps to perform before each evaluation.",
"label": "Steps between evaluations"
},
{
"name": "save_steps",
"type": "int",
"defaultValue": 64,
"info": "Number of steps after which to save model checkpoint. This _must_ be a multiple of the number of steps between evaluations.",
"label": "Steps between checkpoints"
},
{
"name": "learning_rate",
"type": "float",
"defaultValue": 0.0002,
"info": "Learning rate for training.",
"label": "Learning rate"
},
{
"name": "lr_scheduler_type",
"type": "str",
"defaultValue": "linear",
"info": "Type of learning rate scheduler.",
"label": "Learning rate scheduler",
"optionValues": [
"linear",
"linear_with_warmup",
"cosine",
"cosine_with_warmup"
]
},
{
"name": "num_warmup_steps",
"type": "int",
"defaultValue": 400,
"info": "Number of warmup steps for learning rate scheduler. Only relevant for a _with_warmup scheduler.",
"label": "Scheduler warmup steps (if supported)"
}
]
}
################## Advanced config settings ##################
{
"groupId": "advanced",
"fields": [
{
"name": "seed",
"type": "int",
"defaultValue": 42,
"info": "Seed for sampling the data.",
"label": "Random seed"
},
{
"name": "evaluation_strategy",
"type": "str",
"defaultValue": "steps",
"info": "Evaluation strategy to use.",
"label": "Evaluation strategy",
"optionValues": [
"steps",
"epoch",
"no"
]
},
{
"name": "lora_dropout",
"type": "float",
"defaultValue": 0.1,
"info": "Dropout rate for LoRA.",
"label": "LoRA dropout"
},
{
"name": "adam_beta1",
"type": "float",
"defaultValue": 0.9,
"info": "Beta1 hyperparameter for Adam optimizer.",
"label": "Adam beta 1"
},
{
"name": "adam_beta2",
"type": "float",
"defaultValue": 0.95,
"info": "Beta2 hyperparameter for Adam optimizer.",
"label": "Adam beta 2"
},
{
"name": "adam_epsilon",
"type": "float",
"defaultValue": 1e-08,
"info": "Epsilon hyperparameter for Adam optimizer.",
"label": "Adam epsilon"
},
{
"name": "num_training_steps",
"type": "Optional",
"defaultValue": null,
"info": "The number of training steps there will be. If not set (recommended), this will be calculated internally.",
"label": "Number of training steps"
},
{
"name": "gradient_accumulation_steps",
"type": "int",
"defaultValue": 1,
"info": "Number of updates steps to accumulate before performing a backward/update pass.",
"label": "gradient accumulation steps"
},
{
"name": "eval_accumulation_steps",
"type": "Optional",
"defaultValue": null,
"info": "Number of predictions steps to accumulate before moving the tensors to the CPU.",
"label": "eval accumulation steps"
},
{
"name": "eval_delay",
"type": "Optional",
"defaultValue": 0,
"info": "Number of epochs or steps to wait for before the first evaluation can be performed, depending on the eval_strategy.",
"label": "eval delay"
},
{
"name": "weight_decay",
"type": "float",
"defaultValue": 0.0,
"info": "Weight decay for AdamW if we apply some.",
"label": "weight decay"
},
{
"name": "max_grad_norm",
"type": "float",
"defaultValue": 1.0,
"info": "Max gradient norm.",
"label": "max grad norm"
},
{
"name": "gradient_checkpointing",
"type": "bool",
"defaultValue": false,
"info": "If True, use gradient checkpointing to save memory at the expense of slower backward pass.",
"label": "gradient checkpointing"
}
]
}
]
Azureサブスクリプションとリソースグループの変更
以前に設定したAzureサブスクリプションとリソースグループを変更したい場合は、<your_project_path>/model_lab.workspace.provision.configファイルで更新または削除できます。
Phi Silica LoRAアダプターを使用した推論
Phi Silica APIは限定アクセス機能の一部です(LimitedAccessFeaturesクラスを参照)。詳細情報またはアンロックトークンのリクエストについては、LAFアクセストークンリクエストフォームを使用してください。
Phi Silica LoRAアダプターを使用した推論は、現在、ARMプロセッサを搭載したCopilot+ PCでのみサポートされています。
Windows AI APIを使用して推論を行う: LoRAアダプター付きPhi Silica