Compiled libtorch static but getting linking errors when trying to use it (CentOS)

username: davidw82
cooked: <p>Hi,<br>
I’ve previously successfully compiled PyTorch 1.11 statically, and used libtorch from the resulting libraries to run PyTorch models from a C++ project.<br>
Now I tested to update to PyTorch 1.13 compiled the exact same way (the whole pytorch build runs successfully), but I’m suddenly getting linking errors when I link against libtorch in my C++ project. I’m getting the following error:</p>
<pre><code class="lang-auto">/home/david/NeuralNets_software/pytorch/pytorch113_cuda112_static/lib/libtorch_cuda.a(Conv.cpp.o): In function `c10::SmallVector&lt;long, (2)+(2)&gt; MakeConvOutputShape&lt;2&gt;(int, int, std::array&lt;long, 2&gt; const&amp;, std::vector&lt;long, std::allocator&lt;long&gt; &gt; const&amp;, c10::List&lt;long&gt; const&amp;, c10::List&lt;long&gt; const&amp;, c10::List&lt;long&gt; const&amp;)':
Conv.cpp:(.text+0x1089): multiple definition of `c10::SmallVector&lt;long, (2)+(2)&gt; MakeConvOutputShape&lt;2&gt;(int, int, std::array&lt;long, 2&gt; const&amp;, std::vector&lt;long, std::allocator&lt;long&gt; &gt; const&amp;, c10::List&lt;long&gt; const&amp;, c10::List&lt;long&gt; const&amp;, c10::List&lt;long&gt; const&amp;)'
/home/david/NeuralNets_software/pytorch/pytorch113_cuda112_static/lib/libtorch_cpu.a(qconv.cpp.o):qconv.cpp:(.text+0x1553): first defined here
</code></pre>
<p>This is all compiling using GCC 7.3.1 (devtoolset-7) on CentOS.<br>
The linking command in the cmake file is the following:<br>
<code>target_link_libraries(MyProject PRIVATE -L/home/david/NeuralNets_software/pytorch/pytorch113_cuda112_static/lib -L/home/david/NeuralNets_software/pytorch/pytorch113_cuda112_static/lib64 -L/usr/local/cuda-11.2/lib64 -L/usr/local/cuda-11.2/extras/CUPTI/lib64 -Wl,--whole-archive torch torch_cpu torch_cuda c10_cuda onnx magma -Wl,--no-whole-archive -lc10 -Wl,--start-group -lmkl_intel_lp64 -lmkl_gnu_thread -lmkl_core -Wl,--end-group -lmagma_sparse -lnnpack -lqnnpack -lpytorch_qnnpack -lXNNPACK -lpthreadpool -lcpuinfo -lcpuinfo_internals -lclog -lprotobuf -lprotoc -lonnx_proto -lfbgemm -lfoxi_loader -lcaffe2_protos -ldnnl -lpthread -ldl -lrt -l:libopenblas.a -lgomp -lsleef -lasmjit -lm -lcudart -lcublas -lcufft -lcurand -lcusparse -lcusolver -lcudnn -l:libcupti_static.a -lfmt -lnvrtc -lnvToolsExt -Wl,-rpath,$ORIGIN --verbose)</code></p>
<p>Any idea why this is happening? Am I doing something wrong, or is it a bug in PyTorch 1.13 when compiling it statically?</p>
<p>Cheers,<br>
David</p>

username: davidw82
cooked: <p>Is this something you could help me shed some light on <a class="mention" href="/u/ptrblck">@ptrblck</a> ?<br>
Why would the definitions clash between “libtorch_cuda” and “libtorch_cpu” all of a sudden?</p>
<p>Thanks in advance,<br>
David</p>

username: ptrblck
cooked: <p>I don’t know as I haven’t tried this build before, but <a class="mention" href="/u/malfet">@malfet</a> might have an idea.</p>

