

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

# 將任務提交至配額共享
<a name="submit-job-quota-share"></a>

配額管理任務佇列要求所有任務在提交任務時指定配額共享。若要將任務提交至配額共享，請在 [SubmitServiceJob](https://docs.aws.amazon.com/batch/latest/APIReference/API_SubmitServiceJob.html) `quotaShareName`中指定 。`preemptionConfiguration` 可以選擇性地提供 ，以限制任務嘗試進入 之前的先佔嘗試次數`FAILED`。若要限制任務體驗的先佔數量，請在提交任務時於 [ServiceJobPreemptionConfiguration](https://docs.aws.amazon.com/batch/latest/APIReference/API_ServiceJobPreemptionConfiguration.html) `preemptionRetriesBeforeTermination`中設定 。

## 先決條件
<a name="submit-job-quota-share-prerequisites"></a>

在將任務提交至配額共享之前，請確定您有：
+ **配額管理資源** – 為配額管理設定的排程政策、服務環境和任務佇列。如需詳細資訊，請參閱[建立配額管理資源](create-quota-management-resources.md)。
+ **配額共享** – 在任務佇列上建立至少一個配額共享。如需詳細資訊，請參閱[建立配額共享](create-quota-shares.md)。
+ **IAM 許可** – 提交任務的許可 AWS Batch。如需詳細資訊，請參閱[AWS Batch IAM 政策、角色和許可](IAM_policies.md)。

## 將服務任務提交至配額共享
<a name="submit-job-quota-share-example"></a>

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

------
#### [ Submit using the SageMaker Python SDK ]

[SageMaker Python SDK](https://sagemaker.readthedocs.io/en/stable/v3-examples/training-examples/aws_batch/sm-training-queues_quota-management.html) 具有內建支援，可將任務提交至已啟用配額管理的任務佇列。下列範例示範如何建立模型訓練器、建立訓練佇列，以及將任務提交至配額共享。如需完整範例，請參閱 GitHub 上的[完整範例筆記本](https://github.com/aws/sagemaker-python-sdk/blob/master/v3-examples/training-examples/aws_batch/sm-training-queues_quota-management.ipynb)。

建立`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}}'}}"
```

------