View a markdown version of this page

在 SageMaker 訓練任務的 Nova 2.0 上監督微調 (SFT) - Amazon Nova

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

在 SageMaker 訓練任務的 Nova 2.0 上監督微調 (SFT)

先決條件

開始訓練任務之前,請注意以下事項:

  • Amazon S3 儲存貯體,用於存放訓練任務的輸入資料和輸出。您可以為兩種類型的資料使用一個儲存貯體,也可以為每一種類型的資料使用個別的儲存貯體。請確定您的儲存貯體位於您建立所有其他資源以進行訓練 AWS 區域 的相同位置。如需詳細資訊,請參閱建立一般用儲存貯體

  • 具有執行訓練任務許可的 IAM 角色。請務必使用 AmazonSageMakerFullAccess 連接 IAM 政策。如需詳細資訊,請參閱如何使用 SageMaker AI 執行角色

  • 基本 Amazon Nova 配方,請參閱取得 Amazon Nova 配方

什麼是 SFT?

監督式微調 (SFT) 會使用標記的輸入輸出對來訓練語言模型。此模型會從包含提示和回應的示範範例中學習,改善其功能以符合特定任務、指示或所需行為。

若要判斷 SFT 是否適合您的使用案例,請參閱 監督式微調 (SFT)

啟動訓練任務

準備您的資料

如需準備 SFT 訓練資料的資料格式、支援的功能、限制條件和最佳實務的相關資訊,請參閱 準備 Amazon Nova 2 上的 SFT 資料

上傳資料

資料集應上傳至 SageMaker 訓練任務可存取的儲存貯體。如需設定正確許可的詳細資訊,請參閱先決條件

選取超參數並更新配方

Nova 2.0 的設定大致上與 Nova 1.0 相同。輸入資料上傳到 S3 後,請使用 GitHub 上微調資料夾下 SageMaker HyperPod 配方的配方。對於 Nova 2.0 Lite,以下是您可以根據使用案例更新的一些關鍵超參數。以下是 Nova 2.0 Lite SFT PEFT 配方的範例。對於容器映像 URI,請使用 708977205387.dkr.ecr.us-east-1.amazonaws.com/nova-fine-tune-repo:SM-TJ-SFT-V2-latest執行 SFT 微調任務。

範例輸入

run: name: {peft_recipe_job_name} model_type: amazon.nova-2-lite-v1:0:256k model_name_or_path: {peft_model_name_or_path} data_s3_path: {train_dataset_s3_path} # SageMaker HyperPod (SMHP) only and not compatible with SageMaker Training jobs. Note replace my-bucket-name with your real bucket name for SMHP job replicas: 4 # Number of compute instances for training, allowed values are 4, 8, 16, 32 output_s3_path: "" # Output artifact path (Hyperpod job-specific; not compatible with standard SageMaker Training jobs). Note replace my-bucket-name with your real bucket name for SMHP job training_config: max_steps: 10 # Maximum training steps. Minimal is 4. save_steps: 10 # How many training steps the checkpoint will be saved. Should be less than or equal to max_steps save_top_k: 1 # Keep top K best checkpoints. Note supported only for SageMaker HyperPod jobs. Minimal is 1. max_length: 32768 # Sequence length (options: 8192, 16384, 32768 [default], 65536) global_batch_size: 32 # Global batch size (options: 32, 64, 128) reasoning_enabled: true # If data has reasoningContent, set to true; otherwise False lr_scheduler: warmup_steps: 15 # Learning rate warmup steps. Recommend 15% of max_steps min_lr: 1e-6 # Minimum learning rate, must be between 0.0 and 1.0 optim_config: # Optimizer settings lr: 1e-5 # Learning rate, must be between 0.0 and 1.0 weight_decay: 0.0 # L2 regularization strength, must be between 0.0 and 1.0 adam_beta1: 0.9 # Exponential decay rate for first-moment estimates, must be between 0.0 and 1.0 adam_beta2: 0.95 # Exponential decay rate for second-moment estimates, must be between 0.0 and 1.0 peft: # Parameter-efficient fine-tuning (LoRA) peft_scheme: "lora" # Enable LoRA for PEFT lora_tuning: alpha: 64 # Scaling factor for LoRA weights ( options: 32, 64, 96, 128, 160, 192), lora_plus_lr_ratio: 64.0

配方也包含與 Nova 1.0 大致相同的超參數。值得注意的超參數為:

  • max_steps – 您要執行任務的步驟數目。一般而言,對於一個 epoch (一個透過整個資料集執行),步驟數目 = 資料樣本數目/全域批次大小。步驟數目越大,全域批次大小越小,任務執行的時間就越長。

  • reasoning_enabled – 控制資料集的推理模式。選項:

    • true:啟用推理模式 (相當於高推理)

    • false:停用推理模式

    注意:對於 SFT,無法精細控制推理工作量。設定 reasoning_enabled: true 可啟用完整推理功能。

  • peft.peft_scheme – 將此設定為「lora」可啟用以 PEFT 為基礎的微調。將其設定為 null (無引號) 可啟用全執行微調。

啟動訓練任務

from sagemaker.pytorch import PyTorch # define OutputDataConfig path if default_prefix: output_path = f"s3://{bucket_name}/{default_prefix}/{sm_training_job_name}" else: output_path = f"s3://{bucket_name}/{sm_training_job_name}" output_kms_key = "<KMS key arn to encrypt trained model in Amazon-owned S3 bucket>" # optional, leave blank for Amazon managed encryption recipe_overrides = { "run": { "replicas": instance_count, # Required "output_s3_path": output_path }, } estimator = PyTorch( output_path=output_path, base_job_name=sm_training_job_name, role=role, disable_profiler=True, debugger_hook_config=False, instance_count=instance_count, instance_type=instance_type, training_recipe=training_recipe, recipe_overrides=recipe_overrides, max_run=432000, sagemaker_session=sagemaker_session, image_uri=image_uri, output_kms_key=output_kms_key, tags=[ {'Key': 'model_name_or_path', 'Value': model_name_or_path}, ] ) print(f"\nsm_training_job_name:\n{sm_training_job_name}\n") print(f"output_path:\n{output_path}")
from sagemaker.inputs import TrainingInput train_input = TrainingInput( s3_data=train_dataset_s3_path, distribution="FullyReplicated", s3_data_type="Converse", ) estimator.fit(inputs={"validation": val_input}, wait=False)
注意

Nova 2.0 Lite 的監督式微調不支援傳遞驗證資料集。

若要開始任務:

  • 使用資料集路徑和超參數更新配方

  • 在筆記本中執行指定的儲存格以提交訓練任務

筆記本會處理任務提交並提供狀態追蹤。