Skip to content

Commit 66d5fdf

Browse files
authored
change: improve logging and exception messages (#4877)
* chore: improve logging and exception messages * fix: flake8 * chore: address PR comments
1 parent 92e493c commit 66d5fdf

File tree

8 files changed

+76
-21
lines changed

8 files changed

+76
-21
lines changed

src/sagemaker/jumpstart/cache.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ def _model_id_retrieval_function(
293293
raise KeyError(error_msg)
294294

295295
error_msg = f"Unable to find model manifest for '{model_id}' with version '{version}'. "
296-
error_msg += f"Visit {MODEL_ID_LIST_WEB_URL} for updated list of models. "
296+
error_msg += "Specify a different model ID or try a different AWS Region. "
297+
error_msg += f"For a list of available models, see {MODEL_ID_LIST_WEB_URL}. "
297298

298299
other_model_id_version = None
299300
if model_type == JumpStartModelType.OPEN_WEIGHTS:

src/sagemaker/jumpstart/exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
)
3333

3434
INVALID_MODEL_ID_ERROR_MSG = (
35-
"Invalid model ID: '{model_id}'. Please visit "
36-
f"{MODEL_ID_LIST_WEB_URL} for a list of valid model IDs. "
35+
"Invalid model ID: '{model_id}'. Specify a different model ID or try a different AWS Region. "
36+
f"For a list of available models, see {MODEL_ID_LIST_WEB_URL}. "
3737
"The module `sagemaker.jumpstart.notebook_utils` contains utilities for "
3838
"fetching model IDs. We recommend upgrading to the latest version of sagemaker "
3939
"to get access to the most models."

src/sagemaker/jumpstart/utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@
5050
)
5151
from sagemaker.session import Session
5252
from sagemaker.config import load_sagemaker_config
53-
from sagemaker.utils import resolve_value_from_config, TagsDict, get_instance_rate_per_hour
53+
from sagemaker.utils import (
54+
resolve_value_from_config,
55+
TagsDict,
56+
get_instance_rate_per_hour,
57+
get_domain_for_region,
58+
)
5459
from sagemaker.workflow import is_pipeline_variable
5560
from sagemaker.user_agent import get_user_agent_extra_suffix
5661

@@ -553,7 +558,7 @@ def get_eula_message(model_specs: JumpStartModelSpecs, region: str) -> str:
553558
return (
554559
f"Model '{model_specs.model_id}' requires accepting end-user license agreement (EULA). "
555560
f"See https://{get_jumpstart_content_bucket(region=region)}.s3.{region}."
556-
f"amazonaws.com{'.cn' if region.startswith('cn-') else ''}"
561+
f"{get_domain_for_region(region)}"
557562
f"/{model_specs.hosting_eula_key} for terms of use."
558563
)
559564

src/sagemaker/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@
5252
from sagemaker.workflow import is_pipeline_variable, is_pipeline_parameter_string
5353
from sagemaker.workflow.entities import PipelineVariable
5454

55+
ALTERNATE_DOMAINS = {
56+
"cn-north-1": "amazonaws.com.cn",
57+
"cn-northwest-1": "amazonaws.com.cn",
58+
"us-iso-east-1": "c2s.ic.gov",
59+
"us-isob-east-1": "sc2s.sgov.gov",
60+
"us-isof-south-1": "csp.hci.ic.gov",
61+
"us-isof-east-1": "csp.hci.ic.gov",
62+
}
63+
5564
ECR_URI_PATTERN = r"^(\d+)(\.)dkr(\.)ecr(\.)(.+)(\.)(.*)(/)(.*:.*)$"
5665
MODEL_PACKAGE_ARN_PATTERN = (
5766
r"arn:aws([a-z\-]*)?:sagemaker:([a-z0-9\-]*):([0-9]{12}):model-package/(.*)"
@@ -1905,3 +1914,12 @@ def remove_tag_with_key(key: str, tags: Optional[Tags]) -> Optional[Tags]:
19051914
if len(updated_tags) == 1:
19061915
return updated_tags[0]
19071916
return updated_tags
1917+
1918+
1919+
def get_domain_for_region(region: str) -> str:
1920+
"""Returns the domain for the given region.
1921+
1922+
Args:
1923+
region (str): AWS region name.
1924+
"""
1925+
return ALTERNATE_DOMAINS.get(region, "amazonaws.com")

tests/unit/sagemaker/image_uris/expected_uris.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,8 @@
1212
# language governing permissions and limitations under the License.
1313
from __future__ import absolute_import
1414

15-
ALTERNATE_DOMAINS = {
16-
"cn-north-1": "amazonaws.com.cn",
17-
"cn-northwest-1": "amazonaws.com.cn",
18-
"us-iso-east-1": "c2s.ic.gov",
19-
"us-isob-east-1": "sc2s.sgov.gov",
20-
"us-isof-south-1": "csp.hci.ic.gov",
21-
"us-isof-east-1": "csp.hci.ic.gov",
22-
}
15+
from sagemaker.utils import ALTERNATE_DOMAINS
16+
2317
DOMAIN = "amazonaws.com"
2418
IMAGE_URI_FORMAT = "{}.dkr.ecr.{}.{}/{}:{}"
2519
MONITOR_URI_FORMAT = "{}.dkr.ecr.{}.{}/sagemaker-model-monitor-analyzer"

tests/unit/sagemaker/jumpstart/test_cache.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,26 +205,31 @@ def test_jumpstart_cache_get_header():
205205
)
206206
assert (
207207
"Unable to find model manifest for 'pytorch-ic-imagenet-inception-v3-classification-4' with "
208-
"version '3.*'. Visit https://sagemaker.readthedocs.io/en/stable/doc_utils/pretrainedmodels.html "
209-
"for updated list of models. Consider using model ID 'pytorch-ic-imagenet-inception-v3-"
208+
"version '3.*'. Specify a different model ID or try a different AWS Region. "
209+
"For a list of available models, see "
210+
"https://sagemaker.readthedocs.io/en/stable/doc_utils/pretrainedmodels.html. "
211+
"Consider using model ID "
212+
"'pytorch-ic-imagenet-inception-v3-"
210213
"classification-4' with version '2.0.0'."
211214
) in str(e.value)
212215

213216
with pytest.raises(KeyError) as e:
214217
cache.get_header(model_id="pytorch-ic-", semantic_version_str="*")
215218
assert (
216219
"Unable to find model manifest for 'pytorch-ic-' with version '*'. "
217-
"Visit https://sagemaker.readthedocs.io/en/stable/doc_utils/pretrainedmodels.html "
218-
"for updated list of models. "
220+
"Specify a different model ID or try a different AWS Region. "
221+
"For a list of available models, see "
222+
"https://sagemaker.readthedocs.io/en/stable/doc_utils/pretrainedmodels.html. "
219223
"Did you mean to use model ID 'pytorch-ic-imagenet-inception-v3-classification-4'?"
220224
) in str(e.value)
221225

222226
with pytest.raises(KeyError) as e:
223227
cache.get_header(model_id="tensorflow-ic-", semantic_version_str="*")
224228
assert (
225229
"Unable to find model manifest for 'tensorflow-ic-' with version '*'. "
226-
"Visit https://sagemaker.readthedocs.io/en/stable/doc_utils/pretrainedmodels.html "
227-
"for updated list of models. "
230+
"Specify a different model ID or try a different AWS Region. For a list "
231+
"of available models, see "
232+
"https://sagemaker.readthedocs.io/en/stable/doc_utils/pretrainedmodels.html. "
228233
"Did you mean to use model ID 'tensorflow-ic-imagenet-inception-"
229234
"v3-classification-4'?"
230235
) in str(e.value)
@@ -237,8 +242,9 @@ def test_jumpstart_cache_get_header():
237242
)
238243
assert (
239244
"Unable to find model manifest for 'ai21-summarize' with version '1.1.003'. "
240-
"Visit https://sagemaker.readthedocs.io/en/stable/doc_utils/pretrainedmodels.html "
241-
"for updated list of models. "
245+
"Specify a different model ID or try a different AWS Region. "
246+
"For a list of available models, see "
247+
"https://sagemaker.readthedocs.io/en/stable/doc_utils/pretrainedmodels.html. "
242248
"Did you mean to use model ID 'ai21-summarization'?"
243249
) in str(e.value)
244250

