Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add supported inference and incremental training configs #4637

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/sagemaker/jumpstart/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def __init__(
disable_output_compression: Optional[bool] = None,
enable_remote_debug: Optional[Union[bool, PipelineVariable]] = None,
config_name: Optional[str] = None,
inference_config_name: Optional[str] = None,
Captainia marked this conversation as resolved.
Show resolved Hide resolved
):
"""Initializes a ``JumpStartEstimator``.

Expand Down Expand Up @@ -504,7 +505,10 @@ def __init__(
enable_remote_debug (bool or PipelineVariable): Optional.
Specifies whether RemoteDebug is enabled for the training job
config_name (Optional[str]):
Name of the JumpStart Model config to apply. (Default: None).
Name of the training configuration to apply to the Estimator. (Default: None).
inference_config_name (Optional[str]):
Captainia marked this conversation as resolved.
Show resolved Hide resolved
Name of the inference configuraion to apply to the Estimator,
to be used when deploying the fine-tuned mode. (Default: None).

Raises:
ValueError: If the model ID is not recognized by JumpStart.
Expand Down Expand Up @@ -583,7 +587,8 @@ def _validate_model_id_and_get_type_hook():
disable_output_compression=disable_output_compression,
enable_infra_check=enable_infra_check,
enable_remote_debug=enable_remote_debug,
config_name=config_name,
training_config_name=config_name,
inference_config_name=inference_config_name,
)

self.model_id = estimator_init_kwargs.model_id
Expand All @@ -597,7 +602,8 @@ def _validate_model_id_and_get_type_hook():
self.role = estimator_init_kwargs.role
self.sagemaker_session = estimator_init_kwargs.sagemaker_session
self._enable_network_isolation = estimator_init_kwargs.enable_network_isolation
self.config_name = estimator_init_kwargs.config_name
self.training_config_name = estimator_init_kwargs.training_config_name
self.inference_config_name = estimator_init_kwargs.inference_config_name
self.init_kwargs = estimator_init_kwargs.to_kwargs_dict(False)

super(JumpStartEstimator, self).__init__(**estimator_init_kwargs.to_kwargs_dict())
Expand Down Expand Up @@ -673,7 +679,7 @@ def fit(
tolerate_vulnerable_model=self.tolerate_vulnerable_model,
tolerate_deprecated_model=self.tolerate_deprecated_model,
sagemaker_session=self.sagemaker_session,
config_name=self.config_name,
config_name=self.training_config_name,
)

return super(JumpStartEstimator, self).fit(**estimator_fit_kwargs.to_kwargs_dict())
Expand Down Expand Up @@ -1091,7 +1097,7 @@ def deploy(
git_config=git_config,
use_compiled_model=use_compiled_model,
training_instance_type=self.instance_type,
config_name=self.config_name,
config_name=self.inference_config_name,
)

predictor = super(JumpStartEstimator, self).deploy(
Expand All @@ -1108,7 +1114,7 @@ def deploy(
tolerate_deprecated_model=self.tolerate_deprecated_model,
tolerate_vulnerable_model=self.tolerate_vulnerable_model,
sagemaker_session=self.sagemaker_session,
config_name=self.config_name,
config_name=self.inference_config_name,
)

# If a predictor class was passed, do not mutate predictor
Expand Down Expand Up @@ -1140,7 +1146,9 @@ def set_training_config(self, config_name: str) -> None:
config_name (str): The name of the config.
"""
self.__init__(
model_id=self.model_id, model_version=self.model_version, config_name=config_name
model_id=self.model_id,
model_version=self.model_version,
config_name=config_name,
)

def __str__(self) -> str:
Expand Down
104 changes: 89 additions & 15 deletions src/sagemaker/jumpstart/factory/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
_retrieve_model_package_model_artifact_s3_uri,
)
from sagemaker.jumpstart.artifacts.resource_names import _retrieve_resource_name_base
from sagemaker.jumpstart.session_utils import get_model_info_from_training_job
from sagemaker.session import Session
from sagemaker.async_inference.async_inference_config import AsyncInferenceConfig
from sagemaker.base_deserializers import BaseDeserializer
Expand Down Expand Up @@ -130,7 +131,8 @@ def get_init_kwargs(
disable_output_compression: Optional[bool] = None,
enable_infra_check: Optional[Union[bool, PipelineVariable]] = None,
enable_remote_debug: Optional[Union[bool, PipelineVariable]] = None,
config_name: Optional[str] = None,
training_config_name: Optional[str] = None,
inference_config_name: Optional[str] = None,
) -> JumpStartEstimatorInitKwargs:
"""Returns kwargs required to instantiate `sagemaker.estimator.Estimator` object."""

