Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1d6c559
Add aws batch implementation (works with example notebook)
aviruthen Dec 11, 2025
2cdb2d4
fixing unit tests and adding integration test
aviruthen Dec 11, 2025
3438a87
Merge branch 'master' into add-aws-batch
aviruthen Dec 11, 2025
a60459c
add example notebook
aviruthen Dec 11, 2025
149f02c
Merge branch 'add-aws-batch' of https://github.com/aviruthen/sagemake…
aviruthen Dec 11, 2025
41f71d6
Adding missing dependencies for aws_batch
aviruthen Dec 11, 2025
9326821
Fixing indentation bug in source code
aviruthen Dec 12, 2025
711b5a3
comment out delete resources in example notebook
aviruthen Dec 12, 2025
1e5a534
Merge branch 'master' into add-aws-batch
aviruthen Dec 12, 2025
3cd0846
Add notebook png and remove extraneous comments
aviruthen Dec 16, 2025
d41bd5a
Merge branch 'add-aws-batch' of https://github.com/aviruthen/sagemake…
aviruthen Dec 16, 2025
486377b
Merge branch 'master' into add-aws-batch
aviruthen Dec 16, 2025
5e5d0cc
Add in png correctly
aviruthen Dec 16, 2025
5030010
Merge branch 'add-aws-batch' of https://github.com/aviruthen/sagemake…
aviruthen Dec 16, 2025
8194f1b
Removing logs_from_job from session_helper
aviruthen Dec 18, 2025
f96e58a
Merge branch 'master' into add-aws-batch
aviruthen Dec 18, 2025
8121734
Adding helpers for logging
aviruthen Dec 18, 2025
3fc1eff
Merge branch 'add-aws-batch' of https://github.com/aviruthen/sagemake…
aviruthen Dec 18, 2025
9fd23a9
Make helper methods internal
aviruthen Dec 18, 2025
1dffe75
Merge branch 'master' into add-aws-batch
aviruthen Dec 18, 2025
b7f4cfb
Adding back nest asyncio dependency
aviruthen Dec 18, 2025
848dff9
Merge branch 'add-aws-batch' of https://github.com/aviruthen/sagemake…
aviruthen Dec 18, 2025
c3dab73
Merge branch 'master' into add-aws-batch
aviruthen Dec 18, 2025
4044caa
Merge branch 'master' into add-aws-batch
aviruthen Dec 19, 2025
5148e14
Updating unit tests for internal-external method changes
aviruthen Dec 19, 2025
8ab3b05
Merge branch 'add-aws-batch' of https://github.com/aviruthen/sagemake…
aviruthen Dec 19, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sagemaker-core/src/sagemaker/core/helper/session_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1877,7 +1877,7 @@ def expand_role(self, role):
if "/" in role:
return role
return self.boto_session.resource("iam").Role(role).arn


