Model — sagemaker 2.199.0 documentation (original) (raw)

sagemaker

class sagemaker.model. Model(image_uri, model_data=None, role=None, predictor_cls=None, env=None, name=None, vpc_config=None, sagemaker_session=None, enable_network_isolation=None, model_kms_key=None, image_config=None, source_dir=None, code_location=None, entry_point=None, container_log_level=20, dependencies=None, git_config=None, resources=None)

Bases: sagemaker.model.ModelBase, sagemaker.inference_recommender.inference_recommender_mixin.InferenceRecommenderMixin

A SageMaker Model that can be deployed to an Endpoint.

Initialize an SageMaker Model.

Parameters

register(content_types=None, response_types=None, inference_instances=None, transform_instances=None, model_package_name=None, model_package_group_name=None, image_uri=None, model_metrics=None, metadata_properties=None, marketplace_cert=False, approval_status=None, description=None, drift_check_baselines=None, customer_metadata_properties=None, validation_specification=None, domain=None, task=None, sample_payload_url=None, framework=None, framework_version=None, nearest_model_name=None, data_input_configuration=None, skip_model_validation=None)

Creates a model package for creating SageMaker models or listing on Marketplace.

Parameters

Returns

A sagemaker.model.ModelPackage instance or pipeline step arguments in case the Model instance is built withPipelineSession

create(instance_type=None, accelerator_type=None, serverless_inference_config=None, tags=None)

Create a SageMaker Model Entity

Parameters

Returns

None or pipeline step arguments in case the Model instance is built withPipelineSession

prepare_container_def(instance_type=None, accelerator_type=None, serverless_inference_config=None)

Return a dict created by sagemaker.container_def().

It is used for deploying this model to a specified instance type.

Subclasses can override this to provide custom container definitions for deployment to a specific instance type. Called by deploy().

Parameters

Returns

A container definition object usable with the CreateModel API.

Return type

dict

is_repack()

Whether the source code needs to be repacked before uploading to S3.

Returns

if the source need to be repacked or not

Return type

bool

enable_network_isolation()

Whether to enable network isolation when creating this Model

Returns

If network isolation should be enabled or not.

Return type

bool

package_for_edge(output_path, model_name, model_version, role=None, job_name=None, resource_key=None, s3_kms_key=None, tags=None)

Package this Model with SageMaker Edge.

Creates a new EdgePackagingJob and wait for it to finish. model_data will now point to the packaged artifacts.

Parameters

Returns

A SageMaker Model object. SeeModel() for full details.

Return type

sagemaker.model.Model

compile(target_instance_family, input_shape, output_path, role=None, tags=None, job_name=None, compile_max_run=900, framework=None, framework_version=None, target_platform_os=None, target_platform_arch=None, target_platform_accelerator=None, compiler_options=None)

Compile this Model with SageMaker Neo.

Parameters

Returns

A SageMaker Model object. SeeModel() for full details.

Return type

sagemaker.model.Model

deploy(initial_instance_count=None, instance_type=None, serializer=None, deserializer=None, accelerator_type=None, endpoint_name=None, tags=None, kms_key=None, wait=True, data_capture_config=None, async_inference_config=None, serverless_inference_config=None, volume_size=None, model_data_download_timeout=None, container_startup_health_check_timeout=None, inference_recommendation_id=None, explainer_config=None, accept_eula=None, endpoint_logging=False, resources=None, endpoint_type=<EndpointType.MODEL_BASED: 'ModelBased'>, managed_instance_scaling=None, **kwargs)

Deploy this Model to an Endpoint and optionally return a Predictor.

Create a SageMaker Model and EndpointConfig, and deploy anEndpoint from this Model. If self.predictor_cls is not None, this method returns a the result of invoking self.predictor_cls on the created endpoint name.

The name of the created model is accessible in the name field of this Model after deploy returns

The name of the created endpoint is accessible in theendpoint_name field of this Model after deploy returns.

Parameters

Raises

ValueError – If arguments combination check failed in these circumstances: - If no role is specified or - If serverless inference config is not specified and instance type and instance count are also not specified or - If a wrong type of object is provided as serverless inference config or async inference config or - If inference recommendation id is specified along with incompatible parameters

Returns

Invocation of

self.predictor_cls on the created endpoint name, if self.predictor_clsis not None. Otherwise, return None.

Return type

callable[string, sagemaker.session.Session] or None

transformer(instance_count, instance_type, strategy=None, assemble_with=None, output_path=None, output_kms_key=None, accept=None, env=None, max_concurrent_transforms=None, max_payload=None, tags=None, volume_kms_key=None)

Return a Transformer that uses this Model.

