

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

# 使用本機模式執行管道
<a name="pipelines-local-mode"></a>

在受管 SageMaker AI 服務上執行管道之前，SageMaker Pipelines 本機模式為您測試訓練、處理和推論命令碼，以及[管道參數](https://sagemaker.readthedocs.io/en/stable/amazon_sagemaker_model_building_pipeline.html#pipeline-parameters)的執行時期相容性提供一種簡單方法。透過使用本機模式，您可以使用較小的資料集在本機測試 SageMaker AI 管道。這樣一來，您可以快速輕鬆地對使用者命令碼和管道定義中的錯誤進行偵錯，而不會產生使用受管服務的成本。下列主題說明如何在本機定義和執行管道。

管道本機模式會在幕後利用 [SageMaker AI 任務本機模式](https://sagemaker.readthedocs.io/en/stable/overview.html#local-mode)。這是 SageMaker Python SDK 中的特徵，可讓您使用 Docker 容器在本機執行 SageMaker AI 內建或自訂映像。Pipelines 本機模式建置在 SageMaker AI 任務本機模式之上。因此，您看到的結果應當與單獨執行這些工作相同。例如，本機模式仍使用 Amazon S3 上傳模型成品和處理輸出。如果希望本機工作產生的資料存放在區域磁碟上，您可以使用[本機模式](https://sagemaker.readthedocs.io/en/stable/overview.html#local-mode)中提到的設定。

管道本機模式目前支援下列步驟類型：
+ [訓練步驟](build-and-manage-steps-types.md#step-type-training)
+ [處理步驟](build-and-manage-steps-types.md#step-type-processing)
+ [轉換步驟](build-and-manage-steps-types.md#step-type-transform)
+ [模型步驟](https://docs.aws.amazon.com/sagemaker/latest/dg/build-and-manage-steps.html#step-type-model-create) (僅包含建立模型引數)
+ [條件步驟](build-and-manage-steps-types.md#step-type-condition)
+ [失敗步驟](build-and-manage-steps-types.md#step-type-fail)

與允許使用[平行組態](https://sagemaker.readthedocs.io/en/stable/workflows/pipelines/sagemaker.workflow.pipelines.html#parallelism-configuration)平行執行多個步驟的受管管道服務不同，本機管道執行程式會依序執行步驟。因此，本機管道的整體執行效能可能會比在雲端上執行的管道差，這主要取決於資料集的大小、演算法以及本機電腦的功能。另請注意，在本機模式下執行的管道不會記錄在[SageMaker Experiments](https://docs.aws.amazon.com/sagemaker/latest/dg/pipelines-experiments.html)中。

**注意**  
Pipelines 本機模式與 XGBoost 之類的 SageMaker AI 演算法不相容。如果想要使用這些演算法，則必須在[指令碼模式](https://sagemaker-examples.readthedocs.io/en/latest/sagemaker-script-mode/sagemaker-script-mode.html)中使用。

為了在本機執行管道，與管道步驟和管道本身相關聯的 `sagemaker_session` 欄位的類型必須是 `LocalPipelineSession`。下列範例展示如何定義 SageMaker AI 管道以在本機執行。

```
from sagemaker.workflow.pipeline_context import LocalPipelineSession
from sagemaker.pytorch import PyTorch
from sagemaker.workflow.steps import TrainingStep
from sagemaker.workflow.pipeline import Pipeline

local_pipeline_session = LocalPipelineSession()

pytorch_estimator = PyTorch(
    sagemaker_session=local_pipeline_session,
    role=sagemaker.get_execution_role(),
    instance_type="ml.c5.xlarge",
    instance_count=1,
    framework_version="1.8.0",
    py_version="py36",
    entry_point="./entry_point.py",
)

step = TrainingStep(
    name="MyTrainingStep",
    step_args=pytorch_estimator.fit(
        inputs=TrainingInput(s3_data="s3://amzn-s3-demo-bucket/my-data/train"),
    )
)

pipeline = Pipeline(
    name="MyPipeline",
    steps=[step],
    sagemaker_session=local_pipeline_session
)

pipeline.create(
    role_arn=sagemaker.get_execution_role(), 
    description="local pipeline example"
)

// pipeline will execute locally
execution = pipeline.start()

steps = execution.list_steps()

training_job_name = steps['PipelineExecutionSteps'][0]['Metadata']['TrainingJob']['Arn']

step_outputs = pipeline_session.sagemaker_client.describe_training_job(TrainingJobName = training_job_name)
```

準備好在受管的 SageMaker Pipelines 服務上執行管道之後，您可以使用 `PipelineSession` (如下列程式碼範例所示) 取代先前的程式碼片段 `LocalPipelineSession`，然後重新執行程式碼。

```
from sagemaker.workflow.pipeline_context import PipelineSession

pipeline_session = PipelineSession()
```