View a markdown version of this page

直接偏好最佳化 (DPO) - Amazon Nova

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

直接偏好最佳化 (DPO)

概觀

Direct Preference Optimization (DPO) 是一種對齊技術,使用配對比較資料微調基礎模型,使模型輸出與人類偏好設定保持一致。與強化學習方法不同,DPO 會根據人類對哪些回應更理想的意見回饋,直接最佳化模型行為,提供更穩定且可擴展的方法。

為什麼要使用 DPO

基礎模型可能會產生實際上正確的輸出,但無法符合特定使用者需求、組織值或安全需求。DPO 可讓您:

  • 針對所需的行為模式微調模型

  • 減少不想要或有害的輸出

  • 使模型回應與品牌語音和通訊指導方針保持一致

  • 根據網域專家意見回饋改善回應品質

  • 透過偏好的回應模式實作安全護欄

DPO 的運作方式

DPO 使用配對範例,其中人工評估人員會指出偏好兩種可能回應中的哪一種。此模型會學習將產生偏好回應的可能性最大化,同時將不需要的回應減至最少。

何時使用 DPO

在下列案例中使用 DPO:

  • 最佳化需要符合特定人類偏好設定的主觀輸出

  • 調整模型的色調、風格或內容特性

  • 根據使用者意見回饋和錯誤分析進行有針對性的改善

  • 在不同使用案例中維持一致的輸出品質

  • 僅使用偏好資料進行無獎勵強化學習的訓練

支援的模型和技術

DPO 同時支援全參數微調和 LoRA (低排名調整):

模型 支援的輸入 執行個體類型 建議的執行個體計數 允許的執行個體計數
Amazon Nova Micro 文字 ml.p5.48xlarge 2 2、4、8
Amazon Nova Lite 文字、影像 ml.p5.48xlarge 4 2、4、8、16
Amazon Nova Pro 文字、影像 ml.p5.48xlarge 6 6、12、24

訓練方法

  • 全階 DPO:更新所有模型參數。可能提供更好的一致性品質,但需要更多的運算資源,並產生更大的模型。

  • LoRA DPO:使用輕量型轉接器進行參數效率微調。以較小的輸出模型提供更有效率的訓練和部署,同時保持良好的一致性品質。

對於大多數使用案例,LoRA 方法提供足夠的調整功能,並大幅提升效率。

資料格式

DPO 訓練資料遵循與 SFT 相同的格式,但最後一個助理輪換必須包含具有 preferrednon-preferred標籤的偏好設定對。

基本結構

最終助理輪換使用candidates陣列而非 content

{ "role": "assistant", "candidates": [ { "content": [ { "text": "This is the preferred response." } ], "preferenceLabel": "preferred" }, { "content": [ { "text": "This is the non-preferred response." } ], "preferenceLabel": "non-preferred" } ] }

完成文字範例

{ "schemaVersion": "bedrock-conversation-2024", "system": [ { "text": "You are a helpful assistant." } ], "messages": [ { "role": "user", "content": [ { "text": "What is the capital of France?" } ] }, { "role": "assistant", "content": [ { "text": "The capital of France is Paris." } ] }, { "role": "user", "content": [ { "text": "Tell me more about it." } ] }, { "role": "assistant", "candidates": [ { "content": [ { "text": "Paris is the capital and largest city of France, known for the Eiffel Tower, world-class museums like the Louvre, and its rich cultural heritage." } ], "preferenceLabel": "preferred" }, { "content": [ { "text": "Paris is a city in France." } ], "preferenceLabel": "non-preferred" } ] } ] }

具有映像的範例

{ "schemaVersion": "bedrock-conversation-2024", "system": [ { "text": "You are a helpful assistant." } ], "messages": [ { "role": "user", "content": [ { "text": "Describe this image." }, { "image": { "format": "jpeg", "source": { "s3Location": { "uri": "s3://your-bucket/your-path/image.jpg", "bucketOwner": "your-aws-account-id" } } } } ] }, { "role": "assistant", "candidates": [ { "content": [ { "text": "The image shows a detailed description with relevant context and observations." } ], "preferenceLabel": "preferred" }, { "content": [ { "text": "This is a picture." } ], "preferenceLabel": "non-preferred" } ] } ] }

資料集要求

  • 格式:用於訓練的單一 JSONL 檔案、用於驗證的單一 JSONL 檔案 (選用)

  • 大小下限:為有效訓練建議 1,000 個偏好設定配對

  • 品質:高品質偏好資料會產生更有效的結果

  • 其他限制條件:與 SFT 相同。如需詳細資訊,請參閱資料集限制條件。

上傳資料

aws s3 cp /path/to/training-data/ s3://your-bucket/train/ --recursive aws s3 cp /path/to/validation-data/ s3://your-bucket/val/ --recursive

配方組態

一般執行組態

