/
test_local_mode.py
420 lines (341 loc) · 14.6 KB
/
test_local_mode.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# Copyright 2017-2019 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 tarfile
import boto3
import numpy
import pytest
import tempfile
import stopit
import tests.integ.lock as lock
from tests.integ import DATA_DIR, PYTHON_VERSION
from sagemaker.local import LocalSession, LocalSagemakerRuntimeClient, LocalSagemakerClient
from sagemaker.mxnet import MXNet
from sagemaker.tensorflow import TensorFlow
# endpoint tests all use the same port, so we use this lock to prevent concurrent execution
LOCK_PATH = os.path.join(tempfile.gettempdir(), "sagemaker_test_local_mode_lock")
DATA_PATH = os.path.join(DATA_DIR, "iris", "data")
DEFAULT_REGION = "us-west-2"
class LocalNoS3Session(LocalSession):
"""
This Session sets local_code: True regardless of any config file settings
"""
def __init__(self):
super(LocalSession, self).__init__()
def _initialize(self, boto_session, sagemaker_client, sagemaker_runtime_client):
self.boto_session = boto3.Session(region_name=DEFAULT_REGION)
if self.config is None:
self.config = {"local": {"local_code": True, "region_name": DEFAULT_REGION}}
self._region_name = DEFAULT_REGION
self.sagemaker_client = LocalSagemakerClient(self)
self.sagemaker_runtime_client = LocalSagemakerRuntimeClient(self.config)
self.local_mode = True
@pytest.fixture(scope="module")
def mxnet_model(sagemaker_local_session, mxnet_full_version):
def _create_model(output_path):
script_path = os.path.join(DATA_DIR, "mxnet_mnist", "mnist.py")
data_path = os.path.join(DATA_DIR, "mxnet_mnist")
mx = MXNet(
entry_point=script_path,
role="SageMakerRole",
train_instance_count=1,
train_instance_type="local",
output_path=output_path,
framework_version=mxnet_full_version,
sagemaker_session=sagemaker_local_session,
)
train_input = mx.sagemaker_session.upload_data(
path=os.path.join(data_path, "train"), key_prefix="integ-test-data/mxnet_mnist/train"
)
test_input = mx.sagemaker_session.upload_data(
path=os.path.join(data_path, "test"), key_prefix="integ-test-data/mxnet_mnist/test"
)
mx.fit({"train": train_input, "test": test_input})
model = mx.create_model(1)
return model
return _create_model
@pytest.mark.local_mode
@pytest.mark.skipif(PYTHON_VERSION != "py2", reason="TensorFlow image supports only python 2.")
def test_tf_local_mode(sagemaker_local_session):
with stopit.ThreadingTimeout(5 * 60, swallow_exc=False):
script_path = os.path.join(DATA_DIR, "iris", "iris-dnn-classifier.py")
estimator = TensorFlow(
entry_point=script_path,
role="SageMakerRole",
framework_version="1.12",
training_steps=1,
evaluation_steps=1,
hyperparameters={"input_tensor_name": "inputs"},
train_instance_count=1,
train_instance_type="local",
base_job_name="test-tf",
sagemaker_session=sagemaker_local_session,
)
inputs = estimator.sagemaker_session.upload_data(
path=DATA_PATH, key_prefix="integ-test-data/tf_iris"
)
estimator.fit(inputs)
print("job succeeded: {}".format(estimator.latest_training_job.name))
endpoint_name = estimator.latest_training_job.name
with lock.lock(LOCK_PATH):
try:
json_predictor = estimator.deploy(
initial_instance_count=1, instance_type="local", endpoint_name=endpoint_name
)
features = [6.4, 3.2, 4.5, 1.5]
dict_result = json_predictor.predict({"inputs": features})
print("predict result: {}".format(dict_result))
list_result = json_predictor.predict(features)
print("predict result: {}".format(list_result))
assert dict_result == list_result
finally:
estimator.delete_endpoint()
@pytest.mark.local_mode
@pytest.mark.skipif(PYTHON_VERSION != "py2", reason="TensorFlow image supports only python 2.")
def test_tf_distributed_local_mode(sagemaker_local_session):
with stopit.ThreadingTimeout(5 * 60, swallow_exc=False):
script_path = os.path.join(DATA_DIR, "iris", "iris-dnn-classifier.py")
estimator = TensorFlow(
entry_point=script_path,
role="SageMakerRole",
framework_version="1.12",
training_steps=1,
evaluation_steps=1,
hyperparameters={"input_tensor_name": "inputs"},
train_instance_count=3,
train_instance_type="local",
base_job_name="test-tf",
sagemaker_session=sagemaker_local_session,
)
inputs = "file://" + DATA_PATH
estimator.fit(inputs)
print("job succeeded: {}".format(estimator.latest_training_job.name))
endpoint_name = estimator.latest_training_job.name
with lock.lock(LOCK_PATH):
try:
json_predictor = estimator.deploy(
initial_instance_count=1, instance_type="local", endpoint_name=endpoint_name
)
features = [6.4, 3.2, 4.5, 1.5]
dict_result = json_predictor.predict({"inputs": features})
print("predict result: {}".format(dict_result))
list_result = json_predictor.predict(features)
print("predict result: {}".format(list_result))
assert dict_result == list_result
finally:
estimator.delete_endpoint()
@pytest.mark.local_mode
@pytest.mark.skipif(PYTHON_VERSION != "py2", reason="TensorFlow image supports only python 2.")
def test_tf_local_data(sagemaker_local_session):
with stopit.ThreadingTimeout(5 * 60, swallow_exc=False):
script_path = os.path.join(DATA_DIR, "iris", "iris-dnn-classifier.py")
estimator = TensorFlow(
entry_point=script_path,
role="SageMakerRole",
framework_version="1.12",
training_steps=1,
evaluation_steps=1,
hyperparameters={"input_tensor_name": "inputs"},
train_instance_count=1,
train_instance_type="local",
base_job_name="test-tf",
sagemaker_session=sagemaker_local_session,
)
inputs = "file://" + DATA_PATH
estimator.fit(inputs)
print("job succeeded: {}".format(estimator.latest_training_job.name))
endpoint_name = estimator.latest_training_job.name
with lock.lock(LOCK_PATH):
try:
json_predictor = estimator.deploy(
initial_instance_count=1, instance_type="local", endpoint_name=endpoint_name
)
features = [6.4, 3.2, 4.5, 1.5]
dict_result = json_predictor.predict({"inputs": features})
print("predict result: {}".format(dict_result))
list_result = json_predictor.predict(features)
print("predict result: {}".format(list_result))
assert dict_result == list_result
finally:
estimator.delete_endpoint()
@pytest.mark.local_mode
@pytest.mark.skipif(PYTHON_VERSION != "py2", reason="TensorFlow image supports only python 2.")
def test_tf_local_data_local_script():
with stopit.ThreadingTimeout(5 * 60, swallow_exc=False):
script_path = os.path.join(DATA_DIR, "iris", "iris-dnn-classifier.py")
estimator = TensorFlow(
entry_point=script_path,
role="SageMakerRole",
framework_version="1.12",
training_steps=1,
evaluation_steps=1,
hyperparameters={"input_tensor_name": "inputs"},
train_instance_count=1,
train_instance_type="local",
base_job_name="test-tf",
sagemaker_session=LocalNoS3Session(),
)
inputs = "file://" + DATA_PATH
estimator.fit(inputs)
print("job succeeded: {}".format(estimator.latest_training_job.name))
endpoint_name = estimator.latest_training_job.name
with lock.lock(LOCK_PATH):
try:
json_predictor = estimator.deploy(
initial_instance_count=1, instance_type="local", endpoint_name=endpoint_name
)
features = [6.4, 3.2, 4.5, 1.5]
dict_result = json_predictor.predict({"inputs": features})
print("predict result: {}".format(dict_result))
list_result = json_predictor.predict(features)
print("predict result: {}".format(list_result))
assert dict_result == list_result
finally:
estimator.delete_endpoint()
@pytest.mark.local_mode
def test_local_mode_serving_from_s3_model(sagemaker_local_session, mxnet_model, mxnet_full_version):
path = "s3://%s" % sagemaker_local_session.default_bucket()
s3_model = mxnet_model(path)
s3_model.sagemaker_session = sagemaker_local_session
predictor = None
with lock.lock(LOCK_PATH):
try:
predictor = s3_model.deploy(initial_instance_count=1, instance_type="local")
data = numpy.zeros(shape=(1, 1, 28, 28))
predictor.predict(data)
finally:
if predictor:
predictor.delete_endpoint()
@pytest.mark.local_mode
def test_local_mode_serving_from_local_model(tmpdir, sagemaker_local_session, mxnet_model):
predictor = None
with lock.lock(LOCK_PATH):
try:
path = "file://%s" % (str(tmpdir))
model = mxnet_model(path)
model.sagemaker_session = sagemaker_local_session
predictor = model.deploy(initial_instance_count=1, instance_type="local")
data = numpy.zeros(shape=(1, 1, 28, 28))
predictor.predict(data)
finally:
if predictor:
predictor.delete_endpoint()
@pytest.mark.local_mode
def test_mxnet_local_mode(sagemaker_local_session, mxnet_full_version):
script_path = os.path.join(DATA_DIR, "mxnet_mnist", "mnist.py")
data_path = os.path.join(DATA_DIR, "mxnet_mnist")
mx = MXNet(
entry_point=script_path,
role="SageMakerRole",
py_version=PYTHON_VERSION,
train_instance_count=1,
train_instance_type="local",
sagemaker_session=sagemaker_local_session,
framework_version=mxnet_full_version,
)
train_input = mx.sagemaker_session.upload_data(
path=os.path.join(data_path, "train"), key_prefix="integ-test-data/mxnet_mnist/train"
)
test_input = mx.sagemaker_session.upload_data(
path=os.path.join(data_path, "test"), key_prefix="integ-test-data/mxnet_mnist/test"
)
mx.fit({"train": train_input, "test": test_input})
endpoint_name = mx.latest_training_job.name
with lock.lock(LOCK_PATH):
try:
predictor = mx.deploy(1, "local", endpoint_name=endpoint_name)
data = numpy.zeros(shape=(1, 1, 28, 28))
predictor.predict(data)
finally:
mx.delete_endpoint()
@pytest.mark.local_mode
def test_mxnet_local_data_local_script(mxnet_full_version):
data_path = os.path.join(DATA_DIR, "mxnet_mnist")
script_path = os.path.join(data_path, "mnist.py")
mx = MXNet(
entry_point=script_path,
role="SageMakerRole",
train_instance_count=1,
train_instance_type="local",
framework_version=mxnet_full_version,
sagemaker_session=LocalNoS3Session(),
)
train_input = "file://" + os.path.join(data_path, "train")
test_input = "file://" + os.path.join(data_path, "test")
mx.fit({"train": train_input, "test": test_input})
endpoint_name = mx.latest_training_job.name
with lock.lock(LOCK_PATH):
try:
predictor = mx.deploy(1, "local", endpoint_name=endpoint_name)
data = numpy.zeros(shape=(1, 1, 28, 28))
predictor.predict(data)
finally:
mx.delete_endpoint()
@pytest.mark.local_mode
def test_mxnet_training_failure(sagemaker_local_session, mxnet_full_version, tmpdir):
script_path = os.path.join(DATA_DIR, "mxnet_mnist", "failure_script.py")
mx = MXNet(
entry_point=script_path,
role="SageMakerRole",
framework_version=mxnet_full_version,
py_version=PYTHON_VERSION,
train_instance_count=1,
train_instance_type="local",
sagemaker_session=sagemaker_local_session,
output_path="file://{}".format(tmpdir),
)
with pytest.raises(RuntimeError):
mx.fit()
with tarfile.open(os.path.join(str(tmpdir), "output.tar.gz")) as tar:
tar.getmember("failure")
@pytest.mark.local_mode
def test_local_transform_mxnet(
sagemaker_local_session, tmpdir, mxnet_full_version, cpu_instance_type
):
data_path = os.path.join(DATA_DIR, "mxnet_mnist")
script_path = os.path.join(data_path, "mnist.py")
mx = MXNet(
entry_point=script_path,
role="SageMakerRole",
train_instance_count=1,
train_instance_type="local",
framework_version=mxnet_full_version,
sagemaker_session=sagemaker_local_session,
)
train_input = mx.sagemaker_session.upload_data(
path=os.path.join(data_path, "train"), key_prefix="integ-test-data/mxnet_mnist/train"
)
test_input = mx.sagemaker_session.upload_data(
path=os.path.join(data_path, "test"), key_prefix="integ-test-data/mxnet_mnist/test"
)
with stopit.ThreadingTimeout(5 * 60, swallow_exc=False):
mx.fit({"train": train_input, "test": test_input})
transform_input_path = os.path.join(data_path, "transform")
transform_input_key_prefix = "integ-test-data/mxnet_mnist/transform"
transform_input = mx.sagemaker_session.upload_data(
path=transform_input_path, key_prefix=transform_input_key_prefix
)
output_path = "file://%s" % (str(tmpdir))
transformer = mx.transformer(
1,
"local",
assemble_with="Line",
max_payload=1,
strategy="SingleRecord",
output_path=output_path,
)
with lock.lock(LOCK_PATH):
transformer.transform(transform_input, content_type="text/csv", split_type="Line")
transformer.wait()
assert os.path.exists(os.path.join(str(tmpdir), "data.csv.out"))