Skip to content

Commit 4632611

Browse files
authored
change: use image_uris.retrieve() for ModelMonitor default image (#1765)
1 parent 0b3d286 commit 4632611

File tree

7 files changed

+101
-37
lines changed

7 files changed

+101
-37
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"scope": ["monitoring"],
3+
"versions": {
4+
"": {
5+
"registries": {
6+
"eu-north-1": "895015795356",
7+
"me-south-1": "607024016150",
8+
"ap-south-1": "126357580389",
9+
"us-east-2": "777275614652",
10+
"eu-west-1": "468650794304",
11+
"eu-central-1": "048819808253",
12+
"sa-east-1": "539772159869",
13+
"ap-east-1": "001633400207",
14+
"us-east-1": "156813124566",
15+
"ap-northeast-2": "709848358524",
16+
"eu-west-2": "749857270468",
17+
"eu-west-3": "680080141114",
18+
"ap-northeast-1": "574779866223",
19+
"us-west-2": "159807026194",
20+
"us-west-1": "890145073186",
21+
"ap-southeast-1": "245545462676",
22+
"ap-southeast-2": "563025443158",
23+
"ca-central-1": "536280801234",
24+
"cn-north-1": "453000072557",
25+
"cn-northwest-1": "453252182341"
26+
},
27+
"repository": "sagemaker-model-monitor-analyzer"
28+
}
29+
}
30+
}

src/sagemaker/image_uris.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
logger = logging.getLogger(__name__)
2424

25-
ECR_URI_TEMPLATE = "{registry}.dkr.{hostname}/{repository}:{tag}"
25+
ECR_URI_TEMPLATE = "{registry}.dkr.{hostname}/{repository}"
2626

2727

