LaurentMazare / tch-rs

Rust bindings for the C++ api of PyTorch.
Apache License 2.0
4.02k stars 317 forks source link

Trying to run basic examples, but I think I have some issues with my config #177

Closed NicolasVidal closed 3 years ago

NicolasVidal commented 4 years ago

Hello there :) I wanted to give this crate a go and was attracted by the apparent simplijcity of usage. I downloaded libtorch 1.5 from pytorch website and filled the corresponding Environment Variables I tried to start a new cargo project and just put :

[dependencies]
tch = "0.1.6"

in my cargo.toml file.

I used the first main in example :

extern crate tch;
use tch::Tensor;

fn main() {
    let t = Tensor::of_slice(&[3, 1, 4, 1, 5]);
    let t = t * 2;
    t.print();
}

When I try to run it I encounter som compilation issue :

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

Caused by:
  process didn't exit successfully: `F:\RustProjects\tt_torch_rl_demo\target\debug\build\torch-sys-d475e3cf6635366d\build-script-build` (exit code: 1)
--- stdout
cargo:rustc-link-search=native=C:\SDKs\libtorch\libtorch-1.5\lib
TARGET = Some("x86_64-pc-windows-msvc")
OPT_LEVEL = Some("0")
HOST = Some("x86_64-pc-windows-msvc")
CXX_x86_64-pc-windows-msvc = None
CXX_x86_64_pc_windows_msvc = None
HOST_CXX = None
CXX = None
CXXFLAGS_x86_64-pc-windows-msvc = None
CXXFLAGS_x86_64_pc_windows_msvc = None
HOST_CXXFLAGS = None
CXXFLAGS = None
CRATE_CC_NO_DEFAULTS = None
CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2")
DEBUG = Some("true")
running: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\bin\\HostX64\\x64\\cl.exe" "-nologo" "-MD" "-Z7" "-Brepro" "-I" "C:\\SDKs\\libtorch\\libtorch-1.5\\include" "-I" "C:\\SDKs\\libtorch\\libtorch-1.5\\include/torch/csrc/api/include" "-FoF:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\build\\torch-sys-51e7d731766f4f38\\out\\libtch/torch_api.o" "-c" "libtch/torch_api.cpp"
torch_api.cpp
C:\SDKs\libtorch\libtorch-1.5\include\torch\csrc\api\include\torch/data/worker_exception.h(18): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
libtch/torch_api.cpp(380): error C2248: 'torch::autograd::Engine::Engine': cannot access protected member declared in class 'torch::autograd::Engine'
C:\SDKs\libtorch\libtorch-1.5\include\torch/csrc/autograd/engine.h(213): note: see declaration of 'torch::autograd::Engine::Engine'
C:\SDKs\libtorch\libtorch-1.5\include\torch/csrc/autograd/engine.h(159): note: see declaration of 'torch::autograd::Engine'
libtch/torch_api.cpp(394): error C2039: 'beta1': is not a member of 'torch::optim::AdamOptions'
C:\SDKs\libtorch\libtorch-1.5\include\torch\csrc\api\include\torch/optim/adam.h(21): note: see declaration of 'torch::optim::AdamOptions'
libtch/torch_api.cpp(394): error C3536: 'options': cannot be used before it is initialized
libtch/torch_api.cpp(450): error C2039: 'options': is not a member of 'torch::optim::Adam'
C:\SDKs\libtorch\libtorch-1.5\include\torch\csrc\api\include\torch/optim/adam.h(49): note: see declaration of 'torch::optim::Adam'
libtch/torch_api.cpp(450): error C2039: 'options': is not a member of 'torch::optim::RMSprop'
C:\SDKs\libtorch\libtorch-1.5\include\torch\csrc\api\include\torch/optim/rmsprop.h(54): note: see declaration of 'torch::optim::RMSprop'
libtch/torch_api.cpp(450): error C2039: 'options': is not a member of 'torch::optim::SGD'
C:\SDKs\libtorch\libtorch-1.5\include\torch\csrc\api\include\torch/optim/sgd.h(48): note: see declaration of 'torch::optim::SGD'
libtch/torch_api.cpp(463): error C2039: 'options': is not a member of 'torch::optim::Adam'
C:\SDKs\libtorch\libtorch-1.5\include\torch\csrc\api\include\torch/optim/adam.h(49): note: see declaration of 'torch::optim::Adam'
libtch/torch_api.cpp(463): error C2039: 'options': is not a member of 'torch::optim::RMSprop'
C:\SDKs\libtorch\libtorch-1.5\include\torch\csrc\api\include\torch/optim/rmsprop.h(54): note: see declaration of 'torch::optim::RMSprop'
libtch/torch_api.cpp(463): error C2039: 'options': is not a member of 'torch::optim::SGD'
C:\SDKs\libtorch\libtorch-1.5\include\torch\csrc\api\include\torch/optim/sgd.h(48): note: see declaration of 'torch::optim::SGD'
libtch/torch_api.cpp(699): error C2039: 'isGenericList': is not a member of 'c10::IValue'
C:\SDKs\libtorch\libtorch-1.5\include\torch/csrc/jit/runtime/interpreter.h(13): note: see declaration of 'c10::IValue'
libtch/torch_api.cpp(751): error C2039: 'isGenericList': is not a member of 'c10::IValue'
C:\SDKs\libtorch\libtorch-1.5\include\torch/csrc/jit/runtime/interpreter.h(13): note: see declaration of 'c10::IValue'
libtch/torch_api.cpp(751): error C2039: 'toGenericList': is not a member of 'c10::IValue'
C:\SDKs\libtorch\libtorch-1.5\include\torch/csrc/jit/runtime/interpreter.h(13): note: see declaration of 'c10::IValue'
libtch/torch_api.cpp(785): error C2039: 'toGenericList': is not a member of 'c10::IValue'
C:\SDKs\libtorch\libtorch-1.5\include\torch/csrc/jit/runtime/interpreter.h(13): note: see declaration of 'c10::IValue'
libtch/torch_api.cpp(785): error C3536: 'vec': cannot be used before it is initialized
libtch/torch_api.cpp(785): error C2109: subscript requires array or pointer type
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(878): error C2039: '_test_optional_float': is not a member of 'torch'
C:\SDKs\libtorch\libtorch-1.5\include\torch/custom_class.h(18): note: see declaration of 'torch'
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(878): error C3861: '_test_optional_float': identifier not found
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(2364): error C2039: 'cudnn_convolution_backward_bias': is not a member of 'torch'
C:\SDKs\libtorch\libtorch-1.5\include\torch/custom_class.h(18): note: see declaration of 'torch'
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(2364): error C3861: 'cudnn_convolution_backward_bias': identifier not found
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(2392): error C2039: 'cudnn_convolution_transpose_backward_bias': is not a member of 'torch'
C:\SDKs\libtorch\libtorch-1.5\include\torch/custom_class.h(18): note: see declaration of 'torch'
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(2392): error C3861: 'cudnn_convolution_transpose_backward_bias': identifier not found
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(3703): error C2039: 'imag_out': is not a member of 'torch'
C:\SDKs\libtorch\libtorch-1.5\include\torch/custom_class.h(18): note: see declaration of 'torch'
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(3703): error C3861: 'imag_out': identifier not found
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(3985): error C2660: 'at::leaky_relu_backward': function does not take 3 arguments
C:\SDKs\libtorch\libtorch-1.5\include\ATen/Functions.h(14254): note: see declaration of 'at::leaky_relu_backward'
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(3988): error C3536: 'outputs__': cannot be used before it is initialized
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(3985): error C2664: 'at::Tensor::Tensor(at::Tensor &&)': cannot convert argument 1 from 'int' to 'c10::intrusive_ptr<c10::TensorImpl,c10::UndefinedTensorImpl>'
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(3988): note: No constructor could take the source type, or constructor overload resolution was ambiguous
C:\SDKs\libtorch\libtorch-1.5\include\ATen/core/TensorBody.h(85): note: see declaration of 'at::Tensor::Tensor'
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(3992): error C2039: 'leaky_relu_backward_out': is not a member of 'torch'
C:\SDKs\libtorch\libtorch-1.5\include\torch/custom_class.h(18): note: see declaration of 'torch'
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(3992): error C3861: 'leaky_relu_backward_out': identifier not found
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(5958): error C2039: 'real_out': is not a member of 'torch'
C:\SDKs\libtorch\libtorch-1.5\include\torch/custom_class.h(18): note: see declaration of 'torch'
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(5958): error C3861: 'real_out': identifier not found
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(6382): error C2660: 'at::rrelu_with_noise_backward': function does not take 6 arguments
C:\SDKs\libtorch\libtorch-1.5\include\ATen/Functions.h(14406): note: see declaration of 'at::rrelu_with_noise_backward'
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(6385): error C3536: 'outputs__': cannot be used before it is initialized
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(6382): error C2664: 'at::Tensor::Tensor(at::Tensor &&)': cannot convert argument 1 from 'int' to 'c10::intrusive_ptr<c10::TensorImpl,c10::UndefinedTensorImpl>'
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(6385): note: No constructor could take the source type, or constructor overload resolution was ambiguous
C:\SDKs\libtorch\libtorch-1.5\include\ATen/core/TensorBody.h(85): note: see declaration of 'at::Tensor::Tensor'
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(6389): error C2039: 'rrelu_with_noise_backward_out': is not a member of 'torch'
C:\SDKs\libtorch\libtorch-1.5\include\torch/custom_class.h(18): note: see declaration of 'torch'
C:\Users\vidal\.cargo\registry\src\github.com-1ecc6299db9ec823\torch-sys-0.1.6\libtch\torch_api_generated.cpp.h(6389): error C3861: 'rrelu_with_noise_backward_out': identifier not found
exit code: 2

