Model — sagemaker 2.247.0 documentation (original) (raw)

class sagemaker.model.Model(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=20, dependencies=None, git_config=None, resources=None, additional_model_data_sources=None, model_reference_arn=None)

Bases: ModelBase, InferenceRecommenderMixin

A SageMaker Model that can be deployed to an Endpoint.

Initialize an SageMaker Model.

Parameters:

add_tags(tags)

Add tags to this Model

Parameters:

tags (Tags) – Tags to add.

Return type:

None

remove_tag_with_key(key)

Remove a tag with the given key from the list of tags.

Parameters:

key (str) – The key of the tag to remove.

Return type:

None

classmethod attach(endpoint_name, inference_component_name=None, sagemaker_session=None)

Attaches a Model object to an existing SageMaker Endpoint.

Parameters:

Return type:

Model

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, source_uri=None, model_card=None, model_life_cycle=None, accept_eula=None, model_type=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, accept_eula=None, model_reference_arn=None)

Create a SageMaker Model Entity

Parameters:

Or

tags = {'tagname', 'tagvalue'}
For more information about tags, seeboto3 documentation

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, accept_eula=None, model_reference_arn=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, managed_instance_scaling=None, inference_component_name=None, routing_config=None, model_reference_arn=None, inference_ami_version=None, update_endpoint=False, **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], Any] 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, hub_name=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, config_name=None, additional_model_data_sources=None)

Bases: 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.

log_subscription_warning()

Log message prompting the customer to subscribe to the proprietary model.

Return type:

None

retrieve_all_examples()

Returns all example payloads associated with the model.

Raises:

Return type:

List[_JumpStartSerializablePayload_] | None

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:

JumpStartSerializablePayload

set_deployment_config(config_name, instance_type)

Sets the deployment config to apply to the model.

Parameters:

Return type:

None

property deployment_config_: Dict[str, Any] | None_

The deployment config that will be applied to This model.

Returns:

Deployment config.

Return type:

Optional[Dict[str, Any]]

property benchmark_metrics_: DataFrame_

Benchmark Metrics for deployment configs.

Returns:

Pandas DataFrame object.

Return type:

Benchmark Metrics

display_benchmark_metrics(**kwargs)

Display deployment configs benchmark metrics.

Return type:

None

list_deployment_configs()

List deployment configs for This model.

Returns:

A list of deployment configs.

Return type:

List[Dict[str, Any]]

classmethod attach(endpoint_name, inference_component_name=None, model_id=None, model_version=None, sagemaker_session=<sagemaker.session.Session object>, hub_name=None)

Attaches a JumpStartModel object to an existing SageMaker Endpoint.

The model id, version (and inference component name) can be inferred from the tags.

Parameters:

Return type:

JumpStartModel

deploy(initial_instance_count=None, instance_type=None, serializer=None, deserializer=None, accelerator_type=None, endpoint_name=None, inference_component_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, routing_config=None, model_access_configs=None, inference_ami_version=None)

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:

Raises:

MarketplaceModelSubscriptionError – If the caller is not subscribed to the model.

Return type:

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, source_uri=None, model_card=None, accept_eula=None, model_life_cycle=None)

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

Parameters:

Returns:

A sagemaker.model.ModelPackage instance.

add_tags(tags)

Add tags to this Model

Parameters:

tags (Tags) – Tags to add.

Return type:

None

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, accept_eula=None, model_reference_arn=None)

Create a SageMaker Model Entity

Parameters:

Or

tags = {'tagname', 'tagvalue'}
For more information about tags, seeboto3 documentation

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, accept_eula=None, model_reference_arn=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

remove_tag_with_key(key)

Remove a tag with the given key from the list of tags.

Parameters:

key (str) – The key of the tag to remove.

Return type:

None

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: 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: 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:

update_customer_metadata(customer_metadata_properties)

Updating customer metadata properties for the model package

Parameters:

customer_metadata_properties (dict[_str,_ str]) – A dictionary of key-value paired metadata properties (default: None).

update_inference_specification(containers=None, image_uris=None, content_types=None, response_types=None, inference_instances=None, transform_instances=None)

Inference specification to be set for the model package

Parameters:

update_source_uri(source_uri)

Source uri to be set for the model package

Parameters:

source_uri (str) – The URI of the source for the model package.

update_model_life_cycle(model_life_cycle)

Modellifecycle to be set for the model package

Parameters:

model_life_cycle (ModelLifeCycle) – The current state of model package in its life cycle

remove_customer_metadata_properties(customer_metadata_properties_to_remove)

Removes the specified keys from customer metadata properties

Parameters:

add_inference_specification(name, containers=None, image_uris=None, description=None, content_types=None, response_types=None, inference_instances=None, transform_instances=None)

Additional inference specification to be added for the model package

Parameters:

update_model_card(model_card)

Updates Created model card content which created with model package

Parameters:

model_card (ModelCard | ModelPackageModelCard) – Updated Model Card content