Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(trainer): pin CUDA_DEVICE_ORDER alongside CUDA_VISIBLE_DEVICES (B2)

purplea3e9b20f4bdf
diff --git a/src-tauri/src/commands/gpu.rs b/src-tauri/src/commands/gpu.rs
index 2a3ca642..309fe5d0 100644
--- a/src-tauri/src/commands/gpu.rs
+++ b/src-tauri/src/commands/gpu.rs
@@ -1484,7 +1484,12 @@ pub fn apply_gpu_env(cmd: &mut Command, selection: &GpuSelection) {
if selection.indices.is_empty() { return }
let csv: String = selection.indices.iter().map(|i| i.to_string()).collect::<Vec<_>>().join(",");
match selection.vendor.as_str() {
- "nvidia" => { cmd.env("CUDA_VISIBLE_DEVICES", &csv); }
+ // Trainer Nachbesserung Runde 2 (bau/review-trainer.md): the same
+ // ordering gap as trainer.rs::pin_trainer_gpu. Without
+ // CUDA_DEVICE_ORDER=PCI_BUS_ID, CUDA's own FASTEST_FIRST default can
+ // read this index against a different physical card than
+ // nvidia-smi's PCI-order list promised for Ollama and ComfyUI.
+ "nvidia" => { cmd.env("CUDA_DEVICE_ORDER", "PCI_BUS_ID"); cmd.env("CUDA_VISIBLE_DEVICES", &csv); }
"amd" => {
// HIP_VISIBLE_DEVICES is the official ROCm name; ROCR_VISIBLE_DEVICES
// is the lower-level Runtime equivalent that some older builds
@@ -1736,6 +1741,11 @@ End of search: 3 match(es) found.
// We can inspect envs via get_envs (Rust 1.69+).
let has_cuda = cmd.get_envs().any(|(k, v)| k == "CUDA_VISIBLE_DEVICES" && v.map(|s| s == "1,2").unwrap_or(false));
assert!(has_cuda, "CUDA_VISIBLE_DEVICES should be set to 1,2");
+ // Trainer Nachbesserung Runde 2: without this, CUDA's own
+ // FASTEST_FIRST default can disagree with nvidia-smi's PCI-order
+ // index, and Ollama/ComfyUI would pin the wrong physical card.
+ let has_order = cmd.get_envs().any(|(k, v)| k == "CUDA_DEVICE_ORDER" && v.map(|s| s == "PCI_BUS_ID").unwrap_or(false));
+ assert!(has_order, "CUDA_DEVICE_ORDER should be set to PCI_BUS_ID");
}
#[test]
diff --git a/src-tauri/src/commands/trainer.rs b/src-tauri/src/commands/trainer.rs
index 88027789..20c449e4 100644
--- a/src-tauri/src/commands/trainer.rs
+++ b/src-tauri/src/commands/trainer.rs
@@ -561,7 +561,20 @@ pub(crate) fn resolve_trainer_gpu(selection: &crate::commands::gpu::GpuSelection
/// Set on a Command right before it runs: every trainer child sees exactly
/// this one card, regardless of how many are actually plugged in.
+///
+/// BLOCKER fix, Runde 2: `choice.index` comes from `nvidia-smi`, which
+/// numbers cards in PCI bus order. CUDA itself, without `CUDA_DEVICE_ORDER`,
+/// defaults to FASTEST_FIRST, a different ordering whenever visible cards
+/// differ in speed. `CUDA_VISIBLE_DEVICES` alone can therefore pin the wrong
+/// physical card on a multi-GPU box, the VRAM preflight measures a
+/// different card than the one that trains, and the log line names the
+/// intended card while a different one runs. This also covers the branch in
+/// `choose_trainer_gpu` that reads a user-set `CUDA_VISIBLE_DEVICES`: its
+/// index is matched against `detect_gpus()`'s PCI-order list, so the index
+/// this function writes back out needs the same ordering to mean what it
+/// says.
fn pin_trainer_gpu(cmd: &mut Command, choice: &TrainerGpuChoice) {
+ cmd.env("CUDA_DEVICE_ORDER", "PCI_BUS_ID");
cmd.env("CUDA_VISIBLE_DEVICES", choice.index.to_string());
}
@@ -3107,6 +3120,11 @@ mod tests {
envs.contains(&("CUDA_VISIBLE_DEVICES".into(), Some("1".into()))),
"the chosen card's index is not what the child sees: {envs:?}",
);
+ assert!(
+ envs.contains(&("CUDA_DEVICE_ORDER".into(), Some("PCI_BUS_ID".into()))),
+ "BLOCKER B2 (Runde 2): without this, CUDA's own FASTEST_FIRST default \
+ can pin a different physical card than the index promises: {envs:?}",
+ );
}
/// The priority order the review asked for: the Hardware tab's own pick