Skip to content

Commit f24bc79

Browse files
authored
[ML] Upgrade to gcc 10.3 for Linux compilation (#2028)
gcc 10.3 contains the fix for the bug that hinders compilation of PyTorch on aarch64. The binutils package used for compiling the final distribution is also upgraded, from version 2.34 to version 2.37, and the version used for bootstrapping the cross compiler from version 2.25 to version 2.27. patchelf is upgraded from version 0.10 to version 0.13.
1 parent 0934a4f commit f24bc79

File tree

22 files changed

+206
-218
lines changed

22 files changed

+206
-218
lines changed

3rd_party/3rd_party.sh

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,31 @@ case `uname` in
6161

6262
Linux)
6363
if [ -z "$CPP_CROSS_COMPILE" ] ; then
64-
BOOST_LOCATION=/usr/local/gcc93/lib
64+
BOOST_LOCATION=/usr/local/gcc103/lib
6565
BOOST_COMPILER=gcc
6666
if [ `uname -m` = aarch64 ] ; then
6767
BOOST_ARCH=a64
6868
else
6969
BOOST_ARCH=x64
70-
MKL_LOCATION=/usr/local/gcc93/lib
70+
MKL_LOCATION=/usr/local/gcc103/lib
7171
MKL_EXTENSION=.so
7272
MKL_PREFIX=libmkl_
73-
MKL_LIBRARIES=`ls $MKL_LOCATION/$MKL_PREFIX*`
73+
MKL_LIBRARIES=`cd "$MKL_LOCATION" && ls $MKL_PREFIX*$MKL_EXTENSION`
7474
fi
7575
BOOST_EXTENSION=mt-${BOOST_ARCH}-1_71.so.1.71.0
7676
BOOST_LIBRARIES='atomic chrono date_time filesystem iostreams log log_setup program_options regex system thread'
77-
XML_LOCATION=/usr/local/gcc93/lib
77+
XML_LOCATION=/usr/local/gcc103/lib
7878
XML_EXTENSION=.so.2
79-
GCC_RT_LOCATION=/usr/local/gcc93/lib64
79+
GCC_RT_LOCATION=/usr/local/gcc103/lib64
8080
GCC_RT_EXTENSION=.so.1
81-
OMP_LOCATION=/usr/local/gcc93/lib64
81+
OMP_LOCATION=/usr/local/gcc103/lib64
8282
OMP_EXTENSION=.so.1
83-
STL_LOCATION=/usr/local/gcc93/lib64
83+
STL_LOCATION=/usr/local/gcc103/lib64
8484
STL_PATTERN=libstdc++
8585
STL_EXTENSION=.so.6
8686
ZLIB_LOCATION=
8787
TORCH_LIBRARIES="torch_cpu c10"
88-
TORCH_LOCATION=/usr/local/gcc93/lib
88+
TORCH_LOCATION=/usr/local/gcc103/lib
8989
TORCH_EXTENSION=.so
9090
elif [ "$CPP_CROSS_COMPILE" = macosx ] ; then
9191
SYSROOT=/usr/local/sysroot-x86_64-apple-macosx10.14
@@ -103,7 +103,7 @@ case `uname` in
103103
TORCH_EXTENSION=.dylib
104104
else
105105
SYSROOT=/usr/local/sysroot-$CPP_CROSS_COMPILE-linux-gnu
106-
BOOST_LOCATION=$SYSROOT/usr/local/gcc93/lib
106+
BOOST_LOCATION=$SYSROOT/usr/local/gcc103/lib
107107
BOOST_COMPILER=gcc
108108
if [ "$CPP_CROSS_COMPILE" = aarch64 ] ; then
109109
BOOST_ARCH=a64
@@ -113,18 +113,18 @@ case `uname` in
113113
fi
114114
BOOST_EXTENSION=mt-${BOOST_ARCH}-1_71.so.1.71.0
115115
BOOST_LIBRARIES='atomic chrono date_time filesystem iostreams log log_setup program_options regex system thread'
116-
XML_LOCATION=$SYSROOT/usr/local/gcc93/lib
116+
XML_LOCATION=$SYSROOT/usr/local/gcc103/lib
117117
XML_EXTENSION=.so.2
118-
GCC_RT_LOCATION=$SYSROOT/usr/local/gcc93/lib64
118+
GCC_RT_LOCATION=$SYSROOT/usr/local/gcc103/lib64
119119
GCC_RT_EXTENSION=.so.1
120-
OMP_LOCATION=$SYSROOT/usr/local/gcc93/lib64
120+
OMP_LOCATION=$SYSROOT/usr/local/gcc103/lib64
121121
OMP_EXTENSION=.so.1
122-
STL_LOCATION=$SYSROOT/usr/local/gcc93/lib64
123-
STL_PREFIX=libstdc++
122+
STL_LOCATION=$SYSROOT/usr/local/gcc103/lib64
123+
STL_PATTERN=libstdc++
124124
STL_EXTENSION=.so.6
125125
ZLIB_LOCATION=
126126
TORCH_LIBRARIES="torch_cpu c10"
127-
TORCH_LOCATION=$SYSROOT/usr/local/gcc93/lib
127+
TORCH_LOCATION=$SYSROOT/usr/local/gcc103/lib
128128
TORCH_EXTENSION=.so
129129
fi
130130
;;
@@ -259,18 +259,18 @@ if [ ! -z "$TORCH_LOCATION" ] ; then
259259
fi
260260
fi
261261
if [ ! -z "$MKL_LOCATION" ] ; then
262-
if ls $MKL_LOCATION/$MKL_PREFIX*$MKL_EXTENSION >/dev/null ; then
262+
if [ ! -z "$MKL_LIBRARIES" ] ; then
263263
if [ -n "$INSTALL_DIR" ] ; then
264264
for LIBRARY in $MKL_LIBRARIES
265265
do
266-
rm -f $INSTALL_DIR/*$LIBRARY*$MKL_EXTENSION
267-
cp $LIBRARY $INSTALL_DIR
268-
chmod u+wx $INSTALL_DIR/*$LIBRARY*$MKL_EXTENSION
266+
rm -f $INSTALL_DIR/$LIBRARY
267+
cp $MKL_LOCATION/$LIBRARY $INSTALL_DIR
268+
chmod u+wx $INSTALL_DIR/$LIBRARY
269269
done
270270
fi
271271
else
272272
echo "Intel MKL libraries not found"
273-
exit 11
273+
exit 12
274274
fi
275275
fi
276276

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
name,version,revision,url,license,copyright,sourceURL
2-
GCC Runtime Library,9.3.0,4212a6a3e44f870412d9025eeb323fd4f50a61da,https://gcc.gnu.org,GPL-3.0 WITH GCC-exception-3.1,"Copyright (C) 2020 Free Software Foundation, Inc.",http://ftpmirror.gnu.org/gcc/gcc-9.3.0/gcc-9.3.0.tar.gz
2+
GCC Runtime Library,10.3.0,f00b5710a30f22efc3171c393e56aeb335c3cd39,https://gcc.gnu.org,GPL-3.0 WITH GCC-exception-3.1,"Copyright (C) 2021 Free Software Foundation, Inc.",http://ftpmirror.gnu.org/gcc/gcc-10.3.0/gcc-10.3.0.tar.gz

build-setup/linux.md

Lines changed: 45 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ You will need the following environment variables to be defined:
88

99
- `JAVA_HOME` - Should point to the JDK you want to use to run Gradle.
1010
- `CPP_SRC_HOME` - Only required if building the C++ code directly using `make`, as Gradle sets it automatically.
11-
- `PATH` - Must have `/usr/local/gcc93/bin` before `/usr/bin` and `/bin`.
12-
- `LD_LIBRARY_PATH` - Must have `/usr/local/gcc93/lib64` and `/usr/local/gcc93/lib` before `/usr/lib` and `/lib`.
11+
- `PATH` - Must have `/usr/local/gcc103/bin` before `/usr/bin` and `/bin`.
12+
- `LD_LIBRARY_PATH` - Must have `/usr/local/gcc103/lib64` and `/usr/local/gcc103/lib` before `/usr/lib` and `/lib`.
1313

1414
For example, you might create a `.bashrc` file in your home directory containing something like this:
1515

1616
```
1717
umask 0002
1818
export JAVA_HOME=/usr/local/jdk1.8.0_121
19-
export LD_LIBRARY_PATH=/usr/local/gcc93/lib64:/usr/local/gcc93/lib:/usr/lib:/lib
20-
export PATH=$JAVA_HOME/bin:/usr/local/gcc93/bin:/usr/local/cmake/bin:/usr/bin:/bin:/usr/sbin:/sbin:/home/vagrant/bin
19+
export LD_LIBRARY_PATH=/usr/local/gcc103/lib64:/usr/local/gcc103/lib:/usr/lib:/lib
20+
export PATH=$JAVA_HOME/bin:/usr/local/gcc103/bin:/usr/local/cmake/bin:/usr/bin:/bin:/usr/sbin:/sbin:/home/vagrant/bin
2121
# Only required if building the C++ code directly using make - adjust depending on the location of your Git clone
2222
export CPP_SRC_HOME=$HOME/ml-cpp
2323
```
@@ -53,22 +53,22 @@ These environment variables only need to be set when building tools on Linux. Th
5353

5454
We have to build on old Linux versions to enable our software to run on the older versions of Linux that users have. However, this means the default compiler on our Linux build servers is also very old. To enable use of more modern C++ features, we use the default compiler to build a newer version of gcc and then use that to build all our other dependencies.
5555

56-
Download `gcc-9.3.0.tar.gz` from <http://ftpmirror.gnu.org/gcc/gcc-9.3.0/gcc-9.3.0.tar.gz>.
56+
Download `gcc-10.3.0.tar.gz` from <http://ftpmirror.gnu.org/gcc/gcc-10.3.0/gcc-10.3.0.tar.gz>.
5757

5858
Unlike most automake-based tools, gcc must be built in a directory adjacent to the directory containing its source code, so build and install it like this:
5959

6060
```
61-
tar zxvf gcc-9.3.0.tar.gz
62-
cd gcc-9.3.0
61+
tar zxvf gcc-10.3.0.tar.gz
62+
cd gcc-10.3.0
6363
contrib/download_prerequisites
6464
sed -i -e 's/$(SHLIB_LDFLAGS)/-Wl,-z,relro -Wl,-z,now $(SHLIB_LDFLAGS)/' libgcc/config/t-slibgcc
6565
cd ..
66-
mkdir gcc-9.3.0-build
67-
cd gcc-9.3.0-build
66+
mkdir gcc-10.3.0-build
67+
cd gcc-10.3.0-build
6868
unset CXX
6969
unset LD_LIBRARY_PATH
7070
export PATH=/usr/bin:/bin:/usr/sbin:/sbin
71-
../gcc-9.3.0/configure --prefix=/usr/local/gcc93 --enable-languages=c,c++ --enable-vtable-verify --with-system-zlib --disable-multilib
71+
../gcc-10.3.0/configure --prefix=/usr/local/gcc103 --enable-languages=c,c++ --enable-vtable-verify --with-system-zlib --disable-multilib
7272
make -j 6
7373
sudo make install
7474
```
@@ -90,21 +90,21 @@ g++ --version
9090
It should print:
9191

9292
```
93-
g++ (GCC) 9.3.0
93+
g++ (GCC) 10.3.0
9494
```
9595

96-
in the first line of the output. If it doesn't then double check that `/usr/local/gcc93/bin` is near the beginning of your `PATH`.
96+
in the first line of the output. If it doesn't then double check that `/usr/local/gcc103/bin` is near the beginning of your `PATH`.
9797

9898
### binutils
9999

100100
Also due to building on old Linux versions yet wanting to use modern libraries we have to install an up-to-date version of binutils.
101101

102-
Download `binutils-2.34.tar.bz2` from <http://ftpmirror.gnu.org/binutils/binutils-2.34.tar.bz2>.
102+
Download `binutils-2.37.tar.bz2` from <http://ftpmirror.gnu.org/binutils/binutils-2.37.tar.bz2>.
103103

104104
Uncompress and untar the resulting file. Then run:
105105

106106
```
107-
./configure --prefix=/usr/local/gcc93 --enable-vtable-verify --with-system-zlib --disable-libstdcxx --with-gcc-major-version-only
107+
./configure --prefix=/usr/local/gcc103 --enable-vtable-verify --with-system-zlib --disable-libstdcxx --with-gcc-major-version-only
108108
```
109109

110110
This should build an appropriate Makefile. Assuming it does, type:
@@ -159,7 +159,7 @@ Anonymous FTP to ftp.xmlsoft.org, change directory to libxml2, switch to binary
159159
Uncompress and untar the resulting file. Then run:
160160

161161
```
162-
./configure --prefix=/usr/local/gcc93 --without-python --without-readline
162+
./configure --prefix=/usr/local/gcc103 --without-python --without-readline
163163
```
164164

165165
This should build an appropriate Makefile. Assuming it does, type:
@@ -205,7 +205,7 @@ Finally, run:
205205

206206
```
207207
./b2 -j6 --layout=versioned --disable-icu pch=off optimization=speed inlining=full define=BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS define=BOOST_LOG_WITHOUT_DEBUG_OUTPUT define=BOOST_LOG_WITHOUT_EVENT_LOG define=BOOST_LOG_WITHOUT_SYSLOG define=BOOST_LOG_WITHOUT_IPC define=_FORTIFY_SOURCE=2 cxxflags='-std=gnu++17 -fstack-protector -msse4.2 -mfpmath=sse' linkflags='-std=gnu++17 -Wl,-z,relro -Wl,-z,now'
208-
sudo env PATH="$PATH" LD_LIBRARY_PATH="$LD_LIBRARY_PATH" ./b2 install --prefix=/usr/local/gcc93 --layout=versioned --disable-icu pch=off optimization=speed inlining=full define=BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS define=BOOST_LOG_WITHOUT_DEBUG_OUTPUT define=BOOST_LOG_WITHOUT_EVENT_LOG define=BOOST_LOG_WITHOUT_SYSLOG define=BOOST_LOG_WITHOUT_IPC define=_FORTIFY_SOURCE=2 cxxflags='-std=gnu++17 -fstack-protector -msse4.2 -mfpmath=sse' linkflags='-std=gnu++17 -Wl,-z,relro -Wl,-z,now'
208+
sudo env PATH="$PATH" LD_LIBRARY_PATH="$LD_LIBRARY_PATH" ./b2 install --prefix=/usr/local/gcc103 --layout=versioned --disable-icu pch=off optimization=speed inlining=full define=BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS define=BOOST_LOG_WITHOUT_DEBUG_OUTPUT define=BOOST_LOG_WITHOUT_EVENT_LOG define=BOOST_LOG_WITHOUT_SYSLOG define=BOOST_LOG_WITHOUT_IPC define=_FORTIFY_SOURCE=2 cxxflags='-std=gnu++17 -fstack-protector -msse4.2 -mfpmath=sse' linkflags='-std=gnu++17 -Wl,-z,relro -Wl,-z,now'
209209
```
210210

211211
to install the Boost headers and libraries. (Note the `env PATH="$PATH"` bit in the install command - this is because `sudo` usually resets `PATH` and that will cause Boost to rebuild everything again with the default compiler as part of the install!)
@@ -214,18 +214,18 @@ For aarch64 replace `-msse4.2 -mfpmath=sse` with `-march=armv8-a+crc+crypto`.
214214

215215
### patchelf
216216

217-
Obtain patchelf from <http://nixos.org/releases/patchelf/patchelf-0.10/> - the download file will be `patchelf-0.10.tar.bz2`.
217+
Obtain patchelf from <https://github.com/NixOS/patchelf/releases/download/0.13/patchelf-0.13.tar.bz2>.
218218

219219
Extract it to a temporary directory using:
220220

221221
```
222-
bzip2 -cd patchelf-0.10.tar.bz2 | tar xvf -
222+
bzip2 -cd patchelf-0.13.tar.bz2 | tar xvf -
223223
```
224224

225-
In the resulting `patchelf-0.10` directory, run the:
225+
In the resulting `patchelf-0.13.20210805.a949ff2` directory, run the:
226226

227227
```
228-
./configure --prefix=/usr/local/gcc93
228+
./configure --prefix=/usr/local/gcc103
229229
```
230230

231231
script. This should build an appropriate Makefile. Assuming it does, run:
@@ -256,46 +256,47 @@ PyTorch currently requires Python 3.6, 3.7 or 3.8, and version 3.7 appears to ca
256256
```
257257
tar -xzf Python-3.7.9.tgz
258258
cd Python-3.7.9
259-
./configure --prefix=/usr/local/gcc93 --enable-optimizations
259+
./configure --prefix=/usr/local/gcc103 --enable-optimizations
260260
make
261261
sudo make altinstall
262262
```
263263

264264
### Intel MKL
265265

266-
Intel Maths Kernal Library is an optimized BLAS library which
266+
Skip this step if you are building on aarch64.
267+
268+
Intel Maths Kernel Library is an optimized BLAS library which
267269
greatly improves the performance of PyTorch CPU inference
268270
when PyTorch is built with it.
269271

270272
```
271-
yum-config-manager --add-repo https://yum.repos.intel.com/mkl/setup/intel-mkl.repo
272-
yum -y install intel-mkl-2020.4-912
273+
sudo yum-config-manager --add-repo https://yum.repos.intel.com/mkl/setup/intel-mkl.repo
274+
sudo yum -y install intel-mkl-2020.4-912
273275
```
274276

275277
Then copy the required libraries to the system directory:
278+
276279
```
277-
cp /opt/intel/mkl/lib/intel64/libmkl_intel_lp64.so /usr/local/gcc93/lib
278-
cp /opt/intel/mkl/lib/intel64/libmkl_core.so /usr/local/gcc93/lib
279-
cp /opt/intel/mkl/lib/intel64/libmkl_def.so /usr/local/gcc93/lib
280-
cp /opt/intel/mkl/lib/intel64/libmkl_gnu_thread.so /usr/local/gcc93/lib
281-
cp /opt/intel/mkl/lib/intel64/libmkl_avx*.so /usr/local/gcc93/lib
282-
cp /opt/intel/mkl/lib/intel64/libmkl_vml*.so /usr/local/gcc93/lib
280+
sudo cp /opt/intel/mkl/lib/intel64/libmkl_intel_lp64.so /usr/local/gcc103/lib
281+
sudo cp /opt/intel/mkl/lib/intel64/libmkl_core.so /usr/local/gcc103/lib
282+
sudo cp /opt/intel/mkl/lib/intel64/libmkl_def.so /usr/local/gcc103/lib
283+
sudo cp /opt/intel/mkl/lib/intel64/libmkl_gnu_thread.so /usr/local/gcc103/lib
284+
sudo cp /opt/intel/mkl/lib/intel64/libmkl_avx*.so /usr/local/gcc103/lib
285+
sudo cp /opt/intel/mkl/lib/intel64/libmkl_vml*.so /usr/local/gcc103/lib
283286
```
284287

285-
If you are building on aarch64 skip this step.
286-
287288
### PyTorch 1.9.0
288289

289290
PyTorch requires that certain Python modules are installed. Install these modules with `pip` using the same Python version you will build PyTorch with. If you followed the instructions above and built Python from source use `python3.7`:
290291

291292
```
292-
sudo /usr/local/gcc93/bin/python3.7 -m pip install install numpy ninja pyyaml setuptools cffi typing_extensions future six requests dataclasses
293+
sudo /usr/local/gcc103/bin/python3.7 -m pip install install numpy ninja pyyaml setuptools cffi typing_extensions future six requests dataclasses
293294
```
294295

295296
For aarch64 the `ninja` module is not available, so use:
296297

297298
```
298-
sudo /usr/local/gcc93/bin/python3.7 -m pip install install numpy pyyaml setuptools cffi typing_extensions future six requests dataclasses
299+
sudo /usr/local/gcc103/bin/python3.7 -m pip install install numpy pyyaml setuptools cffi typing_extensions future six requests dataclasses
299300
```
300301

301302
Then obtain the PyTorch code:
@@ -315,34 +316,10 @@ a heuristic virus scanner looking for potentially dangerous function
315316
calls in our shipped product will not encounter these functions that run
316317
external processes.
317318

318-
If you are building on aarch64, also edit `aten/src/ATen/cpu/vml.h` and
319-
change lines 88-93 from:
320-
321-
```
322-
parallel_for(0, size, 2048, [out, in](int64_t begin, int64_t end) { \
323-
map([](const Vec256<scalar_t>& x) { return x.op(); }, \
324-
out + begin, \
325-
in + begin, \
326-
end - begin); \
327-
}); \
328-
```
329-
330-
to:
331-
332-
```
333-
map([](const Vec256<scalar_t>& x) { return x.op(); }, \
334-
out, \
335-
in, \
336-
size); \
337-
```
338-
339-
(That edit is a workaround for a compiler bug that affects gcc 9.3 and 10.2
340-
and will hopefully become unnecessary if we upgrade to gcc 9.4 or 10.3.)
341-
342319
Build as follows:
343320

344321
```
345-
[ $(uname -m) = x86_64 ] && export BLAS=MKL
322+
[ $(uname -m) = x86_64 ] && export BLAS=MKL || export BLAS=Eigen
346323
export BUILD_TEST=OFF
347324
[ $(uname -m) = x86_64 ] && export BUILD_CAFFE2=OFF
348325
[ $(uname -m) != x86_64 ] && export USE_FBGEMM=OFF
@@ -355,16 +332,16 @@ export USE_PYTORCH_QNNPACK=OFF
355332
[ $(uname -m) = x86_64 ] && export USE_XNNPACK=OFF
356333
export PYTORCH_BUILD_VERSION=1.9.0
357334
export PYTORCH_BUILD_NUMBER=1
358-
/usr/local/gcc93/bin/python3.7 setup.py install
335+
/usr/local/gcc103/bin/python3.7 setup.py install
359336
```
360337

361338
Once built copy headers and libraries to system directories:
362339

363340
```
364-
sudo mkdir -p /usr/local/gcc93/include/pytorch
365-
sudo cp -r torch/include/* /usr/local/gcc93/include/pytorch/
366-
sudo cp torch/lib/libtorch_cpu.so /usr/local/gcc93/lib
367-
sudo cp torch/lib/libc10.so /usr/local/gcc93/lib
341+
sudo mkdir -p /usr/local/gcc103/include/pytorch
342+
sudo cp -r torch/include/* /usr/local/gcc103/include/pytorch/
343+
sudo cp torch/lib/libtorch_cpu.so /usr/local/gcc103/lib
344+
sudo cp torch/lib/libc10.so /usr/local/gcc103/lib
368345
```
369346

370347
### valgrind
@@ -374,20 +351,20 @@ to debug with `valgrind` then you'll get better results if you build a version t
374351
with our `gcc` instead of using the version you can get via your package manager.
375352

376353
If you find yourself needing to do this, download `valgrind` from <http://valgrind.org/downloads/> - the
377-
download file will be `valgrind-3.15.0.tar.bz2`.
354+
download file will be `valgrind-3.17.0.tar.bz2`.
378355

379356
Extract it to a temporary directory using:
380357

381358
```
382-
tar jxvf valgrind-3.15.0.tar.bz2
359+
tar jxvf valgrind-3.17.0.tar.bz2
383360
```
384361

385-
In the resulting `valgrind-3.15.0` directory, run:
362+
In the resulting `valgrind-3.17.0` directory, run:
386363

387364
```
388365
unset CFLAGS
389366
unset CXXFLAGS
390-
./configure --prefix=/usr/local/gcc93 --disable-dependency-tracking --enable-only64bit
367+
./configure --prefix=/usr/local/gcc103 --disable-dependency-tracking --enable-only64bit
391368
```
392369

393370
The reason for unsetting the compiler flags is that `valgrind` does not build correctly

0 commit comments

Comments
 (0)