-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbuild.sh
executable file
·315 lines (277 loc) · 8.93 KB
/
build.sh
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
#!/usr/bin/env bash
# *******************************************************************************
# Copyright 2020 Arm Limited and affiliates.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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.
# *******************************************************************************
# Staged docker build for PyTorch
# ==================================
################################################################################
function print_usage_and_exit {
echo "Usage: build.sh [OPTIONS]"
echo ""
echo "Options:"
echo " -h, --help Display this message"
echo " --jobs Specify number of jobs to run in parallel during the build"
echo " --bazel_memory_limit Set a memory limit (MB) for Bazel build (default: 2048)."
echo " --pt_onednn Build and link to oneDNN / DNNL on PyTorch:"
echo " * reference - use the C++ reference kernels throughout."
echo " * acl - use Arm Compute Library (default)."
echo " --tf_onednn Build and link to oneDNN / DNNL on TensorFlow:"
echo " * reference - use the C++ reference kernels throughout."
echo " * acl - use Arm Compute Library (default)."
echo " --build-type Type of build to perform:"
echo " * base - build the basic portion of the image, OS and essential packages"
echo " * libs - build image including maths libraries and Python3."
echo " * tools - build image including Python3 venv, with numpy."
echo " * dev - build image including Bazel, TensorFlow and PyTorch, with sources."
echo " * coding - build image including TensorFlow and PyTorch"
echo " * examples - build image including TensorFlow and PyTorch build and benchmarks installed"
echo " * full - build all images."
echo " --clean Pull a new base image and build without using any cached images."
echo ""
echo "Example:"
echo " build.sh --build-type full"
exit $1
}
################################################################################
cpu="native"
tune="native"
arch="native"
blas_cpu=
blas_ncores=
acl_arch="arm64-v8a"
# Enable Buildkit
# Required for advanced multi-stage builds
# Requires Docker v 18.09.1
export DOCKER_BUILDKIT=1
# Default build flags
build_base_image=
build_libs_image=
build_tools_image=
build_dev_image=
build_coding_image=1
build_examples_image=
readonly target_arch="aarch64"
readonly host_arch=$(arch)
if ! [ "$host_arch" == "$target_arch" -o "$host_arch" == "arm64" ]; then
echo "Error: $(arch) is not supported"
print_usage_and_exit 1
fi
# Default args
extra_args=""
nproc_build=
bazel_mem="2048"
enable_onednn=0
pt_onednn=
tf_onednn=
target="native"
clean_build=
xla=
while [ $# -gt 0 ]
do
case $1 in
--build-type )
case $2 in
base )
build_base_image=1
build_libs_image=
build_tools_image=
build_dev_image=
build_coding_image=
build_examples_image=
;;
libs )
build_base_image=
build_libs_image=1
build_tools_image=
build_dev_image=
build_coding_image=
build_examples_image=
;;
tools )
build_base_image=
build_libs_image=
build_tools_image=1
build_dev_image=
build_coding_image=
build_examples_image=
;;
dev )
build_base_image=
build_libs_image=
build_tools_image=
build_dev_image=1
build_coding_image=
build_examples_image=
;;
coding )
build_base_image=
build_libs_image=
build_tools_image=
build_dev_image=
build_coding_image=1
build_examples_image=
;;
full )
build_base_image=1
build_libs_image=1
build_tools_image=1
build_dev_image=1
build_coding_image=1
build_examples_image=1
;;
examples )
build_base_image=
build_libs_image=
build_tools_image=
build_dev_image=
build_coding_image=
build_examples_image=1
;;
* )
echo "Error: $2 is an invalid build type!"
print_usage_and_exit 1
;;
esac
shift
;;
--jobs )
nproc_build=$2
shift
;;
--bazel_memory_limit )
bazel_mem=$2
shift
;;
--pt_onednn )
if [[ $# -gt 1 ]]; then
case $2 in
reference )
pt_onednn="reference"
shift
;;
acl )
pt_onednn="acl"
shift
;;
* )
echo "Defaulting to oneDNN-ACL build."
echo "Note: support for oneDNN builds with OpenBLAS or ArmPL is now deprecated."
pt_onednn="acl"
;;
esac
else
pt_onednn="acl"
fi
;;
--tf_onednn )
enable_onednn=1
if [[ $# -gt 1 ]]; then
case $2 in
reference )
tf_onednn="reference"
shift
;;
acl )
tf_onednn="acl"
shift
;;
* )
echo "Defaulting to oneDNN-ACL build."
echo "Note: support for oneDNN builds with OpenBLAS or ArmPL is now deprecated."
tf_onednn="acl"
;;
esac
else
tf_onednn="acl"
fi
;;
--clean )
clean_build=1
;;
-h | --help )
print_usage_and_exit 0
;;
esac
shift
done
# exec > >(tee -i build.log)
# exec 2>&1
if [[ $nproc_build ]]; then
# Set -j to use for builds, if specified
extra_args="$extra_args --build-arg njobs=$nproc_build"
fi
if [[ $bazel_mem ]]; then
# Set -j to use for builds, if specified
extra_args="$extra_args --build-arg bazel_mem=$bazel_mem"
fi
if [[ $pt_onednn ]]; then
# Use oneDNN backend
extra_args="--build-arg pt_onednn_opt=$pt_onednn $extra_args"
fi
# Add oneDNN build options
if [[ $tf_onednn ]]; then
extra_args="--build-arg tf_onednn_opt=$tf_onednn $extra_args --build-arg enable_onednn=$enable_onednn"
fi
if [[ $clean_build ]]; then
# Pull a new base image, and don't use any caches
extra_args="--pull --no-cache $extra_args"
fi
if [[ $xla ]]; then
# Build xla backend
extra_args="--build-arg build_xla=$xla $extra_args"
fi
# Set TensorFlow
tf_version="v2.9.1"
tfserving_version="2.7.0"
extra_args="$extra_args \
--build-arg tf_version=$tf_version \
--build-arg tfserving_version=$tfserving_version"
image_tag="pytorch1.12.0_tensorflow2.9.1"
extra_args="$extra_args --build-arg cpu=$cpu \
--build-arg tune=$tune \
--build-arg arch=$arch \
--build-arg blas_cpu=$blas_cpu \
--build-arg blas_ncores=$blas_ncores \
--build-arg eigen_l1_cache= \
--build-arg eigen_l2_cache= \
--build-arg eigen_l3_cache= \
--build-arg acl_arch=$acl_arch \
--build-arg image_tag=$image_tag"
echo $extra_args
if [[ $build_base_image ]]; then
# Stage 1: Base image, Ubuntu with core packages and GCC
docker build $extra_args --target deep-learning-base -t deep-learning-base:$image_tag .
fi
if [[ $build_libs_image ]]; then
# Stage 2: Libs image, essential maths libs and Python built and installed
docker build $extra_args --target deep-learning-libs -t deep-learning-libs:$image_tag .
fi
if [[ $build_tools_image ]]; then
# Stage 3: Tools image, Python3 venv added with additional Python essentials
docker build $extra_args --target deep-learning-tools -t deep-learning-tools:$image_tag .
fi
if [[ $build_dev_image ]]; then
# Stage 4: Adds TensorFlow and Pytorch build with sources
docker build $extra_args --target deep-learning-dev -t deep-learning-dev:$image_tag .
fi
if [[ $build_coding_image ]]; then
# Stage 5: Setup Coding Environment
docker build $extra_args --target deep-learning-coding -t deep-learning-coding:$image_tag .
fi
if [[ $build_examples_image ]]; then
# Stage 6: Adds Deep Learning examples
docker build $extra_args --target deep-learning-examples -t deep-learning-examples:$image_tag .
fi