-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathimage.py
1248 lines (1023 loc) · 46.1 KB
/
image.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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.
"""Placeholder docstring"""
from __future__ import absolute_import, annotations
import base64
import copy
import errno
import json
import logging
import os
import platform
import random
import re
import shlex
import shutil
import string
import subprocess
import sys
import tarfile
import tempfile
from threading import Thread
from typing import Dict, List
from six.moves.urllib.parse import urlparse
import sagemaker
from sagemaker.config.config_schema import CONTAINER_CONFIG, LOCAL
import sagemaker.local.data
import sagemaker.local.utils
import sagemaker.utils
from sagemaker.utils import custom_extractall_tarfile
CONTAINER_PREFIX = "algo"
STUDIO_HOST_NAME = "sagemaker-local"
DOCKER_COMPOSE_FILENAME = "docker-compose.yaml"
DOCKER_COMPOSE_HTTP_TIMEOUT_ENV = "COMPOSE_HTTP_TIMEOUT"
DOCKER_COMPOSE_HTTP_TIMEOUT = "120"
# Environment variables to be set during training
REGION_ENV_NAME = "AWS_REGION"
TRAINING_JOB_NAME_ENV_NAME = "TRAINING_JOB_NAME"
S3_ENDPOINT_URL_ENV_NAME = "S3_ENDPOINT_URL"
SM_STUDIO_LOCAL_MODE = "SM_STUDIO_LOCAL_MODE"
# SELinux Enabled
SELINUX_ENABLED = os.environ.get("SAGEMAKER_LOCAL_SELINUX_ENABLED", "False").lower() in [
"1",
"true",
"yes",
]
logger = logging.getLogger(__name__)
class _SageMakerContainer(object):
"""Handle the lifecycle and configuration of a local container execution.
This class is responsible for creating the directories and configuration
files that the docker containers will use for either training or serving.
"""
def __init__(
self,
instance_type,
instance_count,
image,
sagemaker_session=None,
container_entrypoint=None,
container_arguments=None,
):
"""Initialize a SageMakerContainer instance
It uses a :class:`sagemaker.session.Session` for general interaction
with user configuration such as getting the default sagemaker S3 bucket.
However this class does not call any of the SageMaker APIs.
Args:
instance_type (str): The instance type to use. Either 'local' or
'local_gpu'
instance_count (int): The number of instances to create.
image (str): docker image to use.
sagemaker_session (sagemaker.session.Session): a sagemaker session
to use when interacting with SageMaker.
container_entrypoint (str): the container entrypoint to execute
container_arguments (str): the container entrypoint arguments
"""
from sagemaker.local.local_session import LocalSession
# check if docker-compose is installed
self.compose_cmd_prefix = _SageMakerContainer._get_compose_cmd_prefix()
self.sagemaker_session = sagemaker_session or LocalSession()
self.instance_type = instance_type
self.instance_count = instance_count
self.image = image
self.container_entrypoint = container_entrypoint
self.container_arguments = container_arguments
# Since we are using a single docker network, Generate a random suffix to attach to the
# container names. This way multiple jobs can run in parallel.
suffix = "".join(random.choice(string.ascii_lowercase + string.digits) for _ in range(5))
self.is_studio = sagemaker.local.utils.check_for_studio()
if self.is_studio:
if self.instance_count > 1:
raise NotImplementedError(
"Multi instance Local Mode execution is "
"currently not supported in SageMaker Studio."
)
# For studio use-case, directories need to be created in `~/tmp`, rather than /tmp
home = os.path.expanduser("~")
root_dir = os.path.join(home, "tmp")
if not os.path.isdir(root_dir):
os.mkdir(root_dir)
if self.sagemaker_session.config:
self.sagemaker_session.config["local"]["container_root"] = root_dir
else:
self.sagemaker_session.config = {"local": {"container_root": root_dir}}
# Studio only supports single instance run
self.hosts = [STUDIO_HOST_NAME]
else:
self.hosts = [
"{}-{}-{}".format(CONTAINER_PREFIX, i, suffix)
for i in range(1, self.instance_count + 1)
]
self.container_root = None
self.container = None
@staticmethod
def _get_compose_cmd_prefix():
"""Gets the Docker Compose command.
The method initially looks for 'docker compose' v2
executable, if not found looks for 'docker-compose' executable.
Returns:
Docker Compose executable split into list.
Raises:
ImportError: If Docker Compose executable was not found.
"""
compose_cmd_prefix = []
output = None
try:
output = subprocess.check_output(
["docker", "compose", "version"],
stderr=subprocess.DEVNULL,
encoding="UTF-8",
)
except subprocess.CalledProcessError:
logger.info(
"'Docker Compose' is not installed. "
"Proceeding to check for 'docker-compose' CLI."
)
if output and "v2" in output.strip():
logger.info("'Docker Compose' found using Docker CLI.")
compose_cmd_prefix.extend(["docker", "compose"])
return compose_cmd_prefix
if shutil.which("docker-compose") is not None:
logger.info("'Docker Compose' found using Docker Compose CLI.")
compose_cmd_prefix.extend(["docker-compose"])
return compose_cmd_prefix
raise ImportError(
"Docker Compose is not installed. "
"Local Mode features will not work without docker compose. "
"For more information on how to install 'docker compose', please, see "
"https://docs.docker.com/compose/install/"
)
def process(
self,
processing_inputs,
processing_output_config,
environment,
processing_job_name,
):
"""Run a processing job locally using docker-compose.
Args:
processing_inputs (dict): The processing input specification.
processing_output_config (dict): The processing output configuration specification.
environment (dict): The environment collection for the processing job.
processing_job_name (str): Name of the local processing job being run.
"""
self.container_root = self._create_tmp_folder()
# A shared directory for all the containers;
# it is only mounted if the processing script is Local.
shared_dir = os.path.join(self.container_root, "shared")
os.mkdir(shared_dir)
data_dir = self._create_tmp_folder()
volumes = self._prepare_processing_volumes(
data_dir, processing_inputs, processing_output_config
)
# Create the configuration files for each container that we will create.
for host in self.hosts:
_create_processing_config_file_directories(self.container_root, host)
self.write_processing_config_files(
host,
environment,
processing_inputs,
processing_output_config,
processing_job_name,
)
self._generate_compose_file(
"process", additional_volumes=volumes, additional_env_vars=environment
)
if _ecr_login_if_needed(self.sagemaker_session.boto_session, self.image):
_pull_image(self.image)
compose_command = self._compose()
process = subprocess.Popen(
compose_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
try:
_stream_output(process)
finally:
# Uploading processing outputs back to Amazon S3.
self._upload_processing_outputs(data_dir, processing_output_config)
try:
# Deleting temporary directories.
dirs_to_delete = [shared_dir, data_dir]
self._cleanup(dirs_to_delete)
except OSError:
pass
# Print our Job Complete line to have a similar experience to training on SageMaker where
# you see this line at the end.
logger.info("===== Job Complete =====")
def train(self, input_data_config, output_data_config, hyperparameters, environment, job_name):
"""Run a training job locally using docker-compose.
Args:
input_data_config (dict): The Input Data Configuration, this contains data such as the
channels to be used for training.
output_data_config: The configuration of the output data.
hyperparameters (dict): The HyperParameters for the training job.
environment (dict): The environment collection for the training job.
job_name (str): Name of the local training job being run.
Returns (str): Location of the trained model.
"""
self.container_root = self._create_tmp_folder()
os.mkdir(os.path.join(self.container_root, "output"))
# create output/data folder since sagemaker-containers 2.0 expects it
os.mkdir(os.path.join(self.container_root, "output", "data"))
# A shared directory for all the containers. It is only mounted if the training script is
# Local.
shared_dir = os.path.join(self.container_root, "shared")
os.mkdir(shared_dir)
data_dir = self._create_tmp_folder()
volumes = self._prepare_training_volumes(
data_dir, input_data_config, output_data_config, hyperparameters
)
# If local, source directory needs to be updated to mounted /opt/ml/code path
hyperparameters = self._update_local_src_path(
hyperparameters, key=sagemaker.estimator.DIR_PARAM_NAME
)
# Create the configuration files for each container that we will create
# Each container will map the additional local volumes (if any).
for host in self.hosts:
_create_config_file_directories(self.container_root, host)
self.write_config_files(host, hyperparameters, input_data_config)
shutil.copytree(data_dir, os.path.join(self.container_root, host, "input", "data"))
training_env_vars = {
REGION_ENV_NAME: self.sagemaker_session.boto_region_name,
TRAINING_JOB_NAME_ENV_NAME: job_name,
}
training_env_vars.update(environment)
if self.sagemaker_session.s3_resource is not None:
training_env_vars[S3_ENDPOINT_URL_ENV_NAME] = (
self.sagemaker_session.s3_resource.meta.client._endpoint.host
)
compose_data = self._generate_compose_file(
"train", additional_volumes=volumes, additional_env_vars=training_env_vars
)
if _ecr_login_if_needed(self.sagemaker_session.boto_session, self.image):
_pull_image(self.image)
compose_command = self._compose()
process = subprocess.Popen(
compose_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
try:
_stream_output(process)
finally:
artifacts = self.retrieve_artifacts(compose_data, output_data_config, job_name)
# free up the training data directory as it may contain
# lots of data downloaded from S3. This doesn't delete any local
# data that was just mounted to the container.
dirs_to_delete = [data_dir, shared_dir]
self._cleanup(dirs_to_delete)
# Print our Job Complete line to have a similar experience to training on SageMaker where
# you see this line at the end.
logger.info("===== Job Complete =====")
return artifacts
def serve(self, model_dir, environment):
"""Host a local endpoint using docker-compose.
Args:
primary_container (dict): dictionary containing the container runtime settings
for serving. Expected keys:
- 'ModelDataUrl' pointing to a file or s3:// location.
- 'Environment' a dictionary of environment variables to be passed to the
hosting container.
"""
logger.info("serving")
self.container_root = self._create_tmp_folder()
logger.info("creating hosting dir in %s", self.container_root)
if model_dir is not None:
volumes = self._prepare_serving_volumes(model_dir)
else:
volumes = None
# if model_data is None, then force create ../sagemaker-local under
# contianer root
os.makedirs(
os.path.join(self.container_root, self.hosts[0]),
exist_ok=True
)
# If the user script was passed as a file:// mount it to the container.
if sagemaker.estimator.DIR_PARAM_NAME.upper() in environment:
script_dir = environment[sagemaker.estimator.DIR_PARAM_NAME.upper()]
parsed_uri = urlparse(script_dir)
if parsed_uri.scheme == "file":
host_dir = os.path.abspath(parsed_uri.netloc + parsed_uri.path)
volumes.append(_Volume(host_dir, "/opt/ml/code"))
# Update path to mount location
environment = environment.copy()
environment[sagemaker.estimator.DIR_PARAM_NAME.upper()] = "/opt/ml/code"
if _ecr_login_if_needed(self.sagemaker_session.boto_session, self.image):
_pull_image(self.image)
self._generate_compose_file(
"serve", additional_env_vars=environment, additional_volumes=volumes
)
compose_command = self._compose()
self.container = _HostingContainer(compose_command)
self.container.start()
def stop_serving(self):
"""Stop the serving container.
The serving container runs in async mode to allow the SDK to do other
tasks.
"""
if self.container:
self.container.down()
self.container.join()
self._cleanup()
# for serving we can delete everything in the container root.
_delete_tree(self.container_root)
def retrieve_artifacts(self, compose_data, output_data_config, job_name):
"""Get the model artifacts from all the container nodes.
Used after training completes to gather the data from all the
individual containers. As the official SageMaker Training Service, it
will override duplicate files if multiple containers have the same file
names.
Args:
compose_data (dict): Docker-Compose configuration in dictionary
format.
output_data_config: The configuration of the output data.
job_name: The name of the job.
Returns: Local path to the collected model artifacts.
"""
# We need a directory to store the artfiacts from all the nodes
# and another one to contained the compressed final artifacts
artifacts = os.path.join(self.container_root, "artifacts")
compressed_artifacts = os.path.join(self.container_root, "compressed_artifacts")
os.mkdir(artifacts)
model_artifacts = os.path.join(artifacts, "model")
output_artifacts = os.path.join(artifacts, "output")
artifact_dirs = [model_artifacts, output_artifacts, compressed_artifacts]
for d in artifact_dirs:
os.mkdir(d)
# Gather the artifacts from all nodes into artifacts/model and artifacts/output
for host in self.hosts:
volumes = compose_data["services"][str(host)]["volumes"]
volumes = [v[:-2] if v.endswith(":z") else v for v in volumes]
for volume in volumes:
if re.search(r"^[A-Za-z]:", volume):
unit, host_dir, container_dir = volume.split(":")
host_dir = unit + ":" + host_dir
else:
host_dir, container_dir = volume.split(":")
if container_dir == "/opt/ml/model":
sagemaker.local.utils.recursive_copy(host_dir, model_artifacts)
elif container_dir == "/opt/ml/output":
sagemaker.local.utils.recursive_copy(host_dir, output_artifacts)
# Tar Artifacts -> model.tar.gz and output.tar.gz
model_files = [os.path.join(model_artifacts, name) for name in os.listdir(model_artifacts)]
output_files = [
os.path.join(output_artifacts, name) for name in os.listdir(output_artifacts)
]
sagemaker.utils.create_tar_file(
model_files, os.path.join(compressed_artifacts, "model.tar.gz")
)
sagemaker.utils.create_tar_file(
output_files, os.path.join(compressed_artifacts, "output.tar.gz")
)
if output_data_config["S3OutputPath"] == "":
output_data = "file://%s" % compressed_artifacts
else:
# Now we just need to move the compressed artifacts to wherever they are required
output_data = sagemaker.local.utils.move_to_destination(
compressed_artifacts,
output_data_config["S3OutputPath"],
job_name,
self.sagemaker_session,
prefix="output",
)
_delete_tree(model_artifacts)
_delete_tree(output_artifacts)
return os.path.join(output_data, "model.tar.gz")
def write_processing_config_files(
self,
host,
environment,
processing_inputs,
processing_output_config,
processing_job_name,
):
"""Write the config files for the processing containers.
This method writes the hyperparameters, resources and input data
configuration files.
Args:
host (str): Host to write the configuration for
environment (dict): Environment variable collection.
processing_inputs (dict): Processing inputs.
processing_output_config (dict): Processing output configuration.
processing_job_name (str): Processing job name.
"""
config_path = os.path.join(self.container_root, host, "config")
resource_config = {
"current_host": host,
"hosts": self.hosts,
"network_interface_name": "eth0",
"current_instance_type": self.instance_type,
}
_write_json_file(os.path.join(config_path, "resourceconfig.json"), resource_config)
processing_job_config = {
"ProcessingJobArn": processing_job_name,
"ProcessingJobName": processing_job_name,
"AppSpecification": {
"ImageUri": self.image,
"ContainerEntrypoint": self.container_entrypoint,
"ContainerArguments": self.container_arguments,
},
"Environment": environment,
"ProcessingInputs": processing_inputs,
"ProcessingOutputConfig": processing_output_config,
"ProcessingResources": {
"ClusterConfig": {
"InstanceCount": self.instance_count,
"InstanceType": self.instance_type,
"VolumeSizeInGB": 30,
"VolumeKmsKeyId": None,
}
},
"RoleArn": "<no_role>",
"StoppingCondition": {"MaxRuntimeInSeconds": 86400},
}
_write_json_file(
os.path.join(config_path, "processingjobconfig.json"), processing_job_config
)
def write_config_files(self, host, hyperparameters, input_data_config):
"""Write the config files for the training containers.
This method writes the hyperparameters, resources and input data
configuration files.
Returns: None
Args:
host (str): Host to write the configuration for
hyperparameters (dict): Hyperparameters for training.
input_data_config (dict): Training input channels to be used for
training.
"""
config_path = os.path.join(self.container_root, host, "input", "config")
resource_config = {
"current_host": host,
"hosts": self.hosts,
"network_interface_name": "eth0",
"current_instance_type": self.instance_type,
}
json_input_data_config = {}
for c in input_data_config:
channel_name = c["ChannelName"]
json_input_data_config[channel_name] = {"TrainingInputMode": "File"}
if "ContentType" in c:
json_input_data_config[channel_name]["ContentType"] = c["ContentType"]
_write_json_file(os.path.join(config_path, "hyperparameters.json"), hyperparameters)
_write_json_file(os.path.join(config_path, "resourceconfig.json"), resource_config)
_write_json_file(os.path.join(config_path, "inputdataconfig.json"), json_input_data_config)
def _prepare_training_volumes(
self, data_dir, input_data_config, output_data_config, hyperparameters
):
"""Prepares the training volumes based on input and output data configs.
Args:
data_dir:
input_data_config:
output_data_config:
hyperparameters:
"""
shared_dir = os.path.join(self.container_root, "shared")
model_dir = os.path.join(self.container_root, "model")
volumes = []
volumes.append(_Volume(model_dir, "/opt/ml/model"))
# Mount the metadata directory if present.
# Only expected to be present on SM notebook instances.
# This is used by some DeepEngine libraries
metadata_dir = "/opt/ml/metadata"
if os.path.isdir(metadata_dir):
volumes.append(_Volume(metadata_dir, metadata_dir))
# Set up the channels for the containers. For local data we will
# mount the local directory to the container. For S3 Data we will download the S3 data
# first.
for channel in input_data_config:
uri = channel["DataUri"]
channel_name = channel["ChannelName"]
channel_dir = os.path.join(data_dir, channel_name)
os.mkdir(channel_dir)
data_source = sagemaker.local.data.get_data_source_instance(uri, self.sagemaker_session)
volumes.append(_Volume(data_source.get_root_dir(), channel=channel_name))
# If there is a training script directory and it is a local directory,
# mount it to the container.
if sagemaker.estimator.DIR_PARAM_NAME in hyperparameters:
training_dir = json.loads(hyperparameters[sagemaker.estimator.DIR_PARAM_NAME])
parsed_uri = urlparse(training_dir)
if parsed_uri.scheme == "file":
host_dir = os.path.abspath(parsed_uri.netloc + parsed_uri.path)
volumes.append(_Volume(host_dir, "/opt/ml/code"))
# Also mount a directory that all the containers can access.
volumes.append(_Volume(shared_dir, "/opt/ml/shared"))
parsed_uri = urlparse(output_data_config["S3OutputPath"])
if (
parsed_uri.scheme == "file"
and sagemaker.model.SAGEMAKER_OUTPUT_LOCATION in hyperparameters
):
dir_path = os.path.abspath(parsed_uri.netloc + parsed_uri.path)
intermediate_dir = os.path.join(dir_path, "output", "intermediate")
if not os.path.exists(intermediate_dir):
os.makedirs(intermediate_dir)
volumes.append(_Volume(intermediate_dir, "/opt/ml/output/intermediate"))
return volumes
def _prepare_processing_volumes(self, data_dir, processing_inputs, processing_output_config):
"""Prepares local container volumes for the processing job.
Args:
data_dir: The local data directory.
processing_inputs: The configuration of processing inputs.
processing_output_config: The configuration of processing outputs.
Returns:
The volumes configuration.
"""
shared_dir = os.path.join(self.container_root, "shared")
volumes = []
# Set up the input/outputs for the container.
for item in processing_inputs:
uri = item["DataUri"]
input_container_dir = item["S3Input"]["LocalPath"]
data_source = sagemaker.local.data.get_data_source_instance(uri, self.sagemaker_session)
volumes.append(_Volume(data_source.get_root_dir(), input_container_dir))
if processing_output_config and "Outputs" in processing_output_config:
for item in processing_output_config["Outputs"]:
output_name = item["OutputName"]
output_container_dir = item["S3Output"]["LocalPath"]
output_dir = os.path.join(data_dir, "output", output_name)
os.makedirs(output_dir)
volumes.append(_Volume(output_dir, output_container_dir))
volumes.append(_Volume(shared_dir, "/opt/ml/shared"))
return volumes
def _upload_processing_outputs(self, data_dir, processing_output_config):
"""Uploads processing outputs to Amazon S3.
Args:
data_dir: The local data directory.
processing_output_config: The processing output configuration.
"""
if processing_output_config and "Outputs" in processing_output_config:
for item in processing_output_config["Outputs"]:
output_name = item["OutputName"]
output_s3_uri = item["S3Output"]["S3Uri"]
output_dir = os.path.join(data_dir, "output", output_name)
sagemaker.local.utils.move_to_destination(
output_dir, output_s3_uri, "", self.sagemaker_session
)
def _update_local_src_path(self, params, key):
"""Updates the local path of source code.
Args:
params: Existing configuration parameters.
key: Lookup key for the path of the source code in the configuration parameters.
Returns:
The updated parameters.
"""
if key in params:
src_dir = json.loads(params[key])
parsed_uri = urlparse(src_dir)
if parsed_uri.scheme == "file":
new_params = params.copy()
new_params[key] = json.dumps("/opt/ml/code")
return new_params
return params
def _prepare_serving_volumes(self, model_location):
"""Prepares the serving volumes.
Args:
model_location: Location of the models.
"""
volumes = []
host = self.hosts[0]
# Make the model available to the container. If this is a local file just mount it to
# the container as a volume. If it is an S3 location, the DataSource will download it, we
# just need to extract the tar file.
host_dir = os.path.join(self.container_root, host)
os.makedirs(host_dir)
model_data_source = sagemaker.local.data.get_data_source_instance(
model_location, self.sagemaker_session
)
for filename in model_data_source.get_file_list():
if tarfile.is_tarfile(filename):
with tarfile.open(filename) as tar:
custom_extractall_tarfile(tar, model_data_source.get_root_dir())
volumes.append(_Volume(model_data_source.get_root_dir(), "/opt/ml/model"))
return volumes
def _generate_compose_file(self, command, additional_volumes=None, additional_env_vars=None):
"""Writes a config file describing a training/hosting environment.
This method generates a docker compose configuration file, it has an
entry for each container that will be created (based on self.hosts). it
calls
:meth:~sagemaker.local_session.SageMakerContainer._create_docker_host to
generate the config for each individual container.
Args:
command (str): either 'train' or 'serve'
additional_volumes (list): a list of volumes that will be mapped to
the containers
additional_env_vars (dict): a dictionary with additional environment
variables to be passed on to the containers.
Returns: (dict) A dictionary representation of the configuration that was written.
"""
boto_session = self.sagemaker_session.boto_session
additional_volumes = additional_volumes or []
additional_env_vars = additional_env_vars or {}
environment = []
optml_dirs = set()
aws_creds = _aws_credentials(boto_session)
if aws_creds is not None:
environment.extend(aws_creds)
additional_env_var_list = ["{}={}".format(k, v) for k, v in additional_env_vars.items()]
environment.extend(additional_env_var_list)
if self.is_studio:
environment.extend([f"{SM_STUDIO_LOCAL_MODE}=True"])
if os.environ.get(DOCKER_COMPOSE_HTTP_TIMEOUT_ENV) is None:
os.environ[DOCKER_COMPOSE_HTTP_TIMEOUT_ENV] = DOCKER_COMPOSE_HTTP_TIMEOUT
if command == "train":
optml_dirs = {"output", "output/data", "input"}
elif command == "process":
optml_dirs = {"output", "config"}
services = {
h: self._create_docker_host(h, environment, optml_dirs, command, additional_volumes)
for h in self.hosts
}
if self.is_studio:
content = {
# Use version 2.3 as a minimum so that we can specify the runtime
"version": "2.3",
"services": services,
}
else:
content = {
# Use version 2.3 as a minimum so that we can specify the runtime
"version": "2.3",
"services": services,
"networks": {"sagemaker-local": {"name": "sagemaker-local"}},
}
docker_compose_path = os.path.join(self.container_root, DOCKER_COMPOSE_FILENAME)
try:
import yaml
except ImportError as e:
logger.error(sagemaker.utils._module_import_error("yaml", "Local mode", "local"))
raise e
yaml_content = yaml.dump(content, default_flow_style=False)
# Mask all environment vars for logging, could contain secrects.
masked_content = copy.deepcopy(content)
for _, service_data in masked_content["services"].items():
service_data["environment"] = ["[Masked]" for _ in service_data["environment"]]
masked_content_for_logging = yaml.dump(masked_content, default_flow_style=False)
logger.info("docker compose file: \n%s", masked_content_for_logging)
with open(docker_compose_path, "w") as f:
f.write(yaml_content)
return content
def _compose(self, detached=False):
"""Invokes the docker compose command.
Args:
detached:
"""
compose_cmd = self.compose_cmd_prefix
command = [
"-f",
os.path.join(self.container_root, DOCKER_COMPOSE_FILENAME),
"up",
"--build",
"--abort-on-container-exit" if not detached else "--detach", # mutually exclusive
]
compose_cmd.extend(command)
logger.info("docker command: %s", " ".join(compose_cmd))
return compose_cmd
def _create_docker_host(
self,
host: str,
environment: List[str],
optml_subdirs: set[str],
command: str,
volumes: List,
) -> Dict:
"""Creates the docker host configuration.
Args:
host (str): The host address
environment (List[str]): List of environment variables
optml_subdirs (Set[str]): Set of subdirs
command (str): Either 'train' or 'serve'
volumes (list): List of volumes that will be mapped to the containers
"""
optml_volumes = self._build_optml_volumes(host, optml_subdirs)
optml_volumes.extend(volumes)
container_name_prefix = "".join(
random.choice(string.ascii_lowercase + string.digits) for _ in range(10)
)
container_default_config = (
sagemaker.utils.get_config_value(
f"{LOCAL}.{CONTAINER_CONFIG}", self.sagemaker_session.config
)
or {}
)
host_config = {
**container_default_config,
"image": self.image,
"container_name": f"{container_name_prefix}-{host}",
"stdin_open": True,
"tty": True,
"volumes": [v.map for v in optml_volumes],
"environment": environment,
}
is_train_with_entrypoint = False
if command == "train" and self.container_entrypoint:
# Remote function or Pipeline function step is translated into a training job
# with container_entrypoint configured
is_train_with_entrypoint = True
if command != "process" and not is_train_with_entrypoint:
host_config["command"] = command
else:
if self.container_entrypoint:
host_config["entrypoint"] = self.container_entrypoint
if self.container_arguments:
host_config["entrypoint"] = host_config["entrypoint"] + self.container_arguments
if self.is_studio:
host_config["network_mode"] = "sagemaker"
else:
host_config["networks"] = {"sagemaker-local": {"aliases": [host]}}
# for GPU support pass in nvidia as the runtime, this is equivalent
# to setting --runtime=nvidia in the docker commandline.
if self.instance_type == "local_gpu":
host_config["deploy"] = {
"resources": {
"reservations": {"devices": [{"driver": "nvidia", "count": "all", "capabilities": ["gpu"]}]}
}
}
if not self.is_studio and command == "serve":
serving_port = (
sagemaker.utils.get_config_value(
"local.serving_port", self.sagemaker_session.config
)
or 8080
)
host_config.update({"ports": ["%s:8080" % serving_port]})
return host_config
def _create_tmp_folder(self):
"""Placeholder docstring"""
root_dir = sagemaker.utils.get_config_value(
"local.container_root", self.sagemaker_session.config
)
if root_dir:
root_dir = os.path.abspath(root_dir)
working_dir = tempfile.mkdtemp(dir=root_dir)
# Docker cannot mount Mac OS /var folder properly see
# https://forums.docker.com/t/var-folders-isnt-mounted-properly/9600
# Only apply this workaround if the user didn't provide an alternate storage root dir.
if root_dir is None and platform.system() == "Darwin":
working_dir = "/private{}".format(working_dir)
return os.path.abspath(working_dir)
def _build_optml_volumes(self, host, subdirs):
"""Generate a list of :class:`~sagemaker.local_session.Volume`.
These are required for the container to start. It takes a folder with
the necessary files for training and creates a list of opt volumes
that the Container needs to start.
Args:
host (str): container for which the volumes will be generated.
subdirs (list): list of subdirectories that will be mapped. For
example: ['input', 'output', 'model']
Returns: (list) List of :class:`~sagemaker.local_session.Volume`
"""
volumes = []
for subdir in subdirs:
host_dir = os.path.join(self.container_root, host, subdir)
container_dir = "/opt/ml/{}".format(subdir)
volume = _Volume(host_dir, container_dir)
volumes.append(volume)
return volumes
def _cleanup(self, dirs_to_delete=None):
"""Cleans up directories and the like.
Args:
dirs_to_delete:
"""
if dirs_to_delete:
for d in dirs_to_delete:
_delete_tree(d)
# Free the container config files.
for host in self.hosts:
container_config_path = os.path.join(self.container_root, host)
_delete_tree(container_config_path)
class _HostingContainer(Thread):
"""Placeholder docstring."""
def __init__(self, command):
"""Creates a new threaded hosting container.
Args:
command (dict): docker compose command
"""
Thread.__init__(self)
self.command = command
self.process = None
def run(self):
"""Placeholder docstring"""
self.process = subprocess.Popen(
self.command, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
try:
_stream_output(self.process)
except RuntimeError as e:
# _stream_output() doesn't have the command line. We will handle the exception
# which contains the exit code and append the command line to it.
msg = "Failed to run: %s, %s" % (self.command, str(e))
raise RuntimeError(msg)
def down(self):
"""Placeholder docstring"""
if os.name != "nt":
sagemaker.local.utils.kill_child_processes(self.process.pid)
self.process.terminate()
class _Volume(object):
"""Represent a Volume that will be mapped to a container."""
def __init__(self, host_dir, container_dir=None, channel=None):
"""Create a Volume instance.
The container path can be provided as a container_dir or as a channel name but not both.
Args:
host_dir (str): path to the volume data in the host
container_dir (str): path inside the container that host_dir will be mapped to
channel (str): channel name that the host_dir represents. It will be mapped as
/opt/ml/input/data/<channel> in the container.
"""