2.141.0-alpha.0 • Published 3 days ago

@aws-cdk/aws-sagemaker-alpha v2.141.0-alpha.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
3 days ago

Amazon SageMaker Construct Library


cdk-constructs: Experimental

The APIs of higher level constructs in this module are experimental and under active development. They are subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model and breaking changes will be announced in the release notes. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.


Amazon SageMaker provides every developer and data scientist with the ability to build, train, and deploy machine learning models quickly. Amazon SageMaker is a fully-managed service that covers the entire machine learning workflow to label and prepare your data, choose an algorithm, train the model, tune and optimize it for deployment, make predictions, and take action. Your models get to production faster with much less effort and lower cost.

Model

To create a machine learning model with Amazon Sagemaker, use the Model construct. This construct includes properties that can be configured to define model components, including the model inference code as a Docker image and an optional set of separate model data artifacts. See the AWS documentation to learn more about SageMaker models.

Single Container Model

In the event that a single container is sufficient for your inference use-case, you can define a single-container model:

import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';
import * as path from 'path';

const image = sagemaker.ContainerImage.fromAsset(path.join('path', 'to', 'Dockerfile', 'directory'));
const modelData = sagemaker.ModelData.fromAsset(path.join('path', 'to', 'artifact', 'file.tar.gz'));

const model = new sagemaker.Model(this, 'PrimaryContainerModel', {
  containers: [
    {
      image: image,
      modelData: modelData,
    }
  ]
});

Inference Pipeline Model

An inference pipeline is an Amazon SageMaker model that is composed of a linear sequence of multiple containers that process requests for inferences on data. See the AWS documentation to learn more about SageMaker inference pipelines. To define an inference pipeline, you can provide additional containers for your model:

import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

declare const image1: sagemaker.ContainerImage;
declare const modelData1: sagemaker.ModelData;
declare const image2: sagemaker.ContainerImage;
declare const modelData2: sagemaker.ModelData;
declare const image3: sagemaker.ContainerImage;
declare const modelData3: sagemaker.ModelData;

const model = new sagemaker.Model(this, 'InferencePipelineModel', {
  containers: [
    { image: image1, modelData: modelData1 },
    { image: image2, modelData: modelData2 },
    { image: image3, modelData: modelData3 }
  ],
});

Container Images

Inference code can be stored in the Amazon EC2 Container Registry (Amazon ECR), which is specified via ContainerDefinition's image property which accepts a class that extends the ContainerImage abstract base class.

Asset Image

Reference a local directory containing a Dockerfile:

import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';
import * as path from 'path';

const image = sagemaker.ContainerImage.fromAsset(path.join('path', 'to', 'Dockerfile', 'directory'));

ECR Image

Reference an image available within ECR:

import * as ecr from 'aws-cdk-lib/aws-ecr';
import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

const repository = ecr.Repository.fromRepositoryName(this, 'Repository', 'repo');
const image = sagemaker.ContainerImage.fromEcrRepository(repository, 'tag');

DLC Image

Reference a deep learning container image:

import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

const repositoryName = 'huggingface-pytorch-training';
const tag = '1.13.1-transformers4.26.0-gpu-py39-cu117-ubuntu20.04';

const image = sagemaker.ContainerImage.fromDlc(repositoryName, tag);

Model Artifacts

If you choose to decouple your model artifacts from your inference code (as is natural given different rates of change between inference code and model artifacts), the artifacts can be specified via the modelData property which accepts a class that extends the ModelData abstract base class. The default is to have no model artifacts associated with a model.

Asset Model Data

Reference local model data:

import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';
import * as path from 'path';

const modelData = sagemaker.ModelData.fromAsset(path.join('path', 'to', 'artifact', 'file.tar.gz'));

S3 Model Data

Reference an S3 bucket and object key as the artifacts for a model:

import * as s3 from 'aws-cdk-lib/aws-s3';
import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

const bucket = new s3.Bucket(this, 'MyBucket');
const modelData = sagemaker.ModelData.fromBucket(bucket, 'path/to/artifact/file.tar.gz');

Model Hosting

Amazon SageMaker provides model hosting services for model deployment. Amazon SageMaker provides an HTTPS endpoint where your machine learning model is available to provide inferences.

Endpoint Configuration

