LibTorch C++ frontend on Android

I am developing a C++ library with LibTorch and want to use its C++ frontend on Android.

I’ve successfully linked LibTorch to the library by unpacking the official AAR release, but it seems like I cannot use torch/torch.h from it. I found this header in headers/torch/csrc/api/include/torch/torch.h but if I include this directory to use #include <torch/torch.h> in my code I get this error:

In file included from <my project>\app\src\main\cpp\Test.cpp:6:
In file included from <my project>\app\.cxx\Debug\x311e382\x86_64\_deps\torch-src\headers\torch\csrc\api\include\torch\torch.h:3:
In file included from <my project>\app\.cxx\Debug\x311e382\x86_64\_deps\torch-src\headers\torch\csrc\api\include\torch\all.h:13:
In file included from <my project>\app\.cxx\Debug\x311e382\x86_64\_deps\torch-src\headers\torch\csrc\api\include\torch\nn.h:4:
In file included from <my project>\app\.cxx\Debug\x311e382\x86_64\_deps\torch-src\headers\torch\csrc\api\include\torch\nn\functional.h:11:
In file included from <my project>\app\.cxx\Debug\x311e382\x86_64\_deps\torch-src\headers\torch\csrc\api\include\torch\nn\functional\normalization.h:4:
<my project>\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

The Making Native Android Application that uses PyTorch prebuilt libraries article doesn’t explicitly include headers/torch/csrc/api/include directory, so does this mean that PyTorch’s C++ frontend is unavailable on Android?

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

Wow, thanks, just adding PadNd.h from <PyTorch sources dir>/aten/src/ATen/native to <unpacked PyTorch AAR dir>/headers/ATen/native makes the project build and run.

But since I’m just using the prebuild binaries, won’t something break if I actually call something that uses the contents of PadNd.cpp? I mean, is this source file included into the prebuild .so files for Android?

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.

I found this issue about the exact same problem and seems like it hasn’t been fixed yet. Asked there if copying PadNd.h like this will cause any trouble later.

As one of the devs replied in the issue, copying PadNd.h shouldn’t break anything.

1 Like