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.
TypeScript ai-photo-editorcomfyui-alternativegenerative-fillimage-editingimage-generationinpaintinglinuxlocal-aimacosmcpmcp-servernano-banana-alternativeofflinephoto-editorprivacyqwen-imageqwen-image-2-1stable-diffusion-cppvulkanwindows
fix(setup): choose the GPU on multi-GPU machines (#16)
Gabriele Lozupone82d7d78fe11d
diff --git a/crates/qls-core/src/diagnostics.rs b/crates/qls-core/src/diagnostics.rsindex 32ba30d..476e785 100644--- a/crates/qls-core/src/diagnostics.rs+++ b/crates/qls-core/src/diagnostics.rs@@ -43,6 +43,37 @@ pub fn query_gpus() -> Option<Vec<GpuInfo>> {query_nvidia().or_else(query_dxgi).or_else(query_apple)}+/// Card and engine device names refer to the same GPU ("NVIDIA GeForce RTX 3090" from+/// nvidia-smi, DXGI or ggml; one may carry a suffix).+fn same_gpu(card: &str, device: &str) -> bool {+ let (a, b) = (card.trim().to_ascii_lowercase(), device.trim().to_ascii_lowercase());+ !a.is_empty() && !b.is_empty() && (a.contains(&b) || b.contains(&a))+}++/// Engine devices (`--list-devices`) of this engine's kind, in their order.+fn engine_gpus<'a>(devices: &'a [(String, String)], prefix: &str) -> impl Iterator<Item = &'a (String, String)> {+ let prefix = prefix.to_string();+ devices.iter().filter(move |(n, _)| n.to_ascii_lowercase().starts_with(&prefix))+}++/// The engine device (e.g. "cuda1") of card `card` in `cards`. Matched by name, not by+/// position: nvidia-smi, DXGI (sorted by VRAM) and the engine each order cards their own+/// way. The k-th card of a name maps to the k-th engine device of that name.+pub fn device_for_card(devices: &[(String, String)], cards: &[GpuInfo], card: usize, prefix: &str) -> Option<String> {+ let name = &cards.get(card)?.name;+ let k = cards[..card].iter().filter(|c| c.name.eq_ignore_ascii_case(name)).count();+ engine_gpus(devices, prefix).filter(|(_, d)| same_gpu(name, d)).nth(k).map(|(n, _)| n.to_ascii_lowercase())+}++/// The card in `cards` behind engine device `device` (inverse of `device_for_card`).+pub fn card_for_device(devices: &[(String, String)], cards: &[GpuInfo], device: &str, prefix: &str) -> Option<usize> {+ let gpus: Vec<_> = engine_gpus(devices, prefix).collect();+ let i = gpus.iter().position(|(n, _)| n.eq_ignore_ascii_case(device))?;+ let desc = &gpus[i].1;+ let k = gpus[..i].iter().filter(|(_, d)| d.eq_ignore_ascii_case(desc)).count();+ cards.iter().enumerate().filter(|(_, c)| same_gpu(&c.name, desc)).nth(k).map(|(j, _)| j)+}+/// Apple Silicon: chip name and unified memory from sysctl (they never change, so read once).#[cfg(target_os = "macos")]fn query_apple() -> Option<Vec<GpuInfo>> {@@ -284,6 +315,26 @@ mod tests {assert_eq!(crate::catalog::recommended_preset(Some(mac.planning_mib())).id, "12gb");}+ #[test]+ fn cards_map_to_engine_devices_by_name() {+ let card = |n: &str, mib| GpuInfo { name: n.into(), memory_total_mib: mib, ..Default::default() };+ // DXGI sorts by VRAM; the engine lists the 3060 first.+ let cards = vec![card("NVIDIA GeForce RTX 3090", 24576), card("NVIDIA GeForce RTX 3060", 12288), card("NVIDIA GeForce RTX 3060", 12288)];+ let devs: Vec<(String, String)> = [("CUDA0", "NVIDIA GeForce RTX 3060"), ("CUDA1", "NVIDIA GeForce RTX 3090"), ("CUDA2", "NVIDIA GeForce RTX 3060"), ("CPU", "AMD Ryzen 9")]+ .iter()+ .map(|(a, b)| (a.to_string(), b.to_string()))+ .collect();+ assert_eq!(device_for_card(&devs, &cards, 0, "cuda").as_deref(), Some("cuda1"));+ assert_eq!(device_for_card(&devs, &cards, 1, "cuda").as_deref(), Some("cuda0"));+ assert_eq!(device_for_card(&devs, &cards, 2, "cuda").as_deref(), Some("cuda2"));+ assert_eq!(device_for_card(&devs, &cards, 0, "vulkan"), None, "another engine's devices");+ for i in 0..3 {+ let dev = device_for_card(&devs, &cards, i, "cuda").unwrap();+ assert_eq!(card_for_device(&devs, &cards, &dev, "cuda"), Some(i));+ }+ assert_eq!(card_for_device(&devs, &cards, "cuda7", "cuda"), None);+ }+#[test]fn dxgi_reports_real_adapters() {assert_eq!((vendor_name(0x10DE), vendor_name(0x1002), vendor_name(0x8086), vendor_name(0x5143)), ("nvidia", "amd", "intel", "other"));diff --git a/crates/qls-core/src/engine.rs b/crates/qls-core/src/engine.rsindex fbeb034..f4b70c0 100644--- a/crates/qls-core/src/engine.rs+++ b/crates/qls-core/src/engine.rs@@ -99,13 +99,18 @@ pub struct Engine {active: Mutex<Option<String>>,wake: Notify,pub events: broadcast::Sender<Event>,- gpu: Mutex<(Option<Instant>, Option<GpuInfo>)>,+ gpus: Mutex<(Option<Instant>, Vec<GpuInfo>)>,+ /// `--list-devices` of the engine, keyed by its path and modification time.+ devices: Mutex<Option<DeviceList>>,/// First-run downloads (setup wizard).pub installer: Arc<crate::installer::Installer>,backend_version: Mutex<Option<String>>,last_activity: Mutex<Instant>,}+/// An engine's path, its modification time and its `--list-devices` output.+type DeviceList = (PathBuf, Option<std::time::SystemTime>, Vec<(String, String)>);+fn random_seed() -> i64 {let b = uuid::Uuid::new_v4();let v = u32::from_le_bytes(b.as_bytes()[..4].try_into().unwrap());@@ -168,7 +173,8 @@ impl Engine {active: Mutex::new(None),wake: Notify::new(),events,- gpu: Mutex::new((None, None)),+ gpus: Mutex::new((None, Vec::new())),+ devices: Mutex::new(None),installer: crate::installer::Installer::new(),backend_version: Mutex::new(None),last_activity: Mutex::new(Instant::now()),@@ -268,14 +274,63 @@ impl Engine {self.active.lock().unwrap().is_some() || self.jobs.lock().unwrap().values().any(|j| !j.view.status.is_terminal()) || self.installer.is_running()}+ /// Every graphics card found (refreshed every few seconds for the live memory use).+ pub fn gpus(&self) -> Vec<GpuInfo> {+ let mut g = self.gpus.lock().unwrap();+ if !g.0.is_some_and(|t| t.elapsed() < Duration::from_secs(3)) {+ *g = (Some(Instant::now()), query_gpus().unwrap_or_default());+ }+ g.1.clone()+ }++ /// The engine's devices (`--list-devices`); empty until the engine is installed.+ pub fn devices(&self) -> Vec<(String, String)> {+ let exe = self.config().server_exe();+ let mtime = std::fs::metadata(&exe).and_then(|m| m.modified()).ok();+ if mtime.is_none() {+ return Vec::new();+ }+ let mut d = self.devices.lock().unwrap();+ if let Some((p, t, list)) = d.as_ref()+ && *p == exe+ && *t == mtime+ {+ return list.clone();+ }+ let list = crate::diagnostics::list_devices(&exe);+ *d = Some((exe, mtime, list.clone()));+ list+ }++ /// Index in `gpus()` of the card the engine runs on (the configured device); the first+ /// card when there is one or the engine can't tell.+ pub fn gpu_index(&self) -> usize {+ let cards = self.gpus();+ if cards.len() < 2 {+ return 0;+ }+ let cfg = self.config();+ crate::diagnostics::card_for_device(&self.devices(), &cards, &cfg.effective_device(), cfg.gpu_backend.device_prefix()).unwrap_or(0)+ }++ /// The card the engine runs on; plans and presets are sized for it.pub fn gpu(&self) -> Option<GpuInfo> {- let mut g = self.gpu.lock().unwrap();- if g.0.map(|t| t.elapsed() < Duration::from_secs(3)).unwrap_or(false) {- return g.1.clone();+ let i = self.gpu_index();+ self.gpus().into_iter().nth(i)+ }++ /// Runs the engine on card `card` of `gpus()`: Some(whether the server must restart), or+ /// None if the engine can't list its devices yet (not installed) or doesn't see that card.+ pub fn use_card(&self, card: usize) -> AppResult<Option<bool>> {+ let mut cfg = self.config();+ let Some(dev) = crate::diagnostics::device_for_card(&self.devices(), &self.gpus(), card, cfg.gpu_backend.device_prefix()) else {+ return Ok(None);+ };+ if cfg.effective_device() == dev {+ return Ok(Some(false));}- let info = query_gpus().and_then(|v| v.into_iter().next());- *g = (Some(Instant::now()), info.clone());- info+ cfg.device = dev;+ self.set_config(cfg).map(Some)}pub fn backend_version(&self) -> Option<String> {diff --git a/crates/qls-service/src/api.rs b/crates/qls-service/src/api.rsindex 1ba218e..96c6ca9 100644--- a/crates/qls-service/src/api.rs+++ b/crates/qls-service/src/api.rs@@ -61,6 +61,7 @@ pub fn router(state: Arc<AppState>) -> Router {.route("/setup/hardware", get(setup_hardware)).route("/setup/install", get(install_status).post(install_start)).route("/setup/install/cancel", post(install_cancel))+ .route("/setup/gpu", post(setup_gpu)).route("/setup/legacy-upscalers", get(legacy_upscalers).delete(remove_legacy_upscalers)).route("/upscalers", get(list_upscalers)).route("/upscalers/{name}/download", post(download_upscaler))@@ -221,13 +222,18 @@ async fn setup(State(s): St) -> ApiResult<Json<Value>> {struct HardwareQ {/// Engine to list the downloads for; default: the configured one.backend: Option<GpuBackend>,+ /// Card (index in `gpus`) to size the presets for; default: the one in use.+ gpu: Option<usize>,}async fn setup_hardware(State(s): St, Query(q): Query<HardwareQ>) -> ApiResult<Json<Value>> {let engine = s.engine.clone();let v = tokio::task::spawn_blocking(move || {let cfg = engine.config();- let gpu = engine.gpu();+ let cards = engine.gpus();+ let in_use = engine.gpu_index();+ let card = q.gpu.filter(|&i| i < cards.len()).unwrap_or(in_use);+ let gpu = cards.get(card).cloned();let detected = gpu.as_ref().map(|g| g.planning_mib());let vram = cfg.planning_vram_mib(detected);let mut sys = sysinfo::System::new();@@ -250,6 +256,9 @@ async fn setup_hardware(State(s): St, Query(q): Query<HardwareQ>) -> ApiResult<J.collect();json!({"gpu": gpu,+ "gpus": cards.iter().map(|c| json!({ "name": c.name, "memory_total_mib": c.memory_total_mib })).collect::<Vec<_>>(),+ "gpu_index": card,+ "gpu_in_use": in_use,"nvidia": vendor.as_deref() == Some("nvidia"),"vendor": vendor,"backend": backend,@@ -280,6 +289,8 @@ struct InstallBody {/// Make the preset active when done (false: an engine update keeps the current settings).#[serde(default = "yes")]apply: bool,+ /// Card (index in `/setup/hardware` `gpus`) to run on once the engine is installed.+ gpu: Option<usize>,}fn yes() -> bool {@@ -305,6 +316,7 @@ async fn install_start(State(s): St, Json(b): Json<InstallBody>) -> ApiResult<Js// Below ~8 GiB of planning memory (a 16 GB Mac) only streaming weights from disk fits.let small_unified = s.engine.gpu().is_some_and(|g| g.unified && g.planning_mib() < 8 * 1024);let apply = b.apply;+ let card = b.gpu;let on_finish = Box::new(move |complete: bool| {if !complete || !apply {return;@@ -318,6 +330,14 @@ async fn install_start(State(s): St, Json(b): Json<InstallBody>) -> ApiResult<Jsif let Err(e) = engine.set_config(c) {tracing::error!("could not save the installed preset: {}", e.message);}+ // The engine can name its devices only now that it is installed.+ if let Some(i) = card {+ match engine.use_card(i) {+ Ok(Some(_)) => {}+ Ok(None) => tracing::warn!("the engine does not list card {i}; keeping device {}", engine.config().device),+ Err(e) => tracing::error!("could not save the chosen GPU: {}", e.message),+ }+ }});// The bundled upscaler first, so it is not downloaded again.let e = s.engine.clone();@@ -371,6 +391,27 @@ async fn remove_legacy_upscalers(State(s): St, Query(q): Query<KeepQ>) -> ApiResOk(Json(json!({ "freed_bytes": freed })))}+#[derive(Deserialize)]+struct GpuBody {+ /// Index in `/setup/hardware` `gpus`.+ gpu: usize,+}++/// Runs the engine on another card (setup's GPU picker).+async fn setup_gpu(State(s): St, Json(b): Json<GpuBody>) -> ApiResult<Json<Value>> {+ let e = s.engine.clone();+ let r = tokio::task::spawn_blocking(move || e.use_card(b.gpu)).await.map_err(AppError::internal)??;+ let Some(restart) = r else {+ return Err(AppError::invalid("The engine does not see this graphics card. Pick it in Settings → Performance → GPU device.").into());+ };+ let st = s.engine.server.status();+ if restart && st.owned && st.state != qls_core::process::ServerState::Stopped && !s.engine.has_pending_work() {+ s.engine.server.stop().await;+ s.engine.emit_server();+ }+ Ok(Json(json!({ "saved": true })))+}+async fn install_status(State(s): St) -> Json<Value> {Json(serde_json::to_value(s.engine.installer.status()).unwrap())}diff --git a/src/components/Setup.tsx b/src/components/Setup.tsxindex 46ef14e..3367302 100644--- a/src/components/Setup.tsx+++ b/src/components/Setup.tsx@@ -85,16 +85,18 @@ export function Setup() {const [inst, setInst] = useState<InstallStatus | null>(null);const [preset, setPreset] = useState<string | null>(null);const [backend, setBackend] = useState<GpuBackend | null>(null);+ // Card picked on a multi-GPU machine (index in hw.gpus); null: the one in use.+ const [card, setCard] = useState<number | null>(null);const [showFiles, setShowFiles] = useState(false);const [checking, setChecking] = useState(false);const wasRunning = useRef(false);const setupDone = useStore((s) => s.status?.setup_complete ?? false);const engineUpdate = useStore((s) => s.status?.engine);- const refresh = async (engine: GpuBackend | null = backend) => {+ const refresh = async (engine: GpuBackend | null = backend, gpu: number | null = card) => {setChecking(true);try {- const [h, r, i] = await Promise.all([api.hardware(engine ?? undefined), api.setup(), api.installStatus()]);+ const [h, r, i] = await Promise.all([api.hardware(engine ?? undefined, gpu ?? undefined), api.setup(), api.installStatus()]);setHw(h);setBackend(h.backend);setReport(r);@@ -139,7 +141,7 @@ export function Setup() {const install = async () => {if (!preset) return;try {- setInst(await api.install(preset, backend ?? hw?.backend ?? "cuda"));+ setInst(await api.install(preset, backend ?? hw?.backend ?? "cuda", true, card ?? undefined));} catch (e) {toastError(e);}@@ -147,6 +149,7 @@ export function Setup() {const start = async () => {try {+ if (hw && card !== null && card !== hw.gpu_in_use) await api.setGpu(card);const [cfg, st] = await Promise.all([api.config(), refreshStatus()]);// An engine update the user chose not to install now: don't open Setup for it again.const ui = { ...((cfg.ui ?? {}) as Record<string, unknown>) };@@ -240,6 +243,29 @@ export function Setup() {</div>) : (<>+ {hw.gpus.length > 1 && (+ <label className="field">+ <span>Graphics card</span>+ <select+ className="select"+ value={hw.gpu_index}+ onChange={(e) => {+ const i = Number(e.target.value);+ setCard(i);+ setPreset(null); // the recommendation follows the card+ refresh(backend, i);+ }}+ >+ {hw.gpus.map((g, i) => (+ <option key={i} value={i}>+ {g.name} · {gb(g.memory_total_mib)}+ {i === hw.gpu_in_use && setupDone ? " (in use)" : ""}+ </option>+ ))}+ </select>+ <span className="muted">Kokon runs on one card. The preset is sized for the card you pick; you can change it later in Settings → Performance.</span>+ </label>+ )}{hw.gpu && (<div className="field"><span>Engine</span>@@ -248,7 +274,7 @@ export function Setup() {options={engineOptions(hw.vendor)}onChange={(b) => {setBackend(b);- refresh(b);+ refresh(b, card);}}/><span className="muted">{engineNote(backend ?? hw.backend, hw.nvidia)}</span>diff --git a/src/lib/api.ts b/src/lib/api.tsindex c11a1bd..a1d82f9 100644--- a/src/lib/api.ts+++ b/src/lib/api.ts@@ -54,9 +54,19 @@ const json = (body: unknown) => JSON.stringify(body);export const api = {status: () => call<StatusResponse>("/status"),setup: () => call<SetupReport>("/setup"),- hardware: (backend?: GpuBackend) => call<HardwareReport>(backend ? `/setup/hardware?backend=${backend}` : "/setup/hardware"),- /** `apply = false`: only download (an engine update), keeping the configured preset and settings. */- install: (preset: string, backend: GpuBackend, apply = true) => call<InstallStatus>("/setup/install", { method: "POST", body: json({ preset, backend, apply }) }),+ /** `gpu`: index in `gpus` to size the presets for (default: the card in use). */+ hardware: (backend?: GpuBackend, gpu?: number) => {+ const q = new URLSearchParams();+ if (backend) q.set("backend", backend);+ if (gpu !== undefined) q.set("gpu", String(gpu));+ const qs = q.toString();+ return call<HardwareReport>(`/setup/hardware${qs ? `?${qs}` : ""}`);+ },+ /** `apply = false`: only download (an engine update), keeping the configured preset and settings.+ * `gpu`: card to run on once installed (index in `hardware().gpus`). */+ install: (preset: string, backend: GpuBackend, apply = true, gpu?: number) => call<InstallStatus>("/setup/install", { method: "POST", body: json({ preset, backend, apply, gpu }) }),+ /** Runs the engine on another card (index in `hardware().gpus`); needs the engine installed. */+ setGpu: (gpu: number) => call<{ saved: boolean }>("/setup/gpu", { method: "POST", body: json({ gpu }) }),upscalers: () => call<{ selected: string; active: string | null; items: UpscalerItem[] }>("/upscalers"),/** Progress in `installStatus` (preset "upscaler:<name>"). */downloadUpscaler: (name: string) => call<InstallStatus>(`/upscalers/${encodeURIComponent(name)}/download`, { method: "POST" }),diff --git a/src/lib/types.ts b/src/lib/types.tsindex 8d2b313..096d43d 100644--- a/src/lib/types.ts+++ b/src/lib/types.ts@@ -288,7 +288,12 @@ export interface HardwarePreset {}export interface HardwareReport {+ /** The card the presets are sized for (`gpu_index` in `gpus`). */gpu: StatusResponse["gpu"];+ gpus: { name: string; memory_total_mib: number }[];+ gpu_index: number;+ /** Card the engine runs on now. */+ gpu_in_use: number;nvidia: boolean;vendor: string | null;/** Engine the presets' file lists are for. */