forked from aws/sagemaker-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_local_model_trainer.py
More file actions
224 lines (187 loc) · 7.77 KB
/
test_local_model_trainer.py
File metadata and controls
224 lines (187 loc) · 7.77 KB
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
# 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 peCWDissions and limitations under the License.
"""This module contains code to test image builder with local mode"""
from __future__ import absolute_import
import os
import errno
import shutil
import tempfile
from tests.integ import DATA_DIR
import tests.integ.lock as lock
from sagemaker.modules.configs import Compute, InputData, SourceCode
from sagemaker.modules.distributed import Torchrun
from sagemaker.modules.train.model_trainer import Mode, ModelTrainer
import subprocess
DEFAULT_CPU_IMAGE = "763104351884.dkr.ecr.us-west-2.amazonaws.com/pytorch-training:2.0.0-cpu-py310"
CWD = os.getcwd()
SOURCE_DIR = os.path.join(DATA_DIR, "modules/local_script")
LOCK_PATH = os.path.join(tempfile.gettempdir(), "sagemaker_test_local_mode_lock")
def delete_local_path(path):
try:
if os.path.exists(path) and os.path.isdir(path):
shutil.rmtree(path)
print(f"Removed directory: {path}")
else:
print(f"Directory does not exist: {path}")
except OSError as exc:
# on Linux, when docker writes to any mounted volume, it uses the container's user. In most
# cases this is root. When the container exits and we try to delete them we can't because
# root owns those files. We expect this to happen, so we handle EACCESS. Any other error
# we will raise the exception up.
if exc.errno == errno.EACCES:
print(f"Failed to delete: {path} Please remove it manually.")
else:
print(f"Failed to delete: {path}")
raise
def test_single_container_local_mode_local_data(modules_sagemaker_session):
with lock.lock(LOCK_PATH):
try:
source_code = SourceCode(
source_dir=SOURCE_DIR,
entry_script="local_training_script.py",
)
compute = Compute(
instance_type="local_cpu",
instance_count=1,
)
train_data = InputData(
channel_name="train",
data_source=os.path.join(SOURCE_DIR, "data/train/"),
)
test_data = InputData(
channel_name="test",
data_source=os.path.join(SOURCE_DIR, "data/test/"),
)
model_trainer = ModelTrainer(
training_image=DEFAULT_CPU_IMAGE,
sagemaker_session=modules_sagemaker_session,
source_code=source_code,
compute=compute,
input_data_config=[train_data, test_data],
base_job_name="local_mode_single_container_local_data",
training_mode=Mode.LOCAL_CONTAINER,
)
model_trainer.train()
assert os.path.exists(os.path.join(CWD, "compressed_artifacts/model.tar.gz"))
finally:
subprocess.run(["docker", "compose", "down", "-v"])
directories = [
"compressed_artifacts",
"artifacts",
"model",
"output",
]
for directory in directories:
path = os.path.join(CWD, directory)
delete_local_path(path)
def test_single_container_local_mode_s3_data(modules_sagemaker_session):
with lock.lock(LOCK_PATH):
try:
# upload local data to s3
session = modules_sagemaker_session
bucket = session.default_bucket()
session.upload_data(
path=os.path.join(SOURCE_DIR, "data/train/"),
bucket=bucket,
key_prefix="data/train",
)
session.upload_data(
path=os.path.join(SOURCE_DIR, "data/test/"),
bucket=bucket,
key_prefix="data/test",
)
source_code = SourceCode(
source_dir=SOURCE_DIR,
entry_script="local_training_script.py",
)
compute = Compute(
instance_type="local_cpu",
instance_count=1,
)
# read input data from s3
train_data = InputData(channel_name="train", data_source=f"s3://{bucket}/data/train/")
test_data = InputData(channel_name="test", data_source=f"s3://{bucket}/data/test/")
model_trainer = ModelTrainer(
training_image=DEFAULT_CPU_IMAGE,
sagemaker_session=modules_sagemaker_session,
source_code=source_code,
compute=compute,
input_data_config=[train_data, test_data],
base_job_name="local_mode_single_container_s3_data",
training_mode=Mode.LOCAL_CONTAINER,
)
model_trainer.train()
assert os.path.exists(os.path.join(CWD, "compressed_artifacts/model.tar.gz"))
finally:
subprocess.run(["docker", "compose", "down", "-v"])
assert not os.path.exists(os.path.join(CWD, "shared"))
assert not os.path.exists(os.path.join(CWD, "input"))
assert not os.path.exists(os.path.join(CWD, "algo-1"))
directories = [
"compressed_artifacts",
"artifacts",
"model",
"output",
]
for directory in directories:
path = os.path.join(CWD, directory)
delete_local_path(path)
def test_multi_container_local_mode(modules_sagemaker_session):
with lock.lock(LOCK_PATH):
try:
source_code = SourceCode(
source_dir=SOURCE_DIR,
entry_script="local_training_script.py",
)
distributed = Torchrun(
process_count_per_node=1,
)
compute = Compute(
instance_type="local_cpu",
instance_count=2,
)
train_data = InputData(
channel_name="train",
data_source=os.path.join(SOURCE_DIR, "data/train/"),
)
test_data = InputData(
channel_name="test",
data_source=os.path.join(SOURCE_DIR, "data/test/"),
)
model_trainer = ModelTrainer(
training_image=DEFAULT_CPU_IMAGE,
sagemaker_session=modules_sagemaker_session,
source_code=source_code,
distributed=distributed,
compute=compute,
input_data_config=[train_data, test_data],
base_job_name="local_mode_multi_container",
training_mode=Mode.LOCAL_CONTAINER,
)
model_trainer.train()
assert os.path.exists(os.path.join(CWD, "compressed_artifacts/model.tar.gz"))
finally:
subprocess.run(["docker", "compose", "down", "-v"])
assert not os.path.exists(os.path.join(CWD, "shared"))
assert not os.path.exists(os.path.join(CWD, "input"))
assert not os.path.exists(os.path.join(CWD, "algo-1"))
assert not os.path.exists(os.path.join(CWD, "algo-2"))
directories = [
"compressed_artifacts",
"artifacts",
"model",
"output",
]
for directory in directories:
path = os.path.join(CWD, directory)
delete_local_path(path)