File size: 6,258 Bytes
476455e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | # Copyright 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 os
import numpy
import pytest
from sagemaker.chainer.estimator import Chainer
from sagemaker.chainer.model import ChainerModel
from sagemaker.utils import unique_name_from_base
from tests.integ import DATA_DIR, TRAINING_DEFAULT_TIMEOUT_MINUTES
from tests.integ.timeout import timeout, timeout_and_delete_endpoint_by_name
@pytest.fixture(scope="module")
def chainer_local_training_job(
sagemaker_local_session, chainer_latest_version, chainer_latest_py_version
):
return _run_mnist_training_job(
sagemaker_local_session, "local", 1, chainer_latest_version, chainer_latest_py_version
)
@pytest.mark.local_mode
def test_distributed_cpu_training(
sagemaker_local_session, chainer_latest_version, chainer_latest_py_version
):
_run_mnist_training_job(
sagemaker_local_session, "local", 2, chainer_latest_version, chainer_latest_py_version
)
@pytest.mark.local_mode
def test_training_with_additional_hyperparameters(
sagemaker_local_session, chainer_latest_version, chainer_latest_py_version
):
script_path = os.path.join(DATA_DIR, "chainer_mnist", "mnist.py")
data_path = os.path.join(DATA_DIR, "chainer_mnist")
chainer = Chainer(
entry_point=script_path,
role="SageMakerRole",
instance_count=1,
instance_type="local",
framework_version=chainer_latest_version,
py_version=chainer_latest_py_version,
sagemaker_session=sagemaker_local_session,
hyperparameters={"epochs": 1},
use_mpi=True,
num_processes=2,
process_slots_per_host=2,
additional_mpi_options="-x NCCL_DEBUG=INFO",
)
train_input = "file://" + os.path.join(data_path, "train")
test_input = "file://" + os.path.join(data_path, "test")
chainer.fit({"train": train_input, "test": test_input})
def test_attach_deploy(
sagemaker_session, chainer_latest_version, chainer_latest_py_version, cpu_instance_type
):
with timeout(minutes=TRAINING_DEFAULT_TIMEOUT_MINUTES):
script_path = os.path.join(DATA_DIR, "chainer_mnist", "mnist.py")
data_path = os.path.join(DATA_DIR, "chainer_mnist")
chainer = Chainer(
entry_point=script_path,
role="SageMakerRole",
framework_version=chainer_latest_version,
py_version=chainer_latest_py_version,
instance_count=1,
instance_type=cpu_instance_type,
sagemaker_session=sagemaker_session,
hyperparameters={"epochs": 1},
)
train_input = sagemaker_session.upload_data(
path=os.path.join(data_path, "train"), key_prefix="integ-test-data/chainer_mnist/train"
)
test_input = sagemaker_session.upload_data(
path=os.path.join(data_path, "test"), key_prefix="integ-test-data/chainer_mnist/test"
)
job_name = unique_name_from_base("test-chainer-training")
chainer.fit({"train": train_input, "test": test_input}, wait=False, job_name=job_name)
endpoint_name = unique_name_from_base("test-chainer-attach-deploy")
with timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session):
estimator = Chainer.attach(
chainer.latest_training_job.name, sagemaker_session=sagemaker_session
)
predictor = estimator.deploy(1, cpu_instance_type, endpoint_name=endpoint_name)
_predict_and_assert(predictor)
@pytest.mark.local_mode
def test_deploy_model(
chainer_local_training_job,
sagemaker_local_session,
chainer_latest_version,
chainer_latest_py_version,
):
script_path = os.path.join(DATA_DIR, "chainer_mnist", "mnist.py")
model = ChainerModel(
chainer_local_training_job.model_data,
"SageMakerRole",
entry_point=script_path,
sagemaker_session=sagemaker_local_session,
framework_version=chainer_latest_version,
py_version=chainer_latest_py_version,
)
predictor = model.deploy(1, "local")
try:
_predict_and_assert(predictor)
finally:
predictor.delete_endpoint()
def _run_mnist_training_job(
sagemaker_session, instance_type, instance_count, chainer_version, py_version
):
script_path = (
os.path.join(DATA_DIR, "chainer_mnist", "mnist.py")
if instance_type == 1
else os.path.join(DATA_DIR, "chainer_mnist", "distributed_mnist.py")
)
data_path = os.path.join(DATA_DIR, "chainer_mnist")
chainer = Chainer(
entry_point=script_path,
role="SageMakerRole",
framework_version=chainer_version,
py_version=py_version,
instance_count=instance_count,
instance_type=instance_type,
sagemaker_session=sagemaker_session,
hyperparameters={"epochs": 1},
# test output_path without trailing slash
output_path="s3://{}".format(sagemaker_session.default_bucket()),
)
train_input = "file://" + os.path.join(data_path, "train")
test_input = "file://" + os.path.join(data_path, "test")
job_name = unique_name_from_base("test-chainer-training")
chainer.fit({"train": train_input, "test": test_input}, job_name=job_name)
return chainer
def _predict_and_assert(predictor):
batch_size = 100
data = numpy.zeros((batch_size, 784), dtype="float32")
output = predictor.predict(data)
assert len(output) == batch_size
data = numpy.zeros((batch_size, 1, 28, 28), dtype="float32")
output = predictor.predict(data)
assert len(output) == batch_size
data = numpy.zeros((batch_size, 28, 28), dtype="float32")
output = predictor.predict(data)
assert len(output) == batch_size
|