使用 PyTorch 训练编译器运行 SageMaker 训练作业 - Amazon SageMaker
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 PyTorch 训练编译器运行 SageMaker 训练作业

您可以使用任何 SageMaker 接口通过 Training Compiler 运行训练作业:Amazon SageMaker Studio Classic、Amazon SageMaker 笔记本实例和 Amazon Command Line Interface。 SageMaker Amazon SDK for Python (Boto3)

使用 SageMaker Python 开发工具包

SageMaker 的训练编译 PyTorch 器可通过 SageMaker PyTorchHuggingFace框架估算器类获得。要打开 T SageMaker raining Compil compiler_config er,请将参数添加到 SageMaker 估计器中。导入 TrainingCompilerConfig 类,并将它的一个实例传递给 compiler_config 参数。以下代码示例显示了 T SageMaker raining Compiler 处于开启状态的 SageMaker 估计器类的结构。

提示

要开始使用 PyTorch 或《变形金刚》提供的预建模型,请尝试使用参考表中提供的批次大小,网址为经过测试的模型

注意

原生 PyTorch 支持在 SageMaker Python SDK v2.121.0 及更高版本中可用。请务必相应地更新 SageMaker Python 开发工具包。

注意

从 PyTorch v1.12.0 开始,“ SageMaker 训练编译器” 容器可用。 PyTorch 请注意,的 SageMaker 训练编译器容器未预先打包 Hu PyTorch gging Face Transformers。如果您需要在容器中安装库,请务必在提交训练作业时将 requirements.txt 文件添加到源目录下。

对于 PyTorch v1.11.0 及更早版本,请使用之前版本的 Hugging Face 和 T SageMaker raining Compiler 容器。 PyTorch

有关框架版本和相应容器信息的完整列表,请参阅支持的框架

有关适合您的使用案例的信息,请参阅下列选项之一。

PyTorch v1.12.0 and later

要编译和训练 PyTorch 模型,请使用 SageMaker 训练编译器配置 SageMaker PyTorch估计器,如以下代码示例所示。

注意

此原生 PyTorch 支持在 SageMaker Python SDK v2.120.0 及更高版本中可用。请务必更新 SageMaker Python 开发工具包。

from sagemaker.pytorch import PyTorch, TrainingCompilerConfig # the original max batch size that can fit into GPU memory without compiler batch_size_native=12 learning_rate_native=float('5e-5') # an updated max batch size that can fit into GPU memory with compiler batch_size=64 # update learning rate learning_rate=learning_rate_native/batch_size_native*batch_size hyperparameters={ "n_gpus": 1, "batch_size": batch_size, "learning_rate": learning_rate } pytorch_estimator=PyTorch( entry_point='train.py', source_dir='path-to-requirements-file', # Optional. Add this if need to install additional packages. instance_count=1, instance_type='ml.p3.2xlarge', framework_version='1.13.1', py_version='py3', hyperparameters=hyperparameters, compiler_config=TrainingCompilerConfig(), disable_profiler=True, debugger_hook_config=False ) pytorch_estimator.fit()
Hugging Face Transformers with PyTorch v1.11.0 and before

要使用编译和训练转换器模型 PyTorch,请使用训练编译器配置 SageMaker Hugging Face 估计器 SageMaker ,如以下代码示例所示。

from sagemaker.huggingface import HuggingFace, TrainingCompilerConfig # the original max batch size that can fit into GPU memory without compiler batch_size_native=12 learning_rate_native=float('5e-5') # an updated max batch size that can fit into GPU memory with compiler batch_size=64 # update learning rate learning_rate=learning_rate_native/batch_size_native*batch_size hyperparameters={ "n_gpus": 1, "batch_size": batch_size, "learning_rate": learning_rate } pytorch_huggingface_estimator=HuggingFace( entry_point='train.py', instance_count=1, instance_type='ml.p3.2xlarge', transformers_version='4.21.1', pytorch_version='1.11.0', hyperparameters=hyperparameters, compiler_config=TrainingCompilerConfig(), disable_profiler=True, debugger_hook_config=False ) pytorch_huggingface_estimator.fit()

要准备训练脚本,请参阅以下页面。

要查找 end-to-end 示例,请参阅以下笔记本:

PyTorch v1.12

对于 PyTorch v1.12,您可以通过在 SageMaker PyTorch估计器类的distribution参数中添加指定的pytorch_xla选项,使用 SageMaker 训练编译器运行分布式训练。

注意

