Skip to content

Commit cca0d40

Browse files
authored
infra: move Neo unit tests to a new file and directly use the Model class (#1420)
1 parent d753109 commit cca0d40

File tree

3 files changed

+180
-170
lines changed

3 files changed

+180
-170
lines changed

tests/unit/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,27 @@
1515
import os
1616

1717
DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data")
18+
19+
NEO_REGION_LIST = [
20+
"us-west-1",
21+
"us-west-2",
22+
"us-east-1",
23+
"us-east-2",
24+
"eu-west-1",
25+
"eu-west-2",
26+
"eu-west-3",
27+
"eu-central-1",
28+
"eu-north-1",
29+
"ap-northeast-1",
30+
"ap-northeast-2",
31+
"ap-east-1",
32+
"ap-south-1",
33+
"ap-southeast-1",
34+
"ap-southeast-2",
35+
"sa-east-1",
36+
"ca-central-1",
37+
"me-south-1",
38+
"cn-north-1",
39+
"cn-northwest-1",
40+
"us-gov-west-1",
41+
]

tests/unit/sagemaker/model/test_framework_model.py

Lines changed: 0 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
ACCELERATOR_TYPE = "ml.eia.medium"
3737
IMAGE_NAME = "fakeimage"
3838
REGION = "us-west-2"
39-
NEO_REGION_ACCOUNT = "301217895009"
4039
MODEL_NAME = "{}-{}".format(MODEL_IMAGE, TIMESTAMP)
4140
GIT_REPO = "https://github.com/aws/sagemaker-python-sdk.git"
4241
BRANCH = "test-branch-git-config"
@@ -50,11 +49,6 @@
5049
CODECOMMIT_BRANCH = "master"
5150
REPO_DIR = "/tmp/repo_dir"
5251

53-
DESCRIBE_COMPILATION_JOB_RESPONSE = {
54-
"CompilationJobStatus": "Completed",
55-
"ModelArtifacts": {"S3ModelArtifacts": "s3://output-path/model.tar.gz"},
56-
}
57-
5852

5953
class DummyFrameworkModel(FrameworkModel):
6054
def __init__(self, sagemaker_session, **kwargs):
@@ -237,170 +231,6 @@ def test_deploy_update_endpoint_optional_args(sagemaker_session, tmpdir):
237231
sagemaker_session.create_endpoint.assert_not_called()
238232

239233

240-
def test_compile_model_for_inferentia(sagemaker_session, tmpdir):
241-
sagemaker_session.wait_for_compilation_job = Mock(
242-
return_value=DESCRIBE_COMPILATION_JOB_RESPONSE
243-
)
244-
model = DummyFrameworkModel(sagemaker_session, source_dir=str(tmpdir))
245-
model.compile(
246-
target_instance_family="ml_inf",
247-
input_shape={"data": [1, 3, 1024, 1024]},
248-
output_path="s3://output",
249-
role="role",
250-
framework="tensorflow",
251-
framework_version="1.15.0",
252-
job_name="compile-model",
253-
)
254-
assert (
255-
"{}.dkr.ecr.{}.amazonaws.com/sagemaker-neo-tensorflow:1.15.0-inf-py3".format(
256-
NEO_REGION_ACCOUNT, REGION
257-
)
258-
== model.image
259-
)
260-
assert model._is_compiled_model is True
261-
262-
263-
def test_compile_model_for_edge_device(sagemaker_session, tmpdir):
264-
sagemaker_session.wait_for_compilation_job = Mock(
265-
return_value=DESCRIBE_COMPILATION_JOB_RESPONSE
266-
)
267-
model = DummyFrameworkModel(sagemaker_session, source_dir=str(tmpdir))
268-
model.compile(
269-
target_instance_family="deeplens",
270-
input_shape={"data": [1, 3, 1024, 1024]},
271-
output_path="s3://output",
272-
role="role",
273-
framework="tensorflow",
274-
job_name="compile-model",
275-
)
276-
assert model._is_compiled_model is False
277-
278-
279-
def test_compile_model_for_edge_device_tflite(sagemaker_session, tmpdir):
280-
sagemaker_session.wait_for_compilation_job = Mock(
281-
return_value=DESCRIBE_COMPILATION_JOB_RESPONSE
282-
)
283-
model = DummyFrameworkModel(sagemaker_session, source_dir=str(tmpdir))
284-
model.compile(
285-
target_instance_family="deeplens",
286-
input_shape={"data": [1, 3, 1024, 1024]},
287-
output_path="s3://output",
288-
role="role",
289-
framework="tflite",
290-
job_name="tflite-compile-model",
291-
)
292-
assert model._is_compiled_model is False
293-
294-
295-
def test_compile_model_for_cloud(sagemaker_session, tmpdir):
296-
sagemaker_session.wait_for_compilation_job = Mock(
297-
return_value=DESCRIBE_COMPILATION_JOB_RESPONSE
298-
)
299-
model = DummyFrameworkModel(sagemaker_session, source_dir=str(tmpdir))
300-
model.compile(
301-
target_instance_family="ml_c4",
302-
input_shape={"data": [1, 3, 1024, 1024]},
303-
output_path="s3://output",
304-
role="role",
305-
framework="tensorflow",
306-
job_name="compile-model",
307-
)
308-
assert model._is_compiled_model is True
309-
310-
311-
def test_compile_model_for_cloud_tflite(sagemaker_session, tmpdir):
312-
sagemaker_session.wait_for_compilation_job = Mock(
313-
return_value=DESCRIBE_COMPILATION_JOB_RESPONSE
314-
)
315-
model = DummyFrameworkModel(sagemaker_session, source_dir=str(tmpdir))
316-
model.compile(
317-
target_instance_family="ml_c4",
318-
input_shape={"data": [1, 3, 1024, 1024]},
319-
output_path="s3://output",
320-
role="role",
321-
framework="tflite",
322-
job_name="tflite-compile-model",
323-
)
324-
assert model._is_compiled_model is True
325-
326-
327-
@patch("sagemaker.session.Session")
328-
@patch("sagemaker.fw_utils.tar_and_upload_dir", MagicMock())
329-
def test_compile_creates_session(session):
330-
session.return_value.boto_region_name = "us-west-2"
331-
332-
model = DummyFrameworkModel(sagemaker_session=None)
333-
model.compile(
334-
target_instance_family="ml_c4",
335-
input_shape={"data": [1, 3, 1024, 1024]},
336-
output_path="s3://output",
337-
role="role",
338-
framework="tensorflow",
339-
job_name="compile-model",
340-
)
341-
342-
assert model.sagemaker_session == session.return_value
343-
344-
345-
def test_check_neo_region(sagemaker_session, tmpdir):
346-
sagemaker_session.wait_for_compilation_job = Mock(
347-
return_value=DESCRIBE_COMPILATION_JOB_RESPONSE
348-
)
349-
model = DummyFrameworkModel(sagemaker_session, source_dir=str(tmpdir))
350-
ec2_region_list = [
351-
"us-east-2",
352-
"us-east-1",
353-
"us-west-1",
354-
"us-west-2",
355-
"ap-east-1",
356-
"ap-south-1",
357-
"ap-northeast-3",
358-
"ap-northeast-2",
359-
"ap-southeast-1",
360-
"ap-southeast-2",
361-
"ap-northeast-1",
362-
"ca-central-1",
363-
"cn-north-1",
364-
"cn-northwest-1",
365-
"eu-central-1",
366-
"eu-west-1",
367-
"eu-west-2",
368-
"eu-west-3",
369-
"eu-north-1",
370-
"sa-east-1",
371-
"us-gov-east-1",
372-
"us-gov-west-1",
373-
]
374-
neo_support_region = [
375-
"us-west-1",
376-
"us-west-2",
377-
"us-east-1",
378-
"us-east-2",
379-
"eu-west-1",
380-
"eu-west-2",
381-
"eu-west-3",
382-
"eu-central-1",
383-
"eu-north-1",
384-
"ap-northeast-1",
385-
"ap-northeast-2",
386-
"ap-east-1",
387-
"ap-south-1",
388-
"ap-southeast-1",
389-
"ap-southeast-2",
390-
"sa-east-1",
391-
"ca-central-1",
392-
"me-south-1",
393-
"cn-north-1",
394-
"cn-northwest-1",
395-
"us-gov-west-1",
396-
]
397-
for region_name in ec2_region_list:
398-
if region_name in neo_support_region:
399-
assert model.check_neo_region(region_name) is True
400-
else:
401-
assert model.check_neo_region(region_name) is False
402-
403-
404234
@patch("sagemaker.git_utils.git_clone_repo")
405235
@patch("sagemaker.model.fw_utils.tar_and_upload_dir")
406236
def test_git_support_succeed(tar_and_upload_dir, git_clone_repo, sagemaker_session):
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# Copyright 2017-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+
import boto3
16+
import pytest
17+
from mock import Mock, patch
18+
19+
from sagemaker.model import Model
20+
from tests.unit import NEO_REGION_LIST
21+
22+
MODEL_DATA = "s3://bucket/model.tar.gz"
23+
MODEL_IMAGE = "mi"
24+
25+
REGION = "us-west-2"
26+
27+
NEO_REGION_ACCOUNT = "301217895009"
28+
DESCRIBE_COMPILATION_JOB_RESPONSE = {
29+
"CompilationJobStatus": "Completed",
30+
"ModelArtifacts": {"S3ModelArtifacts": "s3://output-path/model.tar.gz"},
31+
}
32+
33+
34+
@pytest.fixture
35+
def sagemaker_session():
36+
return Mock(boto_region_name=REGION)
37+
38+
39+
def _create_model(sagemaker_session=None):
40+
return Model(MODEL_DATA, MODEL_IMAGE, sagemaker_session=sagemaker_session)
41+
42+
43+
def test_compile_model_for_inferentia(sagemaker_session):
44+
sagemaker_session.wait_for_compilation_job = Mock(
45+
return_value=DESCRIBE_COMPILATION_JOB_RESPONSE
46+
)
47+
model = _create_model(sagemaker_session)
48+
model.compile(
49+
target_instance_family="ml_inf",
50+
input_shape={"data": [1, 3, 1024, 1024]},
51+
output_path="s3://output",
52+
role="role",
53+
framework="tensorflow",
54+
framework_version="1.15.0",
55+
job_name="compile-model",
56+
)
57+
assert (
58+
"{}.dkr.ecr.{}.amazonaws.com/sagemaker-neo-tensorflow:1.15.0-inf-py3".format(
59+
NEO_REGION_ACCOUNT, REGION
60+
)
61+
== model.image
62+
)
63+
assert model._is_compiled_model is True
64+
65+
66+
def test_compile_model_for_edge_device(sagemaker_session):
67+
sagemaker_session.wait_for_compilation_job = Mock(
68+
return_value=DESCRIBE_COMPILATION_JOB_RESPONSE
69+
)
70+
model = _create_model(sagemaker_session)
71+
model.compile(
72+
target_instance_family="deeplens",
73+
input_shape={"data": [1, 3, 1024, 1024]},
74+
output_path="s3://output",
75+
role="role",
76+
framework="tensorflow",
77+
job_name="compile-model",
78+
)
79+
assert model._is_compiled_model is False
80+
81+
82+
def test_compile_model_for_edge_device_tflite(sagemaker_session):
83+
sagemaker_session.wait_for_compilation_job = Mock(
84+
return_value=DESCRIBE_COMPILATION_JOB_RESPONSE
85+
)
86+
model = _create_model(sagemaker_session)
87+
model.compile(
88+
target_instance_family="deeplens",
89+
input_shape={"data": [1, 3, 1024, 1024]},
90+
output_path="s3://output",
91+
role="role",
92+
framework="tflite",
93+
job_name="tflite-compile-model",
94+
)
95+
assert model._is_compiled_model is False
96+
97+
98+
def test_compile_model_for_cloud(sagemaker_session):
99+
sagemaker_session.wait_for_compilation_job = Mock(
100+
return_value=DESCRIBE_COMPILATION_JOB_RESPONSE
101+
)
102+
model = _create_model(sagemaker_session)
103+
model.compile(
104+
target_instance_family="ml_c4",
105+
input_shape={"data": [1, 3, 1024, 1024]},
106+
output_path="s3://output",
107+
role="role",
108+
framework="tensorflow",
109+
job_name="compile-model",
110+
)
111+
assert model._is_compiled_model is True
112+
113+
114+
def test_compile_model_for_cloud_tflite(sagemaker_session):
115+
sagemaker_session.wait_for_compilation_job = Mock(
116+
return_value=DESCRIBE_COMPILATION_JOB_RESPONSE
117+
)
118+
model = _create_model(sagemaker_session)
119+
model.compile(
120+
target_instance_family="ml_c4",
121+
input_shape={"data": [1, 3, 1024, 1024]},
122+
output_path="s3://output",
123+
role="role",
124+
framework="tflite",
125+
job_name="tflite-compile-model",
126+
)
127+
assert model._is_compiled_model is True
128+
129+
130+
@patch("sagemaker.session.Session")
131+
def test_compile_creates_session(session):
132+
session.return_value.boto_region_name = REGION
133+
134+
model = _create_model()
135+
model.compile(
136+
target_instance_family="ml_c4",
137+
input_shape={"data": [1, 3, 1024, 1024]},
138+
output_path="s3://output",
139+
role="role",
140+
framework="tensorflow",
141+
job_name="compile-model",
142+
)
143+
144+
assert session.return_value == model.sagemaker_session
145+
146+
147+
def test_check_neo_region(sagemaker_session):
148+
sagemaker_session.wait_for_compilation_job = Mock(
149+
return_value=DESCRIBE_COMPILATION_JOB_RESPONSE
150+
)
151+
model = _create_model(sagemaker_session)
152+
153+
boto_session = boto3.Session()
154+
for partition in boto_session.get_available_partitions():
155+
for region_name in boto_session.get_available_regions("ec2", partition_name=partition):
156+
assert (region_name in NEO_REGION_LIST) is model.check_neo_region(region_name)

0 commit comments

Comments
 (0)