本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
在本页上,您将学习如何使用 SageMaker 模型并行度库 v2, APIs 并开始在训练平台或集群上运行 PyTorch 完全分片数据并行 (FSDP) SageMaker 训练作业。 SageMaker HyperPod
使用 SMP v2 运行 PyTorch 训练作业有多种场景。
-
要进行 SageMaker 训练,请使用预先构建的 PyTorch v2.0.1 及更高版本的 SageMaker 框架容器,该容器已预先打包在 SMP v2 中。
-
使用 SMP v2 二进制文件设置 Conda 环境,以便在集群上运行分布式训练工作负载。 SageMaker HyperPod
-
扩展适用于 PyTorch v2.0.1 及更高版本的预构建 SageMaker 框架容器,为您的用例安装任何其他功能要求。要了解如何扩展预构建的容器,请参阅 扩展预构建容器。
-
您也可以自带 Docker 容器,使用培训工具包
手动设置所有 SageMaker训练环境并安装 SMP v2 二进制文件。SageMaker 由于依赖关系的复杂性,这是最不推荐的选项。要了解如何运行自己的 Docker 容器,请参阅调整自己的训练容器。
本入门指南涵盖前两个场景。
第 1 步:调整你的 PyTorch FSDP 训练脚本
要激活和配置 SMP v2 库,首先要在脚本顶部导入并添加 torch.sagemaker.init()
模块。本模块采用您将在 步骤 2:启动训练作业 中准备的 SMP v2 核心功能配置参数 的 SMP 配置字典。此外,为了使用 SMP v2 提供的各种核心功能,您可能还需要对训练脚本进行一些修改。有关如何调整训练脚本以使用 SMP v2 核心功能的更详细说明,请参阅 SageMaker 模型并行度库 v2 的核心功能。
在训练脚本中添加以下两行代码,这是开始使用 SMP v2 进行训练的最低要求。在中步骤 2:启动训练作业,你将通过 SageMaker PyTorch
估计器类的distribution
参数设置一个带有 SMP 配置字典的估计器类的对象。
import torch.sagemaker as tsm
tsm.init()
注意
您也可以直接将 SMP v2 核心功能配置参数 的配置字典传递给 torch.sagemaker.init()
模块。但是,传递给 PyTorch 估算器的参数具有优先级,并会覆盖为模块指定的参数。步骤 2:启动训练作业 torch.sagemaker.init()
步骤 2:启动训练作业
了解如何配置 SMP 分发选项,以启动具有 SM PyTorch P 核心功能的 FSDP 训练作业。
在 Pyth SageMaker on SDK 中设置PyTorch 框架估算distribution
参数进行配置。
注意
从 v2.200 开始,SMP v2 的distribution
配置已集成在 Pyt SageMaker hon SDK 中。请务必使用 SageMaker Python 软件开发工具包 v2.200 或更高版本。
注意
在 SMP v2 中,应将smdistributed
估计torch_distributed
器的distribution
参数配置为。 SageMaker PyTorch
使用torch_distributed
, SageMaker AI 运行torchrun
,这是 Distributed 的默认多节点任务启动器PyTorch 。
from sagemaker.pytorch import PyTorch
estimator = PyTorch(
framework_version=2.2.0
,
py_version="310
"
# image_uri="<smp-docker-image-uri>" # For using prior versions, specify the SMP image URI directly.
entry_point="your-training-script.py
", # Pass the training script you adapted with SMP from Step 1.
... # Configure other required and optional parameters
distribution={
"torch_distributed": { "enabled": True },
"smdistributed": {
"modelparallel": {
"enabled": True,
"parameters": {
"hybrid_shard_degree": Integer
,
"sm_activation_offloading": Boolean
,
"activation_loading_horizon": Integer
,
"fsdp_cache_flush_warnings": Boolean
,
"allow_empty_shards": Boolean
,
"tensor_parallel_degree": Integer
,
"expert_parallel_degree": Integer
,
"random_seed": Integer
}
}
}
}
)
重要
要使用 PyTorch 或 SMP 的先前版本而不是最新版本,则需要使用image_uri
参数而不是和对直接指定 SMP Docker 镜像。framework_version
py_version
以下是一个示例
estimator = PyTorch(
...,
image_uri="658645717510.dkr.ecr.us-west-2.amazonaws.com/smdistributed-modelparallel:2.2.0-gpu-py310-cu121"
)
要查找 SMP Docker 镜像 URIs,请参阅。支持的框架