username: davidw82
cooked: <p>Hi <a class="mention" href="/u/malfet">@malfet</a> , do you have any idea why I’m facing this error above when trying to link to my build of a static pytorch 1.13? It has been working well with pytorch 1.11 (and also pytorch 1.8 before that).<br>
This problem is really blocking my production currently, and I’ve been stuck for two weeks, so any help or insight would be very appreciated.</p>
<p>Best regards,<br>
David</p>

username: mpalya
cooked: <p>Hi,<br>
I had the same error with the 2.0.1 version.<br>
I found this commit, that says it solved the issue:</p><aside class="onebox githubcommit" data-onebox-src="https://github.com/pytorch/pytorch/commit/53d1d301c6d7ac24e54ea524a18cb249ef4f61f0">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/commit/53d1d301c6d7ac24e54ea524a18cb249ef4f61f0" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Commit">
    <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M10.86 7c-.45-1.72-2-3-3.86-3-1.86 0-3.41 1.28-3.86 3H0v2h3.14c.45 1.72 2 3 3.86 3 1.86 0 3.41-1.28 3.86-3H14V7h-3.14zM7 10.2c-1.22 0-2.2-.98-2.2-2.2 0-1.22.98-2.2 2.2-2.2 1.22 0 2.2.98 2.2 2.2 0 1.22-.98 2.2-2.2 2.2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/commit/53d1d301c6d7ac24e54ea524a18cb249ef4f61f0" target="_blank" rel="noopener nofollow ugc">Enable CuDNN v8 frontend in RL (#102284)</a>
    </h4>

    <div class="github-info">
      <div class="date">
        committed <span class="discourse-local-date" data-format="ll" data-date="2023-05-28" data-time="13:21:47" data-timezone="UTC">01:21PM - 28 May 23 UTC</span>
      </div>

      <div class="user">
        <a href="https://github.com/malfet" target="_blank" rel="noopener nofollow ugc">
          <img alt="malfet" src="https://avatars.githubusercontent.com/u/2453524?v=4" class="onebox-avatar-inline" width="20" height="20">
          malfet
        </a>
      </div>

      <div class="lines" title="changed 5 files with 75 additions and 95 deletions">
        <a href="https://github.com/pytorch/pytorch/commit/53d1d301c6d7ac24e54ea524a18cb249ef4f61f0" target="_blank" rel="noopener nofollow ugc">
          <span class="added">+75</span>
          <span class="removed">-95</span>
        </a>
      </div>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">Summary:
This enables use of CUDNN v8 in all Meta internal workflows. Also, fixe<span class="show-more-container"><a href="https://github.com/pytorch/pytorch/commit/53d1d301c6d7ac24e54ea524a18cb249ef4f61f0" target="_blank" rel="noopener nofollow ugc" class="show-more">…</a></span><span class="excerpt hidden">s two minor issues:
- Skip LogCumSumExp compilation for complex dtypes for fbcode and RL
- Move `MakeConvOutputShape` template definition/specialization to anonymous namespace inside `at::native::quantized` as it is referenced from both `torch_cpu` and `torch_cuda`. This is necessary to avoid `duplicate symbol` linker error if say `libtorch_cpu` and `libtorch_cuda` are statically linked together.
- Lower CuDNN v8 version guard from 8.3 to 8.2 (as there are no good reason why it should be 8.3, first version of the library that properly supports all the features is actually 8.5)

Test Plan: CI

Differential Revision: D46161651

Pull Request resolved: https://github.com/pytorch/pytorch/pull/102284
Approved by: https://github.com/atalman</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<p>
But it’s in the release 2.1</p>

username: davidw82
cooked: <p>That’s great, thanks a lot <a class="mention" href="/u/mpalya">@mpalya</a> !<br>
I managed to get it to build by adding “inline” to those template lines, which also seems to work. But it’s really good to know a proper fix for this, and I might swap to this solution instead since it’s way more correct. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"><br>
Cheers, David</p>