Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

feat(hardware): expose the NVIDIA driver release and the CUDA version it supports

thinhlpg86b351e472fe
diff --git a/src-tauri/plugins/tauri-plugin-hardware/src/vendor/nvidia.rs b/src-tauri/plugins/tauri-plugin-hardware/src/vendor/nvidia.rs
index 6739fe651..e41d85343 100644
--- a/src-tauri/plugins/tauri-plugin-hardware/src/vendor/nvidia.rs
+++ b/src-tauri/plugins/tauri-plugin-hardware/src/vendor/nvidia.rs
@@ -18,6 +18,77 @@ pub struct NvidiaInfo {
pub compute_capability: String,
}
+/// A CUDA version. NVML reports the one a driver supports as
+/// `1000 * major + 10 * minor`, the same number `cuDriverGetVersion` returns.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
+pub struct CudaVersion {
+ pub major: u32,
+ pub minor: u32,
+}
+
+impl CudaVersion {
+ pub fn from_driver_int(v: i32) -> Option<Self> {
+ let v = u32::try_from(v).ok().filter(|v| *v > 0)?;
+ Some(Self {
+ major: v / 1000,
+ minor: (v % 1000) / 10,
+ })
+ }
+
+ /// Parses `13.2`, the form `nvcc --version` and the engine report.
+ pub fn parse(text: &str) -> Option<Self> {
+ let (major, minor) = text.trim().split_once('.')?;
+ Some(Self {
+ major: major.parse().ok()?,
+ minor: minor.parse().ok()?,
+ })
+ }
+}
+
+impl std::fmt::Display for CudaVersion {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{}.{}", self.major, self.minor)
+ }
+}
+
+/// What the installed NVIDIA driver says about itself.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct NvidiaDriver {
+ /// The driver release, e.g. `595.97`.
+ pub version: String,
+ /// The newest CUDA the driver can run, when NVML reports it.
+ pub cuda: Option<CudaVersion>,
+}
+
+impl NvidiaDriver {
+ /// The release branch NVIDIA names drivers by (`595.97` is R595).
+ pub fn branch(&self) -> Option<u32> {
+ self.version.split('.').next()?.parse().ok()
+ }
+}
+
+/// The installed NVIDIA driver, or None when there is no NVIDIA GPU, NVML does
+/// not load, or the platform has no NVIDIA driver.
+pub fn nvidia_driver() -> Option<NvidiaDriver> {
+ #[cfg(any(target_os = "android", target_os = "ios", target_os = "macos"))]
+ {
+ None
+ }
+
+ #[cfg(not(any(target_os = "android", target_os = "ios", target_os = "macos")))]
+ {
+ with_nvml(|nvml| {
+ let nvml = nvml?;
+ let version = nvml.sys_driver_version().ok()?;
+ let cuda = nvml
+ .sys_cuda_driver_version()
+ .ok()
+ .and_then(CudaVersion::from_driver_int);
+ Some(NvidiaDriver { version, cuda })
+ })
+ }
+}
+
/// Run a closure with the current NVML handle, initializing if needed.
#[cfg(not(any(target_os = "android", target_os = "ios", target_os = "macos")))]
fn with_nvml<F, R>(f: F) -> R
diff --git a/src-tauri/plugins/tauri-plugin-hardware/src/vendor/tests.rs b/src-tauri/plugins/tauri-plugin-hardware/src/vendor/tests.rs
index 4f1681462..2f431576a 100644
--- a/src-tauri/plugins/tauri-plugin-hardware/src/vendor/tests.rs
+++ b/src-tauri/plugins/tauri-plugin-hardware/src/vendor/tests.rs
@@ -131,3 +131,50 @@ fn test_get_vulkan_gpus_on_ios() {
}
}
}
+
+mod nvidia_driver {
+ use crate::vendor::nvidia::{CudaVersion, NvidiaDriver};
+
+ // NVML reports the CUDA a driver supports as 1000 * major + 10 * minor.
+ #[test]
+ fn the_driver_reported_cuda_int_splits_into_major_and_minor() {
+ assert_eq!(
+ CudaVersion::from_driver_int(13020),
+ Some(CudaVersion { major: 13, minor: 2 })
+ );
+ assert_eq!(
+ CudaVersion::from_driver_int(12080),
+ Some(CudaVersion { major: 12, minor: 8 })
+ );
+ assert_eq!(CudaVersion::from_driver_int(0), None);
+ assert_eq!(CudaVersion::from_driver_int(-1), None);
+ }
+
+ #[test]
+ fn a_cuda_version_parses_and_prints_as_major_dot_minor() {
+ let v = CudaVersion::parse(" 13.2 ").expect("parses");
+ assert_eq!(v.to_string(), "13.2");
+ assert_eq!(CudaVersion::parse("13"), None);
+ assert_eq!(CudaVersion::parse("x.y"), None);
+ }
+
+ // Minor 10 is newer than minor 2; a string compare would get it backwards.
+ #[test]
+ fn versions_order_numerically() {
+ let older = CudaVersion { major: 12, minor: 9 };
+ let newer = CudaVersion { major: 13, minor: 0 };
+ assert!(older < newer);
+ assert!(CudaVersion { major: 13, minor: 2 } < CudaVersion { major: 13, minor: 10 });
+ }
+
+ #[test]
+ fn the_release_branch_is_the_number_before_the_dot() {
+ let driver = |version: &str| NvidiaDriver {
+ version: version.into(),
+ cuda: None,
+ };
+ assert_eq!(driver("595.97").branch(), Some(595));
+ assert_eq!(driver("617.42").branch(), Some(617));
+ assert_eq!(driver("not-a-version").branch(), None);
+ }
+}