选择性执行管道步骤 - Amazon SageMaker
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

选择性执行管道步骤

使用 SageMaker Pipelines 创建工作流和编排机器学习训练步骤时,您可能需要经历多个实验阶段。您可能只想迭代特定步骤,而不是从头到尾运行整个管道。SageMaker Pipelines 支持选择性执行管道步骤,以帮助优化机器学习训练。选择性执行在以下情况下很有用:

  • 您想使用更新的实例类型、超参数或其他变量重新启动特定步骤,同时保留上游步骤中的参数。

  • 您的管道未能完成一个中间步骤。执行过程中的先前步骤(如数据准备或特征提取)的重新运行成本很高。您可能需要引入一个修复程序,并手动重新运行某些步骤来完成管道。

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


      示例管道的有向无环图 (DAG)。

您可以在选择性执行中选择步骤 AbaloneTrainAbaloneEval,但不能只选择 AbaloneTrainAbaloneMSECond 步骤来运行选择性执行,因为这些步骤在 DAG 中不相连。对于工作流中的非选定步骤,选择性执行会重复使用参考管道执行的输出,而不是重新计算步骤。此外,处于选定步骤下游的非选定步骤不会在选择性执行中运行。

如果您选择在管道中运行中间步骤的子集,则您的步骤可能会依赖上游步骤。SageMaker 需要一个参考管道执行来为这些依赖关系提供资源。例如,如果您选择运行步骤 AbaloneTrainAbaloneEval,则需要参考管道执行中 AbaloneProcess 步骤的输出数据。您可以提供参考执行 ARN,也可以指示 SageMaker 使用最新的管道执行(这是默认行为)。如果您有参考执行,也可以从参考运行中构建运行时参数,并将它们与任何覆盖项一起提供给选择性执行运行。有关详细信息,请参阅重复使用参考执行中的运行时参数值

具体来说,您可以使用 SelectiveExecutionConfig 为选择性执行管道运行指定配置。如果您为参考管道执行指定 ARN(使用 source_pipeline_execution_arn 参数),SageMaker 将使用来自指定管道执行的上游步骤依赖关系。如果您未指定 ARN 并且存在最新的管道执行,则 SageMaker 会默认使用最新的管道执行作为参考。如果您未指定 ARN,也不希望 SageMaker 使用您最新的管道执行,请将 reference_latest_execution 设置为 False。SageMaker 最终用作参考的管道执行(无论最新还是用户指定)都必须处于 SuccessFailed 状态。

下表总结了 SageMaker 如何根据您的 SelectiveExecutionConfig 参数选择参考执行。

source_pipeline_execution_arn 参数值 reference_latest_execution 参数值 使用的参考执行
管道 ARN

True 或未指定

指定的管道 ARN

管道 ARN

False

指定的管道 ARN

null 或未指定

True 或未指定

最新的管道执行

null 或未指定

False

无 - 在这种情况下,请选择没有上游依赖关系的步骤

有关选择性执行配置要求的更多信息,请参阅 sagemaker.workflow.selective_execution_config.SelectiveExecutionConfig 文档。

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

使用用户指定的管道参考运行选择性执行

以下示例演示了如何使用选择性执行,在同一管道重新运行中使用参考管道执行重新运行 AbaloneTrainAbaloneEval

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, )

以最新管道执行作为参考的选择性执行

以下示例演示了如何使用选择性执行,在同一管道重新运行中使用最新管道执行作为参考来重新运行 AbaloneTrainAbaloneEval。由于 SageMaker 默认使用最新的管道执行,因此您可以选择将 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, )

无参考管道的选择性执行

以下示例演示了如何使用选择性执行,在同一管道重新运行中重新运行 AbaloneProcessAbaloneTrain,而不提供参考 ARN,也不允许使用最新管道运行作为参考。SageMaker 允许进行此配置,因为这种步骤子集没有上游依赖关系。

# 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, )

重复使用参考执行中的运行时参数值

您可以使用 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 )