Skip to content

Commit 09417d1

Browse files
committed
fix: address review on capacity_reservation_config deploy paths
- modelbuilder_list: pass capacity_reservation_config only to the deploy call that creates the endpoint, so later inference components and the custom orchestrator no longer log a false "ignored" warning. - update_endpoint=True: pass wait through to Session.update_endpoint so deploy(update_endpoint=True, wait=False) does not block. - reuse_resources: _reused_endpoint_matches_config compares the variant's MlReservationArn and CapacityReservationPreference, so an On-Demand or differently reserved endpoint is never reused for a reservation request. - model customization: warn when the endpoint already exists and the reservation cannot apply, matching the other deploy paths. - deploy(): move capacity_reservation_config after endpoint_config_name so existing positional arguments keep their positions.
1 parent 5d6888b commit 09417d1

2 files changed

Lines changed: 249 additions & 36 deletions

File tree

‎sagemaker-serve/src/sagemaker/serve/model_builder.py‎

Lines changed: 87 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,14 +2536,23 @@ def _find_reusable_model(self) -> Optional["Model"]:
25362536

25372537
return None
25382538

2539-
def _find_reusable_endpoint(self, instance_type: Optional[str] = None) -> Optional[str]:
2539+
def _find_reusable_endpoint(
2540+
self,
2541+
instance_type: Optional[str] = None,
2542+
capacity_reservation_config: Optional[
2543+
Union[Dict[str, Any], ProductionVariantCapacityReservationConfig]
2544+
] = None,
2545+
) -> Optional[str]:
25402546
"""Return the name of an existing endpoint that can be reused, if any.
25412547
25422548
A candidate must carry the same model-source tag and match the requested
2543-
deployment configuration (env vars, image URI, instance type).
2549+
deployment configuration (env vars, image URI, instance type, capacity
2550+
reservation).
25442551
25452552
Args:
25462553
instance_type: The instance type requested for this deploy, if any.
2554+
capacity_reservation_config: The capacity reservation requested for this
2555+
deploy, if any.
25472556
25482557
Returns:
25492558
The reusable endpoint name, or None if there is no suitable match.
@@ -2561,7 +2570,9 @@ def _find_reusable_endpoint(self, instance_type: Optional[str] = None) -> Option
25612570

25622571
existing_name = existing_arn.rsplit("/", 1)[-1]
25632572
if self._reused_endpoint_matches_config(
2564-
existing_name, instance_type=instance_type or self.instance_type
2573+
existing_name,
2574+
instance_type=instance_type or self.instance_type,
2575+
capacity_reservation_config=capacity_reservation_config,
25652576
):
25662577
return existing_name
25672578

@@ -2573,19 +2584,27 @@ def _find_reusable_endpoint(self, instance_type: Optional[str] = None) -> Option
25732584
return None
25742585

25752586
def _reused_endpoint_matches_config(
2576-
self, endpoint_name: str, instance_type: Optional[str] = None
2587+
self,
2588+
endpoint_name: str,
2589+
instance_type: Optional[str] = None,
2590+
capacity_reservation_config: Optional[
2591+
Union[Dict[str, Any], ProductionVariantCapacityReservationConfig]
2592+
] = None,
25772593
) -> bool:
25782594
"""Check that a reuse candidate endpoint matches the requested deploy config.
25792595
25802596
A source-tag match only proves the endpoint was built from the same model
25812597
artifacts. Before reusing it, confirm the runtime configuration the caller
2582-
requested (container environment variables, instance type, and image URI)
2583-
also matches, so a differently-configured request does not silently get an
2584-
endpoint that contradicts it.
2598+
requested (container environment variables, instance type, image URI, and
2599+
capacity reservation) also matches, so a differently-configured request does
2600+
not silently get an endpoint that contradicts it.
25852601
25862602
Args:
25872603
endpoint_name: Name of the candidate endpoint to inspect.
25882604
instance_type: The instance type requested for this deploy, if any.
2605+
capacity_reservation_config: The capacity reservation requested for this
2606+
deploy, if any. A candidate matches only if its variant uses the same
2607+
reservation ARN and preference.
25892608
25902609
Returns:
25912610
True if the candidate matches (or the config cannot be read and reuse
@@ -2609,6 +2628,19 @@ def _reused_endpoint_matches_config(
26092628
and instance_type != variant["InstanceType"]
26102629
):
26112630
return False
2631+
# Like InstanceType, the reservation lives on the variant: check it before
2632+
# the IC early-return so an On-Demand endpoint is never reused for a
2633+
# reserved-capacity request.
2634+
requested_reservation = _resolve_capacity_reservation_config(
2635+
capacity_reservation_config
2636+
)
2637+
if requested_reservation:
2638+
existing_reservation = variant.get("CapacityReservationConfig") or {}
2639+
if any(
2640+
existing_reservation.get(key) != requested_reservation.get(key)
2641+
for key in ("MlReservationArn", "CapacityReservationPreference")
2642+
):
2643+
return False
26122644
model_name = variant.get("ModelName")
26132645
if not model_name:
26142646
# IC-based endpoint: no ModelName to validate container config
@@ -4334,7 +4366,9 @@ def _deploy_core_endpoint(self, **kwargs):
43344366
explainer_config_dict=explainer_config_dict,
43354367
async_inference_config_dict=async_inference_config_dict,
43364368
)
4337-
self.sagemaker_session.update_endpoint(self.endpoint_name, endpoint_config_name)
4369+
self.sagemaker_session.update_endpoint(
4370+
self.endpoint_name, endpoint_config_name, wait=wait
4371+
)
43384372
else:
43394373
self.sagemaker_session.endpoint_from_production_variants(
43404374
name=self.endpoint_name,
@@ -6116,9 +6150,6 @@ def deploy(
61166150
custom_orchestrator_instance_type: str = None,
61176151
custom_orchestrator_initial_instance_count: int = None,
61186152
reuse_resources: bool = False,
6119-
capacity_reservation_config: Optional[
6120-
Union[Dict[str, Any], ProductionVariantCapacityReservationConfig]
6121-
] = None,
61226153
# Recommendation-mode kwargs. These take effect only on the
61236154
# recommendation deploy path (a recommendation job is attached and
61246155
# use_recommendation is not False); they are ignored when deploying a
@@ -6133,6 +6164,9 @@ def deploy(
61336164
role: Optional[str] = None,
61346165
model_name: Optional[str] = None,
61356166
endpoint_config_name: Optional[str] = None,
6167+
capacity_reservation_config: Optional[
6168+
Union[Dict[str, Any], ProductionVariantCapacityReservationConfig]
6169+
] = None,
61366170
**kwargs,
61376171
) -> Union[Endpoint, LocalEndpoint, Transformer]:
61386172
"""Deploy the built model to an ``Endpoint``.
@@ -6182,19 +6216,6 @@ def deploy(
61826216
``ResourceRequirements``, or a ``modelbuilder_list`` build) are not
61836217
intercepted by this flag — they manage their own reuse by inference
61846218
component name (create vs. in-place update).
6185-
capacity_reservation_config (Union[dict,
6186-
sagemaker.core.shapes.ProductionVariantCapacityReservationConfig], optional):
6187-
Launches the endpoint's instances on reserved capacity instead of the
6188-
On-Demand pool. ``ml_reservation_arn`` is either the ARN of a SageMaker
6189-
training plan purchased with target resource ``endpoint`` (Flexible Training
6190-
Plans), or the ARN of an ML reservation backed by On-Demand Capacity
6191-
Reservations (ODCRs). With ``capacity_reservation_preference=
6192-
"capacity-reservations-only"``, instances launch only into the reservation
6193-
and the deployment fails if it has no free capacity; this setting is
6194-
required for training plans. A dict must use the API keys
6195-
``CapacityReservationPreference`` and ``MlReservationArn``. The
6196-
``instance_type`` must match the reservation's instance type. Not supported
6197-
with serverless inference. (Default: None).
61986219
use_recommendation (bool, optional): Controls the recommendation deploy path.
61996220
None (default) deploys the recommendation when a recommendation job is
62006221
attached, else the built model. False forces the built-model path even if a
@@ -6221,6 +6242,19 @@ def deploy(
62216242
endpoint_config_name (str, optional): Recommendation deploy only. Name for the created
62226243
EndpointConfig; auto-generated if omitted. Ignored when deploying a normally-built
62236244
model.
6245+
capacity_reservation_config (Union[dict,
6246+
sagemaker.core.shapes.ProductionVariantCapacityReservationConfig], optional):
6247+
Launches the endpoint's instances on reserved capacity instead of the
6248+
On-Demand pool. ``ml_reservation_arn`` is either the ARN of a SageMaker
6249+
training plan purchased with target resource ``endpoint`` (Flexible Training
6250+
Plans), or the ARN of an ML reservation backed by On-Demand Capacity
6251+
Reservations (ODCRs). With ``capacity_reservation_preference=
6252+
"capacity-reservations-only"``, instances launch only into the reservation
6253+
and the deployment fails if it has no free capacity; this setting is
6254+
required for training plans. A dict must use the API keys
6255+
``CapacityReservationPreference`` and ``MlReservationArn``. The
6256+
``instance_type`` must match the reservation's instance type. Not supported
6257+
with serverless inference. (Default: None).
62246258
Returns:
62256259
Union[Endpoint, LocalEndpoint, Transformer]: A ``sagemaker.core.resources.Endpoint``
62266260
resource representing the deployed endpoint, a ``LocalEndpoint`` for local mode,
@@ -6341,7 +6375,10 @@ def deploy(
63416375
# build() does not look for or cache an endpoint.
63426376
if reuse_resources and not is_inference_component_deploy:
63436377
requested_instance_type = instance_type or self.instance_type
6344-
reusable_endpoint = self._find_reusable_endpoint(instance_type=requested_instance_type)
6378+
reusable_endpoint = self._find_reusable_endpoint(
6379+
instance_type=requested_instance_type,
6380+
capacity_reservation_config=capacity_reservation_config,
6381+
)
63456382
if reusable_endpoint:
63466383
if endpoint_name and endpoint_name != reusable_endpoint:
63476384
logger.warning(
@@ -6471,15 +6508,26 @@ def deploy(
64716508

64726509
if hasattr(self, "_deployables") and self._deployables:
64736510
endpoints = []
6474-
# Only the capacity reservation is forwarded here; the endpoint created for the
6475-
# first inference component must launch on the reserved capacity.
6476-
ic_kwargs = {}
6511+
# The reservation belongs to the endpoint, which the first deploy call creates.
6512+
# Later calls add components to that endpoint, so they don't get the config
6513+
# (they would otherwise warn that it is ignored).
6514+
pending_capacity_kwargs = {}
64776515
if kwargs.get("capacity_reservation_config") is not None:
6478-
ic_kwargs["capacity_reservation_config"] = kwargs["capacity_reservation_config"]
6516+
pending_capacity_kwargs["capacity_reservation_config"] = kwargs[
6517+
"capacity_reservation_config"
6518+
]
6519+
orchestrator_kwargs = {
6520+
k: v for k, v in kwargs.items() if k != "capacity_reservation_config"
6521+
}
64796522
for ic in self._deployables.get("InferenceComponents", []):
64806523
endpoints.append(
6481-
self._deploy_for_ic(ic_data=ic, endpoint_name=endpoint_name, **ic_kwargs)
6524+
self._deploy_for_ic(
6525+
ic_data=ic, endpoint_name=endpoint_name, **pending_capacity_kwargs
6526+
)
64826527
)
6528+
pending_capacity_kwargs = {}
6529+
# The orchestrator only creates the endpoint when no component came first.
6530+
orchestrator_kwargs.update(pending_capacity_kwargs)
64836531

64846532
# Handle custom orchestrator if present
64856533
if self._deployables.get("CustomOrchestrator"):
@@ -6506,7 +6554,7 @@ def deploy(
65066554
"endpoint_type": EndpointType.INFERENCE_COMPONENT_BASED,
65076555
}
65086556

6509-
deploy_kwargs.update(kwargs)
6557+
deploy_kwargs.update(orchestrator_kwargs)
65106558
endpoints.append(self._deploy(**deploy_kwargs))
65116559
elif custom_orchestrator["Mode"] == "InferenceComponent":
65126560
logger.info(
@@ -6521,7 +6569,7 @@ def deploy(
65216569
initial_instance_count=custom_orchestrator_initial_instance_count
65226570
or initial_instance_count,
65236571
endpoint_name=endpoint_name,
6524-
**kwargs,
6572+
**orchestrator_kwargs,
65256573
)
65266574
)
65276575

@@ -6707,6 +6755,13 @@ def _deploy_model_customization(
67076755
)
67086756
endpoint.wait_for_status("InService")
67096757
else:
6758+
if capacity_reservation_shape is not None:
6759+
logger.warning(
6760+
"capacity_reservation_config is ignored because endpoint %s already "
6761+
"exists; the inference component is added to the endpoint's existing "
6762+
"capacity.",
6763+
endpoint_name,
6764+
)
67106765
endpoint = Endpoint.get(endpoint_name=endpoint_name)
67116766

67126767
if peft_type == "LORA":

0 commit comments

Comments
 (0)