summaryrefslogtreecommitdiff
path: root/candle-core/src
diff options
context:
space:
mode:
Diffstat (limited to 'candle-core/src')
-rw-r--r--candle-core/src/device.rs8
-rw-r--r--candle-core/src/utils.rs7
2 files changed, 15 insertions, 0 deletions
diff --git a/candle-core/src/device.rs b/candle-core/src/device.rs
index ca408529..53e2de43 100644
--- a/candle-core/src/device.rs
+++ b/candle-core/src/device.rs
@@ -109,6 +109,14 @@ impl Device {
}
}
+ pub fn cuda_if_available(ordinal: usize) -> Result<Self> {
+ if crate::utils::cuda_is_available() {
+ Self::new_cuda(ordinal)
+ } else {
+ Ok(Self::Cpu)
+ }
+ }
+
pub(crate) fn rand_uniform(
&self,
shape: &Shape,
diff --git a/candle-core/src/utils.rs b/candle-core/src/utils.rs
index b5621e56..895c97e1 100644
--- a/candle-core/src/utils.rs
+++ b/candle-core/src/utils.rs
@@ -17,3 +17,10 @@ pub fn has_mkl() -> bool {
#[cfg(not(feature = "mkl"))]
return false;
}
+
+pub fn cuda_is_available() -> bool {
+ #[cfg(feature = "cuda")]
+ return true;
+ #[cfg(not(feature = "cuda"))]
+ return false;
+}