选择您的 Cookie 首选项

我们使用必要 Cookie 和类似工具提供我们的网站和服务。我们使用性能 Cookie 收集匿名统计数据,以便我们可以了解客户如何使用我们的网站并进行改进。必要 Cookie 无法停用,但您可以单击“自定义”或“拒绝”来拒绝性能 Cookie。

如果您同意,AWS 和经批准的第三方还将使用 Cookie 提供有用的网站功能、记住您的首选项并显示相关内容,包括相关广告。要接受或拒绝所有非必要 Cookie,请单击“接受”或“拒绝”。要做出更详细的选择,请单击“自定义”。

激活检查点

聚焦模式
激活检查点 - 亚马逊 SageMaker AI

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

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

激活检查点技术通过清除某些层的激活并在向后传递期间重新计算它们,来减少内存使用量。实际上,这是用额外的计算时间来换取内存使用量的减少。如果对模块进行了检查点检查,则在正向传递结束时,只有该模块的初始输入和该模块的最终输出会保留在内存中。 PyTorch 在向前传递期间,释放作为该模块内部计算一部分的任何中间张量。在检查点模块的向后传递过程中, PyTorch 重新计算这些张量。此时,有检查点的模块之外的层已经完成其向后传递,因此检查点操作的峰值内存使用量会变得更低。

SMP v2 支持 PyTorch 激活检查点模块。apply_activation_checkpointing以下是 Hugging Face GPT-NeoX 模型激活检查点的示例。

Hugging Face GPT-NeoX 模型的检查点转换器层

from transformers.models.gpt_neox import GPTNeoXLayer from torch.distributed.algorithms._checkpoint.checkpoint_wrapper import ( apply_activation_checkpointing ) # check_fn receives a module as the arg, # and it needs to return whether the module is to be checkpointed def is_transformer_layer(module): from transformers.models.gpt_neox import GPTNeoXLayer return isinstance(submodule, GPTNeoXLayer) apply_activation_checkpointing(model, check_fn=is_transformer_layer)

Hugging Face GPT-NeoX 模型的每个其他转换器层都要进行检查点检查

# check_fn receives a module as arg, # and it needs to return whether the module is to be checkpointed # here we define that function based on global variable (transformer_layers) from transformers.models.gpt_neox import GPTNeoXLayer from torch.distributed.algorithms._checkpoint.checkpoint_wrapper import ( apply_activation_checkpointing ) transformer_layers = [ m for m model.modules() if isinstance(m, GPTNeoXLayer) ] def is_odd_transformer_layer(module): return transformer_layers.index(module) % 2 == 0 apply_activation_checkpointing(model, check_fn=is_odd_transformer_layer)

或者, PyTorch 还有检查点torch.utils.checkpoint模块,Hugging Face Transformers 模型的子集使用该模块。此模块也适用于 SMP v2。但是,这需要您有权访问模型定义,才能添加检查点封装器。因此,我们建议您使用 apply_activation_checkpointing 方法。

隐私网站条款Cookie 首选项
© 2025, Amazon Web Services, Inc. 或其附属公司。保留所有权利。