From 2781e2fc56aa758ca71c1925312449cae0f50aac Mon Sep 17 00:00:00 2001 From: Arjuna Keshavan Date: Wed, 29 Jan 2020 11:22:47 -0800 Subject: [PATCH 1/4] Update artifacts --- docker/1.2.0/py2/mms-entrypoint.py | 2 - docker/1.2.0/py3/mms-entrypoint.py | 2 - .../deep_learning_container.py | 113 ------------------ docker/build_artifacts/mms-entrypoint.py | 2 - 4 files changed, 119 deletions(-) delete mode 100644 docker/build_artifacts/deep_learning_container.py diff --git a/docker/1.2.0/py2/mms-entrypoint.py b/docker/1.2.0/py2/mms-entrypoint.py index a1c964dc..a179274c 100644 --- a/docker/1.2.0/py2/mms-entrypoint.py +++ b/docker/1.2.0/py2/mms-entrypoint.py @@ -19,8 +19,6 @@ from sagemaker_pytorch_serving_container import serving -if not os.path.exists("/opt/ml/input/config"): - subprocess.call(['python', '/usr/local/bin/deep_learning_container.py', '&>/dev/null', '&']) if sys.argv[1] == 'serve': serving.main() diff --git a/docker/1.2.0/py3/mms-entrypoint.py b/docker/1.2.0/py3/mms-entrypoint.py index a1c964dc..a179274c 100644 --- a/docker/1.2.0/py3/mms-entrypoint.py +++ b/docker/1.2.0/py3/mms-entrypoint.py @@ -19,8 +19,6 @@ from sagemaker_pytorch_serving_container import serving -if not os.path.exists("/opt/ml/input/config"): - subprocess.call(['python', '/usr/local/bin/deep_learning_container.py', '&>/dev/null', '&']) if sys.argv[1] == 'serve': serving.main() diff --git a/docker/build_artifacts/deep_learning_container.py b/docker/build_artifacts/deep_learning_container.py deleted file mode 100644 index 0089a823..00000000 --- a/docker/build_artifacts/deep_learning_container.py +++ /dev/null @@ -1,113 +0,0 @@ -# Copyright 2019-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"). You -# may not use this file except in compliance with the License. A copy of -# the License is located at -# -# http://aws.amazon.com/apache2.0/ -# -# or in the "license" file accompanying this file. This file is -# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF -# ANY KIND, either express or implied. See the License for the specific -# language governing permissions and limitations under the License. -from __future__ import absolute_import - -import re -import json -import logging -import requests - - -def _validate_instance_id(instance_id): - """ - Validate instance ID - """ - instance_id_regex = r'^(i-\S{17})' - compiled_regex = re.compile(instance_id_regex) - match = compiled_regex.match(instance_id) - - if not match: - return None - - return match.group(1) - - -def _retrieve_instance_id(): - """ - Retrieve instance ID from instance metadata service - """ - instance_id = None - url = "http://169.254.169.254/latest/meta-data/instance-id" - response = requests_helper(url, timeout=0.1) - - if response is not None: - instance_id = _validate_instance_id(response.text) - - return instance_id - - -def _retrieve_instance_region(): - """ - Retrieve instance region from instance metadata service - """ - region = None - valid_regions = ['ap-northeast-1', 'ap-northeast-2', 'ap-southeast-1', 'ap-southeast-2', - 'ap-south-1', 'ca-central-1', 'eu-central-1', 'eu-north-1', - 'eu-west-1', 'eu-west-2', 'eu-west-3', 'sa-east-1', - 'us-east-1', 'us-east-2', 'us-west-1', 'us-west-2'] - - url = "http://169.254.169.254/latest/dynamic/instance-identity/document" - response = requests_helper(url, timeout=0.1) - - if response is not None: - response_json = json.loads(response.text) - - if response_json['region'] in valid_regions: - region = response_json['region'] - - return region - - -def query_bucket(): - """ - GET request on an empty object from an Amazon S3 bucket - """ - response = None - instance_id = _retrieve_instance_id() - region = _retrieve_instance_region() - - if instance_id is not None and region is not None: - url = ( - "https://aws-deep-learning-containers-{0}.s3.{0}.amazonaws.com/" - "dlc-containers.txt?x-instance-id={1}" - ).format(region, instance_id) - response = requests_helper(url, timeout=0.2) - - logging.debug("Query bucket finished: {}".format(response)) - - return response - - -def requests_helper(url, timeout): - response = None - try: - response = requests.get(url, timeout=timeout) - except requests.exceptions.RequestException as e: - logging.error("Request exception: {}".format(e)) - - return response - - -def main(): - """ - Invoke bucket query - """ - # Logs are not necessary for normal run. Remove this line while debugging. - logging.getLogger().disabled = True - - logging.basicConfig(level=logging.ERROR) - query_bucket() - - -if __name__ == '__main__': - main() diff --git a/docker/build_artifacts/mms-entrypoint.py b/docker/build_artifacts/mms-entrypoint.py index 917951c3..74bffad8 100644 --- a/docker/build_artifacts/mms-entrypoint.py +++ b/docker/build_artifacts/mms-entrypoint.py @@ -17,8 +17,6 @@ import sys import os.path -if not os.path.exists("/opt/ml/input/config"): - subprocess.call(['python', '/usr/local/bin/deep_learning_container.py', '&>/dev/null', '&']) if sys.argv[1] == 'serve': from sagemaker_pytorch_serving_container import serving From 40f7402a9e6aea20c9b0ecc3a19c1c62124aa946 Mon Sep 17 00:00:00 2001 From: Arjuna Keshavan Date: Wed, 29 Jan 2020 11:38:03 -0800 Subject: [PATCH 2/4] flake8 fixes --- docker/1.2.0/py2/mms-entrypoint.py | 1 - docker/1.2.0/py3/mms-entrypoint.py | 1 - docker/build_artifacts/mms-entrypoint.py | 1 - 3 files changed, 3 deletions(-) diff --git a/docker/1.2.0/py2/mms-entrypoint.py b/docker/1.2.0/py2/mms-entrypoint.py index a179274c..0b24a416 100644 --- a/docker/1.2.0/py2/mms-entrypoint.py +++ b/docker/1.2.0/py2/mms-entrypoint.py @@ -15,7 +15,6 @@ import shlex import subprocess import sys -import os.path from sagemaker_pytorch_serving_container import serving diff --git a/docker/1.2.0/py3/mms-entrypoint.py b/docker/1.2.0/py3/mms-entrypoint.py index a179274c..0b24a416 100644 --- a/docker/1.2.0/py3/mms-entrypoint.py +++ b/docker/1.2.0/py3/mms-entrypoint.py @@ -15,7 +15,6 @@ import shlex import subprocess import sys -import os.path from sagemaker_pytorch_serving_container import serving diff --git a/docker/build_artifacts/mms-entrypoint.py b/docker/build_artifacts/mms-entrypoint.py index 74bffad8..40254f09 100644 --- a/docker/build_artifacts/mms-entrypoint.py +++ b/docker/build_artifacts/mms-entrypoint.py @@ -15,7 +15,6 @@ import shlex import subprocess import sys -import os.path if sys.argv[1] == 'serve': From 70b26faf6f1e7a3ed5a3e9b4afbe467ca9731209 Mon Sep 17 00:00:00 2001 From: Arjuna Keshavan Date: Wed, 29 Jan 2020 11:43:02 -0800 Subject: [PATCH 3/4] remove unit test --- docker/__init__.py | 0 docker/build_artifacts/__init__.py | 13 -- test/unit/test_deep_learning_containers.py | 148 --------------------- 3 files changed, 161 deletions(-) delete mode 100644 docker/__init__.py delete mode 100644 docker/build_artifacts/__init__.py delete mode 100644 test/unit/test_deep_learning_containers.py diff --git a/docker/__init__.py b/docker/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/docker/build_artifacts/__init__.py b/docker/build_artifacts/__init__.py deleted file mode 100644 index 199e66b9..00000000 --- a/docker/build_artifacts/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -# Copyright 2019-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"). You -# may not use this file except in compliance with the License. A copy of -# the License is located at -# -# http://aws.amazon.com/apache2.0/ -# -# or in the "license" file accompanying this file. This file is -# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF -# ANY KIND, either express or implied. See the License for the specific -# language governing permissions and limitations under the License. -from __future__ import absolute_import diff --git a/test/unit/test_deep_learning_containers.py b/test/unit/test_deep_learning_containers.py deleted file mode 100644 index 84dc7bf8..00000000 --- a/test/unit/test_deep_learning_containers.py +++ /dev/null @@ -1,148 +0,0 @@ -# Copyright 2019-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the 'License'). You -# may not use this file except in compliance with the License. A copy of -# the License is located at -# -# http://aws.amazon.com/apache2.0/ -# -# or in the 'license' file accompanying this file. This file is -# distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF -# ANY KIND, either express or implied. See the License for the specific -# language governing permissions and limitations under the License. -from __future__ import absolute_import - -import unittest -import requests - -import pytest - -from docker.build_artifacts import deep_learning_container as deep_learning_container_to_test - -DLC_S3_URI = ( - 'https://aws-deep-learning-containers-us-east-1.s3.us-east-1.amazonaws.com/' - 'dlc-containers.txt?x-instance-id=i-123t32e11s32t1231' -) - - -@pytest.fixture(name='fixture_valid_instance_id') -def fixture_valid_instance_id(requests_mock): - return requests_mock.get('http://169.254.169.254/latest/meta-data/instance-id', text='i-123t32e11s32t1231') - - -@pytest.fixture(name='fixture_invalid_instance_id') -def fixture_invalid_instance_id(requests_mock): - return requests_mock.get('http://169.254.169.254/latest/meta-data/instance-id', text='i-123') - - -@pytest.fixture(name='fixture_none_instance_id') -def fixture_none_instance_id(requests_mock): - return requests_mock.get('http://169.254.169.254/latest/meta-data/instance-id', text=None) - - -@pytest.fixture(name='fixture_invalid_region') -def fixture_invalid_region(requests_mock): - return requests_mock.get('http://169.254.169.254/latest/dynamic/instance-identity/document', - json={'region': 'test'}) - - -@pytest.fixture(name='fixture_valid_region') -def fixture_valid_region(requests_mock): - return requests_mock.get('http://169.254.169.254/latest/dynamic/instance-identity/document', - json={'region': 'us-east-1'}) - - -def test_retrieve_instance_id(fixture_valid_instance_id): - result = deep_learning_container_to_test._retrieve_instance_id() - assert 'i-123t32e11s32t1231' == result - - -def test_retrieve_none_instance_id(fixture_none_instance_id): - result = deep_learning_container_to_test._retrieve_instance_id() - assert result is None - - -def test_retrieve_invalid_instance_id(fixture_invalid_instance_id): - result = deep_learning_container_to_test._retrieve_instance_id() - assert result is None - - -def test_retrieve_invalid_region(fixture_invalid_region): - result = deep_learning_container_to_test._retrieve_instance_region() - assert result is None - - -def test_retrieve_valid_region(fixture_valid_region): - result = deep_learning_container_to_test._retrieve_instance_region() - assert 'us-east-1' == result - - -def test_query_bucket(requests_mock, fixture_valid_region, fixture_valid_instance_id): - fixture_valid_instance_id.return_value = 'i-123t32e11s32t1231' - fixture_valid_region.return_value = 'us-east-1' - requests_mock.get(DLC_S3_URI, text='Access Denied') - actual_response = deep_learning_container_to_test.query_bucket() - assert 'Access Denied' == actual_response.text - - -def test_query_bucket_region_none(fixture_invalid_region, fixture_valid_instance_id): - fixture_valid_instance_id.return_value = 'i-123t32e11s32t1231' - fixture_invalid_region.return_value = None - actual_response = deep_learning_container_to_test.query_bucket() - assert actual_response is None - - -def test_query_bucket_instance_id_none(requests_mock, fixture_valid_region, fixture_none_instance_id): - fixture_none_instance_id.return_value = None - fixture_valid_region.return_value = 'us-east-1' - actual_response = deep_learning_container_to_test.query_bucket() - assert actual_response is None - - -def test_query_bucket_instance_id_invalid(requests_mock, fixture_valid_region, fixture_invalid_instance_id): - fixture_invalid_instance_id.return_value = None - fixture_valid_region.return_value = 'us-east-1' - actual_response = deep_learning_container_to_test.query_bucket() - assert actual_response is None - - -def test_HTTP_error_on_S3(requests_mock, fixture_valid_region, fixture_valid_instance_id): - fixture_valid_instance_id.return_value = 'i-123t32e11s32t1231' - fixture_valid_region.return_value = 'us-east-1' - - requests_mock.get( - DLC_S3_URI, - exc=requests.exceptions.HTTPError) - requests_mock.side_effect = requests.exceptions.HTTPError - - with pytest.raises(requests.exceptions.HTTPError): - actual_response = requests.get(DLC_S3_URI) - assert actual_response is None - - -def test_connection_error_on_S3(requests_mock, fixture_valid_region, fixture_valid_instance_id): - fixture_valid_instance_id.return_value = 'i-123t32e11s32t1231' - fixture_valid_region.return_value = 'us-east-1' - - requests_mock.get( - DLC_S3_URI, - exc=requests.exceptions.ConnectionError) - - with pytest.raises(requests.exceptions.ConnectionError): - actual_response = requests.get(DLC_S3_URI) - assert actual_response is None - - -def test_timeout_error_on_S3(requests_mock, fixture_valid_region, fixture_valid_instance_id): - fixture_valid_instance_id.return_value = 'i-123t32e11s32t1231' - fixture_valid_region.return_value = 'us-east-1' - - requests_mock.get(DLC_S3_URI, exc=requests.Timeout) - - with pytest.raises(requests.exceptions.Timeout): - actual_response = requests.get(DLC_S3_URI) - assert actual_response is None - - -if __name__ == '__main__': - unittest.main() From e8adadb4c8039210fd3b318d24e3d7dc1b6ca8ba Mon Sep 17 00:00:00 2001 From: Arjuna Keshavan Date: Wed, 29 Jan 2020 12:08:53 -0800 Subject: [PATCH 4/4] Remove artifact reference from dockerfile --- docker/1.2.0/py2/Dockerfile.cpu | 4 +--- docker/1.2.0/py2/Dockerfile.gpu | 4 +--- docker/1.2.0/py3/Dockerfile.cpu | 4 +--- docker/1.2.0/py3/Dockerfile.gpu | 4 +--- docker/1.3.1/py2/Dockerfile.cpu | 4 +--- docker/1.3.1/py2/Dockerfile.gpu | 4 +--- docker/1.3.1/py3/Dockerfile.cpu | 4 +--- docker/1.3.1/py3/Dockerfile.gpu | 4 +--- 8 files changed, 8 insertions(+), 24 deletions(-) diff --git a/docker/1.2.0/py2/Dockerfile.cpu b/docker/1.2.0/py2/Dockerfile.cpu index e044bebe..346fd43e 100644 --- a/docker/1.2.0/py2/Dockerfile.cpu +++ b/docker/1.2.0/py2/Dockerfile.cpu @@ -62,10 +62,8 @@ RUN useradd -m model-server \ COPY docker/$PYTORCH_VERSION/py2/mms-entrypoint.py /usr/local/bin/dockerd-entrypoint.py COPY docker/$PYTORCH_VERSION/py2/config.properties /home/model-server -COPY src/sagemaker_pytorch_serving_container/deep_learning_container.py /usr/local/bin/deep_learning_container.py -RUN chmod +x /usr/local/bin/dockerd-entrypoint.py && \ - chmod +x /usr/local/bin/deep_learning_container.py +RUN chmod +x /usr/local/bin/dockerd-entrypoint.py COPY dist/sagemaker_pytorch_serving_container-1.2-py2.py3-none-any.whl /sagemaker_pytorch_serving_container-1.2-py2.py3-none-any.whl RUN pip install --no-cache-dir /sagemaker_pytorch_serving_container-1.2-py2.py3-none-any.whl && \ diff --git a/docker/1.2.0/py2/Dockerfile.gpu b/docker/1.2.0/py2/Dockerfile.gpu index 7072d1a0..165db15d 100644 --- a/docker/1.2.0/py2/Dockerfile.gpu +++ b/docker/1.2.0/py2/Dockerfile.gpu @@ -74,10 +74,8 @@ RUN useradd -m model-server \ COPY docker/$PYTORCH_VERSION/py2/mms-entrypoint.py /usr/local/bin/dockerd-entrypoint.py COPY docker/$PYTORCH_VERSION/py2/config.properties /home/model-server -COPY src/sagemaker_pytorch_serving_container/deep_learning_container.py /usr/local/bin/deep_learning_container.py -RUN chmod +x /usr/local/bin/dockerd-entrypoint.py && \ - chmod +x /usr/local/bin/deep_learning_container.py +RUN chmod +x /usr/local/bin/dockerd-entrypoint.py # Install OpenSSH for MPI to communicate between containers, Allow OpenSSH to talk to containers without asking for confirmation RUN apt-get install -y --no-install-recommends openssh-client openssh-server && \ diff --git a/docker/1.2.0/py3/Dockerfile.cpu b/docker/1.2.0/py3/Dockerfile.cpu index af176ead..fb172ce3 100644 --- a/docker/1.2.0/py3/Dockerfile.cpu +++ b/docker/1.2.0/py3/Dockerfile.cpu @@ -63,10 +63,8 @@ RUN useradd -m model-server \ COPY docker/$PYTORCH_VERSION/py3/mms-entrypoint.py /usr/local/bin/dockerd-entrypoint.py COPY docker/$PYTORCH_VERSION/py3/config.properties /home/model-server -COPY src/sagemaker_pytorch_serving_container/deep_learning_container.py /usr/local/bin/deep_learning_container.py -RUN chmod +x /usr/local/bin/dockerd-entrypoint.py && \ - chmod +x /usr/local/bin/deep_learning_container.py +RUN chmod +x /usr/local/bin/dockerd-entrypoint.py COPY dist/sagemaker_pytorch_serving_container-1.2-py2.py3-none-any.whl /sagemaker_pytorch_serving_container-1.2-py2.py3-none-any.whl RUN pip install --no-cache-dir /sagemaker_pytorch_serving_container-1.2-py2.py3-none-any.whl && \ diff --git a/docker/1.2.0/py3/Dockerfile.gpu b/docker/1.2.0/py3/Dockerfile.gpu index 060ef38a..79eb1d57 100644 --- a/docker/1.2.0/py3/Dockerfile.gpu +++ b/docker/1.2.0/py3/Dockerfile.gpu @@ -75,10 +75,8 @@ RUN useradd -m model-server \ COPY docker/$PYTORCH_VERSION/py3/mms-entrypoint.py /usr/local/bin/dockerd-entrypoint.py COPY docker/$PYTORCH_VERSION/py3/config.properties /home/model-server -COPY src/sagemaker_pytorch_serving_container/deep_learning_container.py /usr/local/bin/deep_learning_container.py -RUN chmod +x /usr/local/bin/dockerd-entrypoint.py && \ - chmod +x /usr/local/bin/deep_learning_container.py +RUN chmod +x /usr/local/bin/dockerd-entrypoint.py COPY dist/sagemaker_pytorch_serving_container-1.2-py2.py3-none-any.whl /sagemaker_pytorch_serving_container-1.2-py2.py3-none-any.whl RUN pip install --no-cache-dir /sagemaker_pytorch_serving_container-1.2-py2.py3-none-any.whl && \ diff --git a/docker/1.3.1/py2/Dockerfile.cpu b/docker/1.3.1/py2/Dockerfile.cpu index 99205e99..826de8de 100644 --- a/docker/1.3.1/py2/Dockerfile.cpu +++ b/docker/1.3.1/py2/Dockerfile.cpu @@ -74,10 +74,8 @@ RUN useradd -m model-server \ COPY mms-entrypoint.py /usr/local/bin/dockerd-entrypoint.py COPY config.properties /home/model-server -COPY deep_learning_container.py /usr/local/bin/deep_learning_container.py -RUN chmod +x /usr/local/bin/dockerd-entrypoint.py \ - && chmod +x /usr/local/bin/deep_learning_container.py +RUN chmod +x /usr/local/bin/dockerd-entrypoint.py COPY sagemaker_pytorch_serving_container-1.3-py2.py3-none-any.whl /sagemaker_pytorch_serving_container-1.3-py2.py3-none-any.whl RUN pip install --no-cache-dir \ diff --git a/docker/1.3.1/py2/Dockerfile.gpu b/docker/1.3.1/py2/Dockerfile.gpu index d0cc9af8..4546cca3 100644 --- a/docker/1.3.1/py2/Dockerfile.gpu +++ b/docker/1.3.1/py2/Dockerfile.gpu @@ -88,10 +88,8 @@ RUN useradd -m model-server \ COPY mms-entrypoint.py /usr/local/bin/dockerd-entrypoint.py COPY config.properties /home/model-server -COPY deep_learning_container.py /usr/local/bin/deep_learning_container.py -RUN chmod +x /usr/local/bin/dockerd-entrypoint.py \ - && chmod +x /usr/local/bin/deep_learning_container.py +RUN chmod +x /usr/local/bin/dockerd-entrypoint.py # Install OpenSSH for MPI to communicate between containers, Allow OpenSSH to talk to containers without asking for confirmation RUN apt-get install -y --no-install-recommends \ diff --git a/docker/1.3.1/py3/Dockerfile.cpu b/docker/1.3.1/py3/Dockerfile.cpu index f83accdc..d49b4abd 100644 --- a/docker/1.3.1/py3/Dockerfile.cpu +++ b/docker/1.3.1/py3/Dockerfile.cpu @@ -73,10 +73,8 @@ RUN useradd -m model-server \ COPY mms-entrypoint.py /usr/local/bin/dockerd-entrypoint.py COPY config.properties /home/model-server -COPY deep_learning_container.py /usr/local/bin/deep_learning_container.py -RUN chmod +x /usr/local/bin/dockerd-entrypoint.py \ - && chmod +x /usr/local/bin/deep_learning_container.py +RUN chmod +x /usr/local/bin/dockerd-entrypoint.py COPY sagemaker_pytorch_serving_container-1.3-py2.py3-none-any.whl /sagemaker_pytorch_serving_container-1.3-py2.py3-none-any.whl RUN pip install --no-cache-dir \ diff --git a/docker/1.3.1/py3/Dockerfile.gpu b/docker/1.3.1/py3/Dockerfile.gpu index d4882d32..a6281a4e 100644 --- a/docker/1.3.1/py3/Dockerfile.gpu +++ b/docker/1.3.1/py3/Dockerfile.gpu @@ -90,10 +90,8 @@ RUN useradd -m model-server \ COPY mms-entrypoint.py /usr/local/bin/dockerd-entrypoint.py COPY config.properties /home/model-server -COPY deep_learning_container.py /usr/local/bin/deep_learning_container.py -RUN chmod +x /usr/local/bin/dockerd-entrypoint.py \ - && chmod +x /usr/local/bin/deep_learning_container.py +RUN chmod +x /usr/local/bin/dockerd-entrypoint.py COPY sagemaker_pytorch_serving_container-1.3-py2.py3-none-any.whl /sagemaker_pytorch_serving_container-1.3-py2.py3-none-any.whl RUN pip install --no-cache-dir \