aws-cdk-neuronx-patterns
Version:
> [!WARNING] > This library is experimental module.
163 lines (162 loc) • 4.94 kB
TypeScript
import { Size } from "aws-cdk-lib";
import * as batch from "aws-cdk-lib/aws-batch";
import { IVpc, SubnetSelection } from "aws-cdk-lib/aws-ec2";
import { ContainerImage } from "aws-cdk-lib/aws-ecs";
import { IRole } from "aws-cdk-lib/aws-iam";
import { IBucket } from "aws-cdk-lib/aws-s3";
import { Construct } from "constructs";
import { INeuronxInstanceType, Model } from "../neuronx";
/**
* Compile runtime.
*/
export interface INeuronxContainerImage {
/**
* An image of the container where the compile job is executed.
*/
readonly image: ContainerImage;
/**
* Neuronx version included in container image.
*/
readonly neuronSdkVersion: string;
}
/**
* The model compiled by Neuronx compiler.
*/
export interface NeuronxCompiledModel {
/**
* The recommended Neuron instance type for running inference with this compiled model.
*/
readonly recommendedInstanceType: INeuronxInstanceType;
/**
* The bucket to upload compiled artifacts.
*/
readonly bucket: IBucket;
/**
* S3 URL that compiled artifact uploaded.
*/
readonly s3Uri: string;
/**
* S3 prefix that compiled artifact uploaded.
*/
readonly s3Prefix: string;
/**
* The model name.
*/
readonly modelName: string;
/**
* The weight size of the model.
*/
readonly weightSize: Size;
}
/**
* Interface for Neuronx compilers.
*/
export interface INeuronxCompiler {
compile(): NeuronxCompiledModel;
}
/**
* Common props for NeuronxCompilerBase.
*/
export interface NeuronxCompilerBaseProps {
/**
* VPC in which this will launch compile worker instance.
*/
readonly vpc: IVpc;
/**
* The bucket to upload compiled artifacts.
*/
readonly bucket: IBucket;
/**
* Secrets to pass to the container.
*/
readonly secrets?: {
[key: string]: batch.Secret;
};
/**
* S3 Prefix that compiled artifact uploaded.
* This property is not depends on compile job finish.
*/
readonly artifactS3Prefix: string;
/**
* The instance type of compile worker instance.
*/
readonly neuronxInstanceType: INeuronxInstanceType;
/**
* The model to be compiled.
*/
readonly model: Model;
/**
* An image of the container where the compile job is executed.
*/
readonly image: INeuronxContainerImage;
/**
* The command to run in the container.
*/
readonly command?: string[];
/**
* The root volume of worker instance.
* @default - N billion parameters * 5GiB EBS
*/
readonly volumeSize?: Size;
/**
* Whether or not to use spot instances. Spot instances are less expensive EC2 instances that can be reclaimed by EC2 at any time; your job will be given two minutes of notice before reclamation.
*
* @default false
*/
readonly spot?: boolean;
/**
* The VPC Subnets this Compute Environment will launch instances in.
*
* @default - new subnets will be created
*/
readonly vpcSubnets?: SubnetSelection;
/**
* The environment variables to pass to the container.
* This is only applicable when using container runtime.
*
* @default - No environment variables.
*/
readonly environment?: {
[key: string]: string;
};
}
/**
* Result of creating a compute environment.
*/
export interface ComputeEnvironmentResult {
/**
* The compute environment.
*/
readonly computeEnvironment: batch.IComputeEnvironment;
/**
* The instance role associated with the compute environment.
*/
readonly instanceRole: IRole;
}
/**
* Abstract base class for Neuronx compilers.
* Provides the common orchestration logic (Lambda, CustomResource, WaitCondition)
* while subclasses define how to create the Batch compute environment and job definition.
*/
export declare abstract class NeuronxCompilerBase extends Construct implements INeuronxCompiler {
private compiledModel?;
private readonly entrypoint;
protected readonly artifactS3Prefix: string;
protected readonly weightSize: Size;
protected readonly neuronxInstanceType: INeuronxInstanceType;
protected readonly model: Model;
protected readonly bucket: IBucket;
constructor(scope: Construct, id: string, props: NeuronxCompilerBaseProps);
/**
* Create the Batch compute environment.
* Subclasses must implement this to provide the appropriate compute environment.
*/
protected abstract createComputeEnvironment(props: NeuronxCompilerBaseProps): ComputeEnvironmentResult;
/**
* Create the Batch job definition.
* Subclasses must implement this to provide the appropriate job definition.
*/
protected abstract createJobDefinition(props: NeuronxCompilerBaseProps): batch.IJobDefinition;
private createJobQueue;
compile(): NeuronxCompiledModel;
}