Parameters

tune(max_tuning_duration=1800)

Tune a Model built in Mode.LOCAL_CONTAINER via ModelBuilder.

tune() is available for DJL Models using Huggingface IDs. In this use case, Tensor Parallel Degree is our tunable parameter. The tuning job first generates all admissible Tensor Parallel Degrees and then benchmarks on 10 invocations serially followed by 10 invocations concurrently. It starts first at the highest admissible Tensor Parallel Degree and then scales down until failure.

Example

Sample flow:

sample_input = { "inputs": "sample_prompt", "parameters": {} } sample_output = { "generated_text": "sample_text_generation" }

builder = ModelBuilder( model=model, schema_builder=SchemaBuilder(sample_input, sample_output), model_path=path_to_model, mode=Mode.LOCAL_CONTAINER, )

model = builder.build() tuned_model = model.tune() tuned_model.deploy()

Parameters

max_tuning_duration (int) – The time out for the Mode.LOCAL_CONTAINER tuning job. Defaults to 1800.

delete_model()

Delete an Amazon SageMaker Model.

Raises

ValueError – if the model is not created yet.

right_size(sample_payload_url=None, supported_content_types=None, supported_instance_types=None, job_name=None, framework=None, job_duration_in_seconds=None, hyperparameter_ranges=None, phases=None, traffic_type=None, max_invocations=None, model_latency_thresholds=None, max_tests=None, max_parallel_tests=None, log_level='Verbose')

Recommends an instance type for a SageMaker or BYOC model.

Create a SageMaker Model or use a registered ModelPackage, to start an Inference Recommender job.

The name of the created model is accessible in the name field of this Model after right_size returns.

Parameters

Returns

A SageMaker Model object. SeeModel() for full details.

Return type

sagemaker.model.Model

class sagemaker.jumpstart.model. JumpStartModel(model_id=None, model_version=None, tolerate_vulnerable_model=None, tolerate_deprecated_model=None, region=None, instance_type=None, image_uri=None, model_data=None, role=None, predictor_cls=None, env=None, name=None, vpc_config=None, sagemaker_session=None, enable_network_isolation=None, model_kms_key=None, image_config=None, source_dir=None, code_location=None, entry_point=None, container_log_level=None, dependencies=None, git_config=None, model_package_arn=None, resources=None)

Bases: sagemaker.model.Model

JumpStartModel class.

This class sets defaults based on the model ID and version.

Initializes a JumpStartModel.

This method sets model-specific defaults for the Model.__init__ method.

Only model ID is required to instantiate this class, however any field can be overriden.

Any field set to None does not get passed to the parent class method.

Parameters

Raises

ValueError – If the model ID is not recognized by JumpStart.

retrieve_all_examples()

Returns all example payloads associated with the model.

Raises

Return type

Optional[List[sagemaker.jumpstart.types.JumpStartSerializablePayload]]

retrieve_example_payload()

Returns the example payload associated with the model.

Payload can be directly used with the sagemaker.predictor.Predictor.predict(…) function.

Raises

Return type

sagemaker.jumpstart.types.JumpStartSerializablePayload

deploy(initial_instance_count=None, instance_type=None, serializer=None, deserializer=None, accelerator_type=None, endpoint_name=None, tags=None, kms_key=None, wait=True, data_capture_config=None, async_inference_config=None, serverless_inference_config=None, volume_size=None, model_data_download_timeout=None, container_startup_health_check_timeout=None, inference_recommendation_id=None, explainer_config=None, accept_eula=None, endpoint_logging=False, resources=None, managed_instance_scaling=None, endpoint_type=<EndpointType.MODEL_BASED: 'ModelBased'>)

Creates endpoint by calling base Model class deploy method.

Create a SageMaker Model and EndpointConfig, and deploy anEndpoint from this Model.

Any field set to None does not get passed to the parent class method.

Parameters

Return type

sagemaker.base_predictor.PredictorBase

register(content_types=None, response_types=None, inference_instances=None, transform_instances=None, model_package_group_name=None, image_uri=None, model_metrics=None, metadata_properties=None, approval_status=None, description=None, drift_check_baselines=None, customer_metadata_properties=None, validation_specification=None, domain=None, task=None, sample_payload_url=None, framework=None, framework_version=None, nearest_model_name=None, data_input_configuration=None, skip_model_validation=None)

Creates a model package for creating SageMaker models or listing on Marketplace.

Parameters

Returns

A sagemaker.model.ModelPackage instance.

compile(target_instance_family, input_shape, output_path, role=None, tags=None, job_name=None, compile_max_run=900, framework=None, framework_version=None, target_platform_os=None, target_platform_arch=None, target_platform_accelerator=None, compiler_options=None)