Expand Down Expand Up @@ -189,7 +191,8 @@ def get_init_kwargs(
disable_output_compression=disable_output_compression,
enable_infra_check=enable_infra_check,
enable_remote_debug=enable_remote_debug,
config_name=config_name,
training_config_name=training_config_name,
inference_config_name=inference_config_name,
)

estimator_init_kwargs = _add_model_version_to_kwargs(estimator_init_kwargs)
Expand All @@ -207,6 +210,7 @@ def get_init_kwargs(
estimator_init_kwargs = _add_role_to_kwargs(estimator_init_kwargs)
estimator_init_kwargs = _add_env_to_kwargs(estimator_init_kwargs)
estimator_init_kwargs = _add_tags_to_kwargs(estimator_init_kwargs)
estimator_init_kwargs = _add_config_name_to_kwargs(estimator_init_kwargs)

return estimator_init_kwargs

Expand Down Expand Up @@ -449,7 +453,7 @@ def _add_instance_type_and_count_to_kwargs(
tolerate_deprecated_model=kwargs.tolerate_deprecated_model,
tolerate_vulnerable_model=kwargs.tolerate_vulnerable_model,
sagemaker_session=kwargs.sagemaker_session,
config_name=kwargs.config_name,
config_name=kwargs.training_config_name,
)

kwargs.instance_count = kwargs.instance_count or 1
Expand All @@ -473,15 +477,15 @@ def _add_tags_to_kwargs(kwargs: JumpStartEstimatorInitKwargs) -> JumpStartEstima
tolerate_vulnerable_model=kwargs.tolerate_vulnerable_model,
tolerate_deprecated_model=kwargs.tolerate_deprecated_model,
sagemaker_session=kwargs.sagemaker_session,
config_name=kwargs.config_name,
config_name=kwargs.training_config_name,
).version

if kwargs.sagemaker_session.settings.include_jumpstart_tags:
kwargs.tags = add_jumpstart_model_info_tags(
kwargs.tags,
kwargs.model_id,
full_model_version,
config_name=kwargs.config_name,
config_name=kwargs.training_config_name,
scope=JumpStartScriptScope.TRAINING,
)
return kwargs
Expand All @@ -500,7 +504,7 @@ def _add_image_uri_to_kwargs(kwargs: JumpStartEstimatorInitKwargs) -> JumpStartE
tolerate_deprecated_model=kwargs.tolerate_deprecated_model,
tolerate_vulnerable_model=kwargs.tolerate_vulnerable_model,
sagemaker_session=kwargs.sagemaker_session,
config_name=kwargs.config_name,
config_name=kwargs.training_config_name,
)

return kwargs
Expand All @@ -526,7 +530,7 @@ def _add_model_uri_to_kwargs(kwargs: JumpStartEstimatorInitKwargs) -> JumpStartE
sagemaker_session=kwargs.sagemaker_session,
region=kwargs.region,
instance_type=kwargs.instance_type,
config_name=kwargs.config_name,
config_name=kwargs.training_config_name,
)

if (
Expand All @@ -539,7 +543,7 @@ def _add_model_uri_to_kwargs(kwargs: JumpStartEstimatorInitKwargs) -> JumpStartE
tolerate_deprecated_model=kwargs.tolerate_deprecated_model,
tolerate_vulnerable_model=kwargs.tolerate_vulnerable_model,
sagemaker_session=kwargs.sagemaker_session,
config_name=kwargs.config_name,
config_name=kwargs.training_config_name,
)
):
JUMPSTART_LOGGER.warning(
Expand Down Expand Up @@ -575,7 +579,7 @@ def _add_source_dir_to_kwargs(kwargs: JumpStartEstimatorInitKwargs) -> JumpStart
tolerate_vulnerable_model=kwargs.tolerate_vulnerable_model,
region=kwargs.region,
sagemaker_session=kwargs.sagemaker_session,
config_name=kwargs.config_name,
config_name=kwargs.training_config_name,
)

return kwargs
Expand All @@ -596,7 +600,7 @@ def _add_env_to_kwargs(
sagemaker_session=kwargs.sagemaker_session,
script=JumpStartScriptScope.TRAINING,
instance_type=kwargs.instance_type,
config_name=kwargs.config_name,
config_name=kwargs.training_config_name,
)