--- stderr

error occurred: Command "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\bin\\HostX64\\x64\\cl.exe" "-nologo" "-MD" "-Z7" "-Brepro" "-I" "C:\\SDKs\\libtorch\\libtorch-1.5\\include" "-I" "C:\\SDKs\\libtorch\\libtorch-1.5\\include/torch/csrc/api/include" "-FoF:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\build\\torch-sys-51e7d731766f4f38\\out\\libtch/torch_api.o" "-c" "libtch/torch_api.cpp" with args "cl.exe" did not execute successfully (status code exit code: 2).

Do you have any idea of what I should do or what is missing on my system ?

I'm on Windows 10 using CLion with rust plugin

Thank you in advance.

Nicolas

LaurentMazare commented 4 years ago

The 0.1.6 version is only compatible with PyTorch 1.4, this probably explains your compilation errors. The current GitHub tip should work with PyTorch 1.5 so you can give this a try. We'll update the version on crates.io in the next few days.

NicolasVidal commented 4 years ago

Thank you for your quick answer ! :) I'll give the GitHub tip version a try this evening, and keep you posted. Thanks again for this work !

NicolasVidal commented 4 years ago

Ok, I used the master tip and it works, I just had to add /lib in my path Thank you again :)

NicolasVidal commented 4 years ago

