How to build PyTorch with a latest LLVM build?

username: anubane
cooked: <p>I have built the LLVM project locally (<em>latest release</em>). I wish to use my clang build to compile PyTorch source code. Currently I am facing issues during the PyTorch build.</p>
<pre><code class="lang-auto">...
...
[89/2272] Performing build step for 'nccl_external'
FAILED: nccl_external-prefix/src/nccl_external-stamp/nccl_external-build nccl/lib/libnccl_static.a /home/anurag/pytorch-2.1.2/build/nccl_external-prefix/src/nccl_external-stamp/nccl_external-build /home/anurag/pytorch-2.1.2/build/nccl/lib/libnccl_static.a
cd /home/anurag/pytorch-2.1.2/third_party/nccl/nccl &amp;&amp; make -j32 -l32 CXX=/home/anurag/llvm-project-llvmorg-17.0.6/installed/bin/clang++ CUDA_HOME=/usr/local/cuda NVCC=/usr/local/cuda/bin/nvcc NVCC_GENCODE=-gencode=arch=compute_70,code=sm_70 BUILDDIR=/home/anurag/pytorch-2.1.2/build/nccl VERBOSE=0 DEBUG=0 &amp;&amp; /usr/local/bin/cmake -E touch /home/anurag/pytorch-2.1.2/build/nccl_external-prefix/src/nccl_external-stamp/nccl_external-build
make -C src build BUILDDIR=/home/anurag/pytorch-2.1.2/build/nccl
make[1]: Entering directory '/home/anurag/pytorch-2.1.2/third_party/nccl/nccl/src'
NVCC_GENCODE is -gencode=arch=compute_70,code=sm_70
...
...
In file included from &lt;built-in&gt;:1:
In file included from /usr/local/cuda/bin/../targets/x86_64-linux/include/cuda_runtime.h:82:
/usr/local/cuda/bin/../targets/x86_64-linux/include/crt/host_config.h:151:2: error: -- unsupported clang version! clang version must be less than 16 and greater than 3.2 . The nvcc flag '-allow-unsupported-compiler' can be used to override this version check; however, using an unsupported host compiler may cause compilation failure or incorrect run time execution. Use at your own risk.
  151 | #error -- unsupported clang version! clang version must be less than 16 and greater than 3.2 . The nvcc flag '-allow-unsupported-compiler' can be used to override this version check; however, using an unsupported host compiler may cause compilation failure or incorrect run time execution. Use at your own risk.
  ...
  ...
  make[1]: Leaving directory '/home/anurag/pytorch-2.1.2/third_party/nccl/nccl/src'
make: *** [Makefile:25: src.build] Error 2
[120/2272] Building CXX object third_party/protobuf/cmake/CMakeFiles/libprotoc.dir/__/src/google/protobuf/compiler/command_line_interface.cc.o
ninja: build stopped: subcommand failed.
</code></pre>
<p>My system configurations are as follows:</p>
<pre><code class="lang-auto">LLVM version: 17.0.6
Python version: 3.10.8
PyTorch version: 2.1.2
CUDA Driver Version: 545.23.08
CUDA Version: 12.3
nvcc version: release 12.3, V12.3.107
</code></pre>
<p>My build configurations are as follows (<em>I put all the following in a</em> <code>.sh</code> <em>file and run it under the pytorch project root</em>):</p>
<pre><code class="lang-auto">export CC=/home/anurag/llvm-project-llvmorg-17.0.6/installed/bin/clang; \
export CXX=/home/anurag/llvm-project-llvmorg-17.0.6/installed/bin/clang++; \
export MAX_JOBS=32; \
export DEBUG=1; \
export USE_DISTRIBUTED=OFF; \
export USE_MKLDNN=OFF; \
export USE_CUDA=ON; \
export CMAKE_CXX_FLAGS="-Wmissing-braces"; \
export CMAKE_CUDA_HOST_COMPILER=/home/anurag/llvm-project-llvmorg-17.0.6/installed/bin/clang++; \
export CMAKE_CUDA_FLAGS="-allow-unsupported-compiler -std=c++17"; \
export BUILD_TEST=OFF; \
export USE_FBGEMM=OFF; \
export USE_NNPACK=OFF; \
export USE_QNNPACK=OFF; \
export USE_XNNPACK=OFF; \
export NVCC_GENCODE="-gencode=arch=compute_70,code=sm_70"; \
export USE_GOLD_LINKER=ON; \
python setup.py clean; \
ccache -C; \
ccache -c; \
python setup.py develop;
</code></pre>
<p>Can anyone help me out with <strong>how to build PyTorch, using a locally built latest LLVM</strong>?</p>
<p><em>(cross posted from <a href="https://dev-discuss.pytorch.org/t/how-to-build-pytorch-with-a-latest-llvm-build/1846" rel="noopener nofollow ugc">dev-discuss forum</a>)</em></p>

username: anubane
cooked: <aside class="quote no-group" data-username="rtwolfe94022" data-post="2" data-topic="196277">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rtwolfe94022/48/67286_2.png" class="avatar"> rtwolfe94022:</div>
<blockquote>
<p>export CMAKE_CUDA_HOST_COMPILER=/usr/bin/gcc</p>
</blockquote>
</aside>
<p>Setting this variable did not make any difference!</p>

username: anubane
cooked: <p>For now, I was able to build PyTorch 2.2.0 with CUDA by downgrading my <code>clang</code> compiler (from <strong>17.0.6</strong> to <strong>10</strong>):</p>
<pre><code class="lang-auto">clear; \
export CC=/usr/bin/clang-10; \
export CXX=/usr/bin/clang++-10; \
#export CC=/home/anurag/llvm-project-llvmorg-17.0.6/installed/bin/clang; \
#export CXX=/home/anurag/llvm-project-llvmorg-17.0.6/installed/bin/clang++; \
export MAX_JOBS=32; \
export DEBUG=1; \
export USE_DISTRIBUTED=OFF; \
export USE_MKLDNN=OFF; \
export USE_CUDA=ON; \
#export CMAKE_CXX_FLAGS="-Wmissing-braces"; \
export CMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc; \
#export CMAKE_CUDA_HOST_COMPILER=/home/anurag/llvm-project-llvmorg-17.0.6/installed/bin/clang++; \
#export CMAKE_CUDA_FLAGS="-allow-unsupported-compiler -std=c++17"; \
export BUILD_TEST=OFF; \
export USE_FBGEMM=OFF; \
export USE_NNPACK=OFF; \
export USE_QNNPACK=OFF; \
export USE_XNNPACK=OFF; \
export NVCC_GENCODE="-gencode=arch=compute_70,code=sm_70"; \
export USE_GOLD_LINKER=ON; \
python setup.py clean; \
ccache -cC; \
python setup.py develop;
</code></pre>
<p>(<em>Some more flags may be set according to <a href="https://github.com/Kitware/CMake/blob/release/Modules/CMakeCUDACompiler.cmake.in" rel="noopener nofollow ugc">cmake CUDA flags</a></em>)</p>
<p>So the real question of how to compile PyTorch with CUDA using newer <code>clang</code> still stands.</p>