Compile this Model with SageMaker Neo.

Parameters

Returns

A SageMaker Model object. SeeModel() for full details.

Return type

sagemaker.model.Model

create(instance_type=None, accelerator_type=None, serverless_inference_config=None, tags=None)

Create a SageMaker Model Entity

Parameters

Returns

None or pipeline step arguments in case the Model instance is built withPipelineSession

delete_model()

Delete an Amazon SageMaker Model.

Raises

ValueError – if the model is not created yet.

enable_network_isolation()

Whether to enable network isolation when creating this Model

Returns

If network isolation should be enabled or not.

Return type

bool

is_repack()

Whether the source code needs to be repacked before uploading to S3.

Returns

if the source need to be repacked or not

Return type

bool

package_for_edge(output_path, model_name, model_version, role=None, job_name=None, resource_key=None, s3_kms_key=None, tags=None)

Package this Model with SageMaker Edge.

Creates a new EdgePackagingJob and wait for it to finish. model_data will now point to the packaged artifacts.

Parameters

Returns

A SageMaker Model object. SeeModel() for full details.

Return type

sagemaker.model.Model

prepare_container_def(instance_type=None, accelerator_type=None, serverless_inference_config=None)

Return a dict created by sagemaker.container_def().

It is used for deploying this model to a specified instance type.

Subclasses can override this to provide custom container definitions for deployment to a specific instance type. Called by deploy().

Parameters

Returns

A container definition object usable with the CreateModel API.

Return type

dict

right_size(sample_payload_url=None, supported_content_types=None, supported_instance_types=None, job_name=None, framework=None, job_duration_in_seconds=None, hyperparameter_ranges=None, phases=None, traffic_type=None, max_invocations=None, model_latency_thresholds=None, max_tests=None, max_parallel_tests=None, log_level='Verbose')

Recommends an instance type for a SageMaker or BYOC model.

Create a SageMaker Model or use a registered ModelPackage, to start an Inference Recommender job.

The name of the created model is accessible in the name field of this Model after right_size returns.

Parameters

Returns

A SageMaker Model object. SeeModel() for full details.

Return type

sagemaker.model.Model

transformer(instance_count, instance_type, strategy=None, assemble_with=None, output_path=None, output_kms_key=None, accept=None, env=None, max_concurrent_transforms=None, max_payload=None, tags=None, volume_kms_key=None)

Return a Transformer that uses this Model.

Parameters

tune(max_tuning_duration=1800)

Tune a Model built in Mode.LOCAL_CONTAINER via ModelBuilder.

tune() is available for DJL Models using Huggingface IDs. In this use case, Tensor Parallel Degree is our tunable parameter. The tuning job first generates all admissible Tensor Parallel Degrees and then benchmarks on 10 invocations serially followed by 10 invocations concurrently. It starts first at the highest admissible Tensor Parallel Degree and then scales down until failure.

Example

Sample flow:

sample_input = { "inputs": "sample_prompt", "parameters": {} } sample_output = { "generated_text": "sample_text_generation" }

builder = ModelBuilder( model=model, schema_builder=SchemaBuilder(sample_input, sample_output), model_path=path_to_model, mode=Mode.LOCAL_CONTAINER, )

model = builder.build() tuned_model = model.tune() tuned_model.deploy()

Parameters

max_tuning_duration (int) – The time out for the Mode.LOCAL_CONTAINER tuning job. Defaults to 1800.

class sagemaker.model. FrameworkModel(model_data, image_uri, role=None, entry_point=None, source_dir=None, predictor_cls=None, env=None, name=None, container_log_level=20, code_location=None, sagemaker_session=None, dependencies=None, git_config=None, **kwargs)

Bases: sagemaker.model.Model

A Model for working with an SageMaker Framework.

This class hosts user-defined code in S3 and sets code location and configuration in model environment variables.

Initialize a FrameworkModel.

Parameters

Tip

You can find additional parameters for initializing this class atModel.

is_repack()

Whether the source code needs to be repacked before uploading to S3.

Returns

if the source need to be repacked or not

Return type

bool

class sagemaker.model. ModelPackage(role=None, model_data=None, algorithm_arn=None, model_package_arn=None, **kwargs)

Bases: sagemaker.model.Model

A SageMaker Model that can be deployed to an Endpoint.

Initialize a SageMaker ModelPackage.

Parameters

enable_network_isolation()

Whether to enable network isolation when creating a model out of this ModelPackage

Returns

If network isolation should be enabled or not.

Return type

bool

update_approval_status(approval_status, approval_description=None)

Update the approval status for the model package

Parameters