Skip to content

Commit 3942b81

Browse files
authored
Add SageMaker Elastic Inference test (#21)
1 parent 4df8226 commit 3942b81

File tree

5 files changed

+118
-4
lines changed

5 files changed

+118
-4
lines changed

README.rst

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Don't forget the period at the end of the command!
8888
Amazon Elastic Inference with MXNet in SageMaker
8989
------------------------------------------------
9090
`Amazon Elastic Inference <https://aws.amazon.com/machine-learning/elastic-inference/>`__ allows you to to attach
91-
low-cost GPU-powered acceleration to Amazon EC2 and Amazon SageMaker instances to reduce the cost running deep
91+
low-cost GPU-powered acceleration to Amazon EC2 and Amazon SageMaker instances to reduce the cost of running deep
9292
learning inference by up to 75%. Currently, Amazon Elastic Inference supports TensorFlow, Apache MXNet, and ONNX
9393
models, with more frameworks coming soon.
9494

@@ -158,7 +158,7 @@ You Docker image must also be built in order to run the tests against it.
158158

159159
Local integration tests use the following pytest arguments:
160160

161-
- ``docker-base-name``: the Docker image's repository. Defaults to 'preprod-mxnet'.
161+
- ``docker-base-name``: the Docker image's repository. Defaults to 'preprod-mxnet-serving'.
162162
- ``framework-version``: the MXNet version. Defaults to the latest supported version.
163163
- ``py-version``: the Python version. Defaults to '3'.
164164
- ``processor``: CPU or GPU. Defaults to 'cpu'.
@@ -183,6 +183,51 @@ To run local integration tests:
183183
--framework-version 1.4.0 \
184184
--processor cpu
185185

186+
SageMaker Integration Tests
187+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
188+
189+
SageMaker integration tests require your Docker image to be within an `Amazon ECR repository <https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ECS_Console_Repositories.html>`__.
190+
191+
SageMaker integration tests use the following pytest arguments:
192+
193+
- ``docker-base-name``: the Docker image's `ECR repository namespace <https://docs.aws.amazon.com/AmazonECR/latest/userguide/Repositories.html>`__.
194+
- ``framework-version``: the MXNet version. Defaults to the latest supported version.
195+
- ``py-version``: the Python version. Defaults to '3'.
196+
- ``processor``: CPU or GPU. Defaults to 'cpu'.
197+
- ``tag``: the Docker image's tag. Defaults to <mxnet_version>-<processor>-py<py-version>
198+
- ``aws-id``: your AWS account ID.
199+
- ``instance-type``: the specified `Amazon SageMaker Instance Type <https://aws.amazon.com/sagemaker/pricing/instance-types/>`__ that the tests will run on.
200+
Defaults to 'ml.c4.xlarge' for CPU and 'ml.p2.xlarge' for GPU.
201+
202+
To run SageMaker integration tests:
203+
204+
::
205+
206+
tox test/integration/sagmaker -- --aws-id <your_aws_id> \
207+
--docker-base-name <your_docker_image> \
208+
--instance-type <amazon_sagemaker_instance_type> \
209+
--tag <your_docker_image_tag> \
210+
211+
::
212+
213+
# Example
214+
tox test/integration/sagemaker -- --aws-id 12345678910 \
215+
--docker-base-name preprod-mxnet-serving \
216+
--instance-type ml.m4.xlarge \
217+
--tag 1.4.0-cpu-py3
218+
219+
If you want to run a SageMaker end to end test for your Elastic Inference container, you will need to provide an ``accelerator_type`` as an additional pytest argument.
220+
221+
The ``accelerator-type`` is your specified `Amazon Elastic Inference Accelerator <https://aws.amazon.com/sagemaker/pricing/instance-types/>`__ type that will be attached to your instance type.
222+
223+
::
224+
225+
# Example for running Elastic Inference SageMaker end to end test
226+
tox test/integration/sagemaker/test_elastic_inference.py -- --aws-id 12345678910 \
227+
--docker-base-name preprod-mxnet-serving \
228+
--instance-type ml.m4.xlarge \
229+
--accelerator-type ml.eia1.medium \
230+
--tag 1.0
186231

187232
Contributing
188233
------------
@@ -195,4 +240,4 @@ License
195240

196241
SageMaker MXNet Containers is licensed under the Apache 2.0 License.
197242
It is copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
198-
The license is available at: http://aws.amazon.com/apache2.0/
243+
The license is available at: http://aws.amazon.com/apache2.0/

test/integration/__init__.py

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

1717
RESOURCE_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'resources'))
18+
19+
# EI is currently only supported in the following regions
20+
# regions were derived from https://aws.amazon.com/machine-learning/elastic-inference/pricing/
21+
EI_SUPPORTED_REGIONS = ['us-east-1', 'us-east-2', 'us-west-2', 'eu-west-1', 'ap-northeast-1', 'ap-northeast-2']

