开启步骤缓存 - 亚马逊 SageMaker AI
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

开启步骤缓存

要开启步骤缓存,必须在步骤定义中添加 CacheConfig 属性。管道定义文件中的 CacheConfig 属性使用以下格式:

{     "CacheConfig": {         "Enabled": false,         "ExpireAfter": "<time>"     } }

Enabled 字段指示是否为特定步骤开启了缓存。您可以将该字段设置为true,这会让 SageMaker AI 尝试查找具有相同属性的该步骤的上一次运行。或者,您可以将该字段设置为false,这会让 SageMaker AI 在每次管道运行时运行该步骤。 ExpireAfterISO8601 持续时间格式的字符串,用于定义超时时间。ExpireAfter 持续时间可以是年、月、周、日、小时或分钟值。每个值都由一个数字和一个表示持续时间单位的字母组成。例如:

  • “30d”= 30 天

  • “5y”= 5 年

  • “T16m”= 16 分钟

  • “30dT5h”= 30 天零 5 小时。

以下讨论描述了使用 Amaz SageMaker on Python SDK 为新的或预先存在的管道开启缓存的过程。

为新管道开启缓存

对于新管道,请通过 enable_caching=True 初始化 CacheConfig 实例,并将其作为管道步骤的输入。以下示例为训练步骤开启缓存,并设置 1 小时的超时时间:

from sagemaker.workflow.pipeline_context import PipelineSession from sagemaker.workflow.steps import CacheConfig cache_config = CacheConfig(enable_caching=True, expire_after="PT1H") estimator = Estimator(..., sagemaker_session=PipelineSession()) step_train = TrainingStep( name="TrainAbaloneModel", step_args=estimator.fit(inputs=inputs), cache_config=cache_config )

为预先存在的管道开启缓存

要为预先存在、已经定义的管道开启缓存,请打开该步骤的 enable_caching 属性,然后将 expire_after 设置为超时值。最后,使用 pipeline.upsert()pipeline.update() 更新管道。再次运行后,以下代码示例将为训练步骤开启缓存,超时时间为 1 小时:

from sagemaker.workflow.pipeline_context import PipelineSession from sagemaker.workflow.steps import CacheConfig from sagemaker.workflow.pipeline import Pipeline cache_config = CacheConfig(enable_caching=True, expire_after="PT1H") estimator = Estimator(..., sagemaker_session=PipelineSession()) step_train = TrainingStep( name="TrainAbaloneModel", step_args=estimator.fit(inputs=inputs), cache_config=cache_config ) # define pipeline pipeline = Pipeline( steps=[step_train] ) # additional step for existing pipelines pipeline.update() # or, call upsert() to update the pipeline # pipeline.upsert()

或者,在定义(预先存在的)管道之后更新缓存配置,这样就可以连续运行一段代码。以下代码示例演示了此方法:

# turn on caching with timeout period of one hour pipeline.steps[0].cache_config.enable_caching = True pipeline.steps[0].cache_config.expire_after = "PT1H" # additional step for existing pipelines pipeline.update() # or, call upsert() to update the pipeline # pipeline.upsert()

有关更详细的代码示例以及有关 Python SDK 参数如何影响缓存的讨论,请参阅 Amaz SageMaker on Python SDK 文档中的缓存配置