Skip to content

Commit

Permalink
Merge branch 'awslabs:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
saikatak authored May 9, 2024
2 parents 35858b6 + dba5397 commit ec7510e
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## UNRELEASED

## v1.1.0

### **Added**

- added managed autoscaling config to `sagemaker-endpoint` module
Expand Down
2 changes: 1 addition & 1 deletion DEPLOYMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
1. Clone the repository and checkout a release branch using the below command:

```
git clone --origin upstream --branch release/1.0.0 https://github.com/awslabs/aiops-modules
git clone --origin upstream --branch release/1.1.0 https://github.com/awslabs/aiops-modules
```
The release version can be replaced with the version of interest.

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0
1.1.0
24 changes: 24 additions & 0 deletions examples/manifests/sagemaker-hugging-face.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: hugging-face-mistral-endpoint
path: modules/fmops/sagemaker-hugging-face-endpoint
targetAccount: primary
parameters:
- name: hugging-face-model-id
value: mistralai/Mistral-7B-Instruct-v0.1
- name: instance-type
value: ml.g5.2xlarge
- name: deep-learning-container-image
value: huggingface-pytorch-tgi-inference:2.0.1-tgi1.1.0-gpu-py39-cu118-ubuntu20.04
- name: hugging-face-token-secret-name
value: aiops/huggingface-token
- name: vpc_id
valueFrom:
moduleMetadata:
group: networking
name: networking
key: VpcId
- name: subnet_ids
valueFrom:
moduleMetadata:
group: networking
name: networking
key: PrivateSubnetIds
23 changes: 0 additions & 23 deletions manifests/fmops-modules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,3 @@ parameters:
group: networking
name: networking
key: PrivateSubnetIds
---
name: hugging-face-mistral-endpoint
path: modules/fmops/sagemaker-hugging-face-endpoint
targetAccount: primary
parameters:
- name: hugging-face-model-id
value: mistralai/Mixtral-8x7B-Instruct-v0.1
- name: instance-type
value: ml.g5.2xlarge
- name: deep-learning-container-image
value: huggingface-pytorch-tgi-inference:2.0.1-tgi1.1.0-gpu-py39-cu118-ubuntu20.04
- name: vpc_id
valueFrom:
moduleMetadata:
group: networking
name: networking
key: VpcId
- name: subnet_ids
valueFrom:
moduleMetadata:
group: networking
name: networking
key: PrivateSubnetIds
1 change: 1 addition & 0 deletions modules/fmops/sagemaker-hugging-face-endpoint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The module uses [AWS Generative AI CDK Constructs](https://github.com/awslabs/ge

- `vpc-id` - VPC id
- `subnet-ids` - VPC subnet ids
- `hugging-face-token-secret-name` - ID of the AWS secret with the Hugging Face access token

### Module Metadata Outputs

Expand Down
3 changes: 3 additions & 0 deletions modules/fmops/sagemaker-hugging-face-endpoint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const deepLearningContainerImage: string = process.env.SEEDFARMER_PARAMETER_DEEP
const vpcId: string | undefined = process.env.SEEDFARMER_PARAMETER_VPC_ID;
const subnetIds: string[] = JSON.parse(process.env.SEEDFARMER_PARAMETER_SUBNET_IDS || ("[]" as string));

const hfTokenSecretName: string | undefined = process.env.SEEDFARMER_PARAMETER_HUGGING_FACE_TOKEN_SECRET_NAME;

const app = new cdk.App();

const stack = new SagemakerHuggingFaceEndpointStack(app, `${projectName}-${deploymentName}-${moduleName}`, {
Expand All @@ -28,6 +30,7 @@ const stack = new SagemakerHuggingFaceEndpointStack(app, `${projectName}-${deplo
deepLearningContainerImage,
vpcId,
subnetIds,
hfTokenSecretName,
env: { account, region },
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
import * as ec2 from "aws-cdk-lib/aws-ec2";
import * as iam from "aws-cdk-lib/aws-iam";
import * as secrets from "aws-cdk-lib/aws-secretsmanager";
import * as sagemaker from "aws-cdk-lib/aws-sagemaker";
import {
DeepLearningContainerImage,
Expand All @@ -19,6 +20,7 @@ interface SagemakerHuggingFaceEndpointStackProps extends cdk.StackProps {
deepLearningContainerImage: string;
vpcId?: string | undefined;
subnetIds: string[];
hfTokenSecretName?: string;
}

export class SagemakerHuggingFaceEndpointStack extends cdk.Stack {
Expand Down Expand Up @@ -83,12 +85,17 @@ export class SagemakerHuggingFaceEndpointStack extends cdk.Stack {
props.deepLearningContainerImage,
);

const hfTokenEnvironmentVars = this.getSecretTokenVars(props.hfTokenSecretName);

this.huggingFaceEndpoint = new HuggingFaceSageMakerEndpoint(this, "HuggingFace Endpoint", {
modelId: props.huggingFaceModelID,
instanceType: SageMakerInstanceType.of(props.instanceType),
container: DeepLearningContainerImage.fromDeepLearningContainerImage(containerImageRepoName, containerImageTag),
role: this.role,
vpcConfig: vpcConfig,
environment: {
...hfTokenEnvironmentVars,
},
});

cdk_nag.NagSuppressions.addResourceSuppressions(
Expand All @@ -111,4 +118,16 @@ export class SagemakerHuggingFaceEndpointStack extends cdk.Stack {

return [parts[0], parts[1]];
}

getSecretTokenVars(tokenSecretName: string | undefined): { [key: string]: string } {
if (!tokenSecretName) {
return {};
}

const secret = secrets.Secret.fromSecretNameV2(this, "HFTokenSecret", tokenSecretName);
const token = secret.secretValueFromJson("TOKEN").toString();
return {
HUGGING_FACE_HUB_TOKEN: token,
};
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as cdk from "aws-cdk-lib";
import * as secrets from "aws-cdk-lib/aws-secretsmanager";
import { Annotations, Match, Template } from "aws-cdk-lib/assertions";
import { SagemakerHuggingFaceEndpointStack } from "../lib/sagemaker-hugging-face-endpoint-stack";

Expand All @@ -16,6 +17,15 @@ describe("Sagemaker JumpStart Fm Endpoint Stack", () => {
const account = "123456789";
const region = "us-east-1";

const setupStack = new cdk.Stack(app, "setupStack", { env: { account, region } });
const secret = new secrets.Secret(setupStack, "secret", {
secretName: `${projectName}-${deploymentName}-${moduleName}`,
generateSecretString: {
secretStringTemplate: JSON.stringify({}),
generateStringKey: "TOKEN",
},
});

const stack = new SagemakerHuggingFaceEndpointStack(app, `${projectName}-${deploymentName}-${moduleName}`, {
projectName,
deploymentName,
Expand All @@ -25,6 +35,7 @@ describe("Sagemaker JumpStart Fm Endpoint Stack", () => {
deepLearningContainerImage,
vpcId,
subnetIds,
hfTokenSecretName: secret.secretName,
env: { account, region },
});

Expand All @@ -36,6 +47,18 @@ describe("Sagemaker JumpStart Fm Endpoint Stack", () => {
template.hasResource("AWS::EC2::SecurityGroup", {});
});

test("Hugging Face Token", () => {
const template = Template.fromStack(stack);

template.hasResourceProperties("AWS::SageMaker::Model", {
PrimaryContainer: {
Environment: {
HUGGING_FACE_HUB_TOKEN: Match.anyValue(),
},
},
});
});

test("No CDK Nag Errors", () => {
const errors = Annotations.fromStack(stack).findError("*", Match.stringLikeRegexp("AwsSolutions-.*"));
expect(errors).toHaveLength(0);
Expand Down

0 comments on commit ec7510e

Please sign in to comment.