How to build libtorch for aarch64?

What is the right way to cross-compile libtorch for aarch64 target on an amd64 Linux host?

I’m currently using tools/build_libtorch.py, however, when I try to use this library to build my C++ code using, cmake -DCMAKE_PREFIX_PATH=/path/to/build_libtorch .., I get errors regarding missing header files and libcaffe2_gpu.so.

Is there a script or a tutorial that would help organize the libtorch library similar to the structure released on the downloads page?

libtorch/
  bin/
  include/
  lib/
  share/

You might try to follow the route android takes, providing a CMAKE_TOOLCHAIN_FILE and you might need a host protobuf, see scripts/build_android.sh for inspiration.
I will admit that I did not try but build natively on aarch64 for the time being…

Best regards

Thomas

Any update on this I tray to built it myself too, I got an error when I try simple c++ example from the official website

So this has two parts:

  1. Build libtorch on aarch64,
  2. build libtorch on Android.

Which one are you most interested in?

For 1) you can build PyTorch natively on the largest Raspberry Pi 4 or NVidia Jetsons pretty much by running setup.py. The caveat is that the build may (will) crash with out of memory if you run with -j4. My solution to that has been to then build the large C++ with -j1 and afterwards continue with -j4. More fancy things like distcc probably get better results yet.

For 2) check out the Pytorch mobile build instructions.

Best regards

Thomas

2 Likes

Thanks for your response Tom, Finally I make it work on Jetson Nano, by just using python3 setup.py install . The problem was I built libtorch without cxx 11 abi activated. I reinstall it with cxx 11 abi = 1, run /usr/bin/jetson_clock and I used 1G swap file. works like charm, no crash, it took about 12 hours to complete

2 Likes

Would it be possible for you to leave a few more breadcrumbs about how you did this?
There is very written about how to get LibTorch on a Jetson.

You wrote

using python3 setup.py install . The problem was I built libtorch without cxx 11 abi activated

Would it be possible for you to post the contents if you still have these files with your modifications?

here are full build steps using cross compile tool aarch64 g++
https://fatalfeel.blogspot.com/2013/12/libtorch-cross-compile-on-aarch64-linux.html

Demo:
https://www.mediafire.com/view/1zgp44v08n7tpj6/libtorch_build_ok.png
https://www.mediafire.com/view/fdwap6ldb8zl6w0/torchvision.png

Hi, I follow your blog and tried to cross compile to aarch64, then i could not achieve the same result:

  1. i could not get libtorch_cuda.so after following your instruction to cross compile pytorch source code, just got lictorch_cpu.so and other so files.
  2. after then i changed USE_CUDA=ON & USE_CUDNN=ON, i met some cmake errors.
    could you provide more help, and my target is Jetson TX2, thx.

I use ubuntu 16.04, root login
cuda_10.2.89_440.33.01_linux ,
cuda_10.2.1_linux,
cuda_10.2.2_linux
libcudnn8_8.0.5.39-1+cuda10.2_amd64
libcudnn8-dev_8.0.5.39-1+cuda10.2_amd64
libcudnn8-samples_8.0.5.39-1+cuda10.2_amd64

driver use data center version:
NVIDIA-Linux-x86_64-440.118.02

cmake 3.20.5

CUDA 10 is not supported in recent PyTorch releases anymore so you might need to update to 11.4 or newer if you want to build from source.