guillaume-be / rust-bert

Rust native ready-to-use NLP pipelines and transformer-based models (BERT, DistilBERT, GPT2,...)
https://docs.rs/crate/rust-bert
Apache License 2.0
2.67k stars 216 forks source link

Unable to build when using rust-bert as a dependency in another Rust project on M2 Mac Sonoma 14.5 #459

Closed hkfi closed 4 months ago

hkfi commented 4 months ago

I followed the steps mentioned in the docs

I ran the commands mentioned:

brew install pytorch jq
export LIBTORCH=$(brew --cellar pytorch)/$(brew info --json pytorch | jq -r '.[0].installed[0].version')
export LD_LIBRARY_PATH=${LIBTORCH}/lib:$LD_LIBRARY_PATH

I am able to run cargo run --example sentence_embeddings.

However, when creating a new Rust project and adding rust-bert = "0.22.0" as a dependency and running cargo build, I run into the following error:

warning: clang: warning: -Wl,-rpath=/opt/homebrew/Cellar/pytorch/2.2.0_6/lib: 'linker' input unused [-Wunused-command-line-argument]
warning: clang: warning: -Wl,-rpath=/opt/homebrew/Cellar/pytorch/2.2.0_6/lib: 'linker' input unused [-Wunused-command-line-argument]
warning: clang: warning: -Wl,-rpath=/opt/homebrew/Cellar/pytorch/2.2.0_6/lib: 'linker' input unused [-Wunused-command-line-argument]
warning: libtch/torch_api_generated.cpp:640:104: error: reference to type 'const c10::optional<at::Tensor>' could not bind to an rvalue of type 'bool'
warning:     auto outputs__ = torch::_cslt_sparse_mm(*compressed_A, *dense_B, (bias ? *bias : torch::Tensor()), (bool)transpose_result);
warning:                                                                                                        ^~~~~~~~~~~~~~~~~~~~~~
warning: libtch/torch_api.h:19:5: note: expanded from macro 'PROTECT'
warning:     x \
warning:     ^
warning: /opt/homebrew/Cellar/pytorch/2.2.0_6/include/ATen/ops/_cslt_sparse_mm.h:26:173: note: passing argument to parameter 'alpha' here
warning: inline at::Tensor _cslt_sparse_mm(const at::Tensor & compressed_A, const at::Tensor & dense_B, const c10::optional<at::Tensor> & bias={}, const c10::optional<at::Tensor> & alpha={}, c10::optional<at::ScalarType> out_dtype=c10::nullopt, bool transpose_result=false) {
warning:                                                                                                                                                                             ^
warning: 1 error generated.
warning: ToolExecError: Command env -u IPHONEOS_DEPLOYMENT_TARGET "c++" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "--target=arm64-apple-darwin" "-mmacosx-version-min=14.4" "-I" "/opt/homebrew/Cellar/pytorch/2.2.0_6/include" "-I" "/opt/homebrew/Cellar/pytorch/2.2.0_6/include/torch/csrc/api/include" "-Wl,-rpath=/opt/homebrew/Cellar/pytorch/2.2.0_6/lib" "-std=c++17" "-D_GLIBCXX_USE_CXX11_ABI=1" "-o" "/Users/hkfi/code/rust-app/target/debug/build/torch-sys-83b42f94b3128dbd/out/19072f24a82f85ae-torch_api_generated.o" "-c" "libtch/torch_api_generated.cpp" with args c++ did not execute successfully (status code exit status: 1).

error: failed to run custom build command for `torch-sys v0.14.0`

Caused by:
  process didn't exit successfully: `/Users/hkfi/code/rust-app/target/debug/build/torch-sys-053c39dc52413439/build-script-build` (exit status: 1)
  --- stdout
  cargo:rerun-if-env-changed=LIBTORCH_USE_PYTORCH
  cargo:rerun-if-env-changed=LIBTORCH
  cargo:rerun-if-env-changed=LIBTORCH_INCLUDE
  cargo:rerun-if-env-changed=LIBTORCH_LIB
  cargo:rerun-if-env-changed=LIBTORCH_CXX11_ABI
  cargo:rerun-if-env-changed=LIBTORCH_STATIC
  cargo:rustc-link-search=native=/opt/homebrew/Cellar/pytorch/2.2.0_6/lib
  cargo:rerun-if-changed=libtch/fake_cuda_dependency.cpp
  cargo:rerun-if-changed=libtch/torch_python.cpp
  cargo:rerun-if-changed=libtch/torch_python.h
  cargo:rerun-if-changed=libtch/torch_api_generated.cpp
  cargo:rerun-if-changed=libtch/torch_api_generated.h
  cargo:rerun-if-changed=libtch/torch_api.cpp
  cargo:rerun-if-changed=libtch/torch_api.h
  cargo:rerun-if-changed=libtch/stb_image_write.h
  cargo:rerun-if-changed=libtch/stb_image_resize.h
  cargo:rerun-if-changed=libtch/stb_image.h
  cargo:libtorch_lib=/opt/homebrew/Cellar/pytorch/2.2.0_6/lib
  OUT_DIR = Some(/Users/hkfi/code/rust-app/target/debug/build/torch-sys-83b42f94b3128dbd/out)
  TARGET = Some(aarch64-apple-darwin)
  OPT_LEVEL = Some(0)
  HOST = Some(aarch64-apple-darwin)
  cargo:rerun-if-env-changed=CXX_aarch64-apple-darwin
  CXX_aarch64-apple-darwin = None
  cargo:rerun-if-env-changed=CXX_aarch64_apple_darwin
  CXX_aarch64_apple_darwin = None
  cargo:rerun-if-env-changed=HOST_CXX
  HOST_CXX = None
  cargo:rerun-if-env-changed=CXX
  CXX = None
  cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT
  RUSTC_WRAPPER = None
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some(true)
  cargo:rerun-if-env-changed=MACOSX_DEPLOYMENT_TARGET
  MACOSX_DEPLOYMENT_TARGET = None
  cargo:rerun-if-env-changed=CXXFLAGS_aarch64-apple-darwin
  CXXFLAGS_aarch64-apple-darwin = None
  cargo:rerun-if-env-changed=CXXFLAGS_aarch64_apple_darwin
  CXXFLAGS_aarch64_apple_darwin = None
  cargo:rerun-if-env-changed=HOST_CXXFLAGS
  HOST_CXXFLAGS = None
  cargo:rerun-if-env-changed=CXXFLAGS
  CXXFLAGS = None
  cargo:warning=clang: warning: -Wl,-rpath=/opt/homebrew/Cellar/pytorch/2.2.0_6/lib: 'linker' input unused [-Wunused-command-line-argument]
  cargo:warning=clang: warning: -Wl,-rpath=/opt/homebrew/Cellar/pytorch/2.2.0_6/lib: 'linker' input unused [-Wunused-command-line-argument]
  cargo:warning=clang: warning: -Wl,-rpath=/opt/homebrew/Cellar/pytorch/2.2.0_6/lib: 'linker' input unused [-Wunused-command-line-argument]
  cargo:warning=libtch/torch_api_generated.cpp:640:104: error: reference to type 'const c10::optional<at::Tensor>' could not bind to an rvalue of type 'bool'
  cargo:warning=    auto outputs__ = torch::_cslt_sparse_mm(*compressed_A, *dense_B, (bias ? *bias : torch::Tensor()), (bool)transpose_result);
  cargo:warning=                                                                                                       ^~~~~~~~~~~~~~~~~~~~~~
  cargo:warning=libtch/torch_api.h:19:5: note: expanded from macro 'PROTECT'
  cargo:warning=    x \
  cargo:warning=    ^
  cargo:warning=/opt/homebrew/Cellar/pytorch/2.2.0_6/include/ATen/ops/_cslt_sparse_mm.h:26:173: note: passing argument to parameter 'alpha' here
  cargo:warning=inline at::Tensor _cslt_sparse_mm(const at::Tensor & compressed_A, const at::Tensor & dense_B, const c10::optional<at::Tensor> & bias={}, const c10::optional<at::Tensor> & alpha={}, c10::optional<at::ScalarType> out_dtype=c10::nullopt, bool transpose_result=false) {
  cargo:warning=                                                                                                                                                                            ^
  cargo:warning=1 error generated.
  exit status: 0
  exit status: 1
  cargo:warning=ToolExecError: Command env -u IPHONEOS_DEPLOYMENT_TARGET "c++" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "--target=arm64-apple-darwin" "-mmacosx-version-min=14.4" "-I" "/opt/homebrew/Cellar/pytorch/2.2.0_6/include" "-I" "/opt/homebrew/Cellar/pytorch/2.2.0_6/include/torch/csrc/api/include" "-Wl,-rpath=/opt/homebrew/Cellar/pytorch/2.2.0_6/lib" "-std=c++17" "-D_GLIBCXX_USE_CXX11_ABI=1" "-o" "/Users/hkfi/code/rust-app/target/debug/build/torch-sys-83b42f94b3128dbd/out/19072f24a82f85ae-torch_api_generated.o" "-c" "libtch/torch_api_generated.cpp" with args c++ did not execute successfully (status code exit status: 1).
  exit status: 0

  --- stderr

  error occurred: Command env -u IPHONEOS_DEPLOYMENT_TARGET "c++" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "--target=arm64-apple-darwin" "-mmacosx-version-min=14.4" "-I" "/opt/homebrew/Cellar/pytorch/2.2.0_6/include" "-I" "/opt/homebrew/Cellar/pytorch/2.2.0_6/include/torch/csrc/api/include" "-Wl,-rpath=/opt/homebrew/Cellar/pytorch/2.2.0_6/lib" "-std=c++17" "-D_GLIBCXX_USE_CXX11_ABI=1" "-o" "/Users/hkfi/code/rust-app/target/debug/build/torch-sys-83b42f94b3128dbd/out/19072f24a82f85ae-torch_api_generated.o" "-c" "libtch/torch_api_generated.cpp" with args c++ did not execute successfully (status code exit status: 1).

I checked that the variables LIBTORCH and LD_LIBRARY_PATH were set correctly with echo:

echo $LIBTORCH
/opt/homebrew/Cellar/pytorch/2.2.0_6
echo $LD_LIBRARY_PATH
/opt/homebrew/Cellar/pytorch/2.2.0_6/lib

LD_LIBRARY_PATH when run with the commands in the docs will actually result in /opt/homebrew/Cellar/pytorch/2.2.0_6/lib: instead of /opt/homebrew/Cellar/pytorch/2.2.0_6/lib as mentioned here. I tried both and they both fail to build.

hkfi commented 4 months ago

Turns out setting the LIBTORCH and LD__LIBRARY_PATH variables as mentioned in the docs is no longer necessary as of two weeks ago due to an update adding the features=["download-libtorch] flag to the tch dependency.

I managed to get a fresh Rust project to compile with rust-bert as a dependency by adding the same flag, so the dep looks like this:

rust-bert = {version = "0.22.0", features = ["download-libtorch"]}

It will not work with rust-bert = "0.22.0", and trying to point LIBTORCH to the pytorch that was installed via Homebrew like mentioned in the docs like:

export LIBTORCH=$(brew --cellar pytorch)/$(brew info --json pytorch | jq -r '.[0].installed[0].version')
export LD_LIBRARY_PATH=${LIBTORCH}/lib:$LD_LIBRARY_PATH
oiwn commented 3 months ago

@hkfi Are you able to build release version?