Repository navigation
feature: support capacity_reservation_config in ModelBuilder.deploy #6575
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
base: master
Are you sure you want to change the base?
Changes from all commits
9192bb7
86e0357
5d6888b
09417d1
f19ebf4
a202011
0b4b90c
693365f
e9056e8
ffcb9f4
c0b0264
8b85ab1
ce0f06d
e67637e
6a7011c
84da446
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2129,6 +2129,75 @@ def _resolve_routing_config(routing_config: Optional[Dict[str, Any]]) -> Optiona | |
| return None | ||
|
|
||
|
|
||
| _CAPACITY_RESERVATION_CONFIG_KEYS = ("CapacityReservationPreference", "MlReservationArn") | ||
| _CAPACITY_RESERVATIONS_ONLY = "capacity-reservations-only" | ||
|
|
||
|
|
||
| def _resolve_capacity_reservation_config( | ||
| capacity_reservation_config: Optional[Any], | ||
| ) -> Optional[Dict[str, Any]]: | ||
| """Resolve a production variant capacity reservation config to its request dict. | ||
|
|
||
| Args: | ||
| capacity_reservation_config (Optional[Union[dict, | ||
| sagemaker.core.shapes.ProductionVariantCapacityReservationConfig]]): | ||
| Either a ``ProductionVariantCapacityReservationConfig`` shape or a dict using the | ||
| ``CreateEndpointConfig`` request keys (``CapacityReservationPreference``, | ||
| ``MlReservationArn``). | ||
|
|
||
| Returns: | ||
| Optional[Dict[str, Any]]: The ``CapacityReservationConfig`` request dict, or None if | ||
| no config was given or it has no fields set. A training plan ARN | ||
| (``...:training-plan/<name>``) given without a preference gets | ||
| ``CapacityReservationPreference="capacity-reservations-only"``, the only value | ||
| SageMaker accepts for training plans. Other ARNs are returned as given. | ||
|
|
||
| Raises: | ||
| ValueError: If a dict contains keys other than ``CapacityReservationPreference`` and | ||
| ``MlReservationArn``. | ||
| TypeError: If the config is neither a dict nor a | ||
| ``ProductionVariantCapacityReservationConfig``. | ||
| """ | ||
| if capacity_reservation_config is None: | ||
| return None | ||
|
|
||
| # Imported lazily: sagemaker.core.shapes imports from this module. | ||
| from sagemaker.core.shapes import ProductionVariantCapacityReservationConfig | ||
| from sagemaker.core.utils.utils import serialize | ||
|
|
||
| if isinstance(capacity_reservation_config, ProductionVariantCapacityReservationConfig): | ||
| return _default_training_plan_preference(serialize(capacity_reservation_config) or None) | ||
|
|
||
| if isinstance(capacity_reservation_config, dict): | ||
| unknown_keys = set(capacity_reservation_config) - set(_CAPACITY_RESERVATION_CONFIG_KEYS) | ||
| if unknown_keys: | ||
| raise ValueError( | ||
| f"Unsupported capacity_reservation_config keys: {sorted(unknown_keys)}. " | ||
| f"Supported keys are: {list(_CAPACITY_RESERVATION_CONFIG_KEYS)}." | ||
| ) | ||
| resolved = {k: v for k, v in capacity_reservation_config.items() if v is not None} | ||
| return _default_training_plan_preference(resolved or None) | ||
|
|
||
| raise TypeError( | ||
| "capacity_reservation_config must be a dict or a " | ||
| "sagemaker.core.shapes.ProductionVariantCapacityReservationConfig, got " | ||
| f"{type(capacity_reservation_config).__name__}." | ||
| ) | ||
|
|
||
|
|
||
| def _default_training_plan_preference( | ||
| resolved: Optional[Dict[str, Any]], | ||
| ) -> Optional[Dict[str, Any]]: | ||
| """Set the preference a training plan requires when the caller left it unset.""" | ||
| if ( | ||
| resolved | ||
| and ":training-plan/" in str(resolved.get("MlReservationArn", "")) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Add the default only when the ARN is a reservation_arn = (resolved or {}).get("MlReservationArn")
if (
resolved
and "CapacityReservationPreference" not in resolved
and isinstance(reservation_arn, str)
and ":training-plan/" in reservation_arn
):With this condition, the call returns the |
||
| and "CapacityReservationPreference" not in resolved | ||
| ): | ||
| return {**resolved, "CapacityReservationPreference": _CAPACITY_RESERVATIONS_ONLY} | ||
| return resolved | ||
|
|
||
|
|
||
| @lru_cache | ||
| def get_instance_rate_per_hour( | ||
| instance_type: str, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ | |
| instance_supports_kms, | ||
| create_paginator_config, | ||
| validate_path_within_directory, | ||
| _resolve_capacity_reservation_config, | ||
| ) | ||
|
|
||
| from sagemaker.core.config.config_utils import _log_sagemaker_config_merge | ||
|
|
@@ -2940,6 +2941,7 @@ def production_variant( | |
| managed_instance_scaling=None, | ||
| routing_config=None, | ||
| inference_ami_version=None, | ||
| capacity_reservation_config=None, | ||
| ): | ||
| """Create a production variant description suitable for use in a ``ProductionVariant`` list. | ||
|
|
||
|
|
@@ -2973,6 +2975,13 @@ def production_variant( | |
| https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-inference-code.html | ||
| #your-algorithms | ||
| -inference-algo-ping-requests | ||
| capacity_reservation_config (Union[dict, | ||
| sagemaker.core.shapes.ProductionVariantCapacityReservationConfig]): Runs the | ||
| variant's instances on reserved capacity, either a SageMaker training plan | ||
| created for the ``endpoint`` target resource or an ML reservation backed by | ||
| On-Demand Capacity Reservations (ODCRs). A dict must use the | ||
| ``CreateEndpointConfig`` request keys ``CapacityReservationPreference`` and | ||
| ``MlReservationArn``. Ignored for serverless variants (default: None). | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Low: This docstring says |
||
|
|
||
| Returns: | ||
| dict[str, str]: An SageMaker ``ProductionVariant`` description | ||
|
|
@@ -3002,6 +3011,9 @@ def production_variant( | |
| ModelDataDownloadTimeoutInSeconds=model_data_download_timeout, | ||
| ContainerStartupHealthCheckTimeoutInSeconds=container_startup_health_check_timeout, | ||
| RoutingConfig=routing_config, | ||
| CapacityReservationConfig=_resolve_capacity_reservation_config( | ||
| capacity_reservation_config | ||
| ), | ||
| ) | ||
|
|
||
| if inference_ami_version: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing from my live testing for FTP/ODCR change in Sherpa:
CreateEndpointConfigrejects a training-plan ARN unlessCapacityReservationPreferenceiscapacity-reservations-only. The docstring says this, but a caller who passes only the ARN won't find out until the API call fails. Would it make sense to default the preference when the ARN is atraining-plan/ARN, or raise a clear client-side error? botocore doesn't validate the ARN pattern or the enum either, so this would be the first place that catches it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in e9056e8. A
training-plan/ARN without a preference now getsCapacityReservationPreference="capacity-reservations-only"in_resolve_capacity_reservation_config. Every deploy path goes through that function. ML reservation ARNs and explicit preferences are passed as given, and the service still decides whether they're valid. Tests cover the shape and dict inputs, check that the caller's dict isn't mutated, and check the request built bydeploy.