Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the dummy dependency hack and use extra-ling-args instead. #330

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ fn grad_example() {
}

fn main() {
tch::maybe_init_cuda();
let t = Tensor::of_slice(&[3, 1, 4, 1, 5]);
t.print();
let t = Tensor::randn(&[5, 4], kind::FLOAT_CPU);
Expand Down
6 changes: 0 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,3 @@ pub use tensor::{

pub mod nn;
pub mod vision;

pub fn maybe_init_cuda() {
unsafe {
torch_sys::dummy_cuda_dependency();
}
}
4 changes: 0 additions & 4 deletions src/tensor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{Device, Kind, Scalar, TchError};
use half::f16;
use std::convert::{TryFrom, TryInto};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use torch_sys::*;

pub mod index;
mod iter;
Expand Down Expand Up @@ -750,6 +749,3 @@ try_from_impl!(i32);
try_from_impl!(f64);
try_from_impl!(i64);
try_from_impl!(bool);

#[used]
static INIT_ARRAY: [unsafe extern "C" fn(); 1] = [dummy_cuda_dependency];
33 changes: 8 additions & 25 deletions torch-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,8 @@ fn prepare_libtorch_dir() -> PathBuf {
}
}

fn make<P: AsRef<Path>>(libtorch: P, use_cuda: bool, use_hip: bool) {
fn make<P: AsRef<Path>>(libtorch: P) {
let os = env::var("CARGO_CFG_TARGET_OS").expect("Unable to get TARGET_OS");

let cuda_dependency = if use_cuda || use_hip {
"libtch/dummy_cuda_dependency.cpp"
} else {
"libtch/fake_cuda_dependency.cpp"
};
println!("cargo:rerun-if-changed=libtch/torch_api.cpp");
println!("cargo:rerun-if-changed=libtch/torch_api.h");
println!("cargo:rerun-if-changed=libtch/torch_api_generated.cpp.h");
Expand All @@ -165,7 +159,6 @@ fn make<P: AsRef<Path>>(libtorch: P, use_cuda: bool, use_hip: bool) {
.flag("-std=c++14")
.flag(&format!("-D_GLIBCXX_USE_CXX11_ABI={}", libtorch_cxx11_abi))
.file("libtch/torch_api.cpp")
.file(cuda_dependency)
.compile("tch");
}
"windows" => {
Expand All @@ -179,7 +172,6 @@ fn make<P: AsRef<Path>>(libtorch: P, use_cuda: bool, use_hip: bool) {
.include(libtorch.as_ref().join("include"))
.include(libtorch.as_ref().join("include/torch/csrc/api/include"))
.file("libtch/torch_api.cpp")
.file(cuda_dependency)
.compile("tch");
}
_ => panic!("Unsupported OS"),
Expand All @@ -189,20 +181,6 @@ fn make<P: AsRef<Path>>(libtorch: P, use_cuda: bool, use_hip: bool) {
fn main() {
if !cfg!(feature = "doc-only") {
let libtorch = prepare_libtorch_dir();
// use_cuda is a hacky way to detect whether cuda is available and
// if it's the case link to it by explicitly depending on a symbol
// from the torch_cuda library.
// It would be better to use -Wl,--no-as-needed but there is no way
// to specify arbitrary linker flags at the moment.
//
// Once https://github.com/rust-lang/cargo/pull/8441 is available
// we should switch to using rustc-link-arg instead e.g. with the
// following flags:
// -Wl,--no-as-needed -Wl,--copy-dt-needed-entries -ltorch
//
// This will be available starting from cargo 1.50 but will be a nightly
// only option to start with.
// https://github.com/rust-lang/cargo/blob/master/CHANGELOG.md
let use_cuda = libtorch.join("lib").join("libtorch_cuda.so").exists()
|| libtorch.join("lib").join("torch_cuda.dll").exists();
let use_cuda_cu = libtorch.join("lib").join("libtorch_cuda_cu.so").exists()
Expand All @@ -216,8 +194,11 @@ fn main() {
libtorch.join("lib").display()
);

make(&libtorch, use_cuda, use_hip);
make(&libtorch);

// PyTorch uses two separate library for the cpu bits and the cuda bits.
// The following flags ensure that the linker does not remove the
// cuda dependencies.
println!("cargo:rustc-link-lib=static=tch");
if use_cuda {
println!("cargo:rustc-link-lib=torch_cuda");
Expand All @@ -231,7 +212,9 @@ fn main() {
if use_hip {
println!("cargo:rustc-link-lib=torch_hip");
}
println!("cargo:rustc-link-lib=torch");
println!("cargo:rustc-link-arg=-Wl,--no-as-needed");
println!("cargo:rustc-link-arg=-Wl,--copy-dt-needed-entries");
println!("cargo:rustc-link-arg=-ltorch");
println!("cargo:rustc-link-lib=torch_cpu");
println!("cargo:rustc-link-lib=c10");
if use_hip {
Expand Down
18 changes: 0 additions & 18 deletions torch-sys/libtch/dummy_cuda_dependency.cpp

This file was deleted.

6 changes: 0 additions & 6 deletions torch-sys/libtch/fake_cuda_dependency.cpp

This file was deleted.

4 changes: 0 additions & 4 deletions torch-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,3 @@ extern "C" {
noutputs: c_int,
);
}

extern "C" {
pub fn dummy_cuda_dependency();
}