Search code examples
c++cmakepytorchconda

Conda CMAKE CXX Compiler error while compiling Pytorch


I was following How to build PyTorch from Source in order to install Pytorch as I have older Graphic card which only supports Cuda 11.4. I am working in Conda environment. At the last stage I am getting following error.

Building wheel torch-2.4.0a0+git2b1ba0c
-- Building version 2.4.0a0+git2b1ba0c
cmake -GNinja -DBUILD_PYTHON=True -DBUILD_TEST=True -DCMAKE_ARGS=-DCMAKE_AR=/home/sinfinities/.conda/envs/StableD/bin/x86_64-conda-linux-gnu-ar -DCMAKE_CXX_COMPILER_AR=/home/sinfinities/.conda/envs/StableD/bin/x86_64-conda-linux-gnu-gcc-ar -DCMAKE_C_COMPILER_AR=/home/sinfinities/.conda/envs/StableD/bin/x86_64-conda-linux-gnu-gcc-ar -DCMAKE_RANLIB=/home/sinfinities/.conda/envs/StableD/bin/x86_64-conda-linux-gnu-ranlib -DCMAKE_CXX_COMPILER_RANLIB=/home/sinfinities/.conda/envs/StableD/bin/x86_64-conda-linux-gnu-gcc-ranlib -DCMAKE_C_COMPILER_RANLIB=/home/sinfinities/.conda/envs/StableD/bin/x86_64-conda-linux-gnu-gcc-ranlib -DCMAKE_LINKER=/home/sinfinities/.conda/envs/StableD/bin/x86_64-conda-linux-gnu-ld -DCMAKE_STRIP=/home/sinfinities/.conda/envs/StableD/bin/x86_64-conda-linux-gnu-strip -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/home/sinfinities/Downloads/pytorch/torch -DCMAKE_PREFIX_PATH=/home/sinfinities/.conda/envs/StableD/lib/python3.12/site-packages;/home/sinfinities/.conda/envs/StableD:/home/sinfinities/.conda/envs/StableD/x86_64-conda-linux-gnu/sysroot/usr -DNUMPY_INCLUDE_DIR=/home/sinfinities/.conda/envs/StableD/lib/python3.12/site-packages/numpy/core/include -DPYTHON_EXECUTABLE=/home/sinfinities/.conda/envs/StableD/bin/python -DPYTHON_INCLUDE_DIR=/home/sinfinities/.conda/envs/StableD/include/python3.12 -DPYTHON_LIBRARY=/home/sinfinities/.conda/envs/StableD/lib/libpython3.12.a -DTORCH_BUILD_VERSION=2.4.0a0+git2b1ba0c -DUSE_NUMPY=True /home/sinfinities/Downloads/pytorch
-- The CXX compiler identification is GNU 9.5.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: /home/sinfinities/.conda/envs/StableD/bin/c++
-- Check for working CXX compiler: /home/sinfinities/.conda/envs/StableD/bin/c++ - broken
CMake Error at /home/sinfinities/.conda/envs/StableD/share/cmake-3.26/Modules/CMakeTestCXXCompiler.cmake:60 (message):
  The C++ compiler

    "/home/sinfinities/.conda/envs/StableD/bin/c++"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /home/sinfinities/Downloads/pytorch/build/CMakeFiles/CMakeScratch/TryCompile-tQ7Kro
    
    Run Build Command(s):/home/sinfinities/.conda/envs/StableD/bin/ninja -v cmTC_6d41d && [1/2] /home/sinfinities/.conda/envs/StableD/bin/c++    -o CMakeFiles/cmTC_6d41d.dir/testCXXCompiler.cxx.o -c /home/sinfinities/Downloads/pytorch/build/CMakeFiles/CMakeScratch/TryCompile-tQ7Kro/testCXXCompiler.cxx
    [2/2] : && /home/sinfinities/.conda/envs/StableD/bin/c++   CMakeFiles/cmTC_6d41d.dir/testCXXCompiler.cxx.o -o cmTC_6d41d   && :
    FAILED: cmTC_6d41d 
    : && /home/sinfinities/.conda/envs/StableD/bin/c++   CMakeFiles/cmTC_6d41d.dir/testCXXCompiler.cxx.o -o cmTC_6d41d   && :
    /home/sinfinities/.conda/envs/StableD/bin/../lib/gcc/x86_64-conda-linux-gnu/9.5.0/../../../../x86_64-conda-linux-gnu/bin/ld: /home/sinfinities/.conda/envs/StableD/bin/../lib/gcc/x86_64-conda-linux-gnu/9.5.0/libstdc++.so: undefined reference to `memcpy@GLIBC_2.14'
    /home/sinfinities/.conda/envs/StableD/bin/../lib/gcc/x86_64-conda-linux-gnu/9.5.0/../../../../x86_64-conda-linux-gnu/bin/ld: /home/sinfinities/.conda/envs/StableD/bin/../lib/gcc/x86_64-conda-linux-gnu/9.5.0/libstdc++.so: undefined reference to `aligned_alloc@GLIBC_2.16'
    /home/sinfinities/.conda/envs/StableD/bin/../lib/gcc/x86_64-conda-linux-gnu/9.5.0/../../../../x86_64-conda-linux-gnu/bin/ld: /home/sinfinities/.conda/envs/StableD/bin/../lib/gcc/x86_64-conda-linux-gnu/9.5.0/libstdc++.so: undefined reference to `clock_gettime@GLIBC_2.17'
    collect2: error: ld returned 1 exit status
    ninja: build stopped: subcommand failed.
    
    

  

  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:29 (project)

My nvidia-smi output is

Sun Mar 31 22:59:04 2024       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.223.02   Driver Version: 470.223.02   CUDA Version: 11.4     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  Off  | 00000000:01:00.0 N/A |                  N/A |
| 33%   43C    P8    N/A /  N/A |     53MiB /  1999MiB |     N/A      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

nvcc --version

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Wed_Jun__2_19:15:15_PDT_2021
Cuda compilation tools, release 11.4, V11.4.48
Build cuda_11.4.r11.4/compiler.30033411_0

gcc --version

gcc (conda-forge gcc 9.5.0-17) 9.5.0
Copyright (C) 2019 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.

c++ --version

c++ (conda-forge gcc 9.5.0-17) 9.5.0
Copyright (C) 2019 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 have been going back and forth with this but not able to figure out how to fix it. Any guidence will be much appreciated.


Solution

  • After valueable comment from @AlanBirtles it was clear that the issue was in GLIBC or Libc. After banging my head against the wall for one day I figurered out it was a silly change I needed to do. Let me provide the entire instruction incase someone like me gets stuck with older GPU.

    You can install entire thing in Conda environment. Just have the Nvidia driver installed prior where you can run

    nvidia-smi 
    

    and get proper output. Them run following commands step one by one. For me I have GPU maximum supports Cuda 11.4 and Cudnn 8.2. and on pythorch forums I could find this specification will support Pytorch 2.1 So, you can make chanes as per your specification.

    conda create --name StableDiffusion
    conda activate StableDiffusion
    conda config --set channel_priority flexible
    conda install nvidia/label/cuda-11.4.0::cuda-toolkit
    conda install anaconda::cudnn=8.2
    conda list cudnn
    nvcc --version
    nvidia-smi
    
    conda install python-3.9
    conda install conda-forge::gxx=9.5
    conda install conda-forge::sysroot_linux-64=2.28
    
    conda install cmake ninja
    conda install intel::mkl-static intel::mkl-include
    conda install -c pytorch magma-cuda113
    
    git clone --recursive --branch v2.0.1 https://github.com/pytorch/pytorch
    cd pytorch
    pip install -r requirements.txt
    git submodule sync
    git submodule update --init --recursive
    export _GLIBCXX_USE_CXX11_ABI=1
    export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
    python setup.py install
    

    If you just want to know where I was making mistakes, I was installing GLIBC by

    conda install conda-forge::sysroot_linux-64
    

    Which installs LIBC 2.14 changing it to

    conda install conda-forge::sysroot_linux-64=2.28
    

    installs LIBC 2.28