Search code examples
rustpytorchrust-cargolibtorch

Issue building Libtorch Rust Projects


Apologies if this isn't the best place to post this.

I'm trying to learn how to do Pytorch in Rust. Have previous experience with Libtorch in C++ and Pytorch in Python. I'm running into issues setting up the installation, and am unsure of why.
I first tried downloading current Libtorch, and then attempting to link against it. am using these Rust bindings https://github.com/LaurentMazare/tch-rs .
I added the following to my .bashrc file:

     export LIBTORCH=/home/me/libtorch/
     export LIBTORCH_INCLUDE=/home/me/libtorch/
     export LIBTORCH_LIB=/home/me/libtorch/

I then added the dependency to my Cargo.toml, and cargo build works
However, cargo run results in

    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/torch_test`
target/debug/torch_test: error while loading shared libraries: libtorch_cpu.so: cannot open shared object file: No such file or directory

Upon looking under the /home/me/libtorch/lib/ there is indeed a libtorch_cpu.so file.

Any idea why its failing? I'm rather frustrated because I'm very eager to try a Rust ML project idea, but can't.

I also have Pytorch installed, and tried doing (in .bashrc)

export LIBTORCH_USE_PYTORCH=1

so it would use the Pytorch install instead of the manually downloaded Libtorch files, but puzzlingly, it gives the same error? Either it's only ever looking into the Python libraries or linking is generally messed up.

Updated my Fedora installation, conda and other dependencies etc. Have the latest cargo installed.

Adding /home/me/libtorch/lib/ to LD_LIBRARY_PATH variable did do something, but now I get the error

target/debug/torch_test: symbol lookup error: target/debug/torch_test: undefined symbol: _ZN3c106detail23torchInternalAssertFailEPKcS2_jS2_RKSs

Solution

  • Solved, thanks to a comment on Reddit and browsing Pytorch forums.

    Solution was to:

    1. Modify LD_LIBRARY_PATH as the README for the project says at the bottom.
    2. Switch to the non-ABI version of Libtorch. I use the ABI version for other C++ projects, but here the non-ABI version was needed. Using the ABI version would lead to successful linking, but then produce undefined symbol errors.