LaurentMazare / tch-rs

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

Device::cuda_if_available() is false #907

Open Jaroslove opened 1 week ago

Jaroslove commented 1 week ago

I use tch = "0.18.0" on windows 11 Downloaded libtorch-win-shared-with-deps-2.5.0+cu118

nvcc --version nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2022 NVIDIA Corporation Built on Wed_Sep_21_10:41:10_Pacific_Daylight_Time_2022 Cuda compilation tools, release 11.8, V11.8.89 Build cuda_11.8.r11.8/compiler.31833905_0

My env vars are: LIBTORCH=D:\softwares\Libtorch\libtorch-win-shared-with-deps-2.5.0+cu118\libtorch TORCH_CUDA_VERSION=11.8 TORCH_VERSION=cu118

Could anybody help me, what I need to do to have cuda?

Anivie commented 4 days ago

same on Ubuntu enviroment, is there any way to solve it?

vincestorm commented 4 days ago

exactly the same issue on Windows 11

LaurentMazare commented 4 days ago

Ensure that you have a build.rs script in your repo that goes along the lines of the ones in tch-rs here, this ensures that the proper flags are used when linking the binary. Also good if you can checkin the tch-rs repro and run cargo run --example basics -r this should print whether or not the cuda bits are found in this case.

Anivie commented 4 days ago

Ensure that you have a build.rs script in your repo that goes along the lines of the ones in tch-rs here, this ensures that the proper flags are used when linking the binary. Also good if you can checkin the tch-rs repro and run cargo run --example basics -r this should print whether or not the cuda bits are found in this case.

Thanks! I tried your suggestion, but unfortunately, the problem still persists. I checked the contents of target/debug/build/torch-sys-xxx/output and saw that it is already trying to link torch_cuda, but tch::Cuda::is_available is still false.

...
cargo:rustc-link-lib=stdc++
cargo:rustc-link-lib=static=tch
cargo:rustc-link-lib=torch_cuda
cargo:rustc-link-lib=torch_cpu
cargo:rustc-link-lib=torch
cargo:rustc-link-lib=c10
cargo:rustc-link-lib=gomp
Anivie commented 4 days ago

Well, I'm not sure if this can be the final solution, even I am still can't sure what the problem is, but it seems to have solved my problem for now.

The answer in this link worked for me.

use std::ffi::CString;
use libc::dlopen;

fn main() {
    let path = CString::new("/root/libtorch/lib/libtorch_cuda.so").unwrap();
    unsafe {
        dlopen(path.into_raw(), 1);
    }

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