Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(llamacpp): only advise a driver update when the versions allow it, and read the driver off the stderr task
thinhlpgd77afd4d7497
diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/commands.rs b/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/commands.rsindex 22556d4a0..1d38b83fb 100644--- a/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/commands.rs+++ b/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/commands.rs@@ -7,7 +7,7 @@ use std::sync::Arc;use rand::Rng;use serde::{Deserialize, Serialize};use tauri::{Manager, State};-use tauri_plugin_hardware::vendor::nvidia::{nvidia_driver, CudaVersion};+use tauri_plugin_hardware::vendor::nvidia::{nvidia_driver, CudaVersion, NvidiaDriver};use super::driver;use super::worker::{self, WorkerHandle};@@ -135,27 +135,40 @@ fn worker_missing_message(tried: &[PathBuf]) -> String {/// update the driver instead of leaving the user with a bare ggml abort.fn fault_emitter<R: tauri::Runtime>(app_handle: tauri::AppHandle<R>,- toolkit: Arc<std::sync::OnceLock<CudaVersion>>,+ versions: Arc<EngineVersions>,) -> worker::FaultCallback {use tauri::Emitter;Arc::new(move |fault: worker::RuntimeFault, line: String| {log::error!("jan-llama-worker fault ({fault:?}): {line}");- let payload =- driver::fault_payload(&line, nvidia_driver, toolkit.get().copied());+ let payload = driver::fault_payload(+ &line,+ versions.driver.get(),+ versions.toolkit.get().copied(),+ );if let Err(e) = app_handle.emit(fault.event_name(), payload) {log::warn!("emit {} failed: {e}", fault.event_name());}})}-/// Writes the NVIDIA driver and CUDA versions to `app.log` next to the engine's-/// own toolkit, and warns when the driver is older than the engine. Without-/// this a CUDA failure on an old driver cannot be told from any other (#9185).-async fn log_nvidia_driver(toolkit: Option<CudaVersion>) {+/// What the fault callback needs to word a driver error. Filled once, off the+/// callback's path: it runs on the task draining the worker's stderr, where an+/// NVML load must not block. A fault before the fill is worded without them.+#[derive(Default)]+struct EngineVersions {+ toolkit: std::sync::OnceLock<CudaVersion>,+ driver: std::sync::OnceLock<NvidiaDriver>,+}++/// Reads the NVIDIA driver, writes it and the engine's CUDA toolkit to+/// `app.log`, and warns when the driver is older than the engine. Without this a+/// CUDA failure on an old driver cannot be told from any other (#9185).+async fn record_nvidia_driver(versions: Arc<EngineVersions>) {// NVML loads a shared library and may take a moment on first use.let Ok(Some(driver)) = tokio::task::spawn_blocking(nvidia_driver).await else {return;};+ let toolkit = versions.toolkit.get().copied();log::info!("{}", driver::startup_line(&driver, toolkit));if driver::driver_older_than_engine(&driver, toolkit) {log::warn!(@@ -163,6 +176,7 @@ async fn log_nvidia_driver(toolkit: Option<CudaVersion>) {driver::actionable_message(&driver, toolkit));}+ let _ = versions.driver.set(driver);}/// Starts the worker. `slot_cache_mib` is the ceiling on the per-thread KV@@ -187,7 +201,7 @@ pub async fn start_engine<R: tauri::Runtime>(let api_key = generate_worker_key();// Filled from the handshake, after the worker is up; a fault before then// is worded without it.- let toolkit = Arc::new(std::sync::OnceLock::new());+ let versions = Arc::new(EngineVersions::default());let handle = worker::spawn(&exe,&PathBuf::from(preset_path),@@ -196,15 +210,15 @@ pub async fn start_engine<R: tauri::Runtime>(models_max,slot_cache_mib,envs,- Some(fault_emitter(app_handle.clone(), toolkit.clone())),+ Some(fault_emitter(app_handle.clone(), versions.clone())),).await.map_err(|e| e.to_string())?;if let Some(v) = handle.cuda_toolkit {- let _ = toolkit.set(v);+ let _ = versions.toolkit.set(v);}// Detached: NVML can be slow to load, and start_engine holds the engine lock.- tokio::spawn(log_nvidia_driver(handle.cuda_toolkit));+ tokio::spawn(record_nvidia_driver(versions));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 7c7f7bfc2..18de79e71 100644--- a/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/driver.rs+++ b/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/driver.rs@@ -54,28 +54,30 @@ pub fn actionable_message(driver: &NvidiaDriver, toolkit: Option<CudaVersion>) -/// 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.+/// ahead of ggml's own words. Anything else is passed through: a Vulkan driver+/// error, which updating the NVIDIA driver does not fix, and a driver that is+/// known to support the engine's CUDA, where "too old" would contradict the+/// versions printed beside it.///-/// `driver` is only called for a line that qualifies, so an ordinary fault-/// does not pay for an NVML query.+/// With either version unknown the engine's own line is the evidence, so the+/// advice still applies.pub fn fault_payload(line: &str,- driver: impl FnOnce() -> Option<NvidiaDriver>,+ driver: Option<&NvidiaDriver>,toolkit: Option<CudaVersion>,) -> String {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)- );- }+ let Some(driver) = driver.filter(|_| cuda && crate::error::is_driver_too_old(&lower)) else {+ return line.to_string();+ };+ if matches!((driver.cuda, toolkit), (Some(supported), Some(built)) if supported >= built) {+ return line.to_string();}- line.to_string()+ format!(+ "{} Engine output: {line}",+ actionable_message(driver, toolkit)+ )}#[cfg(test)]@@ -173,11 +175,7 @@ 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}"@@ -190,16 +188,28 @@ mod tests {#[test]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,- || panic!("an OOM line must not query the driver"),- CUDA_13_2- ),- oom+ assert_eq!(fault_payload(oom, Some(&nvidia), CUDA_13_2), oom);+ assert_eq!(fault_payload(PTX_LINE, None, CUDA_13_2), PTX_LINE);+ }++ // The banner prints both versions, so "too old" beside a driver that+ // supports the engine's CUDA would contradict itself.+ #[test]+ fn a_driver_that_supports_the_engines_cuda_is_not_told_to_update() {+ let current = driver("595.97", Some((13, 2)));+ assert_eq!(fault_payload(PTX_LINE, Some(¤t), CUDA_13_2), PTX_LINE);+ }++ // The engine's own line is the evidence when a version is unknown.+ #[test]+ fn an_unknown_version_still_gets_the_advice_the_engine_asked_for() {+ let payload = fault_payload(PTX_LINE, Some(&driver("580.88", None)), None);+ assert!(+ payload.starts_with("Update your NVIDIA driver to the latest version."),+ "{payload}");- 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@@ -208,9 +218,6 @@ mod tests {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- );+ assert_eq!(fault_payload(vulkan, Some(&nvidia), CUDA_13_2), vulkan);}}