Skip to content
Merged
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
905 changes: 550 additions & 355 deletions Cargo.lock

Large diffs are not rendered by default.

63 changes: 43 additions & 20 deletions next-plaid-onnx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,17 @@ pub fn is_cuda_available() -> bool {
}
// If CUDA_VISIBLE_DEVICES is not set, GPUs are visible by default

// Only now check if CUDA EP is compiled in (may trigger CUDA driver init)
CUDAExecutionProvider::default()
.is_available()
.unwrap_or(false)
// Try to check if CUDA EP is available, catching any panics from CUDA driver loading
// This can panic if CUDA libraries are present but corrupted/incomplete (stub libraries)
std::panic::catch_unwind(|| {
CUDAExecutionProvider::default()
.is_available()
.unwrap_or(false)
})
.unwrap_or_else(|_| {
eprintln!("[next-plaid-onnx] CUDA driver check failed (invalid/stub library?), using CPU");
false
})
}

/// Check if CUDA execution provider is available.
Expand All @@ -270,15 +277,22 @@ fn configure_auto_provider(builder: SessionBuilder) -> Result<SessionBuilder> {

#[cfg(feature = "cuda")]
if !force_cpu {
let device_id = get_cuda_device_id();
if let Ok(b) = builder
.clone()
.with_execution_providers([CUDAExecutionProvider::default()
.with_device_id(device_id)
.with_tf32(true)
.build()])
{
return Ok(b);
// Wrap CUDA initialization in catch_unwind to handle panics from stub libraries
let cuda_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let device_id = get_cuda_device_id();
builder
.clone()
.with_execution_providers([CUDAExecutionProvider::default()
.with_device_id(device_id)
.with_tf32(true)
.build()])
}));
match cuda_result {
Ok(Ok(b)) => return Ok(b),
Ok(Err(_)) => { /* CUDA failed normally, try next provider */ }
Err(_) => {
eprintln!("[next-plaid-onnx] CUDA init panicked (invalid/stub library?), falling back to CPU");
}
}
}

Expand Down Expand Up @@ -332,15 +346,24 @@ fn configure_cuda(builder: SessionBuilder) -> Result<SessionBuilder> {
return Ok(builder);
}

let device_id = get_cuda_device_id();
builder
.with_execution_providers([
CUDAExecutionProvider::default()
// Wrap CUDA initialization in catch_unwind to handle panics from stub/invalid libraries
let cuda_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let device_id = get_cuda_device_id();
builder
.clone()
.with_execution_providers([CUDAExecutionProvider::default()
.with_device_id(device_id)
.with_tf32(true)
.build()
])
.context("Failed to configure CUDA execution provider. Ensure CUDA toolkit and cuDNN are installed.")
.build()])
}));

match cuda_result {
Ok(result) => result.context("Failed to configure CUDA execution provider. Ensure CUDA toolkit and cuDNN are installed."),
Err(_) => {
eprintln!("[next-plaid-onnx] CUDA init panicked (invalid/stub library?), falling back to CPU");
Ok(builder)
}
}
}

#[cfg(not(feature = "cuda"))]
Expand Down
7 changes: 4 additions & 3 deletions next-plaid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ thiserror = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
ndarray-npy = "0.9.1"
fastkmeans-rs = "1.0.1"
fastkmeans-rs = "1.0.2"
memmap2 = "0.9"
fs2 = "0.4"
half = "2.4"
byteorder = "1.5"
rusqlite = { version = "0.38", features = ["bundled", "functions"] }
regex = "1.11"
cudarc = { version = "0.12", features = [
cudarc = { version = "0.19.3", default-features = false, features = [
"cuda-version-from-build-system",
"cublas",
"dynamic-linking",
"dynamic-loading",
"nvrtc",
], optional = true }

[dev-dependencies]
Expand Down
34 changes: 34 additions & 0 deletions next-plaid/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
fn main() {
// Only run this when the cuda feature is enabled
#[cfg(feature = "cuda")]
{
// Common CUDA library search paths
let cuda_paths = [
"/usr/local/cuda/lib64/stubs",
"/usr/local/cuda/targets/x86_64-linux/lib/stubs",
"/usr/local/cuda-12/lib64/stubs",
"/usr/local/cuda-12.9/lib64/stubs",
"/usr/local/cuda-12.9/targets/x86_64-linux/lib/stubs",
"/usr/lib/x86_64-linux-gnu",
"/usr/lib64",
];

for path in cuda_paths {
if std::path::Path::new(path).exists() {
println!("cargo:rustc-link-search=native={}", path);
}
}

// Also check CUDA_PATH environment variable
if let Ok(cuda_path) = std::env::var("CUDA_PATH") {
let stubs_path = format!("{}/lib64/stubs", cuda_path);
if std::path::Path::new(&stubs_path).exists() {
println!("cargo:rustc-link-search=native={}", stubs_path);
}
let targets_stubs = format!("{}/targets/x86_64-linux/lib/stubs", cuda_path);
if std::path::Path::new(&targets_stubs).exists() {
println!("cargo:rustc-link-search=native={}", targets_stubs);
}
}
}
}
2 changes: 1 addition & 1 deletion next-plaid/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl ResidualCodec {
if let Some(ctx) = crate::cuda::get_global_context() {
let centroids = self.centroids_view();
match crate::cuda::compress_into_codes_cuda_batched(
ctx,
&ctx,
&embeddings.view(),
&centroids,
None,
Expand Down
Loading
Loading