LibTorch C++ frontend on Android

TimPushkin: <p>I am developing a C++ library with LibTorch and want to use its C++ frontend on Android.</p>
<p>I’ve successfully linked LibTorch to the library by unpacking the official AAR release, but it seems like I cannot use <code>torch/torch.h</code> from it. I found this header in <code>headers/torch/csrc/api/include/torch/torch.h</code> but if I include this directory to use <code>#include &lt;torch/torch.h&gt;</code> in my code I get this error:</p>
<pre><code class="lang-auto">In file included from &lt;my project&gt;\app\src\main\cpp\Test.cpp:6:
In file included from &lt;my project&gt;\app\.cxx\Debug\x311e382\x86_64\_deps\torch-src\headers\torch\csrc\api\include\torch\torch.h:3:
In file included from &lt;my project&gt;\app\.cxx\Debug\x311e382\x86_64\_deps\torch-src\headers\torch\csrc\api\include\torch\all.h:13:
In file included from &lt;my project&gt;\app\.cxx\Debug\x311e382\x86_64\_deps\torch-src\headers\torch\csrc\api\include\torch\nn.h:4:
In file included from &lt;my project&gt;\app\.cxx\Debug\x311e382\x86_64\_deps\torch-src\headers\torch\csrc\api\include\torch\nn\functional.h:11:
In file included from &lt;my project&gt;\app\.cxx\Debug\x311e382\x86_64\_deps\torch-src\headers\torch\csrc\api\include\torch\nn\functional\normalization.h:4:
&lt;my project&gt;\app\.cxx\Debug\x311e382\x86_64\_deps\torch-src\headers\torch\csrc\api\include\torch\nn\functional\padding.h:4:10: fatal error: 'ATen/native/PadNd.h' file not found
</code></pre>
<p>The <a href="https://pytorch.org/tutorials/recipes/android_native_app_with_custom_op.html" rel="noopener nofollow ugc">Making Native Android Application that uses PyTorch prebuilt libraries</a> article doesn’t explicitly include <code>headers/torch/csrc/api/include</code> directory, so does this mean that PyTorch’s C++ frontend is unavailable on Android?</p>

scheckmedia: <p>Hi Tim, I had the same problem and was able to solve it as I described here: <a href="https://discuss.pytorch.org/t/cmake-error-during-pytorch-mobile-custom-build/112843/9">https://discuss.pytorch.org/t/cmake-error-during-pytorch-mobile-custom-build/112843/9?u=scheckmedia</a>. After that I was able to compile everything as expected.</p>

TimPushkin: <p>Wow, thanks, just adding <code>PadNd.h</code> from <code>&lt;PyTorch sources dir&gt;/aten/src/ATen/native</code> to <code>&lt;unpacked PyTorch AAR dir&gt;/headers/ATen/native</code> makes the project build and run.</p>
<p>But since I’m just using the prebuild binaries, won’t something break if I actually call something that uses the contents of <code>PadNd.cpp</code>? I mean, is this source file included into the prebuild .so files for Android?</p>

scheckmedia: <p>That’s a good question I have no answer for. I’ve compiled libtorch with the changes I made manually and never observed any issues but I think too, using the prebuild binaries and the applied changes might produce issues.</p>

TimPushkin: <p>I found <a href="https://github.com/pytorch/pytorch/issues/81198" rel="noopener nofollow ugc">this issue</a> about the exact same problem and seems like it hasn’t been fixed yet. Asked there if copying <code>PadNd.h</code> like this will cause any trouble later.</p>

TimPushkin: <p>As one of the devs replied in the issue, copying <code>PadNd.h</code> shouldn’t break anything.</p>