

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# パイプラインステップの選択的実行
<a name="pipelines-selective-ex"></a>

Pipelines を使用してワークフローを作成し、ML トレーニングステップのオーケストレーションを行いながら、複数の実験フェーズを実行する必要がある場合があります。毎回完全なパイプラインを実行する代わりに、特定のステップのみを繰り返す必要がある場合があります。Pipelines を使用すると、パイプラインステップを選択的に実行できます。これにより、ML トレーニングを最適化できます。選択的実行は、以下のシナリオで役に立ちます。
+ アップストリームステップのパラメータを保持したまま、インスタンスタイプ、ハイパーパラメータ、またはその他の変数を使用して特定のステップを再起動する必要がある場合
+ パイプラインが中間ステップで失敗した場合。データ準備や特徴量抽出などの実行前のステップを再実行するとコストがかかります。パイプラインを完了するには、修正を適用して特定のステップを手動で再実行する必要がある場合もあります。

選択的実行を使用すると、パイプラインの有向非巡回グラフ (DAG) で接続されている限り、任意のステップのサブセットを実行できます。次の DAG は、パイプラインワークフローの例を説明しています。

![\[サンプルパイプラインの有向非巡回グラフ (DAG)\]](http://docs.aws.amazon.com/ja_jp/sagemaker/latest/dg/images/pipeline-full.png)


選択的実行では、`AbaloneTrain` ステップと `AbaloneEval` ステップを選択できます。ただしこれらのステップは DAG 内で接続されていないため、`AbaloneTrain` ステップと `AbaloneMSECond` ステップのみを選択して選択的実行を実行することはできません。選択的実行は、ワークフロー内の選択されていないステップについて、ステップを再計算するのではなく、リファレンスパイプライン実行からの出力を再利用します。また、選択したステップの下流にある選択されていないステップは、選択的実行では実行されません。

パイプラインで中間ステップのサブセットを実行する場合、ステップが前のステップに依存している場合があります。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` | なし — この場合は、アップストリーム依存関係のないステップを選択 | 

選択的実行設定の要件に関する詳細については、「[SageMaker.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>

次の例は、リファレンス ARN を指定せず、最新のパイプライン実行をリファレンスとして使用するオプションをオフにして、`AbaloneProcess` ステップと `AbaloneTrain` ステップの選択的実行を行うする方法を説明しています。このステップのサブセットは前のステップに依存しないため、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
)
```