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

Add LLM fine-tuning and evaluation template #170

Merged
merged 8 commits into from
Jul 8, 2024
Merged
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
20 changes: 10 additions & 10 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ The template contains an example SageMaker Pipeline to train a model on Abalone

![Abalone with XGBoost](docs/_static/abalone-xgboost-template.png "Abalone with XGBoost Template Architecture")

#### LLM fine-tuning and evaluation

The template is based on LLM fine-tuning template from [AWS Enterprise MLOps Framework](https://github.com/aws-samples/aws-enterprise-mlops-framework/tree/main/mlops-multi-account-cdk/mlops-sm-project-template/mlops_sm_project_template/templates/finetune_deploy_llm_product).

![LLM fine-tuning and evaluation template](docs/_static/llm-evaluate.png "LLM Evaluate Template Architecture")
![SM pipeline graph](docs/_static/llm-evaluation-pipeline-graph.png "SM Pipeline graph")

kukushking marked this conversation as resolved.
Show resolved Hide resolved
The template is based on basic multi-account template from [AWS Enterprise MLOps Framework](https://github.com/aws-samples/aws-enterprise-mlops-framework/blob/main/mlops-multi-account-cdk/mlops-sm-project-template/README.md#sagemaker-project-stack).

#### Batch Inference Template
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile modified="2024-04-15T13:30:57.955Z" host="design-inspector.a2z.com" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" etag="LSdO9LAzmekkD11aoc1V" version="10.1.8"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2yi1acsoUapkoSQL1eBLryQb1Wk2yvqbq9+i79N/jI/qF0Obe38lX0NEjvbGK3kmeu4qHRymlTk07abI6uZRB8BHffXbBj5+0zm/kECXfD20h2S4eFZLot0L5mRbRpE9xAxBF7ksgYmhiBqdliDDMlJLEmSSIGuaICNk44AosyUEWQ6L2oBRJYHSeQJFQqULhkr1519VAJczcGOncf6u5JWBO3iBo52D3hQ9dhZGS9tmtHb0XxfOU68b+naghd9htFp6nnpbmaCmGa369SAZLcloSZh2cYzWXzGUDo99FiNzYjJLk2SWJLMkmdV0tJfeVjIr93JaAE8lmSXJLImS2ouSatuxgVnaw2M17PJdDo/1OlwoX38jcaHEhY1vcqp6GRfibVyIK3AhPjou7PZbcPC1vgWni/hI4v7O4/5LgbjnR2W/s7t4tHfqXGHwff2GywFblVqDKOHEW5VdedFPffXbZvPbusV3oq3Ko2+5yq1KScJdDwl3UoSyf6vy3TMNZ5SzY+9W1ilJyUpJVkqyUg2yUoZx9t3KelDUBoQqWYvOsxYSKF0wUKptxzZsOcOG5Wkcv05uWO4Hnl/YMEj4Ul+fA3hqEnhK4CmBZ8PA0zS1lgLPnK6XwFMCzxY0UALPjgLPbdjSHuDZMFPfcuApujCKeOHWD0JRpKOC3/0f</diagram></mxfile>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Adding a comment here - empty files create issues with zipping https://github.com/aws/aws-cdk/issues/19012
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

from typing import Any

import aws_cdk
from aws_cdk import Aws
from aws_cdk import aws_cloudwatch as cloudwatch
from aws_cdk import aws_codebuild as codebuild
from aws_cdk import aws_codecommit as codecommit
from aws_cdk import aws_codepipeline as codepipeline
from aws_cdk import aws_codepipeline_actions as codepipeline_actions
from aws_cdk import aws_iam as iam
from aws_cdk import aws_s3 as s3
from aws_cdk import aws_s3_assets as s3_assets
from constructs import Construct


class BuildPipelineConstruct(Construct):
def __init__(
self,
scope: Construct,
construct_id: str,
project_name: str,
project_id: str,
model_package_group_name: str,
model_bucket: s3.IBucket,
pipeline_artifact_bucket: s3.IBucket,
repo_asset: s3_assets.Asset,
**kwargs: Any,
) -> None:
super().__init__(scope, construct_id, **kwargs)

# Define resource name
sagemaker_pipeline_name = f"{project_name}-{project_id}"
sagemaker_pipeline_description = f"{project_name} Model Build Pipeline"

# Create source repo from seed bucket/key
build_app_repository = codecommit.Repository(
self,
"Build App Code Repo",
repository_name=f"{project_name}-{construct_id}",
code=codecommit.Code.from_asset(
asset=repo_asset,
branch="main",
),
)
aws_cdk.Tags.of(build_app_repository).add("sagemaker:project-id", project_id)
aws_cdk.Tags.of(build_app_repository).add("sagemaker:project-name", project_name)

sagemaker_seedcode_bucket = s3.Bucket.from_bucket_name(
self,
"SageMaker Seedcode Bucket",
f"sagemaker-servicecatalog-seedcode-{Aws.REGION}",
)

codebuild_role = iam.Role(
self,
"CodeBuild Role",
assumed_by=iam.ServicePrincipal("codebuild.amazonaws.com"),
path="/service-role/",
)

sagemaker_execution_role = iam.Role(
self,
"SageMaker Execution Role",
assumed_by=iam.ServicePrincipal("sagemaker.amazonaws.com"),
path="/service-role/",
)

# Create a policy statement for SM and ECR pull
sagemaker_policy = iam.Policy(
self,
"SageMaker Policy",
document=iam.PolicyDocument(
statements=[
iam.PolicyStatement(
actions=[
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
],
resources=["*"],
),
iam.PolicyStatement(
actions=[
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:Describe*",
"ecr:GetAuthorizationToken",
"ecr:GetDownloadUrlForLayer",
],
resources=["*"],
),
iam.PolicyStatement(
actions=[
"kms:Encrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Decrypt",
"kms:DescribeKey",
],
effect=iam.Effect.ALLOW,
resources=[f"arn:{Aws.PARTITION}:kms:{Aws.REGION}:{Aws.ACCOUNT_ID}:key/*"],
),
]
),
)

cloudwatch.Metric.grant_put_metric_data(sagemaker_policy)
model_bucket.grant_read_write(sagemaker_policy)
sagemaker_seedcode_bucket.grant_read_write(sagemaker_policy)

sagemaker_execution_role.grant_pass_role(codebuild_role)
sagemaker_execution_role.grant_pass_role(sagemaker_execution_role)

# Attach the policy
sagemaker_policy.attach_to_role(sagemaker_execution_role)
sagemaker_policy.attach_to_role(codebuild_role)

# Grant extra permissions for the SageMaker role
sagemaker_execution_role.add_to_policy(
iam.PolicyStatement(
actions=[
"sagemaker:CreateModel",
"sagemaker:DeleteModel",
"sagemaker:DescribeModel",
"sagemaker:CreateProcessingJob",
"sagemaker:DescribeProcessingJob",
"sagemaker:StopProcessingJob",
"sagemaker:CreateTrainingJob",
"sagemaker:DescribeTrainingJob",
"sagemaker:StopTrainingJob",
"sagemaker:AddTags",
"sagemaker:DeleteTags",
"sagemaker:ListTags",
],
resources=[
f"arn:{Aws.PARTITION}:sagemaker:{Aws.REGION}:{Aws.ACCOUNT_ID}:model/*",
f"arn:{Aws.PARTITION}:sagemaker:{Aws.REGION}:{Aws.ACCOUNT_ID}:processing-job/*",
f"arn:{Aws.PARTITION}:sagemaker:{Aws.REGION}:{Aws.ACCOUNT_ID}:training-job/*",
],
)
)
sagemaker_execution_role.add_to_policy(
iam.PolicyStatement(
actions=[
"sagemaker:CreateModelPackageGroup",
"sagemaker:DeleteModelPackageGroup",
"sagemaker:DescribeModelPackageGroup",
"sagemaker:CreateModelPackage",
"sagemaker:DeleteModelPackage",
"sagemaker:UpdateModelPackage",
"sagemaker:DescribeModelPackage",
"sagemaker:ListModelPackages",
"sagemaker:AddTags",
"sagemaker:DeleteTags",
"sagemaker:ListTags",
],
resources=[
f"arn:{Aws.PARTITION}:sagemaker:{Aws.REGION}:{Aws.ACCOUNT_ID}:model-package-group/"
f"{model_package_group_name}",
f"arn:{Aws.PARTITION}:sagemaker:{Aws.REGION}:{Aws.ACCOUNT_ID}:model-package/"
f"{model_package_group_name}/*",
],
),
)

# Grant extra permissions for the CodeBuild role
codebuild_role.add_to_policy(
iam.PolicyStatement(
actions=[
"sagemaker:DescribeModelPackage",
"sagemaker:ListModelPackages",
"sagemaker:UpdateModelPackage",
"sagemaker:AddTags",
"sagemaker:DeleteTags",
"sagemaker:ListTags",
],
resources=[
f"arn:{Aws.PARTITION}:sagemaker:{Aws.REGION}:{Aws.ACCOUNT_ID}:model-package/"
f"{model_package_group_name}/*"
],
)
)
codebuild_role.add_to_policy(
iam.PolicyStatement(
actions=[
"sagemaker:CreatePipeline",
"sagemaker:UpdatePipeline",
"sagemaker:DeletePipeline",
"sagemaker:StartPipelineExecution",
"sagemaker:StopPipelineExecution",
"sagemaker:DescribePipelineExecution",
"sagemaker:ListPipelineExecutionSteps",
"sagemaker:AddTags",
"sagemaker:DeleteTags",
"sagemaker:ListTags",
],
resources=[
f"arn:{Aws.PARTITION}:sagemaker:{Aws.REGION}:{Aws.ACCOUNT_ID}:pipeline/"
f"{sagemaker_pipeline_name}",
f"arn:{Aws.PARTITION}:sagemaker:{Aws.REGION}:{Aws.ACCOUNT_ID}:pipeline/"
f"{sagemaker_pipeline_name}/execution/*",
],
),
)
codebuild_role.add_to_policy(
iam.PolicyStatement(
actions=[
"sagemaker:DescribeImageVersion",
],
resources=[
f"arn:{Aws.PARTITION}:sagemaker:{Aws.REGION}:{Aws.ACCOUNT_ID}:image-version/*",
],
)
)

# Create the CodeBuild project
sm_pipeline_build = codebuild.PipelineProject(
self,
"SM Pipeline Build",
project_name=f"{project_name}-{construct_id}",
role=codebuild_role, # figure out what actually this role would need
build_spec=codebuild.BuildSpec.from_source_filename("buildspec.yml"),
environment=codebuild.BuildEnvironment(
build_image=codebuild.LinuxBuildImage.STANDARD_5_0,
environment_variables={
"SAGEMAKER_PROJECT_NAME": codebuild.BuildEnvironmentVariable(value=project_name),
"SAGEMAKER_PROJECT_ID": codebuild.BuildEnvironmentVariable(value=project_id),
"MODEL_PACKAGE_GROUP_NAME": codebuild.BuildEnvironmentVariable(value=model_package_group_name),
"AWS_REGION": codebuild.BuildEnvironmentVariable(value=Aws.REGION),
"SAGEMAKER_PIPELINE_NAME": codebuild.BuildEnvironmentVariable(
value=sagemaker_pipeline_name,
),
"SAGEMAKER_PIPELINE_DESCRIPTION": codebuild.BuildEnvironmentVariable(
value=sagemaker_pipeline_description,
),
"SAGEMAKER_PIPELINE_ROLE_ARN": codebuild.BuildEnvironmentVariable(
value=sagemaker_execution_role.role_arn,
),
"ARTIFACT_BUCKET": codebuild.BuildEnvironmentVariable(value=model_bucket.bucket_name),
"ARTIFACT_BUCKET_KMS_ID": codebuild.BuildEnvironmentVariable(
value=model_bucket.encryption_key.key_id # type: ignore[union-attr]
),
},
),
)

source_artifact = codepipeline.Artifact(artifact_name="GitSource")

build_pipeline = codepipeline.Pipeline(
self,
"Pipeline",
pipeline_name=f"{project_name}-{construct_id}",
artifact_bucket=pipeline_artifact_bucket,
)

# add a source stage
source_stage = build_pipeline.add_stage(stage_name="Source")
source_stage.add_action(
codepipeline_actions.CodeCommitSourceAction(
action_name="Source",
output=source_artifact,
repository=build_app_repository,
branch="main",
)
)

# add a build stage
build_stage = build_pipeline.add_stage(stage_name="Build")
build_stage.add_action(
codepipeline_actions.CodeBuildAction(
action_name="SMPipeline",
input=source_artifact,
project=sm_pipeline_build,
)
)
Loading
Loading