I'm trying to use CUDA, used the correct libtorch from website, but I encounter this issue, any idea on how to pass that ?

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: TorchError { c_error: "Cannot initialize CUDA without ATen_cuda library. PyTorch splits its backend into two shared libraries: a CPU library and a CUDA library;
 this error has occurred because you are trying to use some CUDA functionality, but the CUDA library has not been loaded by the dynamic linker for some reason.  The CUDA library MUST be loaded, EVEN IF you don\'t directly use any sy
mbols from the CUDA library! One common culprit is a lack of -Wl,--no-as-needed in your link arguments; many dynamic linkers will delete dynamic library dependencies if you don\'t depend on any of their symbols.  You can check if th
is has occurred by using ldd on your binary to see if there is a dependency on *_cuda.so library. (initCUDA at C:\\w\\b\\windows\\pytorch\\aten\\src\\ATen/detail/CUDAHooksInterface.h:63)\n(no backtrace available)" }', C:\SDKs\rust_l
ibs\torch-rs_1.7\src\wrappers\tensor_generated.rs:8090:9
println!("{}",tch::Cuda::is_available());

prints False

Thanks in advance

LaurentMazare commented 4 years ago

Right, the cuda detection and linking has become a bit messier since the 1.5 release, see [1] for some context. The main culprit is that cargo does not let you pass arbitrary flags so it is difficult to pass the -Wl-no-as-needed required in the error message you reported. There is an ongoing PR [2] adding support for these arbitrary flags but it seems a bit staled. Anyway I've tweaked the current hack to work better on windows, could you give it a try? (sadly I don't have access to a windows/cuda box to test)

[1] https://github.com/pytorch/pytorch/issues/36437 [2] https://github.com/rust-lang/cargo/pull/7811

NicolasVidal commented 4 years ago

With your update, I now have a new error :

