Compiling LibTorch from sources working recipe

ipoletaev: <p>I wonder if there are any tested recipes available to compile LibTorch from sources that result in the exactly same ready-to-use package as one that is linked on the main page <a href="https://pytorch.org/" rel="noopener nofollow ugc">here</a>?</p>
<p>Instructions mentioned <a href="https://github.com/pytorch/pytorch/blob/main/docs/libtorch.rst#building-libtorch-using-cmake" rel="noopener nofollow ugc">here</a> don’t really work well. For example, for the image <code>nvidia/cuda:11.7.1-cudnn8-devel-ubuntu20.04</code> while trying to build a release branch:</p>
<pre><code class="lang-auto">WORKDIR /workspace
RUN git clone -b v2.0.0 --recurse-submodule https://github.com/pytorch/pytorch.git
WORKDIR /workspace/pytorch/build
ARG TORCH_CUDA_ARCH_LIST="Ampere"
RUN cmake \
    -DBUILD_SHARED_LIBS:BOOL=ON \ 
    -DCMAKE_BUILD_TYPE:STRING=Release \
    -DPYTHON_EXECUTABLE:PATH=`which python3` \
    -DBUILD_PYTHON:BOOL=OFF \
    -DCMAKE_INSTALL_PREFIX:/workspace/libtorch .. &amp;&amp; \
    cmake --build . --target install --config Release -- -j$(nproc)
</code></pre>
<p>installation won’t results all the shared libraries created, e.g. <code>libnvfuser_codegen.so</code> is missing, etc.<br>
There are a few related and still un-answered questions: <a href="https://discuss.pytorch.org/t/compile-libtorch-c-api-from-source/81624/2">this</a> and <a href="https://discuss.pytorch.org/t/compile-a-model-with-libtorch-locally-built-from-the-source/170068">this</a>.</p>
<p>Thanks in advance!</p>

ptrblck: <p>The <a href="https://github.com/pytorch/builder"><code>pytorch/builder</code></a> repository contains the scripts we use to build these binaries so you could take a look at it.</p>

ipoletaev: <p>Thanks! Indeed, as I thought the build’s logic copies <a href="https://github.com/pytorch/builder/blob/7ce69b2658701eaa9fcdc6d118549c172fd5104e/manywheel/build_libtorch.sh#L146" rel="noopener nofollow ugc">everything</a> was compiled.</p>
<p>Maybe worth adding a mention of it inside torch’s repo docs to not confuse others in the future?</p>

ptrblck: <p>This might be a good idea! Would you be interested in updating the docs?</p>

ipoletaev: <p>Well, before doing so I’d need to find our why the docker cmake command mentioned above leads to the <code>libtorch</code> build with which on a simple <a href="https://pytorch.org/tutorials/advanced/cpp_export.html" rel="noopener nofollow ugc">toy example</a> I get:</p>
<pre><code class="lang-auto">what():  Type c10::intrusive_ptr&lt;LinearPackedParamsBase&gt; could not be converted to any of the known types.
Exception raised from operator() at /workspace/pytorch/aten/src/ATen/core/jit_type.h:1793 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;) + 0x60 (0x7fca98ab29a0 in /app/libs/libc10.so)
frame #1: c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::__cxx11:
</code></pre>
<p>issue ? This one looks to be <a href="https://github.com/pytorch/pytorch/issues/83255" rel="noopener nofollow ugc">similar</a> but already fixed…</p>