

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

# 缓存管道步骤
<a name="pipelines-caching"></a>

在 Amazon Pipelin SageMaker es 中，您可以在重新运行管道时使用步骤缓存来节省时间和资源。当某个步骤的配置和输入相同时，步骤缓存会重复使用该步骤前一次成功运行的输出（而不是重新计算）。这可帮助您在使用相同参数重新运行管道时获得一致的结果。下面的主题将向您介绍如何为管道配置和开启步骤缓存。

使用步骤签名缓存时，Pipelines 会尝试查找当前管道步骤中某些属性值相同的前一次运行。如果发现，Pipelines 会传播上一次运行的输出结果，而不是重新计算步骤。所检查的属性特定于步骤类型，并在 [按管道步骤类型划分的默认缓存键属性](pipelines-default-keys.md) 中列出。

您必须选择步骤缓存 - 默认情况下，它处于关闭状态。开启步骤缓存时，还必须定义超时。此超时定义了之前运行可以持续多久才能继续作为重复使用的候选项。

步骤缓存仅考虑成功的运行 - 它从不重复使用失败的运行。如果超时时间内有多次成功运行，Pipelines 会使用最近一次成功运行的结果。如果在超时时间内没有成功运行，Pipelines 会重新运行该步骤。如果执行程序发现之前的运行符合条件但仍在进行中，则两个步骤都将继续运行，如果成功运行，则会更新缓存。

步骤缓存仅适用于单个管道，因此即使存在步骤签名匹配项，也无法重复使用另一个管道中的步骤。

