forked from aws-samples/generative-ai-cdk-constructs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsagemaker_huggingface_model_llava-stack.ts
80 lines (74 loc) · 2.91 KB
/
sagemaker_huggingface_model_llava-stack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* 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://www.apache.org/licenses/LICENSE-2.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, express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import * as path from 'path';
import * as cdk from 'aws-cdk-lib';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import { Construct } from 'constructs';
import * as genai from '@cdklabs/generative-ai-cdk-constructs';
export class SagemakerHuggingfaceModelLlavaStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Define some constants
const SG_ENDPOINT_NAME = 'llavaendpoint';
const HUGGING_FACE_MODEL_ID = 'llava-hf/llava-1.5-7b-hf';
// Custom Sagemaker Endpoint construct
const CustomHuggingFaceEndpoint = new genai.CustomSageMakerEndpoint(this, 'testllavaendpoint', {
modelId: HUGGING_FACE_MODEL_ID,
instanceType: genai.SageMakerInstanceType.ML_G5_12XLARGE,
container: genai.DeepLearningContainerImage.HUGGINGFACE_PYTORCH_INFERENCE_2_1_0_TRANSFORMERS4_37_0_GPU_PY310_CU118_UBUNTU20_04,
modelDataUrl: 's3://BUCKET/KEY',
environment: {
HF_MODEL_ID: HUGGING_FACE_MODEL_ID,
SAGEMAKER_CONTAINER_LOG_LEVEL: "20",
SAGEMAKER_REGION: "us-east-1",
},
endpointName: SG_ENDPOINT_NAME,
startupHealthCheckTimeoutInSeconds: 900,
modelDataDownloadTimeoutInSeconds: 900,
});
CustomHuggingFaceEndpoint.addToRolePolicy(
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
's3:GetObject',
's3:GetObject*',
's3:GetBucket*',
's3:List*',
],
resources: [
'BUCKET_ARN',
'BUCKET_ARN/*',
],
}),
);
// Lambda request handler used to interact with the SageMaker endpoint
const requestHandler = new lambda.Function(this, 'DemoRequestHandlerHuggingFace', {
code: lambda.Code.fromAsset(
path.join(__dirname, '../lambda')
),
functionName: 'testllavahuggingface',
handler: 'lambda.handler',
description: 'Lambda request handler used to interact with the SageMaker endpoint',
runtime: lambda.Runtime.PYTHON_3_12,
architecture: lambda.Architecture.ARM_64,
tracing: lambda.Tracing.ACTIVE,
timeout: cdk.Duration.minutes(15),
memorySize: 1024,
environment: {
'ENDPOINT_NAME': SG_ENDPOINT_NAME
}
});
CustomHuggingFaceEndpoint.grantInvoke(requestHandler);
}
}