|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""This module stores SageMaker Session utilities for JumpStart models.""" |
| 14 | + |
| 15 | +from __future__ import absolute_import |
| 16 | + |
| 17 | +from typing import Optional, Tuple |
| 18 | +from sagemaker.jumpstart.constants import DEFAULT_JUMPSTART_SAGEMAKER_SESSION |
| 19 | + |
| 20 | +from sagemaker.jumpstart.utils import get_jumpstart_model_id_version_from_resource_arn |
| 21 | +from sagemaker.session import Session |
| 22 | +from sagemaker.utils import aws_partition |
| 23 | + |
| 24 | + |
| 25 | +def get_model_id_version_from_endpoint( |
| 26 | + endpoint_name: str, |
| 27 | + inference_component_name: Optional[str] = None, |
| 28 | + sagemaker_session: Session = DEFAULT_JUMPSTART_SAGEMAKER_SESSION, |
| 29 | +) -> Tuple[str, str, Optional[str]]: |
| 30 | + """Given an endpoint name (and optionally an IC name), get the JumpStart model ID and version. |
| 31 | +
|
| 32 | + Infers the model ID and version based on the resource tags. Returns a tuple of the model ID |
| 33 | + and version. A third string element is included in the tuple for any inferred inference |
| 34 | + component name, or None if it's a non-IC endpoint. |
| 35 | +
|
| 36 | + Raises: |
| 37 | + ValueError: If model ID and version cannot be inferred from the endpoint. |
| 38 | + """ |
| 39 | + if inference_component_name or sagemaker_session.is_ic_based_endpoint(endpoint_name): |
| 40 | + if inference_component_name: |
| 41 | + model_id, model_version = _get_model_id_version_from_ic_endpoint_with_ic_name( |
| 42 | + inference_component_name, sagemaker_session |
| 43 | + ) |
| 44 | + |
| 45 | + else: |
| 46 | + ( |
| 47 | + model_id, |
| 48 | + model_version, |
| 49 | + inference_component_name, |
| 50 | + ) = _get_model_id_version_from_ic_endpoint_without_ic_name( |
| 51 | + endpoint_name, sagemaker_session |
| 52 | + ) |
| 53 | + |
| 54 | + else: |
| 55 | + model_id, model_version = _get_model_id_version_from_non_ic_endpoint( |
| 56 | + endpoint_name, inference_component_name, sagemaker_session |
| 57 | + ) |
| 58 | + return model_id, model_version, inference_component_name |
| 59 | + |
| 60 | + |
| 61 | +def _get_model_id_version_from_ic_endpoint_without_ic_name( |
| 62 | + endpoint_name: str, sagemaker_session: Session |
| 63 | +) -> Tuple[str, str, str]: |
| 64 | + """Given an endpoint name, derives the model ID, version, and inferred inference component name. |
| 65 | +
|
| 66 | + This function assumes the endpoint corresponds to an inference component-based endpoint. |
| 67 | +
|
| 68 | + Raises: |
| 69 | + ValueError: If there is not a single inference component associated with the endpoint. |
| 70 | + """ |
| 71 | + inference_components = sagemaker_session.list_inference_components( |
| 72 | + endpoint_name_equals=endpoint_name |
| 73 | + )["InferenceComponents"] |
| 74 | + |
| 75 | + if len(inference_components) == 0: |
| 76 | + raise ValueError( |
| 77 | + f"No inference components found for the following endpoint: {endpoint_name}. " |
| 78 | + "Use ``SageMaker.CreateInferenceComponent`` to add inference components to " |
| 79 | + "your endpoint." |
| 80 | + ) |
| 81 | + if len(inference_components) > 1: |
| 82 | + raise ValueError( |
| 83 | + "Must provide inference component name for endpoint with " |
| 84 | + "multiple inference components." |
| 85 | + ) |
| 86 | + inference_component_name = inference_components[0]["InferenceComponentName"] |
| 87 | + return ( |
| 88 | + *_get_model_id_version_from_ic_endpoint_with_ic_name( |
| 89 | + inference_component_name, sagemaker_session |
| 90 | + ), |
| 91 | + inference_component_name, |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def _get_model_id_version_from_ic_endpoint_with_ic_name( |
| 96 | + inference_component_name: str, sagemaker_session: Session |
| 97 | +): |
| 98 | + """Returns the model ID and version inferred from a sagemaker inference component. |
| 99 | +
|
| 100 | + Raises: |
| 101 | + ValueError: If the inference component does not have tags from which the model ID |
| 102 | + and version can be inferred. |
| 103 | + """ |
| 104 | + region: str = sagemaker_session.boto_region_name |
| 105 | + partition: str = aws_partition(region) |
| 106 | + account_id: str = sagemaker_session.account_id() |
| 107 | + |
| 108 | + inference_component_arn = ( |
| 109 | + f"arn:{partition}:sagemaker:{region}:{account_id}:" |
| 110 | + f"inference-component/{inference_component_name}" |
| 111 | + ) |
| 112 | + |
| 113 | + model_id, model_version = get_jumpstart_model_id_version_from_resource_arn( |
| 114 | + inference_component_arn, sagemaker_session |
| 115 | + ) |
| 116 | + |
| 117 | + if not model_id: |
| 118 | + raise ValueError( |
| 119 | + "Cannot infer JumpStart model ID from inference component " |
| 120 | + f"'{inference_component_name}'. Please specify JumpStart `model_id` " |
| 121 | + "when retrieving default predictor for this inference component." |
| 122 | + ) |
| 123 | + |
| 124 | + return model_id, model_version |
| 125 | + |
| 126 | + |
| 127 | +def _get_model_id_version_from_non_ic_endpoint( |
| 128 | + endpoint_name: str, |
| 129 | + inference_component_name: Optional[str], |
| 130 | + sagemaker_session: Session, |
| 131 | +) -> Tuple[str, str]: |
| 132 | + """Returns the model ID and version inferred from a model-based endpoint.. |
| 133 | +
|
| 134 | + Raises: |
| 135 | + ValueError: If a non-None inference component name is supplied, or if the endpoint does |
| 136 | + not have tags from which the model ID and version can be inferred. |
| 137 | + """ |
| 138 | + |
| 139 | + if inference_component_name: |
| 140 | + raise ValueError("Cannot specify inference component name for model-based endpoints.") |
| 141 | + |
| 142 | + region: str = sagemaker_session.boto_region_name |
| 143 | + partition: str = aws_partition(region) |
| 144 | + account_id: str = sagemaker_session.account_id() |
| 145 | + |
| 146 | + endpoint_arn = f"arn:{partition}:sagemaker:{region}:{account_id}:endpoint/{endpoint_name}" |
| 147 | + |
| 148 | + model_id, model_version = get_jumpstart_model_id_version_from_resource_arn( |
| 149 | + endpoint_arn, sagemaker_session |
| 150 | + ) |
| 151 | + |
| 152 | + if not model_id: |
| 153 | + raise ValueError( |
| 154 | + f"Cannot infer JumpStart model ID from endpoint '{endpoint_name}'. " |
| 155 | + "Please specify JumpStart `model_id` when retrieving default " |
| 156 | + "predictor for this endpoint." |
| 157 | + ) |
| 158 | + |
| 159 | + return model_id, model_version |
0 commit comments