|
| 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 json |
| 16 | +import os |
| 17 | +import tarfile |
| 18 | + |
| 19 | +import boto3 |
| 20 | +import pytest |
| 21 | +from six.moves.urllib.parse import urlparse |
| 22 | + |
| 23 | +import sagemaker.utils |
| 24 | +import tests.integ as integ |
| 25 | +from sagemaker.mxnet import MXNet |
| 26 | +from tests.integ import timeout |
| 27 | + |
| 28 | +horovod_dir = os.path.join(os.path.dirname(__file__), "..", "data", "horovod") |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.canary_quick |
| 32 | +def test_hvd_cpu(sagemaker_session, cpu_instance_type, tmpdir): |
| 33 | + _create_and_fit_estimator(sagemaker_session, cpu_instance_type, tmpdir) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.canary_quick |
| 37 | +@pytest.mark.skipif( |
| 38 | + integ.test_region() in integ.TRAINING_NO_P2_REGIONS, reason="no ml.p2 instances in this region" |
| 39 | +) |
| 40 | +def test_hvd_gpu(sagemaker_session, gpu_instance_type, tmpdir): |
| 41 | + _create_and_fit_estimator(sagemaker_session, gpu_instance_type, tmpdir) |
| 42 | + |
| 43 | + |
| 44 | +def read_json(file, tmp): |
| 45 | + with open(os.path.join(tmp, file)) as f: |
| 46 | + return json.load(f) |
| 47 | + |
| 48 | + |
| 49 | +def extract_files_from_s3(s3_url, tmpdir, sagemaker_session): |
| 50 | + parsed_url = urlparse(s3_url) |
| 51 | + s3 = boto3.resource("s3", region_name=sagemaker_session.boto_region_name) |
| 52 | + |
| 53 | + model = os.path.join(tmpdir, "model") |
| 54 | + s3.Bucket(parsed_url.netloc).download_file(parsed_url.path.lstrip("/"), model) |
| 55 | + |
| 56 | + with tarfile.open(model, "r") as tar_file: |
| 57 | + tar_file.extractall(tmpdir) |
| 58 | + |
| 59 | + |
| 60 | +def _create_and_fit_estimator(sagemaker_session, instance_type, tmpdir): |
| 61 | + job_name = sagemaker.utils.unique_name_from_base("mx-horovod") |
| 62 | + estimator = MXNet( |
| 63 | + entry_point=os.path.join(horovod_dir, "hvd_mnist_mxnet.py"), |
| 64 | + role="SageMakerRole", |
| 65 | + train_instance_count=2, |
| 66 | + train_instance_type=instance_type, |
| 67 | + sagemaker_session=sagemaker_session, |
| 68 | + py_version=integ.PYTHON_VERSION, |
| 69 | + framework_version="1.6.0", |
| 70 | + distributions={"mpi": {"enabled": True}}, |
| 71 | + ) |
| 72 | + |
| 73 | + with timeout.timeout(minutes=integ.TRAINING_DEFAULT_TIMEOUT_MINUTES): |
| 74 | + estimator.fit(job_name=job_name) |
| 75 | + |
| 76 | + tmp = str(tmpdir) |
| 77 | + extract_files_from_s3(estimator.model_data, tmp, sagemaker_session) |
| 78 | + |
| 79 | + for rank in range(2): |
| 80 | + assert read_json("rank-%s" % rank, tmp)["rank"] == rank |
0 commit comments