

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# Pipelines 步驟
<a name="build-and-manage-steps"></a>

Pipelines 由步驟組成。這些步驟使用屬性定義管道採取的動作以及步驟之間的關係。下頁描述步驟的類型、其屬性，以及它們之間的關係。

**Topics**
+ [新增步驟](build-and-manage-steps-types.md)
+ [新增整合](build-and-manage-steps-integration.md)
+ [步驟屬性](#build-and-manage-properties)
+ [步驟平行處理](#build-and-manage-parallelism)
+ [步驟之間的資料相依性](#build-and-manage-data-dependency)
+ [步驟之間的自訂相依性](#build-and-manage-custom-dependency)
+ [步驟中的自訂映像](#build-and-manage-images)

## 步驟屬性
<a name="build-and-manage-properties"></a>

使用 `properties` 屬性在管道中的步驟之間新增資料相依性。Pipelines 會使用這些資料相依性，從管道定義建構 DAG。這些屬性可以作為預留位置值參考，並在執行期解析。

Pipelines 步驟的 `properties` 屬性與 `Describe` 呼叫針對對應 SageMaker AI 任務類型傳回的物件相符。對於每個任務類型，`Describe` 呼叫都會傳回下列回應物件：
+ `ProcessingStep` – [DescribeProcessingJob](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeProcessingJob.html)
+ `TrainingStep` – [DescribeTrainingJob](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeTrainingJob.html)
+ `TransformStep` – [DescribeTransformJob](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeTransformJob.html)

若要在建立資料相依性期間檢查每個步驟類型可參考的屬性，請參閱 [Amazon SageMaker Python SDK](https://sagemaker.readthedocs.io/en/stable) 中的 *[資料相依性 - 屬性參考](https://sagemaker.readthedocs.io/en/stable/amazon_sagemaker_model_building_pipeline.html#data-dependency-property-reference)*。

## 步驟平行處理
<a name="build-and-manage-parallelism"></a>

當步驟不依賴任何其他步驟時，其會在管道執行時立即執行。不過，平行執行過多管道步驟可能會快速耗盡可用的資源。使用 `ParallelismConfiguration` 控制管道執行的並行步驟數量。

下列範例使用 `ParallelismConfiguration` 將並行步驟限制為 5 個。

```
pipeline.create(
    parallelism_config=ParallelismConfiguration(5),
)
```

## 步驟之間的資料相依性
<a name="build-and-manage-data-dependency"></a>

您可以透過指定步驟之間的資料關係來定義 DAG 的結構。若要在步驟之間建立資料相依性，請將一個步驟的屬性作為輸入傳遞至管道中的另一個步驟。在提供輸入的步驟執行完後，接收輸入的步驟才會啟動。

資料相依性使用以下格式的 JsonPath 表示法。此格式會周遊 JSON 屬性檔案。這表示您可以視需要附加任意數量的 {{<property>}} 執行個體，以達到檔案中所需的巢狀屬性。如需有關 JsonPath 符號的詳細資訊，請參閱 [JSONPath 回購](https://github.com/json-path/JsonPath)。

```
{{<step_name>}}.properties.{{<property>}}.{{<property>}}
```

下面示範如何使用處理步驟的 `ProcessingOutputConfig` 屬性來指定 Amazon S3 儲存貯體。

```
step_process.properties.ProcessingOutputConfig.Outputs["train_data"].S3Output.S3Uri
```

若要建立資料相依性，請將儲存貯體傳遞至訓練步驟，如下所示。

```
from sagemaker.workflow.pipeline_context import PipelineSession

sklearn_train = SKLearn(..., sagemaker_session=PipelineSession())

step_train = TrainingStep(
    name="CensusTrain",
    step_args=sklearn_train.fit(inputs=TrainingInput(
        s3_data=step_process.properties.ProcessingOutputConfig.Outputs[
            "train_data"].S3Output.S3Uri
    ))
)
```

若要在建立資料相依性期間檢查每個步驟類型可參考的屬性，請參閱 [Amazon SageMaker Python SDK](https://sagemaker.readthedocs.io/en/stable) 中的[Data Dependency - Property Reference](https://sagemaker.readthedocs.io/en/stable/amazon_sagemaker_model_building_pipeline.html#data-dependency-property-reference)。**

## 步驟之間的自訂相依性
<a name="build-and-manage-custom-dependency"></a>

當您指定資料相依性時，Pipelines 會提供步驟之間的資料連線。或者，一個步驟可以存取上一個步驟中的資料，而無需直接使用 Pipelines。在此情況下，您可以建立自訂相依性，告知 Pipelines 在另一個步驟完成執行之前不要開始步驟。您可以透過指定步驟的 `DependsOn` 屬性來建立自訂相依性。

例如，下列內容定義了僅在 `A` 步驟和 `B` 步驟執行完後才開始的 `C` 步驟。

```
{
  'Steps': [
    {'Name':'A', ...},
    {'Name':'B', ...},
    {'Name':'C', 'DependsOn': ['A', 'B']}
  ]
}
```

如果相依性將建立循環相依性，Pipelines 會擲回驗證例外狀況。

下列範例會建立在處理步驟執行完後開始的訓練步驟。

```
processing_step = ProcessingStep(...)
training_step = TrainingStep(...)

training_step.add_depends_on([processing_step])
```

下列範例會建立在兩個不同的處理步驟執行完後才開始的訓練步驟。

```
processing_step_1 = ProcessingStep(...)
processing_step_2 = ProcessingStep(...)

training_step = TrainingStep(...)

training_step.add_depends_on([processing_step_1, processing_step_2])
```

下面提供了建立自訂相依性的替代方法。

```
training_step.add_depends_on([processing_step_1])
training_step.add_depends_on([processing_step_2])
```

下列範例會建立一個訓練步驟，接收來自一個處理步驟的輸入，並等待另一個處理步驟執行完成。

```
processing_step_1 = ProcessingStep(...)
processing_step_2 = ProcessingStep(...)

training_step = TrainingStep(
    ...,
    inputs=TrainingInput(
        s3_data=processing_step_1.properties.ProcessingOutputConfig.Outputs[
            "train_data"
        ].S3Output.S3Uri
    )

training_step.add_depends_on([processing_step_2])
```

下列範例示範如何擷取步驟之自訂相依性的字串清單。

```
custom_dependencies = training_step.depends_on
```

## 步驟中的自訂映像
<a name="build-and-manage-images"></a>

 在您的管道中建立步驟時，您可以使用任何可用的 SageMaker AI [深度學習容器映像](https://github.com/aws/deep-learning-containers/blob/master/available_images.md)。

您也可以在管道步驟中使用您自己的容器。因為您無法從 Studio Classic 內建立映像，所以必須先使用另一種方法建立映像，然後才能使用該映像搭配 Pipelines。

若要在為管道建立步驟時使用您自己的容器，請在估算器定義中包含映像 URI。如需使用您自己的容器搭配 SageMaker AI 的詳細資訊，請參閱[使用 Docker 容器搭配 SageMaker AI](https://docs.aws.amazon.com/sagemaker/latest/dg/docker-containers.html)。