View a markdown version of this page

在 SageMaker 训练作业中对 Nova 2.0 进行监督式微调(SFT) - Amazon Nova

在 SageMaker 训练作业中对 Nova 2.0 进行监督式微调(SFT)

先决条件

在开始训练作业之前,请注意具备以下内容:

  • Amazon S3 存储桶,用于存储您的输入数据和训练作业的输出。您可以为这两者使用一个存储桶,也可以为每种类型的数据使用不同的存储桶。确保您的存储桶位于您创建所有其他训练资源所用的 AWS 区域。有关更多信息,请参阅创建通用存储桶。

  • 具有运行训练作业权限的 IAM 角色。请务必为 IAM 策略附加 AmazonSageMakerFullAccess。有关更多信息,请参阅如何使用 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 Recipes 中“微调”文件夹下的配方。对于 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:希望作业运行的步数。通常,1 轮 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 的监督式微调不支持传递验证数据集。

要启动作业,请执行以下操作:

  • 更新配方中的数据集路径与超参数

  • 运行笔记本中指定的代码单元,提交训练作业

笔记本将自动处理作业提交,并提供状态跟踪功能。