ankane / torch.rb

Deep learning for Ruby, powered by LibTorch
Other
704 stars 30 forks source link

gem install fails with clang error (unsupported option '-fopenmp') #26

Closed raoulalwani closed 2 years ago

raoulalwani commented 3 years ago

Thanks for this nice library. When installing on OSX Catalina 10.15.6, I get the following error:

$ gem install torch-rb

=>

Fetching torch-rb-0.8.1.gem
Fetching rice-4.0.2.gem
Successfully installed rice-4.0.2
Building native extensions. This could take a while...
ERROR:  Error installing torch-rb:
    ERROR: Failed to build gem native extension.

    current directory: /Users/name/.rvm/gems/ruby-2.6.3/gems/torch-rb-0.8.1/ext/torch
/Users/name/.rvm/rubies/ruby-2.6.3/bin/ruby -I /Users/name/.rvm/rubies/ruby-2.6.3/lib/ruby/site_ruby/2.6.0 -r ./siteconf20210801-37848-s5jnqv.rb extconf.rb
/Users/name/.rvm/gems/ruby-2.6.3/gems/rice-4.0.2/lib/mkmf-rice.rb:16: warning: already initialized constant MakeMakefile::CONFTEST_C
/Users/name/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/mkmf.rb:259: warning: previous definition of CONFTEST_C was here
checking for rice/rice.hpp in /Users/name/.rvm/gems/ruby-2.6.3/gems/rice-4.0.2/include... yes
checking for -lc++... yes
checking for -lomp... yes
checking for -ltorch... yes
checking for -lmkldnn... no
checking for -lnnpack... no
Generating C++ functions...
creating Makefile

current directory: /Users/name/.rvm/gems/ruby-2.6.3/gems/torch-rb-0.8.1/ext/torch
make "DESTDIR=" clean

current directory: /Users/name/.rvm/gems/ruby-2.6.3/gems/torch-rb-0.8.1/ext/torch
make "DESTDIR="
compiling backends.cpp
clang: error: unsupported option '-fopenmp'
make: *** [backends.o] Error 1

make failed, exit code 2

libtorch was installed successfully via homebrew. Some quick googling showed this: https://stackoverflow.com/questions/43555410/enable-openmp-support-in-clang-in-mac-os-x-sierra-mojave but I'm hesitant to brew install llvm because brew info llvm gives the following warning: "because macOS already provides this software and installing another version in parallel can cause all kinds of trouble."

I already have libomp installed via brew earlier.

ankane commented 3 years ago

Hey @raoulalwani, can you paste the output of g++ --version and clang --version?

raoulalwani commented 3 years ago

@ankane thanks for the quick reply.

$ g++ --version

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.27)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

$ clang --version

Apple clang version 12.0.0 (clang-1200.0.32.27)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

I also checked my libtorch with brew info libtorch:

libtorch: stable 1.5.0 (bottled)
Tensors and dynamic neural networks
https://pytorch.org/
/usr/local/Cellar/libtorch/1.5.0_1 (2,965 files, 102.2MB) *
  Built from source on 2021-08-01 at 19:13:55
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/libtorch.rb

It says 1.5.0 but in #22 there was a discussion about it being in version 1.8.1? I ran brew upgrade libtorch but got this:

Warning: libtorch 1.5.0_1 already installed
ankane commented 3 years ago

Thanks @raoulalwani. Unfortunately, I'm not sure how to reproduce. Apple Clang should support OpenMP with the -Xclang flag. https://github.com/ankane/torch.rb/blob/d851e888b2a4ffb9f0cd4a7b2da89a1941103084/ext/torch/extconf.rb#L11-L14

Repro commands - https://github.com/ankane/torch.rb/blob/5a8f8a33e77fbeb43990e5ab9263d071037281f6/.github/workflows/build.yml CI result - https://github.com/ankane/torch.rb/runs/3225038756?check_suite_focus=true

For LibTorch, you want to use the latest version (1.9.0 at the moment). Try running brew update before brew upgrade libtorch, or download it from https://pytorch.org/.

raoulalwani commented 3 years ago

Thanks @ankane - no worries, will try on my Linux machine this weekend. Will reopen or recomment if I can figure it out for OSX.

raoulalwani commented 2 years ago

Hi @ankane just re-opening this, finally had some time to try this out again.

Stil getting the same error:

$ gem install torch-rb

compiling backends.cpp
clang: error: unsupported option '-fopenmp'

I'm using libtorch 1.9.0, I have libomp, I also installed llvm, and so the outputs for g++ --version and clang --version are:

$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.27)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

$ clang --version
Homebrew clang version 12.0.1
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin

Inspecting the Makefile after the error stops the install:

Line 68: CXX = g++
Line 90: CXXFLAGS = $(CCDLFLAGS) $(cxxflags) -Wno-register -std=c++17 -std=c++17 $(optflags) -D_GLIBCXX_USE_CXX11_ABI=1 -fopenmp -Wno-noexcept-type -Wno-duplicated-cond -Wno-suggest-attribute=noreturn $(ARCH_FLAG)

So the -Xclang flag is not getting added to the Makefile. Modifying the Makefile to either include the -Xclang flag, or changing CXX = clang to use the Homebrew llvm and running make throws a lot of errors (eg: fft_functions.cpp:321:51: error: no viable conversion from 'optional<c10::string_view>' to 'optional<std::string>')so that hasn't worked either.

I have Pytorch working fine with Python, so I can do import torch in Python scripts and use it, but would much prefer exploring it in Ruby instead. Just sharing the above in case anyone has thoughts on how to get it working.

ankane commented 2 years ago

Hey @raoulalwani, with the latest version of the gem, you'll need LibTorch 1.10.0 or 1.10.1 (see compatibility table). Also, llvm shouldn't be needed.

For the -Xclang issue, what does the following command output?

ruby -v
gem -v
ruby -e 'puts RbConfig::CONFIG["CC_VERSION_MESSAGE"]'
raoulalwani commented 2 years ago

Hi @ankane, thanks for the quick reply. Got it on the gem/libtorch versions. Re: the command outputs:

$ ruby -v
ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-darwin15]
$ gem -v
3.0.8
$ ruby -e 'puts RbConfig::CONFIG["CC_VERSION_MESSAGE"]'
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.0 (clang-700.0.72)
Target: x86_64-apple-darwin15.3.0
Thread model: posix

Seems like it's pointing at a super old version of clang? I ran rvm get stable --auto-dotfiles but that isn't updating the RBConfig.

Is there another way to build the gem from source? This issue def seems related to something in my machine, not in the gem itself.

ankane commented 2 years ago

It looks like your Ruby installation was compiled on darwin15 (El Capitan). I'd try creating a fresh installation of Ruby with rvm install 2.6.9 (alternatively, you could do rvm remove 2.6.6 && rvm install 2.6.6).

raoulalwani commented 2 years ago

@ankane thanks - it was indeed the Ruby issue on my machine (restoring an old laptop to a new one using Time Machine).

For anyone else potentially in the same spot: a fresh install of Ruby did the trick, so the output was as follows:

$ ruby -e 'puts RbConfig::CONFIG["CC_VERSION_MESSAGE"]'
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.27)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

The errors I encountered above when running make were because of the incompatible gem/libtorch versions, so since I am still on libtorch 1.9.0, I just did gem install torch-rb -v 0.8.3 (latest compatible gem version with my libtorch version) and everything is working nicely.

Thanks for the nice library!

ankane commented 2 years ago

Great, glad it's working.