error: linking with `link.exe` failed: exit code: 1120
  |
  = note: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Tools\\MSVC\\14.25.28610\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.1dxwc203ifs3q4x1.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.1jilsd1gwz51m7va.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.1s238h5flsmpiqik.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.21t34az07qi13qvr.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.35nko41dgaltuymq.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.3cx9upi5fll1dqjt.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.3hp8l2ptihb6bjgd.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.3uaq6fjp24fgs9a0.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.425u0mv86j3t3lxk.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.47n3nsr4nkruacgr.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.4ic7dsd93umizs9f.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.4l84z255of6mky22.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.4o3dwpgmosh81nkq.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.4sgseqheikosx1yg.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.53hkhdubugxtfrra.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.53ubcfab18i76ooy.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.8rincjnbfmqqzx0.rcgu.o" "/OUT:F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.exe" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.5d6deqj959vna207.rcgu.o" "/OPT:REF,NOICF" "/DEBUG" "/NATVIS:C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\intrinsic.natvis" "/NATVIS:C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libcore.natvis" "/NATVIS:C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libstd.natvis" "/LIBPATH:F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps" "/LIBPATH:C:\\SDKs\\libtorch\\libtorch-1.5\\lib" "/LIBPATH:F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\build\\torch-sys-9a8e73ee46502b95\\out" "/LIBPATH:F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\build\\bzip2-sys-bd7e6452502e0e44\\out\\lib" "/LIBPATH:C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libtch-d9f638faf707f5e4.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libndarray-b10ddb666820c2c8.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libmatrixmultiply-510cbed5c202f088.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libnum_complex-a87bae0c02ca46a7.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libnum_integer-ac8bce78377ea147.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libnum_traits-fe9038bc112d8d23.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librawpointer-ddb5d60ccff09186.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand-b476d1837e35b5a3.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_xorshift-42f969f419d66576.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_pcg-bc326566e6f3bd3d.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_hc-ba4edcebb9226d44.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_chacha-2f8fed9ac0ebcc4b.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_isaac-6f05876192115013.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_core-64fbc33bdc7ff086.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_os-efedf937a1af1ef9.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_jitter-448fd85754352ee0.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_core-1a047724d008fd68.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libtorch_sys-4c34870afdb4e651.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libzip-49da5212f27861ab.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libtime-9f3beab9dfb9ca44.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libwinapi-a3345ab261663d33.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libbzip2-7e8d94f8fe6c14e9.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libbzip2_sys-1e61c18c0c42a248.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libflate2-eb08119a00ae8e23.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libminiz_oxide-d67b9505be1606b5.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libadler32-467fd20deaf85d4a.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libcrc32fast-2a4845b411ed3fdb.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libpodio-57fd92ebee6bdc09.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\liblibc-15f253067534ad4f.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libfailure-b9da1fdc252edfb6.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libbacktrace-c7115006a8f65e26.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libcfg_if-859a82a62dd8e4a6.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librustc_demangle-ecbbcf4e9f63de4d.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\liblazy_static-c388c12ec01bf26d.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd-5f7efcb90b37627e.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libpanic_unwind-eefd82c4fa1c0b7d.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libhashbrown-ae243f3791b2b8a9.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_alloc-bc06a7ddebbbdd93.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libbacktrace-a5159e13ad443b02.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_demangle-35d15d0dddf14281.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libunwind-374fc6ad1b7dd17d.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcfg_if-47c5ccef32cd17d2.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liblibc-365ba91e90516a6a.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liballoc-f8e2a4c542f4c17b.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_core-7ea334fd66b60d57.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcore-5c99a63f001ecfda.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcompiler_builtins-649f0d5692fd93eb.rlib" "torch_cuda.lib" "torch.lib" "torch_cpu.lib" "c10.lib" "advapi32.lib" "bcrypt.lib" "cfgmgr32.lib" "credui.lib" "crypt32.lib" "cryptnet.lib" "fwpuclnt.lib" "kernel32.lib" "ncrypt.lib" "ntdll.lib" "secur32.lib" "user32.lib" "ws2_32.lib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "msvcrt.lib"
  = note: Non-UTF-8 output:    Cr\x82ation de la biblioth\x8aque F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.lib et de l\'objet F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.exp\r\nlibtorch_sys-4c34870afdb4e651.rlib(dummy_cuda_dependency.o) : error LNK2019: symbole externe non r\x82solu \"void __cdecl at::cuda::getDeviceProperties(long)\" (?getDeviceProperties@cuda@at@@YAXJ@Z) r\x82f\x82renc\x82 dans la fonction dummy_cuda_dependency\r\nF:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.exe : fatal error LNK1120: 1 externes non r\x82solus\r\n