run: name: "my-dpo-run" model_type: "amazon.nova-lite-v1:0:300k" model_name_or_path: "nova-lite/prod" replicas: 4
參數 Description
name 訓練任務的描述性名稱
model_type Nova 模型變體 (請勿修改)
model_name_or_path 基本模型路徑 (請勿修改)
replicas 分散式訓練的運算執行個體數量

訓練組態

training_config: max_length: 16384 global_batch_size: 32 trainer: max_epochs: 3 model: hidden_dropout: 0.0 attention_dropout: 0.0 ffn_dropout: 0.0
參數 Description 範圍
max_length 字符中的序列長度上限 1024–32768
global_batch_size 每個最佳化工具步驟的範例 Micro/Lite/Pro:16、32、64、128。Micro/Lite:256
max_epochs 訓練透過資料集傳遞 最小值:1
hidden_dropout 隱藏狀態的捨棄 0.0–1.0
attention_dropout 捨棄注意力權重 0.0–1.0
ffn_dropout 向前饋送圖層的退出 0.0–1.0

最佳化工具組態

model: optim: lr: 1e-5 name: distributed_fused_adam adam_w_mode: true eps: 1e-08 weight_decay: 0.0 betas: - 0.9 - 0.999 sched: warmup_steps: 10 constant_steps: 0 min_lr: 1e-6
參數 Description 範圍
lr 學習率 0–1 (通常是 1e-6 到 1e-4)
weight_decay L2 正規化強度 0.0–1.0
warmup_steps 逐步提高學習率的步驟 0–20
min_lr 衰減結束時的最低學習率 0–1 (必須 < lr)

DPO 特定組態

model: dpo_cfg: beta: 0.1
參數 Description 範圍
beta 在調整訓練資料與保持接近原始模型之間取得平衡 0.001–0.5
  • 較高的 Beta 值 (0.1):保留更多參考模型行為,但可能會更慢地學習偏好設定

  • 較低的 Beta 版 (0.01–0.05):更積極的偏好學習,但有偏離參考的風險

建議:如果偏好學習似乎不足,請從 開始beta: 0.1並向下調整。

LoRA PEFT 組態

model: peft: peft_scheme: "lora" lora_tuning: loraplus_lr_ratio: 64.0 alpha: 32 adapter_dropout: 0.01
參數 Description 允許的值
peft_scheme 微調方法 "lora"null(完整排名)
alpha LoRA 權重的擴展因素 32、64、96、128、160、192
loraplus_lr_ratio LoRA+ 學習率擴展因素 0.0–100.0
adapter_dropout LoRA 參數的正規化 0.0–1.0

啟動訓練任務

容器映像

708977205387.dkr.ecr.us-east-1.amazonaws.com/nova-fine-tune-repo:SM-TJ-DPO-latest

範例程式碼

from sagemaker.pytorch import PyTorch from sagemaker.inputs import TrainingInput instance_type = "ml.p5.48xlarge" instance_count = 4 image_uri = "708977205387.dkr.ecr.us-east-1.amazonaws.com/nova-fine-tune-repo:SM-TJ-DPO-latest" recipe_overrides = { "training_config": { "trainer": {"max_epochs": 2}, "model": { "dpo_cfg": {"beta": 0.1}, "peft": { "peft_scheme": "lora", "lora_tuning": { "loraplus_lr_ratio": 64.0, "alpha": 32, "adapter_dropout": 0.01, }, }, }, }, } estimator = PyTorch( output_path=f"s3://{bucket_name}/{job_name}", base_job_name=job_name, role=role, instance_count=instance_count, instance_type=instance_type, training_recipe="fine-tuning/nova/nova_lite_p5_gpu_lora_dpo", recipe_overrides=recipe_overrides, max_run=18000, sagemaker_session=sagemaker_session, image_uri=image_uri, disable_profiler=True, debugger_hook_config=False, ) train_input = TrainingInput( s3_data=train_dataset_s3_path, distribution="FullyReplicated", s3_data_type="Converse", ) val_input = TrainingInput( s3_data=val_dataset_s3_path, distribution="FullyReplicated", s3_data_type="Converse", ) estimator.fit(inputs={"train": train_input, "validation": val_input}, wait=True)

部署模型

訓練完成後,使用自訂模型匯入功能將自訂模型部署至 Amazon Bedrock。此模型支援佈建輸送量和隨需推論。LoRA 訓練的模型支援隨需推論。

如需部署說明,請參閱部署自訂模型

限制

  • 輸入模式:DPO 僅接受文字和影像。不支援視訊輸入。

  • 輸出模式:僅限文字

  • 偏好設定對:最終助理輪換必須包含剛好兩個具有 preferrednon-preferred標籤的候選者

  • 影像限制:每個內容區塊最多 10 個影像

  • 混合模式:無法在相同的訓練任務中結合文字、影像和影片