本地模式 - Amazon SageMaker
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本地模式

SageMaker Pipelines 本地模式是在托管 SageMaker 服务上执行管道之前,测试训练、处理和推理脚本以及管道参数的运行时兼容性的一种简单方法。在本地模式下,您可以使用较小的数据集在本地测试 SageMaker 管道。这样可以快速轻松地调试用户脚本和管道定义本身中的错误,而不会产生使用托管服务的成本。

管道本地模式在后台利用 SageMaker 作业本地模式。这是 SageMaker Python SDK 中的一项特征,允许您使用 Docker 容器在本地运行 SageMaker 内置映像或自定义映像。管道本地模式建立在 SageMaker 作业本地模式之上。因此,您将会看到与单独运行这些作业时相同的结果。例如,本地模式仍使用 Amazon S3 上传模型构件和处理输出。如果要将本地作业生成的数据存储在本地磁盘上,可以使用本地模式中提到的设置。

管道本地模式目前支持以下步骤类型:

托管管道服务允许使用并行配置并行执行多个步骤,而本地管道执行程序则是按顺序运行步骤。因此,本地管道的总体执行性能可能比在云上运行的管道差,这主要取决于数据集的大小、算法以及本地计算机的性能。另请注意,在本地模式下运行的管道不会记录在 SageMaker Experiments 中。

注意

管道本地模式与 SageMaker 算法(如 XGBoost)不兼容。如果要使用这些算法,则必须在脚本模式下使用它们。

为了在本地执行管道,与管道步骤和管道本身关联的 sagemaker_session 字段必须是 LocalPipelineSession 类型。以下示例说明如何定义要在本地执行的 SageMaker 管道。

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://my-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()