error: aborting due to previous error; 2 warnings emitted

Maybe I have a missing dll or something ?

I can successfully use tensorflow on my system as well as pyTorch python with cuda 10.2 and dll 7.5 though .

NicolasVidal commented 4 years ago

I'll be quite available this week end if you need me to test anything

LaurentMazare commented 4 years ago

That's weird, the getDeviceProperties symbol should be exported in torch_cuda.lib which is part of your linker flags. Could you try adding below this line, something like the following?

    println!("cargo:rustc-link-lib=c10_cuda");

It doesn't seem likely to help but worth trying when you get a chance.

NicolasVidal commented 4 years ago

I just tried this :

        if use_cuda {
            println!("cargo:rustc-link-lib=torch_cuda");
            println!("cargo:rustc-link-lib=c10_cuda");
        }

and got this result :

error: linking with `link.exe` failed: exit code: 1120
  |
  = note: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Tools\\MSVC\\14.25.28610\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.1dxwc203ifs3q4x1.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.1jilsd1gwz51m7va.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.1s238h5flsmpiqik.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.21t34az07qi13qvr.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.35nko41dgaltuymq.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.3cx9upi5fll1dqjt.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.3hp8l2ptihb6bjgd.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.3uaq6fjp24fgs9a0.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.425u0mv86j3t3lxk.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.47n3nsr4nkruacgr.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.4ic7dsd93umizs9f.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.4l84z255of6mky22.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.4o3dwpgmosh81nkq.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.4sgseqheikosx1yg.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.53hkhdubugxtfrra.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.53ubcfab18i76ooy.rcgu.o" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.8rincjnbfmqqzx0.rcgu.o" "/OUT:F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.exe" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.5d6deqj959vna207.rcgu.o" "/OPT:REF,NOICF" "/DEBUG" "/NATVIS:C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\intrinsic.natvis" "/NATVIS:C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libcore.natvis" "/NATVIS:C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libstd.natvis" "/LIBPATH:F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps" "/LIBPATH:C:\\SDKs\\libtorch\\libtorch-1.5\\lib" "/LIBPATH:F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\build\\torch-sys-9a8e73ee46502b95\\out" "/LIBPATH:F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\build\\bzip2-sys-bd7e6452502e0e44\\out\\lib" "/LIBPATH:C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libtch-d9f638faf707f5e4.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libndarray-b10ddb666820c2c8.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libmatrixmultiply-510cbed5c202f088.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libnum_complex-a87bae0c02ca46a7.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libnum_integer-ac8bce78377ea147.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libnum_traits-fe9038bc112d8d23.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librawpointer-ddb5d60ccff09186.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand-b476d1837e35b5a3.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_xorshift-42f969f419d66576.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_pcg-bc326566e6f3bd3d.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_hc-ba4edcebb9226d44.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_chacha-2f8fed9ac0ebcc4b.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_isaac-6f05876192115013.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_core-64fbc33bdc7ff086.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_os-efedf937a1af1ef9.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_jitter-448fd85754352ee0.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librand_core-1a047724d008fd68.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libtorch_sys-4c34870afdb4e651.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libzip-49da5212f27861ab.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libtime-9f3beab9dfb9ca44.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libwinapi-a3345ab261663d33.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libbzip2-7e8d94f8fe6c14e9.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libbzip2_sys-1e61c18c0c42a248.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libflate2-eb08119a00ae8e23.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libminiz_oxide-d67b9505be1606b5.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libadler32-467fd20deaf85d4a.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libcrc32fast-2a4845b411ed3fdb.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libpodio-57fd92ebee6bdc09.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\liblibc-15f253067534ad4f.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libfailure-b9da1fdc252edfb6.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libbacktrace-c7115006a8f65e26.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\libcfg_if-859a82a62dd8e4a6.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\librustc_demangle-ecbbcf4e9f63de4d.rlib" "F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\liblazy_static-c388c12ec01bf26d.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd-5f7efcb90b37627e.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libpanic_unwind-eefd82c4fa1c0b7d.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libhashbrown-ae243f3791b2b8a9.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_alloc-bc06a7ddebbbdd93.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libbacktrace-a5159e13ad443b02.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_demangle-35d15d0dddf14281.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libunwind-374fc6ad1b7dd17d.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcfg_if-47c5ccef32cd17d2.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liblibc-365ba91e90516a6a.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liballoc-f8e2a4c542f4c17b.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_core-7ea334fd66b60d57.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcore-5c99a63f001ecfda.rlib" "C:\\Users\\vidal\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcompiler_builtins-649f0d5692fd93eb.rlib" "torch_cuda.lib" "c10_cuda.lib" "torch.lib" "torch_cpu.lib" "c10.lib" "advapi32.lib" "bcrypt.lib" "cfgmgr32.lib" "credui.lib" "crypt32.lib" "cryptnet.lib" "fwpuclnt.lib" "kernel32.lib" "ncrypt.lib" "ntdll.lib" "secur32.lib" "user32.lib" "ws2_32.lib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "msvcrt.lib"
  = note: Non-UTF-8 output:    Cr\x82ation de la biblioth\x8aque F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.lib et de l\'objet F:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.exp\r\nlibtorch_sys-4c34870afdb4e651.rlib(dummy_cuda_dependency.o) : error LNK2019: symbole externe non r\x82solu \"void __cdecl at::cuda::getDeviceProperties(long)\" (?getDeviceProperties@cuda@at@@YAXJ@Z) r\x82f\x82renc\x82 dans la fonction dummy_cuda_dependency\r\nF:\\RustProjects\\tt_torch_rl_demo\\target\\debug\\deps\\tt_torch_rl_demo.exe : fatal error LNK1120: 1 externes non r\x82solus\r\n

