How to building static libtorch on ubuntu 18.04 without CUDA

Yin_Zheng: <h2><a name="how-to-build-libtorch-static-library-without-cuda-1" class="anchor" href="#how-to-build-libtorch-static-library-without-cuda-1"></a>How to build libtorch static library without CUDA</h2>
<h3><a name="for-some-reason-i-need-to-use-the-static-library-of-libtorch-eg-libtorcha-not-libtorchso-as-provided-by-the-official-website-so-i-would-like-to-compile-it-from-source-my-compute-is-ubuntu-1804-with-cuda-but-i-set-no_cuda1-2" class="anchor" href="#for-some-reason-i-need-to-use-the-static-library-of-libtorch-eg-libtorcha-not-libtorchso-as-provided-by-the-official-website-so-i-would-like-to-compile-it-from-source-my-compute-is-ubuntu-1804-with-cuda-but-i-set-no_cuda1-2"></a>For some reason, I need to use the static library of libtorch, e.g. libtorch.a (not libtorch.so as provided by the official website". So I would like to compile it from source. My compute is Ubuntu 18.04 with CUDA, but I set NO_CUDA=1.</h3>
<p>Here is what I tried:<br>
<code>git clone https://github.com/pytorch/pytorch.git</code></p>
<pre><code class="lang-auto">cd pytorch
mkdir build_libtorch &amp;&amp; cd build_libtorch
NO_CUDA=1
BUILD_SHARED_LIBS=OFF
python ../tools/build_libtorch.py
 
</code></pre>
<p>However, the compilation failed with the error:</p>
<blockquote></blockquote>
<blockquote>
<p>Determining if the pthread_create exist failed with the following output:<br>
Change Dir: /home/yinzheng/repos/pytorch/build_libtorch/build/CMakeFiles/CMakeTmp</p>
<p>Run Build Command(s):/home/yinzheng/anaconda3/bin/ninja cmTC_aa6e6<br>
[1/2] Building C object CMakeFiles/cmTC_aa6e6.dir/CheckSymbolExists.c.o<br>
[2/2] Linking C executable cmTC_aa6e6<br>
FAILED: cmTC_aa6e6<br>
: &amp;&amp; /usr/bin/cc   -rdynamic CMakeFiles/cmTC_aa6e6.dir/CheckSymbolExists.c.o  -o cmTC_aa6e6   &amp;&amp; :<br>
CMakeFiles/cmTC_aa6e6.dir/CheckSymbolExists.c.o: In function <code>main': CheckSymbolExists.c:(.text+0x1b): undefined reference to </code>pthread_create’<br>
collect2: error: ld returned 1 exit status<br>
ninja: build stopped: subcommand failed.</p>
<p>File /home/yinzheng/repos/pytorch/build_libtorch/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c:<br>
/* */<br>
<span class="hashtag-raw">#include</span> &lt;pthread.h&gt;</p>
<p>int main(int argc, char** argv)<br>
{<br>
(void)argv;<br>
<span class="hashtag-raw">#ifndef</span> pthread_create<br>
return ((int*)(&amp;pthread_create))[argc];<br>
<span class="hashtag-raw">#else</span><br>
(void)argc;<br>
return 0;<br>
<span class="hashtag-raw">#endif</span><br>
}</p>
<p>Determining if the function pthread_create exists in the pthreads failed with the following output:<br>
Change Dir: /home/yinzheng/repos/pytorch/build_libtorch/build/CMakeFiles/CMakeTmp</p>
<p>Run Build Command(s):/home/yinzheng/anaconda3/bin/ninja cmTC_c5dad<br>
[1/2] Building C object CMakeFiles/cmTC_c5dad.dir/CheckFunctionExists.c.o<br>
[2/2] Linking C executable cmTC_c5dad<br>
FAILED: cmTC_c5dad<br>
: &amp;&amp; /usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create  -rdynamic CMakeFiles/cmTC_c5dad.dir/CheckFunctionExists.c.o  -o cmTC_c5dad  -lpthreads &amp;&amp; :<br>
/usr/bin/ld: cannot find -lpthreads<br>
collect2: error: ld returned 1 exit status<br>
ninja: build stopped: subcommand failed.</p>
<p>Performing C SOURCE FILE Test C_HAS_AVX_1 failed with the following output:<br>
Change Dir: /home/yinzheng/repos/pytorch/build_libtorch/build/CMakeFiles/CMakeTmp</p>
<p>Run Build Command(s):/home/yinzheng/anaconda3/bin/ninja cmTC_e13be<br>
[1/2] Building C object CMakeFiles/cmTC_e13be.dir/src.c.o<br>
FAILED: CMakeFiles/cmTC_e13be.dir/src.c.o<br>
/usr/bin/cc   -fopenmp -DC_HAS_AVX_1 -fPIE -o CMakeFiles/cmTC_e13be.dir/src.c.o   -c src.c<br>
src.c: In function ‘main’:<br>
src.c:7:7: warning: AVX vector return without AVX enabled changes the ABI [-Wpsabi]<br>
a = _mm256_set1_ps(0);<br>

^
~~~~~~~~~~~~~~~~<br>
In file included from /usr/lib/gcc/x86_64-linux-gnu/7/include/immintrin.h:41:0,<br>
from src.c:2:<br>
/usr/lib/gcc/x86_64-linux-gnu/7/include/avxintrin.h:1310:1: error: inlining failed in call to always_inline ‘_mm256_set1_ps’: target specific option mismatch<br>
_mm256_set1_ps (float __A)<br>
^~~~~~~~~~~~~~<br>
src.c:7:7: note: called from here<br>
a = _mm256_set1_ps(0);<br>

^
~~~~~~~~~~~~~~~~<br>
ninja: build stopped: subcommand failed.</p>
<p>Source file was:</p>
<p><span class="hashtag-raw">#include</span> &lt;immintrin.h&gt;</p>
<p>int main()<br>
{<br>
__m256 a;<br>
a = _mm256_set1_ps(0);<br>
return 0;<br>
}</p>
<p>Performing C SOURCE FILE Test C_HAS_AVX2_1 failed with the following output:<br>
Change Dir: /home/yinzheng/repos/pytorch/build_libtorch/build/CMakeFiles/CMakeTmp</p>
<p>Run Build Command(s):/home/yinzheng/anaconda3/bin/ninja cmTC_e89cc<br>
[1/2] Building C object CMakeFiles/cmTC_e89cc.dir/src.c.o<br>
FAILED: CMakeFiles/cmTC_e89cc.dir/src.c.o<br>
/usr/bin/cc   -fopenmp -DC_HAS_AVX2_1 -fPIE -o CMakeFiles/cmTC_e89cc.dir/src.c.o   -c src.c<br>
src.c: In function ‘main’:<br>
src.c:7:7: warning: AVX vector return without AVX enabled changes the ABI [-Wpsabi]<br>
a = _mm256_abs_epi16(a);<br>

^
~~~~~~~~~~~~~~~~~~<br>
In file included from /usr/lib/gcc/x86_64-linux-gnu/7/include/immintrin.h:43:0,<br>
from src.c:2:<br>
/usr/lib/gcc/x86_64-linux-gnu/7/include/avx2intrin.h:63:1: error: inlining failed in call to always_inline ‘_mm256_abs_epi16’: target specific option mismatch<br>
_mm256_abs_epi16 (__m256i __A)<br>
^~~~~~~~~~~~~~~~<br>
src.c:7:7: note: called from here<br>
a = _mm256_abs_epi16(a);<br>

^
~~~~~~~~~~~~~~~~~~<br>
ninja: build stopped: subcommand failed.</p>
<p>Source file was:</p>
<p><span class="hashtag-raw">#include</span> &lt;immintrin.h&gt;</p>
<p>int main()<br>
{<br>
__m256i a = {0};<br>
a = _mm256_abs_epi16(a);<br>
__m256i x;<br>
_mm256_extract_epi64(x, 0); // we rely on this in our AVX2 code<br>
return 0;<br>
}</p>
<p>Performing C SOURCE FILE Test CXX_HAS_AVX_1 failed with the following output:<br>
Change Dir: /home/yinzheng/repos/pytorch/build_libtorch/build/CMakeFiles/CMakeTmp</p>
<p>Run Build Command(s):/home/yinzheng/anaconda3/bin/ninja cmTC_a61ef<br>
[1/2] Building C object CMakeFiles/cmTC_a61ef.dir/src.c.o<br>
FAILED: CMakeFiles/cmTC_a61ef.dir/src.c.o<br>
/usr/bin/cc   -fopenmp -DCXX_HAS_AVX_1 -fPIE -o CMakeFiles/cmTC_a61ef.dir/src.c.o   -c src.c<br>
src.c: In function ‘main’:<br>
src.c:7:7: warning: AVX vector return without AVX enabled changes the ABI [-Wpsabi]<br>
a = _mm256_set1_ps(0);<br>

^
~~~~~~~~~~~~~~~~<br>
In file included from /usr/lib/gcc/x86_64-linux-gnu/7/include/immintrin.h:41:0,<br>
from src.c:2:<br>
/usr/lib/gcc/x86_64-linux-gnu/7/include/avxintrin.h:1310:1: error: inlining failed in call to always_inline ‘_mm256_set1_ps’: target specific option mismatch<br>
_mm256_set1_ps (float __A)<br>
^~~~~~~~~~~~~~<br>
src.c:7:7: note: called from here<br>
a = _mm256_set1_ps(0);<br>

^
~~~~~~~~~~~~~~~~<br>
ninja: build stopped: subcommand failed.</p>
<p>Source file was:</p>
<p><span class="hashtag-raw">#include</span> &lt;immintrin.h&gt;</p>
<p>int main()<br>
{<br>
__m256 a;<br>
a = _mm256_set1_ps(0);<br>
return 0;<br>
}</p>
<p>Performing C SOURCE FILE Test CXX_HAS_AVX2_1 failed with the following output:<br>
Change Dir: /home/yinzheng/repos/pytorch/build_libtorch/build/CMakeFiles/CMakeTmp</p>
<p>Run Build Command(s):/home/yinzheng/anaconda3/bin/ninja cmTC_f2d12<br>
[1/2] Building C object CMakeFiles/cmTC_f2d12.dir/src.c.o<br>
FAILED: CMakeFiles/cmTC_f2d12.dir/src.c.o<br>
/usr/bin/cc   -fopenmp -DCXX_HAS_AVX2_1 -fPIE -o CMakeFiles/cmTC_f2d12.dir/src.c.o   -c src.c<br>
src.c: In function ‘main’:<br>
src.c:7:7: warning: AVX vector return without AVX enabled changes the ABI [-Wpsabi]<br>
a = _mm256_abs_epi16(a);<br>

^
~~~~~~~~~~~~~~~~~~<br>
In file included from /usr/lib/gcc/x86_64-linux-gnu/7/include/immintrin.h:43:0,<br>
from src.c:2:<br>
/usr/lib/gcc/x86_64-linux-gnu/7/include/avx2intrin.h:63:1: error: inlining failed in call to always_inline ‘_mm256_abs_epi16’: target specific option mismatch<br>
_mm256_abs_epi16 (__m256i __A)<br>
^~~~~~~~~~~~~~~~<br>
src.c:7:7: note: called from here<br>
a = _mm256_abs_epi16(a);<br>

^
~~~~~~~~~~~~~~~~~~<br>
ninja: build stopped: subcommand failed.</p>
<p>Source file was:</p>
<p><span class="hashtag-raw">#include</span> &lt;immintrin.h&gt;</p>
<p>int main()<br>
{<br>
__m256i a = {0};<br>
a = _mm256_abs_epi16(a);<br>
__m256i x;<br>
_mm256_extract_epi64(x, 0); // we rely on this in our AVX2 code<br>
return 0;<br>
}</p>
</blockquote>
<p>But I am sure that I have pthreads</p>
<pre><code class="lang-auto">(base) yinzheng@yinzheng-LC0:/lib/x86_64-linux-gnu$ ll *thread*
-rwxr-xr-x 1 root root 144976 4月  17  2018 libpthread-2.27.so*
lrwxrwxrwx 1 root root     18 8月  30 19:13 libpthread.so.0 -&gt; libpthread-2.27.so*
-rw-r--r-- 1 root root  35648 4月  17  2018 libthread_db-1.0.so
lrwxrwxrwx 1 root root     19 8月  30 19:13 libthread_db.so.1 -&gt; libthread_db-1.0.so
</code></pre>
<p>and I add /lib/x86_64-linux-gnu to LD_LIBRARY_PATH</p>
<pre><code class="lang-auto">(base) yinzheng@yinzheng-LC0:/lib/x86_64-linux-gnu$ echo $LD_LIBRARY_PATH 
/home/yinzheng/anaconda3/lib:/lib:/usr/lib:
</code></pre>
<p>So is there any suggestion and help to tell me how to compile libtorch static library with CUDA on ubuntu?<br>
Thanks very much in advance!</p>
<p>Regards<br>
Yin</p>

jiapei100: <p>Same here… Di you find the solution? Thank you …</p>