|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import json |
| 4 | +import logging |
| 5 | +import os |
| 6 | + |
| 7 | +# Import the pipeline |
| 8 | +from pipelines.pipeline import get_pipeline, upload_pipeline |
| 9 | + |
| 10 | +from aws_cdk import core |
| 11 | +from infra.batch_config import BatchConfig |
| 12 | +from infra.sagemaker_pipeline_stack import SageMakerPipelineStack |
| 13 | +from infra.model_registry import ModelRegistry |
| 14 | + |
| 15 | + |
| 16 | +# Configure the logger |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | +logging.basicConfig(level=os.environ.get("LOG_LEVEL", "INFO")) |
| 19 | + |
| 20 | + |
| 21 | +registry = ModelRegistry() |
| 22 | + |
| 23 | + |
| 24 | +def create_pipeline( |
| 25 | + app: core.App, |
| 26 | + project_name: str, |
| 27 | + project_id: str, |
| 28 | + region: str, |
| 29 | + sagemaker_pipeline_role_arn: str, |
| 30 | + artifact_bucket: str, |
| 31 | + evaluate_drift_function_arn: str, |
| 32 | + stage_name: str, |
| 33 | +): |
| 34 | + # Get the stage specific deployment config for sagemaker |
| 35 | + with open(f"{stage_name}-config.json", "r") as f: |
| 36 | + j = json.load(f) |
| 37 | + batch_config = BatchConfig(**j) |
| 38 | + |
| 39 | + # Set the model package group to project name |
| 40 | + package_group_name = project_name |
| 41 | + |
| 42 | + # If we don't have a specific champion variant defined, get the latest approved |
| 43 | + if batch_config.model_package_version is None: |
| 44 | + logger.info("Selecting latest approved") |
| 45 | + p = registry.get_latest_approved_packages(package_group_name, max_results=1)[0] |
| 46 | + batch_config.model_package_version = p["ModelPackageVersion"] |
| 47 | + batch_config.model_package_arn = p["ModelPackageArn"] |
| 48 | + else: |
| 49 | + # Get the versioned package and update ARN |
| 50 | + logger.info(f"Selecting variant version {batch_config.model_package_version}") |
| 51 | + p = registry.get_versioned_approved_packages( |
| 52 | + package_group_name, |
| 53 | + model_package_versions=[batch_config.model_package_version], |
| 54 | + )[0] |
| 55 | + batch_config.model_package_arn = p["ModelPackageArn"] |
| 56 | + |
| 57 | + # Set the default input data uri |
| 58 | + data_uri = f"s3://{artifact_bucket}/{project_id}/batch/{stage_name}" |
| 59 | + |
| 60 | + # set the output transform uri |
| 61 | + transform_uri = f"s3://{artifact_bucket}/{project_id}/transform/{stage_name}" |
| 62 | + |
| 63 | + # Get the pipeline execution to get the baseline uri |
| 64 | + pipeline_execution_arn = registry.get_pipeline_execution_arn( |
| 65 | + batch_config.model_package_arn |
| 66 | + ) |
| 67 | + logger.info(f"Got pipeline exection arn: {pipeline_execution_arn}") |
| 68 | + model_uri = registry.get_model_artifact(pipeline_execution_arn) |
| 69 | + logger.info(f"Got model uri: {model_uri}") |
| 70 | + |
| 71 | + # Set the sagemaker pipeline name and descrption with model version |
| 72 | + sagemaker_pipeline_name = f"{project_name}-batch-{stage_name}" |
| 73 | + sagemaker_pipeline_description = f"Batch Pipeline for {stage_name} model version: {batch_config.model_package_version}" |
| 74 | + |
| 75 | + # If we have drift configuration then get the baseline uri |
| 76 | + baseline_uri = None |
| 77 | + if batch_config.drift_config is not None: |
| 78 | + baseline_uri = registry.get_processing_output(pipeline_execution_arn) |
| 79 | + logger.info(f"Got baseline uri: {baseline_uri}") |
| 80 | + |
| 81 | + # Create batch pipeline |
| 82 | + pipeline = get_pipeline( |
| 83 | + region=region, |
| 84 | + role=sagemaker_pipeline_role_arn, |
| 85 | + pipeline_name=sagemaker_pipeline_name, |
| 86 | + default_bucket=artifact_bucket, |
| 87 | + base_job_prefix=project_id, |
| 88 | + evaluate_drift_function_arn=evaluate_drift_function_arn, |
| 89 | + data_uri=data_uri, |
| 90 | + model_uri=model_uri, |
| 91 | + transform_uri=transform_uri, |
| 92 | + baseline_uri=baseline_uri, |
| 93 | + ) |
| 94 | + |
| 95 | + # Create the pipeline definition |
| 96 | + logger.info("Creating/updating a SageMaker Pipeline for batch transform") |
| 97 | + pipeline_definition_body = pipeline.definition() |
| 98 | + parsed = json.loads(pipeline_definition_body) |
| 99 | + logger.info(json.dumps(parsed, indent=2, sort_keys=True)) |
| 100 | + |
| 101 | + # Upload the pipeline to S3 bucket/key and return JSON with key/value for for Cfn Stack parameters. |
| 102 | + # see: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sagemaker-pipeline.html |
| 103 | + logger.info(f"Uploading {stage_name} pipeline to {artifact_bucket}") |
| 104 | + pipeline_definition_key = upload_pipeline( |
| 105 | + pipeline, |
| 106 | + default_bucket=artifact_bucket, |
| 107 | + base_job_prefix=f"{project_id}/batch-{stage_name}", |
| 108 | + ) |
| 109 | + |
| 110 | + tags = [ |
| 111 | + core.CfnTag(key="sagemaker:deployment-stage", value=stage_name), |
| 112 | + core.CfnTag(key="sagemaker:project-id", value=project_id), |
| 113 | + core.CfnTag(key="sagemaker:project-name", value=project_name), |
| 114 | + ] |
| 115 | + |
| 116 | + SageMakerPipelineStack( |
| 117 | + app, |
| 118 | + f"drift-batch-{stage_name}", |
| 119 | + pipeline_name=sagemaker_pipeline_name, |
| 120 | + pipeline_description=sagemaker_pipeline_description, |
| 121 | + pipeline_definition_bucket=artifact_bucket, |
| 122 | + pipeline_definition_key=pipeline_definition_key, |
| 123 | + sagemaker_role_arn=sagemaker_pipeline_role_arn, |
| 124 | + tags=tags, |
| 125 | + drift_config=batch_config.drift_config, |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +def main( |
| 130 | + project_name: str, |
| 131 | + project_id: str, |
| 132 | + region: str, |
| 133 | + sagemaker_pipeline_role_arn: str, |
| 134 | + artifact_bucket: str, |
| 135 | + evaluate_drift_function_arn: str, |
| 136 | +): |
| 137 | + # Create App and stacks |
| 138 | + app = core.App() |
| 139 | + |
| 140 | + create_pipeline( |
| 141 | + app=app, |
| 142 | + project_name=project_name, |
| 143 | + project_id=project_id, |
| 144 | + region=region, |
| 145 | + sagemaker_pipeline_role_arn=sagemaker_pipeline_role_arn, |
| 146 | + artifact_bucket=artifact_bucket, |
| 147 | + evaluate_drift_function_arn=evaluate_drift_function_arn, |
| 148 | + stage_name="staging", |
| 149 | + ) |
| 150 | + |
| 151 | + create_pipeline( |
| 152 | + app=app, |
| 153 | + project_name=project_name, |
| 154 | + project_id=project_id, |
| 155 | + region=region, |
| 156 | + sagemaker_pipeline_role_arn=sagemaker_pipeline_role_arn, |
| 157 | + artifact_bucket=artifact_bucket, |
| 158 | + evaluate_drift_function_arn=evaluate_drift_function_arn, |
| 159 | + stage_name="prod", |
| 160 | + ) |
| 161 | + |
| 162 | + app.synth() |
| 163 | + |
| 164 | + |
| 165 | +if __name__ == "__main__": |
| 166 | + parser = argparse.ArgumentParser(description="Load parameters") |
| 167 | + parser.add_argument("--region", default=os.environ.get("AWS_REGION")) |
| 168 | + parser.add_argument( |
| 169 | + "--project-name", |
| 170 | + default=os.environ.get("SAGEMAKER_PROJECT_NAME"), |
| 171 | + ) |
| 172 | + parser.add_argument("--project-id", default=os.environ.get("SAGEMAKER_PROJECT_ID")) |
| 173 | + parser.add_argument( |
| 174 | + "--sagemaker-pipeline-role-arn", |
| 175 | + default=os.environ.get("SAGEMAKER_PIPELINE_ROLE_ARN"), |
| 176 | + ) |
| 177 | + parser.add_argument( |
| 178 | + "--evaluate-drift-function-arn", |
| 179 | + default=os.environ.get("EVALUATE_DRIFT_FUNCTION_ARN"), |
| 180 | + ) |
| 181 | + parser.add_argument( |
| 182 | + "--artifact-bucket", |
| 183 | + default=os.environ.get("ARTIFACT_BUCKET"), |
| 184 | + ) |
| 185 | + args = vars(parser.parse_args()) |
| 186 | + logger.info("args: {}".format(args)) |
| 187 | + main(**args) |
0 commit comments