diff --git a/crates/cuda-driver/src/original_verify_schedule/impl_originalverifier_2.rs b/crates/cuda-driver/src/original_verify_schedule/impl_originalverifier_2.rs index da204a0b..46713406 100644 --- a/crates/cuda-driver/src/original_verify_schedule/impl_originalverifier_2.rs +++ b/crates/cuda-driver/src/original_verify_schedule/impl_originalverifier_2.rs @@ -38,28 +38,7 @@ impl<'ctx, R: OriginalVerifyGraphResources<'ctx> + OriginalNativeHeadGraphResour } let span_commit = crate::host_profile::span("drv.commit_enqueue"); self.bank.as_mut().ok_or(Error::ContextPoisoned)?.stage_commit(n_keep)?; - let operation = (|| { - self.commit - .as_mut() - .ok_or(Error::ContextPoisoned)? - .enqueue(self.compute.as_ref().ok_or(Error::ContextPoisoned)?)?; - self.window_ready = false; - let forced = commit_sync_forced(); - if async_commit && !forced { - self.event - .as_mut() - .ok_or(Error::ContextPoisoned)? - .record(self.compute.as_ref().ok_or(Error::ContextPoisoned)?)?; - self.commit_pending = true; - Ok(()) - } else { - let receipt = self.compute.as_ref().ok_or(Error::ContextPoisoned)?.synchronize_with_receipt()?; - self.commit.as_mut().ok_or(Error::ContextPoisoned)?.resources_after_stream_drain(&receipt)?; - self.commit.as_ref().ok_or(Error::ContextPoisoned)?.verifier_complete_commit(self.bank()?)?; - self.commits = self.commits.checked_add(1).ok_or(Error::InvalidSize)?; - Ok(()) - } - })(); + let operation = self.commit_after_stage(async_commit); drop(span_commit); if let Err(primary) = operation { self.failed = true; @@ -67,4 +46,32 @@ impl<'ctx, R: OriginalVerifyGraphResources<'ctx> + OriginalNativeHeadGraphResour } Ok(()) } + + /// The commit graph launch after the bank staged `n_keep`: asynchronous with an event, or drained and completed. + fn commit_after_stage(&mut self, async_commit: bool) -> Result<()> { + self.commit + .as_mut() + .ok_or(Error::ContextPoisoned)? + .enqueue(self.compute.as_ref().ok_or(Error::ContextPoisoned)?)?; + self.window_ready = false; + let forced = commit_sync_forced(); + if async_commit && !forced { + self.event + .as_mut() + .ok_or(Error::ContextPoisoned)? + .record(self.compute.as_ref().ok_or(Error::ContextPoisoned)?)?; + self.commit_pending = true; + Ok(()) + } else { + self.complete_commit_sync() + } + } + + fn complete_commit_sync(&mut self) -> Result<()> { + let receipt = self.compute.as_ref().ok_or(Error::ContextPoisoned)?.synchronize_with_receipt()?; + self.commit.as_mut().ok_or(Error::ContextPoisoned)?.resources_after_stream_drain(&receipt)?; + self.commit.as_ref().ok_or(Error::ContextPoisoned)?.verifier_complete_commit(self.bank()?)?; + self.commits = self.commits.checked_add(1).ok_or(Error::InvalidSize)?; + Ok(()) + } } diff --git a/crates/cuda-driver/src/original_verify_schedule/impl_originalverifier_4.rs b/crates/cuda-driver/src/original_verify_schedule/impl_originalverifier_4.rs index dafb171b..709dd081 100644 --- a/crates/cuda-driver/src/original_verify_schedule/impl_originalverifier_4.rs +++ b/crates/cuda-driver/src/original_verify_schedule/impl_originalverifier_4.rs @@ -131,25 +131,7 @@ impl<'ctx, R: OriginalVerifyGraphResources<'ctx> + OriginalNativeHeadGraphResour let start = Instant::now(); // Original #267 bounded release: no blocking wait on a spin kernel forever. for s in [self.compute.as_ref(), self.copy.as_ref()].into_iter().flatten() { - loop { - match s.query() { - Ok(true) => break, - Ok(false) => { - if start.elapsed() > Duration::from_millis(5000) { - if let Some(bank) = &self.bank { - poison(bank.physical.context) - } - result = combine(result, Err(Error::InvalidDriverValue("Verifier release timeout"))); - break; - } - std::thread::sleep(Duration::from_millis(1)); - } - Err(error) => { - result = combine(result, Err(error)); - break; - } - } - } + result = self.drain_bounded(s, start, result); } if result.is_ok() { for graph in self.window.iter_mut().filter_map(Option::as_mut) { @@ -163,6 +145,29 @@ impl<'ctx, R: OriginalVerifyGraphResources<'ctx> + OriginalNativeHeadGraphResour self.commit_pending = false; result } + /// Polls one stream until idle; a stuck stream poisons the context after 5 s, a query error ends the wait. + fn drain_bounded(&self, s: &OriginalGpuStream<'ctx>, start: Instant, mut result: Result<()>) -> Result<()> { + loop { + match s.query() { + Ok(true) => break, + Ok(false) => { + if start.elapsed() > Duration::from_millis(5000) { + if let Some(bank) = &self.bank { + poison(bank.physical.context) + } + result = combine(result, Err(Error::InvalidDriverValue("Verifier release timeout"))); + break; + } + std::thread::sleep(Duration::from_millis(1)); + } + Err(error) => { + result = combine(result, Err(error)); + break; + } + } + } + result + } pub fn close(mut self) -> Result<()> { self.close_inner() } diff --git a/crates/cuda-driver/src/original_verify_schedule/window_phases.rs b/crates/cuda-driver/src/original_verify_schedule/window_phases.rs index 6b8c15a3..38de4c0b 100644 --- a/crates/cuda-driver/src/original_verify_schedule/window_phases.rs +++ b/crates/cuda-driver/src/original_verify_schedule/window_phases.rs @@ -120,7 +120,7 @@ impl<'ctx, R: OriginalVerifyGraphResources<'ctx> + OriginalNativeHeadGraphResour let span_bell = crate::host_profile::span("drv.doorbell_wait"); self.wait_doorbell(want, start)?; drop(span_bell); - self.pool_group(t, groups, (layer, group), want, pool)?; + self.pool_group(PoolAt { t, groups, layer, group, want }, pool)?; self.publish_group(k, groups, group, want) } @@ -160,16 +160,21 @@ impl<'ctx, R: OriginalVerifyGraphResources<'ctx> + OriginalNativeHeadGraphResour } Ok(last_flush) } +} +/// Where in the window a CPU group sits: `t` tokens split in `groups`, the `layer`/`group` pair, ring `want`. +#[derive(Clone, Copy)] +struct PoolAt { + t: usize, + groups: usize, + layer: usize, + group: usize, + want: u32, +} + +impl<'ctx, R: OriginalVerifyGraphResources<'ctx> + OriginalNativeHeadGraphResources<'ctx>> OriginalVerifier<'ctx, R> { /// Hands the group's mapped CPU rows to the caller's pool together with the plan sink. - fn pool_group
( - &mut self, - t: usize, - groups: usize, - (layer, group): (usize, usize), - want: u32, - pool: &mut P, - ) -> Result<()> + fn pool_group
(&mut self, at: PoolAt, pool: &mut P) -> Result<()>
where
P: FnMut(
OriginalVerifyCpuRows<'_>,
@@ -177,10 +182,10 @@ impl<'ctx, R: OriginalVerifyGraphResources<'ctx> + OriginalNativeHeadGraphResour
&mut OriginalGpuStream<'ctx>,
) -> Result<()>,
{
+ let PoolAt { t, groups, layer, group, want } = at;
let (begin, end) = window_plan::group_rows(t, groups, group);
let rows = end - begin;
- let g = self.bank()?.geometry();
- let physical = self.bank()?.physical.clone();
+ let (g, physical) = (self.bank()?.geometry(), self.bank()?.physical.clone());
let x = physical.mapped[6].try_borrow().map_err(|_| Error::InvalidQwenShape)?;
let ids = physical.mapped[7].try_borrow().map_err(|_| Error::InvalidQwenShape)?;
let w = physical.mapped[8].try_borrow().map_err(|_| Error::InvalidQwenShape)?;
@@ -197,7 +202,6 @@ impl<'ctx, R: OriginalVerifyGraphResources<'ctx> + OriginalNativeHeadGraphResour
self.invoke_pool(pool, cpu, (begin, rows, groups, want))
},
)?;
- drop((x, ids, w));
Ok(())
}
diff --git a/crates/cuda-driver/src/original_verify_window_gpu.rs b/crates/cuda-driver/src/original_verify_window_gpu.rs
index f61bf2ff..c6201751 100644
--- a/crates/cuda-driver/src/original_verify_window_gpu.rs
+++ b/crates/cuda-driver/src/original_verify_window_gpu.rs
@@ -3,6 +3,7 @@
//! executed. They certify that the pinned PTX loads on the card and exports every entry the
//! planners name, NOT numerics (the per-kernel golden comparisons against the Original are listed in
//! the work report).
+use super::abi::{Arg, Command, Module};
use super::*;
use crate::Driver;
@@ -45,7 +46,6 @@ fn sealed_window_assets_load_on_the_device_and_export_every_named_entry() {
#[test]
#[ignore = "Root-only: needs a CUDA device"]
fn the_pcie_span_copy_stages_gate_up_down_like_the_dma_path() {
- use super::abi::{Arg, Command, Module};
assert_eq!(std::env::var(ENABLE).ok().as_deref(), Some("yes"));
let driver = Driver::load().expect("driver");
let device = driver.device(0).expect("device");
@@ -53,6 +53,45 @@ fn the_pcie_span_copy_stages_gate_up_down_like_the_dma_path() {
let roles = [1_126_400usize, 1_126_400, 655_360];
let stride: usize = roles.iter().sum();
let fetches = 5usize;
+ let (sources, expected) = pcie_sources(&context, roles, fetches);
+ // ptr2 holds 3 * fetches aliases (a 60-entry table like a max_t = 6 window's plan block)
+ let table = pcie_table(&context, &sources);
+ let mut count = context.allocate(4).expect("count");
+ count.copy_from(&(fetches as i32).to_le_bytes()).expect("count upload");
+ let staging = context.allocate(16 * stride).expect("staging");
+ launch_span_copy_and_rebase(&context, [&table, &count, &staging], roles);
+ let staged = staging.copy_to_owned(16 * stride).expect("read staging");
+ assert!(staged[..fetches * stride] == expected[..], "staging slots are gate|up|down per expert");
+ let table_after = table.copy_to_owned(60 * 8).expect("read table");
+ for q in 0..fetches {
+ let ptr = u64::from_le_bytes(table_after[q * 8..q * 8 + 8].try_into().unwrap());
+ assert_eq!(ptr, address(&staging) + (q * stride) as u64, "ptr2[{q}] rebased to its slot");
+ }
+ // entries past the rebased ones keep their aliases
+ let tail = u64::from_le_bytes(table_after[fetches * 8..fetches * 8 + 8].try_into().unwrap());
+ assert_eq!(tail, address(&sources[fetches]));
+ eprintln!("pcie span copy: {fetches} experts x {stride} bytes staged bit-identical, ptr2 rebased");
+}
+
+/// The 60-entry `ptr2` table holding the device alias of every source span.
+fn pcie_table<'c>(context: &'c Context<'c>, sources: &[DeviceBuffer<'_>]) -> DeviceBuffer<'c> {
+ let mut table = context.allocate(60 * 8).expect("table");
+ let mut words = vec![0u8; 60 * 8];
+ for (i, s) in sources.iter().enumerate() {
+ words[i * 8..i * 8 + 8].copy_from_slice(&address(s).to_le_bytes());
+ }
+ table.copy_from(&words).expect("table upload");
+ table
+}
+
+fn address(b: &DeviceBuffer<'_>) -> u64 {
+ b.handle.expect("handle").qwen_address()
+}
+
+/// Three device source spans per expert (gate, up, down) of a deterministic pattern, and the bytes the staging slots
+/// must hold afterwards.
+fn pcie_sources<'c>(context: &'c Context<'c>, roles: [usize; 3], fetches: usize) -> (Vec