Skip to content

fix: alt configs model deployment and training issues #4833

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

Merged
merged 9 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
21 changes: 11 additions & 10 deletions src/sagemaker/jumpstart/artifacts/metric_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,17 @@ def _retrieve_default_training_metric_definitions(
else []
)

instance_specific_metric_name: str
for instance_specific_metric_definition in instance_specific_metric_definitions:
instance_specific_metric_name = instance_specific_metric_definition["Name"]
default_metric_definitions = list(
filter(
lambda metric_definition: metric_definition["Name"]
!= instance_specific_metric_name,
default_metric_definitions,
if instance_specific_metric_definitions:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

if not instance_specific_metric_definitions:
  return default_metric_definitions

instance_specific_metric_name: str
for instance_specific_metric_definition in instance_specific_metric_definitions:
instance_specific_metric_name = instance_specific_metric_definition["Name"]
default_metric_definitions = list(
filter(
lambda metric_definition: metric_definition["Name"]
!= instance_specific_metric_name,
default_metric_definitions,
)
)
)
default_metric_definitions.append(instance_specific_metric_definition)
default_metric_definitions.append(instance_specific_metric_definition)

return default_metric_definitions
2 changes: 2 additions & 0 deletions src/sagemaker/jumpstart/factory/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ def _add_instance_type_to_kwargs(
sagemaker_session=kwargs.sagemaker_session,
model_type=kwargs.model_type,
config_name=kwargs.config_name,
hub_arn=kwargs.hub_arn,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're going to be finding these lines forever aren't we....

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an optional field but looks like integ tests could be a possible solution here

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we desperately need integ tests for (at least):

  1. ModelReference fine-tuning
  2. ModelReference deploy
  3. Alt config deploy

)

if specs.inference_configs and kwargs.config_name not in specs.inference_configs.configs:
Expand Down Expand Up @@ -780,6 +781,7 @@ def _add_config_name_to_deploy_kwargs(
sagemaker_session=temp_session,
model_type=kwargs.model_type,
config_name=kwargs.config_name,
hub_arn=kwargs.hub_arn,
)
default_config_name = _select_inference_config_from_training_config(
specs=specs, training_config_name=training_config_name
Expand Down
34 changes: 24 additions & 10 deletions src/sagemaker/jumpstart/hub/parser_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
from __future__ import absolute_import

import re
from typing import Any, Dict
from typing import Any, Dict, List, Optional


def camel_to_snake(camel_case_string: str) -> str:
"""Converts camelCaseString or UpperCamelCaseString to snake_case_string."""
snake_case_string = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", camel_case_string)
if "-" in snake_case_string:
# remove any hyphen from the string for accurate conversion.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: unnecessary comment

snake_case_string = snake_case_string.replace("-", "")
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", snake_case_string).lower()


Expand All @@ -29,20 +32,29 @@ def snake_to_upper_camel(snake_case_string: str) -> str:
return upper_camel_case_string


def walk_and_apply_json(json_obj: Dict[Any, Any], apply) -> Dict[Any, Any]:
"""Recursively walks a json object and applies a given function to the keys."""
def walk_and_apply_json(
json_obj: Dict[Any, Any], apply, stop_keys: Optional[List[str]] = ["metrics"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-blocking: apply typing please

) -> Dict[Any, Any]:
"""Recursively walks a json object and applies a given function to the keys.

stop_keys (Optional[list[str]]): List of field keys that should stop the application function.
Any children of these keys will not have the application function applied to them.
"""

def _walk_and_apply_json(json_obj, new):
if isinstance(json_obj, dict) and isinstance(new, dict):
for key, value in json_obj.items():
new_key = apply(key)
if isinstance(value, dict):
new[new_key] = {}
_walk_and_apply_json(value, new=new[new_key])
elif isinstance(value, list):
new[new_key] = []
for item in value:
_walk_and_apply_json(item, new=new[new_key])
if (stop_keys and new_key not in stop_keys) or stop_keys is None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

if stop_keys and new_key in stop_keys:
    new[new_key] = value
else:
    ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, conditional is too complex. If you want to keep this condition, create a utils and unit-test separately please.

if isinstance(value, dict):
new[new_key] = {}
_walk_and_apply_json(value, new=new[new_key])
elif isinstance(value, list):
new[new_key] = []
for item in value:
_walk_and_apply_json(item, new=new[new_key])
else:
new[new_key] = value
else:
new[new_key] = value
elif isinstance(json_obj, dict) and isinstance(new, list):
Expand All @@ -51,6 +63,8 @@ def _walk_and_apply_json(json_obj, new):
new.update(json_obj)
elif isinstance(json_obj, list) and isinstance(new, list):
new.append(json_obj)
elif isinstance(json_obj, str) and isinstance(new, list):
new.append(json_obj)
return new

return _walk_and_apply_json(json_obj, new={})
4 changes: 2 additions & 2 deletions src/sagemaker/jumpstart/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ def __init__(self, spec: Optional[Dict[str, Any]], is_hub_content=False):
spec (Dict[str, Any]): Dictionary representation of training config ranking.
"""
if is_hub_content:
spec = {camel_to_snake(key): val for key, val in spec.items()}
spec = walk_and_apply_json(spec, camel_to_snake)
self.from_json(spec)

def from_json(self, json_obj: Dict[str, Any]) -> None:
Expand Down Expand Up @@ -1400,7 +1400,7 @@ def from_json(self, json_obj: Dict[str, Any]) -> None:

if self.training_supported:
if self._is_hub_content:
self.training_ecr_uri: Optional[str] = json_obj["training_ecr_uri"]
self.training_ecr_uri: Optional[str] = json_obj.get("training_ecr_uri")
self._non_serializable_slots.append("training_ecr_specs")
else:
self.training_ecr_specs: Optional[JumpStartECRSpecs] = (
Expand Down
12 changes: 12 additions & 0 deletions src/sagemaker/jumpstart/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

from sagemaker.jumpstart import constants, enums
from sagemaker.jumpstart import accessors
from sagemaker.jumpstart.hub.parser_utils import camel_to_snake, snake_to_upper_camel
from sagemaker.s3 import parse_s3_url
from sagemaker.jumpstart.exceptions import (
DeprecatedJumpStartModelError,
Expand Down Expand Up @@ -1103,6 +1104,17 @@ def get_jumpstart_configs(
metadata_configs.config_rankings.get("overall").rankings if metadata_configs else []
)

if hub_arn:
return (
{
config_name: metadata_configs.configs[
camel_to_snake(snake_to_upper_camel(config_name))
]
for config_name in config_names
}
if metadata_configs
else {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ternary + list-comprehension is a recipe for unreadability. Consider:

if metadata_configs:
  return { ... }
else:
  return {}

)
return (
{config_name: metadata_configs.configs[config_name] for config_name in config_names}
if metadata_configs
Expand Down
Loading