From be2b169d8e86e60a4fce8f813373a5a871fbcd7f Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Fri, 23 Oct 2026 22:34:33 +0300 Subject: [PATCH] Bracket each leg, and measure the drift where it happens Content: the tree of `development` at 5733d54743, unchanged. --- .../src/bin/conformance.rs | 34 ++- .../src/slow_calls.rs | 278 +++++++++++------- 2 files changed, 195 insertions(+), 117 deletions(-) diff --git a/crates/metal2vulkan-conformance/src/bin/conformance.rs b/crates/metal2vulkan-conformance/src/bin/conformance.rs index e68a7aae5..9e97c200b 100644 --- a/crates/metal2vulkan-conformance/src/bin/conformance.rs +++ b/crates/metal2vulkan-conformance/src/bin/conformance.rs @@ -597,20 +597,26 @@ fn times( calls, shaders, } = asked; - // Read on both sides of the measurement, so the pair brackets the host the measurement - // actually ran on. One reading before it can only describe the host it started on, and a run - // that starts on an empty queue and finishes against five other builds is the case that - // matters: both halves have to be quiet for the figures to be of one machine. - let load_before = metal2vulkan_conformance::clocks::host_load(); + // Each leg is bracketed by its own pair of load readings. The legs run one after the other, + // so what corrupts a ratio is the host moving between their two windows -- and bracketing the + // whole run cannot see that, because the run's endpoints average over exactly the interval + // where the damage happens. These two brackets measure it. + use metal2vulkan_conformance::clocks::host_load; + use metal2vulkan_conformance::slow_calls::LegLoad; + let mut metal_load = LegLoad::default(); + let mut our_load = LegLoad::default(); + metal_load.before = host_load(); let metal = match legs { Legs::Both | Legs::Metal => metal_times(cases, runs)?, Legs::Ours => None, }; + metal_load.after = host_load(); + our_load.before = host_load(); let ours = match legs { Legs::Both | Legs::Ours => Some(run::times_on_under(venues, policy, cases, runs, shaders)?), Legs::Metal => None, }; - let load_after = metal2vulkan_conformance::clocks::host_load(); + our_load.after = host_load(); // The host's load belongs in the words a record carries, because our leg is CPU-bound where // Metal's largest calls are GPU-blocked: a figure taken on a busy host is not the figure a // quiet one gives, and a reader of the recording cannot otherwise tell which they have. @@ -622,12 +628,13 @@ fn times( run::Shaders::Warm => "carried over between the cases of a round", run::Shaders::Cold => "fresh for every case", }, - match (load_before, load_after) { - (Some(before), Some(after)) => format!( - "the host's one-minute load average was {before:.2} when the measurement started \ - and {after:.2} when it finished, and the per-call margin is judged only when both \ - are {:.1} or under", - metal2vulkan_conformance::slow_calls::QUIET_ENOUGH + match (metal_load.typical(), our_load.typical()) { + (Some(metal_at), Some(ours_at)) => format!( + "Metal's leg ran at a one-minute load average of {metal_at:.2} and ours at \ + {ours_at:.2}; the legs run one after the other, so the per-call margin is judged \ + only when both are {:.1} or under and within {:.1}x of each other", + metal2vulkan_conformance::slow_calls::QUIET_ENOUGH, + metal2vulkan_conformance::slow_calls::LOAD_MAY_MOVE ), _ => "this host answers no load average".to_owned(), } @@ -660,8 +667,7 @@ fn times( // It also needs a measurement whose readings localise: a warm one rather than a cold one, and // one taken on a quiet host rather than a busy one -- see `slow_calls::judges`. let mut green = true; - if let Err(why) = metal2vulkan_conformance::slow_calls::judges(shaders, load_before, load_after) - { + if let Err(why) = metal2vulkan_conformance::slow_calls::judges(shaders, metal_load, our_load) { println!("\n{why}"); } else if let (Some(metal), Some(ours)) = (&metal, &ours) { let ledger = metal2vulkan_conformance::slow_calls::ledger()?; diff --git a/crates/metal2vulkan-conformance/src/slow_calls.rs b/crates/metal2vulkan-conformance/src/slow_calls.rs index 5b04cf905..ac6bbc204 100644 --- a/crates/metal2vulkan-conformance/src/slow_calls.rs +++ b/crates/metal2vulkan-conformance/src/slow_calls.rs @@ -135,64 +135,110 @@ pub const QUIET_ENOUGH: f64 = 26.0; /// through the whole run's endpoints; that is the next change, not this one. pub const LOAD_MAY_MOVE: f64 = 2.0; -/// Whether the gate judges a measurement taken under `shaders` on a host whose one-minute load -/// average was `before` when it started and `after` when it finished. -/// -/// Warm, quiet, and quiet *throughout*; otherwise it answers the reason for the caller to print. -/// -/// - A cold reading does not separate our compile cost from first use on a new device -/// ([`WHY_COLD_IS_NOT_JUDGED`]). -/// - A loaded host moves our leg and not Metal's ([`QUIET_ENOUGH`]), so **both** readings have to -/// be at or under the bound. Checking only the first blesses a run that became contended while -/// it ran, which is not hypothetical: a measurement here began on an empty device queue and -/// finished at a load average of 74.84 because other work landed during it. -/// - The two readings also have to agree within [`LOAD_MAY_MOVE`]. Two samples under the bound -/// that differ by a factor turn the run into two measurements of two machines, and the pooled -/// figure is of neither. -/// -/// A host that answers no load average is judged on what it does answer, and judged outright if -/// it answers nothing: a missing reading is not evidence of a busy machine. -pub fn judges( - shaders: crate::run::Shaders, - before: Option, - after: Option, -) -> Result<(), String> { - if matches!(shaders, crate::run::Shaders::Cold) { - return Err(WHY_COLD_IS_NOT_JUDGED.to_owned()); - } - let why_load = |what: &str, seen: f64| { - format!( - "the margin is reported and not judged: this host's one-minute load average {what} \ - {seen:.2}\nand the gate wants it at {QUIET_ENOUGH:.1} or under, start to finish. Our \ - leg spends its cost on the\ncalling thread's CPU where Metal's largest calls sit \ - blocked on the GPU, so load moves our ratios\nand leaves Metal's -- measured, 4.5x \ - against 1.6x on one identical case set. Re-run it on a\nquiet host for a verdict." - ) - }; - if let Some(before) = before { - if before > QUIET_ENOUGH { - return Err(why_load("was", before)); +/// What the host was doing while one leg ran: its one-minute load average on each side of that +/// leg, and nothing from the other leg's window. +#[derive(Clone, Copy, Debug, Default)] +pub struct LegLoad { + /// The reading taken immediately before this leg started. + pub before: Option, + /// The reading taken immediately after it finished. + pub after: Option, +} + +impl LegLoad { + /// The load to compare this leg by: the mean of its two readings, or whichever one the host + /// answered. + pub fn typical(self) -> Option { + match (self.before, self.after) { + (Some(before), Some(after)) => Some((before + after) / 2.0), + (Some(one), None) | (None, Some(one)) => Some(one), + (None, None) => None, } } - if let Some(after) = after { - if after > QUIET_ENOUGH { - return Err(why_load("reached", after)); - } - } - // Both under the bound, but not under the same conditions throughout. - if let (Some(before), Some(after)) = (before, after) { + + /// How far the host moved while this one leg ran, as a factor. + fn moved(self) -> Option { + let (before, after) = (self.before?, self.after?); let (low, high) = if before < after { (before, after) } else { (after, before) }; + if low <= 0.0 { + return None; + } + Some(high / low) + } +} + +/// Whether the gate judges a measurement taken under `shaders` with `metal` and `ours` describing +/// the host while each leg ran. +/// +/// **The per-leg brackets are the point.** `times` measures Metal's leg to completion and then +/// ours, so the quantity that corrupts a ratio is the host moving *between* those two windows: +/// that divides one machine's Metal figure by another machine's figure of ours. Bracketing the +/// whole run could never see it, because the run's endpoints average over exactly the interval +/// where the damage happens. These brackets measure it directly. +/// +/// Refused four ways, each with its reason for the caller to print: a cold reading, which does +/// not separate our compile cost from first use on a new device; any reading over +/// [`QUIET_ENOUGH`]; a single leg whose own load moved by more than [`LOAD_MAY_MOVE`], so that +/// leg is not one measurement; and the two legs' loads differing by more than [`LOAD_MAY_MOVE`], +/// so the ratio is of two machines. +/// +/// A host that answers no load average is judged on what it does answer, and judged outright if +/// it answers nothing: a missing reading is not evidence of a busy machine. +pub fn judges(shaders: crate::run::Shaders, metal: LegLoad, ours: LegLoad) -> Result<(), String> { + if matches!(shaders, crate::run::Shaders::Cold) { + return Err(WHY_COLD_IS_NOT_JUDGED.to_owned()); + } + let why_load = |whose: &str, seen: f64| { + format!( + "the margin is reported and not judged: this host's one-minute load average was \ + {seen:.2} while\n{whose} ran, and the gate wants it at {QUIET_ENOUGH:.1} or under. \ + Measured on this host, the ratios are\nflat to within 1.1x from a load of 8.6 to \ + 26.5, and nothing above that was measured. Re-run it on a\nquieter host for a \ + verdict." + ) + }; + for (whose, leg) in [("Metal's leg", metal), ("our leg", ours)] { + if let Some(seen) = leg + .before + .into_iter() + .chain(leg.after) + .find(|seen| *seen > QUIET_ENOUGH) + { + return Err(why_load(whose, seen)); + } + if let Some(moved) = leg.moved() { + if moved > LOAD_MAY_MOVE { + return Err(format!( + "the margin is reported and not judged: while {whose} ran, this host's \ + one-minute load\naverage moved from {:.2} to {:.2}, a factor of {moved:.2} \ + and more than the {LOAD_MAY_MOVE:.1}x the gate\nallows. That leg is not one \ + measurement, so neither is the ratio. Re-run it on a host that\nstays put \ + for a verdict.", + leg.before.unwrap_or_default(), + leg.after.unwrap_or_default() + )); + } + } + } + // The interval that actually corrupts a ratio: between the two legs' windows. + if let (Some(metal_load), Some(our_load)) = (metal.typical(), ours.typical()) { + let (low, high) = if metal_load < our_load { + (metal_load, our_load) + } else { + (our_load, metal_load) + }; if low > 0.0 && high / low > LOAD_MAY_MOVE { return Err(format!( - "the margin is reported and not judged: this host's one-minute load average moved \ - from {before:.2}\nto {after:.2} across the measurement, more than the {LOAD_MAY_MOVE:.1}x \ - the gate allows. Both readings are under\nthe bound, but the run measured two \ - machines rather than one and the pooled figure is of neither.\nRe-run it on a \ - host that stays put for a verdict." + "the margin is reported and not judged: Metal's leg ran at a load average of \ + {metal_load:.2} and\nours at {our_load:.2}, a factor of {:.2} apart and more \ + than the {LOAD_MAY_MOVE:.1}x the gate allows. The legs\nrun one after the \ + other, so a ratio across that gap divides one machine's figure by another's.\n\ + Re-run it on a host that stays put for a verdict.", + high / low )); } } @@ -413,13 +459,21 @@ mod tests { /// every case a fresh device and a reading over the margin then does not say which of the two /// it is. Measured on this M3 Max, `target` is 0.9x warm and 14.3x cold without compiling /// anything. + /// One leg that held still at `load`. + fn steady(load: f64) -> LegLoad { + LegLoad { + before: Some(load), + after: Some(load), + } + } + #[test] fn the_gate_does_not_judge_a_cold_measurement() { assert!( - judges(crate::run::Shaders::Warm, Some(0.5), Some(0.5)).is_ok(), + judges(crate::run::Shaders::Warm, steady(1.0), steady(1.0)).is_ok(), "the warm gate is off on a quiet host" ); - let cold = judges(crate::run::Shaders::Cold, Some(0.5), Some(0.5)).expect_err( + let cold = judges(crate::run::Shaders::Cold, steady(1.0), steady(1.0)).expect_err( "a cold measurement was judged, and its readings do not separate the two causes", ); assert!( @@ -428,91 +482,109 @@ mod tests { ); } - /// **A loaded host is reported and never judged.** Our leg is CPU-bound where Metal's largest - /// calls are GPU-blocked, so load moves our ratios and leaves Metal's: it does not cancel. - /// A host that answers no load average is still judged, because a missing reading is not - /// evidence of a busy machine. + /// **A loaded host is reported and never judged**, whichever leg was on it. The bound is the + /// top of the range the ratios were measured flat across, and nothing above it is claimed. #[test] fn the_gate_does_not_judge_a_loaded_host() { - let busy = judges( + // Either leg over the bound refuses, and the reason names which one. + let why = judges( crate::run::Shaders::Warm, - Some(QUIET_ENOUGH + 0.01), - Some(QUIET_ENOUGH + 0.01), + steady(QUIET_ENOUGH + 0.01), + steady(QUIET_ENOUGH), ) - .expect_err("a loaded host was judged, and its ratios are not the two stacks'"); + .expect_err("a loaded Metal leg was judged"); assert!( - busy.contains("load average"), - "the reason does not say what it read: {busy}" + why.contains("Metal's leg"), + "the reason does not name the loaded leg: {why}" ); assert!( judges( crate::run::Shaders::Warm, - Some(QUIET_ENOUGH), - Some(QUIET_ENOUGH) + steady(QUIET_ENOUGH), + steady(QUIET_ENOUGH + 0.01) + ) + .is_err(), + "a loaded leg of ours was judged" + ); + assert!( + judges( + crate::run::Shaders::Warm, + steady(QUIET_ENOUGH), + steady(QUIET_ENOUGH) ) .is_ok(), "the bound itself is refused, so no host can ever meet it" ); assert!( - judges(crate::run::Shaders::Warm, None, None).is_ok(), + judges( + crate::run::Shaders::Warm, + LegLoad::default(), + LegLoad::default() + ) + .is_ok(), "a host with no load average was refused a verdict for not having one" ); } - /// **A run that became contended while it ran is refused**, which one reading before the - /// measurement could never catch. This is the case that happened: a whole-set pass started on - /// an empty device queue and finished at a load average of 74.84 because other work landed - /// during it. + /// **A leg that did not hold still is not one measurement.** Its own two readings disagree, so + /// the figures it produced are of a machine that changed under it. #[test] - fn the_gate_does_not_judge_a_run_that_became_loaded() { - let climbed = judges( - crate::run::Shaders::Warm, - Some(0.5), - Some(QUIET_ENOUGH + 10.0), - ) - .expect_err("a run that started quiet and finished loaded was judged"); - assert!( - climbed.contains("reached"), - "the reason does not say the load rose during the run: {climbed}" + fn the_gate_does_not_judge_a_leg_that_moved_under_itself() { + let moved = LegLoad { + before: Some(1.0), + after: Some(1.0 * LOAD_MAY_MOVE + 1.0), + }; + // **This is the case the run-level bracket could not see.** Its two readings are the + // run's first and last -- `moved.before` is 1.0 and `steady(1.0).after` is 1.0 -- so the + // endpoints are identical and a bracket of the whole run reports a host that never + // changed, while the load in fact tripled inside Metal's leg. + assert_eq!( + moved.before, + steady(1.0).after, + "this test no longer pins the case the run-level bracket missed" ); - // And the other way: quiet at the end says nothing about a loaded start. + let why = judges(crate::run::Shaders::Warm, moved, steady(1.0)) + .expect_err("a leg whose own load moved was judged"); assert!( - judges( - crate::run::Shaders::Warm, - Some(QUIET_ENOUGH + 10.0), - Some(0.5) - ) - .is_err(), - "a run that started loaded was judged on how quiet it ended" + why.contains("Metal's leg") && why.contains("moved from"), + "the reason does not name the leg that moved: {why}" + ); + assert!( + judges(crate::run::Shaders::Warm, steady(1.0), moved).is_err(), + "our leg moving under itself was judged" ); } - /// **Two readings under the bound that disagree are two machines, not one.** The pooled - /// figure is then of neither, so the run is refused even though both samples are quiet. + /// **The interval that actually corrupts a ratio is between the legs**, because they run one + /// after the other: a ratio across that gap divides one machine's Metal figure by another + /// machine's figure of ours. This is the case bracketing the whole run could never catch, + /// since the run's endpoints average over exactly that interval. The numbers are the measured + /// ones: a whole-set pass ran Metal's leg near 16.8 and ours near 55.9. #[test] - fn the_gate_does_not_judge_a_host_that_moved_under_it() { - let moved = judges( + fn the_gate_does_not_judge_a_ratio_taken_across_a_load_change() { + let why = judges( crate::run::Shaders::Warm, - Some(1.0), - Some(LOAD_MAY_MOVE + 1.0), - ); + steady(1.0), + steady(LOAD_MAY_MOVE + 1.0), + ) + .expect_err("a ratio taken across a load change between the legs was judged"); assert!( - moved.is_err(), - "a host that moved by more than {LOAD_MAY_MOVE}x under the measurement was judged" - ); - let why = moved.unwrap_err(); - assert!( - why.contains("moved from"), - "the reason does not say the load moved: {why}" + why.contains("Metal's leg ran at") && why.contains("ours at"), + "the reason does not give both legs' loads: {why}" ); // Exactly at the allowance is still one machine. assert!( - judges(crate::run::Shaders::Warm, Some(1.0), Some(LOAD_MAY_MOVE)).is_ok(), + judges( + crate::run::Shaders::Warm, + steady(1.0), + steady(LOAD_MAY_MOVE) + ) + .is_ok(), "the allowance itself is refused" ); - // A quiet host at rest reads near zero on both sides and must not trip the ratio. + // An idle host reads near zero on both legs and must not trip the ratio. assert!( - judges(crate::run::Shaders::Warm, Some(0.0), Some(0.0)).is_ok(), + judges(crate::run::Shaders::Warm, steady(0.0), steady(0.0)).is_ok(), "an idle host was refused for having no load to compare" ); }