test/integration/sagemaker/__init__.py

Whitespace-only changes.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License").
4+
# You may not use this file except in compliance with the License.
5+
# A copy of the License is located at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# or in the "license" file accompanying this file. This file is distributed
10+
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
# express or implied. See the License for the specific language governing
12+
# permissions and limitations under the License.
13+
from __future__ import absolute_import
14+
15+
import os
16+
17+
import pytest
18+
from sagemaker import utils
19+
from sagemaker.mxnet import MXNetModel
20+
21+
from test.integration import EI_SUPPORTED_REGIONS, RESOURCE_PATH
22+
from test.integration.sagemaker.timeout import timeout_and_delete_endpoint_by_name
23+
24+
DEFAULT_HANDLER_PATH = os.path.join(RESOURCE_PATH, 'default_handlers')
25+
MODEL_PATH = os.path.join(DEFAULT_HANDLER_PATH, 'model.tar.gz')
26+
SCRIPT_PATH = os.path.join(DEFAULT_HANDLER_PATH, 'model', 'code', 'empty_module.py')
27+
28+
29+
@pytest.fixture(autouse=True)
30+
def skip_if_no_accelerator(accelerator_type):
31+
if accelerator_type is None:
32+
pytest.skip('Skipping because accelerator type was not provided')
33+
34+
35+
@pytest.fixture(autouse=True)
36+
def skip_if_non_supported_ei_region(region):
37+
if region not in EI_SUPPORTED_REGIONS:
38+
pytest.skip('EI is not supported in {}'.format(region))
39+
40+
41+
@pytest.mark.skip_if_non_supported_ei_region
42+
@pytest.mark.skip_if_no_accelerator
43+
def test_elastic_inference(ecr_image, sagemaker_session, instance_type, accelerator_type, framework_version):
44+
endpoint_name = utils.unique_name_from_base('test-mxnet-ei')
45+
46+
with timeout_and_delete_endpoint_by_name(endpoint_name=endpoint_name,
47+
sagemaker_session=sagemaker_session,
48+
minutes=20):
49+
prefix = 'mxnet-serving/default-handlers'
50+
model_data = sagemaker_session.upload_data(path=MODEL_PATH, key_prefix=prefix)
51+
model = MXNetModel(model_data=model_data,
52+
entry_point=SCRIPT_PATH,
53+
role='SageMakerRole',
54+
image=ecr_image,
55+
framework_version=framework_version,
56+
sagemaker_session=sagemaker_session)
57+
58+
predictor = model.deploy(initial_instance_count=1,
59+
instance_type=instance_type,
60+
accelerator_type=accelerator_type,
61+
endpoint_name=endpoint_name)
62+
63+
output = predictor.predict([[1, 2]])
64+
assert [[4.9999918937683105]] == output

test/integration/sagemaker/test_hosting.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
SCRIPT_PATH = os.path.join(DEFAULT_HANDLER_PATH, 'model', 'code', 'empty_module.py')
2626

2727

28-
def test_hosting(sagemaker_session, ecr_image, instance_type):
28+
def test_hosting(sagemaker_session, ecr_image, instance_type, framework_version):
2929
prefix = 'mxnet-serving/default-handlers'
3030
model_data = sagemaker_session.upload_data(path=MODEL_PATH, key_prefix=prefix)
3131
model = MXNetModel(model_data,
3232
'SageMakerRole',
3333
SCRIPT_PATH,
3434
image=ecr_image,
35+
framework_version=framework_version,
3536
sagemaker_session=sagemaker_session)
3637

3738
endpoint_name = utils.unique_name_from_base('test-mxnet-serving')

0 commit comments

Comments
 (0)