By using the EndpointConfig construct, you can define a set of endpoint configuration which can be used to provision one or more endpoints. In this configuration, you identify one or more models to deploy and the resources that you want Amazon SageMaker to provision. You define one or more production variants, each of which identifies a model. Each production variant also describes the resources that you want Amazon SageMaker to provision. If you are hosting multiple models, you also assign a variant weight to specify how much traffic you want to allocate to each model. For example, suppose that you want to host two models, A and B, and you assign traffic weight 2 for model A and 1 for model B. Amazon SageMaker distributes two-thirds of the traffic to Model A, and one-third to model B:

import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

declare const modelA: sagemaker.Model;
declare const modelB: sagemaker.Model;

const endpointConfig = new sagemaker.EndpointConfig(this, 'EndpointConfig', {
  instanceProductionVariants: [
    {
      model: modelA,
      variantName: 'modelA',
      initialVariantWeight: 2.0,
    },
    {
      model: modelB,
      variantName: 'variantB',
      initialVariantWeight: 1.0,
    },
  ]
});

Endpoint

When you create an endpoint from an EndpointConfig, Amazon SageMaker launches the ML compute instances and deploys the model or models as specified in the configuration. To get inferences from the model, client applications send requests to the Amazon SageMaker Runtime HTTPS endpoint. For more information about the API, see the InvokeEndpoint API. Defining an endpoint requires at minimum the associated endpoint configuration:

import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

declare const endpointConfig: sagemaker.EndpointConfig;

const endpoint = new sagemaker.Endpoint(this, 'Endpoint', { endpointConfig });

AutoScaling

To enable autoscaling on the production variant, use the autoScaleInstanceCount method:

import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

declare const model: sagemaker.Model;

const variantName = 'my-variant';
const endpointConfig = new sagemaker.EndpointConfig(this, 'EndpointConfig', {
  instanceProductionVariants: [
    {
      model: model,
      variantName: variantName,
    },
  ]
});

const endpoint = new sagemaker.Endpoint(this, 'Endpoint', { endpointConfig });
const productionVariant = endpoint.findInstanceProductionVariant(variantName);
const instanceCount = productionVariant.autoScaleInstanceCount({
  maxCapacity: 3
});
instanceCount.scaleOnInvocations('LimitRPS', {
  maxRequestsPerSecond: 30,
});

For load testing guidance on determining the maximum requests per second per instance, please see this documentation.

Metrics

To monitor CloudWatch metrics for a production variant, use one or more of the metric convenience methods:

import * as sagemaker from '@aws-cdk/aws-sagemaker-alpha';

declare const endpointConfig: sagemaker.EndpointConfig;

const endpoint = new sagemaker.Endpoint(this, 'Endpoint', { endpointConfig });
const productionVariant = endpoint.findInstanceProductionVariant('my-variant');
productionVariant.metricModelLatency().createAlarm(this, 'ModelLatencyAlarm', {
  threshold: 100000,
  evaluationPeriods: 3,
});
2.141.0-alpha.0

3 days ago

2.140.0-alpha.0

9 days ago

2.139.1-alpha.0

11 days ago

2.139.0-alpha.0

17 days ago

2.138.0-alpha.0

23 days ago

2.137.0-alpha.0

1 month ago

2.136.1-alpha.0

1 month ago

2.136.0-alpha.0

1 month ago

2.135.0-alpha.0

1 month ago

2.134.0-alpha.0

2 months ago

2.133.0-alpha.0

2 months ago

2.132.1-alpha.0

2 months ago

2.132.0-alpha.0

2 months ago

2.131.0-alpha.0

2 months ago

2.129.0-alpha.0

3 months ago

2.130.0-alpha.0

3 months ago

2.128.0-alpha.0

3 months ago

2.127.0-alpha.0

3 months ago

2.126.0-alpha.0

3 months ago

2.125.0-alpha.0

3 months ago

2.124.0-alpha.0

4 months ago

2.123.0-alpha.0

4 months ago

2.122.0-alpha.0

4 months ago

2.121.0-alpha.0

4 months ago

2.120.0-alpha.0

4 months ago

2.121.1-alpha.0

4 months ago

2.119.0-alpha.0

4 months ago

2.118.0-alpha.0

4 months ago

2.117.0-alpha.0

5 months ago

