-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtemplates.py
More file actions
117 lines (97 loc) · 3.13 KB
/
templates.py
File metadata and controls
117 lines (97 loc) · 3.13 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
# 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.
"""Templates module."""
from __future__ import absolute_import
EXECUTE_BASE_COMMANDS = """
CMD="{base_command}"
echo "Executing command: $CMD"
eval $CMD
"""
EXECUTE_BASIC_SCRIPT_DRIVER = """
echo "Running Basic Script driver"
$SM_PYTHON_CMD /opt/ml/input/data/sm_drivers/distributed_drivers/basic_script_driver.py
"""
INSTALL_AUTO_REQUIREMENTS = """
if [ -f requirements.txt ]; then
echo "Installing requirements"
cat requirements.txt
$SM_PIP_CMD install -r requirements.txt
else
echo "No requirements.txt file found. Skipping installation."
fi
"""
INSTALL_REQUIREMENTS = """
echo "Installing requirements"
$SM_PIP_CMD install -r {requirements_file}
"""
INSTALL_DEPENDENCIES = """
echo "Setting up additional dependencies"
if [ -d /opt/ml/input/data/sm_dependencies ]; then
for dep_dir in /opt/ml/input/data/sm_dependencies/*/; do
if [ -d "$dep_dir" ]; then
echo "Adding $dep_dir to PYTHONPATH"
export PYTHONPATH="$dep_dir:$PYTHONPATH"
fi
done
# Also add the root dependencies dir in case of single files
export PYTHONPATH="/opt/ml/input/data/sm_dependencies:$PYTHONPATH"
fi
"""
EXEUCTE_DISTRIBUTED_DRIVER = """
echo "Running {driver_name} Driver"
$SM_PYTHON_CMD /opt/ml/input/data/sm_drivers/distributed_drivers/{driver_script}
"""
TRAIN_SCRIPT_TEMPLATE = """
#!/bin/bash
set -e
echo "Starting training script"
handle_error() {{
EXIT_STATUS=$?
echo "An error occurred with exit code $EXIT_STATUS"
if [ ! -s /opt/ml/output/failure ]; then
echo "Training Execution failed. For more details, see CloudWatch logs at 'aws/sagemaker/TrainingJobs'.
TrainingJob - $TRAINING_JOB_NAME" >> /opt/ml/output/failure
fi
exit $EXIT_STATUS
}}
check_python() {{
SM_PYTHON_CMD=$(command -v python3 || command -v python)
SM_PIP_CMD=$(command -v pip3 || command -v pip)
# Check if Python is found
if [[ -z "$SM_PYTHON_CMD" || -z "$SM_PIP_CMD" ]]; then
echo "Error: The Python executable was not found in the system path."
return 1
fi
return 0
}}
trap 'handle_error' ERR
check_python
set -x
$SM_PYTHON_CMD --version
echo "/opt/ml/input/config/resourceconfig.json:"
cat /opt/ml/input/config/resourceconfig.json
echo
echo "/opt/ml/input/config/inputdataconfig.json:"
cat /opt/ml/input/config/inputdataconfig.json
echo
echo "Setting up environment variables"
$SM_PYTHON_CMD /opt/ml/input/data/sm_drivers/scripts/environment.py
set +x
source /opt/ml/input/sm_training.env
set -x
{working_dir}
{install_dependencies}
{install_requirements}
{execute_driver}
echo "Training Container Execution Completed"
"""