|
14 | 14 | from __future__ import absolute_import |
15 | 15 | import logging |
16 | 16 | from typing import Dict, List, Optional |
| 17 | +from urllib.parse import urlparse |
17 | 18 | from packaging.version import Version |
18 | 19 | import sagemaker |
19 | | -from sagemaker.jumpstart import constants |
| 20 | +from sagemaker.jumpstart import constants, enums |
20 | 21 | from sagemaker.jumpstart import accessors |
| 22 | +from sagemaker.s3 import parse_s3_url |
21 | 23 | from sagemaker.jumpstart.exceptions import ( |
22 | 24 | DeprecatedJumpStartModelError, |
23 | 25 | VulnerableJumpStartModelError, |
@@ -150,6 +152,145 @@ def is_jumpstart_model_input(model_id: Optional[str], version: Optional[str]) -> |
150 | 152 | return False |
151 | 153 |
|
152 | 154 |
|
| 155 | +def is_jumpstart_model_uri(uri: Optional[str]) -> bool: |
| 156 | + """Returns True if URI corresponds to a JumpStart-hosted model. |
| 157 | +
|
| 158 | + Args: |
| 159 | + uri (Optional[str]): uri for inference/training job. |
| 160 | + """ |
| 161 | + |
| 162 | + bucket = None |
| 163 | + if urlparse(uri).scheme == "s3": |
| 164 | + bucket, _ = parse_s3_url(uri) |
| 165 | + |
| 166 | + return bucket in constants.JUMPSTART_BUCKET_NAME_SET |
| 167 | + |
| 168 | + |
| 169 | +def tag_key_in_array(tag_key: str, tag_array: List[Dict[str, str]]) -> bool: |
| 170 | + """Returns True if ``tag_key`` is in the ``tag_array``. |
| 171 | +
|
| 172 | + Args: |
| 173 | + tag_key (str): the tag key to check if it's already in the ``tag_array``. |
| 174 | + tag_array (List[Dict[str, str]]): array of tags to check for ``tag_key``. |
| 175 | + """ |
| 176 | + for tag in tag_array: |
| 177 | + if tag_key == tag["Key"]: |
| 178 | + return True |
| 179 | + return False |
| 180 | + |
| 181 | + |
| 182 | +def get_tag_value(tag_key: str, tag_array: List[Dict[str, str]]) -> str: |
| 183 | + """Return the value of a tag whose key matches the given ``tag_key``. |
| 184 | +
|
| 185 | + Args: |
| 186 | + tag_key (str): AWS tag for which to search. |
| 187 | + tag_array (List[Dict[str, str]]): List of AWS tags, each formatted as dicts. |
| 188 | +
|
| 189 | + Raises: |
| 190 | + KeyError: If the number of matches for the ``tag_key`` is not equal to 1. |
| 191 | + """ |
| 192 | + tag_values = [tag["Value"] for tag in tag_array if tag_key == tag["Key"]] |
| 193 | + if len(tag_values) != 1: |
| 194 | + raise KeyError( |
| 195 | + f"Cannot get value of tag for tag key '{tag_key}' -- found {len(tag_values)} " |
| 196 | + f"number of matches in the tag list." |
| 197 | + ) |
| 198 | + |
| 199 | + return tag_values[0] |
| 200 | + |
| 201 | + |
| 202 | +def add_single_jumpstart_tag( |
| 203 | + uri: str, tag_key: enums.JumpStartTag, curr_tags: Optional[List[Dict[str, str]]] |
| 204 | +) -> Optional[List]: |
| 205 | + """Adds ``tag_key`` to ``curr_tags`` if ``uri`` corresponds to a JumpStart model. |
| 206 | +
|
| 207 | + Args: |
| 208 | + uri (str): URI which may correspond to a JumpStart model. |
| 209 | + tag_key (enums.JumpStartTag): Custom tag to apply to current tags if the URI |
| 210 | + corresponds to a JumpStart model. |
| 211 | + curr_tags (Optional[List]): Current tags associated with ``Estimator`` or ``Model``. |
| 212 | + """ |
| 213 | + if is_jumpstart_model_uri(uri): |
| 214 | + if curr_tags is None: |
| 215 | + curr_tags = [] |
| 216 | + if not tag_key_in_array(tag_key, curr_tags): |
| 217 | + curr_tags.append( |
| 218 | + { |
| 219 | + "Key": tag_key, |
| 220 | + "Value": uri, |
| 221 | + } |
| 222 | + ) |
| 223 | + return curr_tags |
| 224 | + |
| 225 | + |
| 226 | +def add_jumpstart_tags( |
| 227 | + tags: Optional[List[Dict[str, str]]] = None, |
| 228 | + inference_model_uri: Optional[str] = None, |
| 229 | + inference_script_uri: Optional[str] = None, |
| 230 | + training_model_uri: Optional[str] = None, |
| 231 | + training_script_uri: Optional[str] = None, |
| 232 | +) -> Optional[List[Dict[str, str]]]: |
| 233 | + """Add custom tags to JumpStart models, return the updated tags. |
| 234 | +
|
| 235 | + No-op if this is not a JumpStart model related resource. |
| 236 | +
|
| 237 | + Args: |
| 238 | + tags (Optional[List[Dict[str,str]]): Current tags for JumpStart inference |
| 239 | + or training job. (Default: None). |
| 240 | + inference_model_uri (Optional[str]): S3 URI for inference model artifact. |
| 241 | + (Default: None). |
| 242 | + inference_script_uri (Optional[str]): S3 URI for inference script tarball. |
| 243 | + (Default: None). |
| 244 | + training_model_uri (Optional[str]): S3 URI for training model artifact. |
| 245 | + (Default: None). |
| 246 | + training_script_uri (Optional[str]): S3 URI for training script tarball. |
| 247 | + (Default: None). |
| 248 | + """ |
| 249 | + |
| 250 | + if inference_model_uri: |
| 251 | + tags = add_single_jumpstart_tag( |
| 252 | + inference_model_uri, enums.JumpStartTag.INFERENCE_MODEL_URI, tags |
| 253 | + ) |
| 254 | + |
| 255 | + if inference_script_uri: |
| 256 | + tags = add_single_jumpstart_tag( |
| 257 | + inference_script_uri, enums.JumpStartTag.INFERENCE_SCRIPT_URI, tags |
| 258 | + ) |
| 259 | + |
| 260 | + if training_model_uri: |
| 261 | + tags = add_single_jumpstart_tag( |
| 262 | + training_model_uri, enums.JumpStartTag.TRAINING_MODEL_URI, tags |
| 263 | + ) |
| 264 | + |
| 265 | + if training_script_uri: |
| 266 | + tags = add_single_jumpstart_tag( |
| 267 | + training_script_uri, enums.JumpStartTag.TRAINING_SCRIPT_URI, tags |
| 268 | + ) |
| 269 | + |
| 270 | + return tags |
| 271 | + |
| 272 | + |
| 273 | +def update_inference_tags_with_jumpstart_training_tags( |
| 274 | + inference_tags: Optional[List[Dict[str, str]]], training_tags: Optional[List[Dict[str, str]]] |
| 275 | +) -> Optional[List[Dict[str, str]]]: |
| 276 | + """Updates the tags for the ``sagemaker.model.Model.deploy`` command with any JumpStart tags. |
| 277 | +
|
| 278 | + Args: |
| 279 | + inference_tags (Optional[List[Dict[str, str]]]): Custom tags to appy to inference job. |
| 280 | + training_tags (Optional[List[Dict[str, str]]]): Tags from training job. |
| 281 | + """ |
| 282 | + if training_tags: |
| 283 | + for tag_key in enums.JumpStartTag: |
| 284 | + if tag_key_in_array(tag_key, training_tags): |
| 285 | + tag_value = get_tag_value(tag_key, training_tags) |
| 286 | + if inference_tags is None: |
| 287 | + inference_tags = [] |
| 288 | + if not tag_key_in_array(tag_key, inference_tags): |
| 289 | + inference_tags.append({"Key": tag_key, "Value": tag_value}) |
| 290 | + |
| 291 | + return inference_tags |
| 292 | + |
| 293 | + |
153 | 294 | def verify_model_region_and_return_specs( |
154 | 295 | model_id: Optional[str], |
155 | 296 | version: Optional[str], |
|
0 commit comments