选择性执行管道步骤 - Amazon SageMaker

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

选择性执行管道步骤

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

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

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

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

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

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

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

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

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

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

True 或未指定

指定的管道 ARN

管道 ARN

False

指定的管道 ARN

null 或未指定

True 或未指定

最新的管道执行

null 或未指定

False

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

有关选择性执行配置要求的更多信息,请参阅 s agemaker.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, )

无参考管道的选择性执行

以下示例演示了如何使用选择性执行来重新运行,AbaloneProcess并在同一管道AbaloneTrain中重新运行,而不提供参考 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 )