diff options
author | Laurent Mazare <laurent.mazare@gmail.com> | 2023-07-15 08:25:15 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-15 08:25:15 +0100 |
commit | 66750f98271dc852464d55689ca2d29f04f3fa34 (patch) | |
tree | 00fb0c302205dcddd97e58d674c2198f837d3f8b /candle-core/src | |
parent | 2ddda706bde9936cbc8f90142ed4acc43390904e (diff) | |
download | candle-66750f98271dc852464d55689ca2d29f04f3fa34.tar.gz candle-66750f98271dc852464d55689ca2d29f04f3fa34.tar.bz2 candle-66750f98271dc852464d55689ca2d29f04f3fa34.zip |
Add some 'cuda-if-available' helper function. (#172)
Diffstat (limited to 'candle-core/src')
-rw-r--r-- | candle-core/src/device.rs | 8 | ||||
-rw-r--r-- | candle-core/src/utils.rs | 7 |
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; +} |