|
| 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 and optionally inference component names, return the 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 model-based endpoint. |
| 35 | +
|
| 36 | + JumpStart adds tags automatically to endpoints, models, endpoint configs, and inference |
| 37 | + components launched in SageMaker Studio and programmatically with the SageMaker Python SDK. |
| 38 | +
|
| 39 | + Raises: |
| 40 | + ValueError: If model ID and version cannot be inferred from the endpoint. |
| 41 | + """ |
| 42 | + if inference_component_name or sagemaker_session.is_inference_component_based_endpoint( |
| 43 | + endpoint_name |
| 44 | + ): |
| 45 | + if inference_component_name: |
| 46 | + ( |
| 47 | + model_id, |
| 48 | + model_version, |
| 49 | + ) = _get_model_id_version_from_inference_component_endpoint_with_inference_component_name( # noqa E501 # pylint: disable=c0301 |
| 50 | + inference_component_name, sagemaker_session |
| 51 | + ) |
| 52 | + |
| 53 | + else: |
| 54 | + ( |
| 55 | + model_id, |
| 56 | + model_version, |
| 57 | + inference_component_name, |
| 58 | + ) = _get_model_id_version_from_inference_component_endpoint_without_inference_component_name( # noqa E501 # pylint: disable=c0301 |
| 59 | + endpoint_name, sagemaker_session |
| 60 | + ) |
| 61 | + |
| 62 | + else: |
| 63 | + model_id, model_version = _get_model_id_version_from_model_based_endpoint( |
| 64 | + endpoint_name, inference_component_name, sagemaker_session |
| 65 | + ) |
| 66 | + return model_id, model_version, inference_component_name |
| 67 | + |
| 68 | + |
| 69 | +def _get_model_id_version_from_inference_component_endpoint_without_inference_component_name( |
| 70 | + endpoint_name: str, sagemaker_session: Session |
| 71 | +) -> Tuple[str, str, str]: |
| 72 | + """Given an endpoint name, derives the model ID, version, and inferred inference component name. |
| 73 | +
|
| 74 | + This function assumes the endpoint corresponds to an inference-component-based endpoint. |
| 75 | + An endpoint is inference-component-based if and only if the associated endpoint config |
| 76 | + has a role associated with it and no production variants with a ``ModelName`` field. |
| 77 | +
|
| 78 | + Raises: |
| 79 | + ValueError: If there is not a single inference component associated with the endpoint. |
| 80 | + """ |
| 81 | + inference_component_names = ( |
| 82 | + sagemaker_session.list_and_paginate_inference_component_names_associated_with_endpoint( |
| 83 | + endpoint_name=endpoint_name |
| 84 | + ) |
| 85 | + ) |
| 86 | + |
| 87 | + if len(inference_component_names) == 0: |
| 88 | + raise ValueError( |
| 89 | + f"No inference component found for the following endpoint: {endpoint_name}. " |
| 90 | + "Use ``SageMaker.CreateInferenceComponent`` to add inference components to " |
| 91 | + "your endpoint." |
| 92 | + ) |
| 93 | + if len(inference_component_names) > 1: |
| 94 | + raise ValueError( |
| 95 | + f"Multiple inference components found for the following endpoint: {endpoint_name}. " |
| 96 | + "Provide an 'inference_component_name' to retrieve the model ID and version " |
| 97 | + "associated with a particular inference component." |
| 98 | + ) |
| 99 | + inference_component_name = inference_component_names[0] |
| 100 | + return ( |
| 101 | + *_get_model_id_version_from_inference_component_endpoint_with_inference_component_name( |
| 102 | + inference_component_name, sagemaker_session |
| 103 | + ), |
| 104 | + inference_component_name, |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +def _get_model_id_version_from_inference_component_endpoint_with_inference_component_name( |
| 109 | + inference_component_name: str, sagemaker_session: Session |
| 110 | +): |
| 111 | + """Returns the model ID and version inferred from a SageMaker inference component. |
| 112 | +
|
| 113 | + Raises: |
| 114 | + ValueError: If the inference component does not have tags from which the model ID |
| 115 | + and version can be inferred. |
| 116 | + """ |
| 117 | + region: str = sagemaker_session.boto_region_name |
| 118 | + partition: str = aws_partition(region) |
| 119 | + account_id: str = sagemaker_session.account_id() |
| 120 | + |
| 121 | + inference_component_arn = ( |
| 122 | + f"arn:{partition}:sagemaker:{region}:{account_id}:" |
| 123 | + f"inference-component/{inference_component_name}" |
| 124 | + ) |
| 125 | + |
| 126 | + model_id, model_version = get_jumpstart_model_id_version_from_resource_arn( |
| 127 | + inference_component_arn, sagemaker_session |
| 128 | + ) |
| 129 | + |
| 130 | + if not model_id: |
| 131 | + raise ValueError( |
| 132 | + "Cannot infer JumpStart model ID from inference component " |
| 133 | + f"'{inference_component_name}'. Please specify JumpStart `model_id` " |
| 134 | + "when retrieving default predictor for this inference component." |
| 135 | + ) |
| 136 | + |
| 137 | + return model_id, model_version |
| 138 | + |
| 139 | + |
| 140 | +def _get_model_id_version_from_model_based_endpoint( |
| 141 | + endpoint_name: str, |
| 142 | + inference_component_name: Optional[str], |
| 143 | + sagemaker_session: Session, |
| 144 | +) -> Tuple[str, str]: |
| 145 | + """Returns the model ID and version inferred from a model-based endpoint. |
| 146 | +
|
| 147 | + Raises: |
| 148 | + ValueError: If an inference component name is supplied, or if the endpoint does |
| 149 | + not have tags from which the model ID and version can be inferred. |
| 150 | + """ |
| 151 | + |
| 152 | + if inference_component_name: |
| 153 | + raise ValueError("Cannot specify inference component name for model-based endpoints.") |
| 154 | + |
| 155 | + region: str = sagemaker_session.boto_region_name |
| 156 | + partition: str = aws_partition(region) |
| 157 | + account_id: str = sagemaker_session.account_id() |
| 158 | + |
| 159 | + # SageMaker Tagging requires endpoint names to be lower cased |
| 160 | + endpoint_name = endpoint_name.lower() |
| 161 | + |
| 162 | + endpoint_arn = f"arn:{partition}:sagemaker:{region}:{account_id}:endpoint/{endpoint_name}" |
| 163 | + |
| 164 | + model_id, model_version = get_jumpstart_model_id_version_from_resource_arn( |
| 165 | + endpoint_arn, sagemaker_session |
| 166 | + ) |
| 167 | + |
| 168 | + if not model_id: |
| 169 | + raise ValueError( |
| 170 | + f"Cannot infer JumpStart model ID from endpoint '{endpoint_name}'. " |
| 171 | + "Please specify JumpStart `model_id` when retrieving default " |
| 172 | + "predictor for this endpoint." |
| 173 | + ) |
| 174 | + |
| 175 | + return model_id, model_version |
| 176 | + |
| 177 | + |
| 178 | +def get_model_id_version_from_training_job( |
| 179 | + training_job_name: str, |
| 180 | + sagemaker_session: Optional[Session] = DEFAULT_JUMPSTART_SAGEMAKER_SESSION, |
| 181 | +) -> Tuple[str, str]: |
| 182 | + """Returns the model ID and version inferred from a training job. |
| 183 | +
|
| 184 | + Raises: |
| 185 | + ValueError: If the training job does not have tags from which the model ID |
| 186 | + and version can be inferred. JumpStart adds tags automatically to training jobs |
| 187 | + launched in SageMaker Studio and programmatically with the SageMaker Python SDK. |
| 188 | + """ |
| 189 | + region: str = sagemaker_session.boto_region_name |
| 190 | + partition: str = aws_partition(region) |
| 191 | + account_id: str = sagemaker_session.account_id() |
| 192 | + |
| 193 | + training_job_arn = ( |
| 194 | + f"arn:{partition}:sagemaker:{region}:{account_id}:training-job/{training_job_name}" |
| 195 | + ) |
| 196 | + |
| 197 | + model_id, inferred_model_version = get_jumpstart_model_id_version_from_resource_arn( |
| 198 | + training_job_arn, sagemaker_session |
| 199 | + ) |
| 200 | + |
| 201 | + model_version = inferred_model_version or None |
| 202 | + |
| 203 | + if not model_id: |
| 204 | + raise ValueError( |
| 205 | + f"Cannot infer JumpStart model ID from training job '{training_job_name}'. " |
| 206 | + "Please specify JumpStart `model_id` when retrieving Estimator " |
| 207 | + "for this training job." |
| 208 | + ) |
| 209 | + |
| 210 | + return model_id, model_version |
0 commit comments