def _expand_container_def(c_def):
"""Placeholder docstring"""
Expand Down Expand Up @@ -2974,4 +2974,4 @@ def container_def(
c_def["Mode"] = container_mode
if image_config:
c_def["ImageConfig"] = image_config
return c_def
return c_def
1 change: 1 addition & 0 deletions sagemaker-train/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies = [
"jinja2>=3.0,<4.0",
"sagemaker-mlflow>=0.0.1,<1.0.0",
"mlflow>=3.0.0,<4.0.0",
"nest_asyncio>=1.5.0",
]

[project.urls]
Expand Down
Empty file.
186 changes: 186 additions & 0 deletions sagemaker-train/src/sagemaker/train/aws_batch/batch_api_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""The module provides helper function for Batch Submit/Describe/Terminal job APIs."""
from __future__ import absolute_import

import json
from typing import List, Dict, Optional
from sagemaker.train.aws_batch.constants import (
SAGEMAKER_TRAINING,
DEFAULT_TIMEOUT,
DEFAULT_SAGEMAKER_TRAINING_RETRY_CONFIG,
)
from sagemaker.train.aws_batch.boto_client import get_batch_boto_client


def _submit_service_job(
training_payload: Dict,
job_name: str,
job_queue: str,
retry_config: Optional[Dict] = None,
scheduling_priority: Optional[int] = None,
timeout: Optional[Dict] = None,
share_identifier: Optional[str] = None,
tags: Optional[Dict] = None,
) -> Dict:
"""Batch submit_service_job API helper function.

Args:
training_payload: a dict containing a dict of arguments for Training job.
job_name: Batch job name.
job_queue: Batch job queue ARN.
retry_config: Batch job retry configuration.
scheduling_priority: An integer representing scheduling priority.
timeout: Set with value of timeout if specified, else default to 1 day.
share_identifier: value of shareIdentifier if specified.
tags: A dict of string to string representing Batch tags.

Returns:
A dict containing jobArn, jobName and jobId.
"""
if timeout is None:
timeout = DEFAULT_TIMEOUT
client = get_batch_boto_client()
training_payload_tags = training_payload.pop("Tags", None)
payload = {
"jobName": job_name,
"jobQueue": job_queue,
"retryStrategy": DEFAULT_SAGEMAKER_TRAINING_RETRY_CONFIG,
"serviceJobType": SAGEMAKER_TRAINING,
"serviceRequestPayload": json.dumps(training_payload),
"timeoutConfig": timeout,
}
if retry_config:
payload["retryStrategy"] = retry_config
if scheduling_priority:
payload["schedulingPriority"] = scheduling_priority
if share_identifier:
payload["shareIdentifier"] = share_identifier
if tags or training_payload_tags:
payload["tags"] = __merge_tags(tags, training_payload_tags)
return client.submit_service_job(**payload)


def _describe_service_job(job_id: str) -> Dict:
"""Batch describe_service_job API helper function.

Args:
job_id: Job ID used.

Returns: a dict. See the sample below
{
'attempts': [
{
'serviceResourceId': {
'name': 'string',
'value': 'string'
},
'startedAt': 123,
'stoppedAt': 123,
'statusReason': 'string'
},
],
'createdAt': 123,
'isTerminated': True|False,
'jobArn': 'string',
'jobId': 'string',
'jobName': 'string',
'jobQueue': 'string',
'retryStrategy': {
'attempts': 123
},
'schedulingPriority': 123,
'serviceRequestPayload': 'string',
'serviceJobType': 'EKS'|'ECS'|'ECS_FARGATE'|'SAGEMAKER_TRAINING',
'shareIdentifier': 'string',
'startedAt': 123,
'status': 'SUBMITTED'|'PENDING'|'RUNNABLE'|'STARTING'|'RUNNING'|'SUCCEEDED'|'FAILED',
'statusReason': 'string',
'stoppedAt': 123,
'tags': {
'string': 'string'
},
'timeout': {
'attemptDurationSeconds': 123
}
}
"""
client = get_batch_boto_client()
return client.describe_service_job(jobId=job_id)


def _terminate_service_job(job_id: str, reason: Optional[str] = "default terminate reason") -> Dict:
"""Batch terminate_service_job API helper function.

Args:
job_id: Job ID
reason: A string representing terminate reason.

Returns: an empty dict
"""
client = get_batch_boto_client()
return client.terminate_service_job(jobId=job_id, reason=reason)


def _list_service_job(
job_queue: str,
job_status: Optional[str] = None,
filters: Optional[List] = None,
next_token: Optional[str] = None,
) -> Dict:
"""Batch list_service_job API helper function.

Args:
job_queue: Batch job queue ARN.
job_status: Batch job status.
filters: A list of Dict. Each contains a filter.
next_token: Used to retrieve data in next page.

Returns: A generator containing list results.

"""
client = get_batch_boto_client()
payload = {"jobQueue": job_queue}
if filters:
payload["filters"] = filters
if next_token:
payload["nextToken"] = next_token
if job_status:
payload["jobStatus"] = job_status
part_of_jobs = client.list_service_jobs(**payload)
next_token = part_of_jobs.get("nextToken")
yield part_of_jobs
if next_token:
yield from _list_service_job(job_queue, job_status, filters, next_token)


def __merge_tags(batch_tags: Optional[Dict], training_tags: Optional[List]) -> Optional[Dict]:
"""Merges Batch and training payload tags.

Returns a copy of Batch tags merged with training payload tags. Training payload tags take
precedence in the case of key conflicts.

:param batch_tags: A dict of string to string representing Batch tags.
:param training_tags: A list of `{"Key": "string", "Value": "string"}` objects representing
training payload tags.
:return: A dict of string to string representing batch tags merged with training tags.
batch_tags is returned unaltered if training_tags is None or empty.
"""
if not training_tags:
return batch_tags

training_tags_to_merge = {tag["Key"]: tag["Value"] for tag in training_tags}
batch_tags_copy = batch_tags.copy() if batch_tags else {}
batch_tags_copy.update(training_tags_to_merge)

return batch_tags_copy
33 changes: 33 additions & 0 deletions sagemaker-train/src/sagemaker/train/aws_batch/boto_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""The file provides helper function for getting Batch boto client."""
from __future__ import absolute_import

from typing import Optional
import boto3


def get_batch_boto_client(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it internal?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used in the notebook, should stay external

region: Optional[str] = None,
endpoint: Optional[str] = None,
) -> boto3.session.Session.client:
"""Helper function for getting Batch boto3 client.

Args:
region: Region specified
endpoint: Batch API endpoint.

Returns: Batch boto3 client.

"""
return boto3.client("batch", region_name=region, endpoint_url=endpoint)
34 changes: 34 additions & 0 deletions sagemaker-train/src/sagemaker/train/aws_batch/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""The file defines constants used for Batch API helper functions."""

from __future__ import absolute_import

SAGEMAKER_TRAINING = "SAGEMAKER_TRAINING"
DEFAULT_ATTEMPT_DURATION_IN_SECONDS = 86400 # 1 day in seconds.
DEFAULT_TIMEOUT = {"attemptDurationSeconds": DEFAULT_ATTEMPT_DURATION_IN_SECONDS}
POLL_IN_SECONDS = 5
JOB_STATUS_RUNNING = "RUNNING"
JOB_STATUS_COMPLETED = "SUCCEEDED"
JOB_STATUS_FAILED = "FAILED"
DEFAULT_SAGEMAKER_TRAINING_RETRY_CONFIG = {
"attempts": 1,
"evaluateOnExit": [
{
"action": "RETRY",
"onStatusReason": "Received status from SageMaker:InternalServerError: "
"We encountered an internal error. Please try again.",
},
{"action": "EXIT", "onStatusReason": "*"},
],
}
52 changes: 52 additions & 0 deletions sagemaker-train/src/sagemaker/train/aws_batch/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""The file Defines customized exception for Batch queueing"""
from __future__ import absolute_import


class NoTrainingJob(Exception):
"""Define NoTrainingJob Exception.

It means no Training job has been created by AWS Batch service.
"""

def __init__(self, value):
super().__init__(value)
self.value = value

def __str__(self):
"""Convert Exception to string.

Returns: a String containing exception error messages.

"""
return repr(self.value)


class MissingRequiredArgument(Exception):
"""Define MissingRequiredArgument exception.

It means some required arguments are missing.
"""

def __init__(self, value):
super().__init__(value)
self.value = value

def __str__(self):
"""Convert Exception to string.

Returns: a String containing exception error messages.

"""
return repr(self.value)
Loading
Loading