File size: 4,952 Bytes
476455e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from __future__ import absolute_import
import copy
from typing import List
from sagemaker.jumpstart.cache import JumpStartModelsCache
from sagemaker.jumpstart.constants import JUMPSTART_DEFAULT_REGION_NAME, JUMPSTART_REGION_NAME_SET
from sagemaker.jumpstart.types import (
JumpStartCachedS3ContentKey,
JumpStartCachedS3ContentValue,
JumpStartModelSpecs,
JumpStartS3FileType,
JumpStartModelHeader,
)
from sagemaker.jumpstart.utils import get_formatted_manifest
from tests.unit.sagemaker.jumpstart.constants import (
PROTOTYPICAL_MODEL_SPECS_DICT,
BASE_MANIFEST,
BASE_SPEC,
BASE_HEADER,
)
def get_header_from_base_header(
_obj: JumpStartModelsCache = None,
region: str = None,
model_id: str = None,
semantic_version_str: str = None,
version: str = None,
) -> JumpStartModelHeader:
if version and semantic_version_str:
raise ValueError("Cannot specify both `version` and `semantic_version_str` fields.")
if all(
[
"pytorch" not in model_id,
"tensorflow" not in model_id,
"huggingface" not in model_id,
"mxnet" not in model_id,
"xgboost" not in model_id,
"catboost" not in model_id,
"lightgbm" not in model_id,
"sklearn" not in model_id,
]
):
raise KeyError("Bad model ID")
if region is not None and region not in JUMPSTART_REGION_NAME_SET:
raise ValueError(
f"Region name {region} not supported. Please use one of the supported regions in "
f"{JUMPSTART_REGION_NAME_SET}"
)
spec = copy.deepcopy(BASE_HEADER)
spec["version"] = version or semantic_version_str
spec["model_id"] = model_id
return JumpStartModelHeader(spec)
def get_prototype_manifest(
region: str = JUMPSTART_DEFAULT_REGION_NAME,
) -> List[JumpStartModelHeader]:
return [
get_header_from_base_header(region=region, model_id=model_id, version=version)
for model_id in PROTOTYPICAL_MODEL_SPECS_DICT.keys()
for version in ["1.0.0"]
]
def get_prototype_model_spec(
region: str = None, model_id: str = None, version: str = None
) -> JumpStartModelSpecs:
"""This function mocks cache accessor functions. For this mock,
we only retrieve model specs based on the model ID.
"""
specs = JumpStartModelSpecs(PROTOTYPICAL_MODEL_SPECS_DICT[model_id])
return specs
def get_spec_from_base_spec(
_obj: JumpStartModelsCache = None,
region: str = None,
model_id: str = None,
semantic_version_str: str = None,
version: str = None,
) -> JumpStartModelSpecs:
if version and semantic_version_str:
raise ValueError("Cannot specify both `version` and `semantic_version_str` fields.")
if all(
[
"pytorch" not in model_id,
"tensorflow" not in model_id,
"huggingface" not in model_id,
"mxnet" not in model_id,
"xgboost" not in model_id,
"catboost" not in model_id,
"lightgbm" not in model_id,
"sklearn" not in model_id,
]
):
raise KeyError("Bad model ID")
if region is not None and region not in JUMPSTART_REGION_NAME_SET:
raise ValueError(
f"Region name {region} not supported. Please use one of the supported regions in "
f"{JUMPSTART_REGION_NAME_SET}"
)
spec = copy.deepcopy(BASE_SPEC)
spec["version"] = version or semantic_version_str
spec["model_id"] = model_id
return JumpStartModelSpecs(spec)
def patched_retrieval_function(
_modelCacheObj: JumpStartModelsCache,
key: JumpStartCachedS3ContentKey,
value: JumpStartCachedS3ContentValue,
) -> JumpStartCachedS3ContentValue:
filetype, s3_key = key.file_type, key.s3_key
if filetype == JumpStartS3FileType.MANIFEST:
return JumpStartCachedS3ContentValue(
formatted_content=get_formatted_manifest(BASE_MANIFEST)
)
if filetype == JumpStartS3FileType.SPECS:
_, model_id, specs_version = s3_key.split("/")
version = specs_version.replace("specs_v", "").replace(".json", "")
return JumpStartCachedS3ContentValue(
formatted_content=get_spec_from_base_spec(model_id=model_id, version=version)
)
raise ValueError(f"Bad value for filetype: {filetype}")
|