diff --git a/docs/TODO.txt b/docs/TODO.txt index d28add3bf..b5e1413f5 100644 --- a/docs/TODO.txt +++ b/docs/TODO.txt @@ -308,6 +308,12 @@ random source; they open /dev/urandom, so the selene test suite is not qualified on Windows. +[ ] Run the Helios release tests on Windows once the RoCE sysfs fixture in + helios/src/execution/roce_fabric.rs is limited to Linux. The fixture uses + std::os::unix::fs::symlink and prevents the Windows test binary from compiling. + Preserve LF bytes in the Kimi K3 template used by the message_boundaries + integration test. A CRLF checkout changes the hash used by the upgrade check. + [ ] Qualify the Windows desktop client beyond Windows 11 ARM64: x64 linking and inference, Flare.exe on a machine without the development environment, transfer interruption, switching under load, accessibility, and recovery diff --git a/flare-winui/README.md b/flare-winui/README.md index 4401198b1..f3129c792 100644 --- a/flare-winui/README.md +++ b/flare-winui/README.md @@ -225,7 +225,7 @@ Reload model finishes accepted requests before releasing and loading the core. | Setting | Runtime behavior | |---|---| -| Serving GPU | Selects one visible NVIDIA GPU by device index, name, and total memory. Defaults to GPU 0 and respects inherited `CUDA_VISIBLE_DEVICES` numbering. Save applies the selection on the next load or reload; the dashboard continues to measure the active model's GPU until then. The desktop accepts single-GPU cores. | +| Serving GPU | Lists visible NVIDIA GPUs by index, name, and reported total memory. The selection defaults to GPU 0 and follows CUDA's visible device numbering when `CUDA_VISIBLE_DEVICES` is set. Model loading matches the selected GPU's UUID to its CUDA ordinal. Save applies the selection on load or reload. Until then, the dashboard measures the active model's GPU. The desktop accepts single-GPU cores. | | Serving memory limit | `30G` caps occupied memory, `-8G` reserves free memory, and `0.8` selects a total-memory fraction per GPU. Blank inherits the configured/core reserve. KV pages use the remainder after model and runtime allocations. | | KV history capacity | One model context by default. Available memory and Context × concurrency are also selectable. The limit applies to KV history shared across requests. | | Maximum concurrent requests | Blank inherits model/configuration/core defaults. An override must fit prepared capacity. | diff --git a/flare/src/controller.rs b/flare/src/controller.rs index bd26bc59b..3e618d4c0 100644 --- a/flare/src/controller.rs +++ b/flare/src/controller.rs @@ -1265,9 +1265,8 @@ impl Worker { .map_err(|e| e.to_string())?.1.is_none() { options.revision = Some( - crate::hardware::detect_devices(&[job.settings.runtime.gpu]) - .map_err(|e| e.to_string())? - .into(), + crate::hardware::desktop_platform(job.settings.runtime.gpu) + .map_err(|e| e.to_string())?, ); } let store = ModelStore::open(job.saved.directory.clone()) @@ -1522,8 +1521,8 @@ impl Worker { Command::LocateReference { id, path } => self.locate_reference(&id, path), Command::Discover(query) => { self.shared.update(|s| s.view.catalog_loading = true); - let hardware = crate::hardware::detect_devices(&[self.settings.runtime.gpu]).ok(); - let result = crate::catalog::search_for_hardware(&query, hardware) + let hardware = crate::hardware::desktop_platform(self.settings.runtime.gpu).ok(); + let result = crate::catalog::search_for_hardware(&query, hardware.as_deref()) .map_err(|e| e.to_string()); self.shared.update(|s| s.view.catalog_loading = false); let entries = result?; @@ -2812,15 +2811,12 @@ impl Worker { } let names = authorized.core.model_names().map_err(|e| e.to_string())?; let gpu = self.settings.runtime.gpu; - if !crate::hardware::inventory().map_err(|e| e.to_string())?.iter() - .any(|device| device.visible_ordinal == Some(gpu as u32)) - { - return Err(format!("Serving GPU {gpu} is unavailable; choose a visible GPU in Settings")); - } - let options = self + let cuda_ordinal = crate::hardware::desktop_cuda_ordinal(gpu).map_err(|e| e.to_string())?; + let mut options = self .settings .load_options(&names) .map_err(|e| e.to_string())?; + options.device_labels = Some(vec![gpu]); let runtime = RuntimeIdentity::new(&self.settings.runtime, &options)?; let model_names: Vec = names.iter().map(|name| (*name).to_owned()).collect(); let path = authorized.path.canonicalize().map_err(|e| e.to_string())?.display().to_string(); @@ -2883,7 +2879,7 @@ impl Worker { // SAFETY: Publisher consent authorizes running a trusted prepared core. // Its programs must obey the loader's CUDA contract; signatures do not // establish memory safety. The owned session retains resources through drain. - let owner = match unsafe { NativeModel::load(core, &[runtime.gpu], &options, &progress) } { + let owner = match unsafe { NativeModel::load(core, &[cuda_ordinal], &options, &progress) } { Ok(owner) => owner, Err(error) => { // The loader does not return drain evidence. Keep the core diff --git a/flare/src/hardware.rs b/flare/src/hardware.rs index d45f3bfae..3c41ba499 100644 --- a/flare/src/hardware.rs +++ b/flare/src/hardware.rs @@ -3,15 +3,17 @@ * SPDX-License-Identifier: Apache-2.0 */ -//! Hardware revision selection through NVML without initializing CUDA. +//! Hardware revision selection and desktop GPU inventory. #[path = "hardware_monitor.rs"] mod monitor; pub use monitor::{HardwareMonitor, HardwareSnapshot}; use std::ffi::{CString, c_int}; +use std::fmt::Write; use flare_helios::nvml::{NOT_SUPPORTED as NVML_ERROR_NOT_SUPPORTED, Nvml}; +use flare_helios::runtime_core::driver::Driver; type Result = std::result::Result>; @@ -99,6 +101,7 @@ pub fn detect_devices(indices: &[i32]) -> Result<&'static str> { #[derive(Clone, Debug, Default)] pub struct GpuInfo { pub ordinal: u32, + /// The index shown in desktop settings, which can differ from the CUDA ordinal. pub visible_ordinal: Option, pub name: String, pub uuid: String, @@ -110,6 +113,71 @@ pub struct GpuInfo { /// Enumerate actual NVIDIA devices and their inherited visibility without changing process policy. pub fn inventory() -> Result> { + let mut devices = nvml_inventory()?; + let cuda = if has_visibility_mask() { + Some(cuda_uuids()?) + } else { + None + }; + assign_visibility(&mut devices, cuda.as_deref()); + Ok(devices) +} + +pub(crate) fn desktop_platform(index: i32) -> Result { + let devices = inventory()?; + let gpu = selected_gpu(&devices, index)?; + gpu.platform.clone().ok_or_else(|| { + format!("No published platform is known for GPU {index} ({})", gpu.name).into() + }) +} + +pub(crate) fn desktop_cuda_ordinal(index: i32) -> Result { + let mut devices = nvml_inventory()?; + let cuda = cuda_uuids()?; + assign_visibility(&mut devices, has_visibility_mask().then_some(cuda.as_slice())); + let gpu = selected_gpu(&devices, index)?; + cuda_ordinal(gpu, &cuda).ok_or_else(|| { + format!("{} is unavailable to CUDA. Choose a visible GPU in Settings.", gpu.name).into() + }) +} + +fn has_visibility_mask() -> bool { + std::env::var_os("CUDA_VISIBLE_DEVICES").is_some() +} + +fn selected_gpu(devices: &[GpuInfo], index: i32) -> Result<&GpuInfo> { + devices + .iter() + .find(|gpu| gpu.visible_ordinal == Some(index as u32)) + .ok_or_else(|| { + format!("Serving GPU {index} is unavailable. Choose a visible GPU in Settings.").into() + }) +} + +fn cuda_ordinal(gpu: &GpuInfo, cuda: &[String]) -> Option { + cuda.iter() + .position(|uuid| uuid.eq_ignore_ascii_case(&gpu.uuid)) + .map(|ordinal| ordinal as i32) +} + +fn cuda_uuids() -> Result> { + Ok(Driver::load()? + .visible_device_uuids()? + .iter() + .map(|bytes| { + let mut uuid = String::from("GPU-"); + for (index, byte) in bytes.iter().enumerate() { + if matches!(index, 4 | 6 | 8 | 10) { + uuid.push('-'); + } + write!(uuid, "{byte:02x}").expect("writing to a String cannot fail"); + } + uuid + }) + .collect()) +} + +fn nvml_inventory() -> Result> { let nvml = Nvml::load().map_err(|error| format!("NVIDIA device information is unavailable: {error}"))?; let count = nvml @@ -158,46 +226,17 @@ pub fn inventory() -> Result> { platform, }); } - assign_visibility( - &mut devices, - std::env::var("CUDA_VISIBLE_DEVICES").ok().as_deref(), - )?; Ok(devices) } -fn assign_visibility(devices: &mut [GpuInfo], inherited: Option<&str>) -> Result<()> { - let Some(inherited) = inherited else { - for (visible, device) in devices.iter_mut().enumerate() { - device.visible_ordinal = Some(visible as u32); - } - return Ok(()); - }; - let inherited = inherited.trim(); - if inherited.is_empty() || inherited == "-1" { - return Ok(()); - } - crate::environment::visibility(&[0], Some(inherited))?; - for (visible, identifier) in inherited.split(',').map(str::trim).enumerate() { - let matches = devices - .iter() - .enumerate() - .filter_map(|(index, device)| { - let matches = identifier.parse::().map_or_else( - |_| !device.uuid.is_empty() && device.uuid.starts_with(identifier), - |ordinal| device.ordinal == ordinal, - ); - matches.then_some(index) - }) - .collect::>(); - let [index] = matches.as_slice() else { - return Err(format!("CUDA_VISIBLE_DEVICES entry {identifier:?} does not identify one detected NVIDIA GPU").into()); +fn assign_visibility(devices: &mut [GpuInfo], cuda: Option<&[String]>) { + for (index, gpu) in devices.iter_mut().enumerate() { + // Preserve existing desktop selections in NVML order on unmasked hosts. + gpu.visible_ordinal = match cuda { + None => Some(index as u32), + Some(visible) => cuda_ordinal(gpu, visible).map(|ordinal| ordinal as u32), }; - if devices[*index].visible_ordinal.is_some() { - return Err("CUDA_VISIBLE_DEVICES lists a GPU more than once".into()); - } - devices[*index].visible_ordinal = Some(visible as u32); } - Ok(()) } fn unique_uuid(prefix: &str, candidates: &[(String, T)]) -> Result { @@ -223,49 +262,34 @@ mod tests { use super::*; #[test] - fn gpu_inventory_visibility_preserves_indices_and_requires_unique_uuid_prefixes() { - let mut devices = vec![ + fn desktop_selection_resolves_the_same_card_when_cuda_reorders_devices() { + let mut devices = [ GpuInfo { ordinal: 0, uuid: "GPU-aaaa".into(), + platform: Some("gb202".into()), ..Default::default() }, GpuInfo { ordinal: 1, uuid: "GPU-bbbb".into(), + platform: Some("gb300".into()), ..Default::default() }, ]; - assign_visibility(&mut devices, Some("1,0")).unwrap(); - assert_eq!(devices[1].visible_ordinal, Some(0)); - assert_eq!(devices[0].visible_ordinal, Some(1)); - for device in &mut devices { - device.visible_ordinal = None; - } - assign_visibility(&mut devices, Some("GPU-bb")).unwrap(); - assert_eq!(devices[1].visible_ordinal, Some(0)); - assert_eq!(devices[0].visible_ordinal, None); - for device in &mut devices { - device.visible_ordinal = None; - } - assert!(assign_visibility(&mut devices, Some("GPU-")).is_err()); - assign_visibility(&mut devices, Some("")).unwrap(); - assert!( - devices - .iter() - .all(|device| device.visible_ordinal.is_none()) - ); - assign_visibility(&mut devices, Some("-1")).unwrap(); - assert!( - devices - .iter() - .all(|device| device.visible_ordinal.is_none()) - ); + let cuda = ["GPU-bbbb".into(), "GPU-aaaa".into()]; + assign_visibility(&mut devices, None); + let selected = selected_gpu(&devices, 1).unwrap(); + assert_eq!(selected.platform.as_deref(), Some("gb300")); + assert_eq!(cuda_ordinal(selected, &cuda), Some(0)); + let selected = selected_gpu(&devices, 0).unwrap(); + assert_eq!(selected.platform.as_deref(), Some("gb202")); + assert_eq!(cuda_ordinal(selected, &cuda), Some(1)); } #[test] - fn gpu_inventory_visibility_matches_nvml_ordinal_when_non_cuda_devices_skipped() { - let mut devices = vec![ + fn desktop_inventory_uses_cuda_visibility_when_a_mask_is_inherited() { + let mut devices = [ GpuInfo { ordinal: 1, uuid: "GPU-bbbb".into(), @@ -277,13 +301,14 @@ mod tests { ..Default::default() }, ]; - assign_visibility(&mut devices, Some("3,1")).unwrap(); - assert_eq!(devices[0].visible_ordinal, Some(1)); - assert_eq!(devices[1].visible_ordinal, Some(0)); - for device in &mut devices { - device.visible_ordinal = None; - } - assert!(assign_visibility(&mut devices, Some("0")).is_err()); + let cuda = ["GPU-dddd".into()]; + assign_visibility(&mut devices, Some(&cuda)); + assert_eq!(selected_gpu(&devices, 0).unwrap().ordinal, 3); + assert!(selected_gpu(&devices, 1).is_err()); + assert_eq!(cuda_ordinal(&devices[0], &cuda), None); + + assign_visibility(&mut devices, Some(&[])); + assert!(selected_gpu(&devices, 0).is_err()); } #[test] diff --git a/helios/src/runtime_core/driver.rs b/helios/src/runtime_core/driver.rs index edaea753c..a9cc85ecb 100644 --- a/helios/src/runtime_core/driver.rs +++ b/helios/src/runtime_core/driver.rs @@ -227,6 +227,8 @@ macro_rules! driver { driver! { init: "cuInit"(c_uint); device: "cuDeviceGet"(*mut c_int, c_int); + device_count: "cuDeviceGetCount"(*mut c_int); + device_uuid: "cuDeviceGetUuid"(*mut [u8; 16], c_int); device_total_memory: "cuDeviceTotalMem_v2"(*mut usize, c_int); device_pci_bus_id: "cuDeviceGetPCIBusId"(*mut c_char, c_int, c_int); device_attribute: "cuDeviceGetAttribute"(*mut c_int, c_int, c_int); @@ -309,6 +311,30 @@ driver! { } impl Driver { + /// Enumerate visible devices in CUDA ordinal order without creating a context. + pub fn visible_device_uuids(&self) -> Result> { + const CUDA_ERROR_NO_DEVICE: c_int = 100; + let mut count = 0; + let mut uuids = Vec::new(); + // SAFETY: These calls use live entrypoints and valid output storage. + unsafe { + let status = (self.init)(0); + if status == CUDA_ERROR_NO_DEVICE { + return Ok(uuids); + } + self.check("cuInit", status)?; + self.check("cuDeviceGetCount", (self.device_count)(&mut count))?; + for ordinal in 0..count { + let mut device = 0; + let mut uuid = [0; 16]; + self.check("cuDeviceGet", (self.device)(&mut device, ordinal))?; + self.check("cuDeviceGetUuid", (self.device_uuid)(&mut uuid, device))?; + uuids.push(uuid); + } + } + Ok(uuids) + } + pub fn check(&self, operation: &str, status: c_int) -> Result<()> { if status == 0 { return Ok(());