tests/unit/sagemaker/jumpstart/test_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,3 +2150,21 @@ def test_has_instance_rate_stat(stats, expected):
21502150
def test_deployment_config_response_data(data, expected):
21512151
out = utils.deployment_config_response_data(data)
21522152
assert out == expected
2153+
2154+
2155+
class TestGetEulaMessage(TestCase):
2156+
mock_model_specs = Mock(model_id="some-model-id", hosting_eula_key="some-eula-key")
2157+
2158+
def test_get_domain_for_region(self):
2159+
self.assertEqual(
2160+
utils.get_eula_message(self.mock_model_specs, "us-west-2"),
2161+
"Model 'some-model-id' requires accepting end-user license agreement (EULA). See"
2162+
" https://jumpstart-cache-prod-us-west-2.s3.us-west-2.amazonaws.com/some-eula-key "
2163+
"for terms of use.",
2164+
)
2165+
self.assertEqual(
2166+
utils.get_eula_message(self.mock_model_specs, "cn-north-1"),
2167+
"Model 'some-model-id' requires accepting end-user license agreement (EULA). See"
2168+
" https://jumpstart-cache-prod-cn-north-1.s3.cn-north-1.amazonaws.com.cn/some-eula-key "
2169+
"for terms of use.",
2170+
)

tests/unit/test_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
camel_case_to_pascal_case,
3939
deep_override_dict,
4040
flatten_dict,
41+
get_domain_for_region,
4142
get_instance_type_family,
4243
retry_with_backoff,
4344
check_and_get_run_experiment_config,
@@ -2231,3 +2232,15 @@ def test_remove_non_existent_tag(self):
22312232
def test_remove_only_tag(self):
22322233
original_tags = [{"Key": "Tag1", "Value": "Value1"}]
22332234
self.assertIsNone(remove_tag_with_key("Tag1", original_tags))
2235+
2236+
2237+
class TestGetDomainForRegion(TestCase):
2238+
def test_get_domain_for_region(self):
2239+
self.assertEqual(get_domain_for_region("us-west-2"), "amazonaws.com")
2240+
self.assertEqual(get_domain_for_region("eu-west-1"), "amazonaws.com")
2241+
self.assertEqual(get_domain_for_region("ap-northeast-1"), "amazonaws.com")
2242+
self.assertEqual(get_domain_for_region("us-gov-west-1"), "amazonaws.com")
2243+
self.assertEqual(get_domain_for_region("cn-northwest-1"), "amazonaws.com.cn")
2244+
self.assertEqual(get_domain_for_region("us-iso-east-1"), "c2s.ic.gov")
2245+
self.assertEqual(get_domain_for_region("us-isob-east-1"), "sc2s.sgov.gov")
2246+
self.assertEqual(get_domain_for_region("invalid-region"), "amazonaws.com")

0 commit comments

Comments
 (0)