-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutils.sh
More file actions
170 lines (159 loc) · 5.62 KB
/
utils.sh
File metadata and controls
170 lines (159 loc) · 5.62 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
#!/bin/bash -eu
function wait_till_instance_not_exist() {
local INSTANCE_NAME=$1
local ZONE=$2
if [ "$#" -ne 2 ]; then
echo "Usage: "
echo " ./wait_till_instance_not_exist [INSTANCE_NAME] [ZONE]"
echo ""
echo "example:"
echo " ./wait_till_instance_not_exist instance1 us-west1-b"
echo ""
return 1
fi
gcloud compute instances tail-serial-port-output "${INSTANCE_NAME}" --zone="${ZONE}" || true
return 0
}
function execute_notebook() {
local ZONE="us-west1-b"
local IMAGE_FAMILY="tf-latest-gpu"
local INPUT_NOTEBOOK=""
local GPU_TYPE=""
local GPU_COUNT=1
local INSTANCE_TYPE="n1-standard-8"
local GCS_LOCATION=""
local BUILD_ID=$(date +%s%5N)
local CUSTOM_META_DATA=""
local PARAM_FILE=""
local OPTIND opt
while getopts "i:z:f:g:c:t:l:o:m:p:h" opt; do
case ${opt} in
i )
INPUT_NOTEBOOK=$OPTARG
;;
z )
ZONE=$OPTARG
;;
f )
IMAGE_FAMILY=$OPTARG
;;
g )
GPU_TYPE=$OPTARG
;;
c )
GPU_COUNT=$OPTARG
;;
t )
INSTANCE_TYPE=$OPTARG
;;
o )
GCS_LOCATION=$OPTARG
;;
m )
CUSTOM_META_DATA=$OPTARG
;;
p )
PARAM_FILE=$OPTARG
;;
h )
echo "Usage: "
echo " ./execute_notebook -i [INPUT_NOTEBOOK] -o [GCS_OUTPUT_LOCATION] -g [GPU_TYPE] -c [GPU_COUNT] -z [ZONE] -t [INSTANCE_TYPE] -f [IMAGE_FAMILY] -m [METAD_DATA_KEY]=[VALUE] -p [PARAM_FILE]"
echo ""
echo "example:"
echo " ./execute_notebook -i test.ipynb -o gs://my-bucket -g p100 -c 4 -z us-west1-b -t n1-standard-8 -f tf-latest-gpu -m mykey=myvalue -p ./params"
echo ""
echo "default values:"
echo " gpu type: empty (no GPU will be used for training)"
echo " gpu count: 1"
echo " image family: tf-latest-gpu"
echo " zone: us-west1-b"
echo " instance-type: n1-standard-8"
echo ""
echo "required keys:"
echo " i, o"
echo ""
return 1
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
return 1
;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
return 1
;;
esac
done
shift $((OPTIND -1))
echo "Excution of the notebook initiated with the following input arguments: $@"
echo "Build id: ${BUILD_ID}"
if [[ -z "${INPUT_NOTEBOOK}" ]]; then
echo "input notebook field is empty (use key -i)"
return 1
fi
local NOTEBOOK_NAME=$(basename "${INPUT_NOTEBOOK}")
if [[ -z "${GCS_LOCATION}" ]]; then
echo "GCS output location field is empty (use key -o)"
return 1
fi
local INPUT_NOTEBOOK_GCS_PATH="${GCS_LOCATION}/staging/${BUILD_ID}/${NOTEBOOK_NAME}"
if [[ ! -z ${PARAM_FILE} ]]; then
INPUT_PARAM_GCS_PATH="${GCS_LOCATION}/staging/${BUILD_ID}/params.yaml"
gsutil cp "${PARAM_FILE}" "${INPUT_PARAM_GCS_PATH}"
PARAM_METADATA=",parameters_file=${INPUT_PARAM_GCS_PATH}"
fi
OUTPUT_CONTENTS=$(gsutil ls "${GCS_LOCATION}")
if [[ $? -ne 0 ]] || grep -q "FAILED" <<< "${OUTPUT_CONTENTS}"; then
echo "FAILED marker file exist from past executions, deleting."
gsutil rm "${GCS_LOCATION}/FAILED"
fi
echo "Staging notebook: ${INPUT_NOTEBOOK_GCS_PATH}"
echo "Output notebook: ${GCS_LOCATION}"
gsutil cp "${INPUT_NOTEBOOK}" "${INPUT_NOTEBOOK_GCS_PATH}"
if [[ $? -eq 1 ]]; then
echo "Upload to the temp GCS location (${INPUT_NOTEBOOK_GCS_PATH}) of the notebook (${INPUT_NOTEBOOK}) has failed."
return 1
fi
INSTANCE_NAME="notebookexecutor-${BUILD_ID}"
META_DATA="input_notebook=${INPUT_NOTEBOOK_GCS_PATH},output_notebook=${GCS_LOCATION}${PARAM_METADATA:-},startup-script-url=https://raw.githubusercontent.com/gclouduniverse/gcp-notebook-executor/v0.1.3/notebook_executor.sh"
if [[ ! -z "${CUSTOM_META_DATA}" ]]; then
META_DATA="${META_DATA},${CUSTOM_META_DATA}"
fi
if [[ -z "${GPU_TYPE}" ]]; then
gcloud compute instances create "${INSTANCE_NAME}" \
--zone="${ZONE}" \
--image-family="${IMAGE_FAMILY}" \
--image-project=deeplearning-platform-release \
--maintenance-policy=TERMINATE \
--machine-type="${INSTANCE_TYPE}" \
--boot-disk-size=200GB \
--scopes=https://www.googleapis.com/auth/cloud-platform \
--metadata="${META_DATA}" \
--quiet
else
gcloud compute instances create "${INSTANCE_NAME}" \
--zone="${ZONE}" \
--image-family="${IMAGE_FAMILY}" \
--image-project=deeplearning-platform-release \
--maintenance-policy=TERMINATE \
--accelerator="type=nvidia-tesla-${GPU_TYPE},count=${GPU_COUNT}" \
--machine-type="${INSTANCE_TYPE}" \
--boot-disk-size=200GB \
--scopes=https://www.googleapis.com/auth/cloud-platform \
--metadata="${META_DATA}" \
--quiet
fi
if [[ $? -eq 1 ]]; then
echo "Creation of background instance for training has failed."
return 1
fi
wait_till_instance_not_exist "${INSTANCE_NAME}" "${ZONE}"
echo "execution has been finished, checking result"
OUTPUT_CONTENTS=$(gsutil ls "${GCS_LOCATION}")
if [[ $? -ne 0 ]] || grep -q "FAILED" <<< "${OUTPUT_CONTENTS}"; then
echo "Job failed or unable to get output."
return 1
fi
echo "done"
return 0
}