libtorch_cpu.dylib not found
I am trying to compile the sentence embedding example locally. It compiles, but the resulting binary gives the error:
dyld[32110]: Library not loaded: @rpath/libtorch_cpu.dylib Referenced from: <64A42FED-1ACF-3448-B0A1-FC382B5454DE> /Users/rpaul1/Documents/test/rust_bert2/target/release/rust_bert2 Reason: no LC_RPATH's found
I have passed the path of the dylib binary but it still does not work. export INSTALL_RPATH=rust_bert2/target/release/build/torch-sys-87156a8d82f3a58d/out/libtorch/libtorch/lib, export BUILD_RPATH=rust_bert2/target/release/build/torch-sys-87156a8d82f3a58d/out/libtorch/libtorch and export LC_RPATH=${BUILD_RPATH}/lib:$LC_RPATH. I tried defining the variable LC_RPATH but it still does not work.
Can somebody suggest how to pass the path of the dylib to the rust binary?
On macOS I tried installing libtorch via brew and adding the variables export LIBTORCH=/opt/homebrew/Cellar/pytorch/2.8.0_3 && export LD_LIBRARY_PATH=${LIBTORCH}/lib:$LD_LIBRARY_PATH as mentioned in the documentation, but I still get the same error message.
Hombrew installs the latest version while rust-bert is compatible with PyTorch 2.4. I am successfully using the download-libtorch feature to build rust-bert:
rust-bert = { version = "0.23.0", features = [ "download-libtorch" ] }
Alternatively:
mkdir libtorch
cd libtorch
python3.12 -m venv .venv
source .venv/bin/activate
pip download torch==2.4.1
deactivate
unzip torch-2.4.1-cp312-none-macosx_11_0_arm64.whl "torch/lib/*"
You now have a torch/lib directory that contains a libtorch and its dependencies compatible with rust-bert.
You then point to it with a .cargo/config.tomlfile in your project:
[target.aarch64-apple-darwin]
rustflags = "-lc++ -l framework=Accelerate -C link-args=-Wl,-rpath,/Users/foobar/.local/share/torch/lib"
You may want to adapt above scripts to your Mac architecture (intel vs apple silicon), and location of the torch lib directory. This way the path to your libtorch library will be hardcoded in your binary, no need for LD_LIBRARY_PATH or other trick to make your binary work on your system.