

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

# 选择性执行管道步骤
<a name="pipelines-selective-ex"></a>

在使用管道创建工作流程和编排 ML 训练步骤时，您可能需要进行多个实验阶段。您可能只想重复某些步骤，而不是每次都运行完整的管道。通过管道，您可以有选择性地执行管道步骤。这有助于优化您的 ML 训练。选择性执行在以下情况下很有用：
+ 您想使用更新的实例类型、超参数或其他变量重新启动特定步骤，同时保留上游步骤中的参数。
+ 您的管道未能完成一个中间步骤。执行过程中的先前步骤（如数据准备或特征提取）的重新运行成本很高。您可能需要引入一个修复程序，并手动重新运行某些步骤来完成管道。

使用选择性执行，您可以选择运行任何步骤子集，前提是这些步骤在管道的有向无环图 (DAG) 中相连。以下 DAG 显示了管道工作流示例：

![\[示例管道的有向无环图 (DAG)。\]](http://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/images/pipeline-full.png)


您可以在选择性执行中选择步骤 `AbaloneTrain` 和 `AbaloneEval`，但不能只选择 `AbaloneTrain` 和 `AbaloneMSECond` 步骤，因为这些步骤在 DAG 中没有连接。对于工作流程中的非选定步骤，选择性执行会重复使用参考管道执行的输出，而不是重新运行这些步骤。此外，处于选定步骤下游的非选定步骤不会在选择性执行中运行。

如果您选择在管道中运行中间步骤的子集，则您的步骤可能取决于之前的步骤。 SageMaker AI 需要一个参考管道执行来为这些依赖项提供资源。例如，如果选择运行步骤 `AbaloneTrain` 和 `AbaloneEval`，则需要 `AbaloneProcess` 步骤的输出。您可以提供参考执行 ARN，也可以指示 SageMaker AI 使用最新的管道执行（这是默认行为）。如果您有参考执行，还可以从参考运行中创建运行时参数，并通过重载将其提供给选择性执行运行。有关更多信息，请参阅 [重复使用参考执行中的运行时参数值](#pipelines-selective-ex-reuse)。

具体来说，您可以使用 `SelectiveExecutionConfig` 为选择性执行管道运行提供配置。如果您为参考管道执行添加 ARN（带`source_pipeline_execution_arn`参数）， SageMaker AI 将使用您提供的管道执行中前一步的依赖关系。如果您未包含 ARN 并且存在最新的管道执行，则 SageMaker AI 会默认将其用作参考。如果您不包含 ARN 且不希望 SageMaker AI 使用您最新的管道执行，请设置为`reference_latest_execution`。`False` SageMaker AI 最终用作参考的管道执行，无论是最新的还是用户指定的，都必须处于`Success`或`Failed`状态。

下表汇总了 SageMaker AI 如何选择参考执行。


| `source_pipeline_execution_arn` 参数值 | `reference_latest_execution` 参数值 | 使用的参考执行 | 
| --- | --- | --- | 
| 管道 ARN | `True` 或未指定 | 指定的管道 ARN | 
| 管道 ARN | `False` | 指定的管道 ARN | 
| null 或未指定 | `True` 或未指定 | 最新的管道执行 | 
| null 或未指定 | `False` | 无 - 在这种情况下，请选择没有上游依赖关系的步骤 | 

有关选择性执行配置要求的更多信息，请参阅 s [agemaker.workflow.selective\$1execution\$1config。 SelectiveExecutionConfig](https://sagemaker.readthedocs.io/en/stable/workflows/pipelines/sagemaker.workflow.pipelines.html#selective-execution-config)文档。

以下讨论包括适用于一些情况的示例，这些情况是：您要指定管道参考执行；使用最新的管道执行作为参考；或者在没有参考管道执行的情况下运行选择性执行。

## 使用用户指定的管道参考运行选择性执行
<a name="pipelines-selective-ex-arn"></a>

下面的示例演示了使用参考管道执行方式选择性执行步骤 `AbaloneTrain` 和 `AbaloneEval`。

```
from sagemaker.workflow.selective_execution_config import SelectiveExecutionConfig

selective_execution_config = SelectiveExecutionConfig(
    source_pipeline_execution_arn="arn:aws:sagemaker:us-west-2:123123123123:pipeline/abalone/execution/123ab12cd3ef", 
    selected_steps=["AbaloneTrain", "AbaloneEval"]
)

selective_execution = pipeline.start(
    execution_display_name=f"Sample-Selective-Execution-1",
    parameters={"MaxDepth":6, "NumRound":60},
    selective_execution_config=selective_execution_config,
)
```

## 以最新管道执行作为参考的选择性执行
<a name="pipelines-selective-ex-latest"></a>

下面的示例演示了以最新的管道执行为参考，有选择地执行步骤 `AbaloneTrain` 和 `AbaloneEval`。由于 SageMaker AI 默认使用最新的管道执行，因此您可以选择将`reference_latest_execution`参数设置为`True`。

```
# Prepare a new selective execution. Select only the first step in the pipeline without providing source_pipeline_execution_arn.
selective_execution_config = SelectiveExecutionConfig(
    selected_steps=["AbaloneTrain", "AbaloneEval"],
    # optional
    reference_latest_execution=True
)

# Start pipeline execution without source_pipeline_execution_arn
pipeline.start(
    execution_display_name=f"Sample-Selective-Execution-1",
    parameters={"MaxDepth":6, "NumRound":60},
    selective_execution_config=selective_execution_config,
)
```

## 无参考管道的选择性执行
<a name="pipelines-selective-ex-none"></a>

以下示例演示了在`AbaloneTrain`不提供参考 ARN 的情况下选择性地执行这些步骤`AbaloneProcess`，并关闭了使用最新管道运行作为参考的选项。 SageMaker AI 允许这种配置，因为这部分步骤不依赖于之前的步骤。

```
# Prepare a new selective execution. Select only the first step in the pipeline without providing source_pipeline_execution_arn.
selective_execution_config = SelectiveExecutionConfig(
    selected_steps=["AbaloneProcess", "AbaloneTrain"],
    reference_latest_execution=False
)

# Start pipeline execution without source_pipeline_execution_arn
pipeline.start(
    execution_display_name=f"Sample-Selective-Execution-1",
    parameters={"MaxDepth":6, "NumRound":60},
    selective_execution_config=selective_execution_config,
)
```

## 重复使用参考执行中的运行时参数值
<a name="pipelines-selective-ex-reuse"></a>

您可以使用 `build_parameters_from_execution` 从参考管道执行中构建参数，并将结果提供给选择性执行管道。您可以使用参考执行中的原始参数，也可以使用 `parameter_value_overrides` 参数应用任何覆盖。

以下示例说明了如何从参考执行构建参数以及如何对 `MseThreshold` 参数应用覆盖。

```
# Prepare a new selective execution.
selective_execution_config = SelectiveExecutionConfig(
    source_pipeline_execution_arn="arn:aws:sagemaker:us-west-2:123123123123:pipeline/abalone/execution/123ab12cd3ef",
    selected_steps=["AbaloneTrain", "AbaloneEval", "AbaloneMSECond"],
)
# Define a new parameters list to test.
new_parameters_mse={
    "MseThreshold": 5,
}

# Build parameters from reference execution and override with new parameters to test.
new_parameters = pipeline.build_parameters_from_execution(
    pipeline_execution_arn="arn:aws:sagemaker:us-west-2:123123123123:pipeline/abalone/execution/123ab12cd3ef",
    parameter_value_overrides=new_parameters_mse
)

# Start pipeline execution with new parameters.
execution = pipeline.start(
    selective_execution_config=selective_execution_config,
    parameters=new_parameters
)
```