-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathjob.py
More file actions
1753 lines (1436 loc) · 70.5 KB
/
job.py
File metadata and controls
1753 lines (1436 loc) · 70.5 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
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.
"""Helper classes that interact with SageMaker Training service."""
from __future__ import absolute_import
import dataclasses
import os
import re
import shutil
import sys
import json
import secrets
from typing import Callable, Dict, List, Optional, Tuple, Union, TYPE_CHECKING
from urllib.parse import urlparse
from io import BytesIO
from sagemaker.config.config_schema import (
REMOTE_FUNCTION_ENVIRONMENT_VARIABLES,
REMOTE_FUNCTION_IMAGE_URI,
REMOTE_FUNCTION_DEPENDENCIES,
REMOTE_FUNCTION_PRE_EXECUTION_COMMANDS,
REMOTE_FUNCTION_PRE_EXECUTION_SCRIPT,
REMOTE_FUNCTION_INCLUDE_LOCAL_WORKDIR,
REMOTE_FUNCTION_INSTANCE_TYPE,
REMOTE_FUNCTION_JOB_CONDA_ENV,
REMOTE_FUNCTION_ROLE_ARN,
REMOTE_FUNCTION_S3_ROOT_URI,
REMOTE_FUNCTION_S3_KMS_KEY_ID,
REMOTE_FUNCTION_VOLUME_KMS_KEY_ID,
REMOTE_FUNCTION_TAGS,
REMOTE_FUNCTION_VPC_CONFIG_SUBNETS,
REMOTE_FUNCTION_VPC_CONFIG_SECURITY_GROUP_IDS,
REMOTE_FUNCTION_ENABLE_INTER_CONTAINER_TRAFFIC_ENCRYPTION,
)
from sagemaker.experiments._run_context import _RunContext
from sagemaker.experiments.run import Run
from sagemaker.image_uris import get_base_python_image_uri
from sagemaker import image_uris
from sagemaker.remote_function.checkpoint_location import CheckpointLocation
from sagemaker.session import get_execution_role, _logs_for_job, Session
from sagemaker.utils import (
name_from_base,
_tmpdir,
resolve_value_from_config,
format_tags,
Tags,
)
from sagemaker.s3 import s3_path_join, S3Uploader
from sagemaker import vpc_utils
from sagemaker.remote_function.core.stored_function import StoredFunction, _SerializedData
from sagemaker.remote_function.core.pipeline_variables import Context
from sagemaker.remote_function.runtime_environment.runtime_environment_manager import (
RuntimeEnvironmentManager,
_DependencySettings,
)
from sagemaker.remote_function import logging_config
from sagemaker.remote_function.spark_config import SparkConfig
from sagemaker.spark import defaults
from sagemaker.remote_function.custom_file_filter import (
CustomFileFilter,
copy_workdir,
resolve_custom_file_filter_from_config_file,
)
from sagemaker.workflow.function_step import DelayedReturn
from sagemaker.workflow.step_outputs import get_step
if TYPE_CHECKING:
from sagemaker.workflow.entities import PipelineVariable
# runtime script names
BOOTSTRAP_SCRIPT_NAME = "bootstrap_runtime_environment.py"
MPI_UTILS_SCRIPT_NAME = "mpi_utils_remote.py"
ENTRYPOINT_SCRIPT_NAME = "job_driver.sh"
PRE_EXECUTION_SCRIPT_NAME = "pre_exec.sh"
RUNTIME_MANAGER_SCRIPT_NAME = "runtime_environment_manager.py"
SPARK_APP_SCRIPT_NAME = "spark_app.py"
# training channel names
RUNTIME_SCRIPTS_CHANNEL_NAME = "sagemaker_remote_function_bootstrap"
REMOTE_FUNCTION_WORKSPACE = "sm_rf_user_ws"
JOB_REMOTE_FUNCTION_WORKSPACE = "sagemaker_remote_function_workspace"
SCRIPT_AND_DEPENDENCIES_CHANNEL_NAME = "pre_exec_script_and_dependencies"
# Spark config channel and file name
SPARK_CONF_CHANNEL_NAME = "conf"
SPARK_CONF_FILE_NAME = "configuration.json"
# Spark submitted files workspace names on S3
SPARK_SUBMIT_JARS_WORKSPACE = "sm_rf_spark_jars"
SPARK_SUBMIT_PY_FILES_WORKSPACE = "sm_rf_spark_py_files"
SPARK_SUBMIT_FILES_WORKSPACE = "sm_rf_spark_data_files"
SPARK_CONF_WORKSPACE = "sm_rf_spark_conf"
# default spark version
DEFAULT_SPARK_VERSION = "3.3"
DEFAULT_SPARK_CONTAINER_VERSION = "v1"
# run context dictionary keys
KEY_EXPERIMENT_NAME = "experiment_name"
KEY_RUN_NAME = "run_name"
JOBS_CONTAINER_ENTRYPOINT = [
"/bin/bash",
f"/opt/ml/input/data/{RUNTIME_SCRIPTS_CHANNEL_NAME}/{ENTRYPOINT_SCRIPT_NAME}",
]
SPARK_APP_SCRIPT_PATH = f"/opt/ml/input/data/{RUNTIME_SCRIPTS_CHANNEL_NAME}/{SPARK_APP_SCRIPT_NAME}"
ENTRYPOINT_SCRIPT = f"""
#!/bin/bash
# Entry point for bootstrapping runtime environment and invoking remote function
set -eu
PERSISTENT_CACHE_DIR=${{SAGEMAKER_MANAGED_WARMPOOL_CACHE_DIRECTORY:-/opt/ml/cache}}
export CONDA_PKGS_DIRS=${{PERSISTENT_CACHE_DIR}}/sm_remotefunction_user_dependencies_cache/conda/pkgs
printf "INFO: CONDA_PKGS_DIRS is set to '$CONDA_PKGS_DIRS'\\n"
export PIP_CACHE_DIR=${{PERSISTENT_CACHE_DIR}}/sm_remotefunction_user_dependencies_cache/pip
printf "INFO: PIP_CACHE_DIR is set to '$PIP_CACHE_DIR'\\n"
printf "INFO: /opt/ml/input/config/resourceconfig.json:\\n"
cat /opt/ml/input/config/resourceconfig.json
printf "INFO: Bootstraping runtime environment.\\n"
python /opt/ml/input/data/{RUNTIME_SCRIPTS_CHANNEL_NAME}/{BOOTSTRAP_SCRIPT_NAME} "$@"
source /opt/ml/input/sm_training.env
if [ -d {JOB_REMOTE_FUNCTION_WORKSPACE} ]
then
if [ -f "remote_function_conda_env.txt" ]
then
cp remote_function_conda_env.txt {JOB_REMOTE_FUNCTION_WORKSPACE}/remote_function_conda_env.txt
fi
printf "INFO: Changing workspace to {JOB_REMOTE_FUNCTION_WORKSPACE}.\\n"
cd {JOB_REMOTE_FUNCTION_WORKSPACE}
fi
if [ -f "remote_function_conda_env.txt" ]
then
conda_env=$(cat remote_function_conda_env.txt)
if which mamba >/dev/null; then
conda_exe="mamba"
else
conda_exe="conda"
fi
printf "INFO: Invoking remote function inside conda environment: $conda_env.\\n"
printf "INFO: $conda_exe run -n $conda_env python -m sagemaker.remote_function.invoke_function \\n"
$conda_exe run -n $conda_env python -m sagemaker.remote_function.invoke_function "$@"
else
printf "INFO: No conda env provided. Invoking remote function\\n"
printf "INFO: python -m sagemaker.remote_function.invoke_function \\n"
python -m sagemaker.remote_function.invoke_function "$@"
fi
"""
ENTRYPOINT_MPIRUN_SCRIPT = f"""
#!/bin/bash
# Entry point for bootstrapping runtime environment and invoking remote function with mpirun
set -eu
PERSISTENT_CACHE_DIR=${{SAGEMAKER_MANAGED_WARMPOOL_CACHE_DIRECTORY:-/opt/ml/cache}}
export CONDA_PKGS_DIRS=${{PERSISTENT_CACHE_DIR}}/sm_remotefunction_user_dependencies_cache/conda/pkgs
printf "INFO: CONDA_PKGS_DIRS is set to '$CONDA_PKGS_DIRS'\\n"
export PIP_CACHE_DIR=${{PERSISTENT_CACHE_DIR}}/sm_remotefunction_user_dependencies_cache/pip
printf "INFO: PIP_CACHE_DIR is set to '$PIP_CACHE_DIR'\\n"
printf "INFO: /opt/ml/input/config/resourceconfig.json:\\n"
cat /opt/ml/input/config/resourceconfig.json
printf "INFO: Bootstraping runtime environment.\\n"
python /opt/ml/input/data/{RUNTIME_SCRIPTS_CHANNEL_NAME}/{BOOTSTRAP_SCRIPT_NAME} "$@"
source /opt/ml/input/sm_training.env
if [ -d {JOB_REMOTE_FUNCTION_WORKSPACE} ]
then
if [ -f "remote_function_conda_env.txt" ]
then
cp remote_function_conda_env.txt {JOB_REMOTE_FUNCTION_WORKSPACE}/remote_function_conda_env.txt
fi
printf "INFO: Changing workspace to {JOB_REMOTE_FUNCTION_WORKSPACE}.\\n"
cd {JOB_REMOTE_FUNCTION_WORKSPACE}
fi
if [ -f "remote_function_conda_env.txt" ]
then
conda_env=$(cat remote_function_conda_env.txt)
if which mamba >/dev/null; then
conda_exe="mamba"
else
conda_exe="conda"
fi
if [ "$SM_CURRENT_HOST" = "$SM_MASTER_ADDR" ]; then
python /opt/ml/input/data/{RUNTIME_SCRIPTS_CHANNEL_NAME}/{MPI_UTILS_SCRIPT_NAME}
printf "INFO: Invoking remote function with mpirun inside conda environment: $conda_env.\\n"
printf "INFO: $conda_exe run -n $conda_env mpirun --host $SM_HOSTS_LIST -np $SM_NPROC_PER_NODE \
--allow-run-as-root --display-map --tag-output -mca btl_tcp_if_include $SM_NETWORK_INTERFACE_NAME \
-mca plm_rsh_no_tree_spawn 1 -mca pml ob1 -mca btl ^openib -mca orte_abort_on_non_zero_status 1 \
-mca btl_vader_single_copy_mechanism none -mca plm_rsh_num_concurrent $SM_HOST_COUNT \
-x NCCL_SOCKET_IFNAME=$SM_NETWORK_INTERFACE_NAME -x LD_LIBRARY_PATH -x PATH \
python -m mpi4py -m sagemaker.remote_function.invoke_function \\n"
$conda_exe run -n $conda_env mpirun --host $SM_HOSTS_LIST -np $SM_NPROC_PER_NODE \
--allow-run-as-root --display-map --tag-output -mca btl_tcp_if_include $SM_NETWORK_INTERFACE_NAME \
-mca plm_rsh_no_tree_spawn 1 -mca pml ob1 -mca btl ^openib -mca orte_abort_on_non_zero_status 1 \
-mca btl_vader_single_copy_mechanism none -mca plm_rsh_num_concurrent $SM_HOST_COUNT \
-x NCCL_SOCKET_IFNAME=$SM_NETWORK_INTERFACE_NAME -x LD_LIBRARY_PATH -x PATH \
$SM_FI_PROVIDER $SM_NCCL_PROTO $SM_FI_EFA_USE_DEVICE_RDMA \
python -m mpi4py -m sagemaker.remote_function.invoke_function "$@"
python /opt/ml/input/data/{RUNTIME_SCRIPTS_CHANNEL_NAME}/{MPI_UTILS_SCRIPT_NAME} --job_ended 1
else
printf "INFO: This is the instance $SM_CURRENT_HOST. mpirun command terminated\\n"
python /opt/ml/input/data/{RUNTIME_SCRIPTS_CHANNEL_NAME}/{MPI_UTILS_SCRIPT_NAME}
fi
else
if [ "$SM_CURRENT_HOST" = "$SM_MASTER_ADDR" ]; then
python /opt/ml/input/data/{RUNTIME_SCRIPTS_CHANNEL_NAME}/{MPI_UTILS_SCRIPT_NAME}
printf "INFO: No conda env provided. Invoking remote function with mpirun\\n"
printf "INFO: mpirun --host $SM_HOSTS_LIST -np $SM_NPROC_PER_NODE \
--allow-run-as-root --display-map --tag-output -mca btl_tcp_if_include $SM_NETWORK_INTERFACE_NAME \
-mca plm_rsh_no_tree_spawn 1 -mca pml ob1 -mca btl ^openib -mca orte_abort_on_non_zero_status 1 \
-mca btl_vader_single_copy_mechanism none -mca plm_rsh_num_concurrent $SM_HOST_COUNT \
-x NCCL_SOCKET_IFNAME=$SM_NETWORK_INTERFACE_NAME -x LD_LIBRARY_PATH -x PATH \
$SM_FI_PROVIDER $SM_NCCL_PROTO $SM_FI_EFA_USE_DEVICE_RDMA \
python -m mpi4py -m sagemaker.remote_function.invoke_function \\n"
mpirun --host $SM_HOSTS_LIST -np $SM_NPROC_PER_NODE \
--allow-run-as-root --display-map --tag-output -mca btl_tcp_if_include $SM_NETWORK_INTERFACE_NAME \
-mca plm_rsh_no_tree_spawn 1 -mca pml ob1 -mca btl ^openib -mca orte_abort_on_non_zero_status 1 \
-mca btl_vader_single_copy_mechanism none -mca plm_rsh_num_concurrent $SM_HOST_COUNT \
-x NCCL_SOCKET_IFNAME=$SM_NETWORK_INTERFACE_NAME -x LD_LIBRARY_PATH -x PATH \
$SM_FI_PROVIDER $SM_NCCL_PROTO $SM_FI_EFA_USE_DEVICE_RDMA \
python -m mpi4py -m sagemaker.remote_function.invoke_function "$@"
python /opt/ml/input/data/{RUNTIME_SCRIPTS_CHANNEL_NAME}/{MPI_UTILS_SCRIPT_NAME} --job_ended 1
else
printf "INFO: This is the instance $SM_CURRENT_HOST.\\n"
python /opt/ml/input/data/{RUNTIME_SCRIPTS_CHANNEL_NAME}/{MPI_UTILS_SCRIPT_NAME}
fi
fi
"""
ENTRYPOINT_TORCHRUN_SCRIPT = f"""
#!/bin/bash
# Entry point for bootstrapping runtime environment and invoking remote function with torchrun
set -eu
PERSISTENT_CACHE_DIR=${{SAGEMAKER_MANAGED_WARMPOOL_CACHE_DIRECTORY:-/opt/ml/cache}}
export CONDA_PKGS_DIRS=${{PERSISTENT_CACHE_DIR}}/sm_remotefunction_user_dependencies_cache/conda/pkgs
printf "INFO: CONDA_PKGS_DIRS is set to '$CONDA_PKGS_DIRS'\\n"
export PIP_CACHE_DIR=${{PERSISTENT_CACHE_DIR}}/sm_remotefunction_user_dependencies_cache/pip
printf "INFO: PIP_CACHE_DIR is set to '$PIP_CACHE_DIR'\\n"
printf "INFO: /opt/ml/input/config/resourceconfig.json:\\n"
cat /opt/ml/input/config/resourceconfig.json
printf "INFO: Bootstraping runtime environment.\\n"
python /opt/ml/input/data/{RUNTIME_SCRIPTS_CHANNEL_NAME}/{BOOTSTRAP_SCRIPT_NAME} "$@"
source /opt/ml/input/sm_training.env
if [ -d {JOB_REMOTE_FUNCTION_WORKSPACE} ]
then
if [ -f "remote_function_conda_env.txt" ]
then
cp remote_function_conda_env.txt {JOB_REMOTE_FUNCTION_WORKSPACE}/remote_function_conda_env.txt
fi
printf "INFO: Changing workspace to {JOB_REMOTE_FUNCTION_WORKSPACE}.\\n"
cd {JOB_REMOTE_FUNCTION_WORKSPACE}
fi
if [ -f "remote_function_conda_env.txt" ]
then
conda_env=$(cat remote_function_conda_env.txt)
if which mamba >/dev/null; then
conda_exe="mamba"
else
conda_exe="conda"
fi
printf "INFO: Invoking remote function with torchrun inside conda environment: $conda_env.\\n"
printf "INFO: $conda_exe run -n $conda_env torchrun --nnodes $SM_HOST_COUNT --nproc_per_node $SM_NPROC_PER_NODE \
--master_addr $SM_MASTER_ADDR --master_port $SM_MASTER_PORT --node_rank $SM_CURRENT_HOST_RANK \
-m sagemaker.remote_function.invoke_function \\n"
$conda_exe run -n $conda_env torchrun --nnodes $SM_HOST_COUNT --nproc_per_node $SM_NPROC_PER_NODE \
--master_addr $SM_MASTER_ADDR --master_port $SM_MASTER_PORT --node_rank $SM_CURRENT_HOST_RANK \
-m sagemaker.remote_function.invoke_function "$@"
else
printf "INFO: No conda env provided. Invoking remote function with torchrun\\n"
printf "INFO: torchrun --nnodes $SM_HOST_COUNT --nproc_per_node $SM_NPROC_PER_NODE --master_addr $SM_MASTER_ADDR \
--master_port $SM_MASTER_PORT --node_rank $SM_CURRENT_HOST_RANK -m sagemaker.remote_function.invoke_function \\n"
torchrun --nnodes $SM_HOST_COUNT --nproc_per_node $SM_NPROC_PER_NODE --master_addr $SM_MASTER_ADDR \
--master_port $SM_MASTER_PORT --node_rank $SM_CURRENT_HOST_RANK -m sagemaker.remote_function.invoke_function "$@"
fi
"""
SPARK_ENTRYPOINT_SCRIPT = f"""
#!/bin/bash
# Entry point for bootstrapping runtime environment and invoking remote function for Spark
set -eu
printf "INFO: Bootstraping Spark runtime environment.\\n"
python3 /opt/ml/input/data/{RUNTIME_SCRIPTS_CHANNEL_NAME}/{BOOTSTRAP_SCRIPT_NAME} "$@"
# Spark Container entry point script to initiate the spark application
smspark-submit "$@"
"""
logger = logging_config.get_logger()
class _JobSettings:
"""Helper class that processes the job settings.
It validates the job settings and provides default values if necessary.
"""
def __init__(
self,
*,
dependencies: str = None,
pre_execution_commands: List[str] = None,
pre_execution_script: str = None,
environment_variables: Dict[str, Union[str, "PipelineVariable"]] = None,
image_uri: Union[str, "PipelineVariable"] = None,
include_local_workdir: bool = None,
custom_file_filter: Optional[Union[Callable[[str, List], List], CustomFileFilter]] = None,
instance_count: Union[int, "PipelineVariable"] = 1,
instance_type: Union[str, "PipelineVariable"] = None,
job_conda_env: Union[str, "PipelineVariable"] = None,
job_name_prefix: str = None,
keep_alive_period_in_seconds: Union[int, "PipelineVariable"] = 0,
max_retry_attempts: Union[int, "PipelineVariable"] = 1,
max_runtime_in_seconds: Union[int, "PipelineVariable"] = 24 * 60 * 60,
role: str = None,
s3_kms_key: Union[str, "PipelineVariable"] = None,
s3_root_uri: str = None,
sagemaker_session: Session = None,
security_group_ids: List[Union[str, "PipelineVariable"]] = None,
subnets: List[Union[str, "PipelineVariable"]] = None,
tags: Optional[Tags] = None,
volume_kms_key: Union[str, "PipelineVariable"] = None,
volume_size: Union[int, "PipelineVariable"] = 30,
encrypt_inter_container_traffic: Union[bool, "PipelineVariable"] = None,
spark_config: SparkConfig = None,
use_spot_instances=False,
max_wait_time_in_seconds=None,
use_torchrun: bool = False,
use_mpirun: bool = False,
nproc_per_node: Optional[int] = None,
):
"""Initialize a _JobSettings instance which configures the remote job.
Args:
dependencies (str): Either the path to a dependencies file or the reserved keyword
``auto_capture``. Defaults to ``None``.
If ``dependencies`` is provided, the value must be one of the following:
* A path to a conda environment.yml file. The following conditions apply.
* If job_conda_env is set, then the conda environment is updated by installing
dependencies from the yaml file and the function is invoked within that
conda environment. For this to succeed, the specified conda environment must
already exist in the image.
* If the environment variable ``SAGEMAKER_JOB_CONDA_ENV`` is set in the image,
then the conda environment is updated by installing dependencies from the
yaml file and the function is invoked within that conda environment. For
this to succeed, the conda environment name must already be set in
``SAGEMAKER_JOB_CONDA_ENV``, and ``SAGEMAKER_JOB_CONDA_ENV`` must already
exist in the image.
* If none of the previous conditions are met, a new conda environment named
``sagemaker-runtime-env`` is created and the function annotated with the remote
decorator is invoked in that conda environment.
* A path to a requirements.txt file. The following conditions apply.
* If ``job_conda_env`` is set in the remote decorator, dependencies are installed
within that conda environment and the function annotated with the remote decorator
is invoked in the same conda environment. For this to succeed, the specified
conda environment must already exist in the image.
* If an environment variable ``SAGEMAKER_JOB_CONDA_ENV`` is set in the image,
dependencies are installed within that conda environment and the function
annotated with the remote decorator is invoked in the same. For this to succeed,
the conda environment name must already be set in ``SAGEMAKER_JOB_CONDA_ENV``, and
``SAGEMAKER_JOB_CONDA_ENV`` must already exist in the image.
* If none of the above conditions are met, conda is not used. Dependencies are
installed at the system level, without any virtual environment, and the function
annotated with the remote decorator is invoked using the Python runtime available
in the system path.
* The parameter dependencies is set to ``auto_capture``. SageMaker will automatically
generate an env_snapshot.yml corresponding to the current active conda environment’s
snapshot. You do not need to provide a dependencies file. The following conditions
apply:
* You must run the remote function within an active conda environment.
* When installing the dependencies on the training job, the same conditions
as when dependencies is set to a path to a conda environment file apply.
These conditions are as follows:
* If job_conda_env is set, then the conda environment is updated by installing
dependencies from the yaml file and the function is invoked within that
conda environment. For this to succeed, the specified conda environment must
already exist in the image.
* If the environment variable ``SAGEMAKER_JOB_CONDA_ENV`` is set in the image,
then the conda environment is updated by installing dependencies from the yaml
file and the function is invoked within that conda environment. For this to
succeed, the conda environment name must already be set in
``SAGEMAKER_JOB_CONDA_ENV``, and ``SAGEMAKER_JOB_CONDA_ENV`` must already exist
in the image.
* If none of the previous conditions are met, a new conda environment with name
``sagemaker-runtime-env`` is created and the function annotated with the
remote decorator is invoked in that conda environment.
* ``None``. SageMaker will assume that there are no dependencies to install while
executing the remote annotated function in the training job.
pre_execution_commands (List[str]): List of commands to be executed prior to executing
remote function. Only one of ``pre_execution_commands`` or ``pre_execution_script``
can be specified at the same time. Defaults to None.
pre_execution_script (str): Path to script file to be executed prior to executing
remote function. Only one of ``pre_execution_commands`` or ``pre_execution_script``
can be specified at the same time. Defaults to None.
environment_variables (dict[str, str] or dict[str, PipelineVariable]): The environment
variables used inside the decorator function. Defaults to ``None``.
image_uri (str, PipelineVariable): The universal resource identifier (URI) location of
a Docker image on Amazon Elastic Container Registry (ECR). Defaults to the following
based on where the SDK is running:
* For users who specify ``spark_config`` and want to run the function in a Spark
application, the ``image_uri`` should be ``None``. A SageMaker Spark image will
be used for training, otherwise a ``ValueError`` is thrown.
* For users on SageMaker Studio notebooks, the image used as the kernel image for
the notebook is used.
* For other users, it is resolved to base python image with the same python version
as the environment running the local code.
If no compatible image is found, a ValueError is thrown.
include_local_workdir (bool): A flag to indicate that the remote function should include
local directories. Set to ``True`` if the remote function code imports local modules
and methods that are not available via PyPI or conda. Default value is ``False``.
custom_file_filter (Callable[[str, List], List], CustomFileFilter): Either a function
that filters job dependencies to be uploaded to S3 or a ``CustomFileFilter`` object
that specifies the local directories and files to be included in the remote function.
If a callable is passed in, that function is passed to the ``ignore`` argument of
``shutil.copytree``. Defaults to ``None``, which means only python
files are accepted and uploaded to S3.
instance_count (int, PipelineVariable): The number of instances to use. Defaults to 1.
instance_type (str, PipelineVariable): The Amazon Elastic Compute Cloud (EC2) instance
type to use to run the SageMaker job. e.g. ml.c4.xlarge. If not provided,
a ValueError is thrown.
job_conda_env (str, PipelineVariable): The name of the conda environment to activate
during job's runtime. Defaults to ``None``.
job_name_prefix (str, PipelineVariable): The prefix used to create the underlying
SageMaker job.
keep_alive_period_in_seconds (int, PipelineVariable): The duration in seconds to retain
and reuse provisioned infrastructure after the completion of a training job, also
known as SageMaker managed warm pools. The use of warm pools reduces the latency time
spent to provision new resources. The default value for
``keep_alive_period_in_seconds`` is 0.
NOTE: Additional charges associated with warm pools may apply. Using this parameter
also activates a new persistent cache feature, which will further reduce job start up
latency than over using SageMaker managed warm pools alone by caching the package
source downloaded in the previous runs.
max_retry_attempts (int, PipelineVariable): The max number of times the job is retried
on ``InternalServerFailure`` Error from SageMaker service. Defaults to 1.
max_runtime_in_seconds (int, PipelineVariable): The upper limit in seconds to be used
for training. After this specified amount of time, SageMaker terminates the job
regardless of its current status. Defaults to 1 day or (86400 seconds).
role (str): The IAM role (either name or full ARN) used to run your SageMaker training
job. Defaults to:
* the SageMaker default IAM role if the SDK is running in SageMaker Notebooks or
SageMaker Studio Notebooks.
* if not above, a ValueError is thrown.
s3_kms_key (str): The key used to encrypt the input and output data.
Default to ``None``.
s3_root_uri (str): The root S3 folder to which the code archives and data are
uploaded to. Defaults to ``s3://<sagemaker-default-bucket>``.
sagemaker_session (sagemaker.session.Session): The underlying SageMaker session to
which SageMaker service calls are delegated to (default: None). If not provided,
one is created using a default configuration chain.
security_group_ids (List[str, PipelineVariable]): A list of security group IDs.
Defaults to ``None`` and the training job is created without VPC config.
subnets (List[str, PipelineVariable]): A list of subnet IDs. Defaults to ``None``
and the job is created without VPC config.
tags (Optional[Tags]): Tags attached to the job. Defaults to ``None``
and the training job is created without tags.
volume_kms_key (str, PipelineVariable): An Amazon Key Management Service (KMS) key
used to encrypt an Amazon Elastic Block Storage (EBS) volume attached to the
training instance. Defaults to ``None``.
volume_size (int, PipelineVariable): The size in GB of the storage volume for storing
input and output data during training. Defaults to ``30``.
encrypt_inter_container_traffic (bool, PipelineVariable): A flag that specifies
whether traffic between training containers is encrypted for the training job.
Defaults to ``False``.
spark_config (SparkConfig): Configurations to the Spark application that runs on
Spark image. If ``spark_config`` is specified, a SageMaker Spark image uri
will be used for training. Note that ``image_uri`` can not be specified at the
same time otherwise a ``ValueError`` is thrown. Defaults to ``None``.
use_spot_instances (bool, PipelineVariable): Specifies whether to use SageMaker
Managed Spot instances for training. If enabled then the ``max_wait`` arg should
also be set. Defaults to ``False``.
max_wait_time_in_seconds (int): Timeout in seconds waiting for spot training job.
After this amount of time Amazon SageMaker will stop waiting for managed spot
training job to complete. Defaults to ``None``.
use_torchrun (bool): Specifies whether to use torchrun for distributed training.
Defaults to ``False``.
use_mpirun (bool): Specifies whether to use mpirun for distributed training.
Defaults to ``False``.
nproc_per_node (Optional int): Specifies the number of processes per node for
distributed training. Defaults to ``None``.
This is defined automatically configured on the instance type.
"""
self.sagemaker_session = sagemaker_session or Session()
self.environment_variables = resolve_value_from_config(
direct_input=environment_variables,
config_path=REMOTE_FUNCTION_ENVIRONMENT_VARIABLES,
default_value={},
sagemaker_session=self.sagemaker_session,
)
self.environment_variables.update(
{"AWS_DEFAULT_REGION": self.sagemaker_session.boto_region_name}
)
# The following will be overridden by the _Job.compile method.
# However, it needs to be kept here for feature store SDK.
# TODO: update the feature store SDK to set the HMAC key there.
self.environment_variables.update({"REMOTE_FUNCTION_SECRET_KEY": secrets.token_hex(32)})
if spark_config and image_uri:
raise ValueError("spark_config and image_uri cannot be specified at the same time!")
if spark_config and job_conda_env:
raise ValueError("Remote Spark jobs do not support job_conda_env.")
if spark_config and dependencies == "auto_capture":
raise ValueError(
"Remote Spark jobs do not support automatically capturing dependencies."
)
_image_uri = resolve_value_from_config(
direct_input=image_uri,
config_path=REMOTE_FUNCTION_IMAGE_URI,
sagemaker_session=self.sagemaker_session,
)
if spark_config:
self.image_uri = self._get_default_spark_image(self.sagemaker_session)
logger.info(
"Set the image uri as %s because value of spark_config is "
"indicating this is a remote spark job.",
self.image_uri,
)
elif _image_uri:
self.image_uri = _image_uri
else:
self.image_uri = self._get_default_image(self.sagemaker_session)
self.dependencies = resolve_value_from_config(
direct_input=dependencies,
config_path=REMOTE_FUNCTION_DEPENDENCIES,
sagemaker_session=self.sagemaker_session,
)
self.pre_execution_commands = resolve_value_from_config(
direct_input=pre_execution_commands,
config_path=REMOTE_FUNCTION_PRE_EXECUTION_COMMANDS,
sagemaker_session=self.sagemaker_session,
)
self.pre_execution_script = resolve_value_from_config(
direct_input=pre_execution_script,
config_path=REMOTE_FUNCTION_PRE_EXECUTION_SCRIPT,
sagemaker_session=self.sagemaker_session,
)
if self.pre_execution_commands is not None and self.pre_execution_script is not None:
raise ValueError(
"Only one of pre_execution_commands or pre_execution_script can be specified!"
)
self.include_local_workdir = resolve_value_from_config(
direct_input=include_local_workdir,
config_path=REMOTE_FUNCTION_INCLUDE_LOCAL_WORKDIR,
default_value=False,
sagemaker_session=self.sagemaker_session,
)
self.custom_file_filter = resolve_custom_file_filter_from_config_file(
custom_file_filter, self.sagemaker_session
)
self.instance_type = resolve_value_from_config(
direct_input=instance_type,
config_path=REMOTE_FUNCTION_INSTANCE_TYPE,
sagemaker_session=self.sagemaker_session,
)
if not self.instance_type:
raise ValueError("instance_type is a required parameter!")
self.instance_count = instance_count
self.volume_size = volume_size
self.max_runtime_in_seconds = max_runtime_in_seconds
self.max_retry_attempts = max_retry_attempts
self.keep_alive_period_in_seconds = keep_alive_period_in_seconds
self.spark_config = spark_config
self.use_spot_instances = use_spot_instances
self.max_wait_time_in_seconds = max_wait_time_in_seconds
self.job_conda_env = resolve_value_from_config(
direct_input=job_conda_env,
config_path=REMOTE_FUNCTION_JOB_CONDA_ENV,
sagemaker_session=self.sagemaker_session,
)
self.job_name_prefix = job_name_prefix
self.encrypt_inter_container_traffic = resolve_value_from_config(
direct_input=encrypt_inter_container_traffic,
config_path=REMOTE_FUNCTION_ENABLE_INTER_CONTAINER_TRAFFIC_ENCRYPTION,
default_value=False,
sagemaker_session=self.sagemaker_session,
)
self.enable_network_isolation = False
_role = resolve_value_from_config(
direct_input=role,
config_path=REMOTE_FUNCTION_ROLE_ARN,
sagemaker_session=self.sagemaker_session,
)
if _role:
self.role = self.sagemaker_session.expand_role(_role)
else:
self.role = get_execution_role(self.sagemaker_session)
self.s3_root_uri = resolve_value_from_config(
direct_input=s3_root_uri,
config_path=REMOTE_FUNCTION_S3_ROOT_URI,
default_value=s3_path_join(
"s3://",
self.sagemaker_session.default_bucket(),
self.sagemaker_session.default_bucket_prefix,
),
sagemaker_session=self.sagemaker_session,
)
self.s3_kms_key = resolve_value_from_config(
direct_input=s3_kms_key,
config_path=REMOTE_FUNCTION_S3_KMS_KEY_ID,
sagemaker_session=self.sagemaker_session,
)
self.volume_kms_key = resolve_value_from_config(
direct_input=volume_kms_key,
config_path=REMOTE_FUNCTION_VOLUME_KMS_KEY_ID,
sagemaker_session=self.sagemaker_session,
)
_subnets = resolve_value_from_config(
direct_input=subnets,
config_path=REMOTE_FUNCTION_VPC_CONFIG_SUBNETS,
sagemaker_session=self.sagemaker_session,
)
_security_group_ids = resolve_value_from_config(
direct_input=security_group_ids,
config_path=REMOTE_FUNCTION_VPC_CONFIG_SECURITY_GROUP_IDS,
sagemaker_session=self.sagemaker_session,
)
vpc_config = vpc_utils.to_dict(subnets=_subnets, security_group_ids=_security_group_ids)
self.vpc_config = vpc_utils.sanitize(vpc_config)
tags = format_tags(tags)
self.tags = self.sagemaker_session._append_sagemaker_config_tags(tags, REMOTE_FUNCTION_TAGS)
self.use_torchrun = use_torchrun
self.use_mpirun = use_mpirun
self.nproc_per_node = nproc_per_node
@staticmethod
def _get_default_image(session):
"""Return Studio notebook image, if in Studio env. Else, base python.
Args:
session (Session): Boto session.
Returns:
Default SageMaker base python image.
"""
if (
"SAGEMAKER_INTERNAL_IMAGE_URI" in os.environ
and os.environ["SAGEMAKER_INTERNAL_IMAGE_URI"]
):
return os.environ["SAGEMAKER_INTERNAL_IMAGE_URI"]
py_version = str(sys.version_info[0]) + str(sys.version_info[1])
if py_version not in ["310", "38"]:
raise ValueError(
"Default image is supported only for Python versions 3.8 and 3.10. If you "
"are using any other python version, you must provide a compatible image_uri."
)
region = session.boto_region_name
image_uri = get_base_python_image_uri(region=region, py_version=py_version)
return image_uri
@staticmethod
def _get_default_spark_image(session):
"""Return the Spark image.
Args:
session (Session): Boto session.
Returns:
SageMaker Spark container image uri.
"""
region = session.boto_region_name
py_version = str(sys.version_info[0]) + str(sys.version_info[1])
if py_version not in ["39"]:
raise ValueError(
"The SageMaker Spark image for remote job only supports Python version 3.9. "
)
image_uri = image_uris.retrieve(
framework=defaults.SPARK_NAME,
region=region,
version=DEFAULT_SPARK_VERSION,
instance_type=None,
py_version=f"py{py_version}",
container_version=DEFAULT_SPARK_CONTAINER_VERSION,
)
return image_uri
class _Job:
"""Helper class that interacts with the SageMaker training service."""
def __init__(self, job_name: str, s3_uri: str, sagemaker_session: Session, hmac_key: str):
"""Initialize a _Job object.
Args:
job_name (str): The training job name.
s3_uri (str): The training job output S3 uri.
sagemaker_session (Session): SageMaker boto session.
hmac_key (str): Remote function secret key.
"""
self.job_name = job_name
self.s3_uri = s3_uri
self.sagemaker_session = sagemaker_session
self.hmac_key = hmac_key
self._last_describe_response = None
@staticmethod
def from_describe_response(describe_training_job_response, sagemaker_session):
"""Construct a _Job from a describe_training_job_response object.
Args:
describe_training_job_response (Dict): Describe training job response.
sagemaker_session (Session): SageMaker boto session.
Returns:
the _Job object.
"""
job_name = describe_training_job_response["TrainingJobName"]
s3_uri = describe_training_job_response["OutputDataConfig"]["S3OutputPath"]
hmac_key = describe_training_job_response["Environment"]["REMOTE_FUNCTION_SECRET_KEY"]
job = _Job(job_name, s3_uri, sagemaker_session, hmac_key)
job._last_describe_response = describe_training_job_response
return job
@staticmethod
def start(job_settings: _JobSettings, func, func_args, func_kwargs, run_info=None):
"""Start a training job.
Args:
job_settings (_JobSettings): the job settings.
func: the function to be executed.
func_args: the positional arguments to the function.
func_kwargs: the keyword arguments to the function
Returns:
the _Job object.
"""
job_name = _Job._get_job_name(job_settings, func)
s3_base_uri = s3_path_join(job_settings.s3_root_uri, job_name)
training_job_request = _Job.compile(
job_settings=job_settings,
job_name=job_name,
s3_base_uri=s3_base_uri,
func=func,
func_args=func_args,
func_kwargs=func_kwargs,
run_info=run_info,
)
logger.info("Creating job: %s", job_name)
job_settings.sagemaker_session.sagemaker_client.create_training_job(**training_job_request)
return _Job(
job_name,
s3_base_uri,
job_settings.sagemaker_session,
training_job_request["Environment"]["REMOTE_FUNCTION_SECRET_KEY"],
)
@staticmethod
def compile(
job_settings: _JobSettings,
job_name: str,
s3_base_uri: str,
func: callable,
func_args: tuple,
func_kwargs: dict,
run_info=None,
serialized_data: _SerializedData = None,
) -> dict:
"""Build the artifacts and generate the training job request."""
from sagemaker.workflow.properties import Properties
from sagemaker.workflow.parameters import Parameter
from sagemaker.workflow.functions import Join
from sagemaker.workflow.execution_variables import ExecutionVariables, ExecutionVariable
from sagemaker.workflow.utilities import load_step_compilation_context
step_compilation_context = load_step_compilation_context()
jobs_container_entrypoint = JOBS_CONTAINER_ENTRYPOINT[:]
# generate hmac key for integrity check
if step_compilation_context is None:
hmac_key = secrets.token_hex(32)
else:
hmac_key = step_compilation_context.function_step_secret_token
# serialize function and arguments
if step_compilation_context is None:
stored_function = StoredFunction(
sagemaker_session=job_settings.sagemaker_session,
s3_base_uri=s3_base_uri,
hmac_key=hmac_key,
s3_kms_key=job_settings.s3_kms_key,
)
stored_function.save(func, *func_args, **func_kwargs)
else:
stored_function = StoredFunction(
sagemaker_session=job_settings.sagemaker_session,
s3_base_uri=s3_base_uri,
hmac_key=hmac_key,
s3_kms_key=job_settings.s3_kms_key,
context=Context(
step_name=step_compilation_context.step_name,
func_step_s3_dir=step_compilation_context.pipeline_build_time,
),
)
stored_function.save_pipeline_step_function(serialized_data)
stopping_condition = {
"MaxRuntimeInSeconds": job_settings.max_runtime_in_seconds,
}
if job_settings.max_wait_time_in_seconds is not None:
stopping_condition["MaxWaitTimeInSeconds"] = job_settings.max_wait_time_in_seconds
request_dict = dict(
TrainingJobName=job_name,
RoleArn=job_settings.role,
StoppingCondition=stopping_condition,
RetryStrategy={"MaximumRetryAttempts": job_settings.max_retry_attempts},
)
_update_job_request_with_checkpoint_config(func_args, func_kwargs, request_dict)
if job_settings.tags:
request_dict["Tags"] = job_settings.tags
# generate other build artifacts including workspace, requirements.txt
request_dict["InputDataConfig"] = _generate_input_data_config(
job_settings=job_settings, s3_base_uri=s3_base_uri
)
if step_compilation_context:
s3_output_path = Join(
on="/",
values=[
s3_base_uri,
ExecutionVariables.PIPELINE_EXECUTION_ID,
step_compilation_context.step_name,
"results",
],
)
output_config = {"S3OutputPath": s3_output_path}
else:
output_config = {"S3OutputPath": s3_base_uri}
if job_settings.s3_kms_key is not None:
output_config["KmsKeyId"] = job_settings.s3_kms_key
request_dict["OutputDataConfig"] = output_config
container_args = ["--s3_base_uri", s3_base_uri]
container_args.extend(["--region", job_settings.sagemaker_session.boto_region_name])
container_args.extend(
["--client_python_version", RuntimeEnvironmentManager()._current_python_version()]
)
container_args.extend(
[
"--client_sagemaker_pysdk_version",
RuntimeEnvironmentManager()._current_sagemaker_pysdk_version(),
]
)
container_args.extend(
[
"--dependency_settings",
_DependencySettings.from_dependency_file_path(
job_settings.dependencies
).to_string(),
]
)
if job_settings.use_torchrun:
container_args.extend(["--distribution", "torchrun"])
elif job_settings.use_mpirun:
container_args.extend(["--distribution", "mpirun"])
if job_settings.nproc_per_node is not None and int(job_settings.nproc_per_node) > 0:
container_args.extend(["--user_nproc_per_node", str(job_settings.nproc_per_node)])
if job_settings.s3_kms_key:
container_args.extend(["--s3_kms_key", job_settings.s3_kms_key])
if job_settings.job_conda_env:
container_args.extend(["--job_conda_env", job_settings.job_conda_env])
if step_compilation_context:
# TODO: remove the duplicates in the list
container_args.extend(["--pipeline_step_name", step_compilation_context.step_name])
container_args.extend(
["--pipeline_execution_id", ExecutionVariables.PIPELINE_EXECUTION_ID]
)
container_args.extend(
["--func_step_s3_dir", step_compilation_context.pipeline_build_time]
)
container_args.extend(["--property_references"])
container_args.extend(