Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug fix] Create the model object in a pipeline #45

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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: 3 additions & 1 deletion batch-inference/seedcode/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ def create_cfn_params_tags_file(config, export_params_file, export_tags_file):
json.dumps({
'region':args.aws_region,
'pipeline_name':f'{args.sagemaker_project_name}-{args.sagemaker_project_id}-BatchInference',
'base_job_prefix':f'{args.sagemaker_project_name}-{args.sagemaker_project_id}'
'base_job_prefix':f'{args.sagemaker_project_name}-{args.sagemaker_project_id}',
'model_package_arn': model_package_arn,
'model_execution_role': args.model_execution_role
})
)

Expand Down
17 changes: 2 additions & 15 deletions batch-inference/seedcode/endpoint-config-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,14 @@ Parameters:
The body of the SageMaker Pipeline.

Resources:
ModelToDeploy:
Type: AWS::SageMaker::Model
Properties:
ExecutionRoleArn: !Ref ModelExecutionRoleArn
Containers:
- ModelPackageName: !Ref ModelPackageName
Tags:
- Key: sagemaker:project-name
Value: !Sub ${SageMakerProjectName}
- Key: sagemaker:project-id
Value: !Sub ${SageMakerProjectId}
BatchPipeline:
Type: AWS::SageMaker::Pipeline
Properties:
PipelineDescription: The SM Pipeline that executes the batch inference
PipelineName: !Sub ${SageMakerProjectName}-${StageName}-BatchPipeline
RoleArn: !Ref ModelExecutionRoleArn
PipelineDefinition:
PipelineDefinitionBody: !Sub
- ${PipelineDefinitionBody}
- { ModelName: !GetAtt ModelToDeploy.ModelName }
PipelineDefinitionBody: !Ref PipelineDefinitionBody
Tags:
- Key: sagemaker:project-name
Value: !Ref SageMakerProjectName
Expand All @@ -78,7 +65,7 @@ Resources:
EventRule:
Type: AWS::Events::Rule
Properties:
Name: !Sub ${SageMakerProjectName}-${StageName}-SchedExecRule
Name: !Sub sagemaker-${SageMakerProjectName}-${StageName}-SchedExecRule
ScheduleExpression: !Sub rate(${ScheduleExpressionforPipeline})
Targets:
- Arn: !Sub arn:aws:sagemaker:${AWS::Region}:${AWS::AccountId}:pipeline/${SageMakerProjectName}-${StageName}-batch-pipeline
Expand Down
25 changes: 16 additions & 9 deletions batch-inference/seedcode/pipelines/batch_inference/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import boto3
import sagemaker
from sagemaker import model
from sagemaker import model, ModelPackage
import sagemaker.session
from sagemaker.workflow.parameters import (
ParameterInteger,
Expand Down Expand Up @@ -68,37 +68,44 @@ def get_pipeline_custom_tags(new_tags, region, sagemaker_project_arn=None):

def get_pipeline(
region,
model_package_arn,
model_execution_role,
pipeline_name="AbalonePipelineBatchInference",
base_job_prefix="Abalone",
):
"""Gets a SageMaker ML Pipeline instance working with on abalone data.

Args:
region: AWS region to create and run the pipeline.
model_name: Name of the SageMaker Model to deploy

model_package_arn: The Amazon Resource Name (ARN) of the SageMaker model package group to deploy
model_execution_role: The Amazon Resource Name (ARN) of the IAM role that SageMaker can assume to access model artifacts and docker image for deployment
base_job_prefix: Base job prefix for jobs in the SageMaker pipeline
Returns:
an instance of a pipeline
"""
sagemaker_session = get_session(region)

#### PARAMETERS
model_name = ParameterString("ModelName", default_value='${ModelName}')
batch_inference_instance_count = ParameterInteger("BatchInstanceCount", default_value=1)
batch_inference_instance_type = ParameterString("BatchInstanceType", default_value='ml.m5.xlarge')
input_path = ParameterString("InputPath", default_value=f"s3://sagemaker-servicecatalog-seedcode-{region}/dataset/abalone-dataset.csv")
output_path = ParameterString("OutputPath")

model = ModelPackage(
role=model_execution_role,
model_package_arn=model_package_arn,
sagemaker_session=sagemaker_session
)

#### SAGEMAKER CONSTRUCTS
transform = Transformer(
model_name=model_name,
transform = model.transformer(
instance_count=1,
instance_type='ml.m5.xlarge',
instance_type='ml.m5.large',
output_path=output_path,
base_transform_job_name=f"{base_job_prefix}/batch-transform-job",
max_payload=10,
accept='text/csv'
)
transform.base_transform_job_name = f"{base_job_prefix}/batch-transform-job",

#### STEPS
transform_step = TransformStep(
Expand All @@ -110,7 +117,7 @@ def get_pipeline(
#### PIPELINE
pipeline = Pipeline(
name=pipeline_name,
parameters=[model_name, batch_inference_instance_count, batch_inference_instance_type, input_path, output_path],
parameters=[batch_inference_instance_count, batch_inference_instance_type, input_path, output_path],
steps=[transform_step],
sagemaker_session=sagemaker_session
)
Expand Down