Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(llamacpp): keep the driver advice to CUDA faults and off the engine start path
thinhlpge78b9f40f996
diff --git a/src-tauri/plugins/tauri-plugin-hardware/src/vendor/nvidia.rs b/src-tauri/plugins/tauri-plugin-hardware/src/vendor/nvidia.rsindex e41d85343..0977feff5 100644--- a/src-tauri/plugins/tauri-plugin-hardware/src/vendor/nvidia.rs+++ b/src-tauri/plugins/tauri-plugin-hardware/src/vendor/nvidia.rs@@ -60,13 +60,6 @@ pub struct NvidiaDriver {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> {diff --git a/src-tauri/plugins/tauri-plugin-hardware/src/vendor/tests.rs b/src-tauri/plugins/tauri-plugin-hardware/src/vendor/tests.rsindex 2f431576a..9e545970d 100644--- a/src-tauri/plugins/tauri-plugin-hardware/src/vendor/tests.rs+++ b/src-tauri/plugins/tauri-plugin-hardware/src/vendor/tests.rs@@ -133,7 +133,7 @@ fn test_get_vulkan_gpus_on_ios() {}mod nvidia_driver {- use crate::vendor::nvidia::{CudaVersion, NvidiaDriver};+ use crate::vendor::nvidia::CudaVersion;// NVML reports the CUDA a driver supports as 1000 * major + 10 * minor.#[test]@@ -166,15 +166,4 @@ mod nvidia_driver {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);- }}diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/commands.rs b/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/commands.rsindex 169e9d076..22556d4a0 100644--- a/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/commands.rs+++ b/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/commands.rs@@ -141,7 +141,7 @@ fn fault_emitter<R: tauri::Runtime>(Arc::new(move |fault: worker::RuntimeFault, line: String| {log::error!("jan-llama-worker fault ({fault:?}): {line}");let payload =- driver::fault_payload(&line, nvidia_driver().as_ref(), toolkit.get().copied());+ driver::fault_payload(&line, nvidia_driver, toolkit.get().copied());if let Err(e) = app_handle.emit(fault.event_name(), payload) {log::warn!("emit {} failed: {e}", fault.event_name());}@@ -203,7 +203,8 @@ pub async fn start_engine<R: tauri::Runtime>(if let Some(v) = handle.cuda_toolkit {let _ = toolkit.set(v);}- log_nvidia_driver(handle.cuda_toolkit).await;+ // Detached: NVML can be slow to load, and start_engine holds the engine lock.+ tokio::spawn(log_nvidia_driver(handle.cuda_toolkit));let info = EngineInfo::from(&handle);// Subscribed here rather than lazily: an eviction can happen on the verydiff --git a/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/driver.rs b/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/driver.rsindex fcb7af8da..8615ba644 100644--- a/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/driver.rs+++ b/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/driver.rs@@ -52,23 +52,30 @@ pub fn actionable_message(driver: &NvidiaDriver, toolkit: Option<CudaVersion>) -)}-/// The text the backend-error banner shows for a worker fault line. A line that-/// says the driver cannot run the engine's code gets the update advice ahead of-/// ggml's own words; anything else, or no NVIDIA driver to name, is passed through.+/// The text the backend-error banner shows for a worker fault line. A CUDA line+/// that says the driver cannot run the engine's code gets the update advice+/// ahead of ggml's own words. Anything else is passed through: that includes a+/// Vulkan driver error, which is not fixed by updating the NVIDIA driver even+/// on a machine that has one.+///+/// `driver` is only called for a line that qualifies, so an ordinary fault+/// does not pay for an NVML query.pub fn fault_payload(line: &str,- driver: Option<&NvidiaDriver>,+ driver: impl FnOnce() -> Option<NvidiaDriver>,toolkit: Option<CudaVersion>,) -> String {- match driver {- Some(driver) if crate::error::is_driver_too_old(&line.to_lowercase()) => {- format!(+ let lower = line.to_lowercase();+ let cuda = lower.contains("cuda") || lower.contains("ptx");+ if cuda && crate::error::is_driver_too_old(&lower) {+ if let Some(driver) = driver() {+ return format!("{} Engine output: {line}",- actionable_message(driver, toolkit)- )+ actionable_message(&driver, toolkit)+ );}- _ => line.to_string(),}+ line.to_string()}#[cfg(test)]@@ -166,7 +173,11 @@ mod tests {#[test]fn a_ptx_toolchain_fault_gets_the_update_advice_ahead_of_ggmls_words() {- let payload = fault_payload(PTX_LINE, Some(&driver("580.88", Some((13, 0)))), CUDA_13_2);+ let payload = fault_payload(+ PTX_LINE,+ || Some(driver("580.88", Some((13, 0)))),+ CUDA_13_2,+ );assert!(payload.starts_with("Update your NVIDIA driver to R595 or newer."),"{payload}"@@ -181,7 +192,26 @@ mod tests {fn other_faults_and_a_missing_driver_pass_through_untouched() {let nvidia = driver("580.88", Some((13, 0)));let oom = "CUDA error: out of memory";- assert_eq!(fault_payload(oom, Some(&nvidia), CUDA_13_2), oom);- assert_eq!(fault_payload(PTX_LINE, None, CUDA_13_2), PTX_LINE);+ assert_eq!(+ fault_payload(+ oom,+ || panic!("an OOM line must not query the driver"),+ CUDA_13_2+ ),+ oom+ );+ assert_eq!(fault_payload(PTX_LINE, || None, CUDA_13_2), PTX_LINE);+ }++ // A box with an NVIDIA card can still hit a Vulkan driver error on another+ // device; telling that user to update the NVIDIA driver would be wrong.+ #[test]+ fn a_vulkan_driver_error_is_not_blamed_on_the_nvidia_driver() {+ let nvidia = driver("580.88", Some((13, 0)));+ let vulkan = "ggml_vulkan: vkCreateInstance failed: ERROR_INCOMPATIBLE_DRIVER";+ assert_eq!(+ fault_payload(vulkan, || Some(nvidia.clone()), CUDA_13_2),+ vulkan+ );}}