From 4efb65abeb009a5f9c18fe60940e8c56aaa9e855 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Fri, 6 Nov 2026 22:31:40 +0100 Subject: [PATCH] STRA-338 size2 cd7: qwen_dispatch installed-driver tests cut into helpers --- .../qwen_dispatch/gdn_products_installed.rs | 395 +++++++++--------- .../src/qwen_dispatch/queued_gr_installed.rs | 220 ++++++---- 2 files changed, 339 insertions(+), 276 deletions(-) diff --git a/crates/cuda-driver/src/qwen_dispatch/gdn_products_installed.rs b/crates/cuda-driver/src/qwen_dispatch/gdn_products_installed.rs index b24015ad..24756348 100644 --- a/crates/cuda-driver/src/qwen_dispatch/gdn_products_installed.rs +++ b/crates/cuda-driver/src/qwen_dispatch/gdn_products_installed.rs @@ -1,7 +1,7 @@ //! Moved out of the parent file by STRA-338 (same module path as before). use super::*; -use crate::Driver; +use crate::{Device, Driver}; fn encode(values: impl Iterator) -> Vec { values.flat_map(f32::to_le_bytes).collect() } @@ -19,6 +19,209 @@ fn count_difference(expected: &[Vec; 2], actual: &[Vec; 2]) -> u64 { .map(|(x, y)| x.chunks_exact(4).zip(y.chunks_exact(4)).filter(|(a, b)| a != b).count() as u64) .sum() } +/// Allocate one buffer per size, in order; a failure drops the ones allocated so far. +fn allocate_all<'a, const N: usize>(context: &'a Context<'_>, sizes: [usize; N]) -> Result<[DeviceBuffer<'a>; N]> { + let mut buffers = Vec::with_capacity(N); + for size in sizes { + buffers.push(context.allocate(size)?); + } + buffers.try_into().map_err(|_| Error::InvalidQwenShape) +} +fn signed_zero(i: usize) -> f32 { + if i % 2 == 0 { 0.0f32 } else { -0.0f32 } +} +/// The host inputs of one pattern: the state, the Q/K and H products, the gate and beta. +struct Host { + state: Vec, + qk: Vec, + h: Vec, + gate: Vec, + beta: Vec, +} +impl Host { + fn pattern(pattern: usize) -> Self { + let state = encode( + (0..786432usize).map(|i| if pattern == 0 { signed_zero(i) } else { ((i % 13) as f32 - 6.0) / 1024.0 }), + ); + let qk = encode((0..10240usize).map(|i| { + if i >= 4096 { + 17.0f32 + (i % 7) as f32 + } else if pattern == 0 { + signed_zero(i) + } else { + ((i % 11) as f32 - 5.0) / 64.0 + } + })); + let h = encode((0..10240usize).map(|i| { + if i < 4096 { + -23.0f32 - (i % 9) as f32 + } else if pattern == 0 { + signed_zero(i) + } else { + ((i % 29) as f32 - 14.0) / 32.0 + } + })); + let gate = encode((0..48usize).map(|i| -0.0625f32 * (1 + (i % 5)) as f32)); + let beta = encode((0..48usize).map(|i| 0.125f32 + (i % 7) as f32 / 16.0)); + Self { state, qk, h, gate, beta } + } +} +fn has_nonfinite(pair: &[Vec; 2]) -> bool { + pair.iter() + .any(|bytes| bytes.chunks_exact(4).any(|b| !f32::from_le_bytes(b.try_into().expect("four bytes")).is_finite())) +} +/// The operands of the boundary and the tally of its checks. +struct Rig<'a> { + inputs: [DeviceBuffer<'a>; 5], + uploaded: [DeviceBuffer<'a>; 5], + outputs: [DeviceBuffer<'a>; 4], + mismatches: u64, + guards: u32, +} +impl<'a> Rig<'a> { + fn allocate(context: &'a Context<'_>) -> Result { + let inputs = allocate_all(context, [3145728, 40960, 40960, 192, 192])?; + let uploaded = allocate_all(context, [8192, 8192, 24576, 192, 192])?; + let outputs = allocate_all(context, [3145728, 24576, 3145728, 24576])?; + Ok(Self { inputs, uploaded, outputs, mismatches: 0, guards: 0 }) + } + /// Independent OLD marshaling FIRST from canonical host + /// slices, not candidate handles or fitted expected math. + fn old_expected(&mut self, context: &Context<'_>, host: &Host) -> Result<[Vec; 2]> { + self.inputs[0].copy_from(&host.state)?; + self.uploaded[0].copy_from(&host.qk[..8192])?; + self.uploaded[1].copy_from(&host.qk[8192..16384])?; + self.uploaded[2].copy_from(&host.h[16384..])?; + self.uploaded[3].copy_from(&host.gate)?; + self.uploaded[4].copy_from(&host.beta)?; + let [next, output, _, _] = &mut self.outputs; + let [u0, u1, u2, u3, u4] = &self.uploaded; + context.gdn_step_f32(&self.inputs[0], u0, u1, u2, u3, u4, next, output)?; + read_pair(&self.outputs[0], &self.outputs[1]) + } + /// The candidate step from the whole product owners. + fn products_step(&mut self, context: &Context<'_>) -> Result<()> { + let [_, _, next, output] = &mut self.outputs; + let [i0, i1, i2, i3, i4] = &self.inputs; + context.gdn_step_from_products_f32(i0, i1, i2, i3, i4, next, output) + } + fn candidate(&mut self, context: &Context<'_>, host: &Host) -> Result<[Vec; 2]> { + self.inputs[1].copy_from(&host.qk)?; + self.inputs[2].copy_from(&host.h)?; + self.inputs[3].copy_from(&host.gate)?; + self.inputs[4].copy_from(&host.beta)?; + self.products_step(context)?; + read_pair(&self.outputs[2], &self.outputs[3]) + } + /// Two patterns, each against the old step; the expected output and products of the last one. + fn patterns(&mut self, context: &Context<'_>) -> Result<([Vec; 2], Vec, Vec)> { + let mut last = None; + for pattern in 0..2usize { + let host = Host::pattern(pattern); + let expected = self.old_expected(context, &host)?; + self.mismatches += u64::from(has_nonfinite(&expected)); + let actual = self.candidate(context, &host)?; + self.mismatches += count_difference(&expected, &actual); + last = Some((expected, host.qk, host.h)); + } + last.ok_or(Error::InvalidQwenShape) + } + /// Only unused portions are poisoned. The same original old + /// expected output proves exact fixed subrange association. + fn poisoned_tails(&mut self, context: &Context<'_>, last: ([Vec; 2], Vec, Vec)) -> Result<()> { + let (expected, mut qk, mut h) = last; + for bytes in qk[16384..].chunks_exact_mut(4) { + bytes.copy_from_slice(&f32::NAN.to_le_bytes()); + } + for bytes in h[..16384].chunks_exact_mut(4) { + bytes.copy_from_slice(&f32::INFINITY.to_le_bytes()); + } + self.inputs[1].copy_from(&qk)?; + self.inputs[2].copy_from(&h)?; + self.products_step(context)?; + self.mismatches += count_difference(&expected, &read_pair(&self.outputs[2], &self.outputs[3])?); + Ok(()) + } + /// New whole-owner admission must reject both full shape gaps + /// before writing either next-state or readout candidate. + fn shape_gaps(&mut self, context: &Context<'_>) -> Result<()> { + let bad = context.allocate(40956)?; + let operation = self.refuse_shape(context, &bad); + combine(operation, bad.release())?; + self.guards += 2; + Ok(()) + } + fn refuse_shape(&mut self, context: &Context<'_>, bad: &DeviceBuffer<'_>) -> Result<()> { + let [_, _, next, output] = &mut self.outputs; + let [i0, i1, i2, i3, i4] = &self.inputs; + if context.gdn_step_from_products_f32(i0, bad, i2, i3, i4, next, output) != Err(Error::InvalidQwenShape) { + self.mismatches += 1; + } + if context.gdn_step_from_products_f32(i0, i1, bad, i3, i4, next, output) != Err(Error::InvalidQwenShape) { + self.mismatches += 1; + } + Ok(()) + } + fn foreign_context(&mut self, context: &Context<'_>, device: &Device<'_>) -> Result<()> { + let foreign_context = device.create_context(1024 * 1024)?; + let foreign = foreign_context.allocate(40960)?; + let operation = self.refuse_foreign(context, &foreign); + let operation = combine(operation, foreign.release()); + combine(operation, foreign_context.close())?; + self.guards += 1; + Ok(()) + } + fn refuse_foreign(&mut self, context: &Context<'_>, foreign: &DeviceBuffer<'_>) -> Result<()> { + let [_, _, next, output] = &mut self.outputs; + let [i0, _, i2, i3, i4] = &self.inputs; + if context.gdn_step_from_products_f32(i0, foreign, i2, i3, i4, next, output) != Err(Error::ContextMismatch) { + self.mismatches += 1; + } + Ok(()) + } + /// Safe public mutable references cannot express state==next. + /// Test the SAME private whole-owner admission with duplicated + /// actual immutable owner refs; no forged buffer or launch. + fn alias_guard(&mut self, context: &Context<'_>) { + let mut whole = Operation::GdnStep.layout(); + whole.bytes = [3145728, 40960, 40960, 192, 192, 3145728, 24576, 0, 0]; + whole.align = [4; 9]; + whole.count = 7; + whole.outputs = 0b1100000; + let [i0, i1, i2, i3, i4] = &self.inputs; + let request = Admission { + operation: Operation::GdnStep, + layout: Ok(whole), + asset: qwen_ffi::asset(Operation::GdnStep), + epsilon: 0.0, + in_place: false, + }; + let alias = admission(context, &[i0, i1, i2, i3, i4, i0, &self.outputs[3]], request); + self.mismatches += u64::from(!matches!(alias, Err(Error::OutputAliasesInput))); + self.guards += 1; + } + fn run_checks(&mut self, context: &Context<'_>, device: &Device<'_>) -> Result<()> { + let last = self.patterns(context)?; + self.poisoned_tails(context, last)?; + let before = read_pair(&self.outputs[2], &self.outputs[3])?; + self.shape_gaps(context)?; + self.foreign_context(context, device)?; + self.alias_guard(context); + self.mismatches += u64::from(read_pair(&self.outputs[2], &self.outputs[3])? != before); + Ok(()) + } +} +/// Allocate the operands, run every check and attempt every owned release even after any primary refusal. +fn with_rig(context: &Context<'_>, device: &Device<'_>) -> Result<(u64, u32)> { + let mut rig = Rig::allocate(context)?; + let operation = rig.run_checks(context, device); + let (mismatches, guards) = (rig.mismatches, rig.guards); + let mut cleanup = Ok(()); + for buffer in rig.outputs.into_iter().chain(rig.uploaded).chain(rig.inputs) { + cleanup = combine(cleanup, buffer.release()); + } + combine(operation, cleanup).map(|()| (mismatches, guards)) +} #[test] #[ignore = "Root-only new GDN whole-product transport boundary"] fn original_first_gdn_full_products_exact_and_whole_owner_guards() { @@ -26,192 +229,7 @@ fn original_first_gdn_full_products_exact_and_whole_owner_guards() { let driver = Driver::load().expect("installed driver"); let device = driver.device(0).expect("device"); let context = device.create_context(24 * 1024 * 1024).expect("bounded context"); - let result: Result<(u64, u32)> = (|| { - let mut inputs = [ - context.allocate(3145728)?, - context.allocate(40960)?, - context.allocate(40960)?, - context.allocate(192)?, - context.allocate(192)?, - ]; - let mut uploaded = [ - context.allocate(8192)?, - context.allocate(8192)?, - context.allocate(24576)?, - context.allocate(192)?, - context.allocate(192)?, - ]; - let mut outputs = [ - context.allocate(3145728)?, - context.allocate(24576)?, - context.allocate(3145728)?, - context.allocate(24576)?, - ]; - let operation: Result<(u64, u32)> = (|| { - let mut mismatches = 0u64; - let mut guards = 0u32; - let mut last_expected = None; - let mut last_qk = Vec::new(); - let mut last_h = Vec::new(); - for pattern in 0..2usize { - let state = encode((0..786432usize).map(|i| { - if pattern == 0 { - if i % 2 == 0 { 0.0f32 } else { -0.0f32 } - } else { - ((i % 13) as f32 - 6.0) / 1024.0 - } - })); - let qk = encode((0..10240usize).map(|i| { - if i >= 4096 { - 17.0f32 + (i % 7) as f32 - } else if pattern == 0 { - if i % 2 == 0 { 0.0f32 } else { -0.0f32 } - } else { - ((i % 11) as f32 - 5.0) / 64.0 - } - })); - let h = encode((0..10240usize).map(|i| { - if i < 4096 { - -23.0f32 - (i % 9) as f32 - } else if pattern == 0 { - if i % 2 == 0 { 0.0f32 } else { -0.0f32 } - } else { - ((i % 29) as f32 - 14.0) / 32.0 - } - })); - let gate = encode((0..48usize).map(|i| -0.0625f32 * (1 + (i % 5)) as f32)); - let beta = encode((0..48usize).map(|i| 0.125f32 + (i % 7) as f32 / 16.0)); - inputs[0].copy_from(&state)?; - // Independent OLD marshaling FIRST from canonical host - // slices, not candidate handles or fitted expected math. - uploaded[0].copy_from(&qk[..8192])?; - uploaded[1].copy_from(&qk[8192..16384])?; - uploaded[2].copy_from(&h[16384..])?; - uploaded[3].copy_from(&gate)?; - uploaded[4].copy_from(&beta)?; - { - let [next, output, _, _] = &mut outputs; - context.gdn_step_f32( - &inputs[0], - &uploaded[0], - &uploaded[1], - &uploaded[2], - &uploaded[3], - &uploaded[4], - next, - output, - )?; - } - let expected = read_pair(&outputs[0], &outputs[1])?; - if expected.iter().any(|bytes| { - bytes.chunks_exact(4).any(|b| !f32::from_le_bytes(b.try_into().expect("four bytes")).is_finite()) - }) { - mismatches += 1; - } - inputs[1].copy_from(&qk)?; - inputs[2].copy_from(&h)?; - inputs[3].copy_from(&gate)?; - inputs[4].copy_from(&beta)?; - { - let [_, _, next, output] = &mut outputs; - context.gdn_step_from_products_f32( - &inputs[0], &inputs[1], &inputs[2], &inputs[3], &inputs[4], next, output, - )?; - } - mismatches += count_difference(&expected, &read_pair(&outputs[2], &outputs[3])?); - last_expected = Some(expected); - last_qk = qk; - last_h = h; - } - // Only unused portions are poisoned. The same original old - // expected output proves exact fixed subrange association. - for bytes in last_qk[16384..].chunks_exact_mut(4) { - bytes.copy_from_slice(&f32::NAN.to_le_bytes()); - } - for bytes in last_h[..16384].chunks_exact_mut(4) { - bytes.copy_from_slice(&f32::INFINITY.to_le_bytes()); - } - inputs[1].copy_from(&last_qk)?; - inputs[2].copy_from(&last_h)?; - { - let [_, _, next, output] = &mut outputs; - context.gdn_step_from_products_f32( - &inputs[0], &inputs[1], &inputs[2], &inputs[3], &inputs[4], next, output, - )?; - } - mismatches += - count_difference(&last_expected.ok_or(Error::InvalidQwenShape)?, &read_pair(&outputs[2], &outputs[3])?); - let before = read_pair(&outputs[2], &outputs[3])?; - // New whole-owner admission must reject both full shape gaps - // before writing either next-state or readout candidate. - let bad = context.allocate(40956)?; - let shape_operation: Result<()> = (|| { - let [_, _, next, output] = &mut outputs; - if context - .gdn_step_from_products_f32(&inputs[0], &bad, &inputs[2], &inputs[3], &inputs[4], next, output) - != Err(Error::InvalidQwenShape) - { - mismatches += 1; - } - if context - .gdn_step_from_products_f32(&inputs[0], &inputs[1], &bad, &inputs[3], &inputs[4], next, output) - != Err(Error::InvalidQwenShape) - { - mismatches += 1; - } - Ok(()) - })(); - combine(shape_operation, bad.release())?; - guards += 2; - let foreign_context = device.create_context(1024 * 1024)?; - let foreign = foreign_context.allocate(40960)?; - let context_operation: Result<()> = (|| { - let [_, _, next, output] = &mut outputs; - if context - .gdn_step_from_products_f32(&inputs[0], &foreign, &inputs[2], &inputs[3], &inputs[4], next, output) - != Err(Error::ContextMismatch) - { - mismatches += 1; - } - Ok(()) - })(); - let context_operation = combine(context_operation, foreign.release()); - combine(context_operation, foreign_context.close())?; - guards += 1; - // Safe public mutable references cannot express state==next. - // Test the SAME private whole-owner admission with duplicated - // actual immutable owner refs; no forged buffer or launch. - let mut whole = Operation::GdnStep.layout(); - whole.bytes = [3145728, 40960, 40960, 192, 192, 3145728, 24576, 0, 0]; - whole.align = [4; 9]; - whole.count = 7; - whole.outputs = 0b1100000; - let alias = admission( - &context, - &[&inputs[0], &inputs[1], &inputs[2], &inputs[3], &inputs[4], &inputs[0], &outputs[3]], - Admission { - operation: Operation::GdnStep, - layout: Ok(whole), - asset: qwen_ffi::asset(Operation::GdnStep), - epsilon: 0.0, - in_place: false, - }, - ); - if !matches!(alias, Err(Error::OutputAliasesInput)) { - mismatches += 1; - } - guards += 1; - if read_pair(&outputs[2], &outputs[3])? != before { - mismatches += 1; - } - Ok((mismatches, guards)) - })(); - let mut cleanup = Ok(()); - for buffer in outputs.into_iter().chain(uploaded).chain(inputs) { - cleanup = combine(cleanup, buffer.release()); - } - combine(operation, cleanup) - })(); + let result = with_rig(&context, &device); let allocated = context.allocated_bytes(); let result = combine(result, context.close()); let (mismatches, guards) = result.expect("completed changed GDN boundary and cleanup"); @@ -219,6 +237,7 @@ fn original_first_gdn_full_products_exact_and_whole_owner_guards() { assert_eq!(mismatches, 0); assert_eq!(guards, 4); eprintln!( - "GDN_PRODUCTS PASS finite_words=1585152 unused_tail_words=792576 exact_words=2377728 old_first=true next_state_and_readout=true guards={guards} allocated0=true" + "GDN_PRODUCTS PASS finite_words=1585152 unused_tail_words=792576 exact_words=2377728 old_first=true \ + next_state_and_readout=true guards={guards} allocated0=true" ); } diff --git a/crates/cuda-driver/src/qwen_dispatch/queued_gr_installed.rs b/crates/cuda-driver/src/qwen_dispatch/queued_gr_installed.rs index d3f4418c..486daf7d 100644 --- a/crates/cuda-driver/src/qwen_dispatch/queued_gr_installed.rs +++ b/crates/cuda-driver/src/qwen_dispatch/queued_gr_installed.rs @@ -42,6 +42,135 @@ fn residual_bytes(pattern: usize) -> Vec { }) .collect() } +/// Allocate one buffer per size, in order; a failure drops the ones allocated so far. +fn allocate_all<'a, const N: usize>(context: &'a Context<'_>, sizes: [usize; N]) -> Result<[DeviceBuffer<'a>; N]> { + let mut buffers = Vec::with_capacity(N); + for size in sizes { + buffers.push(context.allocate(size)?); + } + buffers.try_into().map_err(|_| Error::InvalidQwenShape) +} +/// Compare the bytes of two output sets word by word. +fn word_mismatches(expected: &[Vec], actual: &[Vec]) -> u64 { + expected + .iter() + .zip(actual) + .map(|(x, y)| x.chunks_exact(4).zip(y.chunks_exact(4)).filter(|(a, b)| a != b).count() as u64) + .sum() +} +/// The norm weights and the two BF16 projection weights. +fn fill_inputs(inputs: &mut [DeviceBuffer<'_>; 4]) -> Result<()> { + let gamma: Vec = (0..10240usize).flat_map(|i| (1.0f32 + (i % 7) as f32 / 16.0).to_le_bytes()).collect(); + inputs[1].copy_from(&gamma)?; + // Sanitized exact BF16 powers and sign bits, independent of + // candidate outputs. Original admitted kernels are the oracle. + for (role, weight) in inputs[2..].iter_mut().enumerate() { + let bytes: Vec<_> = (0..3276800usize) + .flat_map(|i| { + let bits = if (i + role) % 3 == 0 { 0x3b00u16 } else { 0xbb00u16 }; + bits.to_le_bytes() + }) + .collect(); + weight.copy_from(&bytes)?; + } + Ok(()) +} +/// The original chain against the queued one on two residual patterns; the mismatches, and the outputs of the last +/// pattern. +fn compare_patterns( + context: &Context<'_>, + inputs: &mut [DeviceBuffer<'_>; 4], + outputs: &mut [DeviceBuffer<'_>; 6], +) -> Result<(u64, Vec>)> { + let mut mismatches = 0u64; + let mut reset_expected = Vec::new(); + for pattern in 0..2 { + let (count, expected) = compare_pattern(context, inputs, outputs, pattern)?; + mismatches += count; + reset_expected = expected; + } + Ok((mismatches, reset_expected)) +} +/// One residual pattern: the original chain against the queued one; the mismatches and the original outputs. +fn compare_pattern( + context: &Context<'_>, + inputs: &mut [DeviceBuffer<'_>; 4], + outputs: &mut [DeviceBuffer<'_>; 6], + pattern: usize, +) -> Result<(u64, Vec>)> { + inputs[0].copy_from(&residual_bytes(pattern))?; + let original_mask = execute_read(context, inputs, outputs, false, 1e-6)?; + let expected = read_outputs(outputs)?; + let candidate_mask = execute_read(context, inputs, outputs, true, 1e-6)?; + let actual = read_outputs(outputs)?; + let mask_mismatch = original_mask != 0 || candidate_mask != original_mask; + Ok((u64::from(mask_mismatch) + word_mismatches(&expected, &actual), expected)) +} +/// A negative epsilon is refused and leaves every output as it was. +fn check_bad_epsilon( + context: &Context<'_>, + inputs: &[DeviceBuffer<'_>; 4], + outputs: &mut [DeviceBuffer<'_>; 6], +) -> Result { + let before = read_outputs(outputs)?; + let mut mismatches = 0; + if execute_read(context, inputs, outputs, true, -1.0) != Err(Error::InvalidQwenShape) { + mismatches += 1; + } + if read_outputs(outputs)? != before { + mismatches += 1; + } + Ok(mismatches) +} +/// Full-state late NaN: compare actual original finite mask. +fn check_late_nan( + context: &Context<'_>, + inputs: &mut [DeviceBuffer<'_>; 4], + outputs: &mut [DeviceBuffer<'_>; 6], +) -> Result { + let mut invalid = residual_bytes(1); + invalid[40956..].copy_from_slice(&f32::NAN.to_le_bytes()); + inputs[0].copy_from(&invalid)?; + let old = execute_read(context, inputs, outputs, false, 1e-6)?; + let queued = execute_read(context, inputs, outputs, true, 1e-6)?; + Ok(u64::from(old == 0 || old != queued)) +} +/// Prove the dedicated status reset by a valid recovery. +fn check_recovery( + context: &Context<'_>, + inputs: &mut [DeviceBuffer<'_>; 4], + outputs: &mut [DeviceBuffer<'_>; 6], + reset_expected: Vec>, +) -> Result { + inputs[0].copy_from(&residual_bytes(1))?; + let bad = execute_read(context, inputs, outputs, true, 1e-6)? != 0 || read_outputs(outputs)? != reset_expected; + Ok(u64::from(bad)) +} +/// Every check of the queued chain; the mismatches and the number of guards exercised. +fn run_checks( + context: &Context<'_>, + inputs: &mut [DeviceBuffer<'_>; 4], + outputs: &mut [DeviceBuffer<'_>; 6], +) -> Result<(u64, u32)> { + fill_inputs(inputs)?; + let (mut mismatches, reset_expected) = compare_patterns(context, inputs, outputs)?; + mismatches += check_bad_epsilon(context, inputs, outputs)?; + mismatches += check_late_nan(context, inputs, outputs)?; + mismatches += check_recovery(context, inputs, outputs, reset_expected)?; + mismatches += u64::from(outputs.iter().map(|b| b.bytes).ne(OUTPUT_BYTES)); + Ok((mismatches, 3)) +} +/// Allocate the operands, run every check and attempt every owned release even after any primary refusal. +fn with_operands(context: &Context<'_>) -> Result<(u64, u32)> { + let mut inputs = allocate_all(context, [40960, 40960, 6553600, 6553600])?; + let mut outputs = allocate_all(context, [40960, 1280, 1280, 40960, 40960, 10240])?; + let operation = run_checks(context, &mut inputs, &mut outputs); + let mut cleanup = Ok(()); + for buffer in outputs.into_iter().chain(inputs) { + cleanup = combine(cleanup, buffer.release()); + } + combine(operation, cleanup) +} #[test] #[ignore = "Root-only new same-kernel queued GR completion boundary"] fn original_first_queued_gr_read_exact_and_status_reset() { @@ -49,93 +178,7 @@ fn original_first_queued_gr_read_exact_and_status_reset() { let driver = Driver::load().expect("installed driver"); let device = driver.device(0).expect("device"); let context = device.create_context(32 * 1024 * 1024).expect("bounded context"); - let result: Result<(u64, u32)> = (|| { - let mut inputs = [ - context.allocate(40960)?, - context.allocate(40960)?, - context.allocate(6553600)?, - context.allocate(6553600)?, - ]; - let mut outputs = [ - context.allocate(40960)?, - context.allocate(1280)?, - context.allocate(1280)?, - context.allocate(40960)?, - context.allocate(40960)?, - context.allocate(10240)?, - ]; - let operation: Result<(u64, u32)> = (|| { - inputs[1].copy_from( - &(0..10240usize).flat_map(|i| (1.0f32 + (i % 7) as f32 / 16.0).to_le_bytes()).collect::>(), - )?; - // Sanitized exact BF16 powers and sign bits, independent of - // candidate outputs. Original admitted kernels are the oracle. - for (role, weight) in inputs[2..].iter_mut().enumerate() { - let bytes: Vec<_> = (0..3276800usize) - .flat_map(|i| { - let bits = if (i + role) % 3 == 0 { 0x3b00u16 } else { 0xbb00u16 }; - bits.to_le_bytes() - }) - .collect(); - weight.copy_from(&bytes)?; - } - let mut mismatches = 0u64; - let mut guards = 0u32; - let mut reset_expected = None; - for pattern in 0..2 { - inputs[0].copy_from(&residual_bytes(pattern))?; - let original_mask = execute_read(&context, &inputs, &mut outputs, false, 1e-6)?; - let expected = read_outputs(&outputs)?; - let candidate_mask = execute_read(&context, &inputs, &mut outputs, true, 1e-6)?; - let actual = read_outputs(&outputs)?; - if original_mask != 0 || candidate_mask != original_mask { - mismatches += 1; - } - for (x, y) in expected.iter().zip(&actual) { - mismatches += x.chunks_exact(4).zip(y.chunks_exact(4)).filter(|(a, b)| a != b).count() as u64; - } - if pattern == 1 { - reset_expected = Some(expected); - } - } - let before = read_outputs(&outputs)?; - if execute_read(&context, &inputs, &mut outputs, true, -1.0) != Err(Error::InvalidQwenShape) { - mismatches += 1; - } - if read_outputs(&outputs)? != before { - mismatches += 1; - } - guards += 1; - // Full-state late NaN: compare actual original finite mask, - // then prove dedicated status reset by a valid recovery. - let mut invalid = residual_bytes(1); - invalid[40956..].copy_from_slice(&f32::NAN.to_le_bytes()); - inputs[0].copy_from(&invalid)?; - let old = execute_read(&context, &inputs, &mut outputs, false, 1e-6)?; - let queued = execute_read(&context, &inputs, &mut outputs, true, 1e-6)?; - if old == 0 || old != queued { - mismatches += 1; - } - guards += 1; - inputs[0].copy_from(&residual_bytes(1))?; - if execute_read(&context, &inputs, &mut outputs, true, 1e-6)? != 0 - || read_outputs(&outputs)? != reset_expected.ok_or(Error::InvalidQwenShape)? - { - mismatches += 1; - } - guards += 1; - if outputs.iter().map(|b| b.bytes).ne(OUTPUT_BYTES) { - mismatches += 1; - } - Ok((mismatches, guards)) - })(); - // Attempt every owned release even after any primary refusal. - let mut cleanup = Ok(()); - for buffer in outputs.into_iter().chain(inputs) { - cleanup = combine(cleanup, buffer.release()); - } - combine(operation, cleanup) - })(); + let result = with_operands(&context); let allocated = context.allocated_bytes(); let result = combine(result, context.close()); let (mismatches, guards) = result.expect("completed new boundary with exhaustive cleanup"); @@ -143,6 +186,7 @@ fn original_first_queued_gr_read_exact_and_status_reset() { assert_eq!(mismatches, 0); assert_eq!(guards, 3); eprintln!( - "QUEUED_GR PASS exact_words=67840 old_first=true all6_outputs=true status_reset=true guards={guards} allocated0=true" + "QUEUED_GR PASS exact_words=67840 old_first=true all6_outputs=true status_reset=true guards={guards} \ + allocated0=true" ); }