tch-rs
tch-rs copied to clipboard
M1 Mac Installation
I have an M1 Mac running Monterrey 12.1.
I installed pytorch via:
$ conda create --name tensorflow_m1 python==3.9
$ conda activate tensorflow_m1
$ conda install -c apple tensorflow-deps
$ pip install tensorflow-macos
$ pip install tensorflow-metal
And then set vars like:
export TORCH=/Users/user/miniforge3/pkgs/pytorch-1.12.1-py3.9_0/lib/python3.9/site-packages/torch
export LIBTORCH=$TORCH
export LD_LIBRARY_PATH=${LIBTORCH}/lib:$LD_LIBRARY_PATH
Then run a project via:
cargo run
which yields:
The following warnings were emitted during compilation:
warning: clang: warning: -Wl,-rpath=/Users/kylefahey/miniforge3/pkgs/pytorch-1.12.1-py3.9_0/lib/python3.9/site-packages/torch/lib: 'linker' input unused [-Wunused-command-line-argument]
warning: libtch/torch_api.cpp:142:9: error: no member named '_amp_non_finite_check_and_unscale_' in namespace 'at'; did you mean '_amp_foreach_non_finite_check_and_unscale_'?
warning: at::_amp_non_finite_check_and_unscale_(*t, *found_inf, *inf_scale);
warning: ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
warning: _amp_foreach_non_finite_check_and_unscale_
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: /Users/kylefahey/miniforge3/pkgs/pytorch-1.12.1-py3.9_0/lib/python3.9/site-packages/torch/include/ATen/ops/_amp_foreach_non_finite_check_and_unscale.h:26:23: note: '_amp_foreach_non_finite_check_and_unscale_' declared here
warning: TORCH_API inline void _amp_foreach_non_finite_check_and_unscale_(at::TensorList self, at::Tensor & found_inf, const at::Tensor & inv_scale) {
warning: ^
warning: libtch/torch_api.cpp:487:109: error: no viable conversion from 'vector<torch::autograd::Edge>' to 'bool'
warning: auto vl = torch::autograd::Engine::get_default_engine().execute(roots, grads, keep_graph, create_graph, inputs_);
warning: ^~~~~~~
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: /Users/kylefahey/miniforge3/pkgs/pytorch-1.12.1-py3.9_0/lib/python3.9/site-packages/torch/include/torch/csrc/autograd/engine.h:304:12: note: passing argument to parameter 'accumulate_grad' here
warning: bool accumulate_grad,
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:187:29: error: no member named '_addmv_impl_' in namespace 'torch'
warning: auto outputs__ = torch::_addmv_impl_(*self, *self2, *mat, *vec);
warning: ~~~~~~~^
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:210:29: error: no member named '_amp_update_scale' in namespace 'torch'; did you mean '_amp_update_scale_'?
warning: auto outputs__ = torch::_amp_update_scale(*growth_tracker, *current_scale, *found_inf, scale_growth_factor, scale_backoff_factor, growth_interval);
warning: ~~~~~~~^~~~~~~~~~~~~~~~~
warning: _amp_update_scale_
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: /Users/kylefahey/miniforge3/pkgs/pytorch-1.12.1-py3.9_0/lib/python3.9/site-packages/torch/include/ATen/ops/_amp_update_scale.h:26:31: note: '_amp_update_scale_' declared here
warning: TORCH_API inline at::Tensor & _amp_update_scale_(at::Tensor & self, at::Tensor & growth_tracker, const at::Tensor & found_inf, double scale_growth_factor, double scale_backoff_factor, int64_t growth_interval) {
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:217:29: error: no member named '_baddbmm_mkl_' in namespace 'torch'
warning: auto outputs__ = torch::_baddbmm_mkl_(*self, *batch1, *batch2);
warning: ~~~~~~~^
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:224:29: error: no member named '_bmm' in namespace 'torch'
warning: auto outputs__ = torch::_bmm(*self, *mat2, (bool)deterministic);
warning: ~~~~~~~^
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:231:29: error: no member named '_bmm_out' in namespace 'torch'
warning: auto outputs__ = torch::_bmm_out(*out, *self, *mat2, (bool)deterministic);
warning: ~~~~~~~^
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:294:29: error: no member named '_cat' in namespace 'torch'; did you mean 'cat'?
warning: auto outputs__ = torch::_cat(of_carray_tensor(tensors_data, tensors_len), dim);
warning: ~~~~~~~^~~~
warning: cat
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: /Users/kylefahey/miniforge3/pkgs/pytorch-1.12.1-py3.9_0/lib/python3.9/site-packages/torch/include/ATen/ops/cat.h:26:29: note: 'cat' declared here
warning: TORCH_API inline at::Tensor cat(at::TensorList tensors, int64_t dim=0) {
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:301:29: error: no member named '_cat_out' in namespace 'torch'; did you mean 'cat_out'?
warning: auto outputs__ = torch::_cat_out(*out, of_carray_tensor(tensors_data, tensors_len), dim);
warning: ~~~~~~~^~~~~~~~
warning: cat_out
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: /Users/kylefahey/miniforge3/pkgs/pytorch-1.12.1-py3.9_0/lib/python3.9/site-packages/torch/include/ATen/ops/cat.h:31:31: note: 'cat_out' declared here
warning: TORCH_API inline at::Tensor & cat_out(at::Tensor & out, at::TensorList tensors, int64_t dim=0) {
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:315:29: error: no member named '_cholesky_helper' in namespace 'torch'
warning: auto outputs__ = torch::_cholesky_helper(*self, (bool)upper);
warning: ~~~~~~~^
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:371:29: error: no member named '_convolution_nogroup' in namespace 'torch'
warning: auto outputs__ = torch::_convolution_nogroup(*input, *weight, (bias ? *bias : torch::Tensor()), torch::IntArrayRef(stride_data, stride_len), torch::IntArrayRef(padding_data, padding_len), torch::IntArrayRef(dilation_data, dilation_len), (bool)transposed, torch::IntArrayRef(output_padding_data, output_padding_len));
warning: ~~~~~~~^
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:415:386: error: too few arguments to function call, expected 16, have 15
warning: auto outputs__ = torch::_cudnn_rnn(*input, of_carray_tensor(weight_data, weight_len), weight_stride0, (weight_buf ? *weight_buf : torch::Tensor()), *hx, (cx ? *cx : torch::Tensor()), mode, hidden_size, num_layers, (bool)batch_first, dropout, (bool)train, (bool)bidirectional, torch::IntArrayRef(batch_sizes_data, batch_sizes_len), (dropout_state ? *dropout_state : torch::Tensor()));
warning: ~~~~~~~~~~~~~~~~~ ^
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: /Users/kylefahey/miniforge3/pkgs/pytorch-1.12.1-py3.9_0/lib/python3.9/site-packages/torch/include/ATen/ops/_cudnn_rnn.h:26:87: note: '_cudnn_rnn' declared here
warning: TORCH_API inline ::std::tuple<at::Tensor,at::Tensor,at::Tensor,at::Tensor,at::Tensor> _cudnn_rnn(const at::Tensor & input, at::TensorList weight, int64_t weight_stride0, const c10::optional<at::Tensor> & weight_buf, const at::Tensor & hx, const c10::optional<at::Tensor> & cx, int64_t mode, int64_t hidden_size, int64_t proj_size, int64_t num_layers, bool batch_first, double dropout, bool train, bool bidirectional, at::IntArrayRef batch_sizes, const c10::optional<at::Tensor> & dropout_state) {
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:426:203: error: too few arguments to function call, expected 9, have 8
warning: auto outputs__ = torch::_cudnn_rnn_flatten_weight(of_carray_tensor(weight_arr_data, weight_arr_len), weight_stride0, input_size, mode, hidden_size, num_layers, (bool)batch_first, (bool)bidirectional);
warning: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: /Users/kylefahey/miniforge3/pkgs/pytorch-1.12.1-py3.9_0/lib/python3.9/site-packages/torch/include/ATen/ops/_cudnn_rnn_flatten_weight.h:26:29: note: '_cudnn_rnn_flatten_weight' declared here
warning: TORCH_API inline at::Tensor _cudnn_rnn_flatten_weight(at::TensorList weight_arr, int64_t weight_stride0, int64_t input_size, int64_t mode, int64_t hidden_size, int64_t proj_size, int64_t num_layers, bool batch_first, bool bidirectional) {
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:433:29: error: no member named '_cumprod' in namespace 'torch'; did you mean 'cumprod'?
warning: auto outputs__ = torch::_cumprod(*self, dim);
warning: ~~~~~~~^~~~~~~~
warning: cumprod
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: /Users/kylefahey/miniforge3/pkgs/pytorch-1.12.1-py3.9_0/lib/python3.9/site-packages/torch/include/ATen/ops/cumprod.h:26:29: note: 'cumprod' declared here
warning: TORCH_API inline at::Tensor cumprod(const at::Tensor & self, int64_t dim, c10::optional<at::ScalarType> dtype=c10::nullopt) {
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:440:29: error: no member named '_cumprod_out' in namespace 'torch'; did you mean 'cumprod_out'?
warning: auto outputs__ = torch::_cumprod_out(*out, *self, dim);
warning: ~~~~~~~^~~~~~~~~~~~
warning: cumprod_out
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: /Users/kylefahey/miniforge3/pkgs/pytorch-1.12.1-py3.9_0/lib/python3.9/site-packages/torch/include/ATen/ops/cumprod.h:31:31: note: 'cumprod_out' declared here
warning: TORCH_API inline at::Tensor & cumprod_out(at::Tensor & out, const at::Tensor & self, int64_t dim, c10::optional<at::ScalarType> dtype=c10::nullopt) {
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:447:29: error: no member named '_cumsum' in namespace 'torch'; did you mean 'cumsum'?
warning: auto outputs__ = torch::_cumsum(*self, dim);
warning: ~~~~~~~^~~~~~~
warning: cumsum
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: /Users/kylefahey/miniforge3/pkgs/pytorch-1.12.1-py3.9_0/lib/python3.9/site-packages/torch/include/ATen/ops/cumsum.h:26:29: note: 'cumsum' declared here
warning: TORCH_API inline at::Tensor cumsum(const at::Tensor & self, int64_t dim, c10::optional<at::ScalarType> dtype=c10::nullopt) {
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:454:29: error: no member named '_cumsum_out' in namespace 'torch'; did you mean 'cumsum_out'?
warning: auto outputs__ = torch::_cumsum_out(*out, *self, dim);
warning: ~~~~~~~^~~~~~~~~~~
warning: cumsum_out
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: /Users/kylefahey/miniforge3/pkgs/pytorch-1.12.1-py3.9_0/lib/python3.9/site-packages/torch/include/ATen/ops/cumsum.h:31:31: note: 'cumsum_out' declared here
warning: TORCH_API inline at::Tensor & cumsum_out(at::Tensor & out, const at::Tensor & self, int64_t dim, c10::optional<at::ScalarType> dtype=c10::nullopt) {
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:492:110: error: no viable conversion from 'torch::Tensor' to 'int64_t' (aka 'long long')
warning: auto outputs__ = torch::_embedding_bag_dense_backward(*grad, *indices, *offsets, *offset2bag, *bag_size, *maximum_indices, num_weights, (bool)scale_grad_by_freq, mode, (per_sample_weights ? *per_sample_weights : torch::Tensor()));
warning: ^~~~~~~~~~~~~~~~
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: /Users/kylefahey/miniforge3/pkgs/pytorch-1.12.1-py3.9_0/lib/python3.9/site-packages/torch/include/ATen/ops/_embedding_bag_dense_backward.h:26:216: note: passing argument to parameter 'num_weights' here
warning: TORCH_API inline at::Tensor _embedding_bag_dense_backward(const at::Tensor & grad, const at::Tensor & indices, const at::Tensor & offset2bag, const at::Tensor & bag_size, const at::Tensor & maximum_indices, int64_t num_weights, bool scale_grad_by_freq, int64_t mode, const c10::optional<at::Tensor> & per_sample_weights, int64_t padding_idx=-1) {
warning: ^
warning: In file included from libtch/torch_api.cpp:1215:
warning: libtch/torch_api_generated.cpp.h:576:29: error: no member named '_fft_with_size' in namespace 'torch'
warning: auto outputs__ = torch::_fft_with_size(*self, signal_ndim, (bool)complex_input, (bool)complex_output, (bool)inverse, torch::IntArrayRef(checked_signal_sizes_data, checked_signal_sizes_len), (bool)normalized, (bool)onesided, torch::IntArrayRef(output_sizes_data, output_sizes_len));
warning: ~~~~~~~^
warning: libtch/torch_api.h:16:5: note: expanded from macro 'PROTECT'
warning: x \
warning: ^
warning: fatal error: too many errors emitted, stopping now [-ferror-limit=]
warning: 20 errors generated.
error: failed to run custom build command for `torch-sys v0.3.1`
How do I get this working?
I don't have access to an arm based mac so it's a bit tricky for me to help here. One thing to point out is that the current version of tch-rs
is only compatible with libtorch 1.12.0, not with 1.12.1.
I've installed libtorch 1.12.0 from the PyTorch downloads for MacOS. I don't see the errors above but get a linker error:
at::unsqueeze(at::Tensor const&, long long) in libtorch_sys-fb02243f6d43387f.rlib(torch_api.o) "torch::jit::load(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, c10::optional<c10::Device>)", referenced from: _at_load_callback_with_device in libtorch_sys-fb02243f6d43387f.rlib(torch_api.o) "caffe2::TypeMeta::error_unsupported_typemeta(caffe2::TypeMeta)", referenced from: caffe2::TypeMeta::toScalarType() in libtorch_sys-fb02243f6d43387f.rlib(torch_api.o) "torch::serialize::OutputArchive::write(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, at::Tensor const&, bool)", referenced from: _at_save_multi in libtorch_sys-fb02243f6d43387f.rlib(torch_api.o) "at::_ops::to_dtype_layout::call(at::Tensor const&, c10::optional<c10::ScalarType>, c10::optional<c10::Layout>, c10::optional<c10::Device>, c10::optional<bool>, bool, bool, c10::optional<c10::MemoryFormat>)", referenced from: at::Tensor::to(c10::TensorOptions, bool, bool, c10::optional<c10::MemoryFormat>) const in libtorch_sys-fb02243f6d43387f.rlib(torch_api.o) ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
Turns out the MacOS libraries available from the PyTorch website is for x86. I managed to get it to work by installing it via Homebrew and pointing the LIBTORCH environment variable to that version.
brew install libtorch
LIBTORCH = '/opt/homebrew/Cellar/libtorch/1.12.1'
Thanks joverwey.
This is amazing I got your approach to work for simple app:
https://github.com/LaurentMazare/tch-rs/blob/main/examples/basics.rs
export LIBTORCH=/opt/homebrew/Cellar/libtorch/1.12.1
cargo -v build
cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.10s
Running `target/debug/tch1`
3
1
4
1
5
[ CPUIntType{5} ]
-0.6856 -0.4743 -0.1416 -0.7355
0.2707 -0.9785 -0.2258 -0.2789
1.1346 -0.5988 -0.2379 1.3412
0.4052 0.3510 -0.8027 0.2231
-1.1766 0.5208 -0.5843 0.6955
[ CPUFloatType{5,4} ]
0.8144 1.0257 1.3584 0.7645
1.7707 0.5215 1.2742 1.2211
2.6346 0.9012 1.2621 2.8412
1.9052 1.8510 0.6973 1.7231
0.3234 2.0208 0.9157 2.1955
[ CPUFloatType{5,4} ]
1.8144 2.0257 2.3584 1.7645
2.7707 1.5215 2.2742 2.2211
3.6346 1.9012 2.2621 3.8412
2.9052 2.8510 1.6973 2.7231
1.3234 3.0208 1.9157 3.1955
[ CPUFloatType{5,4} ]
43.1000
44.1000
45.1000
[ CPUFloatType{3} ]
[3] 44.099998474121094
42
5
Cuda available: false
Cudnn available: false
GPU on M1
Is there a way to get the M1's GPU to work?
I followed the same steps, however when installing libtorch it was giving me a warning about it being deprecated and pointing towards pytorch. After doing the brew install, there was no libtorch, so instead of
brew install libtorch
LIBTORCH = '/opt/homebrew/Cellar/libtorch/1.12.1'
I did
brew install libtorch
export LIBTORCH = '/opt/homebrew/Cellar/pytorch/1.12.1'
and it works, but only in a terminal, where I have added the export declartion. So before running cargo run (or other cargo commands) I have to run the command export LIBTORCH = '/opt/homebrew/Cellar/pytorch/1.12.1'
Now I need to figure out where to put it correctly, so all my terminals have it when starting.
Hope it helps.
I was able to get libtorch/pytorch to work on my M1 mac running Monterey. I ran the following command:
brew install pytorch
I then set the following environmental variables:
LIBTORCH = '/opt/homebrew/Cellar/pytorch/1.12.1/'
LD_LIBRARY_PATH = '${LIBTORCH}/lib:$LD_LIBRARY_PATH'
This was able to build for me.
Quick update for M1 users:
Seems the latest Cellar directory changed from 'libtorch' to 'pytorch'.
/opt/homebrew/Cellar/pytorch/1.*.*/
@a2aaron 's comment above mentions it; but I didn't immediately notice it changed over and isn't something brew will outright alert you on.
Closing this in favor of #488