View a markdown version of this page

將任務提交至配額共享 - AWS Batch

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

將任務提交至配額共享

配額管理任務佇列要求所有任務在提交任務時指定配額共享。若要將任務提交至配額共享,請在 SubmitServiceJob quotaShareName中指定 。preemptionConfiguration 可以選擇性地提供 ,以限制任務嘗試進入 之前的先佔嘗試次數FAILED。若要限制任務體驗的先佔數量,請在提交任務時於 ServiceJobPreemptionConfiguration preemptionRetriesBeforeTermination中設定 。

先決條件

在將任務提交至配額共享之前,請確定您有:

將服務任務提交至配額共享

下表顯示如何使用 SageMaker Python SDK 或 CLI AWS 將服務任務提交至配額共享:

Submit using the SageMaker Python SDK

SageMaker Python SDK 具有內建支援,可將任務提交至已啟用配額管理的任務佇列。下列範例示範如何建立模型訓練器、建立訓練佇列,以及將任務提交至配額共享。如需完整範例,請參閱 GitHub 上的完整範例筆記本

建立ModelTrainer定義訓練任務組態的 。

from sagemaker.train.model_trainer import ModelTrainer from sagemaker.train.configs import SourceCode, Compute, StoppingCondition source_code = SourceCode(command="echo 'Hello World'") model_trainer = ModelTrainer( training_image="123456789012.dkr.ecr.us-east-1.amazonaws.com/pytorch-training:2.5-gpu-py311", source_code=source_code, base_job_name="my-training-job", compute=Compute(instance_type="ml.g5.xlarge", instance_count=1), stopping_condition=StoppingCondition(max_runtime_in_seconds=300), )

建立依名稱參考已啟用配額管理任務佇列的TrainingQueue物件。

from sagemaker.train.aws_batch.training_queue import TrainingQueue queue = TrainingQueue("my-sagemaker-job-queue")

呼叫 queue.submit並指定 ,將任務提交至配額共享quota_share_name。您應該設定 priority來影響配額共享中的任務排序。真實世界ModelTrainer將需要 ,inputs才能擁有要訓練的資料。

job = queue.submit( job_name="my-training-job", training_job=model_trainer, quota_share_name="my_quota_share", priority=3, inputs=None, )
Submit using the AWS CLI

下列範例使用 submit-service-job命令將任務提交至配額共享。

aws batch submit-service-job \ --job-name "my-sagemaker-training-job" \ --job-queue "my-sagemaker-job-queue" \ --service-job-type "SAGEMAKER_TRAINING" \ --quota-share-name "my_quota_share" \ --timeout-config '{"attemptDurationSeconds":3600}' \ --scheduling-priority 5 \ --service-request-payload '{\"TrainingJobName\": \"sagemaker-training-job-example\", \"AlgorithmSpecification\": {\"TrainingImage\": \"123456789012.dkr.ecr.us-east-1.amazonaws.com/pytorch-inference:1.8.0-cpu-py3\", \"TrainingInputMode\": \"File\", \"ContainerEntrypoint\": [\"sleep\", \"1\"]}, \"RoleArn\":\"arn:aws:iam::123456789012:role/SageMakerExecutionRole\", \"OutputDataConfig\": {\"S3OutputPath\": \"s3://example-bucket/model-output/\"}, \"ResourceConfig\": {\"InstanceType\": \"ml.m5.large\", \"InstanceCount\": 1, \"VolumeSizeInGB\": 1}}'"