use std::sync::OnceLock;
use crate::backend::Backend;
pub fn cuda_available() -> bool {
static CACHE: OnceLock<bool> = OnceLock::new();
*CACHE.get_or_init(|| {
probe_lib(&["nvcuda.dll", "libcuda.so.1", "libcuda.so", "libcuda.dylib"])
})
}
pub fn rocm_available() -> bool {
static CACHE: OnceLock<bool> = OnceLock::new();
*CACHE.get_or_init(|| {
probe_lib(&[
"amdhip64.dll",
"libamdhip64.so",
"libamdhip64.so.6",
"libamdhip64.so.5",
])
})
}
pub fn metal_available() -> bool {
static CACHE: OnceLock<bool> = OnceLock::new();
*CACHE.get_or_init(|| {
#[cfg(target_os = "macos")]
{
std::path::Path::new("/System/Library/Frameworks/Metal.framework").exists()
}
#[cfg(not(target_os = "macos"))]
{
false
}
})
}
pub fn tpu_available() -> bool {
static CACHE: OnceLock<bool> = OnceLock::new();
*CACHE.get_or_init(|| {
if std::env::var("TPU_NAME").is_ok() {
return true;
}
#[cfg(target_os = "linux")]
{
let paths = [
"/dev/accel0",
"/dev/apex_0",
"/dev/vfio/vfio",
];
for p in paths {
if std::path::Path::new(p).exists() {
return true;
}
}
}
false
})
}
pub fn ane_available() -> bool {
static CACHE: OnceLock<bool> = OnceLock::new();
*CACHE.get_or_init(|| {
cfg!(all(target_os = "macos", target_arch = "aarch64"))
})
}
pub fn wasm_available() -> bool {
cfg!(feature = "wasm-reference")
}
pub fn shared_memory_worker_available() -> bool {
cfg!(feature = "shared-memory-worker-reference")
}
#[cfg(any(feature = "cuda-reference", feature = "gpu-peer"))]
pub fn cuda_sm_count(device_ordinal: usize) -> Option<u32> {
use cudarc::driver::sys as cu;
if !cuda_available() {
return None;
}
if !unsafe { cu::is_culib_present() } {
return None;
}
let r = unsafe { cu::cuInit(0) };
if r != cu::CUresult::CUDA_SUCCESS {
return None;
}
let ordinal = i32::try_from(device_ordinal).ok()?;
let mut dev: cu::CUdevice = 0;
let r = unsafe { cu::cuDeviceGet(&mut dev, ordinal) };
if r != cu::CUresult::CUDA_SUCCESS {
return None;
}
let attr = |a: cu::CUdevice_attribute| -> Option<u32> {
let mut v: i32 = 0;
let r = unsafe { cu::cuDeviceGetAttribute(&mut v, a, dev) };
if r == cu::CUresult::CUDA_SUCCESS && v > 0 { Some(v as u32) } else { None }
};
attr(cu::CUdevice_attribute::CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT)
}
#[cfg(not(any(feature = "cuda-reference", feature = "gpu-peer")))]
pub fn cuda_sm_count(_device_ordinal: usize) -> Option<u32> {
None
}
pub fn detect_all() -> Vec<Backend> {
let mut out = vec![Backend::Cpu];
if cuda_available() {
out.push(Backend::Cuda { device_id: 0 });
}
if rocm_available() {
out.push(Backend::Rocm { device_id: 0 });
}
if metal_available() {
out.push(Backend::Metal { device_id: 0 });
}
if tpu_available() {
out.push(Backend::Tpu { device_id: 0 });
}
if ane_available() {
out.push(Backend::Ane);
}
if wasm_available() {
out.push(Backend::Wasm { device_id: 0 });
}
if shared_memory_worker_available() {
out.push(Backend::SharedMemoryWorker { backend_id: 0 });
}
out
}
fn probe_lib(candidates: &[&str]) -> bool {
for name in candidates {
if let Ok(lib) = unsafe { libloading::Library::new(*name) } {
drop(lib);
return true;
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn detect_all_always_includes_cpu() {
let detected = detect_all();
assert!(detected.contains(&Backend::Cpu));
}
#[test]
fn cuda_probe_returns_a_bool() {
let a = cuda_available();
let b = cuda_available();
assert_eq!(a, b, "cached probe must be stable");
}
#[test]
fn rocm_probe_returns_a_bool() {
let a = rocm_available();
let b = rocm_available();
assert_eq!(a, b);
}
#[test]
fn metal_probe_is_false_off_macos() {
#[cfg(not(target_os = "macos"))]
assert!(!metal_available());
}
#[test]
fn ane_probe_matches_target_classifier() {
let expected = cfg!(all(target_os = "macos", target_arch = "aarch64"));
assert_eq!(ane_available(), expected);
}
#[test]
fn tpu_probe_returns_a_bool() {
let a = tpu_available();
let b = tpu_available();
assert_eq!(a, b);
}
#[cfg(any(feature = "cuda-reference", feature = "gpu-peer"))]
#[test]
fn sm_count_answers_whenever_device_zero_opens() {
let device_opens = cuda_available() && cudarc::driver::CudaContext::new(0).is_ok();
match cuda_sm_count(0) {
Some(sm) => {
assert!(device_opens, "SM count {sm} reported where no device 0 opens");
assert!(
(1..=1024).contains(&sm),
"SM count {sm} outside the plausible range for a CUDA device"
);
}
None => assert!(!device_opens, "device 0 opens but cuda_sm_count(0) answered None"),
}
}
#[cfg(not(any(feature = "cuda-reference", feature = "gpu-peer")))]
#[test]
fn sm_count_is_none_without_the_cuda_features() {
assert_eq!(cuda_sm_count(0), None);
}
#[test]
fn sm_count_is_none_for_an_ordinal_no_host_has() {
assert_eq!(cuda_sm_count(4096), None);
}
#[test]
fn sm_count_is_stable_across_calls() {
assert_eq!(cuda_sm_count(0), cuda_sm_count(0));
}
#[test]
fn probe_lib_returns_false_for_nonexistent_names() {
assert!(!probe_lib(&[
"this_library_does_not_exist_anywhere_42.so",
"nor_does_this_one_pleasestop.dll",
]));
}
}