Keeps referencing wrong version of gcc when building from source

I’m trying to build pytorch from source, having just installed MAGMA due to the error

No CUDA implementation of 'potrf'. Install MAGMA and rebuild cutorch

I’m doing this on a 2013 macbook, so I’m aware support is limited, however I did it before so I have faith it will work again!

The problem I’m having is when I try and call setup.py, it keeps referencing a different version of gcc. I have both 8.1.0 and 5.3.0 – 5 is the one I want. I’ve set up environment variables corresponding to this version in ~/.bash_profile

$ echo $CC
/usr/local/bin/gcc-5
$ echo $CXX
/usr/local/bin/g++-5

I’ve also set up symbolic links to /usr/bin/gcc and /usr/bin/g++ in case it was calling them.

However, when I run the command CC=gcc-5 CXX=g++-5 DCUDA_HOST_COMPILER=/usr/local/bin/gcc-5 python setup.py install

I get the error:

-- Caffe2: CUDA detected: 8.0
-- Caffe2: CUDA nvcc is: /usr/local/cuda/bin/nvcc
-- Caffe2: CUDA toolkit directory: /usr/local/cuda
-- Caffe2: Header version is: 8.0
-- Found cuDNN: v6.0.21  (include: /usr/local/cuda/include, library: /usr/local/cuda/lib/libcudnn.6.dylib)
CMake Error at cmake/public/cuda.cmake:321 (message):
  CUDA 8.0 is not compatible with GCC version >= 6.  Use the following option
  to use another version (for example):

    -DCUDA_HOST_COMPILER=/usr/bin/gcc-5

Call Stack (most recent call first):
  cmake/Dependencies.cmake:401 (include)
  CMakeLists.txt:204 (include)


-- Configuring incomplete, errors occurred!
See also "/Users/askates/Documents/GitRepos/pytorch/build/CMakeFiles/CMakeOutput.log".
See also "/Users/askates/Documents/GitRepos/pytorch/build/CMakeFiles/CMakeError.log".

and when I look at CMakeOutput.log I see it’s referencing /usr/local/bin/gcc-8

How can I make it reference gcc-5 instead of gcc-8? What am I missing?

I seem to have gotten it to work with cmake CMakeLists.txt, however I now get the error:

CMake Error at third_party/sleef/CMakeLists.txt:27 (message):
  SLEEF does not allow in-source builds.

  You can refer to doc/build-with-cmake.md for instructions on how provide a
  separate build directory.  Note: Please remove autogenerated file
  `CMakeCache.txt` and directory `CMakeFiles` in the current directory.


-- Configuring incomplete, errors occurred!

I’ve tried building sleef separately and if I run

$ mkdir third_party/sleef/build_dir
$ cd third_party/sleef/build_dir
$ cmake ..

It builds successfully, but when I try and run setup.py I get this:

-- The CXX compiler identification is unknown
-- The C compiler identification is unknown
CMake Error at CMakeLists.txt:6 (project):
  The CMAKE_CXX_COMPILER:

    /usr/local/bin/g++-8

  is not a full path to an existing compiler tool.

  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.


CMake Error at CMakeLists.txt:6 (project):
  The CMAKE_C_COMPILER:

    /usr/local/bin/gcc-8

  is not a full path to an existing compiler tool.

  Tell CMake where to find the compiler by setting either the environment
  variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
  the compiler, or to the compiler name if it is in the PATH.


-- Configuring incomplete, errors occurred!

I’m not sure what the official way is, but here is what has consistently worked for me for a long time by now:

I have set up two directories with symlinks I add to my path. In ~/pytorch/compiler I have

g++ -> /usr/bin/g++-5
gcc -> /usr/bin/gcc-5
x86_64-linux-gnu-g++ -> /usr/bin/x86_64-linux-gnu-g++-5
x86_64-linux-gnu-gcc -> /usr/bin/x86_64-linux-gnu-gcc-5

and because I like ccache (I use the Debian/unstable ccache seems to do OK) I also have ~/pytorch/compilers-with-ccache

g++ -> /usr/bin/ccache
g++-5 -> /usr/bin/ccache
gcc -> /usr/bin/ccache
gcc-5 -> /usr/bin/ccache
x86_64-linux-gnu-g++ -> /usr/bin/ccache
x86_64-linux-gnu-g++-5 -> /usr/bin/ccache
x86_64-linux-gnu-gcc -> /usr/bin/ccache
x86_64-linux-gnu-gcc-5 -> /usr/bin/ccache

Then I compile PyTorch with

PATH=~/pytorch/compilers-with-ccache/:~/pytorch/compilers/:$PATH python3 setup.py bdist_wheel

to get a whl in the dist directory.
Finally, I install with sudo pip3 -U --no-deps dist/something.whl because my system Python 3 is with pip3.

You might have to clean (python3 setup.py clean) before rerunning setup.py.

Best regards

Thomas

I just tried that – I don’t have a ccache directory though so I missed that out. I also added all the others just in case:

$ mkdir compilers; cd compilers
$ ln -s /usr/local/bin/g++-5 g++
$ ln -s /usr/local/bin/gcc-5 gcc
$ ln -s /usr/local/bin/gcc-ar-5 gcc-ar
$ ln -s /usr/local/bin/gcc-nm-5 gcc-nm
$ ln -s /usr/local/bin/gcc-ranlib-5 gcc-ranlib
$ cd ..
$ PATH=~/Documents/GitRepos/pytorch/compilers/:$PATH python setup.py bdist_wheel 

I get this again:

-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:6 (project):
  The CMAKE_CXX_COMPILER:

    /usr/local/bin/g++-8

  is not a full path to an existing compiler tool.

  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.


-- Configuring incomplete, errors occurred!

I don’t know why it’s defaulting to g++-8 still…

$ compilers/gcc --version
gcc (GCC) 5.3.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I got the whole project again, overwriting all local changes. Your instructions seemed to work up until I got to

[ 48%] Linking CXX shared library ../lib/libcaffe2.dylib
ld: library not found for -lgcc_s
collect2: error: ld returned 1 exit status
make[2]: *** [lib/libcaffe2.dylib] Error 1
make[1]: *** [caffe2/CMakeFiles/caffe2.dir/all] Error 2
make: *** [all] Error 2
Failed to run 'bash tools/build_pytorch_libs.sh --with-cuda --with-nnpack caffe2 nanopb libshm THD'

So I know nothing at all about this (I missed that you are on OS X :confused: , but googling this error message, people apparently do funny manual symlinking with some success.

Best regards

Thomas