|
| 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 contains accessors related to SageMaker JumpStart.""" |
| 14 | +from __future__ import absolute_import |
| 15 | +from typing import Any, Dict, Optional |
| 16 | +from sagemaker.jumpstart.types import JumpStartModelHeader, JumpStartModelSpecs |
| 17 | +from sagemaker.jumpstart import cache |
| 18 | +from sagemaker.jumpstart.constants import JUMPSTART_DEFAULT_REGION_NAME |
| 19 | + |
| 20 | + |
| 21 | +class SageMakerSettings(object): |
| 22 | + """Static class for storing the SageMaker settings.""" |
| 23 | + |
| 24 | + _parsed_sagemaker_version = "" |
| 25 | + |
| 26 | + @staticmethod |
| 27 | + def set_sagemaker_version(version: str) -> None: |
| 28 | + """Set SageMaker version.""" |
| 29 | + SageMakerSettings._parsed_sagemaker_version = version |
| 30 | + |
| 31 | + @staticmethod |
| 32 | + def get_sagemaker_version() -> str: |
| 33 | + """Return SageMaker version.""" |
| 34 | + return SageMakerSettings._parsed_sagemaker_version |
| 35 | + |
| 36 | + |
| 37 | +class JumpStartModelsAccessor(object): |
| 38 | + """Static class for storing the JumpStart models cache.""" |
| 39 | + |
| 40 | + _cache: Optional[cache.JumpStartModelsCache] = None |
| 41 | + _curr_region = JUMPSTART_DEFAULT_REGION_NAME |
| 42 | + |
| 43 | + _cache_kwargs: Dict[str, Any] = {} |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def _validate_and_mutate_region_cache_kwargs( |
| 47 | + cache_kwargs: Optional[Dict[str, Any]] = None, region: Optional[str] = None |
| 48 | + ) -> Dict[str, Any]: |
| 49 | + """Returns cache_kwargs with region argument removed if present. |
| 50 | +
|
| 51 | + Raises: |
| 52 | + ValueError: If region in `cache_kwargs` is inconsistent with `region` argument. |
| 53 | +
|
| 54 | + Args: |
| 55 | + cache_kwargs (Optional[Dict[str, Any]]): cache kwargs to validate. |
| 56 | + region (str): The region to validate along with the kwargs. |
| 57 | + """ |
| 58 | + cache_kwargs_dict = {} if cache_kwargs is None else cache_kwargs |
| 59 | + assert isinstance(cache_kwargs_dict, dict) |
| 60 | + if region is not None and "region" in cache_kwargs_dict: |
| 61 | + if region != cache_kwargs_dict["region"]: |
| 62 | + raise ValueError( |
| 63 | + f"Inconsistent region definitions: {region}, {cache_kwargs_dict['region']}" |
| 64 | + ) |
| 65 | + del cache_kwargs_dict["region"] |
| 66 | + return cache_kwargs_dict |
| 67 | + |
| 68 | + @staticmethod |
| 69 | + def _set_cache_and_region(region: str, cache_kwargs: dict) -> None: |
| 70 | + """Sets ``JumpStartModelsAccessor._cache`` and ``JumpStartModelsAccessor._curr_region``. |
| 71 | +
|
| 72 | + Args: |
| 73 | + region (str): region for which to retrieve header/spec. |
| 74 | + cache_kwargs (dict): kwargs to pass to ``JumpStartModelsCache``. |
| 75 | + """ |
| 76 | + if JumpStartModelsAccessor._cache is None or region != JumpStartModelsAccessor._curr_region: |
| 77 | + JumpStartModelsAccessor._cache = cache.JumpStartModelsCache( |
| 78 | + region=region, **cache_kwargs |
| 79 | + ) |
| 80 | + JumpStartModelsAccessor._curr_region = region |
| 81 | + |
| 82 | + @staticmethod |
| 83 | + def get_model_header(region: str, model_id: str, version: str) -> JumpStartModelHeader: |
| 84 | + """Returns model header from JumpStart models cache. |
| 85 | +
|
| 86 | + Args: |
| 87 | + region (str): region for which to retrieve header. |
| 88 | + model_id (str): model id to retrieve. |
| 89 | + version (str): semantic version to retrieve for the model id. |
| 90 | + """ |
| 91 | + cache_kwargs = JumpStartModelsAccessor._validate_and_mutate_region_cache_kwargs( |
| 92 | + JumpStartModelsAccessor._cache_kwargs, region |
| 93 | + ) |
| 94 | + JumpStartModelsAccessor._set_cache_and_region(region, cache_kwargs) |
| 95 | + assert JumpStartModelsAccessor._cache is not None |
| 96 | + return JumpStartModelsAccessor._cache.get_header(model_id, version) |
| 97 | + |
| 98 | + @staticmethod |
| 99 | + def get_model_specs(region: str, model_id: str, version: str) -> JumpStartModelSpecs: |
| 100 | + """Returns model specs from JumpStart models cache. |
| 101 | +
|
| 102 | + Args: |
| 103 | + region (str): region for which to retrieve header. |
| 104 | + model_id (str): model id to retrieve. |
| 105 | + version (str): semantic version to retrieve for the model id. |
| 106 | + """ |
| 107 | + cache_kwargs = JumpStartModelsAccessor._validate_and_mutate_region_cache_kwargs( |
| 108 | + JumpStartModelsAccessor._cache_kwargs, region |
| 109 | + ) |
| 110 | + JumpStartModelsAccessor._set_cache_and_region(region, cache_kwargs) |
| 111 | + assert JumpStartModelsAccessor._cache is not None |
| 112 | + return JumpStartModelsAccessor._cache.get_specs(model_id, version) |
| 113 | + |
| 114 | + @staticmethod |
| 115 | + def set_cache_kwargs(cache_kwargs: Dict[str, Any], region: str = None) -> None: |
| 116 | + """Sets cache kwargs, clears the cache. |
| 117 | +
|
| 118 | + Raises: |
| 119 | + ValueError: If region in `cache_kwargs` is inconsistent with `region` argument. |
| 120 | +
|
| 121 | + Args: |
| 122 | + cache_kwargs (str): cache kwargs to validate. |
| 123 | + region (str): Optional. The region to validate along with the kwargs. |
| 124 | + """ |
| 125 | + cache_kwargs = JumpStartModelsAccessor._validate_and_mutate_region_cache_kwargs( |
| 126 | + cache_kwargs, region |
| 127 | + ) |
| 128 | + JumpStartModelsAccessor._cache_kwargs = cache_kwargs |
| 129 | + if region is None: |
| 130 | + JumpStartModelsAccessor._cache = cache.JumpStartModelsCache( |
| 131 | + **JumpStartModelsAccessor._cache_kwargs |
| 132 | + ) |
| 133 | + else: |
| 134 | + JumpStartModelsAccessor._curr_region = region |
| 135 | + JumpStartModelsAccessor._cache = cache.JumpStartModelsCache( |
| 136 | + region=region, **JumpStartModelsAccessor._cache_kwargs |
| 137 | + ) |
| 138 | + |
| 139 | + @staticmethod |
| 140 | + def reset_cache(cache_kwargs: Dict[str, Any] = None, region: Optional[str] = None) -> None: |
| 141 | + """Resets cache, optionally allowing cache kwargs to be passed to the new cache. |
| 142 | +
|
| 143 | + Raises: |
| 144 | + ValueError: If region in `cache_kwargs` is inconsistent with `region` argument. |
| 145 | +
|
| 146 | + Args: |
| 147 | + cache_kwargs (str): cache kwargs to validate. |
| 148 | + region (str): The region to validate along with the kwargs. |
| 149 | + """ |
| 150 | + cache_kwargs_dict = {} if cache_kwargs is None else cache_kwargs |
| 151 | + JumpStartModelsAccessor.set_cache_kwargs(cache_kwargs_dict, region) |
0 commit comments