步骤缓存可用于以下步骤类型：
+ [Processing](build-and-manage-steps-types.md#step-type-processing)
+ [训练](build-and-manage-steps-types.md#step-type-training)
+ [优化](build-and-manage-steps-types.md#step-type-tuning)
+ [AutoML](build-and-manage-steps-types.md#step-type-automl)
+ [转换](build-and-manage-steps-types.md#step-type-transform)
+ [`ClarifyCheck`](build-and-manage-steps-types.md#step-type-clarify-check)
+ [`QualityCheck`](build-and-manage-steps-types.md#step-type-quality-check)
+ [EMR](build-and-manage-steps-types.md#step-type-emr)

**Topics**
+ [开启步骤缓存](pipelines-caching-enabling.md)
+ [关闭步骤缓存](pipelines-caching-disabling.md)
+ [按管道步骤类型划分的默认缓存键属性](pipelines-default-keys.md)
+ [缓存数据访问控制](pipelines-access-control.md)

# 开启步骤缓存
<a name="pipelines-caching-enabling"></a>

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

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

`Enabled` 字段指示是否为特定步骤开启了缓存。您可以将该字段设置为`true`，这会让 SageMaker AI 尝试查找具有相同属性的该步骤的上一次运行。或者，您可以将该字段设置为`false`，这会让 SageMaker AI 在每次管道运行时运行该步骤。 `ExpireAfter`是 [ISO 8601 持续时间](https://en.wikipedia.org/wiki/ISO_8601#Durations)格式的字符串，用于定义超时时间。`ExpireAfter` 持续时间可以是年、月、周、日、小时或分钟值。每个值都由一个数字和一个表示持续时间单位的字母组成。例如：
+ “30d”= 30 天
+ “5y”= 5 年
+ “T16m”= 16 分钟
+ “30dT5h”= 30 天零 5 小时。

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

**为新管道开启缓存**

对于新管道，请通过 `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 软件开发工具包参数如何影响缓存的讨论，请参阅 Amaz SageMaker on Python 软件开发工具包文档中的[缓存配置](https://sagemaker.readthedocs.io/en/stable/amazon_sagemaker_model_building_pipeline.html#caching-configuration)。

# 关闭步骤缓存
<a name="pipelines-caching-disabling"></a>

如果您更改未在 [按管道步骤类型划分的默认缓存键属性](pipelines-default-keys.md) 中列出的管道步骤类型的任何属性，则不会重新运行该管道步骤。不过，您可能会决定无论如何都要重新运行该管道步骤。在这种情况下，您需要关闭步骤缓存。

要关闭步骤缓存，请将步骤定义的 `CacheConfig` 属性中的 `Enabled` 属性设置为 `false`，如以下代码片段所示：

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

请注意，当 `Enabled` 为 `false` 时，将忽略 `ExpireAfter` 属性。

要使用 Amaz SageMaker on Python SDK 关闭工作流工序的缓存，请定义工作流工序的管道，关闭该`enable_caching`属性，然后更新管道。

再次运行后，以下代码示例会关闭训练步骤的缓存：

```
from sagemaker.workflow.pipeline_context import PipelineSession
from sagemaker.workflow.steps import CacheConfig
from sagemaker.workflow.pipeline import Pipeline

cache_config = CacheConfig(enable_caching=False, 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]
)

# update the pipeline
pipeline.update()
# or, call upsert() to update the pipeline
# pipeline.upsert()
```

或者，在定义管道之后关闭 `enable_caching` 属性，这样就可以连续运行一段代码。以下代码示例演示了此解决方案：

```
# turn off caching for the training step
pipeline.steps[0].cache_config.enable_caching = False

# update the pipeline
pipeline.update()
# or, call upsert() to update the pipeline
# pipeline.upsert()
```

有关更详细的代码示例以及有关 Python 软件开发工具包参数如何影响缓存的讨论，请参阅 Amaz SageMaker on Python 软件开发工具包文档中的[缓存配置](https://sagemaker.readthedocs.io/en/stable/amazon_sagemaker_model_building_pipeline.html#caching-configuration)。

# 按管道步骤类型划分的默认缓存键属性
<a name="pipelines-default-keys"></a>

在决定是否重用之前的管道步骤或重新运行该步骤时，Pipelines 会检查某些属性是否发生了变化。如果这组属性与超时时间段内所有之前的运行不同，则将再次运行该步骤。这些属性包括输入构件、应用程序或算法规范以及环境变量。以下列表显示了每种管道步骤类型和属性，这些属性如果发生更改，则会启动该步骤的重新运行。有关使用哪些 Python 开发工具包参数来创建以下属性的更多信息，请参阅 Amaz SageMaker on Python 软件开发工具包文档中的[缓存配置](https://sagemaker.readthedocs.io/en/stable/amazon_sagemaker_model_building_pipeline.html#caching-configuration)。

## [处理步骤](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateProcessingJob.html)
<a name="collapsible-caching-section-1"></a>
+ AppSpecification
+ 环境
+ ProcessingInputs。 此属性包含有关预处理脚本的信息。

  

## [训练步骤](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html)
<a name="collapsible-caching-section-2"></a>
+ AlgorithmSpecification
+ CheckpointConfig
+ DebugHookConfig
+ DebugRuleConfigurations
+ 环境
+ HyperParameters
+ InputDataConfig。 此属性包含有关训练脚本的信息。

  

## [优化步骤](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateHyperParameterTuningJob.html)
<a name="collapsible-caching-section-3"></a>
+ HyperParameterTuningJobConfig
+ TrainingJobDefinition。 此属性由多个子属性组成，并非所有子属性都会导致步骤重新运行。可能导致重新运行（如果已更改）的子属性如下：
  + AlgorithmSpecification
  + HyperParameterRanges
  + InputDataConfig
  + StaticHyperParameters
  + TuningObjective
+ TrainingJobDefinitions

  

## [AutoML 步骤](https://docs.aws.amazon.com//sagemaker/latest/APIReference/API_AutoMLJobConfig.html)
<a name="collapsible-caching-section-4"></a>
+ Auto MLJob Config。此属性由多个子属性组成，并非所有子属性都会导致该步骤重新运行。可能导致重新运行（如果已更改）的子属性如下：
  + CompletionCriteria
  + CandidateGenerationConfig
  + DataSplitConfig
  + Mode
+ 自动MLJob物镜
+ InputDataConfig
+ ProblemType

  

## [转换步骤](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTransformJob.html)
<a name="collapsible-caching-section-5"></a>
+ DataProcessing
+ 环境
+ ModelName
+ TransformInput

  

## [ClarifyCheck 步](build-and-manage-steps-types.md#step-type-clarify-check)
<a name="collapsible-caching-section-6"></a>
+ ClarifyCheckConfig
+ CheckJobConfig
+ SkipCheck
+ RegisterNewBaseline
+ ModelPackageGroupName
+ SuppliedBaselineConstraints

  

## [QualityCheck 步](build-and-manage-steps-types.md#step-type-quality-check)
<a name="collapsible-caching-section-7"></a>
+ QualityCheckConfig
+ CheckJobConfig
+ SkipCheck
+ RegisterNewBaseline
+ ModelPackageGroupName
+ SuppliedBaselineConstraints
+ SuppliedBaselineStatistics

  

## [EMR 步骤](https://docs.aws.amazon.com/sagemaker/latest/dg/build-and-manage-steps.html#step-type-emr)
<a name="collapsible-caching-section-8"></a>
+ ClusterId
+ StepConfig

  

# 缓存数据访问控制
<a name="pipelines-access-control"></a>

当 A SageMaker I 管道运行时，它会缓存与管道启动的 SageMaker AI 作业相关的参数和元数据，并将其保存以供后续运行中重复使用。除了缓存的管道步骤外，还可以通过各种来源访问此元数据，包括以下类型：
+ `Describe*Job` 请求
+ CloudWatch 日志
+ CloudWatch 活动
+ CloudWatch 指标
+ SageMaker 人工智能搜索

请注意，对列表中每个数据源的访问都受其自身 IAM 权限集的控制。删除特定角色对一个数据源的访问权限不会影响对其他数据源的访问级别。例如，账户管理员可能会从调用方的角色中删除对 `Describe*Job` 请求的 IAM 权限。虽然调用方无法再发出 `Describe*Job` 请求，但只要他们有权运行管道，就仍然可以从使用缓存步骤的管道运行中检索元数据。如果账户管理员想要从特定 SageMaker AI 作业中完全移除对元数据的访问权限，则需要移除提供数据访问权限的每项相关服务的权限。