error: aborting due to previous error; 2 warnings emitted

error: could not compile `tt_torch_rl_demo`.
LaurentMazare commented 4 years ago

Thanks, that's sadly the same error as previously, I can see the symbol being exported by torch_cuda.lib using winedump but the linker doesn't seem to pick this up - I'm afraid I don't know enough about the windows linker to be helpful here.

NicolasVidal commented 4 years ago

Ok, I'll try to work on the CPU for now, thanks for trying, if I find something I'll let you know. Feel free to ask me to do some tests on Windows if needed.

Thank you again and have a great day !

LaurentMazare commented 4 years ago

I was able to get access to a win+nvidia box and was getting the same error as you did when using cuda. I just pushed some changes that should hopefully fix the issue (at least they worked on this box), let me know if this still doesn't work for you.

NicolasVidal commented 4 years ago

Wow thank you a lot !

It indeed prints true now on :

println!("{}",tch::Cuda::is_available());

:)

I just have to manage to specify GPU for your MNIST example, I replaced kind::FLOAT_CPU to kind::FLOAT_CUDA but I think I need to specify Device::Cuda(0); somewhere else.

I have this error now :

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: TorchError { c_error: "Expected object of scalar type Long but got scalar type Float for argument #2 \'target\' in call to _thnn_nll_loss_forward (checked_dense_tensor_unwrap at C:\\w\\b\\windows\\pytorch\\aten\\src\\ATen/Utils.h:76)\n(no backtrace available)" }

On this last line :

m.train_images = m.train_images.to(tch::Device::Cuda(0));
    m.train_labels = m.train_images.to(tch::Device::Cuda(0));
    m.test_images = m.train_images.to(tch::Device::Cuda(0));
    m.test_labels = m.train_images.to(tch::Device::Cuda(0));
    println!("train-images: {:?}", m.train_images.size());
    println!("train-labels: {:?}", m.train_labels.size());
    println!("test-images: {:?}", m.test_images.size());
    println!("test-labels: {:?}", m.test_labels.size());
    let mut ws = Tensor::zeros(&[IMAGE_DIM, LABELS], kind::FLOAT_CUDA).set_requires_grad(true);
    let mut bs = Tensor::zeros(&[LABELS], kind::FLOAT_CUDA).set_requires_grad(true);
    for epoch in 1..200 {
        let logits = m.train_images.mm(&ws) + &bs;
        let loss = logits
            .log_softmax(-1, Kind::Float)
            .nll_loss(&m.train_labels);

I think I miss some device specifications somewhere.

NicolasVidal commented 4 years ago

Haha that's was a stupid mistake, I did put train_images into train_labels ...

So everything seems to work greatly !

Now I have no excuses to translate my whole python RL system to Rust. I'll keep you posted !

Thank you a lot ! :)