此原生 PyTorch 支持在 SageMaker Python SDK v2.121.0 及更高版本中可用。请务必更新 SageMaker Python 开发工具包。

from sagemaker.pytorch import PyTorch, TrainingCompilerConfig # choose an instance type, specify the number of instances you want to use, # and set the num_gpus variable the number of GPUs per instance. instance_count=1 instance_type='ml.p3.8xlarge' num_gpus=4 # the original max batch size that can fit to GPU memory without compiler batch_size_native=16 learning_rate_native=float('5e-5') # an updated max batch size that can fit to GPU memory with compiler batch_size=26 # update learning rate learning_rate=learning_rate_native/batch_size_native*batch_size*num_gpus*instance_count hyperparameters={ "n_gpus": num_gpus, "batch_size": batch_size, "learning_rate": learning_rate } pytorch_estimator=PyTorch( entry_point='your_training_script.py', source_dir='path-to-requirements-file', # Optional. Add this if need to install additional packages. instance_count=instance_count, instance_type=instance_type, framework_version='1.13.1', py_version='py3', hyperparameters=hyperparameters, compiler_config=TrainingCompilerConfig(), distribution ={'pytorchxla' : { 'enabled': True }}, disable_profiler=True, debugger_hook_config=False ) pytorch_estimator.fit()
提示

要准备训练脚本,请参阅 PyTorch

Transformers v4.21 with PyTorch v1.11

PyTorch 在 v1.11 及更高版本中, SageMaker 训练编译器可用于分布式训练,参数中指定了pytorch_xladistribution选项。

from sagemaker.huggingface import HuggingFace, TrainingCompilerConfig # choose an instance type, specify the number of instances you want to use, # and set the num_gpus variable the number of GPUs per instance. instance_count=1 instance_type='ml.p3.8xlarge' num_gpus=4 # the original max batch size that can fit to GPU memory without compiler batch_size_native=16 learning_rate_native=float('5e-5') # an updated max batch size that can fit to GPU memory with compiler batch_size=26 # update learning rate learning_rate=learning_rate_native/batch_size_native*batch_size*num_gpus*instance_count hyperparameters={ "n_gpus": num_gpus, "batch_size": batch_size, "learning_rate": learning_rate } pytorch_huggingface_estimator=HuggingFace( entry_point='your_training_script.py', instance_count=instance_count, instance_type=instance_type, transformers_version='4.21.1', pytorch_version='1.11.0', hyperparameters=hyperparameters, compiler_config=TrainingCompilerConfig(), distribution ={'pytorchxla' : { 'enabled': True }}, disable_profiler=True, debugger_hook_config=False ) pytorch_huggingface_estimator.fit()
Transformers v4.17 with PyTorch v1.10.2 and before

对于支持的 PyTorch v1.10.2 及更早版本,Training Compiler 需要另一种机制来启动分布式 SageMaker 训练作业。要运行分布式训 SageMaker 练,Training Compiler 要求您将 SageMaker 分布式训练启动器脚本传递给entry_point参数,并将训练脚本传递给hyperparameters参数。以下代码示例显示了如何配置应用所需更改的 SageMaker Hugging Face 估算器。

from sagemaker.huggingface import HuggingFace, TrainingCompilerConfig # choose an instance type, specify the number of instances you want to use, # and set the num_gpus variable the number of GPUs per instance. instance_count=1 instance_type='ml.p3.8xlarge' num_gpus=4 # the original max batch size that can fit to GPU memory without compiler batch_size_native=16 learning_rate_native=float('5e-5') # an updated max batch size that can fit to GPU memory with compiler batch_size=26 # update learning rate learning_rate=learning_rate_native/batch_size_native*batch_size*num_gpus*instance_count training_script="your_training_script.py" hyperparameters={ "n_gpus": num_gpus, "batch_size": batch_size, "learning_rate": learning_rate, "training_script": training_script # Specify the file name of your training script. } pytorch_huggingface_estimator=HuggingFace( entry_point='distributed_training_launcher.py', # Specify the distributed training launcher script. instance_count=instance_count, instance_type=instance_type, transformers_version='4.17.0', pytorch_version='1.10.2', hyperparameters=hyperparameters, compiler_config=TrainingCompilerConfig(), disable_profiler=True, debugger_hook_config=False ) pytorch_huggingface_estimator.fit()

启动器脚本应如下所示。它包装训练脚本,并根据所选训练实例的大小配置分布式训练环境。