2828
def retrieve(
@@ -69,14 +69,17 @@ def retrieve(
6969
registry = _registry_from_region(region, version_config["registries"])
7070
hostname = utils._botocore_resolver().construct_endpoint("ecr", region)["hostname"]
7171

72+
repo = version_config["repository"]
73+
7274
processor = _processor(
7375
instance_type, config.get("processors") or version_config.get("processors")
7476
)
7577
tag = _format_tag(version_config.get("tag_prefix", version), processor, py_version)
7678

77-
repo = version_config["repository"]
79+
if tag:
80+
repo += ":{}".format(tag)
7881

79-
return ECR_URI_TEMPLATE.format(registry=registry, hostname=hostname, repository=repo, tag=tag)
82+
return ECR_URI_TEMPLATE.format(registry=registry, hostname=hostname, repository=repo)
8083

8184

8285
def _config_for_framework_and_scope(framework, image_scope, accelerator_type=None):

src/sagemaker/model_monitor/model_monitoring.py

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,16 @@
2626
from six.moves.urllib.parse import urlparse
2727
from botocore.exceptions import ClientError
2828

29+
from sagemaker import image_uris
2930
from sagemaker.exceptions import UnexpectedStatusException
3031
from sagemaker.model_monitor.monitoring_files import Constraints, ConstraintViolations, Statistics
3132
from sagemaker.network import NetworkConfig
3233
from sagemaker.processing import Processor, ProcessingInput, ProcessingJob, ProcessingOutput
3334
from sagemaker.s3 import S3Uploader
3435
from sagemaker.session import Session
35-
from sagemaker.utils import name_from_base, retries, get_ecr_image_uri_prefix
36-
37-
_DEFAULT_MONITOR_IMAGE_URI_WITH_PLACEHOLDERS = "{}/sagemaker-model-monitor-analyzer"
38-
39-
_DEFAULT_MONITOR_IMAGE_REGION_ACCOUNT_MAPPING = {
40-
"eu-north-1": "895015795356",
41-
"me-south-1": "607024016150",
42-
"ap-south-1": "126357580389",
43-
"us-east-2": "777275614652",
44-
"eu-west-1": "468650794304",
45-
"eu-central-1": "048819808253",
46-
"sa-east-1": "539772159869",
47-
"ap-east-1": "001633400207",
48-
"us-east-1": "156813124566",
49-
"ap-northeast-2": "709848358524",
50-
"eu-west-2": "749857270468",
51-
"eu-west-3": "680080141114",
52-
"ap-northeast-1": "574779866223",
53-
"us-west-2": "159807026194",
54-
"us-west-1": "890145073186",
55-
"ap-southeast-1": "245545462676",
56-
"ap-southeast-2": "563025443158",
57-
"ca-central-1": "536280801234",
58-
"cn-north-1": "453000072557",
59-
"cn-northwest-1": "453252182341",
60-
}
36+
from sagemaker.utils import name_from_base, retries
37+
38+
DEFAULT_REPOSITORY_NAME = "sagemaker-model-monitor-analyzer"
6139

6240
STATISTICS_JSON_DEFAULT_FILE_NAME = "statistics.json"
6341
CONSTRAINTS_JSON_DEFAULT_FILE_NAME = "constraints.json"
@@ -89,6 +67,8 @@
8967

9068
_LOGGER = logging.getLogger(__name__)
9169

70+
framework_name = "model-monitor"
71+
9272

9373
class ModelMonitor(object):
9474
"""Sets up Amazon SageMaker Monitoring Schedules and baseline suggestions. Use this class when
@@ -1787,9 +1767,7 @@ def _get_default_image_uri(region):
17871767
Returns:
17881768
str: The Default Model Monitoring image uri based on the region.
17891769
"""
1790-
return _DEFAULT_MONITOR_IMAGE_URI_WITH_PLACEHOLDERS.format(
1791-
get_ecr_image_uri_prefix(_DEFAULT_MONITOR_IMAGE_REGION_ACCOUNT_MAPPING[region], region)
1792-
)
1770+
return image_uris.retrieve(framework=framework_name, region=region)
17931771

17941772

17951773
class BaseliningJob(ProcessingJob):

src/sagemaker/predictor.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from sagemaker.utils import name_from_base
1919

2020
from sagemaker.model_monitor.model_monitoring import (
21-
_DEFAULT_MONITOR_IMAGE_URI_WITH_PLACEHOLDERS,
21+
DEFAULT_REPOSITORY_NAME,
2222
ModelMonitor,
2323
DefaultModelMonitor,
2424
)
@@ -357,10 +357,7 @@ def list_monitors(self):
357357
image_uri = schedule["MonitoringScheduleConfig"]["MonitoringJobDefinition"][
358358
"MonitoringAppSpecification"
359359
]["ImageUri"]
360-
index_after_placeholders = _DEFAULT_MONITOR_IMAGE_URI_WITH_PLACEHOLDERS.rfind("{}")
361-
if image_uri.endswith(
362-
_DEFAULT_MONITOR_IMAGE_URI_WITH_PLACEHOLDERS[index_after_placeholders + len("{}") :]
363-
):
360+
if image_uri.endswith(DEFAULT_REPOSITORY_NAME):
364361
monitors.append(
365362
DefaultModelMonitor.attach(
366363
monitor_schedule_name=schedule_name,

tests/unit/sagemaker/image_uris/expected_uris.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
}
2020
DOMAIN = "amazonaws.com"
2121
IMAGE_URI_FORMAT = "{}.dkr.ecr.{}.{}/{}:{}"
22+
MONITOR_URI_FORMAT = "{}.dkr.ecr.{}.{}/sagemaker-model-monitor-analyzer"
2223
REGION = "us-west-2"
2324

2425

@@ -34,3 +35,8 @@ def framework_uri(repo, fw_version, account, py_version=None, processor="cpu", r
3435
def algo_uri(algo, account, region, version=1):
3536
domain = ALTERNATE_DOMAINS.get(region, DOMAIN)
3637
return IMAGE_URI_FORMAT.format(account, region, domain, algo, version)
38+
39+
40+
def monitor_uri(account, region=REGION):
41+
domain = ALTERNATE_DOMAINS.get(region, DOMAIN)
42+
return MONITOR_URI_FORMAT.format(account, region, domain)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2020 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+
from __future__ import absolute_import
14+
15+
from sagemaker import image_uris
16+
from tests.unit.sagemaker.image_uris import expected_uris, regions
17+
18+
ACCOUNTS = {
19+
"eu-north-1": "895015795356",
20+
"me-south-1": "607024016150",
21+
"ap-south-1": "126357580389",
22+
"us-east-2": "777275614652",
23+
"eu-west-1": "468650794304",
24+
"eu-central-1": "048819808253",
25+
"sa-east-1": "539772159869",
26+
"ap-east-1": "001633400207",
27+
"us-east-1": "156813124566",
28+
"ap-northeast-2": "709848358524",
29+
"eu-west-2": "749857270468",
30+
"eu-west-3": "680080141114",
31+
"ap-northeast-1": "574779866223",
32+
"us-west-2": "159807026194",
33+
"us-west-1": "890145073186",
34+
"ap-southeast-1": "245545462676",
35+
"ap-southeast-2": "563025443158",
36+
"ca-central-1": "536280801234",
37+
"cn-north-1": "453000072557",
38+
"cn-northwest-1": "453252182341",
39+
}
40+
41+
42+
def test_model_monitor():
43+
for region in regions.regions():
44+
if region in ACCOUNTS.keys():
45+
uri = image_uris.retrieve("model-monitor", region=region)
46+
47+
expected = expected_uris.monitor_uri(ACCOUNTS[region], region)
48+
assert expected == uri

tests/unit/sagemaker/monitor/test_model_monitoring.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656

5757

5858
CUSTOM_IMAGE_URI = "012345678901.dkr.ecr.us-west-2.amazonaws.com/my-custom-image-uri"
59+
DEFAULT_IMAGE_URI = "159807026194.dkr.ecr.us-west-2.amazonaws.com/sagemaker-model-monitor-analyzer"
5960

6061
INTER_CONTAINER_ENCRYPTION_EXCEPTION_MSG = (
6162
"EnableInterContainerTrafficEncryption is not supported in Model Monitor. Please ensure that "
@@ -126,6 +127,7 @@ def test_default_model_monitor_suggest_baseline(sagemaker_session):
126127
assert my_default_monitor.sagemaker_session == sagemaker_session
127128
assert my_default_monitor.tags == TAGS
128129
assert my_default_monitor.network_config == NETWORK_CONFIG
130+
assert my_default_monitor.image_uri == DEFAULT_IMAGE_URI
129131

130132
assert BASE_JOB_NAME in my_default_monitor.latest_baselining_job_name
131133
assert my_default_monitor.latest_baselining_job_name != BASE_JOB_NAME

0 commit comments

Comments
 (0)