aws/sagemaker-hyperpod-recipes
GitHub: aws/sagemaker-hyperpod-recipes
一套基于 Amazon SageMaker 的预配置大模型训练与微调方案,提供主流开源模型的现成训练配置脚本和经过验证的技术栈。
Stars: 98 | Forks: 28
# Amazon SageMaker HyperPod Recipes
## 概述
Amazon SageMaker HyperPod recipes 可帮助客户在几分钟内开始训练和微调热门的公开基础大模型,并提供业界领先的性能表现。它们提供了一套在 Amazon SageMaker 上经过测试和验证的预配置训练技术栈。
完整文档请查看 [Amazon SageMaker HyperPod recipes 文档](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-hyperpod-recipes.html)。
- 使用 Amazon EKS 进行工作负载编排的 **Amazon SageMaker HyperPod**
- 使用 Slurm 进行工作负载编排的 **Amazon SageMaker HyperPod**
- **Amazon SageMaker 训练作业 (SMTJ)**
## 版本历史
本仓库包含 **v2.0.0** 版本的 Amazon SageMaker HyperPod recipes,其中包含了基于最新训练框架构建的 recipes。
**在寻找 v1 版本的 recipes?** 请参考 [v1 分支](../../tree/v1)。我们推荐在新项目中使用 v2 版本的 recipes,因为它们提供了改进的性能和额外的功能。
## 支持的模型与技术
### 支持的模型
- **Amazon Nova**: Micro, Lite, Pro
- **Gemma**: 4 (E4B, 26B, 31B)
- **Llama**: 3.1, 3.2, 3.3 (1B - 90B), 4 Scout (17B)
- **DeepSeek R1 Distilled**: Llama (8B, 70B), Qwen (1.5B, 7B, 14B, 32B)
- **GPT-OSS**: 20B, 120B
- **Nemotron**: 3 Nano (30B), 3 Super (120B)
- **Qwen**: 2.5 (0.5B - 72B), 3 (0.6B - 32B), 3.5 (4B - 27B), 3.6 (27b)
### 支持的技术
### 支持的加速器
- NVIDIA H100 (ml.p5.48xlarge, ml.p5e.48xlarge, ml.p5en.48xlarge)
- NVIDIA A100 (ml.p4d.24xlarge, ml.p4de.24xlarge)
- NVIDIA A10G (ml.g5.48xlarge, ml.g5.12xlarge)
### 高级训练框架
#### LLMFT (LLM Fine-Tuning Framework)
高级微调框架,针对以下内容进行了优化实现:
- DeepSeek R1 Distilled 模型(Llama 和 Qwen 变体)
- GPT-OSS 模型 (20B, 120B)
- Llama 模型 (3.1, 3.2, 3.3, 4)
- Qwen 模型 (2.5, 3)
- 技术:SFT(全量微调和 LoRA)、DPO(全量微调和 LoRA)
#### VERL (Versatile Reinforcement Learning)
使用 GRPO 算法的强化学习框架,适用于:
- Gemma 模型 (4)(仅限 LoRA)
- Llama 模型 (3.1, 3.2, 3.3)
- Nemotron 模型 (3 Nano 30B, 3 Super 120B)
- Qwen 模型 (2.5, 3, 3.5, 3.6)
- DeepSeek R1 Distilled 模型
- GPT-OSS 模型
- 技术:RLAIF 和 RLVR,均支持全量微调或 LoRA
#### 无 Checkpoint 训练
这是一种内存高效的训练方式,消除了训练过程中传统的 checkpoint 存储,显著降低了内存开销和存储需求。对于 checkpoint 大小可能极为庞大的大规模模型尤其有利。
**支持的模型:**
- Llama 3 70B (LoRA, 预训练)
- GPT-OSS 120B (全量微调, LoRA)
**主要优势:**
- 减少训练期间的内存占用
- 降低存储成本
- 加快训练迭代周期
- 非常适合大规模模型训练
**可用的 Recipes:**
- [Llama 3 70B LoRA](recipes_collection/recipes/training/llama/checkpointless_llama3_70b_lora.yaml)
- [Llama 3 70B 预训练](recipes_collection/recipes/training/llama/checkpointless_llama3_70b_pretrain.yaml)
- [GPT-OSS 120B 全量微调](recipes_collection/recipes/training/gpt_oss/checkpointless_gpt_oss_120b_full_fine_tuning.yaml)
- [GPT-OSS 120B LoRA](recipes_collection/recipes/training/gpt_oss/checkpointless_gpt_oss_120b_lora.yaml)
#### 弹性训练
**支持的模型:**
- SFT 和 DPO LLMFT 模型,例如 [LLMFT Llama3.1 8B SFT](recipes_collection/recipes/fine-tuning/llama/llmft_llama3_1_8b_instruct_seq4k_gpu_sft_lora.yaml)
**主要功能:**
- 根据资源可用性自动缩放
- 优化资源利用率
- 通过动态容量调整实现高性价比训练
- 无缝处理节点的增加和移除
- 具备自动恢复能力的容错机制
**优势:**
- 通过更好的资源利用率降低训练成本
- 提高集群效率
- 适应可用资源的灵活训练
- 最大限度减少训练期间的闲置时间
**如何使用:**
结合受支持的 SFT/DPO recipes 和[弹性训练前置条件](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-hyperpod-elastic-training.html),只需在您的启动脚本中添加以下行即可:
```
HYDRA_FULL_ERROR=1 python3 ${SAGEMAKER_TRAINING_LAUNCHER_DIR}/main.py \
...
recipes.elastic_policy.is_elastic=true \
cluster.use_hyperpod_pytorch_job=true \
cluster.queue_name= \
...
```
#### 多轮强化学习 (MTRL)
多轮强化学习通过多轮对话交互实现了强化学习训练,使模型能够从延伸的对话序列中学习,而不是单轮交互。MTRL 的一个关键特性是工具调用。
### 评估
- 开源确定性评估
- LLM-as-Judge 评估
- Nova 专用评估基准
## 安装
Amazon SageMaker HyperPod recipes 应安装在 HyperPod 集群的头节点上,或者安装在配置了 Python 虚拟环境的本地计算机上。
```
git clone --recursive git@github.com:aws/sagemaker-hyperpod-recipes.git
cd sagemaker-hyperpod-recipes
python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt
```
## 使用指南
使用 SageMaker HyperPod recipes 时,您可以创建自己的训练脚本,或使用提供了热门公开模型的现成 recipes。根据您的具体需求,您可能需要修改 recipes 中定义的预训练或微调参数。配置完成后,您可以使用 Amazon SageMaker Python SDK 在 SageMaker HyperPod(使用 Amazon EKS 进行工作负载编排)或 SageMaker 训练作业上运行训练。请注意,Amazon Nova 模型 recipes 仅兼容使用 Amazon EKS 的 SageMaker HyperPod 和 SageMaker 训练作业。
### 容器镜像
以下容器镜像可用于不同的 recipe 类型:
- **用于 LLMFT recipes**: `327873000638.dkr.ecr.us-east-1.amazonaws.com/hyperpod-recipes:llmft-v1.0.0`
- **用于 VERL 0.5.0 recipes (EKS)**: `327873000638.dkr.ecr.us-east-1.amazonaws.com/hyperpod-recipes:verl-v1.0.0-eks`
- **用于 VERL 0.5.0 recipes (SageMaker Training Jobs)**: `327873000638.dkr.ecr.us-east-1.amazonaws.com/hyperpod-recipes:verl-v1.0.0-smtj`
- **用于 VERL 0.7.0 recipes**: `327873000638.dkr.ecr.us-east-1.amazonaws.com/hyperpod-recipes-verl-0-7-0:verl-v1.1.0-smtj`
- **用于 VERL 0.7.0 Gemma 4 recipes**: `327873000638.dkr.ecr.us-east-1.amazonaws.com/hyperpod-recipes-verl-0-7-0:verl-v1.1.0-smtj-tf58`
- **用于 VERL 0.7.0 Nemotron recipes**: `327873000638.dkr.ecr.us-east-1.amazonaws.com/hyperpod-recipes-verl-0-7-0:verl-v1.1.0-smtj-vllm012`
要使用容器镜像进行训练,请在 `recipes_collection/config.yaml` 文件中修改为您选择的容器镜像:
```
container:
```
启动脚本中包含诸如 `TRAIN_DIR` 的变量,您可以通过修改启动脚本或设置环境变量来进行配置。例如:
```
EXP_DIR= TRAIN_DIR= VAL_DIR= bash ./launcher_scripts/deepseek/run_llmft_deepseek_r1_distilled_llama_8b_seq4k_gpu_sft_lora.sh
```
### 在由 Amazon EKS 编排的 SageMaker HyperPod 集群上运行 recipe
在您的集群上开始训练之前,您需要按照安装说明配置本地环境。此外,您还需要在本地计算机上安装 Kubectl 和 Helm。有关安装 [Kubectl](https://docs.aws.amazon.com/eks/latest/userguide/install-kubectl.html) 和 [Helm](https://helm.sh/docs/intro/install/) 的信息,请参阅以下文档。
使用 recipes 涉及更新 `k8s.yaml`、`config.yaml` 以及运行启动脚本。
- 在 k8s.yaml 中,更新 persistent_volume_claims。它将 Amazon FSx 声明挂载到每个计算 pod 的 /data 目录
persistent_volume_claims:
- claimName: fsx-claim
mountPath: data
- 更新您的启动脚本(例如 `launcher_scripts/deepseek/run_llmft_deepseek_r1_distilled_llama_8b_seq4k_gpu_sft_lora.sh`)
- `your_container`:使用 LLMFT 容器镜像:`327873000638.dkr.ecr.us-east-1.amazonaws.com/hyperpod-recipes:llmft-v1.0.0`
- (可选)如果您需要从 HuggingFace 获取预训练权重,可以通过设置以下键值对来提供 HuggingFace token:
recipes.model.hf_access_token=
```
#!/bin/bash
#Users should setup their cluster type in /recipes_collection/config.yaml
IMAGE="327873000638.dkr.ecr.us-east-1.amazonaws.com/hyperpod-recipes:llmft-v1.0.0"
SAGEMAKER_TRAINING_LAUNCHER_DIR=${SAGEMAKER_TRAINING_LAUNCHER_DIR:-"$(pwd)"}
EXP_DIR="" # Location to save experiment info including logging, checkpoints, etc
TRAIN_DIR="" # Location of training dataset
VAL_DIR="" # Location of validation dataset
HYDRA_FULL_ERROR=1 python3 "${SAGEMAKER_TRAINING_LAUNCHER_DIR}/main.py" \
recipes=training/deepseek/llmft_deepseek_r1_distilled_llama_8b_seq4k_gpu_sft_lora \
base_results_dir="${SAGEMAKER_TRAINING_LAUNCHER_DIR}/results" \
recipes.run.name="llmft-deepseek-r1" \
recipes.exp_manager.exp_dir="$EXP_DIR" \
cluster=k8s \
cluster_type=k8s \
container="${IMAGE}" \
recipes.model.data.train_dir=$TRAIN_DIR \
recipes.model.data.val_dir=$VAL_DIR
```
- 启动训练作业
bash launcher_scripts/deepseek/run_llmft_deepseek_r1_distilled_llama_8b_seq4k_gpu_sft_lora.sh
提交训练作业后,您可以使用以下命令验证是否已成功提交。
```
kubectl get pods
```
```
NAME READY STATUS RESTARTS AGE
llmft-deepseek-r1--worker-0 0/1 Running 0 36s
```
如果 `STATUS` 是 `PENDING` 或 `ContainerCreating`,请运行以下命令获取更多详细信息。
```
kubectl describe pod
```
当作业 `STATUS` 变为 `Running` 后,您可以使用以下命令查看日志。
```
kubectl logs
```
当您运行 `kubectl get pods` 时,`STATUS` 将变为 `Completed`。
有关 k8s 集群配置的更多信息,请参阅[在 HyperPod k8s 上运行训练作业](https://docs.aws.amazon.com/sagemaker/latest/dg/cluster-specific-configurations-run-training-job-hyperpod-k8s.html)。
要在由 Amazon EKS 编排的 SageMaker HyperPod 集群上运行 Amazon Nova recipe,您需要在集群中创建一个受限实例组。请参阅以下文档以[了解更多信息](https://docs.aws.amazon.com/sagemaker/latest/dg/nova-hp-cluster.html)。
### 在由 Slurm 编排的 SageMaker HyperPod 集群上运行 recipe
#### 配置 Recipe
在 `recipes_collection/config.yaml` 文件中更新 LLMFT 容器镜像:
```
container: 327873000638.dkr.ecr.us-east-1.amazonaws.com/hyperpod-recipes:llmft-v1.0.0
```
#### 运行训练作业
设置所需的环境变量并启动训练脚本。例如,要运行 LLMFT recipe:
```
EXP_DIR= TRAIN_DIR= VAL_DIR= bash ./launcher_scripts/llama/run_hf_llama3_8b_seq8k_gpu_fine_tuning.sh
```
或者要运行 DeepSeek R1 Distilled 模型:
```
EXP_DIR= TRAIN_DIR= VAL_DIR= bash ./launcher_scripts/deepseek/run_hf_deepseek_r1_llama_8b_seq8k_gpu_lora.sh
```
启动脚本将向您的集群提交 Slurm 作业。您可以使用标准的 Slurm 命令监控作业状态:
```
squeue # View job queue
scontrol show job # View job details
```
### 在 SageMaker 训练作业上运行 recipe
SageMaker 训练作业会自动启动一个弹性的分布式训练集群,监控基础设施,并自动从故障中恢复,以确保顺畅的训练体验。您可以利用 SageMaker Python SDK 在 SageMaker 训练作业上执行您的 recipes。
```
python3 -m venv venv
source venv/bin/activate
pip3 install --upgrade pip setuptools
# 安装 SageMaker SDK
pip install --upgrade sagemaker
```
以下 Python 代码片段演示了如何利用 SageMaker Python SDK 中的 `PyTorch` 估算器,将 recipe 提交到 SageMaker 训练作业上运行。
例如,要在 SageMaker 训练作业上运行 llama3-8b recipe,您需要设置 `training_recipe` 参数以指示要使用的 recipe:这可以是可用 recipes 中的一个,也可以是包含修改后 recipe 的 url 或本地 yaml 文件。还请通过提供 `recipe_overrides` 或直接修改 recipe yaml 文件(url 或本地文件)来修改本地目录路径和 HuggingFace 访问令牌。
```
import os
import sagemaker, boto3
from sagemaker.debugger import TensorBoardOutputConfig
from sagemaker.pytorch import PyTorch
sagemaker_session = sagemaker.Session()
role = sagemaker.get_execution_role()
bucket = sagemaker_session.default_bucket()
output = os.path.join(f"s3://{bucket}", "output")
output_path = ""
recipe_overrides = {
"run": {
"results_dir": "/opt/ml/model",
},
"exp_manager": {
"exp_dir": "",
"explicit_log_dir": "/opt/ml/output/tensorboard",
"checkpoint_dir": "/opt/ml/checkpoints",
},
"model": {
"data": {
"train_dir": "/opt/ml/input/data/train",
"val_dir": "/opt/ml/input/data/val",
},
},
}
tensorboard_output_config = TensorBoardOutputConfig(
s3_output_path=os.path.join(output, 'tensorboard'),
container_local_output_path=recipe_overrides["exp_manager"]["explicit_log_dir"]
)
estimator = PyTorch(
output_path=output_path,
base_job_name=f"llama-recipe",
role=role,
instance_type="ml.p5.48xlarge",
training_recipe="training/llama/llmft_llama3_8b_seq4k_gpu_sft_lora",
recipe_overrides=recipe_overrides,
sagemaker_session=sagemaker_session,
tensorboard_output_config=tensorboard_output_config,
)
estimator.fit(inputs={"train": "s3 or fsx input", "val": "s3 or fsx input"}, wait=True)
```
运行上述代码将使用指定的训练 recipe 创建一个 `PyTorch` 估算器对象,然后使用 `fit()` 方法训练模型。新增的 `training_recipe` 参数使您能够指定要使用的 recipe。
要了解有关在 SageMaker 训练作业上运行 Amazon Nova recipe 的更多信息,请参阅[此文档](https://docs.aws.amazon.com/sagemaker/latest/dg/nova-model-training-job.html)。
## 故障排除
在训练期间,如果 GPU 内存使用量接近上限,尝试将分片 checkpoint 保存到 S3 存储可能会导致核心转储。要解决此问题,您可以选择:
* 降低模型训练的整体内存消耗:
* 增加训练过程的计算节点数量
* 减小 batch size
* 提高分片度
* 使用 FSx 作为共享文件系统
通过采取上述任一方法,您可以缓解内存压力,并防止在保存 checkpoint 期间发生核心转储。
## 测试
```
pip install pytest
pip install pytest-cov
```
### 单元测试
要运行单元测试,请导航到根目录并使用 `python -m pytest` 命令以及任何所需的标志。
`pyproject.toml` 文件定义了始终附加到 `pytest` 命令的额外选项:
```
[tool.pytest.ini_options]
...
addopts = [
"--cache-clear",
"--quiet",
"--durations=0",
"--cov=launcher/",
# uncomment this line to see a detailed HTML test coverage report instead of the usual summary table output to stdout.
# "--cov-report=html",
"tests/",
]
```
对于包括启动 JSON 在内的黄金测试,可以通过运行 `GOLDEN_TEST_WRITE= python -m pytest` 来更新黄金输出。
## 安全
## 许可证
本项目基于 [Apache-2.0 License](LICENSE) 获得许可。
标签:Yelp, 凭据扫描, 子域名突变, 请求拦截, 逆向工具