# distributed_training_launcher.py #!/bin/python import subprocess import sys if __name__ == "__main__": arguments_command = " ".join([arg for arg in sys.argv[1:]]) """ The following line takes care of setting up an inter-node communication as well as managing intra-node workers for each GPU. """ subprocess.check_call("python -m torch_xla.distributed.sm_dist " + arguments_command, shell=True)

以下列表是使用编译器运行 SageMaker 训练作业所需的最少参数集。

注意

使用 SageMaker Hugging Face 估算器时,必须指定transformers_versionpytorch_versionhyperparameterscompiler_config和参数才能启用 Training Compiler。 SageMaker 您无法使用 image_uri 手动指定集成了 支持的框架 上列出的深度学习容器的 Training Compiler。

  • entry_point (str) – 必需。指定训练脚本的文件名。

    注意

    要使用 Training Compiler 和 PyTorch v1.10.2 及更低版本运行分布式 SageMaker 训练,请为此参数指定启动器脚本的文件名。启动器脚本应已准备好,以便包装您的训练脚本并配置分布式训练环境。有关更多信息,请参阅以下示例笔记本:

  • source_dir (str) – 可选。如果需要安装其他包,请添加此项。要安装包,您需要在此目录下准备一个 requirements.txt 文件。

  • instance_count (int) – 必需。指定实例数。

  • instance_type (str) – 必需。指定实例类型。

  • transformers_version(str) — 仅在使用 SageMaker Hugging Face 估算器时才需要。指定训练编译器支持 SageMaker 的 Hugging Face 变形金刚库版本。要查找可用版本,请参阅 支持的框架

  • framework_versionpytorch_version (str) – 必需。指定 SageMaker 训练编译器支持的 PyTorch 版本。要查找可用版本,请参阅 支持的框架

    注意

    使用 SageMaker Hugging Face 估计器时,必须同时指定和。transformers_version pytorch_version

  • hyperparameters (dict) – 可选。为训练作业指定超参数,例如 n_gpusbatch_sizelearning_rate。启用 Train SageMaker ing Compiler 时,请尝试更大的批量大小并相应地调整学习率。要查找有关使用编译器和调整的批处理大小以提高训练速度的案例研究,请参阅经过测试的模型SageMaker 训练编译器示例笔记本和博客

    注意

    要使用 SageMaker Training Compiler 和 PyTorch v1.10.2 及更低版本运行分布式训练"training_script",您需要添加其他参数来指定您的训练脚本,如前面的代码示例所示。

  • compiler_config(TrainingCompilerConfig 对象)-激活 SageMaker 训练编译器所必需的。添加此参数可打开 “ SageMaker 训练编译器”。下面是 TrainingCompilerConfig 类的参数。

    • enabled (bool) – 可选。指定TrueFalse以打开或关闭 SageMaker 训练编译器。默认值为 True

    • debug (bool) – 可选。要从编译器加速的训练作业中接收更详细的训练日志,请将此项更改为 True。但是,额外的日志记录可能会增加开销并减缓编译后的训练作业。默认值为 False

  • distribution (dict) – 可选。要使用训练编译器运行分布式 SageMaker 训练作业,请添加distribution = { 'pytorchxla' : { 'enabled': True }}

警告

如果打开 SageMaker Debugger,可能会影响 SageMaker 训练编译器的性能。我们建议您在运行 SageMaker Training Compiler 时关闭调试器,以确保不会对性能产生影响。有关更多信息,请参阅注意事项。要关闭 Debugger 功能,请向估算器添加以下两个参数:

disable_profiler=True, debugger_hook_config=False

如果使用编译器成功启动训练作业,则在作业初始化阶段将收到以下日志:

  • TrainingCompilerConfig(debug=False)

    Found configuration for Training Compiler Configuring SM Training Compiler...
  • TrainingCompilerConfig(debug=True)

    Found configuration for Training Compiler Configuring SM Training Compiler... Training Compiler set to debug mode

使用 SageMaker CreateTrainingJob API 操作

SageMaker 必须通过 CreateTrainingJobAPI 操作的请求语法中的AlgorithmSpecificationHyperParameters字段指定训练编译器配置选项。

"AlgorithmSpecification": { "TrainingImage": "<sagemaker-training-compiler-enabled-dlc-image>" }, "HyperParameters": { "sagemaker_training_compiler_enabled": "true", "sagemaker_training_compiler_debug_mode": "false", "sagemaker_pytorch_xla_multi_worker_enabled": "false" // set to "true" for distributed training }

要查找已 SageMaker 实现 Training Compiler 的深度学习容器镜像 URI 的完整列表,请参阅支持的框架