From cd4e1939c7054b889d2fed5a16f069c66c241185 Mon Sep 17 00:00:00 2001 From: Roman Inflianskas Date: Tue, 10 Nov 2026 14:15:53 +0200 Subject: [PATCH 1/3] fix(examine): classify AMD GPUs by device id and one name table `rocm examine` misreported several AMD integrated GPUs, which hides `fix-9-igpu-dgpu` on hybrid hosts and makes `rocm serve` warn about VRAM an APU does not have (#520, ROCMAI-534). - The Linux lspci scan now asks the `[1002:xxxx]` device id before the name, as the Windows probe already asks the PNP id. - examine's own name table and APU_KEYWORDS are gone: the scan asks the shared table in gfx_target.rs, which gains the codenames lspci prints. Krackan Point is gfx1152 there (examine said gfx1150), and Renoir and the other Vega codenames now name a target. There is no "Radeon 8045S"; the old `8045s` match was a typo for 8040S. - gfx902, gfx909 (Raven Ridge, Picasso) and gfx1037 (Mendocino) are integrated. - gfx_apu_verdict answers yes, no or "unknown target", stripping feature and generic-target suffixes once before either half. The rocminfo and hipInfo folds revise a verdict only when the target's packaging is known, so more evidence never makes the answer worse. - A target from the KFD topology or the sysfs fallback now decides the verdict of a row the PCI scan could not name, so a Raven laptop with a discrete card reports both. - A rocminfo agent's marketing name decides a still-unknown verdict only when one AMD GPU and one agent are listed: agents pair by position (#508), which is no evidence on a multi-GPU host. - On the install side, a gfx90c named by codename ranks where the same iGPU named by device id already did. The property harness in examine_gpu_proptests.rs draws Windows rows with the generic "AMD Radeon(TM) Graphics" name, so a display probe that stops reading the PNP id fails it. Signed-off-by: Roman Inflianskas --- crates/rocm-core/src/examine.rs | 7 + crates/rocm-core/src/examine/linux_gpus.rs | 683 +++++++- crates/rocm-core/src/examine/windows.rs | 48 +- crates/rocm-core/src/examine_gpu_proptests.rs | 1372 +++++++++++++++++ crates/rocm-core/src/gfx_target.rs | 125 +- .../src/host_gpu/windows_inventory.rs | 97 +- docs/architecture.md | 2 +- docs/host-probing.md | 14 +- xtask/src/ascii_output_contract.rs | 5 - 9 files changed, 2229 insertions(+), 124 deletions(-) create mode 100644 crates/rocm-core/src/examine_gpu_proptests.rs diff --git a/crates/rocm-core/src/examine.rs b/crates/rocm-core/src/examine.rs index fc339d767a..637be531b8 100644 --- a/crates/rocm-core/src/examine.rs +++ b/crates/rocm-core/src/examine.rs @@ -1040,3 +1040,10 @@ mod tests { #[cfg(test)] #[path = "examine_driver_proptests.rs"] mod driver_proptests; + +/// Property tests for how the GPU probes in `linux_gpus` and `windows` +/// enumerate and classify AMD GPUs; a child module so it can reach the folds +/// and classifiers while they stay private to `examine`. +#[cfg(test)] +#[path = "examine_gpu_proptests.rs"] +mod gpu_proptests; diff --git a/crates/rocm-core/src/examine/linux_gpus.rs b/crates/rocm-core/src/examine/linux_gpus.rs index 5cfa8220da..0dd188edf4 100644 --- a/crates/rocm-core/src/examine/linux_gpus.rs +++ b/crates/rocm-core/src/examine/linux_gpus.rs @@ -11,60 +11,57 @@ use crate::probe_exec::{MEDIUM, run, which}; use std::path::Path; use std::time::Duration; -/// Marketing-name fragments that identify an AMD APU when `rocminfo` is absent. -const APU_KEYWORDS: &[&str] = &[ - "strix halo", - "ryzen ai max", - "phoenix", - "hawk point", - "strix point", - "krackan", - "rembrandt", - "raphael", - "barcelo", - "lucienne", - "renoir", - "cezanne", -]; - -/// Best-effort `(gfx_target, is_apu)` for an AMD marketing name. +/// Best-effort `(gfx_target, is_apu)` for an AMD marketing name or `lspci` +/// codename, or `("", false)` when the name resolves no part. +/// +/// The target comes from the crate's one name table, +/// [`crate::gfx_target::gfx_target_from_amd_marketing_name`], and the packaging +/// from classifying that target. `examine` used to keep a second, smaller copy +/// of that table here, which disagreed with it about Krackan Point (gfx1150 +/// here, gfx1152 there and in the device-id table), plus a list of codename +/// fragments that answered "APU?" without ever naming a target, so a Renoir +/// iGPU came back with an empty `gfx_target`. +/// +/// A name that resolves nothing answers `false`, "not an APU", and that is a +/// deliberate default for a PCI row rather than a fact. The only consumer that +/// acts on the verdict is `check_9_igpu_dgpu_collision`, which fires on +/// `has_apu && has_discrete_amd`: +/// +/// - reporting an unidentified *discrete* card as "cannot say" would clear +/// `has_discrete_amd` beside every card the tables do not name -- every RX +/// 5000, Vega 56/64 and Radeon VII -- on exactly the hybrid host that check +/// exists for; +/// - reporting an unidentified *integrated* GPU as discrete sets +/// `has_discrete_amd` on an iGPU-only host, where the check still cannot fire +/// because `has_apu` is false. The cost is one wrong field in `rocm examine +/// --json`, and the integrated set is small and named in the table, so an +/// unresolved row is far more often a discrete card. +/// +/// `rocminfo` and `hipInfo` revise this verdict when they report a target whose +/// packaging is known ([`gfx_apu_verdict`]); where they do not, it stands. pub(super) fn classify_amd_marketing_name(name: &str) -> (String, bool) { - let mut n = name.to_lowercase(); - for deco in ["(tm)", "(r)", "(c)", "(\u{2122})"] { - n = n.replace(deco, " "); - } - let n = n.split_whitespace().collect::>().join(" "); - let contains = |needle: &str| n.contains(needle); - if contains("ryzen ai max") || contains("strix halo") { - return ("gfx1151".to_owned(), true); - } - if contains("radeon 8050s") || contains("radeon 8060s") || contains("radeon 8045s") { - return ("gfx1151".to_owned(), true); - } - if contains("radeon 880m") - || contains("radeon 890m") - || contains("strix point") - || contains("krackan") - { - return ("gfx1150".to_owned(), true); - } - if contains("radeon 780m") - || contains("radeon 760m") - || contains("radeon 740m") - || contains("phoenix") - || contains("hawk point") - { - return ("gfx1103".to_owned(), true); - } - (String::new(), APU_KEYWORDS.iter().any(|kw| n.contains(kw))) + let gfx = crate::gfx_target::gfx_target_from_amd_marketing_name(name).unwrap_or_default(); + (gfx.to_owned(), gfx_is_apu_family(gfx)) +} + +/// The packaging an AMD marketing name states, or `None` when the name resolves +/// no part. +/// +/// Unlike [`classify_amd_marketing_name`], this does not default an +/// unresolved name to "not an APU": it is for a GPU that has no PCI row, where +/// that default would be read as "a discrete GPU" with nothing behind it. +fn marketing_name_apu_verdict(name: &str) -> Option { + let (target, is_apu) = classify_amd_marketing_name(name); + (!target.is_empty()).then_some(is_apu) } /// Whether a gfx target belongs to an AMD APU family. /// -/// APUs: gfx90c (Vega iGPU in Renoir / Lucienne / Cezanne / Barcelo), the -/// RDNA2 iGPUs gfx1033 / gfx1035 / gfx1036 (Van Gogh, Rembrandt / Yellow Carp, -/// Raphael), gfx1103 (Phoenix / Hawk Point) and the gfx115x parts (Strix Point / -/// Strix Halo). +/// APUs: the Vega iGPUs gfx902 / gfx909 (Raven Ridge, Picasso / Raven2) and +/// gfx90c (Renoir / Lucienne / Cezanne / Barcelo), the RDNA2 iGPUs gfx1033 / +/// gfx1035 / gfx1036 / gfx1037 (Van Gogh, Rembrandt / Yellow Carp, Raphael, +/// Mendocino), gfx1103 (Phoenix / Hawk Point) and the gfx115x parts (Strix Point +/// / Strix Halo / Krackan Point). /// /// Their neighbours on both prefixes are *discrete* parts and must not match: /// gfx1100 / gfx1101 / gfx1102 (Navi 31 / 32 / 33) on gfx110x, and gfx1030 / @@ -94,10 +91,17 @@ pub(super) fn classify_amd_marketing_name(name: &str) -> (String, bool) { /// GPU on the host is integrated. pub fn gfx_is_apu_family(gfx: &str) -> bool { let g = gfx.to_lowercase(); - // gfx90c: the Vega iGPU in Renoir / Lucienne / Cezanne / Barcelo. Compared - // whole (up to a feature suffix) so other gfx90x data-center parts such as - // gfx90a stay discrete. - if g == "gfx90c" || g.starts_with("gfx90c:") { + // The Vega iGPUs: gfx90c in Renoir / Lucienne / Cezanne / Barcelo, and + // gfx902 / gfx909 in Raven Ridge and Picasso / Raven2. Compared whole (up to + // a feature or generic-target suffix, both of which follow the base + // target) so the gfx90x data-center parts beside them -- gfx900, + // gfx906, gfx908, gfx90a -- stay discrete. No lookup table produces gfx902 + // or gfx909: they reach the CLI from KFD's packed `gfx_target_version` + // (90002, 90009), through `gfx_target_from_gc_version`. + if matches!( + g.split([':', '-']).next(), + Some("gfx90c" | "gfx902" | "gfx909") + ) { return true; } // gfx115x: every Strix part is an APU. @@ -110,11 +114,12 @@ pub fn gfx_is_apu_family(gfx: &str) -> bool { } // gfx103x: non-contiguous, unlike gfx110x's threshold. gfx1033/1035/1036 // are the RDNA2 APUs (Van Gogh variant aside, gfx_target's own PCI-id table - // maps them as such); gfx1030/1031/1032/1034 are Navi 21-24 desktop/mobile - // discrete cards (RX 6000-series, RX 6500 XT). A plain threshold would get - // this family wrong in both directions. + // maps them as such), and gfx1037 is Mendocino (Ryzen 7020), which KFD and + // DRM ip-discovery report as GC 10.3.7; gfx1030/1031/1032/1034 are Navi + // 21-24 desktop/mobile discrete cards (RX 6000-series, RX 6500 XT). A plain + // threshold would get this family wrong in both directions. if let Some(digit) = gfx_model_digit(&g, "gfx103") { - return matches!(digit, 3 | 5 | 6); + return matches!(digit, 3 | 5 | 6 | 7); } false } @@ -127,6 +132,61 @@ fn gfx_model_digit(gfx: &str, prefix: &str) -> Option { gfx.strip_prefix(prefix)?.chars().next()?.to_digit(10) } +/// Discrete gfx targets: every one a card or accelerator with memory of its own. +/// +/// This is the "no" half of [`gfx_apu_verdict`], which needs one because +/// [`gfx_is_apu_family`] answers `false` both for a discrete part and for a +/// target nothing here has heard of. It lists the discrete targets the lookup +/// tables can produce plus the ones the CLI installs for; +/// `every_target_a_lookup_table_produces_has_an_apu_verdict` fails when a table +/// gains a target that neither this list nor [`gfx_is_apu_family`] classifies. +/// +/// gfx942 covers both the MI300X accelerator and the MI300A, which is an APU +/// with unified HBM. It is listed discrete because MI300X is what ROCm hosts +/// run and what the CLI has always answered; telling the two apart needs a +/// signal the target does not carry. gfx940 and gfx941, the earlier MI300 +/// targets that `normalize_therock_family` folds into the same `gfx94X-dcgpu` +/// family, are listed for the same reason. +const DISCRETE_GFX_TARGETS: &[&str] = &[ + // GCN / CDNA. + "gfx900", "gfx906", "gfx908", "gfx90a", "gfx940", "gfx941", "gfx942", "gfx950", + // RDNA1: Navi 10 / 12 / 14. + "gfx1010", "gfx1011", "gfx1012", // RDNA2: Navi 21 / 22 / 23 / 24. + "gfx1030", "gfx1031", "gfx1032", "gfx1034", // RDNA3: Navi 31 / 32 / 33. + "gfx1100", "gfx1101", "gfx1102", // RDNA4: Navi 44 / 48. + "gfx1200", "gfx1201", // The gfx125X-dcgpu family. + "gfx1250", +]; + +/// Whether a gfx target is an APU, or `None` when this crate does not know. +/// +/// The three-way answer is the point. A probe that folds a target into a +/// verdict another probe already reached needs to tell "this is a discrete +/// card" from "never heard of this target": the second is an absence of +/// evidence, and must leave the earlier verdict standing rather than replace +/// it with [`gfx_is_apu_family`]'s default `false`. Reading `rocminfo` or +/// `hipInfo` must not make the report worse than not reading it. +/// +/// A feature suffix (`gfx1036:xnack-`), a generic-target suffix +/// (`gfx1036-generic`) and case do not change the answer. +/// +/// Both halves read the base target, so the suffixes are stripped once, here, +/// rather than by each half in its own way. +pub(super) fn gfx_apu_verdict(gfx: &str) -> Option { + let base: String = gfx + .trim() + .to_ascii_lowercase() + .chars() + .take_while(char::is_ascii_alphanumeric) + .collect(); + if gfx_is_apu_family(&base) { + return Some(true); + } + DISCRETE_GFX_TARGETS + .contains(&base.as_str()) + .then_some(false) +} + /// Whether an `lspci -nn` line describes a GPU this probe should enumerate. /// /// Instinct parts report PCI class `1200` ("Processing accelerators"), not a @@ -137,7 +197,7 @@ fn gfx_model_digit(gfx: &str, prefix: &str) -> Option { /// `rocminfo` or the sysfs fallback — and came back false when neither was /// reachable. Verified on an 8-GPU MI300X host, where all eight devices read /// `class=0x120000` in sysfs (EAI-8449). -fn is_lspci_gpu_line(line: &str) -> bool { +pub(super) fn is_lspci_gpu_line(line: &str) -> bool { line.contains("VGA compatible controller") || line.contains("3D controller") || line.contains("Display controller") @@ -156,6 +216,17 @@ pub(super) fn probe_gpus_lspci(e: &mut Examination) { .push("lspci returned non-zero; PCI enumeration incomplete".to_owned()); return; } + apply_lspci_gpus(e, &out); +} + +/// Fold an `lspci -nn -D` listing into the GPU list, against caller-supplied +/// output. +/// +/// Split from the process launch for the same reason +/// [`apply_rocminfo_gpu_agents`] is: what matters is how a row is classified, +/// and a test that had to run the real `lspci` could only assert it on the +/// hardware it happened to run on. +pub(super) fn apply_lspci_gpus(e: &mut Examination, out: &str) { for line in out.lines() { if !is_lspci_gpu_line(line) { continue; @@ -184,7 +255,17 @@ pub(super) fn probe_gpus_lspci(e: &mut Examination) { if !is_amd { continue; } - let (gfx_guess, is_apu_guess) = classify_amd_marketing_name(&name); + // `-nn` prints the PCI device id on the line, so ask it first, for the + // reason the Windows probe asks the PNP id first: the id names the + // ASIC, the marketing string only the brand. It matters most where the + // name says nothing at all -- for an id `pci.ids` does not know, `lspci` + // prints the bare word "Device". + let (gfx_guess, is_apu_guess) = match extract_lspci_device_id(line) + .and_then(|device_id| crate::gfx_target::gfx_target_from_amd_pci_device_id(&device_id)) + { + Some(target) => (target.to_owned(), gfx_is_apu_family(target)), + None => classify_amd_marketing_name(&name), + }; e.gpus.push(Gpu { name, gfx_target: gfx_guess, @@ -195,9 +276,27 @@ pub(super) fn probe_gpus_lspci(e: &mut Examination) { } } +/// The AMD PCI device id an `lspci -nn` line carries: the four hex digits after +/// `1002:` in its `[vendor:device]` tag. +/// +/// Only AMD's vendor id is accepted. The tag is what tells a device id apart +/// from the other bracketed numbers on the line -- the PCI class (`[0300]`) and +/// any subsystem text `pci.ids` supplies -- and a number lifted from one of +/// those would decode to an unrelated part. +pub(super) fn extract_lspci_device_id(line: &str) -> Option { + let lower = line.to_ascii_lowercase(); + let start = lower.rfind("[1002:")? + "[1002:".len(); + let device_id: String = lower[start..] + .chars() + .take_while(char::is_ascii_hexdigit) + .take(5) + .collect(); + (device_id.len() == 4 && lower[start + 4..].starts_with(']')).then_some(device_id) +} + /// Pull the marketing name out of an `lspci -nn` line: the text between the /// controller-kind `]:` and the trailing `[vendor:device]`. -fn extract_lspci_name(line: &str) -> String { +pub(super) fn extract_lspci_name(line: &str) -> String { let after_colon = match line.find("]:") { Some(idx) => &line[idx + 2..], None => match line.find(':') { @@ -264,7 +363,7 @@ fn probe_gpus_rocminfo(e: &mut Examination) { /// behaviour here is how an agent is matched onto an existing entry, and a test /// that had to run the real `rocminfo` could only assert it on a host with a /// GPU. See `a_rocminfo_marketing_name_outranks_the_kernel_topology_placeholder`. -fn apply_rocminfo_gpu_agents(e: &mut Examination, out: &str) { +pub(super) fn apply_rocminfo_gpu_agents(e: &mut Examination, out: &str) { let mut gfx_targets: Vec<(String, String)> = Vec::new(); let mut cur_name = String::new(); let mut cur_marketing = String::new(); @@ -300,16 +399,48 @@ fn apply_rocminfo_gpu_agents(e: &mut Examination, out: &str) { .filter(|(_, g)| g.is_amd) .map(|(idx, _)| idx) .collect(); + let pairing_is_unambiguous = amd_indices.len() == 1 && gfx_targets.len() == 1; for (idx, (gfx, marketing)) in gfx_targets.into_iter().enumerate() { if let Some(&gpu_idx) = amd_indices.get(idx) { let gpu = &mut e.gpus[gpu_idx]; gpu.gfx_target = gfx.clone(); + let named_is_apu = marketing_name_apu_verdict(&marketing); if !marketing.is_empty() && gpu_name_is_unknown(&gpu.name) { gpu.name = marketing; } - gpu.is_apu = Some(gfx_is_apu_family(&gfx)); + // The target names the silicon, so where its packaging is known it + // outranks the PCI scan's guess from a marketing string. A target + // nothing here classifies is not evidence of a discrete GPU, so it + // leaves the PCI scan's verdict standing: before this, an agent + // reporting an unlisted target turned an APU the PCI scan had + // identified into a discrete card, clearing `has_apu` and with it + // the iGPU+dGPU collision diagnosis. (Latent while #393 makes the + // parser drop every agent on a real host; live the day it is fixed.) + // + // A row that still has no verdict at all -- a kernel-topology + // placeholder whose target nothing classifies, in a container + // with no `pciutils` -- takes the agent's marketing name when that + // resolves a part, exactly as an unpaired agent does below, but + // only when the pairing cannot be wrong: one AMD row and one + // agent. The agent carries no identifier this parser reads, so on + // a multi-GPU host the pairing is by position (#508), and a + // position is not evidence about which card a name belongs to; the + // verdict stays unknown there rather than becoming a guess. + if let Some(is_apu) = gfx_apu_verdict(&gfx) { + gpu.is_apu = Some(is_apu); + } else if gpu.is_apu.is_none() && pairing_is_unambiguous { + gpu.is_apu = named_is_apu; + } } else { - let is_apu = gfx_is_apu_family(&gfx); + // An agent with no PCI row and no kernel-topology node to pair + // with: KFD unreadable, and no `pciutils`. Same precedence, over the + // evidence there is: the target when its packaging is known, else + // the agent's own marketing name when that resolves a part. When + // neither answers, `is_apu` stays unset rather than defaulting to + // `false`, which `summarise_gpu_categories` would read as "a + // discrete GPU" -- the conclusion `probe_gpus_sysfs_fallback` + // reaches for the same reason. + let is_apu = gfx_apu_verdict(&gfx).or_else(|| marketing_name_apu_verdict(&marketing)); e.gpus.push(Gpu { name: if marketing.is_empty() { "AMD GPU".to_owned() @@ -318,7 +449,7 @@ fn apply_rocminfo_gpu_agents(e: &mut Examination, out: &str) { }, gfx_target: gfx, is_amd: true, - is_apu: Some(is_apu), + is_apu, ..Gpu::default() }); } @@ -360,15 +491,17 @@ fn probe_gpus_sysfs_fallback(e: &mut Examination, gfx_target: fn() -> Option Option { /// The KFD topology nodes directory the membership pass reconciles against. - kfd_nodes: &'a Path, + pub(super) kfd_nodes: &'a Path, /// A `rocminfo` reading to fold in, or `None` to run `rocminfo` here. /// /// `None` is what the host uses; `Some` exists because a test cannot make a /// machine with no GPU produce an agent listing, and the placeholder's fate /// turns entirely on whether one arrived. - rocminfo: Option<&'a str>, + pub(super) rocminfo: Option<&'a str>, /// How the sysfs fallback learns the gfx target when nothing else named a /// card. See [`probe_gpus_sysfs_fallback`] for why it is a function. - sysfs_gfx_target: fn() -> Option, + pub(super) sysfs_gfx_target: fn() -> Option, } impl<'a> GpuProbeSources<'a> { @@ -562,10 +695,10 @@ fn apply_kernel_gpu_membership(e: &mut Examination, nodes: &[crate::KfdGpuNode]) gfx_target: node.gfx_target.clone(), pci_id: node.pci_id.clone(), is_amd: true, - // Left unset rather than guessed, for the same reason - // `probe_gpus_sysfs_fallback` leaves it unset: KFD gives the - // target, not the packaging. - is_apu: None, + // The target's own verdict where its packaging is known, and + // unset rather than guessed where it is not -- the same reading + // `probe_gpus_sysfs_fallback` takes of the same KFD target. + is_apu: gfx_apu_verdict(&node.gfx_target), }); continue; }; @@ -578,6 +711,17 @@ fn apply_kernel_gpu_membership(e: &mut Examination, nodes: &[crate::KfdGpuNode]) // there is nothing to compare it against. if gpu.gfx_target.is_empty() { gpu.gfx_target = node.gfx_target.clone(); + // The verdict follows the target it now carries. A PCI row that + // resolved no target holds the scan's "not an APU" default (see + // `classify_amd_marketing_name`), which is a guess; a target whose + // packaging is known replaces it, the same precedence the + // `rocminfo` fold applies. This is the only route by which a Raven + // Ridge or Picasso iGPU is classified on a real host: no lookup + // table names them, and KFD's packed `gfx_target_version` is what + // yields gfx902 / gfx909. An unknown target leaves the guess alone. + if let Some(is_apu) = gfx_apu_verdict(&gpu.gfx_target) { + gpu.is_apu = Some(is_apu); + } } gpus.push(gpu); } @@ -1255,15 +1399,16 @@ mod tests { #[test] fn a_topology_sourced_gpu_counts_as_present_without_claiming_a_package() { - // What the fallback produces: enough to stop reporting "no AMD GPU", - // and no more. `is_apu: None` is deliberate -- the topology says which - // target, not whether it is integrated -- so the APU and discrete - // tallies must both stay false rather than guess. + // What the fallback produces for a target nothing classifies: enough + // to stop reporting "no AMD GPU", and no more. `is_apu: None` is + // deliberate -- the topology names a target whose packaging this crate + // does not know -- so the APU and discrete tallies must both stay false + // rather than guess. let mut e = Examination { os_family: "linux".to_owned(), gpus: vec![Gpu { name: "AMD GPU (from kernel topology)".to_owned(), - gfx_target: "gfx942".to_owned(), + gfx_target: "gfx1170".to_owned(), is_amd: true, is_apu: None, ..Gpu::default() @@ -1483,6 +1628,382 @@ mod tests { assert!(e.has_discrete_amd); } + /// The Vega and RDNA2 APUs that reach the CLI only as a target synthesised + /// from a GC version -- KFD's packed `gfx_target_version` 90002, 90009 and + /// 100307 -- and that a prefix rule never reached: Raven Ridge, Picasso / + /// Raven2 and Mendocino. Their gfx90x and gfx103x neighbours stay discrete. + #[test] + fn raven_picasso_and_mendocino_are_apus() { + for apu in [ + "gfx902", + "gfx909", + "gfx1037", + "GFX902", + "gfx909:xnack-", + "gfx1037:xnack-", + ] { + assert!(gfx_is_apu_family(apu), "{apu}"); + } + for discrete in ["gfx900", "gfx906", "gfx908", "gfx90a", "gfx1034", "gfx1030"] { + assert!(!gfx_is_apu_family(discrete), "{discrete}"); + } + } + + /// "Not an APU" and "never heard of it" are different answers, and the + /// folds that revise a verdict need to tell them apart. + #[test] + fn an_unclassified_target_has_no_apu_verdict() { + assert_eq!(gfx_apu_verdict("gfx1036"), Some(true)); + assert_eq!(gfx_apu_verdict("gfx1036-generic"), Some(true)); + assert_eq!(gfx_apu_verdict("gfx1100"), Some(false)); + assert_eq!(gfx_apu_verdict("GFX942:sramecc+:xnack-"), Some(false)); + // The earlier MI300 targets, in the same TheRock family as gfx942. + assert_eq!(gfx_apu_verdict("gfx940"), Some(false)); + assert_eq!(gfx_apu_verdict("gfx941"), Some(false)); + assert_eq!(gfx_apu_verdict("gfx1100-generic"), Some(false)); + // The Vega iGPUs are matched whole rather than by a prefix digit, so + // the suffix has to come off before that match, not only before the + // discrete list. + assert_eq!(gfx_apu_verdict("gfx90c-generic"), Some(true)); + assert_eq!(gfx_apu_verdict("gfx902-generic"), Some(true)); + assert_eq!(gfx_apu_verdict("GFX909-generic:xnack-"), Some(true)); + assert!(gfx_is_apu_family("gfx90c-generic")); + assert_eq!(gfx_apu_verdict("gfx1170"), None); + assert_eq!(gfx_apu_verdict(""), None); + } + + /// The `lspci -nn` device id comes from AMD's vendor tag and nowhere else. + #[test] + fn the_lspci_device_id_is_read_from_the_amd_vendor_tag() { + assert_eq!( + extract_lspci_device_id( + "0000:c5:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. \ + [AMD/ATI] Krackan Point [Radeon 860M] [1002:1114] (rev c1)" + ) + .as_deref(), + Some("1114") + ); + // The class `[0300]` is a four-digit number in brackets too, and an + // NVIDIA line carries no AMD tag at all. + assert_eq!( + extract_lspci_device_id( + "0000:01:00.0 VGA compatible controller [0300]: NVIDIA Corporation \ + GA102 [GeForce RTX 3090] [10de:2204] (rev a1)" + ), + None + ); + // A tag that is not exactly four hex digits is not a device id. + assert_eq!(extract_lspci_device_id("Device [1002:11141]"), None); + assert_eq!(extract_lspci_device_id("Device [1002:111]"), None); + assert_eq!( + extract_lspci_device_id("no bracketed vendor tag here"), + None + ); + // Four hex digits followed by anything but the closing bracket is not + // a tag either. + assert_eq!(extract_lspci_device_id("Device [1002:1114g]"), None); + // The tag is the last `[1002:` on the line: a name that carries an + // earlier one does not decide the device. + assert_eq!( + extract_lspci_device_id( + "0000:c5:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. \ + [AMD/ATI] Device [1002:73a0] rebadged [1002:1114] (rev c1)" + ) + .as_deref(), + Some("1114") + ); + } + + /// `examine` names a GPU from the crate's one name table, codenames + /// included, so `lspci`'s codename for an iGPU yields its target rather + /// than an "APU, target unknown" guess. + /// + /// Krackan Point is the case that used to be wrong rather than missing: + /// `examine`'s own table called it gfx1150, while the device-id table and + /// the 860M/840M it sells as say gfx1152, a different TheRock family. + #[test] + fn examine_names_a_gpu_by_its_codename_from_the_shared_table() { + for (name, target) in [ + ( + "Advanced Micro Devices, Inc. [AMD/ATI] Krackan Point", + "gfx1152", + ), + ("Advanced Micro Devices, Inc. [AMD/ATI] Renoir", "gfx90c"), + ( + "Advanced Micro Devices, Inc. [AMD/ATI] Cezanne [Radeon Vega Series]", + "gfx90c", + ), + ("Advanced Micro Devices, Inc. [AMD/ATI] Raphael", "gfx1036"), + ( + "Advanced Micro Devices, Inc. [AMD/ATI] Mendocino", + "gfx1037", + ), + ("Advanced Micro Devices, Inc. [AMD/ATI] Phoenix1", "gfx1103"), + ( + "Advanced Micro Devices, Inc. [AMD/ATI] VanGogh [AMD Custom GPU 0405]", + "gfx1033", + ), + ("AMD Radeon(TM) 8040S Graphics", "gfx1151"), + ] { + assert_eq!( + classify_amd_marketing_name(name), + (target.to_owned(), true), + "{name}" + ); + } + // Discrete names come from the same table, so they now carry a target. + assert_eq!( + classify_amd_marketing_name("AMD Radeon RX 7900 XTX"), + ("gfx1100".to_owned(), false) + ); + } + + /// Every codename and product-line row this change added to the shared + /// name table, each through a name that carries that one pattern and no + /// other, so deleting or retargeting any single row fails here. + /// + /// The `lspci` corpora cannot do this: their rows carry device ids the + /// id table answers first, so a codename row behind them is never asked. + /// The Phoenix spellings are `pci.ids`' names for device ids 15bf, 15c8, + /// 1900 and 1901. + #[test] + fn each_codename_row_names_its_own_target() { + for (name, target) in [ + ("[AMD/ATI] Strix Halo", "gfx1151"), + ("AMD Ryzen AI Max", "gfx1151"), + ("[AMD/ATI] Strix Point", "gfx1150"), + ("[AMD/ATI] Krackan", "gfx1152"), + ("[AMD/ATI] Phoenix", "gfx1103"), + ("[AMD/ATI] Phoenix1", "gfx1103"), + ("[AMD/ATI] Phoenix2", "gfx1103"), + ("[AMD/ATI] Phoenix3", "gfx1103"), + ("[AMD/ATI] Phoenix4", "gfx1103"), + ("[AMD/ATI] Hawk Point", "gfx1103"), + ("[AMD/ATI] Rembrandt", "gfx1035"), + ("[AMD/ATI] Mendocino", "gfx1037"), + ("[AMD/ATI] Raphael", "gfx1036"), + ("[AMD/ATI] VanGogh", "gfx1033"), + ("AMD Custom GPU 0405", "gfx1033"), + ("[AMD/ATI] Renoir", "gfx90c"), + ("[AMD/ATI] Cezanne", "gfx90c"), + ("[AMD/ATI] Lucienne", "gfx90c"), + ("[AMD/ATI] Barcelo", "gfx90c"), + ] { + assert_eq!( + classify_amd_marketing_name(name), + (target.to_owned(), true), + "{name}" + ); + } + } + + /// The Linux PCI scan prefers the device id to the name, the way the + /// Windows probe prefers the PNP id. + /// + /// The two inputs disagree on purpose: device id `73a0` is Navi 21 + /// (gfx1030, discrete) while the name is a Strix Halo SKU. No such part + /// ships, which is why it is the input -- only a pair that cannot both be + /// right pins precedence rather than agreement. + #[test] + fn lspci_scan_prefers_the_device_id_to_the_name() { + let mut e = Examination::default(); + apply_lspci_gpus( + &mut e, + "0000:03:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. \ + [AMD/ATI] Radeon 8060S [1002:73a0] (rev c1)", + ); + assert_eq!(e.gpus.len(), 1, "{:?}", e.gpus); + assert_eq!(e.gpus[0].gfx_target, "gfx1030", "{:?}", e.gpus); + assert_eq!(e.gpus[0].is_apu, Some(false), "{:?}", e.gpus); + } + + /// A Raven Ridge laptop with a discrete card, as a real host reads it. + /// + /// No lookup table names Raven: its PCI row resolves no target and so + /// carries the scan's "not an APU" default. The gfx902 target arrives only + /// from KFD's topology, so the kernel-membership pass is where the verdict + /// must follow it -- `rocminfo`, the other route, drops every agent on a + /// real host today (#393). Without that, the host reports no APU and the + /// `fix-9-igpu-dgpu` gate stays shut on the shape it exists for. + #[test] + fn a_kernel_topology_target_decides_the_verdict_of_a_row_the_pci_scan_could_not_name() { + let mut e = Examination::default(); + apply_lspci_gpus( + &mut e, + "0000:03:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. \ + [AMD/ATI] Raven Ridge [Radeon Vega Series / Radeon Vega Mobile Series] [1002:15dd] (rev c4)\n\ + 0000:01:00.0 Display controller [0380]: Advanced Micro Devices, Inc. \ + [AMD/ATI] Device [1002:7480] (rev c7)", + ); + assert_eq!(e.gpus.len(), 2, "{:?}", e.gpus); + assert_eq!( + (e.gpus[0].gfx_target.as_str(), e.gpus[0].is_apu), + ("", Some(false)), + "the premise: nothing names Raven from its PCI row: {:?}", + e.gpus[0] + ); + + apply_kernel_gpu_membership( + &mut e, + &[ + crate::KfdGpuNode { + pci_id: "0000:01:00.0".to_owned(), + gfx_target: "gfx1102".to_owned(), + }, + crate::KfdGpuNode { + pci_id: "0000:03:00.0".to_owned(), + gfx_target: "gfx902".to_owned(), + }, + ], + ); + let raven = e.gpus.iter().find(|g| g.pci_id == "0000:03:00.0").unwrap(); + assert_eq!(raven.gfx_target, "gfx902", "{raven:?}"); + assert_eq!(raven.is_apu, Some(true), "{raven:?}"); + let dgpu = e.gpus.iter().find(|g| g.pci_id == "0000:01:00.0").unwrap(); + assert_eq!(dgpu.is_apu, Some(false), "{dgpu:?}"); + + summarise_gpu_categories(&mut e); + assert!( + e.has_apu && e.has_discrete_amd, + "both gate fix-9-igpu-dgpu: {:?}", + e.gpus + ); + } + + /// The kernel-membership pass revises only a verdict that came with no + /// target. Where the PCI scan resolved a target, that target and its + /// verdict stand; and a KFD target nothing classifies leaves the scan's + /// verdict alone. + #[test] + fn a_kernel_topology_target_never_revises_a_row_that_already_had_one() { + let row = |gfx_target: &str, is_apu| Gpu { + name: "Advanced Micro Devices, Inc. [AMD/ATI] Device".to_owned(), + gfx_target: gfx_target.to_owned(), + pci_id: "0000:03:00.0".to_owned(), + is_amd: true, + is_apu, + }; + for (before, node_target, expected) in [ + // Resolved by the scan: the node's target does not replace it. + ( + row("gfx1035", Some(true)), + "gfx1100", + ("gfx1035", Some(true)), + ), + // Unresolved, and the node's target is one nothing classifies. + (row("", Some(false)), "gfx1170", ("gfx1170", Some(false))), + ] { + let mut e = Examination { + gpus: vec![before], + ..Examination::default() + }; + apply_kernel_gpu_membership( + &mut e, + &[crate::KfdGpuNode { + pci_id: "0000:03:00.0".to_owned(), + gfx_target: node_target.to_owned(), + }], + ); + assert_eq!( + (e.gpus[0].gfx_target.as_str(), e.gpus[0].is_apu), + expected, + "node target {node_target}: {:?}", + e.gpus[0] + ); + } + } + + /// A GPU known only from the kernel topology -- no `lspci`, as in an + /// ordinary ROCm container -- is classified from its target where the + /// target's packaging is known. Where it is not, the `rocminfo` agent's + /// marketing name decides only when the agent cannot belong to another + /// card; on a multi-GPU host the agents pair by position, which is no + /// evidence, so the verdict stays unknown. + #[test] + fn a_kernel_topology_gpu_is_classified_from_the_evidence_there_is() { + let node = |pci_id: &str, gfx_target: &str| crate::KfdGpuNode { + pci_id: pci_id.to_owned(), + gfx_target: gfx_target.to_owned(), + }; + let agent = |gfx: &str, marketing: &str| { + format!( + "*******\nAgent 1\n*******\n Name: {gfx}\n \ + Marketing Name: {marketing}\n \ + Device Type: GPU\n" + ) + }; + let mut e = Examination::default(); + apply_kernel_gpu_membership( + &mut e, + &[ + node("0000:03:00.0", "gfx1151"), + node("0000:04:00.0", "gfx942"), + node("0000:05:00.0", "gfx1170"), + node("0000:06:00.0", "gfx1170"), + ], + ); + let verdicts: Vec<_> = e.gpus.iter().map(|g| g.is_apu).collect(); + assert_eq!( + verdicts, + [Some(true), Some(false), None, None], + "{:?}", + e.gpus + ); + + // `rocminfo` then pairs one agent per row, by position. The third + // agent's marketing name resolves an APU, but nothing ties that agent + // to the third card rather than the fourth, so neither unknown target + // takes a verdict from a name. + let out = [ + agent("gfx1151", "AMD Radeon(TM) 8060S Graphics"), + agent("gfx942", "AMD Instinct MI300X"), + agent("gfx1170", "AMD Radeon(TM) 8060S Graphics"), + agent("gfx1170", "AMD Radeon Graphics"), + ] + .concat(); + apply_rocminfo_gpu_agents(&mut e, &out); + let verdicts: Vec<_> = e.gpus.iter().map(|g| g.is_apu).collect(); + assert_eq!( + verdicts, + [Some(true), Some(false), None, None], + "{:?}", + e.gpus + ); + + // One card and one agent: the pairing cannot be wrong, so the name + // decides, and a name that resolves nothing still leaves it unset. + for (marketing, expected) in [ + ("AMD Radeon(TM) 8060S Graphics", Some(true)), + ("AMD Radeon RX 7900 XTX", Some(false)), + ("AMD Radeon Graphics", None), + ] { + let mut e = Examination::default(); + apply_kernel_gpu_membership(&mut e, &[node("0000:05:00.0", "gfx1170")]); + apply_rocminfo_gpu_agents(&mut e, &agent("gfx1170", marketing)); + assert_eq!(e.gpus.len(), 1, "{:?}", e.gpus); + assert_eq!(e.gpus[0].is_apu, expected, "{marketing}: {:?}", e.gpus[0]); + } + } + + #[test] + fn a_sysfs_fallback_gpu_carries_its_targets_verdict_where_one_is_known() { + for (target, expected) in [ + ("gfx1151", Some(true)), + ("gfx942", Some(false)), + ("gfx1170", None), + ] { + let mut e = Examination::default(); + let probe: fn() -> Option = match target { + "gfx1151" => || Some("gfx1151".to_owned()), + "gfx942" => || Some("gfx942".to_owned()), + _ => || Some("gfx1170".to_owned()), + }; + probe_gpus_sysfs_fallback(&mut e, probe); + assert_eq!(e.gpus.len(), 1, "{:?}", e.gpus); + assert_eq!(e.gpus[0].is_apu, expected, "{target}: {:?}", e.gpus[0]); + } + } + #[test] fn marketing_name_maps_strix_halo() { assert_eq!( diff --git a/crates/rocm-core/src/examine/windows.rs b/crates/rocm-core/src/examine/windows.rs index 0050227dd8..f4d9e9e7a2 100644 --- a/crates/rocm-core/src/examine/windows.rs +++ b/crates/rocm-core/src/examine/windows.rs @@ -5,7 +5,7 @@ //! The native Windows probes: the GPU query, the HIP SDK, the Adrenalin driver and //! the MSVC redistributable. -use super::linux_gpus::{classify_amd_marketing_name, gfx_is_apu_family}; +use super::linux_gpus::{classify_amd_marketing_name, gfx_apu_verdict, gfx_is_apu_family}; use super::{Examination, Gpu}; use crate::probe_exec::{MEDIUM, run}; use std::path::Path; @@ -30,7 +30,7 @@ pub(super) fn probe_gpus_windows(e: &mut Examination) { /// Split out from [`probe_gpus_windows`] so the parsing can be driven with /// synthetic `Win32_VideoController` output -- there is no Windows host to run /// the real probe against in this test suite. -fn parse_gpus_windows_output(out: &str, e: &mut Examination) { +pub(super) fn parse_gpus_windows_output(out: &str, e: &mut Examination) { for line in out.lines() { let line = line.trim(); if line.is_empty() { @@ -79,8 +79,7 @@ fn parse_gpus_windows_output(out: &str, e: &mut Examination) { // the brand. let (gfx_target, is_apu) = match crate::host_gpu::gfx_target_from_pnp_device_id(&pnp) { // `gfx_is_apu_family` knows the answer whenever the id resolved a - // target; the name heuristic's `is_apu` guess is only consulted - // when nothing identified the part. + // target; the name table is only consulted when the id did not. Some(target) => (target.to_owned(), gfx_is_apu_family(target)), None => classify_amd_marketing_name(&name), }; @@ -154,18 +153,18 @@ pub(super) fn probe_hip_sdk_windows(e: &mut Examination) { /// prints a block for a card the PCI-id table resolved, but that card is no /// longer an empty slot, so a naive "first empty slot" walk hands *its* /// architecture to the next card along. The concrete case: a Radeon 680M -/// (resolved to `gfx1035` from its device id) beside an RX 6800M (in neither -/// table). `hipInfo` prints gfx1035 then gfx1031; the first line finds the 6800M -/// as the only empty slot and makes it a `gfx1035` APU. That inverts both -/// `has_apu` and `has_discrete_amd`, which gate `fix-9-igpu-dgpu`, and the -/// second line is then dropped — the discrete card ends up described as the -/// integrated one. +/// (resolved to `gfx1035` from its device id) beside a discrete RX 6800M whose +/// row neither table resolved. `hipInfo` prints gfx1035 then gfx1031; the first +/// line finds the 6800M as the only empty slot and makes it a `gfx1035` APU. +/// That inverts both `has_apu` and `has_discrete_amd`, which gate +/// `fix-9-igpu-dgpu`, and the second line is then dropped — the discrete card +/// ends up described as the integrated one. /// /// So an architecture already held by an identified GPU is consumed rather than /// assigned: one line per holder, by count, not by presence. Counting matters — /// two identical cards print the same name twice, and dropping every repeat /// would leave the second one unidentified forever. -fn fill_unidentified_gpus_from_hipinfo(gpus: &mut [Gpu], hipinfo_stdout: &str) { +pub(super) fn fill_unidentified_gpus_from_hipinfo(gpus: &mut [Gpu], hipinfo_stdout: &str) { // What the earlier, better probes already answered. Each of these has a // `gcnArchName` block of its own coming, which belongs to it. let mut already_identified: Vec = gpus @@ -191,7 +190,16 @@ fn fill_unidentified_gpus_from_hipinfo(gpus: &mut [Gpu], hipinfo_stdout: &str) { else { continue; }; - gpu.is_apu = Some(gfx_is_apu_family(&gfx)); + // Same rule as `apply_rocminfo_gpu_agents`: a target whose packaging is + // known outranks the display probe's verdict, an unknown one leaves it. + // Today the two branches cannot disagree here: a row reaches this loop + // only with an empty target, and the Windows probe gives every such row + // the "not an APU" default, so an unknown target leaves exactly what an + // overwrite with `gfx_is_apu_family`'s `false` would. The rule is kept + // so the hipInfo fold cannot quietly drift from the rocminfo one. + if let Some(is_apu) = gfx_apu_verdict(&gfx) { + gpu.is_apu = Some(is_apu); + } gpu.gfx_target = gfx; } } @@ -320,7 +328,7 @@ mod tests { // ROCMAI-546: the real host this ticket was filed against. Windows // reports Renoir/Cezanne's Vega iGPU under the generic marketing name // "AMD Radeon(TM) Graphics", which `classify_amd_marketing_name` does - // not recognize (it only maps Phoenix/Strix-and-newer names) -- so + // not resolve (the generic name names no part) -- so // without the PCI-id lookup this host's `gfx_target` stays empty and // `info-20-gpu-no-rocm-support` can never fire on it. let out = "AMD Radeon(TM) Graphics\t31.0.21004.7\tPCI\\VEN_1002&DEV_1636&SUBSYS_12341002&REV_C8\\4&1a2b3c4d&0&0008"; @@ -339,7 +347,7 @@ mod tests { // Barcelo-R laptop (PNP id 164d), but `gfx_is_apu_family` used to have // no gfx103x arm at all, so a real iGPU reported `is_apu: Some(false)` // -- the opposite of what the marketing-name path ("AMD Radeon 680M") - // would have said via `APU_KEYWORDS`. That silently suppressed + // would have said. That silently suppressed // `fix-9-igpu-dgpu` detection on exactly the iGPU+dGPU host shape this // PR is otherwise improving detection for. let out = "AMD Radeon 680M\t31.0.21012.9\tPCI\\VEN_1002&DEV_164D&SUBSYS_14F11002&REV_C8\\3&11583659&0&08"; @@ -396,8 +404,8 @@ mod tests { /// (`check_9_igpu_dgpu_collision`), and both are derived from `is_apu` by /// `summarise_gpu_categories`. A Rembrandt iGPU reported as discrete does /// not merely carry a wrong field: it turns the gate off on the one host - /// shape the check exists for. Note the dGPU's marketing name carries no - /// APU keyword either way, so nothing here rests on `APU_KEYWORDS`. + /// shape the check exists for. Both rows resolve by device id here, so + /// nothing in this test rests on the marketing-name table. #[test] fn a_rembrandt_laptop_with_a_discrete_card_opens_the_igpu_dgpu_gate() { let out = "AMD Radeon 680M\t31.0.21012.9\tPCI\\VEN_1002&DEV_164D&SUBSYS_14F11002&REV_C8\n\ @@ -428,7 +436,7 @@ mod tests { /// identify. /// /// Concretely: a Radeon 680M resolved to gfx1035 from its device id, beside - /// an RX 6800M that is in neither table. `hipInfo` prints gfx1035 then + /// an RX 6800M the probe left unidentified. `hipInfo` prints gfx1035 then /// gfx1031. Unguarded, the 6800M becomes a gfx1035 APU, `has_discrete_amd` /// goes false, `has_apu` is the only flag left, and `fix-9-igpu-dgpu` is /// suppressed on the one host shape it exists for. @@ -442,8 +450,10 @@ mod tests { is_apu: Some(true), ..Gpu::default() }, - // In neither table: no device-id arm, and "RX 6800M" carries no APU - // keyword, so it arrives here genuinely unidentified. + // Unidentified on arrival: the probe resolved no target for this + // row, so it carries the "not an APU" default. (The name table does + // know "RX 6800M"; the row stands for any card whose name and + // device id both miss.) Gpu { name: "AMD Radeon RX 6800M".to_owned(), is_amd: true, diff --git a/crates/rocm-core/src/examine_gpu_proptests.rs b/crates/rocm-core/src/examine_gpu_proptests.rs new file mode 100644 index 0000000000..065a35d2e0 --- /dev/null +++ b/crates/rocm-core/src/examine_gpu_proptests.rs @@ -0,0 +1,1372 @@ +// Copyright © Advanced Micro Devices, Inc., or its affiliates. +// +// SPDX-License-Identifier: MIT + +//! Property-based tests for how `examine` enumerates and classifies AMD GPUs. +//! +//! The generators are built from *real* fixture shapes -- `lspci -nn -D` lines, +//! `rocminfo` agent blocks, `Win32_VideoController` rows and AMD marketing +//! names -- and then mutated (case, whitespace, decorations, dropped brackets, +//! truncation). A uniform-random string generator never reaches the branches +//! that matter, so every strategy starts from a corpus and perturbs it. +//! +//! Each property states an invariant the *report* must hold, so a failure names +//! a defect a user would see. `generator_reach_is_measured_and_sufficient` +//! asserts that the generators actually reach the regions the properties are +//! about, so the harness cannot quietly go vacuous. + +use super::linux_gpus::{ + GpuProbeSources, apply_lspci_gpus, apply_rocminfo_gpu_agents, classify_amd_marketing_name, + extract_lspci_device_id, extract_lspci_name, gfx_apu_verdict, gfx_is_apu_family, + is_lspci_gpu_line, probe_gpus_after_lspci, summarise_gpu_categories, +}; +use super::windows::{fill_unidentified_gpus_from_hipinfo, parse_gpus_windows_output}; +use super::{Examination, Gpu}; +use crate::test_support::proptest_cases; +use proptest::prelude::*; +use proptest::strategy::ValueTree; +use proptest::test_runner::{Config, TestRunner}; + +// --------------------------------------------------------------------------- +// Corpora: real strings, from pci.ids, this repo's own fixtures and the +// hardware named in ROCm/rocm-cli#448 / #449. +// --------------------------------------------------------------------------- + +/// `(device_name_as_lspci_prints_it, pci_device_id, true_gfx_target, +/// is_really_an_apu)`. +/// +/// The names are the `pci.ids` device strings an `lspci -nn` line carries after +/// the `[AMD/ATI]` vendor tag. The last two fields are ground truth about the +/// silicon, not about what the code says, so one row is always a *coherent* +/// host: the gfx target really is the one that device reports. +const AMD_LSPCI_DEVICES: &[(&str, &str, &str, bool)] = &[ + // Discrete RDNA2 / RDNA3 / RDNA4. + ( + "Navi 31 [Radeon RX 7900 XT/7900 XTX/7900 GRE/7900M]", + "744c", + "gfx1100", + false, + ), + ( + "Navi 32 [Radeon RX 7700 XT / 7800 XT]", + "747e", + "gfx1101", + false, + ), + ( + "Navi 33 [Radeon RX 7600/7600 XT/7600M XT/7600S/7700S / PRO W7600]", + "7480", + "gfx1102", + false, + ), + ( + "Navi 21 [Radeon RX 6800/6800 XT / 6900 XT]", + "73bf", + "gfx1030", + false, + ), + ( + "Navi 22 [Radeon RX 6700 XT / 6800M / 6950 XT]", + "73df", + "gfx1031", + false, + ), + ( + "Navi 23 [Radeon RX 6600/6600 XT/6600M]", + "73ff", + "gfx1032", + false, + ), + ( + "Navi 24 [Radeon RX 6400/6500 XT/6500M]", + "743f", + "gfx1034", + false, + ), + ("Navi 48 [Radeon RX 9070/9070 XT]", "7550", "gfx1201", false), + // Datacenter. + ("Aqua Vanjaram [Instinct MI300X]", "74a1", "gfx942", false), + // APUs, oldest first. Every one is an integrated GPU sharing system + // memory; `has_apu` must be true on a host that has one. + ("Renoir", "1636", "gfx90c", true), + ("Cezanne", "1638", "gfx90c", true), + ("Lucienne", "164c", "gfx90c", true), + ("Barcelo", "15e7", "gfx90c", true), + ("VanGogh [AMD Custom GPU 0405]", "163f", "gfx1033", true), + ("Rembrandt [Radeon 680M]", "1681", "gfx1035", true), + ("Raphael", "164e", "gfx1036", true), + // No device-id row: only the codename identifies it. + ("Mendocino", "1506", "gfx1037", true), + ("Phoenix1", "15bf", "gfx1103", true), + ("Phoenix3", "1900", "gfx1103", true), + ("Strix [Radeon 880M / 890M]", "150e", "gfx1150", true), + ("Krackan Point [Radeon 860M]", "1114", "gfx1152", true), + // Strix Halo, as reported in ROCm/rocm-cli#449: pci.ids has no entry, so + // `lspci` prints the bare word "Device", and the device-id table has no + // row for 1586 either (#507). + ("Device", "1586", "gfx1151", true), +]; + +/// Corpus rows that nothing can identify: no `pci.ids` name, and an id the +/// device-id table does not map. They stay in the corpus for the parsing and +/// totality properties, and are excluded from the classification sweeps. +const UNIDENTIFIABLE_LSPCI_DEVICES: &[&str] = &["Device"]; + +/// PCI classes an `lspci -nn` line can carry: the ones the probe enumerates, +/// and last the bridge it must not. +const LSPCI_CLASSES: &[(&str, bool)] = &[ + ("VGA compatible controller [0300]", true), + ("Display controller [0380]", true), + ("3D controller [0302]", true), + ("Processing accelerators [1200]", true), + ("PCI bridge [0604]", false), +]; + +/// AMD marketing names as `rocminfo`'s `Marketing Name:` and Windows' +/// `Win32_VideoController.Name` report them, with ground truth about the part. +/// +/// `(name, true_gfx_target, is_really_an_apu, pci_device_id)`. The device id is +/// the one the part's Windows PNP id carries, where the device-id table maps +/// it; [`windows_corpus`] draws only those rows. +const AMD_MARKETING_NAMES: &[(&str, &str, bool, Option<&str>)] = &[ + ("AMD Radeon RX 7900 XTX", "gfx1100", false, None), + ("AMD Radeon RX 7800 XT", "gfx1101", false, None), + ("AMD Radeon RX 7600", "gfx1102", false, None), + ("AMD Radeon RX 9070 XT", "gfx1201", false, Some("7550")), + ("AMD Radeon PRO W7900", "gfx1100", false, None), + ("AMD Instinct MI300X", "gfx942", false, None), + ("AMD Radeon RX 6900 XT", "gfx1030", false, Some("73a0")), + ("AMD Radeon RX 6600M", "gfx1032", false, Some("73e0")), + ("AMD Radeon RX 6500M", "gfx1034", false, Some("743f")), + // APUs. + ( + "AMD Radeon(TM) 610M Graphics", + "gfx1036", + true, + Some("164e"), + ), + ( + "AMD Radeon(TM) 660M Graphics", + "gfx1035", + true, + Some("1681"), + ), + ( + "AMD Radeon(TM) 680M Graphics", + "gfx1035", + true, + Some("1681"), + ), + ( + "AMD Radeon(TM) 740M Graphics", + "gfx1103", + true, + Some("15bf"), + ), + ( + "AMD Radeon(TM) 760M Graphics", + "gfx1103", + true, + Some("15bf"), + ), + ( + "AMD Radeon(TM) 780M Graphics", + "gfx1103", + true, + Some("15bf"), + ), + ("AMD Radeon(TM) 820M Graphics", "gfx1153", true, None), + ( + "AMD Radeon(TM) 840M Graphics", + "gfx1152", + true, + Some("1114"), + ), + ( + "AMD Radeon(TM) 860M Graphics", + "gfx1152", + true, + Some("1114"), + ), + ("AMD Radeon(TM) 880M Graphics", "gfx1150", true, None), + ("AMD Radeon(TM) 890M Graphics", "gfx1150", true, None), + ("AMD Radeon 8040S Graphics", "gfx1151", true, None), + ("AMD Radeon 8050S Graphics", "gfx1151", true, None), + ("AMD Radeon 8060S Graphics", "gfx1151", true, None), + ( + "AMD RYZEN AI MAX+ 395 w/ Radeon 8060S", + "gfx1151", + true, + None, + ), + ("AMD Custom GPU 0405", "gfx1033", true, Some("163f")), +]; + +/// gfx targets a real host reports, with ground truth about packaging. +/// +/// gfx902, gfx909 and gfx1037 are here because no lookup table produces them: +/// they reach the CLI only through `gfx_target_from_gc_version`, from KFD's +/// packed `gfx_target_version` or DRM ip-discovery, so the table drift guard +/// cannot see them and ground truth has to. +const GFX_TARGETS: &[(&str, bool)] = &[ + ("gfx900", false), + ("gfx906", false), + ("gfx908", false), + ("gfx90a", false), + ("gfx942", false), + ("gfx950", false), + ("gfx1010", false), + ("gfx1011", false), + ("gfx1012", false), + ("gfx1030", false), + ("gfx1031", false), + ("gfx1032", false), + ("gfx1034", false), + ("gfx1100", false), + ("gfx1101", false), + ("gfx1102", false), + ("gfx1200", false), + ("gfx1201", false), + ("gfx1250", false), + // APU targets. + ("gfx902", true), + ("gfx909", true), + ("gfx90c", true), + ("gfx1033", true), + ("gfx1035", true), + ("gfx1036", true), + ("gfx1037", true), + ("gfx1103", true), + ("gfx1150", true), + ("gfx1151", true), + ("gfx1152", true), + ("gfx1153", true), +]; + +/// Well-formed gfx targets that nothing in this crate classifies: silicon newer +/// than the tables. A `rocminfo` agent reporting one of these is evidence of a +/// GPU, not of its packaging. +const UNCLASSIFIED_GFX_TARGETS: &[&str] = &["gfx1170", "gfx1180", "gfx1260", "gfx1300"]; + +// --------------------------------------------------------------------------- +// Mutators: the perturbations real-world messiness applies to these strings. +// --------------------------------------------------------------------------- + +/// Textual perturbations applied to a fixture-derived string. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Mutation { + None, + Upper, + Lower, + PadInnerWhitespace, + PadOuterWhitespace, + TrademarkDecoration, + RegisteredDecoration, + TruncateTail, + TruncateHead, + DropBrackets, + TabsForSpaces, +} + +fn mutation() -> impl Strategy { + prop_oneof![ + 6 => Just(Mutation::None), + 2 => Just(Mutation::Upper), + 2 => Just(Mutation::Lower), + 2 => Just(Mutation::PadInnerWhitespace), + 2 => Just(Mutation::PadOuterWhitespace), + 2 => Just(Mutation::TrademarkDecoration), + 1 => Just(Mutation::RegisteredDecoration), + 1 => Just(Mutation::TruncateTail), + 1 => Just(Mutation::TruncateHead), + 1 => Just(Mutation::DropBrackets), + 1 => Just(Mutation::TabsForSpaces), + ] +} + +/// The mutations that preserve the *identity* of the device being named. +/// +/// Case, whitespace and vendor decorations do; truncation and bracket removal +/// can destroy the token a lookup keys on. Properties that assert a +/// device-specific verdict draw from this narrower set rather than filtering +/// the wider one, which would spend proptest's reject budget on draws it was +/// always going to discard. +fn identity_preserving_mutation() -> impl Strategy { + prop_oneof![ + 6 => Just(Mutation::None), + 2 => Just(Mutation::Upper), + 2 => Just(Mutation::Lower), + 2 => Just(Mutation::PadInnerWhitespace), + 2 => Just(Mutation::PadOuterWhitespace), + 2 => Just(Mutation::TrademarkDecoration), + 1 => Just(Mutation::RegisteredDecoration), + 1 => Just(Mutation::TabsForSpaces), + ] +} + +fn apply_mutation(text: &str, m: Mutation) -> String { + match m { + Mutation::None => text.to_owned(), + Mutation::Upper => text.to_uppercase(), + Mutation::Lower => text.to_lowercase(), + Mutation::PadInnerWhitespace => text.replace(' ', " "), + Mutation::PadOuterWhitespace => format!(" {text}\t "), + Mutation::TrademarkDecoration => text.replacen("Radeon", "Radeon(TM)", 1), + Mutation::RegisteredDecoration => text.replacen("AMD", "AMD(R)", 1), + Mutation::TruncateTail => { + let keep = text.len().saturating_mul(2) / 3; + text.chars().take(keep).collect() + } + Mutation::TruncateHead => { + let drop = text.len() / 4; + text.chars().skip(drop).collect() + } + Mutation::DropBrackets => text.replace(['[', ']'], ""), + Mutation::TabsForSpaces => text.replace(' ', "\t"), + } +} + +// --------------------------------------------------------------------------- +// Strategies and fixtures +// --------------------------------------------------------------------------- + +/// A plausible PCI address, including the high bus numbers a Strix Halo iGPU +/// enumerates on (`0000:66:00.0` in #449). +fn pci_address() -> impl Strategy { + (0u32..2, 0u32..0x100, 0u32..0x20, 0u32..8) + .prop_map(|(dom, bus, dev, func)| format!("{dom:04x}:{bus:02x}:{dev:02x}.{func}")) +} + +/// A full `lspci -nn -D` line drawn from the real corpus, with the real vendor +/// tag, an optional revision suffix, and a mutation applied. +fn lspci_line() -> impl Strategy +{ + ( + pci_address(), + proptest::sample::select(LSPCI_CLASSES), + proptest::sample::select(AMD_LSPCI_DEVICES), + proptest::option::of(0u32..0x100), + mutation(), + ) + .prop_map(|(addr, class, device, rev, m)| { + let rev = rev.map_or_else(String::new, |r| format!(" (rev {r:02x})")); + let line = format!( + "{addr} {}: Advanced Micro Devices, Inc. [AMD/ATI] {} [1002:{}]{rev}", + class.0, device.0, device.1 + ); + (apply_mutation(&line, m), device) + }) +} + +/// A marketing name drawn from the real corpus with any mutation applied, +/// paired with the ground truth for the part it names. +fn marketing_name() -> impl Strategy< + Value = ( + String, + (&'static str, &'static str, bool, Option<&'static str>), + ), +> { + (proptest::sample::select(AMD_MARKETING_NAMES), mutation()) + .prop_map(|(entry, m)| (apply_mutation(entry.0, m), entry)) +} + +/// The same, restricted to spellings that still name the same device. +fn marketing_name_spelled_differently() -> impl Strategy< + Value = ( + String, + (&'static str, &'static str, bool, Option<&'static str>), + ), +> { + ( + proptest::sample::select(AMD_MARKETING_NAMES), + identity_preserving_mutation(), + ) + .prop_map(|(entry, m)| (apply_mutation(entry.0, m), entry)) +} + +/// Marketing names of parts that really are APUs, differently spelled. +fn apu_marketing_name() -> impl Strategy< + Value = ( + String, + (&'static str, &'static str, bool, Option<&'static str>), + ), +> { + let apus: Vec<_> = AMD_MARKETING_NAMES + .iter() + .copied() + .filter(|entry| entry.2) + .collect(); + assert!(!apus.is_empty(), "the marketing corpus has no APU"); + ( + proptest::sample::select(apus), + identity_preserving_mutation(), + ) + .prop_map(|(entry, m)| (apply_mutation(entry.0, m), entry)) +} + +/// `lspci` entries for parts that really are APUs and that the scan can +/// identify, by name or by device id. +fn apu_lspci_devices() -> Vec<(&'static str, &'static str, &'static str, bool)> { + let apus: Vec<_> = AMD_LSPCI_DEVICES + .iter() + .copied() + .filter(|device| device.3 && !UNIDENTIFIABLE_LSPCI_DEVICES.contains(&device.0)) + .collect(); + assert!(!apus.is_empty(), "the lspci corpus has no identifiable APU"); + apus +} + +/// The gfx target a `rocminfo` agent on an APU host reports: the device's own +/// target or, a quarter of the time, one nothing in this crate classifies. +/// +/// The unclassified half is the one that can tell the never-downgrade rule from +/// an unconditional overwrite. On the device's own target the two compute the +/// same answer, since every APU target in the corpus is classified, so a +/// property drawing only those could not fail with the rule removed. +fn agent_target_for(device_target: &'static str) -> impl Strategy { + prop_oneof![ + 3 => Just(device_target), + 1 => proptest::sample::select(UNCLASSIFIED_GFX_TARGETS), + ] +} + +/// Windows adapters that print only the generic `AMD Radeon(TM) Graphics`, as +/// `(name, true_gfx_target, is_really_an_apu, pci_device_id)`. +/// +/// The name resolves no part, so the device id is the only evidence a probe +/// has. These are the rows that tell a probe asking the id from one that asks +/// only the name: on every [`AMD_MARKETING_NAMES`] row the two agree, so a +/// Windows probe that stopped reading the PNP id would still pass on them. +/// Renoir, Cezanne and Lucienne sell their Vega iGPU under this name, and so +/// does the Raphael desktop iGPU. +const GENERIC_NAME_WINDOWS_ROWS: &[(&str, &str, bool, &str)] = &[ + ("AMD Radeon(TM) Graphics", "gfx90c", true, "1636"), + ("AMD Radeon(TM) Graphics", "gfx90c", true, "1638"), + ("AMD Radeon(TM) Graphics", "gfx90c", true, "164c"), + ("AMD Radeon(TM) Graphics", "gfx1036", true, "164e"), +]; + +/// The Windows rows the display-probe property draws from: every +/// marketing-name row whose PNP device id the device-id table maps, plus +/// [`GENERIC_NAME_WINDOWS_ROWS`]. +/// +/// Asserted to stay large enough to matter, and to keep rows whose name +/// resolves nothing, so the property cannot shrink to rows on which reading +/// the name alone gives the right answer without a test failing. +fn windows_corpus() -> Vec<(&'static str, &'static str, bool, &'static str)> { + let rows: Vec<_> = AMD_MARKETING_NAMES + .iter() + .filter_map(|(name, gfx, apu, id)| id.map(|id| (*name, *gfx, *apu, id))) + .chain(GENERIC_NAME_WINDOWS_ROWS.iter().copied()) + .collect(); + assert!( + rows.len() >= 14, + "only {} Windows rows carry a device id; the Windows property \ + would sample a handful of parts", + rows.len() + ); + let name_says_nothing = rows + .iter() + .filter(|(name, ..)| crate::gfx_target::gfx_target_from_amd_marketing_name(name).is_none()) + .count(); + assert!( + name_says_nothing >= 2, + "only {name_says_nothing} Windows rows have a name that resolves no part; \ + without them the property cannot tell the device id from the name" + ); + rows +} + +/// What the real PCI scan makes of one corpus device. +/// +/// Every test that needs a PCI-sourced GPU goes through this rather than +/// assembling one from the classifiers: a test that re-implements the probe's +/// logic pins the re-implementation, and keeps passing when the probe stops +/// reading the device id. +fn pci_scanned(device: &str, device_id: &str, addr: &str) -> Gpu { + let line = format!( + "{addr} VGA compatible controller [0300]: Advanced Micro Devices, Inc. \ + [AMD/ATI] {device} [1002:{device_id}] (rev c1)" + ); + let mut e = Examination::default(); + apply_lspci_gpus(&mut e, &line); + assert_eq!(e.gpus.len(), 1, "{line:?} must enumerate as one GPU"); + e.gpus.remove(0) +} + +/// A `Win32_VideoController` row as `WIN_GPU_SCRIPT` prints it: name, driver +/// version, PNP device id. +fn windows_row(name: &str, device_id: &str) -> String { + format!("{name}\t32.0.1\tPCI\\VEN_1002&DEV_{device_id}&SUBSYS_00001002&REV_C1") +} + +/// Every PCI device id the device-id table maps, with its target. +/// +/// The table is a `match`, not an iterable, so its whole input domain is swept. +fn every_mapped_pci_device_id() -> Vec<(String, &'static str)> { + let ids: Vec<_> = (0..=0xffff_u32) + .map(|id| format!("{id:04x}")) + .filter_map(|id| crate::gfx_target::gfx_target_from_amd_pci_device_id(&id).map(|t| (id, t))) + .collect(); + assert!(!ids.is_empty(), "the device-id table maps no id at all"); + ids +} + +/// A `rocminfo` agent listing for the given targets, in the agent block shape +/// the current parser can digest (no ISA sub-entries). +fn rocminfo_output(agents: &[(&str, &str)]) -> String { + rocminfo_output_shaped(agents, false) +} + +/// `with_isa_entries` reproduces what `rocminfo` since ROCm 5 prints: each GPU +/// agent is followed by indented ISA sub-entries that *also* begin with +/// `Name:`. The parser keeps a running `cur_name` and lets those lines +/// overwrite the agent's gfx name, so the agent is dropped and the fold does +/// nothing -- ROCm/rocm-cli#393. +fn rocminfo_output_shaped(agents: &[(&str, &str)], with_isa_entries: bool) -> String { + use std::fmt::Write as _; + let mut out = String::from( + "=====================\nHSA System Attributes\n=====================\n\ + Runtime Version: 1.1\n\n", + ); + out.push_str( + "==========\nHSA Agents\n==========\n*******\nAgent 1\n*******\n \ + Name: AMD Ryzen 9 7950X\n \ + Marketing Name: AMD Ryzen 9 7950X\n \ + Device Type: CPU\n", + ); + for (index, (gfx, marketing)) in agents.iter().enumerate() { + let _ = write!( + out, + "*******\nAgent {}\n*******\n Name: {gfx}\n \ + Marketing Name: {marketing}\n Device Type: GPU\n", + index + 2 + ); + if with_isa_entries { + let family = gfx.get(..5).unwrap_or(gfx); + let _ = write!( + out, + " ISA Info:\n ISA 1\n Name: \ + amdgcn-amd-amdhsa--{gfx}\n ISA 2\n Name: \ + amdgcn-amd-amdhsa--{family}-generic\n" + ); + } + } + out +} + +// --------------------------------------------------------------------------- +// Properties +// --------------------------------------------------------------------------- + +proptest! { + #![proptest_config(Config { + cases: proptest_cases(2048), + failure_persistence: None, + ..Config::default() + })] + + /// Totality: no fixture-derived or mutated `lspci` line may panic any of + /// the classifiers, or the PCI scan that composes them, and a single line + /// never enumerates as more than one GPU. + #[test] + fn classifiers_never_panic_on_a_mutated_lspci_line((line, _) in lspci_line()) { + let _ = is_lspci_gpu_line(&line); + let _ = extract_lspci_device_id(&line); + let (gfx, _) = classify_amd_marketing_name(&extract_lspci_name(&line)); + let _ = gfx_apu_verdict(&gfx); + let mut e = Examination::default(); + apply_lspci_gpus(&mut e, &line); + prop_assert!(e.gpus.len() <= 1, "{line:?} enumerated as {:?}", e.gpus); + } + + /// `extract_lspci_name` recovers exactly the vendor-plus-device text of an + /// unmutated `lspci -nn` line: everything between the class `]:` and the + /// trailing `[vendor:device]`. + #[test] + fn lspci_name_extraction_round_trips( + addr in pci_address(), + class in proptest::sample::select(LSPCI_CLASSES), + device in proptest::sample::select(AMD_LSPCI_DEVICES), + rev in proptest::option::of(0u32..0x100), + ) { + let rev = rev.map_or_else(String::new, |r| format!(" (rev {r:02x})")); + let line = format!( + "{addr} {}: Advanced Micro Devices, Inc. [AMD/ATI] {} [1002:{}]{rev}", + class.0, device.0, device.1 + ); + let expected = format!("Advanced Micro Devices, Inc. [AMD/ATI] {}", device.0); + prop_assert_eq!(extract_lspci_name(&line), expected); + prop_assert_eq!(extract_lspci_device_id(&line), Some(device.1.to_owned())); + } + + /// A name that names an APU must not be reported as "not an APU". + #[test] + fn examine_never_calls_a_known_apu_discrete((name, truth) in apu_marketing_name()) { + let (target, is_apu) = classify_amd_marketing_name(&name); + prop_assert!( + is_apu, + "{name} is an APU ({}) but examine says is_apu=false, gfx_target={target:?}", + truth.1, + ); + } + + /// Folding a `rocminfo` reading into the report never *downgrades* an APU + /// verdict the PCI scan already reached. More evidence must not produce a + /// worse answer -- including when the agent reports a target this crate + /// does not classify, which is what [`agent_target_for`] draws. + /// + /// The domain is the APU's own target or an unclassified one, on purpose. + /// An agent reporting a target classified *discrete* does replace the + /// verdict, because the target names the silicon and outranks the PCI + /// scan's reading of a name. On a correctly paired row that does not meet + /// an APU verdict in practice: the scan's verdicts come from the same + /// tables, so its APU rows carry APU targets (gfx942, which MI300A shares, + /// is discrete in both). + /// What the rule forbids is a downgrade from an *absence* of evidence. + #[test] + fn rocminfo_never_downgrades_an_apu_verdict( + (device, agent_target) in proptest::sample::select(apu_lspci_devices()) + .prop_flat_map(|device| (Just(device), agent_target_for(device.2))), + addr in pci_address(), + marketing in proptest::sample::select(&["", "AMD Radeon Graphics"][..]), + ) { + let gpu = pci_scanned(device.0, device.1, &addr); + prop_assert!( + gpu.is_apu == Some(true), + "the PCI scan must already know {} is an APU before this property can \ + say anything about preserving that verdict: {:?}", + device.0, + gpu, + ); + + let mut e = Examination { + gpus: vec![gpu], + ..Examination::default() + }; + apply_rocminfo_gpu_agents(&mut e, &rocminfo_output(&[(agent_target, marketing)])); + summarise_gpu_categories(&mut e); + prop_assert_eq!(&e.gpus[0].gfx_target, agent_target, "the agent's target is recorded"); + prop_assert!( + e.has_apu, + "lspci classified {} as an APU, then rocminfo reporting {} flipped has_apu \ + to false: {:?}", + device.0, + agent_target, + e.gpus, + ); + prop_assert!( + !e.has_discrete_amd, + "an APU-only host must not report has_discrete_amd (agent {}): {:?}", + agent_target, + e.gpus, + ); + } + + /// Classification does not depend on spelling: case, inner/outer + /// whitespace and vendor decorations name the same device. + #[test] + fn classification_is_spelling_invariant((name, truth) in marketing_name_spelled_differently()) { + let canonical = classify_amd_marketing_name(truth.0); + let mutated = classify_amd_marketing_name(&name); + prop_assert_eq!( + &canonical, + &mutated, + "{:?} and {:?} are the same device but classify differently", + truth.0, + name, + ); + } + + /// Both APU answers agree with ground truth for every target a real host + /// reports, whatever its case or feature suffix: `gfx_is_apu_family` says + /// yes or no, and `gfx_apu_verdict` knows the answer. + #[test] + fn gfx_apu_verdicts_match_ground_truth( + gfx in proptest::sample::select(GFX_TARGETS), + suffix in proptest::sample::select(&["", ":xnack-", ":sramecc+:xnack-", ":sramecc-"][..]), + upper in any::(), + ) { + let spelled = if upper { + format!("{}{suffix}", gfx.0.to_uppercase()) + } else { + format!("{}{suffix}", gfx.0) + }; + prop_assert_eq!(gfx_is_apu_family(&spelled), gfx.1, "gfx_is_apu_family({})", spelled); + prop_assert_eq!(gfx_apu_verdict(&spelled), Some(gfx.1), "gfx_apu_verdict({})", spelled); + } + + /// `examine`'s Windows display probe and the install-side display decoder + /// read the same adapter to the same target. + /// + /// They share the device-id and name tables but not the wiring: `examine` + /// splits `namedriverpnp` and asks the PNP id first, the install + /// side reads `namepnp` rows and ranks a gfx90c iGPU below every other + /// adapter. Each side is also held to ground truth, so a change to either + /// wiring that stops reading the PNP id -- reads it from the wrong column, + /// or resolves from the name alone -- fails on the generic-name rows + /// ([`GENERIC_NAME_WINDOWS_ROWS`]), whose name resolves nothing. + /// + /// What this cannot see is the order of the two lookups where both answer: + /// on every real row the id and the name agree, so "id first" and "name + /// first" give the same target. That order is pinned by the hand-built + /// disagreeing pair in `windows_gpu_probe_prefers_the_pci_id_match_when_one_exists`. + #[test] + fn examine_and_the_install_probe_read_a_windows_row_alike( + (name, gfx, apu, device_id) in proptest::sample::select(windows_corpus()), + ) { + let mut e = Examination::default(); + parse_gpus_windows_output(&windows_row(name, device_id), &mut e); + prop_assert_eq!(e.gpus.len(), 1); + let install = crate::gfx_target::parse_windows_display_gfx_target(&format!( + "{name}\tPCI\\VEN_1002&DEV_{device_id}&SUBSYS_00001002&REV_C1" + )); + prop_assert_eq!(install.as_deref(), Some(gfx), "install side, {}", name); + prop_assert_eq!(&e.gpus[0].gfx_target, gfx, "examine, {}", name); + prop_assert_eq!(e.gpus[0].is_apu, Some(apu), "examine, {}", name); + } +} + +// --------------------------------------------------------------------------- +// Exhaustive sweeps: the whole corpus, so a failure lists every case rather +// than the one minimal case a shrinker settles on. +// --------------------------------------------------------------------------- + +/// Every APU in the corpora is classified as one, by every door. +#[test] +fn every_known_apu_is_classified_as_an_apu() { + let mut misses: Vec = Vec::new(); + for (name, target, is_apu, _) in AMD_MARKETING_NAMES { + if !is_apu { + continue; + } + let (examine_target, examine_is_apu) = classify_amd_marketing_name(name); + if !examine_is_apu { + misses.push(format!( + " marketing name {name:?} ({target}): is_apu=false, gfx_target={examine_target:?}" + )); + } + } + for (gfx, is_apu) in GFX_TARGETS { + if *is_apu && !gfx_is_apu_family(gfx) { + misses.push(format!(" gfx target {gfx}: gfx_is_apu_family says false")); + } + } + for (device, id, _gfx, is_apu) in AMD_LSPCI_DEVICES { + if !is_apu || UNIDENTIFIABLE_LSPCI_DEVICES.contains(device) { + continue; + } + let gpu = pci_scanned(device, id, "0000:04:00.0"); + if gpu.is_apu != Some(true) { + misses.push(format!( + " lspci device {device:?} [1002:{id}]: the PCI scan says {gpu:?}" + )); + } + } + assert!( + misses.is_empty(), + "parts that are APUs but are not classified as APUs:\n{}", + misses.join("\n") + ); +} + +/// No discrete part in the corpora is classified as an APU -- the other +/// direction, which keeps a codename pattern from reaching past its part. +#[test] +fn no_discrete_part_is_classified_as_an_apu() { + let mut wrong: Vec = Vec::new(); + for (name, gfx, is_apu, _) in AMD_MARKETING_NAMES { + if !is_apu && classify_amd_marketing_name(name).1 { + wrong.push(format!(" marketing name {name:?} ({gfx})")); + } + } + for (device, id, gfx, is_apu) in AMD_LSPCI_DEVICES { + if !is_apu && pci_scanned(device, id, "0000:03:00.0").is_apu == Some(true) { + wrong.push(format!(" lspci {device:?} [1002:{id}] ({gfx})")); + } + } + assert!( + wrong.is_empty(), + "discrete parts classified as APUs:\n{}", + wrong.join("\n") + ); +} + +/// A reported `gfx_target` never *contradicts* the part: an unresolved lookup +/// is recoverable, a wrong answer selects the wrong runtime build. +#[test] +fn no_part_is_labelled_with_the_wrong_gfx_target() { + let mut wrong: Vec = Vec::new(); + for (device, id, gfx, _is_apu) in AMD_LSPCI_DEVICES { + let guess = pci_scanned(device, id, "0000:04:00.0").gfx_target; + if !guess.is_empty() && guess != *gfx { + wrong.push(format!( + " lspci {device:?} [1002:{id}] is {gfx}, but examine reports {guess}" + )); + } + } + for (name, gfx, _is_apu, _) in AMD_MARKETING_NAMES { + let guess = classify_amd_marketing_name(name).0; + if !guess.is_empty() && guess != *gfx { + wrong.push(format!( + " marketing name {name:?} is {gfx}, but examine reports {guess}" + )); + } + } + assert!( + wrong.is_empty(), + "parts labelled with a gfx target that is not theirs:\n{}", + wrong.join("\n") + ); +} + +/// Every gfx target a lookup table can hand back has an APU verdict. +/// +/// The name and device-id tables are keyed by name and id, and the packaging +/// knowledge by target, so nothing ties them together. A table entry for a +/// target neither `gfx_is_apu_family` nor `DISCRETE_GFX_TARGETS` knows would +/// answer "cannot say" for it, and the rocminfo fold would then leave a wrong +/// PCI-scan verdict standing. +#[test] +fn every_target_a_lookup_table_produces_has_an_apu_verdict() { + let mut unclassified: Vec = Vec::new(); + for entry in crate::gfx_target::AMD_MARKETING_GFX_TARGETS { + if gfx_apu_verdict(entry.gfx_target).is_none() { + unclassified.push(format!( + " marketing pattern {:?} -> {}", + entry.pattern, entry.gfx_target + )); + } + } + for (id, target) in every_mapped_pci_device_id() { + if gfx_apu_verdict(target).is_none() { + unclassified.push(format!(" PCI device id {id} -> {target}")); + } + } + unclassified.sort(); + unclassified.dedup(); + assert!( + unclassified.is_empty(), + "targets a lookup table produces but nothing classifies:\n{}", + unclassified.join("\n") + ); + for target in UNCLASSIFIED_GFX_TARGETS { + assert_eq!( + gfx_apu_verdict(target), + None, + "{target} is meant to be a target nothing classifies; the \ + never-downgrade property draws on that" + ); + } +} + +/// The Linux PCI scan classifies a row by its device id when the name carries +/// nothing. +/// +/// `lspci` prints the bare word `Device` for an id `pci.ids` does not know, so +/// on these rows only the `[1002:xxxx]` tag can identify the part. Every id the +/// device-id table maps is swept, so a scan that falls back to reading the name +/// alone fails here for all of them at once. +#[test] +fn an_lspci_row_is_classified_by_its_device_id_when_its_name_says_nothing() { + let mut wrong: Vec = Vec::new(); + for (id, target) in every_mapped_pci_device_id() { + let gpu = pci_scanned("Device", &id, "0000:66:00.0"); + if gpu.gfx_target != target || gpu.is_apu != Some(gfx_is_apu_family(target)) { + wrong.push(format!(" [1002:{id}] is {target}, scanned as {gpu:?}")); + } + } + assert!( + wrong.is_empty(), + "lspci rows the device id identifies but the scan did not:\n{}", + wrong.join("\n") + ); +} + +// --------------------------------------------------------------------------- +// The folds that follow the PCI scan +// --------------------------------------------------------------------------- + +/// What a `rocminfo` fold does to the APU verdict on each side of +/// ROCm/rocm-cli#393. +/// +/// #393: `rocminfo` prints indented ISA `Name:` sub-entries after each agent, +/// the parser's running `cur_name` ends up holding +/// `amdgcn-amd-amdhsa--gfx11-generic`, and the agent is dropped. That makes the +/// `is_apu` revision unreachable on a real host, and fixing #393 is what makes +/// it reachable, so both parser shapes are driven here, over two rows: +/// +/// - a Raphael iGPU the PCI scan identified: its verdict holds either way; +/// - a row the PCI scan could not identify (`Device`, device id `1586`, which +/// no table maps): only a parser that reads the agent can revise its "not an +/// APU" default from the agent's gfx1151, and with the agent dropped the +/// default stands. The second half pins that the revision is real code, so +/// deleting it fails here rather than leaving this test green. +#[test] +fn the_rocminfo_fold_on_both_sides_of_the_isa_name_defect() { + for (with_isa, parser) in [ + (true, "with the agent-dropping parser of #393"), + (false, "with a parser that reads the agent"), + ] { + let gpu = pci_scanned("Raphael", "164e", "0000:14:00.0"); + assert_eq!(gpu.is_apu, Some(true), "the PCI scan alone knows Raphael"); + let mut e = Examination { + gpus: vec![gpu], + ..Examination::default() + }; + apply_rocminfo_gpu_agents( + &mut e, + &rocminfo_output_shaped(&[("gfx1036", "AMD Radeon Graphics")], with_isa), + ); + summarise_gpu_categories(&mut e); + assert_eq!(e.gpus[0].gfx_target, "gfx1036", "{parser}: {:?}", e.gpus); + assert!( + e.has_apu && !e.has_discrete_amd, + "{parser}: a Raphael-only host is one APU and no discrete GPU: {:?}", + e.gpus + ); + + let gpu = pci_scanned("Device", "1586", "0000:c5:00.0"); + assert_eq!( + (gpu.gfx_target.as_str(), gpu.is_apu), + ("", Some(false)), + "the premise: no table maps 1586" + ); + let mut e = Examination { + gpus: vec![gpu], + ..Examination::default() + }; + apply_rocminfo_gpu_agents( + &mut e, + &rocminfo_output_shaped(&[("gfx1151", "AMD Radeon Graphics")], with_isa), + ); + let expected = if with_isa { + ("", Some(false)) + } else { + ("gfx1151", Some(true)) + }; + assert_eq!( + (e.gpus[0].gfx_target.as_str(), e.gpus[0].is_apu), + expected, + "{parser}: {:?}", + e.gpus + ); + } +} + +/// Reading `rocminfo` only ever improves the report. +/// +/// - A Strix Halo whose `lspci` row identifies nothing: the agent supplies both +/// the target and the APU verdict that follows from it. +/// - A Rembrandt laptop whose agent names a target nothing here classifies: the +/// target is worth recording, but it is not evidence of a discrete GPU, so +/// the PCI scan's verdict stands. +/// - A discrete card the PCI scan could not identify: a known discrete target +/// still revises the default, which keeps the fold from being a no-op. +#[test] +fn folding_in_rocminfo_only_ever_adds_knowledge() { + let host = |lspci_name: &str, device_id: &str, agent_target: &str| { + let mut e = Examination { + gpus: vec![pci_scanned(lspci_name, device_id, "0000:66:00.0")], + ..Examination::default() + }; + apply_rocminfo_gpu_agents( + &mut e, + &rocminfo_output(&[(agent_target, "AMD Radeon Graphics")]), + ); + summarise_gpu_categories(&mut e); + e + }; + + let halo = host("Device", "1586", "gfx1151"); + assert_eq!(halo.gpus[0].gfx_target, "gfx1151", "{:?}", halo.gpus); + assert!( + halo.has_apu && !halo.has_discrete_amd, + "a gfx1151 agent establishes the APU verdict the PCI scan could not: {:?}", + halo.gpus + ); + + let unknown = host("Rembrandt [Radeon 680M]", "1681", "gfx1170"); + assert_eq!(unknown.gpus[0].gfx_target, "gfx1170", "{:?}", unknown.gpus); + assert!( + unknown.has_apu && !unknown.has_discrete_amd, + "an unclassified target must leave the PCI scan's APU verdict standing: {:?}", + unknown.gpus + ); + + // Navi 10 is in neither table, so the scan reports it with the default + // `Some(false)`; a gfx1010 agent confirms it. + let navi10 = host( + "Navi 10 [Radeon RX 5600 OEM/5600 XT / 5700/5700 XT]", + "731f", + "gfx1010", + ); + assert!( + navi10.has_discrete_amd && !navi10.has_apu, + "{:?}", + navi10.gpus + ); +} + +/// The same rule where there is no PCI row at all: an ordinary ROCm container, +/// which ships `rocminfo` but not `pciutils`. +/// +/// Every agent lands in the branch that *creates* a GPU entry, so there is no +/// earlier verdict to preserve -- but "not an APU" and "cannot say" still +/// differ. Defaulting the latter to `Some(false)` reports a discrete AMD GPU on +/// a laptop that has none. +#[test] +fn an_agent_with_no_pci_row_is_classified_from_what_evidence_there_is() { + let container = |agent_target: &str, marketing: &str| { + let mut e = Examination::default(); + apply_rocminfo_gpu_agents(&mut e, &rocminfo_output(&[(agent_target, marketing)])); + summarise_gpu_categories(&mut e); + e + }; + + // The target is enough on its own. + let known = container("gfx1036", "AMD Radeon Graphics"); + assert_eq!(known.gpus[0].is_apu, Some(true), "{:?}", known.gpus); + + // An unclassified target, but the agent names the part: the name answers. + let named = container("gfx1170", "AMD Radeon(TM) 780M Graphics"); + assert_eq!(named.gpus[0].is_apu, Some(true), "{:?}", named.gpus); + assert!(!named.has_discrete_amd, "{:?}", named.gpus); + + // Neither answers. Saying "discrete" here would be inventing a fact. + let silent = container("gfx1170", ""); + assert_eq!(silent.gpus[0].is_apu, None, "{:?}", silent.gpus); + assert!( + silent.has_amd_gpu && !silent.has_apu && !silent.has_discrete_amd, + "{:?}", + silent.gpus + ); + + // A discrete card still reports as one. + let discrete = container("gfx1100", "AMD Radeon RX 7900 XTX"); + assert_eq!(discrete.gpus[0].is_apu, Some(false), "{:?}", discrete.gpus); +} + +/// `hipInfo` revises the display probe's verdict when it reports a target +/// whose packaging is known, and only then. +/// +/// The first half is a host that exists: a Windows Mendocino laptop reports +/// "AMD Radeon(TM) Graphics" with a device id the table does not map, so the +/// display probe gives it no target and the default `Some(false)`, and only +/// `hipInfo`'s `gcnArchName` can say what it is. +/// +/// The second half pins the fold's contract on a state the display probe +/// cannot produce today -- a GPU already called an APU, with no target -- since +/// the probe only reaches `Some(true)` by resolving one. Through the real +/// pipeline the "leave an unknown target's verdict alone" rule therefore +/// changes no output yet; it is asserted so the first new source of a +/// target-less verdict does not inherit the downgrade silently. +#[test] +fn a_hipinfo_target_revises_a_verdict_only_when_its_packaging_is_known() { + let mut e = Examination::default(); + parse_gpus_windows_output(&windows_row("AMD Radeon(TM) Graphics", "1506"), &mut e); + assert_eq!(e.gpus[0].gfx_target, "", "the premise: {:?}", e.gpus); + assert_eq!(e.gpus[0].is_apu, Some(false), "the premise: {:?}", e.gpus); + fill_unidentified_gpus_from_hipinfo(&mut e.gpus, " gcnArchName: gfx1037\n"); + assert_eq!(e.gpus[0].gfx_target, "gfx1037", "{:?}", e.gpus); + assert_eq!( + e.gpus[0].is_apu, + Some(true), + "a known integrated target must revise the default: {:?}", + e.gpus + ); + + let mut gpus = vec![Gpu { + name: "AMD Radeon(TM) Graphics".to_owned(), + is_amd: true, + is_apu: Some(true), + ..Gpu::default() + }]; + fill_unidentified_gpus_from_hipinfo(&mut gpus, " gcnArchName: gfx1170\n"); + assert_eq!(gpus[0].gfx_target, "gfx1170", "{gpus:?}"); + assert_eq!( + gpus[0].is_apu, + Some(true), + "an unclassified target is not evidence of a discrete GPU: {gpus:?}" + ); +} + +/// The never-downgrade rule run through the whole post-PCI sequence against a +/// planted KFD topology, rather than through `apply_rocminfo_gpu_agents` alone. +/// +/// A Ryzen 6800H laptop: `lspci` names the iGPU "Rembrandt [Radeon 680M]", the +/// kernel exposes one KFD node for it at `0000:04:00.0`, and `rocminfo` reports +/// a target nothing here classifies (as a newer ROCm would for a newer part). +/// The report must not call the laptop's only GPU discrete. +#[test] +fn a_rembrandt_laptop_keeps_its_apu_verdict_through_every_probe() { + let root = tempfile::tempdir().expect("scratch dir"); + let nodes = root.path().join("nodes"); + std::fs::create_dir_all(nodes.join("0")).expect("plant the CPU node"); + std::fs::write( + nodes.join("0").join("properties"), + "cpu_cores_count 16\ngfx_target_version 0\nlocation_id 0\ndomain 0\n", + ) + .expect("plant the CPU node properties"); + std::fs::create_dir_all(nodes.join("1")).expect("plant the GPU node"); + std::fs::write( + nodes.join("1").join("properties"), + "simd_count 12\ngfx_target_version 100305\nlocation_id 1024\ndomain 0\n", + ) + .expect("plant the GPU node properties"); + + let mut e = Examination::default(); + apply_lspci_gpus( + &mut e, + "0000:04:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. \ + [AMD/ATI] Rembrandt [Radeon 680M] [1002:1681] (rev c8)", + ); + probe_gpus_after_lspci( + &mut e, + GpuProbeSources { + kfd_nodes: &nodes, + rocminfo: Some(&rocminfo_output(&[("gfx1170", "AMD Radeon Graphics")])), + sysfs_gfx_target: || None, + }, + ); + summarise_gpu_categories(&mut e); + + assert_eq!(e.gpus.len(), 1, "one GPU: {:?}", e.gpus); + assert!( + e.has_apu && !e.has_discrete_amd, + "a Radeon 680M iGPU must stay an APU: {:?}", + e.gpus + ); +} + +/// What the iGPU+dGPU collision check sees, run through the real diagnosis +/// catalog. +/// +/// A Ryzen 7000 desktop pairing the Raphael iGPU with an RX 7900 XTX is the +/// host `check_9_igpu_dgpu_collision` exists for, and it fires only when +/// `has_apu && has_discrete_amd`. The note must also name each card correctly, +/// since a diagnosis that confuses the two is no better than silence. +#[test] +fn the_igpu_dgpu_collision_check_fires_on_a_raphael_plus_rx7900_host() { + let igpu = "0000:14:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. \ + [AMD/ATI] Raphael [1002:164e] (rev c1)"; + let dgpu = "0000:03:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. \ + [AMD/ATI] Navi 31 [Radeon RX 7900 XT/7900 XTX/7900 GRE/7900M] [1002:744c]"; + let mut e = Examination { + os_family: "linux".to_owned(), + ..Examination::default() + }; + apply_lspci_gpus(&mut e, &format!("{dgpu}\n{igpu}\n")); + apply_rocminfo_gpu_agents( + &mut e, + &rocminfo_output(&[ + ("gfx1100", "AMD Radeon RX 7900 XTX"), + ("gfx1036", "AMD Radeon Graphics"), + ]), + ); + summarise_gpu_categories(&mut e); + + let report = crate::diagnose::diagnose(&e, "my training run segfaults"); + let Some(collision) = report + .matched + .iter() + .find(|d| d.id == "fix-9-igpu-dgpu" && d.score > 0) + else { + panic!( + "an iGPU+dGPU host must raise fix-9-igpu-dgpu; has_apu={}, \ + has_discrete_amd={}, gpus={:?}", + e.has_apu, e.has_discrete_amd, e.gpus, + ); + }; + let notes = collision + .fix + .as_ref() + .map(|fix| fix.notes.join(" ")) + .unwrap_or_default(); + assert!( + notes.contains("[\"gfx1100\"]") && notes.contains("[\"gfx1036\"]"), + "the note must name gfx1100 as the discrete GPU and gfx1036 as the APU: {notes}" + ); +} + +/// An AMD PCI row nothing can identify is reported as discrete, on purpose, +/// and both sides of that trade are pinned here -- see +/// `classify_amd_marketing_name`. +/// +/// What it buys: a Ryzen 7000 desktop with an RX 5700 XT. Navi 10 is in +/// neither table; reported as "cannot say", it would clear `has_discrete_amd` +/// and the collision check would never fire on the host it is written for. +/// +/// What it costs: a Strix Halo whose `lspci` row is the nameless `Device` of +/// #449 is reported with `has_discrete_amd` set. The collision check still +/// needs `has_apu`, which that host lacks, so no diagnosis fires; the cost is a +/// wrong field in `rocm examine --json` until `rocminfo` names the target. +#[test] +fn an_unidentifiable_pci_row_is_reported_as_discrete_and_this_is_what_that_costs() { + let raphael = "0000:14:00.0 VGA compatible controller [0300]: Advanced Micro Devices, \ + Inc. [AMD/ATI] Raphael [1002:164e] (rev c1)"; + let navi10 = "0000:03:00.0 VGA compatible controller [0300]: Advanced Micro Devices, \ + Inc. [AMD/ATI] Navi 10 [Radeon RX 5600 OEM/5600 XT / 5700/5700 XT] \ + [1002:731f] (rev c1)"; + let halo = "0000:66:00.0 Display controller [0380]: Advanced Micro Devices, \ + Inc. [AMD/ATI] Device [1002:1586] (rev c1)"; + let host = |lines: &[&str]| { + let mut e = Examination { + os_family: "linux".to_owned(), + ..Examination::default() + }; + apply_lspci_gpus(&mut e, &lines.join("\n")); + summarise_gpu_categories(&mut e); + let fires = crate::diagnose::diagnose(&e, "my training run segfaults") + .matched + .iter() + .any(|d| d.id == "fix-9-igpu-dgpu" && d.score > 0); + (e, fires) + }; + + let (hybrid, fires) = host(&[navi10, raphael]); + assert_eq!( + hybrid.gpus[0].gfx_target, "", + "the premise: Navi 10 is unresolved" + ); + assert!( + hybrid.has_apu && hybrid.has_discrete_amd && fires, + "an unresolved discrete card beside a known iGPU must still raise the \ + collision diagnosis: {:?}", + hybrid.gpus + ); + + let (laptop, fires) = host(&[halo]); + assert_eq!( + laptop.gpus[0].gfx_target, "", + "the premise: the row is unresolved" + ); + assert!( + laptop.has_discrete_amd && !laptop.has_apu, + "the known cost: an unresolved iGPU reads as discrete: {:?}", + laptop.gpus + ); + assert!( + !fires, + "and it stops there: no collision diagnosis on one GPU" + ); +} + +// --------------------------------------------------------------------------- +// Generator reach +// --------------------------------------------------------------------------- + +/// Measure how far the generators reach, by drawing from them directly and +/// tallying which regions each draw lands in. +/// +/// A generator that never samples the interesting region passes every property +/// vacuously, so every counter printed here has a floor. The floors sit well +/// under the measured rates so a reseed does +/// not trip them, and well over zero so a generator that stopped reaching a +/// region does. +#[test] +fn generator_reach_is_measured_and_sufficient() { + const DRAWS: u32 = 2048; + let draws = u64::from(DRAWS); + let mut runner = TestRunner::new(Config::with_cases(DRAWS)); + + let mut lspci_parsed = 0u64; + let mut lspci_gpu_class = 0u64; + let mut lspci_apu_device = 0u64; + let mut lspci_classified_apu = 0u64; + let mut lspci_gfx_resolved = 0u64; + for _ in 0..DRAWS { + let (line, device) = lspci_line() + .new_tree(&mut runner) + .expect("lspci strategy") + .current(); + if is_lspci_gpu_line(&line) { + lspci_gpu_class += 1; + } + if !extract_lspci_name(&line).is_empty() { + lspci_parsed += 1; + } + if device.3 { + lspci_apu_device += 1; + } + let mut e = Examination::default(); + apply_lspci_gpus(&mut e, &line); + if let Some(gpu) = e.gpus.first() { + if gpu.is_apu == Some(true) { + lspci_classified_apu += 1; + } + if !gpu.gfx_target.is_empty() { + lspci_gfx_resolved += 1; + } + } + } + + let mut marketing_resolved = 0u64; + let mut marketing_apu_truth = 0u64; + for _ in 0..DRAWS { + let (name, truth) = marketing_name() + .new_tree(&mut runner) + .expect("marketing strategy") + .current(); + if !classify_amd_marketing_name(&name).0.is_empty() { + marketing_resolved += 1; + } + if truth.2 { + marketing_apu_truth += 1; + } + } + + let mut agent_unclassified = 0u64; + let agent_strategy = proptest::sample::select(apu_lspci_devices()) + .prop_flat_map(|device| agent_target_for(device.2)); + for _ in 0..DRAWS { + let target = agent_strategy + .new_tree(&mut runner) + .expect("agent strategy") + .current(); + if gfx_apu_verdict(target).is_none() { + agent_unclassified += 1; + } + } + + #[allow(clippy::cast_precision_loss)] + let pct = |n: u64| (n as f64) * 100.0 / f64::from(DRAWS); + println!( + "generator reach over {DRAWS} draws each:\n\ + lspci lines : name parsed {:.1}%, GPU-class {:.1}%, APU silicon {:.1}%, \ + scanned as APU {:.1}%, gfx resolved {:.1}%\n\ + marketing names : resolved {:.1}%, APU silicon {:.1}%\n\ + rocminfo agents : unclassified target {:.1}%", + pct(lspci_parsed), + pct(lspci_gpu_class), + pct(lspci_apu_device), + pct(lspci_classified_apu), + pct(lspci_gfx_resolved), + pct(marketing_resolved), + pct(marketing_apu_truth), + pct(agent_unclassified), + ); + let floors = [ + ("lspci: name parsed", lspci_parsed, draws / 2), + // A third, not a half: one class in five is the PCI bridge the probe + // must not enumerate, and the case and tab mutations break the + // case-sensitive class match on purpose so that branch is sampled too. + ("lspci: enumerated GPU class", lspci_gpu_class, draws / 3), + ("lspci: APU silicon", lspci_apu_device, draws / 4), + ("lspci: scanned as an APU", lspci_classified_apu, draws / 6), + ("lspci: gfx target resolved", lspci_gfx_resolved, draws / 4), + ("marketing: name resolved", marketing_resolved, draws / 2), + ("marketing: APU silicon", marketing_apu_truth, draws / 3), + ( + "rocminfo: unclassified agent target", + agent_unclassified, + draws / 8, + ), + ]; + for (region, reached, floor) in floors { + assert!( + reached > floor, + "{region}: {reached} of {DRAWS} draws, under the floor of {floor}" + ); + } +} diff --git a/crates/rocm-core/src/gfx_target.rs b/crates/rocm-core/src/gfx_target.rs index 18c4293431..ca9cd77224 100644 --- a/crates/rocm-core/src/gfx_target.rs +++ b/crates/rocm-core/src/gfx_target.rs @@ -144,8 +144,17 @@ pub(crate) fn parse_windows_display_gfx_target(text: &str) -> Option { } return Some(target.to_owned()); } - if name_fallback.is_none() { - name_fallback = gfx_target_from_amd_marketing_name(name).map(str::to_owned); + if name_fallback.is_none() + && let Some(target) = gfx_target_from_amd_marketing_name(name) + { + // The same rule for a name: the marketing table names gfx90c by its + // codenames, and an iGPU recognised that way must not hide a + // discrete card further down the list either. + if gfx_target_has_no_rocm_build(target) { + unsupported_match = unsupported_match.or(Some(target)); + } else { + name_fallback = Some(target.to_owned()); + } } } name_fallback.or_else(|| unsupported_match.map(str::to_owned)) @@ -240,6 +249,12 @@ pub(crate) fn gfx_target_from_amd_pci_device_id(device_id: &str) -> Option<&'sta } } +/// The gfx target an AMD marketing name or `lspci` codename names, or `None` +/// when no entry in [`AMD_MARKETING_GFX_TARGETS`] matches. +/// +/// This is the crate's one name-to-target table: the install-side display +/// probes and `examine`'s GPU probes both answer from it, so a name resolves to +/// the same target whichever command reads it. pub(crate) fn gfx_target_from_amd_marketing_name(name: &str) -> Option<&'static str> { let lower = name.to_ascii_lowercase(); let normalized = normalize_marketing_name_for_match(&lower); @@ -252,12 +267,12 @@ pub(crate) fn gfx_target_from_amd_marketing_name(name: &str) -> Option<&'static } #[derive(Debug, Clone, Copy)] -struct AmdMarketingGfxTarget { - pattern: &'static str, - gfx_target: &'static str, +pub(crate) struct AmdMarketingGfxTarget { + pub(crate) pattern: &'static str, + pub(crate) gfx_target: &'static str, } -const AMD_MARKETING_GFX_TARGETS: &[AmdMarketingGfxTarget] = &[ +pub(crate) const AMD_MARKETING_GFX_TARGETS: &[AmdMarketingGfxTarget] = &[ // RDNA4 discrete. AmdMarketingGfxTarget { pattern: "ai pro r9700", @@ -406,6 +421,17 @@ const AMD_MARKETING_GFX_TARGETS: &[AmdMarketingGfxTarget] = &[ pattern: "8040s", gfx_target: "gfx1151", }, + // The codename and the product line, as `lspci` and `rocminfo` print them. + // There is no "8045S": the three Strix Halo SKU names are 8060S, 8050S and + // 8040S (the Ryzen AI Max PRO 380's), so this table never carried one. + AmdMarketingGfxTarget { + pattern: "strix halo", + gfx_target: "gfx1151", + }, + AmdMarketingGfxTarget { + pattern: "ryzen ai max", + gfx_target: "gfx1151", + }, // RDNA3.5 APUs. AmdMarketingGfxTarget { pattern: "890m", @@ -415,6 +441,10 @@ const AMD_MARKETING_GFX_TARGETS: &[AmdMarketingGfxTarget] = &[ pattern: "880m", gfx_target: "gfx1150", }, + AmdMarketingGfxTarget { + pattern: "strix point", + gfx_target: "gfx1150", + }, AmdMarketingGfxTarget { pattern: "860m", gfx_target: "gfx1152", @@ -423,6 +453,13 @@ const AMD_MARKETING_GFX_TARGETS: &[AmdMarketingGfxTarget] = &[ pattern: "840m", gfx_target: "gfx1152", }, + // Krackan Point is gfx1152, as its device id (`1114`) and the 860M/840M it + // sells as both say. gfx1150 and gfx1152 are separate TheRock families, so + // calling it gfx1150 selects the wrong runtime build. + AmdMarketingGfxTarget { + pattern: "krackan", + gfx_target: "gfx1152", + }, AmdMarketingGfxTarget { pattern: "820m", gfx_target: "gfx1153", @@ -440,6 +477,32 @@ const AMD_MARKETING_GFX_TARGETS: &[AmdMarketingGfxTarget] = &[ pattern: "740m", gfx_target: "gfx1103", }, + // `lspci` prints the Phoenix codename with a die number (`Phoenix1`, + // `Phoenix3`), which is a different token from `phoenix`. + AmdMarketingGfxTarget { + pattern: "phoenix", + gfx_target: "gfx1103", + }, + AmdMarketingGfxTarget { + pattern: "phoenix1", + gfx_target: "gfx1103", + }, + AmdMarketingGfxTarget { + pattern: "phoenix2", + gfx_target: "gfx1103", + }, + AmdMarketingGfxTarget { + pattern: "phoenix3", + gfx_target: "gfx1103", + }, + AmdMarketingGfxTarget { + pattern: "phoenix4", + gfx_target: "gfx1103", + }, + AmdMarketingGfxTarget { + pattern: "hawk point", + gfx_target: "gfx1103", + }, // RDNA2 APUs. AmdMarketingGfxTarget { pattern: "680m", @@ -449,10 +512,26 @@ const AMD_MARKETING_GFX_TARGETS: &[AmdMarketingGfxTarget] = &[ pattern: "660m", gfx_target: "gfx1035", }, + AmdMarketingGfxTarget { + pattern: "rembrandt", + gfx_target: "gfx1035", + }, + // Mendocino (Ryzen 7020 series) sells as a Radeon 610M too, which the row + // below answers as gfx1036; only the codename tells the two apart, so it is + // listed ahead of that row for `lspci`'s sake. A Windows Mendocino, named + // only "Radeon(TM) 610M", still reads as gfx1036 by name. + AmdMarketingGfxTarget { + pattern: "mendocino", + gfx_target: "gfx1037", + }, AmdMarketingGfxTarget { pattern: "610m", gfx_target: "gfx1036", }, + AmdMarketingGfxTarget { + pattern: "raphael", + gfx_target: "gfx1036", + }, AmdMarketingGfxTarget { pattern: "steam deck", gfx_target: "gfx1033", @@ -461,6 +540,37 @@ const AMD_MARKETING_GFX_TARGETS: &[AmdMarketingGfxTarget] = &[ pattern: "van gogh", gfx_target: "gfx1033", }, + // `lspci` spells it as one word, and the Steam Deck's own display name is + // the part number rather than any Radeon model. + AmdMarketingGfxTarget { + pattern: "vangogh", + gfx_target: "gfx1033", + }, + AmdMarketingGfxTarget { + pattern: "custom gpu 0405", + gfx_target: "gfx1033", + }, + // Vega iGPUs in Ryzen 4000/5000/7030 parts. They sell as a bare "AMD + // Radeon Graphics", so the codename `lspci` prints is the only name that + // identifies them. gfx90c has no ROCm build (`gfx_target_has_no_rocm_build`), + // which is why the install-side probes rank a name answering it below every + // other recognised adapter. + AmdMarketingGfxTarget { + pattern: "renoir", + gfx_target: "gfx90c", + }, + AmdMarketingGfxTarget { + pattern: "cezanne", + gfx_target: "gfx90c", + }, + AmdMarketingGfxTarget { + pattern: "lucienne", + gfx_target: "gfx90c", + }, + AmdMarketingGfxTarget { + pattern: "barcelo", + gfx_target: "gfx90c", + }, ]; fn normalize_marketing_name_for_match(value: &str) -> String { @@ -816,7 +926,8 @@ mod tests { /// Vega iGPU device ids that matter to `info-20-gpu-no-rocm-support`. /// /// This is the only source of a gfx target for those parts on a Windows - /// host with no HIP SDK: `classify_amd_marketing_name` has no Vega arm, and + /// host with no HIP SDK: Windows names these iGPUs only "AMD Radeon(TM) + /// Graphics", which no row of the name table resolves, and /// `hipInfo` is in the SDK that is not installed. Without it the ticket's /// 4700U laptop reported an empty target, and the check that exists for it /// could not fire on the machine it was written for. diff --git a/crates/rocm-core/src/host_gpu/windows_inventory.rs b/crates/rocm-core/src/host_gpu/windows_inventory.rs index 3a47d86747..73ac679498 100644 --- a/crates/rocm-core/src/host_gpu/windows_inventory.rs +++ b/crates/rocm-core/src/host_gpu/windows_inventory.rs @@ -236,16 +236,22 @@ impl WindowsExamineInventory { .iter() .find(|display| pci_target(display).is_some_and(|t| !gfx_target_has_no_rocm_build(t))) .or_else(|| { - self.displays - .iter() - .find(|display| gfx_target_from_amd_marketing_name(&display.name).is_some()) + self.displays.iter().find(|display| { + gfx_target_from_amd_marketing_name(&display.name) + .is_some_and(|t| !gfx_target_has_no_rocm_build(t)) + }) }) // A recognized-but-unsupported iGPU (gfx90c) ranks below every - // other recognized adapter, so it cannot hide a discrete card. + // other recognized adapter, so it cannot hide a discrete card -- + // but above an adapter nothing recognizes, whether the iGPU was + // recognized by its device id or by its name. That is the order + // `parse_windows_display_gfx_target` takes, so the adapter named + // here is the one whose target is reported. .or_else(|| { - self.displays - .iter() - .find(|display| pci_target(display).is_some()) + self.displays.iter().find(|display| { + pci_target(display).is_some() + || gfx_target_from_amd_marketing_name(&display.name).is_some() + }) }) .or_else(|| { self.displays @@ -929,6 +935,83 @@ Class Name: Display assert!(inventory.amd_display_driver_info().is_some()); } + /// A gfx90c iGPU recognised by its *name* must not hide a discrete card + /// either. + /// + /// The name table now answers gfx90c for the Vega iGPUs' codenames, which + /// is what `examine` needs; on the install side that answer has to rank + /// where the PCI-id answer for the same iGPU already does, below every + /// other recognised adapter, or the first adapter's name would decide the + /// install family for the whole host. + #[test] + fn windows_display_parser_does_not_let_a_named_vega_igpu_hide_a_discrete_card() { + let text = "AMD Radeon(TM) Graphics (Renoir)\t\n\ + AMD Radeon RX 6800M\tPCI\\VEN_1002&DEV_9999"; + assert_eq!( + parse_windows_display_gfx_target(text), + Some("gfx1031".to_owned()) + ); + let text = "AMD Radeon(TM) Graphics (Renoir)\t"; + assert_eq!( + parse_windows_display_gfx_target(text), + Some("gfx90c".to_owned()), + "alone, the iGPU is still reported as gfx90c" + ); + + let inventory = WindowsExamineInventory { + displays: vec![ + WindowsDisplayAdapter { + name: "AMD Radeon(TM) Graphics (Renoir)".to_owned(), + driver_version: None, + driver_date: None, + pnp_device_id: None, + }, + WindowsDisplayAdapter { + name: "AMD Radeon RX 6800M".to_owned(), + driver_version: None, + driver_date: None, + pnp_device_id: None, + }, + ], + ..WindowsExamineInventory::default() + }; + assert_eq!( + inventory.amd_display_name().as_deref(), + Some("AMD Radeon RX 6800M") + ); + } + + /// The other half of the same ranking: a gfx90c iGPU recognised by its + /// *name* still outranks an adapter nothing recognises, on both sides. + /// + /// `parse_windows_display_gfx_target` reports gfx90c for this host, so the + /// adapter `preferred_amd_display` names must be the iGPU the target came + /// from, not whichever unrecognised adapter happens to be listed first. + #[test] + fn a_named_vega_igpu_outranks_an_unrecognised_adapter_in_both_install_probes() { + let unrecognised = "AMD Radeon Pro Unlisted"; + let igpu = "AMD Radeon(TM) Graphics (Renoir)"; + assert_eq!(gfx_target_from_amd_marketing_name(unrecognised), None); + assert_eq!( + parse_windows_display_gfx_target(&format!("{unrecognised}\t\n{igpu}\t")), + Some("gfx90c".to_owned()) + ); + + let inventory = WindowsExamineInventory { + displays: [unrecognised, igpu] + .into_iter() + .map(|name| WindowsDisplayAdapter { + name: name.to_owned(), + driver_version: None, + driver_date: None, + pnp_device_id: None, + }) + .collect(), + ..WindowsExamineInventory::default() + }; + assert_eq!(inventory.amd_display_name().as_deref(), Some(igpu)); + } + #[test] fn a_complete_first_source_needs_no_second_query_and_none_repeats_one() { let mut complete = CountingSources { diff --git a/docs/architecture.md b/docs/architecture.md index d66cfbc0da..baee506c8b 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -196,7 +196,7 @@ Already-extracted subsystem modules, following the same full-domain-extraction p - [`download.rs`](../crates/rocm-core/src/download.rs) — streaming file downloads with resume and retry: `DownloadRequest`/`DownloadOutcome`, `download_file_streaming(_with_progress)` and the simpler `download_file_to_path(_with_progress)`, `Backoff`, and the retry policy (`DOWNLOAD_MAX_ATTEMPTS`). A download streams to a per-process `.part-` sibling, resumes with a `Range` request, and enforces the caller's size limit, expected length and SHA-256 digest before publishing the file. It uses `ureq`, not the loopback client in [`net.rs`](../crates/rocm-core/src/net.rs), and depends only on [`disk_space.rs`](../crates/rocm-core/src/disk_space.rs). The module is private and [`lib.rs`](../crates/rocm-core/src/lib.rs) re-exports its public names. - [`endpoint_key.rs`](../crates/rocm-core/src/endpoint_key.rs) — (ROCMAI-662) the bearer key that guards a managed service's endpoint: the CSPRNG-drawn generator and its header-safety check. Split out of [`service_identity.rs`](../crates/rocm-core/src/service_identity.rs): an id and its endpoint key are made by unrelated mechanisms (readable/timestamp-based vs. CSPRNG) and meet only at the filesystem layer, where the key's sidecar is named after the id — not a reason to keep the two in one module. - [`endpoint_readiness.rs`](../crates/rocm-core/src/endpoint_readiness.rs) — how far along a managed service's OpenAI-compatible endpoint is: `managed_service_endpoint_readiness` and `EndpointReadiness` (unreachable, listing the model, or serving a real single-token chat request), the lower-level `openai_chat_completion_probe`/`openai_chat_completion_status`/`openai_models_endpoint_has_model`, the shared inference-verified latch the engine adapters call (`engine_state_inference_verified`, `merge_json_state_file`) and the model-reference matching behind the model-listing check. It depends on [`net.rs`](../crates/rocm-core/src/net.rs), [`service_record.rs`](../crates/rocm-core/src/service_record.rs) and [`model_catalog.rs`](../crates/rocm-core/src/model_catalog.rs); [`net.rs`](../crates/rocm-core/src/net.rs) and [`service_record.rs`](../crates/rocm-core/src/service_record.rs) do not depend on it. The module is private and [`lib.rs`](../crates/rocm-core/src/lib.rs) re-exports its public names, so callers keep using `rocm_core::` paths. -- [`examine.rs`](../crates/rocm-core/src/examine.rs) — the host examination probe: the `Examination` document and its types, and `Examination::probe`, which runs the probes in order. The probes live in child modules under `examine/`, with `examine.rs` kept as the module root so the [`examine_driver_proptests.rs`](../crates/rocm-core/src/examine_driver_proptests.rs) `#[path]` attribute and the links to it stay valid: [`linux_gpus.rs`](../crates/rocm-core/src/examine/linux_gpus.rs) (Linux GPU enumeration from `lspci`, `rocminfo` and the kernel topology, with the marketing-name and APU classification they share), [`linux_driver.rs`](../crates/rocm-core/src/examine/linux_driver.rs) (kernel modules, device-node access and Secure Boot), [`rocm_install_probe.rs`](../crates/rocm-core/src/examine/rocm_install_probe.rs) (how ROCm was installed and which repository files configure it), [`library_copies.rs`](../crates/rocm-core/src/examine/library_copies.rs) (the loader library-copy search for comgr and the HIP runtime, and `comgr_matches_runtime`, which `diagnose/engine.rs` reads), [`framework.rs`](../crates/rocm-core/src/examine/framework.rs) (the PyTorch and llama.cpp probes), [`host_env.rs`](../crates/rocm-core/src/examine/host_env.rs) (the OS and distro, CPU, tracked environment variables, shared memory, container check and kernel log), [`wsl.rs`](../crates/rocm-core/src/examine/wsl.rs) (the WSL probe, including the remote probe that reads a named distribution from the Windows host, and the distro-floor check) and [`windows.rs`](../crates/rocm-core/src/examine/windows.rs) (the native Windows probes: the GPU query, the HIP SDK, the Adrenalin driver and the MSVC redistributable, plus `parse_dotted_version`, which `diagnose/windows.rs` reads). +- [`examine.rs`](../crates/rocm-core/src/examine.rs) — the host examination probe: the `Examination` document and its types, and `Examination::probe`, which runs the probes in order. The probes live in child modules under `examine/`, with `examine.rs` kept as the module root so the `#[path]` attributes of [`examine_driver_proptests.rs`](../crates/rocm-core/src/examine_driver_proptests.rs) and [`examine_gpu_proptests.rs`](../crates/rocm-core/src/examine_gpu_proptests.rs) (the GPU enumeration and APU classification properties) and the links to them stay valid: [`linux_gpus.rs`](../crates/rocm-core/src/examine/linux_gpus.rs) (Linux GPU enumeration from `lspci`, `rocminfo` and the kernel topology, with the marketing-name and APU classification they share), [`linux_driver.rs`](../crates/rocm-core/src/examine/linux_driver.rs) (kernel modules, device-node access and Secure Boot), [`rocm_install_probe.rs`](../crates/rocm-core/src/examine/rocm_install_probe.rs) (how ROCm was installed and which repository files configure it), [`library_copies.rs`](../crates/rocm-core/src/examine/library_copies.rs) (the loader library-copy search for comgr and the HIP runtime, and `comgr_matches_runtime`, which `diagnose/engine.rs` reads), [`framework.rs`](../crates/rocm-core/src/examine/framework.rs) (the PyTorch and llama.cpp probes), [`host_env.rs`](../crates/rocm-core/src/examine/host_env.rs) (the OS and distro, CPU, tracked environment variables, shared memory, container check and kernel log), [`wsl.rs`](../crates/rocm-core/src/examine/wsl.rs) (the WSL probe, including the remote probe that reads a named distribution from the Windows host, and the distro-floor check) and [`windows.rs`](../crates/rocm-core/src/examine/windows.rs) (the native Windows probes: the GPU query, the HIP SDK, the Adrenalin driver and the MSVC redistributable, plus `parse_dotted_version`, which `diagnose/windows.rs` reads). - [`gfx_target.rs`](../crates/rocm-core/src/gfx_target.rs) — gfx-target identification and the TheRock package-family tables, split out of [`host_gpu.rs`](../crates/rocm-core/src/host_gpu.rs) so [`managed_runtime.rs`](../crates/rocm-core/src/managed_runtime.rs) can read the family mapping without importing the module that probes the host: `extract_first_gfx_token`, `normalize_therock_family`/`known_therock_families`, `gfx_target_has_no_rocm_build`, the AMD PCI-device-id and marketing-name tables behind `gfx_target_from_pnp_device_id`, and the parsers for the Windows display text and the kernel's gfx-target version. The module is private; `lib.rs` re-exports four of the public names (all but `gfx_target_from_pnp_device_id`) and `host_gpu.rs`, as before, re-exports all five. - [`gguf.rs`](../crates/rocm-core/src/gguf.rs) — reads the shape of a GGUF model (layers, KV heads, head width, parameter count, data offset) from its header over any `Read + Seek`, never touching the weights. - [`gguf_fit.rs`](../crates/rocm-core/src/gguf_fit.rs) — pure arithmetic over that header: weights + KV cache at a context size and KV type + compute buffer + runtime reserve against dedicated VRAM, the largest context that fits, and what would make a model that does not fit, fit. diff --git a/docs/host-probing.md b/docs/host-probing.md index 057f5517e0..c09b454b68 100644 --- a/docs/host-probing.md +++ b/docs/host-probing.md @@ -21,7 +21,7 @@ For each pair, the proposed survivor and the biggest difference a user can see t | [Kernel version](#kernel-version) | `detect_kernel_version`, called by `probe_os` for `kernel_release` on Linux (the timeout is open) | where `uname` is slow or fails, `kernel:` and `kernel_release` can disagree (UNCONFIRMED); on Windows `kernel_release` is always empty | | [CPU model](#cpu-model) | one reader in `os_facts` returning model and vendor, called by `probe_cpu_linux` and `probe_cpu_windows` | on WSL the text shows the CPU while `cpu_model` is empty and `cpu_vendor` is `unknown`; the change leaves that as it is (option (a)) | | [Windows GPU query](#windows-gpu-query) | the summary stack's inventory, with `probe_gpus_windows` building `gpus[]` from it and NVIDIA kept in the JSON | an NVIDIA-only host reads "AMD GPU not detected yet" in the text beside `has_nvidia_gpu: true` in the JSON | -| [Marketing name to gfx target](#marketing-name-to-gfx-target) | the `AMD_MARKETING_GFX_TARGETS` table, gaining the codenames `classify_amd_marketing_name` knows (finish [#521](https://github.com/ROCm/rocm-cli/pull/521)) | on Windows without a HIP SDK an RX 7900 XTX has an empty `gpus[].gfx_target` while `summary.detected_gfx_target` is `gfx1100` | +| [Marketing name to gfx target](#marketing-name-to-gfx-target) | delivered: the `AMD_MARKETING_GFX_TARGETS` table, with the codenames `classify_amd_marketing_name` knew, and `APU_KEYWORDS` gone | before the change, on Windows without a HIP SDK an RX 7900 XTX had an empty `gpus[].gfx_target` while `summary.detected_gfx_target` was `gfx1100`; now both read the one table | | [PATH lookup](#path-lookup) | delivered: the behaviour of `tool_on_path`, housed in `probe_exec.rs` under the name `which`; an empty `PATH` entry is skipped | before the change, an empty `PATH` entry or a `PATH` that is not UTF-8 changed which tools read as present (for example `wsl.rocminfo`); now there is one answer, and the differences it moved are listed under "PATH lookup" | | [Subprocess runners](#subprocess-runners) | `run_with_timeout` as the one bounded executor, with `CommandCapture` extended by a typed outcome | a probe behind a hung grandchild returns at its deadline, and a successful capture can lose output a descendant writes more than 200 ms after the child exits | @@ -123,7 +123,7 @@ Hosts: this pair is native Windows only; on WSL `gpus[]` is already empty (see t ## Marketing name to gfx target -Verdict: confirmed duplicate, but not equivalent. The two cover different names. +Verdict: confirmed duplicate, but not equivalent, and now converged (see "Delivered" at the end of this section). The comparison that follows is how the two copies differed before that change. | | Summary | Examination | |---|---|---| @@ -137,7 +137,13 @@ The two agree where they overlap on `880m`, `780m` and `8060s`. They disagree on What a user can see, on Windows without a HIP SDK: an RX 7900 XTX is not in the PCI-id table and not in `classify_amd_marketing_name`, so `gpus[].gfx_target` is empty while `summary.detected_gfx_target` in the same JSON document is `gfx1100`, from the name table. (Derived from the code; `hipInfo` fills the examination's value in when an SDK is installed.) On native Linux only the examination's `lspci` path can show a name-derived empty target (see the host table). -Proposal: the table survives. It is pure, data-driven and already the superset for target names. It gains the codenames `classify_amd_marketing_name` knows, and the APU verdict is derived from the target, falling back to a codename list only while some codename still has no target. [#521](https://github.com/ROCm/rocm-cli/pull/521) already proposes exactly this convergence and is open and conflicting with `main`; the cheapest route is to finish and rebase it rather than start a second change. Its Linux device-id work is a separate concern from this pair. Hosts: the table is pure data, so its test needs no host, but the two callers of `classify_amd_marketing_name` differ in what happens around it (on Windows `parse_gpus_windows_output` tries the PNP id first; on native Linux `probe_gpus_lspci` calls it directly, and the later `probe_gpus_after_lspci` steps (the kernel topology, `rocminfo` and the sysfs fallback) may also set a target, which this note did not trace; WSL has no name lookup in the examination at all), so the characterisation test names each caller: a fixture adapter list through `parse_gpus_windows_output`, which a Linux unit test can drive, and a fixture `lspci` output through the Linux scan. +Proposal, as written before the change (see "Delivered" below for what landed): the table survives. It is pure, data-driven and already the superset for target names. It gains the codenames `classify_amd_marketing_name` knows, and the APU verdict is derived from the target, falling back to a codename list only while some codename still has no target. [#521](https://github.com/ROCm/rocm-cli/pull/521) already proposes exactly this convergence and is open and conflicting with `main`; the cheapest route is to finish and rebase it rather than start a second change. Its Linux device-id work is a separate concern from this pair. Hosts: the table is pure data, so its test needs no host, but the two callers of `classify_amd_marketing_name` differ in what happens around it (on Windows `parse_gpus_windows_output` tries the PNP id first; on native Linux `probe_gpus_lspci` calls it directly, and the later `probe_gpus_after_lspci` steps (the kernel topology, `rocminfo` and the sysfs fallback) may also set a target, which this note did not trace; WSL has no name lookup in the examination at all), so the characterisation test names each caller: a fixture adapter list through `parse_gpus_windows_output`, which a Linux unit test can drive, and a fixture `lspci` output through the Linux scan. + +### Delivered + +`classify_amd_marketing_name` now asks `gfx_target_from_amd_marketing_name` and derives the APU verdict from the target with `gfx_is_apu_family`; `APU_KEYWORDS` is gone. The table gained the codenames and product lines the examination knew (Strix Halo, Ryzen AI Max, Strix Point, Krackan, Phoenix and its die-numbered `lspci` spellings, Hawk Point, Rembrandt, Raphael, Van Gogh, Renoir, Cezanne, Lucienne, Barcelo) plus Mendocino (`gfx1037`), so a codename now yields a target rather than "is an APU" with an empty one. Krackan is `gfx1152`, agreeing with device id `1114` and the 860M and 840M rows. The `8045S` the examination matched is not a product name (the Strix Halo names are 8060S, 8050S and 8040S), and the table never carried it. On native Linux the `lspci` scan now asks the `[1002:xxxx]` device id first and the name only when the id is not in the table, as the Windows probe already did. On the install side, a gfx90c recognised by its codename ranks where the same iGPU recognised by its device id already did, below every other recognised adapter, in both `parse_windows_display_gfx_target` and `preferred_amd_display`. + +What a user can see: the RX 7900 XTX case above now reports `gfx1100` in `gpus[]`; a Krackan Point iGPU reports `gfx1152` where the examination said `gfx1150`; a Renoir, Cezanne, Lucienne, Barcelo or Mendocino row named only by its codename now carries a target; and a Mendocino iGPU, which used to carry no target and `is_apu: false`, reports `gfx1037` and `is_apu: true`. The tests are `examine_names_a_gpu_by_its_codename_from_the_shared_table` and `lspci_scan_prefers_the_device_id_to_the_name` in [linux_gpus.rs](../crates/rocm-core/src/examine/linux_gpus.rs), the property harness in [examine_gpu_proptests.rs](../crates/rocm-core/src/examine_gpu_proptests.rs), and scenario `examine-tells-integrated-gpus-from-discrete-ones` in [examine.feature](../tests/e2e-cucumber/features/examine.feature). ## PATH lookup @@ -207,7 +213,7 @@ One pair per change. Each begins with a commit that adds a characterisation test 1. PATH lookup (delivered: see "Decided and delivered" under "PATH lookup"). Smallest, and it settles where `tool_path_candidates` lives before the runner work needs it. It is not output-preserving: the differences listed under "PATH lookup" change which tools are reported as present, and its PR text lists each one as intentional, with the characterisation test that pins it. Hosts covered: Linux, which is also WSL's code path, for the empty entry, the non-UTF-8 `PATH` (set through an `OsString` environment value) and the current-directory file; Windows through an injectable resolver for the `PATHEXT` spellings, the System32 candidates and the `;` splitting, since the Linux lane cannot run them. 2. Kernel version. A single command; settles the timeout and the empty-versus-`None` question. Hosts covered as set out under "Kernel version". 3. CPU model. Adds the model-and-vendor reader; needs the Windows inventory row, which already exists. Its PR says which WSL option under "CPU model" it takes, (a) or (b); a PR that does not is not ready. -4. Marketing name to gfx target, by finishing [#521](https://github.com/ROCm/rocm-cli/pull/521). It must land before the next item, so that `gpus[]` is not rebuilt on top of two name tables. +4. Marketing name to gfx target (delivered: see "Delivered" under "Marketing name to gfx target"). It had to land before the next item, so that `gpus[]` is not rebuilt on top of two name tables. 5. Windows GPU query. The riskiest: it needs an answer on NVIDIA and on adapter names, and a Windows lane. Scenario `examine-windows-driver-version-date-and-age` already runs there; a new scenario should compare the text's `detected_gfx_target` with the JSON `gpus[]` on a named adapter. Native Windows only; no scenario can run this on the Linux or WSL lanes. 6. Subprocess runners. Last, because items 1 to 5 reduce the number of call sites, but independent: the bounded-join fix in [#623](https://github.com/ROCm/rocm-cli/issues/623) does not wait for it and can land at any time. It has two intended differences, and the second is easy to miss because it needs no timeout: a probe behind a hung grandchild returns at its deadline instead of late (with possibly different output kept after a timeout), and a successful capture can keep less, because a descendant that writes more than `DRAIN_GRACE` (200 ms) after the child exited loses that later output where the older helpers waited for EOF. Each of the other successful-run differences listed under "Subprocess runners" needs its own line in the PR text. The characterisation test for this item comes first and must include late descendant output: a child that prints `early`, exits 0 and leaves a descendant that prints `late` after about 0.5 s, run through each of `run` (over `run_raw`, which `run_utf16le` shares), `capture_optional_command*` and `run_with_timeout`, pinning that the first two return both lines and `run_with_timeout` only `early`. Hosts covered: Linux and WSL with shell fixtures (the same shape the `process.rs` tests already use for the grace); `capture_diagnostic_command` and the Windows spellings are Windows only, so on the Windows job they get the injectable-function test of the adapter's result translation instead. UNCONFIRMED whether a Windows child can be arranged to leave a late-writing descendant in the test harness; if not, the PR says that path is untested and why. diff --git a/xtask/src/ascii_output_contract.rs b/xtask/src/ascii_output_contract.rs index 18827f11d6..d75f828cd5 100644 --- a/xtask/src/ascii_output_contract.rs +++ b/xtask/src/ascii_output_contract.rs @@ -205,11 +205,6 @@ mod tests { "\\u{feff}", STRIPPED_FROM_INPUT, ), - ( - "crates/rocm-core/src/examine/linux_gpus.rs", - "(\\u{2122})", - STRIPPED_FROM_INPUT, - ), ("crates/rocm-dash-core/src/state.rs", "⋯", JOB_GLYPH), ("crates/rocm-dash-core/src/state.rs", "✓", JOB_GLYPH), ("crates/rocm-dash-core/src/state.rs", "✗", JOB_GLYPH), From 8b69b0a90c5ee57355d6110ed9c14341822f8b75 Mon Sep 17 00:00:00 2001 From: Roman Inflianskas Date: Tue, 10 Nov 2026 14:16:00 +0200 Subject: [PATCH 2/3] test(e2e): cover how examine tells integrated GPUs from discrete ones Three simulated-host scenarios that run on every Linux lane: - examine-43: five AMD GPUs on a stand-in lspci, each answered by a different door (marketing name, device id alone, device id ahead of a codename, codename alone). - examine-44: a Raven Ridge iGPU and an unnamed card whose PCI rows resolve nothing; the KFD topology's targets must make one integrated and the other discrete. - examine-45: no PCI rows at all, as in a container with no pciutils; two topology-only GPUs must still be classified. docs/testing.md describes them and what stays at unit level, and docs/manual-testing.md says what to check on real hardware, including the Raven/Picasso gap before amdgpu loads. Signed-off-by: Roman Inflianskas --- docs/manual-testing.md | 28 ++ docs/testing.md | 62 ++++ tests/e2e-cucumber/features/examine.feature | 54 ++++ tests/e2e-cucumber/tests/e2e/examine_steps.rs | 279 ++++++++++++++++++ 4 files changed, 423 insertions(+) diff --git a/docs/manual-testing.md b/docs/manual-testing.md index aed52b395e..10fe10dd67 100644 --- a/docs/manual-testing.md +++ b/docs/manual-testing.md @@ -321,6 +321,34 @@ On Ubuntu 22.04, 24.04 and 26.04 the apt source line names the matching suite first prepare command prints a "from a previous installation" warning; confirm nothing is removed (`dpkg -s amdgpu-dkms` still reports installed afterwards). +## 2c. Integrated or discrete + +On a machine with an AMD integrated GPU, check what `rocm examine --json` +reports for it under `gpus`: + +```bash +rocm examine --framework skip --json +``` + +Expected result: + +- Each AMD GPU carries a `gfx_target`, and an integrated one carries + `is_apu: true`. On a Ryzen 7000 or later desktop the iGPU is `gfx1036`; on a + Ryzen 6000 laptop `gfx1035`; on a Krackan Point laptop (Radeon 860M or 840M) + `gfx1152`, never `gfx1150`; on a Ryzen 4000 or 5000 machine `gfx90c`; on a + Ryzen 2000 or 3000 laptop (Raven Ridge or Picasso) `gfx902` or `gfx909`, + which today only the kernel reports, so an `is_apu: false` there means the + topology's target did not reach the verdict. With `amdgpu` not loaded such a + laptop still reports an empty `gfx_target` and `is_apu: false`: no lookup + table names Raven or Picasso, which is a known gap rather than a regression. +- A discrete card beside it carries `is_apu: false`, and `has_apu` and + `has_discrete_amd` are both `true`. An iGPU reported with `is_apu: false`, or + an iGPU-only machine with `has_discrete_amd: true`, is a regression unless + the GPU's `gfx_target` is empty (an unidentified row is reported as discrete + on purpose). +- On a machine whose only GPU is the iGPU, `rocm serve` does not warn about low + VRAM. + ## 3. Lemonade GPU Verification Lemonade is the default local assistant/server engine. Serve a small assistant diff --git a/docs/testing.md b/docs/testing.md index ec502dd602..3f642a8816 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -3666,6 +3666,68 @@ anything: an absent node (EAI-8457 states the scenario does not apply with no there ranks from the sysfs VRAM rows alone. `kfd_readable`'s doc comment carries the same statement next to the code. +## Integrated Or Discrete GPU + +`rocm examine` reports each AMD GPU's `gfx_target` and `is_apu`, and two things +a user sees follow from `is_apu`: `rocm serve` withholds its low-VRAM warning +on a single-APU host, whose "VRAM" is a BIOS carve-out of system RAM, and +`rocm diagnose` raises `fix-9-igpu-dgpu` only when a host has both an +integrated and a discrete AMD GPU. + +`examine-43` in [examine.feature](../tests/e2e-cucumber/features/examine.feature) +covers the PCI scan end to end on every Linux lane, GPU or not. It puts a +stand-in `lspci` and an empty `rocminfo` on `PATH` and points the simulated host +root at an empty tree, so the five planted rows are the only GPUs. The RX 7900 +XTX is classified by its marketing name, the Raphael iGPU by its device id alone +(the `Device` row `lspci` prints when `pci.ids` has no name), Krackan Point and +Renoir by their device ids (the scan asks the id first, so their codenames are +not consulted), and Mendocino by its codename alone. The codename rows for the +other Vega and RDNA iGPUs are covered at unit level +(`examine_names_a_gpu_by_its_codename_from_the_shared_table`). + +```bash +RUSTC_WRAPPER= cargo xtask e2e -- -n 'examine-43 ' +``` + +`examine-44` covers the kernel-topology route the same way, for an integrated +GPU no lookup table names. Its simulated host root carries a KFD topology node +per card, and its two `lspci` rows resolve no target (a Raven Ridge iGPU and an +unnamed `Device` row), so the gfx902 and gfx1102 the topology reports are the +only evidence. The Raven Ridge iGPU must come out integrated and the other card +discrete: + +```bash +RUSTC_WRAPPER= cargo xtask e2e -- -n 'examine-44 ' +``` + +`examine-45` drops the PCI scan altogether, as an ordinary ROCm container with +no `pciutils` does: the stand-in `lspci` lists nothing and neither topology node +states a PCI address, so both GPUs are listed from the topology alone and must +still come out integrated (gfx1151) and discrete (gfx1201). The sysfs fallback +that runs when even the topology is unreadable lists a single GPU, so it cannot +open the iGPU+dGPU gate and is covered at unit level only +(`a_sysfs_fallback_gpu_carries_its_targets_verdict_where_one_is_known`); so is +the `rocminfo` marketing-name fallback, which #393 keeps out of reach end to +end. That fallback decides a verdict only when one AMD GPU and one agent are +listed: `rocminfo` agents are paired with GPUs by position (#508), and on a +multi-GPU host a position is no evidence of which card a name belongs to, so +the verdict stays unknown there +(`a_kernel_topology_gpu_is_classified_from_the_evidence_there_is`). + +What `rocminfo` and `hipInfo` add is covered one level down, in +[examine_gpu_proptests.rs](../crates/rocm-core/src/examine_gpu_proptests.rs): +a target nothing classifies must leave an earlier verdict standing +(`gfx_apu_verdict` answers `None` for it). That rule cannot be reached end to +end today, because a real `rocminfo` prints ISA `Name:` lines that make the +parser drop every agent (#393). The Windows display property draws adapters +that print only the generic `AMD Radeon(TM) Graphics`, so a display probe that +stopped reading the PNP device id fails it. The property harness asserts floors +on how much of each corpus its generators reach, and prints the measured rates: + +```bash +cargo test -p rocm-core --lib -- examine::gpu_proptests --nocapture +``` + ## Windows Runtime Preflight On Windows, `rocm diagnose` compares the Visual C++ runtime in `System32` diff --git a/tests/e2e-cucumber/features/examine.feature b/tests/e2e-cucumber/features/examine.feature index f5237db55f..fa951e4638 100644 --- a/tests/e2e-cucumber/features/examine.feature +++ b/tests/e2e-cucumber/features/examine.feature @@ -639,3 +639,57 @@ Feature: GPU detection and system inspection When the user inspects that machine in machine-readable form Then the inspection reports kfd as loaded And the inspection reports amdgpu as loaded + + # Whether a GPU is integrated decides two things a user sees: `rocm serve` + # withholds its low-VRAM warning on an APU (whose "VRAM" is a BIOS carve-out + # of system RAM), and `rocm diagnose` raises `fix-9-igpu-dgpu` only on a host + # with both an integrated and a discrete AMD GPU. Every AMD GPU on this bus + # used to be misreported: the PCI scan ignored the `[1002:xxxx]` device id, + # so a GPU `pci.ids` cannot name ("Device") got no architecture; `examine` + # kept its own name table, which called Krackan Point gfx1150 (it is gfx1152) + # and knew the Renoir codename only as "some APU" and Mendocino not at all; + # and gfx1037 was not classified as integrated. + # + # The host is simulated, so this runs on every Linux lane, GPU or not. A + # stand-in `lspci` on PATH lists the five GPUs as `lspci -nn -D` prints them, + # a stand-in `rocminfo` lists no agents, and the simulated host root has no + # kernel topology, so the PCI scan alone names every card. (What `rocminfo` + # adds is covered one level down: on a real host its agents are dropped by + # ROCm/rocm-cli#393, so the folds it feeds cannot be reached from here.) + @id:examine-tells-integrated-gpus-from-discrete-ones @requires-os:linux + Scenario: examine-43 - The inspection tells integrated GPUs from discrete ones by the part they are + Given a machine whose PCI bus lists a Radeon RX 7900 XTX and four integrated AMD GPUs + When the user inspects that machine in machine-readable form + Then the inspection names each GPU's architecture and reports only the RX 7900 XTX as discrete + And the inspection reports the machine as having both an integrated and a discrete AMD GPU + + # A Raven Ridge or Picasso integrated GPU is named by no lookup table: its + # device id is not in the device-id table and `pci.ids`' "Raven Ridge" is not + # in the name table. Its architecture reaches the CLI only from the kernel's + # GPU topology, which reports gfx902. The report used to take that + # architecture but keep the PCI scan's "not an APU" default, so a Raven + # laptop with a discrete card reported no integrated GPU and + # `fix-9-igpu-dgpu` could not fire on it. + # + # The host is simulated, so this runs on every Linux lane, GPU or not: a + # stand-in `lspci` lists the two GPUs, a stand-in `rocminfo` lists no agents, + # and the simulated host root carries a kernel topology node for each card. + @id:examine-classifies-an-igpu-from-the-kernel-topology @requires-os:linux + Scenario: examine-44 - An integrated GPU that only the kernel identifies is reported as integrated + Given a machine with a Raven Ridge integrated GPU and a discrete AMD GPU that only the kernel identifies + When the user inspects that machine in machine-readable form + Then the inspection reports the Raven Ridge GPU as an integrated gfx902 and the other card as discrete + And the inspection reports the machine as having both an integrated and a discrete AMD GPU + + # The same rule where no PCI scan answers at all: an ordinary ROCm container + # ships no `pciutils`, so the kernel's topology is the only list of GPUs. A + # card listed from the topology alone used to carry no verdict whatever its + # architecture, so such a host reported neither an integrated nor a discrete + # GPU. Simulated as `examine-44` is, with a stand-in `lspci` that lists + # nothing and no PCI address on either topology node. + @id:examine-classifies-gpus-only-the-kernel-lists @requires-os:linux + Scenario: examine-45 - GPUs that only the kernel lists are reported as integrated or discrete by their architecture + Given a machine whose only list of GPUs is the kernel's, naming a Strix Halo integrated GPU and a Radeon RX 9070 XT + When the user inspects that machine in machine-readable form + Then the inspection reports the Strix Halo GPU as integrated and the RX 9070 XT as discrete + And the inspection reports the machine as having both an integrated and a discrete AMD GPU diff --git a/tests/e2e-cucumber/tests/e2e/examine_steps.rs b/tests/e2e-cucumber/tests/e2e/examine_steps.rs index 0044e897c7..23b1458980 100644 --- a/tests/e2e-cucumber/tests/e2e/examine_steps.rs +++ b/tests/e2e-cucumber/tests/e2e/examine_steps.rs @@ -1661,6 +1661,285 @@ async fn assert_no_amdgpu_fault_evidence(world: &mut E2eWorld) { } } +/// The five AMD GPUs `examine-43` puts on the PCI bus, as `lspci -nn -D` +/// prints them: `(PCI address, gfx target, integrated, lspci line)`. +/// +/// Which door answers each row: the RX 7900 XTX its marketing name (its device +/// id is not in the device-id table); the Raphael iGPU its device id alone +/// (`pci.ids` has no name for it here); Krackan Point and Renoir their device +/// ids (`1114`, `1636`), which the scan asks before the name, so their +/// codenames are never consulted here; and Mendocino its codename alone, its +/// device id (`1506`) being in no table. So the RX 7900 XTX row fails alone if +/// the name door regresses, the Raphael row if the device-id door does, and the +/// Mendocino row if the codename rows do. The Renoir and Krackan Point codename +/// rows are covered one level down, by +/// `examine_names_a_gpu_by_its_codename_from_the_shared_table`. +const PCI_BUS_GPUS: &[(&str, &str, bool, &str)] = &[ + ( + "0000:03:00.0", + "gfx1100", + false, + "0000:03:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. \ + [AMD/ATI] Navi 31 [Radeon RX 7900 XT/7900 XTX/7900 GRE/7900M] [1002:744c] (rev c8)", + ), + ( + "0000:14:00.0", + "gfx1036", + true, + "0000:14:00.0 Display controller [0380]: Advanced Micro Devices, Inc. \ + [AMD/ATI] Device [1002:164e] (rev c1)", + ), + ( + "0000:c5:00.0", + "gfx1152", + true, + "0000:c5:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. \ + [AMD/ATI] Krackan Point [Radeon 860M] [1002:1114] (rev c1)", + ), + ( + "0000:04:00.0", + "gfx90c", + true, + "0000:04:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. \ + [AMD/ATI] Renoir [1002:1636] (rev c6)", + ), + ( + "0000:05:00.0", + "gfx1037", + true, + "0000:05:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. \ + [AMD/ATI] Mendocino [1002:1506] (rev c1)", + ), +]; + +#[given("a machine whose PCI bus lists a Radeon RX 7900 XTX and four integrated AMD GPUs")] +async fn machine_with_five_amd_gpus_on_the_bus(world: &mut E2eWorld) { + plant_pci_bus(world, PCI_BUS_GPUS, &[]); +} + +/// The two AMD GPUs `examine-44` puts on the PCI bus, in [`PCI_BUS_GPUS`]'s +/// shape, plus the KFD `gfx_target_version` the kernel topology reports for +/// each. Neither row resolves a target from its PCI line: the Raven Ridge iGPU +/// (`15dd`) is in neither lookup table, and the Navi 33 card (`7480`) is in +/// neither and has no `pci.ids` name. Only the topology names them. +const KERNEL_IDENTIFIED_GPUS: &[(&str, &str, bool, &str)] = &[ + ( + "0000:03:00.0", + "gfx902", + true, + "0000:03:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. \ + [AMD/ATI] Raven Ridge [Radeon Vega Series / Radeon Vega Mobile Series] [1002:15dd] (rev c4)", + ), + ( + "0000:01:00.0", + "gfx1102", + false, + "0000:01:00.0 Display controller [0380]: Advanced Micro Devices, Inc. \ + [AMD/ATI] Device [1002:7480] (rev c7)", + ), +]; + +/// `(PCI address, KFD gfx_target_version)` for each [`KERNEL_IDENTIFIED_GPUS`] +/// row: gfx902 packs as 90002, gfx1102 as 110002. +const KERNEL_IDENTIFIED_NODES: &[(&str, u32)] = + &[("0000:03:00.0", 90002), ("0000:01:00.0", 110_002)]; + +#[given( + "a machine with a Raven Ridge integrated GPU and a discrete AMD GPU that only the kernel identifies" +)] +async fn machine_with_kernel_identified_gpus(world: &mut E2eWorld) { + plant_pci_bus(world, KERNEL_IDENTIFIED_GPUS, KERNEL_IDENTIFIED_NODES); +} + +/// The two GPUs `examine-45`'s kernel topology lists, as `(KFD +/// gfx_target_version, gfx target, integrated)`: a Strix Halo iGPU and an RX +/// 9070 XT. Neither node states a PCI address, so the report lists both from the +/// topology alone, the way it does in a container with no `pciutils`. +const KERNEL_ONLY_GPUS: &[(u32, &str, bool)] = + &[(110_501, "gfx1151", true), (120_001, "gfx1201", false)]; + +#[given( + "a machine whose only list of GPUs is the kernel's, naming a Strix Halo integrated GPU and a Radeon RX 9070 XT" +)] +async fn machine_whose_gpus_only_the_kernel_lists(world: &mut E2eWorld) { + // An empty address plants a node with no `location_id`, which matches no + // PCI row. + let nodes: Vec<(&str, u32)> = KERNEL_ONLY_GPUS + .iter() + .map(|(version, _, _)| ("", *version)) + .collect(); + plant_pci_bus(world, &[], &nodes); +} + +#[then("the inspection reports the Strix Halo GPU as integrated and the RX 9070 XT as discrete")] +async fn assert_kernel_only_gpus_classified(world: &mut E2eWorld) { + assert_eq!(world.cli_rc, Some(0), "`rocm examine --json` failed"); + let json = parsed_json(world); + let reported: Vec<(Option<&str>, Option)> = json + .get("gpus") + .and_then(serde_json::Value::as_array) + .unwrap_or_else(|| panic!("`examine --json` has no gpus array:\n{json:#}")) + .iter() + .filter(|gpu| gpu.get("is_amd").and_then(serde_json::Value::as_bool) == Some(true)) + .map(|gpu| { + ( + gpu.get("gfx_target").and_then(serde_json::Value::as_str), + gpu.get("is_apu").and_then(serde_json::Value::as_bool), + ) + }) + .collect(); + let expected: Vec<(Option<&str>, Option)> = KERNEL_ONLY_GPUS + .iter() + .map(|(_, target, integrated)| (Some(*target), Some(*integrated))) + .collect(); + assert_eq!( + reported, expected, + "each topology GPU, in node order, with its target and packaging:\n{json:#}" + ); +} + +/// Plant a simulated host: a stand-in `lspci` listing `rows`, a stand-in +/// `rocminfo` with no agents, and a simulated host root whose kernel topology +/// holds one GPU node per `nodes` entry (none at all when `nodes` is empty). A +/// node whose address is empty states none, so it matches no PCI row. +fn plant_pci_bus(world: &mut E2eWorld, rows: &[(&str, &str, bool, &str)], nodes: &[(&str, u32)]) { + let root = world + .isolated_root + .as_ref() + .expect("no isolated root") + .path() + .to_owned(); + // A simulated host root holding only the planted topology: with no nodes, + // nothing but the PCI scan names a card; either way no real card on the + // runner can join the list. + let host_root = root.join("host-root"); + std::fs::create_dir_all(&host_root).expect("failed to create the simulated host root"); + for (index, (address, version)) in nodes.iter().enumerate() { + let node = host_root.join(format!("sys/class/kfd/kfd/topology/nodes/{}", index + 1)); + std::fs::create_dir_all(&node).expect("failed to create planted KFD node"); + if address.is_empty() { + std::fs::write( + node.join("properties"), + format!("cpu_cores_count 0\nsimd_count 120\ngfx_target_version {version}\n"), + ) + .expect("failed to write planted KFD node properties"); + continue; + } + // KFD states the address as `location_id = (bus << 8) | devfn`. + let mut parts = address.split([':', '.']); + let (_domain, bus, device, function) = ( + parts.next(), + u32::from_str_radix(parts.next().expect("bus"), 16).expect("bus"), + u32::from_str_radix(parts.next().expect("device"), 16).expect("device"), + parts + .next() + .expect("function") + .parse::() + .expect("function"), + ); + let location_id = (bus << 8) | (device << 3) | function; + std::fs::write( + node.join("properties"), + format!( + "cpu_cores_count 0\nsimd_count 120\ngfx_target_version {version}\n\ + domain 0\nlocation_id {location_id}\n" + ), + ) + .expect("failed to write planted KFD node properties"); + } + let bin = root.join("fake-bin"); + std::fs::create_dir_all(&bin).expect("failed to create the fake PATH dir"); + let listing = rows + .iter() + .map(|(_, _, _, line)| *line) + .collect::>() + .join("\n"); + rocm_core::fake_executable::write_fake_executable( + &bin.join("lspci"), + format!("#!/bin/sh\ncat <<'LSPCI'\n{listing}\nLSPCI\n"), + ) + .expect("failed to write the fake lspci"); + // A `rocminfo` that answers and lists no agents, ahead of any real one, so + // a GPU runner's own agents are not paired onto these rows. + rocm_core::fake_executable::write_fake_executable(&bin.join("rocminfo"), "#!/bin/sh\nexit 0\n") + .expect("failed to write the fake rocminfo"); + let path = std::env::join_paths(std::iter::once(bin).chain(std::env::split_paths( + &std::env::var_os("PATH").unwrap_or_default(), + ))) + .expect("PATH entries cannot hold a separator"); + world.command_env.push(("PATH", path)); + world + .command_env + .push(("ROCM_CLI_TEST_HOST_ROOT", host_root.into_os_string())); +} + +#[then("the inspection names each GPU's architecture and reports only the RX 7900 XTX as discrete")] +async fn assert_each_gpu_classified(world: &mut E2eWorld) { + assert_gpus_classified(world, PCI_BUS_GPUS); +} + +#[then( + "the inspection reports the Raven Ridge GPU as an integrated gfx902 and the other card as discrete" +)] +async fn assert_kernel_identified_gpus_classified(world: &mut E2eWorld) { + assert_gpus_classified(world, KERNEL_IDENTIFIED_GPUS); +} + +/// Each planted row is reported once, with its own target and packaging, and +/// no other AMD GPU is reported. +fn assert_gpus_classified(world: &E2eWorld, rows: &[(&str, &str, bool, &str)]) { + assert_eq!(world.cli_rc, Some(0), "`rocm examine --json` failed"); + let json = parsed_json(world); + let gpus = json + .get("gpus") + .and_then(serde_json::Value::as_array) + .unwrap_or_else(|| panic!("`examine --json` has no gpus array:\n{json:#}")); + let amd: Vec<&serde_json::Value> = gpus + .iter() + .filter(|gpu| gpu.get("is_amd").and_then(serde_json::Value::as_bool) == Some(true)) + .collect(); + assert_eq!( + amd.len(), + rows.len(), + "one AMD GPU per planted PCI row and no other:\n{json:#}" + ); + let mut wrong = Vec::new(); + for (address, target, integrated, _) in rows { + let Some(gpu) = amd + .iter() + .find(|gpu| gpu.get("pci_id").and_then(serde_json::Value::as_str) == Some(address)) + else { + wrong.push(format!("{address}: not reported")); + continue; + }; + let reported_target = gpu.get("gfx_target").and_then(serde_json::Value::as_str); + let reported_integrated = gpu.get("is_apu").and_then(serde_json::Value::as_bool); + if reported_target != Some(target) || reported_integrated != Some(*integrated) { + wrong.push(format!( + "{address}: expected {target}, integrated={integrated}; got \ + gfx_target={reported_target:?}, is_apu={reported_integrated:?}" + )); + } + } + assert!( + wrong.is_empty(), + "GPUs reported with the wrong architecture or packaging:\n{}\n{json:#}", + wrong.join("\n") + ); +} + +#[then("the inspection reports the machine as having both an integrated and a discrete AMD GPU")] +async fn assert_both_gpu_categories(world: &mut E2eWorld) { + let json = parsed_json(world); + for field in ["has_amd_gpu", "has_apu", "has_discrete_amd"] { + assert_eq!( + json.get(field).and_then(serde_json::Value::as_bool), + Some(true), + "{field}:\n{json:#}" + ); + } +} + #[when("the user inspects that machine in machine-readable form")] async fn user_inspects_planted_machine(world: &mut E2eWorld) { let (stdout, stderr, rc) = From 3115e3275db29c774caeb51c16b20bf2b42388a6 Mon Sep 17 00:00:00 2001 From: Roman Inflianskas Date: Fri, 13 Nov 2026 15:21:24 +0200 Subject: [PATCH 3/3] docs(examine): correct stale GPU-classification claims The examine probe's comments still said the name table has no arm for any Vega iGPU; it now knows the gfx90c codenames, and only the generic Windows string "AMD Radeon(TM) Graphics" resolves nothing. host-probing.md still called this change an open, conflicting pull request, and its "Delivered" paragraph named one scenario. The testing guide and the scenario comment said `rocm serve` withholds its low-VRAM warning because of examine's `is_apu`; serve and `rocm diagnose --model` ask `gfx_is_apu_family` on the detected target instead, and only `fix-9-igpu-dgpu` reads `is_apu`. Signed-off-by: Roman Inflianskas --- crates/rocm-core/src/examine/windows.rs | 22 +++++++++++---------- crates/rocm-core/src/gfx_target.rs | 5 +++-- docs/host-probing.md | 8 ++++---- docs/testing.md | 14 ++++++++----- tests/e2e-cucumber/features/examine.feature | 11 +++++++---- 5 files changed, 35 insertions(+), 25 deletions(-) diff --git a/crates/rocm-core/src/examine/windows.rs b/crates/rocm-core/src/examine/windows.rs index f4d9e9e7a2..68381f4acb 100644 --- a/crates/rocm-core/src/examine/windows.rs +++ b/crates/rocm-core/src/examine/windows.rs @@ -70,13 +70,15 @@ pub(super) fn parse_gpus_windows_output(out: &str, e: &mut Examination) { // // `Win32_VideoController` hands us `PCI\VEN_1002&DEV_1636&...` in the // same row as the name, and the PCI-id table in the `gfx_target` module - // answers for parts the name table has no arm for -- every Vega iGPU - // among them. This probe used to drop the id and ask the name alone, - // so a Renoir / Lucienne / Cezanne / Barcelo laptop reported an empty - // `gfx_target` unless a HIP SDK was installed for `hipInfo` to fill it - // in: exactly the host that has no HIP SDK to ask. A device id is also - // the stronger answer where both have one, being the ASIC rather than - // the brand. + // answers for parts whose Windows name resolves nothing -- every gfx90c + // Vega iGPU (Renoir / Lucienne / Cezanne / Barcelo) among them, which + // Windows names only "AMD Radeon(TM) Graphics" (the name table knows + // those codenames, but this string carries none). This probe used to drop + // the id and ask the name alone, so a Renoir / Lucienne / Cezanne / + // Barcelo laptop reported an empty `gfx_target` unless a HIP SDK was + // installed for `hipInfo` to fill it in: exactly the host that has no + // HIP SDK to ask. A device id is also the stronger answer where both + // have one, being the ASIC rather than the brand. let (gfx_target, is_apu) = match crate::host_gpu::gfx_target_from_pnp_device_id(&pnp) { // `gfx_is_apu_family` knows the answer whenever the id resolved a // target; the name table is only consulted when the id did not. @@ -372,8 +374,8 @@ mod tests { // // The two inputs are made to DISAGREE on purpose, and that disagreement // is the whole point: PNP id `73a0` is Navi 21 (gfx1030, discrete), - // while the name "AMD Radeon(TM) 8060S Graphics" is one of the handful - // `classify_amd_marketing_name` answers for, as gfx1151 and an APU. No + // while the name "AMD Radeon(TM) 8060S Graphics" is one the shared + // marketing-name table resolves, as gfx1151 and an APU. No // such part ships -- which is exactly why it is the input. A pair the // two paths agree about passes with the PNP lookup deleted, silently // re-testing the marketing fallback that `marketing_name_maps_strix_halo` @@ -393,7 +395,7 @@ mod tests { assert_eq!( gpu.is_apu, Some(false), - "`is_apu` has to follow the id's target too, not the name heuristic: {gpu:?}" + "`is_apu` has to follow the id's target too, not the name's: {gpu:?}" ); } diff --git a/crates/rocm-core/src/gfx_target.rs b/crates/rocm-core/src/gfx_target.rs index ca9cd77224..6ce2e9d170 100644 --- a/crates/rocm-core/src/gfx_target.rs +++ b/crates/rocm-core/src/gfx_target.rs @@ -181,8 +181,9 @@ pub(crate) fn parse_windows_display_name(text: &str) -> Option { /// can answer from the same table rather than from marketing-name matching /// alone: `Win32_VideoController` hands that probe the PNP id in the same /// query that gives it the name, and without this it dropped the id on the -/// floor and left `gfx_target` empty for every part the name table does not -/// cover -- which is every Vega iGPU, so a Renoir/Cezanne laptop reported no +/// floor and left `gfx_target` empty for every part whose Windows name the +/// name table cannot resolve -- among them every gfx90c Vega iGPU, named only +/// "AMD Radeon(TM) Graphics" there, so a Renoir/Cezanne laptop reported no /// architecture at all unless a HIP SDK was installed to ask `hipInfo`. /// /// Returns the first match only. Which GPU of several a caller is asking about diff --git a/docs/host-probing.md b/docs/host-probing.md index ad39bfb872..dac15c3efe 100644 --- a/docs/host-probing.md +++ b/docs/host-probing.md @@ -200,17 +200,17 @@ Verdict: confirmed duplicate, but not equivalent, and now converged (see "Delive | APU verdict | none (the caller derives it from the target with `gfx_is_apu_family`) | yes, from the name | | Used on | Windows adapters and the WSL host's display probe | Windows adapters after the PNP id, and the Linux `lspci` scan (`probe_gpus_lspci`), which calls it directly with no PCI-id lookup first | -The two agree where they overlap on `880m`, `780m` and `8060s`. They disagree on Krackan: `classify_amd_marketing_name` returns `gfx1150` for the token `krackan`, while the table maps `860m` and `840m` to `gfx1152`, and `gfx_target_from_amd_pci_device_id` maps device `1114` to `gfx1152`. Which is right for a given part is UNCONFIRMED here (no hardware); the open pull request [#521](https://github.com/ROCm/rocm-cli/pull/521) reaches the same disagreement and resolves it toward `gfx1152`. +The two agreed where they overlapped on `880m`, `780m` and `8060s`. They disagreed on Krackan: `classify_amd_marketing_name` returned `gfx1150` for the token `krackan`, while the table mapped `860m` and `840m` to `gfx1152`, and `gfx_target_from_amd_pci_device_id` mapped device `1114` to `gfx1152`. Which is right for a given part was UNCONFIRMED here (no hardware); [#521](https://github.com/ROCm/rocm-cli/pull/521) resolved it toward `gfx1152` (see "Delivered" below). -What a user can see, on Windows without a HIP SDK: an RX 7900 XTX is not in the PCI-id table and not in `classify_amd_marketing_name`, so `gpus[].gfx_target` is empty while `summary.detected_gfx_target` in the same JSON document is `gfx1100`, from the name table. (Derived from the code; `hipInfo` fills the examination's value in when an SDK is installed.) On native Linux only the examination's `lspci` path can show a name-derived empty target (see the host table). +What a user could see before the change, on Windows without a HIP SDK: an RX 7900 XTX was not in the PCI-id table and not in `classify_amd_marketing_name`, so `gpus[].gfx_target` was empty while `summary.detected_gfx_target` in the same JSON document was `gfx1100`, from the name table. (Derived from the code; `hipInfo` filled the examination's value in when an SDK was installed.) On native Linux only the examination's `lspci` path could show a name-derived empty target (see the host table). -Proposal, as written before the change (see "Delivered" below for what landed): the table survives. It is pure, data-driven and already the superset for target names. It gains the codenames `classify_amd_marketing_name` knows, and the APU verdict is derived from the target, falling back to a codename list only while some codename still has no target. [#521](https://github.com/ROCm/rocm-cli/pull/521) already proposes exactly this convergence and is open and conflicting with `main`; the cheapest route is to finish and rebase it rather than start a second change. Its Linux device-id work is a separate concern from this pair. Hosts: the table is pure data, so its test needs no host, but the two callers of `classify_amd_marketing_name` differ in what happens around it (on Windows `parse_gpus_windows_output` tries the PNP id first; on native Linux `probe_gpus_lspci` calls it directly, and the later `probe_gpus_after_lspci` steps (the kernel topology, `rocminfo` and the sysfs fallback) may also set a target, which this note did not trace; WSL has no name lookup in the examination at all), so the characterisation test names each caller: a fixture adapter list through `parse_gpus_windows_output`, which a Linux unit test can drive, and a fixture `lspci` output through the Linux scan. +Proposal, as written before the change (see "Delivered" below for what landed): the table survives. It is pure, data-driven and already the superset for target names. It gains the codenames `classify_amd_marketing_name` knows, and the APU verdict is derived from the target, falling back to a codename list only while some codename still has no target. [#521](https://github.com/ROCm/rocm-cli/pull/521) proposed exactly this convergence, and it is what delivered it. Its Linux device-id work is a separate concern from this pair. Hosts: the table is pure data, so its test needs no host, but the two callers of `classify_amd_marketing_name` differ in what happens around it (on Windows `parse_gpus_windows_output` tries the PNP id first; on native Linux `probe_gpus_lspci` calls it directly, and the later `probe_gpus_after_lspci` steps (the kernel topology, `rocminfo` and the sysfs fallback) may also set a target, which this note did not trace; WSL has no name lookup in the examination at all), so the characterisation test names each caller: a fixture adapter list through `parse_gpus_windows_output`, which a Linux unit test can drive, and a fixture `lspci` output through the Linux scan. ### Delivered `classify_amd_marketing_name` now asks `gfx_target_from_amd_marketing_name` and derives the APU verdict from the target with `gfx_is_apu_family`; `APU_KEYWORDS` is gone. The table gained the codenames and product lines the examination knew (Strix Halo, Ryzen AI Max, Strix Point, Krackan, Phoenix and its die-numbered `lspci` spellings, Hawk Point, Rembrandt, Raphael, Van Gogh, Renoir, Cezanne, Lucienne, Barcelo) plus Mendocino (`gfx1037`), so a codename now yields a target rather than "is an APU" with an empty one. Krackan is `gfx1152`, agreeing with device id `1114` and the 860M and 840M rows. The `8045S` the examination matched is not a product name (the Strix Halo names are 8060S, 8050S and 8040S), and the table never carried it. On native Linux the `lspci` scan now asks the `[1002:xxxx]` device id first and the name only when the id is not in the table, as the Windows probe already did. On the install side, a gfx90c recognised by its codename ranks where the same iGPU recognised by its device id already did, below every other recognised adapter, in both `parse_windows_display_gfx_target` and `preferred_amd_display`. -What a user can see: the RX 7900 XTX case above now reports `gfx1100` in `gpus[]`; a Krackan Point iGPU reports `gfx1152` where the examination said `gfx1150`; a Renoir, Cezanne, Lucienne, Barcelo or Mendocino row named only by its codename now carries a target; and a Mendocino iGPU, which used to carry no target and `is_apu: false`, reports `gfx1037` and `is_apu: true`. The tests are `examine_names_a_gpu_by_its_codename_from_the_shared_table` and `lspci_scan_prefers_the_device_id_to_the_name` in [linux_gpus.rs](../crates/rocm-core/src/examine/linux_gpus.rs), the property harness in [examine_gpu_proptests.rs](../crates/rocm-core/src/examine_gpu_proptests.rs), and scenario `examine-tells-integrated-gpus-from-discrete-ones` in [examine.feature](../tests/e2e-cucumber/features/examine.feature). +What a user can see: the RX 7900 XTX case above now reports `gfx1100` in `gpus[]`; a Krackan Point iGPU reports `gfx1152` where the examination said `gfx1150`; a Renoir, Cezanne, Lucienne, Barcelo or Mendocino row named only by its codename now carries a target; and a Mendocino iGPU, which used to carry no target and `is_apu: false`, reports `gfx1037` and `is_apu: true`. The tests are `examine_names_a_gpu_by_its_codename_from_the_shared_table` and `lspci_scan_prefers_the_device_id_to_the_name` in [linux_gpus.rs](../crates/rocm-core/src/examine/linux_gpus.rs), the property harness in [examine_gpu_proptests.rs](../crates/rocm-core/src/examine_gpu_proptests.rs), and scenario `examine-tells-integrated-gpus-from-discrete-ones` (`examine-49`) in [examine.feature](../tests/e2e-cucumber/features/examine.feature). The same change also lets the kernel topology's target decide the verdict of a GPU that neither table names, which is outside this pair; scenarios `examine-classifies-an-igpu-from-the-kernel-topology` (`examine-50`) and `examine-classifies-gpus-only-the-kernel-lists` (`examine-51`) cover that. ## PATH lookup diff --git a/docs/testing.md b/docs/testing.md index c1e9270637..2cfca2ac01 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -4309,11 +4309,15 @@ the same statement next to the code. ## Integrated Or Discrete GPU -`rocm examine` reports each AMD GPU's `gfx_target` and `is_apu`, and two things -a user sees follow from `is_apu`: `rocm serve` withholds its low-VRAM warning -on a single-APU host, whose "VRAM" is a BIOS carve-out of system RAM, and -`rocm diagnose` raises `fix-9-igpu-dgpu` only when a host has both an -integrated and a discrete AMD GPU. +`rocm examine` reports each AMD GPU's `gfx_target` and `is_apu`. `rocm +diagnose` raises `fix-9-igpu-dgpu` from `is_apu`, only when a host has both an +integrated and a discrete AMD GPU. `rocm serve` withholds its low-VRAM warning +on a single-GPU host whose detected `gfx_target` is an APU family, whose "VRAM" +is a BIOS carve-out of system RAM: `vram_capacity_is_meaningful` in +[gpu_policy.rs](../apps/rocm/src/gpu_policy.rs) asks `gfx_is_apu_family`, the +predicate `is_apu` is derived from, rather than reading `is_apu` itself, and +`rocm diagnose --model` asks the same check before judging a fit against that +carve-out. `examine-49` in [examine.feature](../tests/e2e-cucumber/features/examine.feature) covers the PCI scan end to end on every Linux lane, GPU or not. It puts a diff --git a/tests/e2e-cucumber/features/examine.feature b/tests/e2e-cucumber/features/examine.feature index a31dee3337..d8c9d38eb6 100644 --- a/tests/e2e-cucumber/features/examine.feature +++ b/tests/e2e-cucumber/features/examine.feature @@ -733,10 +733,13 @@ Feature: GPU detection and system inspection Then the inspection finishes long before the background process does And the inspection lists the GPU that lspci printed - # Whether a GPU is integrated decides two things a user sees: `rocm serve` - # withholds its low-VRAM warning on an APU (whose "VRAM" is a BIOS carve-out - # of system RAM), and `rocm diagnose` raises `fix-9-igpu-dgpu` only on a host - # with both an integrated and a discrete AMD GPU. Every AMD GPU on this bus + # Whether a GPU is integrated decides what a user sees: `rocm diagnose` + # raises `fix-9-igpu-dgpu` from `is_apu` only on a host with both an + # integrated and a discrete AMD GPU, and on a single-GPU host whose target is + # an APU family (whose "VRAM" is a BIOS carve-out of system RAM) `rocm serve` + # withholds its low-VRAM warning and `rocm diagnose --model` does not judge + # the fit against the carve-out; those two ask the same `gfx_is_apu_family` + # predicate `is_apu` is derived from. Every AMD GPU on this bus # used to be misreported: the PCI scan ignored the `[1002:xxxx]` device id, # so a GPU `pci.ids` cannot name ("Device") got no architecture; `examine` # kept its own name table, which called Krackan Point gfx1150 (it is gfx1152)