Skip to content

armgit / kokon

Public Imported from GitHub
0

Edit photos by describing the change, on your own GPU. A free, open-source alternative to Nano Banana and ChatGPT image editing: photos stay on your computer, no daily limit. One installer for Windows, Mac and Linux sets up the best open model (Qwen-Image 2.1). MCP server for AI agents.

fix: SageAttention off on NVIDIA cards older than RTX 30 (compute capability < 8.0)

Gabriele Lozupone64f701df2128
diff --git a/crates/qls-core/src/config.rs b/crates/qls-core/src/config.rs
index c428444..54c0f69 100644
--- a/crates/qls-core/src/config.rs
+++ b/crates/qls-core/src/config.rs
@@ -292,6 +292,11 @@ pub struct AppConfig {
/// steps at 2K on an RTX 5070 Ti with near-identical output. CUDA only (SM80+);
/// ignored by other engines.
pub sage_attention: bool,
+ /// The card the engine runs on is too old for SageAttention (`GpuInfo::lacks_sage_attention`).
+ /// Set by `Engine::config` from the detected GPU and shown in Settings; never read from
+ /// the settings file.
+ #[serde(skip_deserializing)]
+ pub sage_unsupported: bool,
/// VAE tile size in pixels (`--vae-tiling --vae-tile-size N`); 0 disables tiling.
/// Untiled 2K decodes need ~18 GB, OOM and retry with 256px tiles (~22 s); 768px
/// tiles decode 2K in ~4 s on a 16 GB card.
@@ -349,6 +354,7 @@ impl Default for AppConfig {
vram_reserve_gib: 1.0,
flash_attention: true,
sage_attention: true,
+ sage_unsupported: false,
vae_tile_size: 768,
prefix_cache_type: "q8_0".into(),
eager_load: true,
@@ -707,9 +713,9 @@ impl AppConfig {
}
}
- /// SageAttention is requested and the engine supports it.
+ /// SageAttention is requested and the engine and card support it.
pub fn uses_sage_attention(&self) -> bool {
- self.sage_attention && self.gpu_backend == GpuBackend::Cuda
+ self.sage_attention && !self.sage_unsupported && self.gpu_backend == GpuBackend::Cuda
}
/// Folder of the upscaler (`--hires-upscalers-dir`).
@@ -919,6 +925,11 @@ mod tests {
assert_eq!(c.engine_dir(), c.sdcpp_dir);
assert_eq!(c.effective_device(), "cuda0");
assert!(c.uses_sage_attention());
+ c.sage_unsupported = true;
+ assert!(!c.uses_sage_attention(), "pre-Ampere card");
+ let saved: AppConfig = serde_json::from_str(&serde_json::to_string(&c).unwrap()).unwrap();
+ assert!(!saved.sage_unsupported, "detected per run, not read back");
+ c.sage_unsupported = false;
c.gpu_backend = GpuBackend::Vulkan;
assert_eq!(c.engine_dir(), c.sdcpp_dir.join("engine-vulkan"));
assert!(c.server_exe().starts_with(c.sdcpp_dir.join("engine-vulkan")));
diff --git a/crates/qls-core/src/diagnostics.rs b/crates/qls-core/src/diagnostics.rs
index bad53e9..8bafe23 100644
--- a/crates/qls-core/src/diagnostics.rs
+++ b/crates/qls-core/src/diagnostics.rs
@@ -20,9 +20,18 @@ pub struct GpuInfo {
pub memory_used_mib: u64,
pub memory_free_mib: u64,
pub driver_version: String,
+ /// NVIDIA compute capability, e.g. "7.5" (empty when unknown: other vendors, old drivers).
+ pub compute_capability: String,
}
impl GpuInfo {
+ /// An NVIDIA card older than Ampere (compute capability below 8.0, e.g. an RTX 2080 Ti):
+ /// the engine's SageAttention kernels need SM80+ and log an error instead. Unknown
+ /// capability counts as supported.
+ pub fn lacks_sage_attention(&self) -> bool {
+ self.vendor == "nvidia" && self.compute_capability.split('.').next().and_then(|m| m.parse::<u32>().ok()).is_some_and(|m| m < 8)
+ }
+
/// Memory the planner may use for the diffusion model, as on a discrete card. Apple
/// Silicon shares it with the system and the text encoder
/// (`planner::unified_memory_planning_mib`).
@@ -89,7 +98,7 @@ fn query_apple() -> Option<Vec<GpuInfo>> {
}
let total = read("hw.memsize")?.parse::<u64>().ok()? / (1024 * 1024);
let name = read("machdep.cpu.brand_string").unwrap_or_else(|| "Apple Silicon".into());
- Some(GpuInfo { name, vendor: "apple".into(), unified: true, memory_total_mib: total, memory_used_mib: 0, memory_free_mib: total, driver_version: String::new() })
+ Some(GpuInfo { name, vendor: "apple".into(), unified: true, memory_total_mib: total, memory_used_mib: 0, memory_free_mib: total, ..Default::default() })
})
.clone()
.map(|g| vec![g])
@@ -102,20 +111,23 @@ fn query_apple() -> Option<Vec<GpuInfo>> {
/// NVIDIA GPUs through nvidia-smi (ships with the driver). None if unavailable.
fn query_nvidia() -> Option<Vec<GpuInfo>> {
+ // Drivers before compute_cap existed (R510) reject the whole query: ask again without it.
+ let fields = "name,memory.total,memory.used,memory.free,driver_version";
+ let text = nvidia_smi(&format!("{fields},compute_cap")).or_else(|| nvidia_smi(fields))?;
+ let gpus: Vec<GpuInfo> = text.lines().filter_map(parse_gpu_line).collect();
+ (!gpus.is_empty()).then_some(gpus)
+}
+
+fn nvidia_smi(fields: &str) -> Option<String> {
let mut cmd = std::process::Command::new("nvidia-smi");
- cmd.args(["--query-gpu=name,memory.total,memory.used,memory.free,driver_version", "--format=csv,noheader,nounits"]);
+ cmd.args([format!("--query-gpu={fields}").as_str(), "--format=csv,noheader,nounits"]);
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
cmd.creation_flags(0x0800_0000);
}
let out = cmd.output().ok()?;
- if !out.status.success() {
- return None;
- }
- let text = String::from_utf8_lossy(&out.stdout);
- let gpus: Vec<GpuInfo> = text.lines().filter_map(parse_gpu_line).collect();
- (!gpus.is_empty()).then_some(gpus)
+ out.status.success().then(|| String::from_utf8_lossy(&out.stdout).into_owned())
}
pub fn parse_gpu_line(line: &str) -> Option<GpuInfo> {
@@ -131,6 +143,7 @@ pub fn parse_gpu_line(line: &str) -> Option<GpuInfo> {
memory_used_mib: f[2].parse().ok()?,
memory_free_mib: f[3].parse().ok()?,
driver_version: f[4].to_string(),
+ compute_capability: f.get(5).filter(|c| c.parse::<f32>().is_ok()).map(|c| c.to_string()).unwrap_or_default(),
})
}
@@ -169,6 +182,7 @@ fn query_dxgi() -> Option<Vec<GpuInfo>> {
memory_used_mib: 0,
memory_free_mib: total,
driver_version: String::new(),
+ compute_capability: String::new(),
});
}
gpus.sort_by(|a, b| b.memory_total_mib.cmp(&a.memory_total_mib));
@@ -206,6 +220,7 @@ fn query_drm() -> Option<Vec<GpuInfo>> {
memory_used_mib: used,
memory_free_mib: total.saturating_sub(used),
driver_version: String::new(),
+ compute_capability: String::new(),
});
}
gpus.sort_by(|a, b| b.memory_total_mib.cmp(&a.memory_total_mib));
@@ -382,6 +397,14 @@ mod tests {
assert_eq!(g.driver_version, "617.14");
assert!(parse_gpu_line("garbage").is_none());
assert_eq!(g.vendor, "nvidia");
+ assert!(g.compute_capability.is_empty() && !g.lacks_sage_attention(), "old driver: unknown, kept on");
+ let g = parse_gpu_line("NVIDIA GeForce RTX 5070 Ti, 16303, 2312, 13991, 617.14, 12.0").unwrap();
+ assert_eq!(g.compute_capability, "12.0");
+ assert!(!g.lacks_sage_attention());
+ let turing = parse_gpu_line("NVIDIA GeForce RTX 2080 Ti, 11264, 500, 10764, 581.57, 7.5").unwrap();
+ assert!(turing.lacks_sage_attention(), "SageAttention needs SM80+");
+ assert!(!parse_gpu_line("NVIDIA GeForce RTX 3060, 12288, 0, 12288, 581.57, 8.6").unwrap().lacks_sage_attention());
+ assert!(!parse_gpu_line("NVIDIA GeForce RTX 3060, 12288, 0, 12288, 581.57, [N/A]").unwrap().lacks_sage_attention());
assert_eq!(g.planning_mib(), 16303);
let mac = GpuInfo { name: "Apple M2 Pro".into(), vendor: "apple".into(), unified: true, memory_total_mib: 16384, ..Default::default() };
assert!(mac.planning_mib() < 6 * 1024, "16 GB Mac plans like a small card");
diff --git a/crates/qls-core/src/engine.rs b/crates/qls-core/src/engine.rs
index 5234f7c..7b0c2e7 100644
--- a/crates/qls-core/src/engine.rs
+++ b/crates/qls-core/src/engine.rs
@@ -182,7 +182,29 @@ impl Engine {
}
pub fn config(&self) -> AppConfig {
- self.cfg.read().unwrap().clone()
+ let mut cfg = self.cfg.read().unwrap().clone();
+ cfg.sage_unsupported = self.lacks_sage_attention(&cfg);
+ cfg
+ }
+
+ /// The card `cfg` runs the CUDA engine on can't run SageAttention. Uses the cached card
+ /// list (queried once if empty) and engine devices, never `config()` or an engine probe.
+ fn lacks_sage_attention(&self, cfg: &AppConfig) -> bool {
+ if cfg.gpu_backend != crate::config::GpuBackend::Cuda {
+ return false;
+ }
+ let cached = {
+ let g = self.gpus.lock().unwrap();
+ g.0.is_some().then(|| g.1.clone())
+ };
+ let cards = cached.unwrap_or_else(|| self.gpus());
+ let i = if cards.len() < 2 {
+ 0
+ } else {
+ let devices = self.devices.lock().unwrap().as_ref().map(|d| d.2.clone()).unwrap_or_default();
+ crate::diagnostics::card_for_device(&devices, &cards, &cfg.effective_device(), cfg.gpu_backend.device_prefix()).unwrap_or(0)
+ };
+ cards.get(i).is_some_and(GpuInfo::lacks_sage_attention)
}
/// Replaces the configuration. Returns true when the backend must restart.
@@ -191,7 +213,9 @@ impl Engine {
new.normalize();
new.validate()?;
new.save()?;
+ new.sage_unsupported = self.lacks_sage_attention(&new);
let mut g = self.cfg.write().unwrap();
+ g.sage_unsupported = self.lacks_sage_attention(&g);
let restart = crate::process::build_server_args(&g, g.profile()) != crate::process::build_server_args(&new, new.profile())
|| g.sdcpp_dir != new.sdcpp_dir
|| g.gpu_backend != new.gpu_backend;
diff --git a/crates/qls-service/src/api.rs b/crates/qls-service/src/api.rs
index bae81e2..65e1fcb 100644
--- a/crates/qls-service/src/api.rs
+++ b/crates/qls-service/src/api.rs
@@ -512,7 +512,7 @@ async fn diagnostics_json(State(s): St) -> ApiResult<Json<Value>> {
match &gpus {
Some(g) => {
for x in g {
- let _ = writeln!(text, "{} — {} MiB total, {} MiB used, {} MiB free, driver {}", x.name, x.memory_total_mib, x.memory_used_mib, x.memory_free_mib, x.driver_version);
+ let _ = writeln!(text, "{} — {} MiB total, {} MiB used, {} MiB free, driver {}{}", x.name, x.memory_total_mib, x.memory_used_mib, x.memory_free_mib, x.driver_version, if x.compute_capability.is_empty() { String::new() } else { format!(", compute capability {}", x.compute_capability) });
}
}
None => {
@@ -557,7 +557,7 @@ async fn diagnostics_json(State(s): St) -> ApiResult<Json<Value>> {
};
let _ = writeln!(text, "{label}: {} ({state})", p.display());
}
- let _ = writeln!(text, "Device: {}, text encoder: {:?}, VRAM reserve: {} GiB, flash attention: {}, sage attention: {}", cfg.device, cfg.text_encoder_placement, cfg.vram_reserve_gib, cfg.flash_attention, cfg.sage_attention);
+ let _ = writeln!(text, "Device: {}, text encoder: {:?}, VRAM reserve: {} GiB, flash attention: {}, sage attention: {} (in use: {})", cfg.device, cfg.text_encoder_placement, cfg.vram_reserve_gib, cfg.flash_attention, cfg.sage_attention, cfg.uses_sage_attention());
let _ = writeln!(text, "VAE tile size: {}, upscale tile size: {}", cfg.vae_tile_size, cfg.upscale_tile_size);
let up = diagnostics::upscaler_check(&cfg);
let model = cfg.upscaler().map(|u| u.0).unwrap_or_default();
diff --git a/src/components/Settings.tsx b/src/components/Settings.tsx
index a68c7dd..0422542 100644
--- a/src/components/Settings.tsx
+++ b/src/components/Settings.tsx
@@ -493,10 +493,13 @@ export function Settings() {
<Switch on={cfg.flashAttention} onChange={(v) => set({ flashAttention: v })} />
</div>
{cfg.gpuBackend === "cuda" && (
- <div className="field-row">
- <span>SageAttention (faster sampling)</span>
- <Switch on={cfg.sageAttention} onChange={(v) => set({ sageAttention: v })} />
- </div>
+ <>
+ <div className="field-row">
+ <span>SageAttention (faster sampling)</span>
+ <Switch on={cfg.sageAttention && !cfg.sageUnsupported} disabled={cfg.sageUnsupported} onChange={(v) => set({ sageAttention: v })} />
+ </div>
+ {cfg.sageUnsupported && <span className="muted" style={{ marginTop: -10 }}>Needs an RTX 30 series or newer card; off on this one.</span>}
+ </>
)}
<label className="field-row">
<span>VAE tile size (px, 0 = off)</span>
diff --git a/src/lib/types.ts b/src/lib/types.ts
index 19ba2ae..8fe3171 100644
--- a/src/lib/types.ts
+++ b/src/lib/types.ts
@@ -233,6 +233,8 @@ export interface AppConfig {
vramReserveGib: number;
flashAttention: boolean;
sageAttention: boolean;
+ /** The card is too old for SageAttention (before RTX 30 / compute capability 8.0); detected, read-only. */
+ sageUnsupported?: boolean;
vaeTileSize: number;
prefixCacheType: string;
eagerLoad: boolean;