Skip to content

Commit 475e051

Browse files
authored
change: add EI support for TFS framework (#682)
* Add EI support for TFS. * Add integ test to canary, update tfs readme. * Resolve conflicts. * Remove outdated CHANGELOG item.
1 parent 627d7ed commit 475e051

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

src/sagemaker/fw_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
'Please add framework_version={} to your constructor to avoid this error.'
4242

4343
VALID_PY_VERSIONS = ['py2', 'py3']
44-
VALID_EIA_FRAMEWORKS = ['tensorflow', 'mxnet']
44+
VALID_EIA_FRAMEWORKS = ['tensorflow', 'tensorflow-serving', 'mxnet']
4545
VALID_ACCOUNTS_BY_REGION = {'us-gov-west-1': '246785580436',
4646
'us-iso-east-1': '744548109606'}
4747

src/sagemaker/tensorflow/deploying_tensorflow_serving.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ estimator object to create a SageMaker Endpoint:
3434
3535
The code block above deploys a SageMaker Endpoint with one instance of the type 'ml.c5.xlarge'.
3636

37-
As of now, only the Python-based TensorFlow serving endpoints support Elastic Inference. For more information, see `Deploying to Python-based Endpoints <https://github.com/aws/sagemaker-python-sdk/blob/master/src/sagemaker/tensorflow/deploying_python.rst#deploying-to-python-based-endpoints>`_.
38-
3937
What happens when deploy is called
4038
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4139

@@ -66,6 +64,16 @@ If you already have existing model artifacts in S3, you can skip training and de
6664
6765
predictor = model.deploy(initial_instance_count=1, instance_type='ml.c5.xlarge')
6866
67+
Python-based TensorFlow serving on SageMaker has support for `Elastic Inference <https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html>`__, which allows for inference acceleration to a hosted endpoint for a fraction of the cost of using a full GPU instance. In order to attach an Elastic Inference accelerator to your endpoint provide the accelerator type to accelerator_type to your deploy call.
68+
69+
.. code:: python
70+
71+
from sagemaker.tensorflow.serving import Model
72+
73+
model = Model(model_data='s3://mybucket/model.tar.gz', role='MySageMakerRole')
74+
75+
predictor = model.deploy(initial_instance_count=1, instance_type='ml.c5.xlarge', accelerator_type='ml.eia1.medium')
76+
6977
Making predictions against a SageMaker Endpoint
7078
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7179

tests/integ/test_tfs.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ def tfs_predictor(instance_type, sagemaker_session, tf_full_version):
4646
yield predictor
4747

4848

49+
@pytest.fixture(scope='module')
50+
def tfs_predictor_with_accelerator(sagemaker_session, tf_full_version):
51+
endpoint_name = sagemaker.utils.unique_name_from_base("sagemaker-tensorflow-serving")
52+
instance_type = 'ml.c4.large'
53+
accelerator_type = 'ml.eia1.medium'
54+
model_data = sagemaker_session.upload_data(path='tests/data/tensorflow-serving-test-model.tar.gz',
55+
key_prefix='tensorflow-serving/models')
56+
with tests.integ.timeout.timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session):
57+
model = Model(model_data=model_data, role='SageMakerRole',
58+
framework_version=tf_full_version,
59+
sagemaker_session=sagemaker_session)
60+
predictor = model.deploy(1, instance_type, endpoint_name=endpoint_name, accelerator_type=accelerator_type)
61+
yield predictor
62+
63+
4964
@pytest.mark.canary_quick
5065
def test_predict(tfs_predictor, instance_type): # pylint: disable=W0613
5166
input_data = {'instances': [1.0, 2.0, 5.0]}
@@ -55,6 +70,17 @@ def test_predict(tfs_predictor, instance_type): # pylint: disable=W0613
5570
assert expected_result == result
5671

5772

73+
@pytest.mark.skipif(tests.integ.test_region() not in tests.integ.EI_SUPPORTED_REGIONS,
74+
reason='EI is not supported in region {}'.format(tests.integ.test_region()))
75+
@pytest.mark.canary_quick
76+
def test_predict_with_accelerator(tfs_predictor_with_accelerator):
77+
input_data = {'instances': [1.0, 2.0, 5.0]}
78+
expected_result = {'predictions': [3.5, 4.0, 5.5]}
79+
80+
result = tfs_predictor_with_accelerator.predict(input_data)
81+
assert expected_result == result
82+
83+
5884
def test_predict_generic_json(tfs_predictor):
5985
input_data = [[1.0, 2.0, 5.0], [1.0, 2.0, 5.0]]
6086
expected_result = {'predictions': [[3.5, 4.0, 5.5], [3.5, 4.0, 5.5]]}

tests/unit/test_fw_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ def test_create_image_uri_gpu():
8686
assert image_uri == '23.dkr.ecr.mars-south-3.amazonaws.com/sagemaker-mlfw:1.0rc-gpu-py3'
8787

8888

89+
def test_create_image_uri_ei():
90+
image_uri = fw_utils.create_image_uri(MOCK_REGION, 'tensorflow-serving', 'ml.c4.large', '1.1.0',
91+
accelerator_type='ml.eia1.large', account='23')
92+
assert image_uri == '23.dkr.ecr.mars-south-3.amazonaws.com/sagemaker-tensorflow-serving-eia:1.1.0-cpu'
93+
94+
8995
def test_create_image_uri_default_account():
9096
image_uri = fw_utils.create_image_uri(MOCK_REGION, MOCK_FRAMEWORK, 'ml.p3.2xlarge', '1.0rc', 'py3')
9197
assert image_uri == '520713654638.dkr.ecr.mars-south-3.amazonaws.com/sagemaker-mlfw:1.0rc-gpu-py3'

tests/unit/test_tfs.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
CSV_CONTENT_TYPE = 'text/csv'
2626
INSTANCE_COUNT = 1
2727
INSTANCE_TYPE = 'ml.c4.4xlarge'
28-
ACCELERATOR_TYPE = 'ml.eia.medium'
28+
ACCELERATOR_TYPE = 'ml.eia1.medium'
2929
ROLE = 'Dummy'
3030
REGION = 'us-west-2'
3131
PREDICT_INPUT = {'instances': [1.0, 2.0, 5.0]}
@@ -79,8 +79,11 @@ def test_tfs_model(sagemaker_session, tf_version):
7979
def test_tfs_model_image_accelerator(sagemaker_session, tf_version):
8080
model = Model("s3://some/data.tar.gz", role=ROLE, framework_version=tf_version,
8181
sagemaker_session=sagemaker_session)
82-
with pytest.raises(ValueError):
83-
model.prepare_container_def(INSTANCE_TYPE, accelerator_type=ACCELERATOR_TYPE)
82+
cdef = model.prepare_container_def(INSTANCE_TYPE, accelerator_type=ACCELERATOR_TYPE)
83+
assert cdef['Image'].endswith('sagemaker-tensorflow-serving-eia:{}-cpu'.format(tf_version))
84+
85+
predictor = model.deploy(INSTANCE_COUNT, INSTANCE_TYPE)
86+
assert isinstance(predictor, Predictor)
8487

8588

8689
def test_tfs_model_with_log_level(sagemaker_session, tf_version):

0 commit comments

Comments
 (0)