model_package_artifact_uri = _retrieve_model_package_model_artifact_s3_uri(
Expand All @@ -607,7 +611,7 @@ def _add_env_to_kwargs(
tolerate_deprecated_model=kwargs.tolerate_deprecated_model,
tolerate_vulnerable_model=kwargs.tolerate_vulnerable_model,
sagemaker_session=kwargs.sagemaker_session,
config_name=kwargs.config_name,
config_name=kwargs.training_config_name,
)

if model_package_artifact_uri:
Expand Down Expand Up @@ -635,7 +639,7 @@ def _add_env_to_kwargs(
tolerate_deprecated_model=kwargs.tolerate_deprecated_model,
tolerate_vulnerable_model=kwargs.tolerate_vulnerable_model,
sagemaker_session=kwargs.sagemaker_session,
config_name=kwargs.config_name,
config_name=kwargs.training_config_name,
)
if model_specs.is_gated_model():
raise ValueError(
Expand Down Expand Up @@ -696,7 +700,7 @@ def _add_hyperparameters_to_kwargs(
tolerate_vulnerable_model=kwargs.tolerate_vulnerable_model,
sagemaker_session=kwargs.sagemaker_session,
instance_type=kwargs.instance_type,
config_name=kwargs.config_name,
config_name=kwargs.training_config_name,
)

for key, value in default_hyperparameters.items():
Expand Down Expand Up @@ -730,7 +734,7 @@ def _add_metric_definitions_to_kwargs(
tolerate_vulnerable_model=kwargs.tolerate_vulnerable_model,
sagemaker_session=kwargs.sagemaker_session,
instance_type=kwargs.instance_type,
config_name=kwargs.config_name,
config_name=kwargs.training_config_name,
)
or []
)
Expand Down Expand Up @@ -760,7 +764,7 @@ def _add_estimator_extra_kwargs(
tolerate_deprecated_model=kwargs.tolerate_deprecated_model,
tolerate_vulnerable_model=kwargs.tolerate_vulnerable_model,
sagemaker_session=kwargs.sagemaker_session,
config_name=kwargs.config_name,
config_name=kwargs.training_config_name,
)

for key, value in estimator_kwargs_to_add.items():
Expand Down Expand Up @@ -793,3 +797,73 @@ def _add_fit_extra_kwargs(kwargs: JumpStartEstimatorFitKwargs) -> JumpStartEstim
setattr(kwargs, key, value)

return kwargs


def _add_config_name_to_kwargs(
kwargs: JumpStartEstimatorInitKwargs,
) -> JumpStartEstimatorInitKwargs:
"""Sets tags in kwargs based on default or override, returns full kwargs."""

specs = verify_model_region_and_return_specs(
model_id=kwargs.model_id,
version=kwargs.model_version,
scope=JumpStartScriptScope.TRAINING,
region=kwargs.region,
tolerate_vulnerable_model=kwargs.tolerate_vulnerable_model,
tolerate_deprecated_model=kwargs.tolerate_deprecated_model,
sagemaker_session=kwargs.sagemaker_session,
config_name=kwargs.training_config_name,
)

if kwargs.base_job_name:
_, _, _, base_training_config_name = get_model_info_from_training_job(
training_job_name=kwargs.base_job_name, sagemaker_session=kwargs.sagemaker_session
)

kwargs.training_config_name = (
kwargs.training_config_name
or specs.training_configs.configs.get(
base_training_config_name
).default_incremental_trainig_config
or specs.training_configs.get_top_config_from_ranking().default_incremental_trainig_config # noqa E501 # pylint: disable=c0301
)

if specs.training_configs and specs.training_configs.get_top_config_from_ranking().config_name:
kwargs.training_config_name = (
kwargs.training_config_name
or specs.training_configs.get_top_config_from_ranking().config_name
)

kwargs.inference_config_name = (
Captainia marked this conversation as resolved.
Show resolved Hide resolved
kwargs.inference_config_name
or specs.training_configs.configs.get(
kwargs.training_config_name
).default_inference_config
)

if (
kwargs.inference_config_name
and kwargs.inference_config_name
not in specs.training_configs.configs.get(
kwargs.training_config_name
).supported_inference_configs
):
raise ValueError(
f"Inference config {kwargs.inference_config_name}"
f"is not supported for model {kwargs.model_id}."
)

if not kwargs.training_config_name:
return kwargs

resolved_config = specs.training_configs.configs[
kwargs.training_config_name
].resolved_config
supported_instance_types = resolved_config.get("supported_training_instance_types", [])
if kwargs.instance_type not in supported_instance_types:
raise ValueError(
f"Instance type {kwargs.instance_type} "
f"is not supported for config {kwargs.training_config_name}."
)
Captainia marked this conversation as resolved.
Show resolved Hide resolved

return kwargs
Loading
Loading