LaurentMazare commented 4 years ago

Glad that it ended up working, and let me know if there is anything else that could be helpful to you. Fwiw there are a bunch of deep-rl examples in this directory (a2c, ppo, policy gradients using OpenAI gym/atari games).

NicolasVidal commented 4 years ago

Yes, that's why in the first place I was interested in your repository :)

I just encountered another issue though, when I run with --release I encounter the same issue as before :

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: TorchError { c_error: "Cannot initialize CUDA without ATen_cuda library. PyTorch splits its backend into two shared libraries: a CPU library and a CUDA library;
 this error has occurred because you are trying to use some CUDA functionality, but the CUDA library has not been loaded by the dynamic linker for some reason.  The CUDA library MUST be loaded, EVEN IF you don\'t directly use any sy
mbols from the CUDA library! One common culprit is a lack of -Wl,--no-as-needed in your link arguments; many dynamic linkers will delete dynamic library dependencies if you don\'t depend on any of their symbols.  You can check if th
is has occurred by using ldd on your binary to see if there is a dependency on *_cuda.so library. (initCUDA at C:\\w\\b\\windows\\pytorch\\aten\\src\\ATen/detail/CUDAHooksInterface.h:63)\n(no backtrace available)" }', C:\SDKs\rust_l
ibs\torch-rs_1.7\src\wrappers\tensor_generated.rs:7395:9

Any idea ?

LaurentMazare commented 4 years ago

Indeed it seems that symbols get stripped more aggressively in release mode on windows than they do on linux. A possible workaround for now is to add the explicit dependency in your binary code, e.g. the following should do the trick.

use torch_sys::dummy_cuda_dependency();

fn main() {
  unsafe{ dummy_cuda_dependency(); }
  // your normal code there
}

If this ends up working well for you, we can try to make this nicer, at least removing the unsafe keyword should be straightforward.

NicolasVidal commented 4 years ago

Great! That indeed did the trick, do you want me to close the issue then or do you prefer making another test before ?

Thank you again.

LaurentMazare commented 4 years ago

Cool, we can probably keep this issue open until we have a less hacky way of triggering the dependency.

josrzn commented 4 years ago

That trick works indeed, thanks, but one needs to add the dependency to your torch-sys as well. Here is my basic code to test it, and print using Cuda(0) on my W10 laptop after a cargo run --release: Cargo.toml ->

...
[dependencies]
tch = "0.1.7"
torch-sys = "0.1.7"
...

main.rs ->

extern crate tch;
use tch::Device;
fn main() {
    unsafe{ torch_sys::dummy_cuda_dependency(); }
    let device = Device::cuda_if_available();
    println!("using {:?}", device);
}
kahlil29 commented 4 years ago

I think I just got hit by this but I'm on Ubuntu. println!("{}",tch::Cuda::is_available()); prints false for me. I didn't install libtorch manually. Just did a cargo run --release and I believe it picked up the pre-compiled version. Do I need to set some environment variables or something like that?

I get this error :

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: TorchError { c_error: "Cannot initialize CUDA without ATen_cuda library. PyTorch splits its backend into two shared libraries: a CPU library and a CUDA library; this error has occurred because you are trying to use some CUDA functionality, but the CUDA library has not been loaded by the dynamic linker for some reason.  The CUDA library MUST be loaded, EVEN IF you don\'t directly use any symbols from the CUDA library! One common culprit is a lack of -Wl,--no-as-needed in your link arguments; many dynamic linkers will delete dynamic library dependencies if you don\'t depend on any of their symbols.
LaurentMazare commented 4 years ago

Not sure to understand what your setup is: if you didn't install libtorch manually nor have set the TORCH_CUDA_VERSION environment variable, the build process should automatically download the cpu version of libtorch 1.5. I have never seen this CUDA error message with the cpu version but maybe there is something I'm missing here?

kahlil29 commented 4 years ago

Apologies, it seems like I've not been very specific and given you context. I believe the models I'm using are trained specifically for CUDA and will probably not run on a CPU version of libtorch. I'll try installing it manually and setting the flag. 🤞

LaurentMazare commented 3 years ago

Closing this for now as not much has been done recently. The PR[1] allowing cargo to pass arbitrary linker flags is making slow but steady progress so hopefully the hacky bits can be removed at some point.

[1] https://github.com/rust-lang/cargo/pull/8441