2.116.1-alpha.0

5 months ago

2.116.0-alpha.0

5 months ago

2.115.0-alpha.0

5 months ago

2.114.1-alpha.0

5 months ago

2.114.0-alpha.0

5 months ago

2.113.0-alpha.0

5 months ago

2.111.0-alpha.0

6 months ago

2.112.0-alpha.0

5 months ago

2.108.1-alpha.0

6 months ago

2.107.0-alpha.0

6 months ago

2.109.0-alpha.0

6 months ago

2.106.0-alpha.0

6 months ago

2.110.0-alpha.0

6 months ago

2.105.0-alpha.0

6 months ago

2.108.0-alpha.0

6 months ago

2.106.1-alpha.0

6 months ago

2.110.1-alpha.0

6 months ago

2.103.0-alpha.0

7 months ago

2.103.1-alpha.0

7 months ago

2.102.1-alpha.0

7 months ago

2.104.0-alpha.0

6 months ago

2.100.0-alpha.0

7 months ago

2.94.0-alpha.0

8 months ago

2.88.0-alpha.0

10 months ago

2.101.1-alpha.0

7 months ago

2.99.1-alpha.0

7 months ago

2.95.1-alpha.0

8 months ago

2.97.0-alpha.0

8 months ago

2.90.0-alpha.0

9 months ago

2.102.0-alpha.0

7 months ago

2.87.0-alpha.0

10 months ago

2.91.0-alpha.0

9 months ago

2.96.1-alpha.0

8 months ago

2.98.0-alpha.0

8 months ago

2.92.0-alpha.0

9 months ago

2.86.0-alpha.0

11 months ago

2.89.0-alpha.0

10 months ago

2.95.0-alpha.0

8 months ago

2.99.0-alpha.0

8 months ago

2.97.1-alpha.0

8 months ago

2.96.2-alpha.0

8 months ago

2.101.0-alpha.0

7 months ago

2.93.0-alpha.0

9 months ago

2.96.0-alpha.0

8 months ago

2.84.0-alpha.0

11 months ago

2.83.0-alpha.0

11 months ago

2.82.0-alpha.0

11 months ago

2.85.0-alpha.0

11 months ago

2.83.1-alpha.0

11 months ago

2.78.0-alpha.0

1 year ago

2.81.0-alpha.0

12 months ago

2.76.0-alpha.0

1 year ago

2.75.0-alpha.0

1 year ago

2.77.0-alpha.0

1 year ago

2.74.0-alpha.0

1 year ago

2.75.1-alpha.0

1 year ago

2.80.0-alpha.0

12 months ago

2.79.1-alpha.0

1 year ago

2.79.0-alpha.0

1 year ago

2.73.0-alpha.0

1 year ago

2.65.0-alpha.0

1 year ago

2.63.2-alpha.0

1 year ago

2.71.0-alpha.0

1 year ago

2.63.1-alpha.0

1 year ago

2.67.0-alpha.0

1 year ago

2.72.0-alpha.0

1 year ago

2.72.1-alpha.0

1 year ago

2.69.0-alpha.0

1 year ago

2.70.0-alpha.0

1 year ago

2.68.0-alpha.0

1 year ago

2.64.0-alpha.0

1 year ago

2.66.1-alpha.0

1 year ago

2.66.0-alpha.0

1 year ago

2.60.0-alpha.0

1 year ago

2.58.0-alpha.0

1 year ago

2.59.0-alpha.0

1 year ago

2.58.1-alpha.0

1 year ago

2.61.0-alpha.0

1 year ago

2.57.0-alpha.0

1 year ago

2.61.1-alpha.0

1 year ago

2.62.0-alpha.0

1 year ago

2.63.0-alpha.0

1 year ago

2.62.1-alpha.0

1 year ago

2.62.2-alpha.0

1 year ago

2.56.1-alpha.0

1 year ago

2.56.0-alpha.0

1 year ago

2.55.1-alpha.0

1 year ago

2.55.0-alpha.0

1 year ago

2.54.0-alpha.0

1 year ago

2.53.0-alpha.0

1 year ago

2.52.1-alpha.0

1 year ago

2.52.0-alpha.0

1 year ago

2.51.1-alpha.0

1 year ago

2.51.0-alpha.0

1 year ago