STRA-338 size2 cd7: the grouped-workspace and columns-loan fixtures move their cases into child files

This commit is contained in:
Pavlo Ratushnyi
2026-11-06 23:04:03 +01:00
parent f7d3ff8b69
commit 7a848a87cb
5 changed files with 705 additions and 679 deletions
@@ -87,325 +87,6 @@ fn check_table(loan: &RoutedColumnsLoan<'_, '_, '_, '_>, report: &mut Report) ->
}
Ok(())
}
/// The owners of one case besides the inputs, released together after the run.
#[derive(Default)]
struct CaseOwners<'a> {
gate: Option<DeviceBuffer<'a>>,
up: Option<DeviceBuffer<'a>>,
foreign_weight: Option<DeviceBuffer<'a>>,
original: Option<DeviceBuffer<'a>>,
output: Option<DeviceBuffer<'a>>,
short: Option<DeviceBuffer<'a>>,
dual_gate: Option<MmqWeights<'a>>,
dual_up: Option<MmqWeights<'a>>,
}
/// The weights and products of one case, with its format and column count.
struct LoanIo<'b, 'a> {
gate: &'b DeviceBuffer<'a>,
up: &'b DeviceBuffer<'a>,
foreign_weight: &'b DeviceBuffer<'a>,
original: &'b mut DeviceBuffer<'a>,
output: &'b mut DeviceBuffer<'a>,
short: &'b mut DeviceBuffer<'a>,
format: RoutedFormat,
total: usize,
}
fn release_case(owners: CaseOwners<'_>, inputs: Vec<DeviceBuffer<'_>>) -> Result<()> {
let mut cleanup = Ok(());
for buffer in owners
.gate
.into_iter()
.chain(owners.up)
.chain(owners.foreign_weight)
.chain(owners.original)
.chain(owners.output)
.chain(owners.short)
.chain(inputs)
{
cleanup = combine(cleanup, buffer.release());
}
for owner in owners.dual_gate.into_iter().chain(owners.dual_up) {
cleanup = combine(cleanup, owner.close());
}
cleanup
}
/// The finite flags of a stage are clear and the output equals the original product.
fn verify_stage(
finite: &mut RoutedStageFiniteGuard<'_>,
output: &DeviceBuffer<'_>,
(stage, total): (RoutedFiniteStage, usize),
(expected, report): (&[u8], &mut Report),
) -> Result<()> {
report.witness_errors += usize::from(!finite.check_stage(output, stage, total)?);
compare(expected, &read(output)?, report);
Ok(())
}
fn case(
context: &Context<'_>,
foreign: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
format: RoutedFormat,
total: usize,
report: &mut Report,
) -> Result<()> {
let mut owners = CaseOwners::default();
let mut inputs = Vec::new();
let contexts = (context, foreign);
let operation = case_run(contexts, workspace, finite, (&mut owners, &mut inputs), (format, total), report);
combine(operation, release_case(owners, inputs))
}
fn case_run<'a>(
(context, foreign): (&'a Context<'_>, &'a Context<'_>),
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
(owners, inputs): (&mut CaseOwners<'a>, &mut Vec<DeviceBuffer<'a>>),
(format, total): (RoutedFormat, usize),
report: &mut Report,
) -> Result<()> {
let gate_raw = raw(format, 11);
let up_raw = raw(format, 79);
owners.gate = Some(upload(context, &gate_raw)?);
owners.up = Some(upload(context, &up_raw)?);
owners.foreign_weight = Some(upload(foreign, &gate_raw)?);
for i in 0..total {
inputs.push(upload(context, &input(i))?);
}
let prepared = inputs.iter().map(|input| context.prepare_routed_gate_up_q8(input)).collect::<Result<Vec<_>>>()?;
let raws = (&gate_raw[..], &up_raw[..]);
let operation = case_products(context, workspace, finite, owners, (&prepared, raws), (format, total), report);
let mut cleanup = Ok(());
for owner in prepared {
cleanup = combine(cleanup, owner.close());
}
combine(operation, cleanup)
}
fn case_products<'a>(
context: &'a Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
owners: &mut CaseOwners<'a>,
(prepared, raws): (&[RoutedGateUpQ8<'_>], (&[u8], &[u8])),
(format, total): (RoutedFormat, usize),
report: &mut Report,
) -> Result<()> {
let mut refs: Vec<_> = prepared.iter().collect();
owners.allocate_products(context, total)?;
let (mut io, duals) = owners.split(format, total)?;
// Original Gate/Up completion and complete finite bytes precede loan.
let expected = original_pair(context, &mut io, &refs)?;
let allocated = context.allocated_bytes();
check_loan(context, workspace, finite, &mut io, &refs, (&expected, allocated), report)?;
if total == 64 {
refs.reverse();
fresh_loan(context, workspace, finite, &mut io, &refs, report)?;
dual_loan(context, workspace, finite, (duals, &mut io), (&refs, raws), report)?;
}
invalid_columns(workspace, &mut io, &prepared[0], report)
}
/// The dual-format weights slots of a case: gate and up.
type DualSlots<'b, 'a> = (&'b mut Option<MmqWeights<'a>>, &'b mut Option<MmqWeights<'a>>);
impl<'a> CaseOwners<'a> {
/// The original, workspace and short products of a case.
fn allocate_products(&mut self, context: &'a Context<'_>, total: usize) -> Result<()> {
self.original = Some(context.allocate(2560 * total)?);
self.output = Some(context.allocate(2560 * total)?);
self.short = Some(context.allocate(2560 * total - 4)?);
Ok(())
}
/// The owners as the weights and products of a case, and the slots for the dual-format weights.
fn split(&mut self, format: RoutedFormat, total: usize) -> Result<(LoanIo<'_, 'a>, DualSlots<'_, 'a>)> {
let gate = self.gate.as_ref().ok_or(Error::ContextPoisoned)?;
let up = self.up.as_ref().ok_or(Error::ContextPoisoned)?;
let foreign_weight = self.foreign_weight.as_ref().ok_or(Error::ContextPoisoned)?;
let original = self.original.as_mut().ok_or(Error::ContextPoisoned)?;
let output = self.output.as_mut().ok_or(Error::ContextPoisoned)?;
let short = self.short.as_mut().ok_or(Error::ContextPoisoned)?;
let io = LoanIo { gate, up, foreign_weight, original, output, short, format, total };
Ok((io, (&mut self.dual_gate, &mut self.dual_up)))
}
}
/// The original grouped gate and up products of the columns, read back.
fn original_pair(
context: &Context<'_>,
io: &mut LoanIo<'_, '_>,
refs: &[&RoutedGateUpQ8<'_>],
) -> Result<(Vec<u8>, Vec<u8>)> {
context.project_routed_gate_up_columns(io.gate, refs, io.original, io.format)?;
let expected_gate = read(io.original)?;
context.project_routed_gate_up_columns(io.up, refs, io.original, io.format)?;
Ok((expected_gate, read(io.original)?))
}
/// The loan projects gate and up like the original path, refuses a short output and a foreign weight, and
/// keeps the device allocation unchanged.
fn check_loan(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
io: &mut LoanIo<'_, '_>,
refs: &[&RoutedGateUpQ8<'_>],
(expected, allocated): (&(Vec<u8>, Vec<u8>), usize),
report: &mut Report,
) -> Result<()> {
let mut loan = workspace.begin_columns(refs);
loan_gate(&mut loan, finite, io, &expected.0, report)?;
loan_foreign_and_up(&mut loan, finite, io, &expected.1, report)?;
drop(loan);
report.witness_errors += usize::from(context.allocated_bytes() != allocated || workspace.is_failed());
Ok(())
}
fn loan_gate(
loan: &mut RoutedColumnsLoan<'_, '_, '_, '_>,
finite: &mut RoutedStageFiniteGuard<'_>,
io: &mut LoanIo<'_, '_>,
expected: &[u8],
report: &mut Report,
) -> Result<()> {
report.witness_errors += usize::from(loan.table_uploaded);
refuse(loan.project_gate_up(io.gate, io.short, io.format), Error::InvalidProjectionShape, report)?;
report.witness_errors += usize::from(loan.table_uploaded);
loan.project_gate_up(io.gate, io.output, io.format)?;
verify_stage(finite, io.output, (RoutedFiniteStage::Gate, io.total), (expected, report))?;
check_table(loan, report)
}
fn loan_foreign_and_up(
loan: &mut RoutedColumnsLoan<'_, '_, '_, '_>,
finite: &mut RoutedStageFiniteGuard<'_>,
io: &mut LoanIo<'_, '_>,
expected: &[u8],
report: &mut Report,
) -> Result<()> {
let saved = read(io.output)?;
refuse(loan.project_gate_up(io.foreign_weight, io.output, io.format), Error::ContextMismatch, report)?;
report.witness_errors += usize::from(read(io.output)? != saved);
loan.project_gate_up(io.up, io.output, io.format)?;
verify_stage(finite, io.output, (RoutedFiniteStage::Up, io.total), (expected, report))?;
check_table(loan, report)
}
/// A new same-count loan must overwrite all pointers, never hit by shape.
fn fresh_loan(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
io: &mut LoanIo<'_, '_>,
refs: &[&RoutedGateUpQ8<'_>],
report: &mut Report,
) -> Result<()> {
let (expected_gate, expected_up) = original_pair(context, io, refs)?;
let mut fresh = workspace.begin_columns(refs);
report.witness_errors += usize::from(fresh.table_uploaded);
fresh.project_gate_up(io.gate, io.output, io.format)?;
verify_stage(finite, io.output, (RoutedFiniteStage::Gate, io.total), (&expected_gate, report))?;
check_table(&fresh, report)?;
fresh.project_gate_up(io.up, io.output, io.format)?;
verify_stage(finite, io.output, (RoutedFiniteStage::Up, io.total), (&expected_up, report))
}
/// The dual-format weights of the formats that have them.
fn dual_format_of(format: RoutedFormat) -> Option<MmqFormat> {
match format {
RoutedFormat::IQ3_XXS => Some(MmqFormat::Iq3XxsGateUp),
RoutedFormat::IQ2_S => Some(MmqFormat::Iq2SGateUp),
_ => None,
}
}
/// The native dual-format loans agree with the original path (the two gate/up formats that have one).
fn dual_loan<'a>(
context: &'a Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
((dual_gate, dual_up), io): (DualSlots<'_, 'a>, &mut LoanIo<'_, '_>),
(refs, (gate_raw, up_raw)): (&[&RoutedGateUpQ8<'_>], (&[u8], &[u8])),
report: &mut Report,
) -> Result<()> {
let Some(kind) = dual_format_of(io.format) else { return Ok(()) };
*dual_gate = Some(context.mmq_weights(kind, gate_raw)?);
*dual_up = Some(context.mmq_weights(kind, up_raw)?);
let gate = dual_gate.as_ref().ok_or(Error::ContextPoisoned)?;
let up = dual_up.as_ref().ok_or(Error::ContextPoisoned)?;
dual_stages(workspace, finite, (gate, up), (refs, io), report)
}
fn dual_stages(
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
(gate, up): (&MmqWeights<'_>, &MmqWeights<'_>),
(refs, io): (&[&RoutedGateUpQ8<'_>], &mut LoanIo<'_, '_>),
report: &mut Report,
) -> Result<()> {
gate.native_gate_up_columns(refs, io.original)?;
let expected_gate = read(io.original)?;
up.native_gate_up_columns(refs, io.original)?;
let expected_up = read(io.original)?;
let mut loan = workspace.begin_columns(refs);
gate.native_gate_up_columns_loan(&mut loan, io.output)?;
verify_stage(finite, io.output, (RoutedFiniteStage::Gate, io.total), (&expected_gate, report))?;
up.native_gate_up_columns_loan(&mut loan, io.output)?;
verify_stage(finite, io.output, (RoutedFiniteStage::Up, io.total), (&expected_up, report))?;
check_table(&loan, report)
}
/// No columns and too many columns are refused.
fn invalid_columns(
workspace: &mut RoutedGroupWorkspace<'_>,
io: &mut LoanIo<'_, '_>,
first: &RoutedGateUpQ8<'_>,
report: &mut Report,
) -> Result<()> {
for refs in [Vec::new(), vec![first; 65]] {
let mut invalid = workspace.begin_columns(&refs);
refuse(invalid.project_gate_up(io.gate, io.output, io.format), Error::InvalidProjectionShape, report)?;
report.witness_errors += usize::from(invalid.table_uploaded);
}
Ok(())
}
fn mixed_pair(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
report: &mut Report,
) -> Result<()> {
let input = upload(context, &input(3))?;
let gate = upload(context, &raw(RoutedFormat::IQ3_S, 11))?;
let up = upload(context, &raw(RoutedFormat::IQ4_XS, 79))?;
let mut original = context.allocate(2560)?;
let mut output = context.allocate(2560)?;
let weights = (&gate, &up);
let operation = mixed_run(context, workspace, finite, (&input, weights), (&mut original, &mut output), report);
let mut cleanup = Ok(());
for buffer in [input, gate, up, original, output] {
cleanup = combine(cleanup, buffer.release());
}
combine(operation, cleanup)
}
fn mixed_run(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
(input, weights): (&DeviceBuffer<'_>, (&DeviceBuffer<'_>, &DeviceBuffer<'_>)),
products: (&mut DeviceBuffer<'_>, &mut DeviceBuffer<'_>),
report: &mut Report,
) -> Result<()> {
let owner = context.prepare_routed_gate_up_q8(input)?;
let operation = mixed_stages(context, workspace, finite, (&[&owner], weights), products, report);
combine(operation, owner.close())
}
/// A gate of one format and an up of another, through one loan.
fn mixed_stages(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
(refs, (gate, up)): (&[&RoutedGateUpQ8<'_>], (&DeviceBuffer<'_>, &DeviceBuffer<'_>)),
(original, output): (&mut DeviceBuffer<'_>, &mut DeviceBuffer<'_>),
report: &mut Report,
) -> Result<()> {
context.project_routed_gate_up_columns(gate, refs, original, RoutedFormat::IQ3_S)?;
let expected_gate = read(original)?;
context.project_routed_gate_up_columns(up, refs, original, RoutedFormat::IQ4_XS)?;
let expected_up = read(original)?;
let mut loan = workspace.begin_columns(refs);
loan.project_gate_up(gate, output, RoutedFormat::IQ3_S)?;
verify_stage(finite, output, (RoutedFiniteStage::Gate, 1), (&expected_gate, report))?;
loan.project_gate_up(up, output, RoutedFormat::IQ4_XS)?;
verify_stage(finite, output, (RoutedFiniteStage::Up, 1), (&expected_up, report))
}
fn run(context: &Context<'_>, foreign: &Context<'_>) -> Result<Report> {
let mut workspace = Some(context.routed_group_workspace()?);
let mut finite = None;
@@ -462,3 +143,7 @@ fn original_first_columns_loan_gate_up_fresh_order_guards_and_cleanup() {
assert_eq!(report.guards, report.refusals);
eprintln!("ROUTED-COLUMNS-LOAN {report:?}; extra_device_bytes=0; cleanup=0; component_only=true");
}
#[path = "routed_columns_loan_gpu/case_fn.rs"]
mod case_fn;
use case_fn::{case, mixed_pair};
@@ -0,0 +1,324 @@
//! The gate/up cases of the borrowed-table proof: the original path first, then the loan.
#[allow(unused_imports)]
use super::*;
/// The owners of one case besides the inputs, released together after the run.
#[derive(Default)]
struct CaseOwners<'a> {
gate: Option<DeviceBuffer<'a>>,
up: Option<DeviceBuffer<'a>>,
foreign_weight: Option<DeviceBuffer<'a>>,
original: Option<DeviceBuffer<'a>>,
output: Option<DeviceBuffer<'a>>,
short: Option<DeviceBuffer<'a>>,
dual_gate: Option<MmqWeights<'a>>,
dual_up: Option<MmqWeights<'a>>,
}
/// The weights and products of one case, with its format and column count.
struct LoanIo<'b, 'a> {
gate: &'b DeviceBuffer<'a>,
up: &'b DeviceBuffer<'a>,
foreign_weight: &'b DeviceBuffer<'a>,
original: &'b mut DeviceBuffer<'a>,
output: &'b mut DeviceBuffer<'a>,
short: &'b mut DeviceBuffer<'a>,
format: RoutedFormat,
total: usize,
}
fn release_case(owners: CaseOwners<'_>, inputs: Vec<DeviceBuffer<'_>>) -> Result<()> {
let mut cleanup = Ok(());
for buffer in owners
.gate
.into_iter()
.chain(owners.up)
.chain(owners.foreign_weight)
.chain(owners.original)
.chain(owners.output)
.chain(owners.short)
.chain(inputs)
{
cleanup = combine(cleanup, buffer.release());
}
for owner in owners.dual_gate.into_iter().chain(owners.dual_up) {
cleanup = combine(cleanup, owner.close());
}
cleanup
}
/// The finite flags of a stage are clear and the output equals the original product.
fn verify_stage(
finite: &mut RoutedStageFiniteGuard<'_>,
output: &DeviceBuffer<'_>,
(stage, total): (RoutedFiniteStage, usize),
(expected, report): (&[u8], &mut Report),
) -> Result<()> {
report.witness_errors += usize::from(!finite.check_stage(output, stage, total)?);
compare(expected, &read(output)?, report);
Ok(())
}
pub(super) fn case(
context: &Context<'_>,
foreign: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
format: RoutedFormat,
total: usize,
report: &mut Report,
) -> Result<()> {
let mut owners = CaseOwners::default();
let mut inputs = Vec::new();
let contexts = (context, foreign);
let operation = case_run(contexts, workspace, finite, (&mut owners, &mut inputs), (format, total), report);
combine(operation, release_case(owners, inputs))
}
fn case_run<'a>(
(context, foreign): (&'a Context<'_>, &'a Context<'_>),
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
(owners, inputs): (&mut CaseOwners<'a>, &mut Vec<DeviceBuffer<'a>>),
(format, total): (RoutedFormat, usize),
report: &mut Report,
) -> Result<()> {
let gate_raw = raw(format, 11);
let up_raw = raw(format, 79);
owners.gate = Some(upload(context, &gate_raw)?);
owners.up = Some(upload(context, &up_raw)?);
owners.foreign_weight = Some(upload(foreign, &gate_raw)?);
for i in 0..total {
inputs.push(upload(context, &input(i))?);
}
let prepared = inputs.iter().map(|input| context.prepare_routed_gate_up_q8(input)).collect::<Result<Vec<_>>>()?;
let raws = (&gate_raw[..], &up_raw[..]);
let operation = case_products(context, workspace, finite, owners, (&prepared, raws), (format, total), report);
let mut cleanup = Ok(());
for owner in prepared {
cleanup = combine(cleanup, owner.close());
}
combine(operation, cleanup)
}
fn case_products<'a>(
context: &'a Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
owners: &mut CaseOwners<'a>,
(prepared, raws): (&[RoutedGateUpQ8<'_>], (&[u8], &[u8])),
(format, total): (RoutedFormat, usize),
report: &mut Report,
) -> Result<()> {
let mut refs: Vec<_> = prepared.iter().collect();
owners.allocate_products(context, total)?;
let (mut io, duals) = owners.split(format, total)?;
// Original Gate/Up completion and complete finite bytes precede loan.
let expected = original_pair(context, &mut io, &refs)?;
let allocated = context.allocated_bytes();
check_loan(context, workspace, finite, &mut io, &refs, (&expected, allocated), report)?;
if total == 64 {
refs.reverse();
fresh_loan(context, workspace, finite, &mut io, &refs, report)?;
dual_loan(context, workspace, finite, (duals, &mut io), (&refs, raws), report)?;
}
invalid_columns(workspace, &mut io, &prepared[0], report)
}
/// The dual-format weights slots of a case: gate and up.
type DualSlots<'b, 'a> = (&'b mut Option<MmqWeights<'a>>, &'b mut Option<MmqWeights<'a>>);
impl<'a> CaseOwners<'a> {
/// The original, workspace and short products of a case.
fn allocate_products(&mut self, context: &'a Context<'_>, total: usize) -> Result<()> {
self.original = Some(context.allocate(2560 * total)?);
self.output = Some(context.allocate(2560 * total)?);
self.short = Some(context.allocate(2560 * total - 4)?);
Ok(())
}
/// The owners as the weights and products of a case, and the slots for the dual-format weights.
fn split(&mut self, format: RoutedFormat, total: usize) -> Result<(LoanIo<'_, 'a>, DualSlots<'_, 'a>)> {
let gate = self.gate.as_ref().ok_or(Error::ContextPoisoned)?;
let up = self.up.as_ref().ok_or(Error::ContextPoisoned)?;
let foreign_weight = self.foreign_weight.as_ref().ok_or(Error::ContextPoisoned)?;
let original = self.original.as_mut().ok_or(Error::ContextPoisoned)?;
let output = self.output.as_mut().ok_or(Error::ContextPoisoned)?;
let short = self.short.as_mut().ok_or(Error::ContextPoisoned)?;
let io = LoanIo { gate, up, foreign_weight, original, output, short, format, total };
Ok((io, (&mut self.dual_gate, &mut self.dual_up)))
}
}
/// The original grouped gate and up products of the columns, read back.
fn original_pair(
context: &Context<'_>,
io: &mut LoanIo<'_, '_>,
refs: &[&RoutedGateUpQ8<'_>],
) -> Result<(Vec<u8>, Vec<u8>)> {
context.project_routed_gate_up_columns(io.gate, refs, io.original, io.format)?;
let expected_gate = read(io.original)?;
context.project_routed_gate_up_columns(io.up, refs, io.original, io.format)?;
Ok((expected_gate, read(io.original)?))
}
/// The loan projects gate and up like the original path, refuses a short output and a foreign weight, and
/// keeps the device allocation unchanged.
fn check_loan(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
io: &mut LoanIo<'_, '_>,
refs: &[&RoutedGateUpQ8<'_>],
(expected, allocated): (&(Vec<u8>, Vec<u8>), usize),
report: &mut Report,
) -> Result<()> {
let mut loan = workspace.begin_columns(refs);
loan_gate(&mut loan, finite, io, &expected.0, report)?;
loan_foreign_and_up(&mut loan, finite, io, &expected.1, report)?;
drop(loan);
report.witness_errors += usize::from(context.allocated_bytes() != allocated || workspace.is_failed());
Ok(())
}
fn loan_gate(
loan: &mut RoutedColumnsLoan<'_, '_, '_, '_>,
finite: &mut RoutedStageFiniteGuard<'_>,
io: &mut LoanIo<'_, '_>,
expected: &[u8],
report: &mut Report,
) -> Result<()> {
report.witness_errors += usize::from(loan.table_uploaded);
refuse(loan.project_gate_up(io.gate, io.short, io.format), Error::InvalidProjectionShape, report)?;
report.witness_errors += usize::from(loan.table_uploaded);
loan.project_gate_up(io.gate, io.output, io.format)?;
verify_stage(finite, io.output, (RoutedFiniteStage::Gate, io.total), (expected, report))?;
check_table(loan, report)
}
fn loan_foreign_and_up(
loan: &mut RoutedColumnsLoan<'_, '_, '_, '_>,
finite: &mut RoutedStageFiniteGuard<'_>,
io: &mut LoanIo<'_, '_>,
expected: &[u8],
report: &mut Report,
) -> Result<()> {
let saved = read(io.output)?;
refuse(loan.project_gate_up(io.foreign_weight, io.output, io.format), Error::ContextMismatch, report)?;
report.witness_errors += usize::from(read(io.output)? != saved);
loan.project_gate_up(io.up, io.output, io.format)?;
verify_stage(finite, io.output, (RoutedFiniteStage::Up, io.total), (expected, report))?;
check_table(loan, report)
}
/// A new same-count loan must overwrite all pointers, never hit by shape.
fn fresh_loan(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
io: &mut LoanIo<'_, '_>,
refs: &[&RoutedGateUpQ8<'_>],
report: &mut Report,
) -> Result<()> {
let (expected_gate, expected_up) = original_pair(context, io, refs)?;
let mut fresh = workspace.begin_columns(refs);
report.witness_errors += usize::from(fresh.table_uploaded);
fresh.project_gate_up(io.gate, io.output, io.format)?;
verify_stage(finite, io.output, (RoutedFiniteStage::Gate, io.total), (&expected_gate, report))?;
check_table(&fresh, report)?;
fresh.project_gate_up(io.up, io.output, io.format)?;
verify_stage(finite, io.output, (RoutedFiniteStage::Up, io.total), (&expected_up, report))
}
/// The dual-format weights of the formats that have them.
fn dual_format_of(format: RoutedFormat) -> Option<MmqFormat> {
match format {
RoutedFormat::IQ3_XXS => Some(MmqFormat::Iq3XxsGateUp),
RoutedFormat::IQ2_S => Some(MmqFormat::Iq2SGateUp),
_ => None,
}
}
/// The native dual-format loans agree with the original path (the two gate/up formats that have one).
fn dual_loan<'a>(
context: &'a Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
((dual_gate, dual_up), io): (DualSlots<'_, 'a>, &mut LoanIo<'_, '_>),
(refs, (gate_raw, up_raw)): (&[&RoutedGateUpQ8<'_>], (&[u8], &[u8])),
report: &mut Report,
) -> Result<()> {
let Some(kind) = dual_format_of(io.format) else { return Ok(()) };
*dual_gate = Some(context.mmq_weights(kind, gate_raw)?);
*dual_up = Some(context.mmq_weights(kind, up_raw)?);
let gate = dual_gate.as_ref().ok_or(Error::ContextPoisoned)?;
let up = dual_up.as_ref().ok_or(Error::ContextPoisoned)?;
dual_stages(workspace, finite, (gate, up), (refs, io), report)
}
fn dual_stages(
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
(gate, up): (&MmqWeights<'_>, &MmqWeights<'_>),
(refs, io): (&[&RoutedGateUpQ8<'_>], &mut LoanIo<'_, '_>),
report: &mut Report,
) -> Result<()> {
gate.native_gate_up_columns(refs, io.original)?;
let expected_gate = read(io.original)?;
up.native_gate_up_columns(refs, io.original)?;
let expected_up = read(io.original)?;
let mut loan = workspace.begin_columns(refs);
gate.native_gate_up_columns_loan(&mut loan, io.output)?;
verify_stage(finite, io.output, (RoutedFiniteStage::Gate, io.total), (&expected_gate, report))?;
up.native_gate_up_columns_loan(&mut loan, io.output)?;
verify_stage(finite, io.output, (RoutedFiniteStage::Up, io.total), (&expected_up, report))?;
check_table(&loan, report)
}
/// No columns and too many columns are refused.
fn invalid_columns(
workspace: &mut RoutedGroupWorkspace<'_>,
io: &mut LoanIo<'_, '_>,
first: &RoutedGateUpQ8<'_>,
report: &mut Report,
) -> Result<()> {
for refs in [Vec::new(), vec![first; 65]] {
let mut invalid = workspace.begin_columns(&refs);
refuse(invalid.project_gate_up(io.gate, io.output, io.format), Error::InvalidProjectionShape, report)?;
report.witness_errors += usize::from(invalid.table_uploaded);
}
Ok(())
}
pub(super) fn mixed_pair(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
report: &mut Report,
) -> Result<()> {
let input = upload(context, &input(3))?;
let gate = upload(context, &raw(RoutedFormat::IQ3_S, 11))?;
let up = upload(context, &raw(RoutedFormat::IQ4_XS, 79))?;
let mut original = context.allocate(2560)?;
let mut output = context.allocate(2560)?;
let weights = (&gate, &up);
let operation = mixed_run(context, workspace, finite, (&input, weights), (&mut original, &mut output), report);
let mut cleanup = Ok(());
for buffer in [input, gate, up, original, output] {
cleanup = combine(cleanup, buffer.release());
}
combine(operation, cleanup)
}
fn mixed_run(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
(input, weights): (&DeviceBuffer<'_>, (&DeviceBuffer<'_>, &DeviceBuffer<'_>)),
products: (&mut DeviceBuffer<'_>, &mut DeviceBuffer<'_>),
report: &mut Report,
) -> Result<()> {
let owner = context.prepare_routed_gate_up_q8(input)?;
let operation = mixed_stages(context, workspace, finite, (&[&owner], weights), products, report);
combine(operation, owner.close())
}
/// A gate of one format and an up of another, through one loan.
fn mixed_stages(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
finite: &mut RoutedStageFiniteGuard<'_>,
(refs, (gate, up)): (&[&RoutedGateUpQ8<'_>], (&DeviceBuffer<'_>, &DeviceBuffer<'_>)),
(original, output): (&mut DeviceBuffer<'_>, &mut DeviceBuffer<'_>),
report: &mut Report,
) -> Result<()> {
context.project_routed_gate_up_columns(gate, refs, original, RoutedFormat::IQ3_S)?;
let expected_gate = read(original)?;
context.project_routed_gate_up_columns(up, refs, original, RoutedFormat::IQ4_XS)?;
let expected_up = read(original)?;
let mut loan = workspace.begin_columns(refs);
loan.project_gate_up(gate, output, RoutedFormat::IQ3_S)?;
verify_stage(finite, output, (RoutedFiniteStage::Gate, 1), (&expected_gate, report))?;
loan.project_gate_up(up, output, RoutedFormat::IQ4_XS)?;
verify_stage(finite, output, (RoutedFiniteStage::Up, 1), (&expected_up, report))
}
@@ -90,366 +90,6 @@ fn refused(result: Result<()>, report: &mut Report) -> Result<()> {
Err(error) => Err(error),
}
}
/// The owners of one gate/up case besides the inputs, released together after the run.
#[derive(Default)]
struct GuOwners<'a> {
matrix: Option<DeviceBuffer<'a>>,
dual: Option<MmqWeights<'a>>,
reference: Option<DeviceBuffer<'a>>,
output: Option<DeviceBuffer<'a>>,
}
/// The matrix, the original product and the workspace product of a gate/up case.
struct GuIo<'b, 'a> {
matrix: &'b DeviceBuffer<'a>,
reference: &'b mut DeviceBuffer<'a>,
output: &'b mut DeviceBuffer<'a>,
}
impl<'a> GuOwners<'a> {
/// The original and workspace products of a case.
fn allocate_products(&mut self, context: &'a Context<'_>, total: usize) -> Result<()> {
self.reference = Some(context.allocate(total * 2560)?);
self.output = Some(context.allocate(total * 2560)?);
Ok(())
}
/// The owners as the buffers of a case, and the dual-format weights when there are any.
fn split(&mut self) -> Result<(GuIo<'_, 'a>, Option<&MmqWeights<'a>>)> {
let matrix = self.matrix.as_ref().ok_or(Error::ContextPoisoned)?;
let reference = self.reference.as_mut().ok_or(Error::ContextPoisoned)?;
let output = self.output.as_mut().ok_or(Error::ContextPoisoned)?;
Ok((GuIo { matrix, reference, output }, self.dual.as_ref()))
}
}
fn release_gu(inputs: Vec<DeviceBuffer<'_>>, owners: GuOwners<'_>) -> Result<()> {
let mut cleanup = Ok(());
for input in inputs {
cleanup = combine(cleanup, input.release());
}
for buffer in owners.reference.into_iter().chain(owners.output).chain(owners.matrix) {
cleanup = combine(cleanup, buffer.release());
}
if let Some(owner) = owners.dual {
cleanup = combine(cleanup, owner.close());
}
cleanup
}
fn gu_case(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
format: RoutedFormat,
total: usize,
changed: bool,
report: &mut Report,
) -> Result<()> {
let mut owners = GuOwners::default();
let mut inputs = Vec::new();
let operation = gu_run(context, workspace, (&mut owners, &mut inputs), (format, total, changed), report);
combine(operation, release_gu(inputs, owners))
}
fn gu_run<'a>(
context: &'a Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
(owners, inputs): (&mut GuOwners<'a>, &mut Vec<DeviceBuffer<'a>>),
(format, total, changed): (RoutedFormat, usize, bool),
report: &mut Report,
) -> Result<()> {
let raw = weights(format);
owners.matrix = Some(upload(context, &raw)?);
if let Some(kind) = dual_format(format) {
owners.dual = Some(context.mmq_weights(kind, &raw)?);
}
for column in 0..total {
inputs.push(upload(context, &values(2560, column, changed))?);
}
let prepared = inputs.iter().map(|input| context.prepare_routed_gate_up_q8(input)).collect::<Result<Vec<_>>>()?;
let products = gu_products(context, workspace, owners, &prepared, (format, total, changed), report);
let mut cleanup = Ok(());
for owner in prepared {
cleanup = combine(cleanup, owner.close());
}
combine(products, cleanup)
}
fn gu_products<'a>(
context: &'a Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
owners: &mut GuOwners<'a>,
prepared: &[RoutedGateUpQ8<'_>],
(format, total, changed): (RoutedFormat, usize, bool),
report: &mut Report,
) -> Result<()> {
let mut refs: Vec<_> = prepared.iter().collect();
if changed {
refs.reverse();
}
owners.allocate_products(context, total)?;
let (mut io, dual) = owners.split()?;
let allocated = gu_first(context, workspace, &mut io, &refs, (format, total), report)?;
if let Some(dual) = dual {
compare_dual(workspace, dual, &refs, &mut io, report)?;
}
gu_repeat(context, workspace, &mut io, &mut refs, (format, allocated), report)?;
gu_refusals(workspace, &mut io, &refs, format, report)
}
/// The unchanged original raw path completes and the full output is read FIRST; the workspace then agrees.
fn gu_first(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
io: &mut GuIo<'_, '_>,
refs: &[&RoutedGateUpQ8<'_>],
(format, total): (RoutedFormat, usize),
report: &mut Report,
) -> Result<usize> {
context.project_routed_gate_up_columns(io.matrix, refs, io.reference, format)?;
let original = read(io.reference)?;
let allocated = context.allocated_bytes();
workspace.table.as_mut().ok_or(Error::ContextPoisoned)?.copy_from(&[0xa5; TABLE_BYTES])?;
workspace.project_gate_up_columns(io.matrix, refs, io.output, format)?;
compare(&original, &read(io.output)?, report);
check_table_pointers(workspace, refs, total, report)?;
Ok(allocated)
}
/// Only the active pointer prefix of the table was written, and it points at the column scratches.
fn check_table_pointers(
workspace: &RoutedGroupWorkspace<'_>,
refs: &[&RoutedGateUpQ8<'_>],
total: usize,
report: &mut Report,
) -> Result<()> {
let table = read(workspace.table.as_ref().ok_or(Error::ContextPoisoned)?)?;
report.witness_errors += usize::from(table[total * 8..].iter().any(|b| *b != 0xa5));
for (pointer, owner) in table[..total * 8].as_chunks::<8>().0.iter().zip(refs) {
let scratch = owner.scratch.as_ref().ok_or(Error::ContextPoisoned)?;
report.witness_errors +=
usize::from(u64::from_le_bytes(*pointer) != scratch.handle.ok_or(Error::ContextPoisoned)?.qwen_address());
}
Ok(())
}
fn compare_dual(
workspace: &mut RoutedGroupWorkspace<'_>,
dual: &MmqWeights<'_>,
refs: &[&RoutedGateUpQ8<'_>],
io: &mut GuIo<'_, '_>,
report: &mut Report,
) -> Result<()> {
dual.native_gate_up_columns(refs, io.reference)?;
let original = read(io.reference)?;
dual.native_gate_up_columns_in(workspace, refs, io.output)?;
compare(&original, &read(io.output)?, report);
Ok(())
}
/// The active pointer prefix must be completely overwritten on repeat.
fn gu_repeat(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
io: &mut GuIo<'_, '_>,
refs: &mut [&RoutedGateUpQ8<'_>],
(format, allocated): (RoutedFormat, usize),
report: &mut Report,
) -> Result<()> {
refs.reverse();
context.project_routed_gate_up_columns(io.matrix, refs, io.reference, format)?;
let original = read(io.reference)?;
workspace.project_gate_up_columns(io.matrix, refs, io.output, format)?;
compare(&original, &read(io.output)?, report);
report.resets += 1;
report.witness_errors += usize::from(context.allocated_bytes() != allocated || workspace.is_failed());
Ok(())
}
/// No columns, too many columns and a down format are refused and leave the output untouched.
fn gu_refusals(
workspace: &mut RoutedGroupWorkspace<'_>,
io: &mut GuIo<'_, '_>,
refs: &[&RoutedGateUpQ8<'_>],
format: RoutedFormat,
report: &mut Report,
) -> Result<()> {
let saved = read(io.output)?;
refused(workspace.project_gate_up_columns(io.matrix, &[], io.output, format), report)?;
let excess = vec![refs[0]; 65];
refused(workspace.project_gate_up_columns(io.matrix, &excess, io.output, format), report)?;
refused(workspace.project_gate_up_columns(io.matrix, refs, io.output, RoutedFormat::Q2_0), report)?;
report.witness_errors += usize::from(read(io.output)? != saved || workspace.is_failed());
Ok(())
}
/// The owners of one down case, released together after the run.
#[derive(Default)]
struct DownOwners<'a> {
matrix: Option<DeviceBuffer<'a>>,
dual: Option<MmqWeights<'a>>,
hidden: Option<DeviceBuffer<'a>>,
reference: Option<DeviceBuffer<'a>>,
output: Option<DeviceBuffer<'a>>,
}
/// The buffers and geometry of a down case.
struct DownIo<'b, 'a> {
matrix: &'b DeviceBuffer<'a>,
hidden: &'b mut DeviceBuffer<'a>,
reference: &'b mut DeviceBuffer<'a>,
output: &'b mut DeviceBuffer<'a>,
total: usize,
format: RoutedFormat,
}
impl<'a> DownOwners<'a> {
/// The owners as the buffers of a case, and the dual-format weights.
fn split(&mut self, total: usize, format: RoutedFormat) -> Result<(DownIo<'_, 'a>, &MmqWeights<'a>)> {
let matrix = self.matrix.as_ref().ok_or(Error::ContextPoisoned)?;
let hidden = self.hidden.as_mut().ok_or(Error::ContextPoisoned)?;
let reference = self.reference.as_mut().ok_or(Error::ContextPoisoned)?;
let output = self.output.as_mut().ok_or(Error::ContextPoisoned)?;
let dual = self.dual.as_ref().ok_or(Error::ContextPoisoned)?;
Ok((DownIo { matrix, hidden, reference, output, total, format }, dual))
}
}
fn release_down(owners: DownOwners<'_>) -> Result<()> {
let mut cleanup = Ok(());
for buffer in owners.reference.into_iter().chain(owners.output).chain(owners.hidden).chain(owners.matrix) {
cleanup = combine(cleanup, buffer.release());
}
if let Some(owner) = owners.dual {
cleanup = combine(cleanup, owner.close());
}
cleanup
}
fn down_case(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
format: RoutedFormat,
total: usize,
changed: bool,
report: &mut Report,
) -> Result<()> {
let mut owners = DownOwners::default();
let operation = down_run(context, workspace, &mut owners, (format, total, changed), report);
combine(operation, release_down(owners))
}
/// Upload the matrix and the hidden rows and allocate the products; the hidden rows as uploaded.
fn down_alloc<'a>(
context: &'a Context<'_>,
owners: &mut DownOwners<'a>,
(format, total, changed): (RoutedFormat, usize, bool),
) -> Result<Vec<u8>> {
let raw = weights(format);
owners.matrix = Some(upload(context, &raw)?);
owners.dual = Some(context.mmq_weights(dual_format(format).ok_or(Error::UnsupportedProjection)?, &raw)?);
let mut valid = Vec::new();
for column in 0..total {
valid.extend_from_slice(&values(640, column, changed));
}
owners.hidden = Some(upload(context, &valid)?);
owners.reference = Some(context.allocate(total * 10240)?);
owners.output = Some(context.allocate(total * 10240)?);
Ok(valid)
}
fn down_run<'a>(
context: &'a Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
owners: &mut DownOwners<'a>,
(format, total, changed): (RoutedFormat, usize, bool),
report: &mut Report,
) -> Result<()> {
let valid = down_alloc(context, owners, (format, total, changed))?;
let (mut io, dual) = owners.split(total, format)?;
let allocated = down_first(context, workspace, &mut io, report)?;
compare_dual_down(workspace, dual, &mut io, report)?;
// Genuine active T5/max geometry for numerical faults, not extent refusal.
for bits in [0x7fc0_0001u32, 0x7f80_0000, 0xff80_0000] {
down_fault(context, workspace, &mut io, (bits, &valid), report)?;
down_recover(context, workspace, &mut io, &valid, report)?;
}
down_refusals(context, workspace, &mut io, allocated, report)
}
/// The unchanged original path completes and the full output is read FIRST; the workspace then agrees, and only
/// the active scratch prefix was written and the status was reset.
fn down_first(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
io: &mut DownIo<'_, '_>,
report: &mut Report,
) -> Result<usize> {
context.project_routed_down_columns(io.matrix, io.hidden, io.reference, io.total, io.format)?;
let original = read(io.reference)?;
let allocated = context.allocated_bytes();
workspace.q8.as_mut().ok_or(Error::ContextPoisoned)?.copy_from(&[0xa5; Q8_BYTES])?;
workspace.status.as_mut().ok_or(Error::ContextPoisoned)?.copy_from(&u32::MAX.to_le_bytes())?;
workspace.project_down_columns(io.matrix, io.hidden, io.output, io.total, io.format)?;
compare(&original, &read(io.output)?, report);
scratch_witnesses(workspace, io.total, report)?;
Ok(allocated)
}
/// Only the active scratch prefix was written and the status was reset.
fn scratch_witnesses(workspace: &RoutedGroupWorkspace<'_>, total: usize, report: &mut Report) -> Result<()> {
let q8 = read(workspace.q8.as_ref().ok_or(Error::ContextPoisoned)?)?;
report.witness_errors += usize::from(q8[total * 720..].iter().any(|b| *b != 0xa5));
report.witness_errors += usize::from(read(workspace.status.as_ref().ok_or(Error::ContextPoisoned)?)? != [0; 4]);
Ok(())
}
fn compare_dual_down(
workspace: &mut RoutedGroupWorkspace<'_>,
dual: &MmqWeights<'_>,
io: &mut DownIo<'_, '_>,
report: &mut Report,
) -> Result<()> {
dual.native_down_columns(io.hidden, io.total as u32, io.reference)?;
let original = read(io.reference)?;
dual.native_down_columns_in(workspace, io.hidden, io.total as u32, io.output)?;
compare(&original, &read(io.output)?, report);
Ok(())
}
/// A nonfinite activation word is refused by the original path and by the workspace alike, and leaves the output
/// untouched.
fn down_fault(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
io: &mut DownIo<'_, '_>,
(bits, valid): (u32, &[u8]),
report: &mut Report,
) -> Result<()> {
io.hidden.copy_from_range(valid.len() - 4, &bits.to_le_bytes())?;
let before = read(io.output)?;
let old = context.project_routed_down_columns(io.matrix, io.hidden, io.reference, io.total, io.format);
let new = workspace.project_down_columns(io.matrix, io.hidden, io.output, io.total, io.format);
report.guards += 1;
report.refusals +=
usize::from(matches!(&old, Err(Error::InvalidQ8Activation)) && matches!(&new, Err(Error::InvalidQ8Activation)));
if !matches!(&old, Err(Error::InvalidQ8Activation)) {
old?;
}
if !matches!(&new, Err(Error::InvalidQ8Activation)) {
new?;
}
report.witness_errors += usize::from(read(io.output)? != before || workspace.is_failed());
Ok(())
}
/// A valid rewrite of the hidden rows recovers: both paths agree again.
fn down_recover(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
io: &mut DownIo<'_, '_>,
valid: &[u8],
report: &mut Report,
) -> Result<()> {
io.hidden.copy_from(valid)?;
context.project_routed_down_columns(io.matrix, io.hidden, io.reference, io.total, io.format)?;
let original = read(io.reference)?;
workspace.project_down_columns(io.matrix, io.hidden, io.output, io.total, io.format)?;
compare(&original, &read(io.output)?, report);
report.resets += 1;
Ok(())
}
/// Wrong row counts and a gate/up format are refused; nothing was allocated and the workspace is healthy.
fn down_refusals(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
io: &mut DownIo<'_, '_>,
allocated: usize,
report: &mut Report,
) -> Result<()> {
for count in [0, 65, usize::MAX] {
refused(workspace.project_down_columns(io.matrix, io.hidden, io.output, count, io.format), report)?;
}
refused(workspace.project_down_columns(io.matrix, io.hidden, io.output, io.total, RoutedFormat::IQ3_S), report)?;
report.witness_errors += usize::from(context.allocated_bytes() != allocated || workspace.is_failed());
Ok(())
}
fn foreign_and_shape(
context: &Context<'_>,
foreign: &Context<'_>,
@@ -541,3 +181,10 @@ fn original_first_group_workspace_all_products_active_spans_reset_and_cleanup()
assert_eq!(report.guards, report.refusals);
eprintln!("ROUTED-GROUP-WORKSPACE {report:?}; bytes={ALLOCATED_BYTES}; cleanup=0; component_only=true");
}
#[path = "routed_group_workspace_gpu/gu_case.rs"]
mod gu_case;
use gu_case::gu_case;
#[path = "routed_group_workspace_gpu/down_case.rs"]
mod down_case;
use down_case::down_case;
@@ -0,0 +1,185 @@
//! Down case of the grouped workspace component proof: the original path first, then the workspace.
#[allow(unused_imports)]
use super::*;
/// The owners of one down case, released together after the run.
#[derive(Default)]
struct DownOwners<'a> {
matrix: Option<DeviceBuffer<'a>>,
dual: Option<MmqWeights<'a>>,
hidden: Option<DeviceBuffer<'a>>,
reference: Option<DeviceBuffer<'a>>,
output: Option<DeviceBuffer<'a>>,
}
/// The buffers and geometry of a down case.
struct DownIo<'b, 'a> {
matrix: &'b DeviceBuffer<'a>,
hidden: &'b mut DeviceBuffer<'a>,
reference: &'b mut DeviceBuffer<'a>,
output: &'b mut DeviceBuffer<'a>,
total: usize,
format: RoutedFormat,
}
impl<'a> DownOwners<'a> {
/// The owners as the buffers of a case, and the dual-format weights.
fn split(&mut self, total: usize, format: RoutedFormat) -> Result<(DownIo<'_, 'a>, &MmqWeights<'a>)> {
let matrix = self.matrix.as_ref().ok_or(Error::ContextPoisoned)?;
let hidden = self.hidden.as_mut().ok_or(Error::ContextPoisoned)?;
let reference = self.reference.as_mut().ok_or(Error::ContextPoisoned)?;
let output = self.output.as_mut().ok_or(Error::ContextPoisoned)?;
let dual = self.dual.as_ref().ok_or(Error::ContextPoisoned)?;
Ok((DownIo { matrix, hidden, reference, output, total, format }, dual))
}
}
fn release_down(owners: DownOwners<'_>) -> Result<()> {
let mut cleanup = Ok(());
for buffer in owners.reference.into_iter().chain(owners.output).chain(owners.hidden).chain(owners.matrix) {
cleanup = combine(cleanup, buffer.release());
}
if let Some(owner) = owners.dual {
cleanup = combine(cleanup, owner.close());
}
cleanup
}
pub(super) fn down_case(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
format: RoutedFormat,
total: usize,
changed: bool,
report: &mut Report,
) -> Result<()> {
let mut owners = DownOwners::default();
let operation = down_run(context, workspace, &mut owners, (format, total, changed), report);
combine(operation, release_down(owners))
}
/// Upload the matrix and the hidden rows and allocate the products; the hidden rows as uploaded.
fn down_alloc<'a>(
context: &'a Context<'_>,
owners: &mut DownOwners<'a>,
(format, total, changed): (RoutedFormat, usize, bool),
) -> Result<Vec<u8>> {
let raw = weights(format);
owners.matrix = Some(upload(context, &raw)?);
owners.dual = Some(context.mmq_weights(dual_format(format).ok_or(Error::UnsupportedProjection)?, &raw)?);
let mut valid = Vec::new();
for column in 0..total {
valid.extend_from_slice(&values(640, column, changed));
}
owners.hidden = Some(upload(context, &valid)?);
owners.reference = Some(context.allocate(total * 10240)?);
owners.output = Some(context.allocate(total * 10240)?);
Ok(valid)
}
fn down_run<'a>(
context: &'a Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
owners: &mut DownOwners<'a>,
(format, total, changed): (RoutedFormat, usize, bool),
report: &mut Report,
) -> Result<()> {
let valid = down_alloc(context, owners, (format, total, changed))?;
let (mut io, dual) = owners.split(total, format)?;
let allocated = down_first(context, workspace, &mut io, report)?;
compare_dual_down(workspace, dual, &mut io, report)?;
// Genuine active T5/max geometry for numerical faults, not extent refusal.
for bits in [0x7fc0_0001u32, 0x7f80_0000, 0xff80_0000] {
down_fault(context, workspace, &mut io, (bits, &valid), report)?;
down_recover(context, workspace, &mut io, &valid, report)?;
}
down_refusals(context, workspace, &mut io, allocated, report)
}
/// The unchanged original path completes and the full output is read FIRST; the workspace then agrees, and only
/// the active scratch prefix was written and the status was reset.
fn down_first(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
io: &mut DownIo<'_, '_>,
report: &mut Report,
) -> Result<usize> {
context.project_routed_down_columns(io.matrix, io.hidden, io.reference, io.total, io.format)?;
let original = read(io.reference)?;
let allocated = context.allocated_bytes();
workspace.q8.as_mut().ok_or(Error::ContextPoisoned)?.copy_from(&[0xa5; Q8_BYTES])?;
workspace.status.as_mut().ok_or(Error::ContextPoisoned)?.copy_from(&u32::MAX.to_le_bytes())?;
workspace.project_down_columns(io.matrix, io.hidden, io.output, io.total, io.format)?;
compare(&original, &read(io.output)?, report);
scratch_witnesses(workspace, io.total, report)?;
Ok(allocated)
}
/// Only the active scratch prefix was written and the status was reset.
fn scratch_witnesses(workspace: &RoutedGroupWorkspace<'_>, total: usize, report: &mut Report) -> Result<()> {
let q8 = read(workspace.q8.as_ref().ok_or(Error::ContextPoisoned)?)?;
report.witness_errors += usize::from(q8[total * 720..].iter().any(|b| *b != 0xa5));
report.witness_errors += usize::from(read(workspace.status.as_ref().ok_or(Error::ContextPoisoned)?)? != [0; 4]);
Ok(())
}
fn compare_dual_down(
workspace: &mut RoutedGroupWorkspace<'_>,
dual: &MmqWeights<'_>,
io: &mut DownIo<'_, '_>,
report: &mut Report,
) -> Result<()> {
dual.native_down_columns(io.hidden, io.total as u32, io.reference)?;
let original = read(io.reference)?;
dual.native_down_columns_in(workspace, io.hidden, io.total as u32, io.output)?;
compare(&original, &read(io.output)?, report);
Ok(())
}
/// A nonfinite activation word is refused by the original path and by the workspace alike, and leaves the output
/// untouched.
fn down_fault(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
io: &mut DownIo<'_, '_>,
(bits, valid): (u32, &[u8]),
report: &mut Report,
) -> Result<()> {
io.hidden.copy_from_range(valid.len() - 4, &bits.to_le_bytes())?;
let before = read(io.output)?;
let old = context.project_routed_down_columns(io.matrix, io.hidden, io.reference, io.total, io.format);
let new = workspace.project_down_columns(io.matrix, io.hidden, io.output, io.total, io.format);
report.guards += 1;
report.refusals +=
usize::from(matches!(&old, Err(Error::InvalidQ8Activation)) && matches!(&new, Err(Error::InvalidQ8Activation)));
if !matches!(&old, Err(Error::InvalidQ8Activation)) {
old?;
}
if !matches!(&new, Err(Error::InvalidQ8Activation)) {
new?;
}
report.witness_errors += usize::from(read(io.output)? != before || workspace.is_failed());
Ok(())
}
/// A valid rewrite of the hidden rows recovers: both paths agree again.
fn down_recover(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
io: &mut DownIo<'_, '_>,
valid: &[u8],
report: &mut Report,
) -> Result<()> {
io.hidden.copy_from(valid)?;
context.project_routed_down_columns(io.matrix, io.hidden, io.reference, io.total, io.format)?;
let original = read(io.reference)?;
workspace.project_down_columns(io.matrix, io.hidden, io.output, io.total, io.format)?;
compare(&original, &read(io.output)?, report);
report.resets += 1;
Ok(())
}
/// Wrong row counts and a gate/up format are refused; nothing was allocated and the workspace is healthy.
fn down_refusals(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
io: &mut DownIo<'_, '_>,
allocated: usize,
report: &mut Report,
) -> Result<()> {
for count in [0, 65, usize::MAX] {
refused(workspace.project_down_columns(io.matrix, io.hidden, io.output, count, io.format), report)?;
}
refused(workspace.project_down_columns(io.matrix, io.hidden, io.output, io.total, RoutedFormat::IQ3_S), report)?;
report.witness_errors += usize::from(context.allocated_bytes() != allocated || workspace.is_failed());
Ok(())
}
@@ -0,0 +1,185 @@
//! Gate/up case of the grouped workspace component proof: the original path first, then the workspace.
#[allow(unused_imports)]
use super::*;
/// The owners of one gate/up case besides the inputs, released together after the run.
#[derive(Default)]
struct GuOwners<'a> {
matrix: Option<DeviceBuffer<'a>>,
dual: Option<MmqWeights<'a>>,
reference: Option<DeviceBuffer<'a>>,
output: Option<DeviceBuffer<'a>>,
}
/// The matrix, the original product and the workspace product of a gate/up case.
struct GuIo<'b, 'a> {
matrix: &'b DeviceBuffer<'a>,
reference: &'b mut DeviceBuffer<'a>,
output: &'b mut DeviceBuffer<'a>,
}
impl<'a> GuOwners<'a> {
/// The original and workspace products of a case.
fn allocate_products(&mut self, context: &'a Context<'_>, total: usize) -> Result<()> {
self.reference = Some(context.allocate(total * 2560)?);
self.output = Some(context.allocate(total * 2560)?);
Ok(())
}
/// The owners as the buffers of a case, and the dual-format weights when there are any.
fn split(&mut self) -> Result<(GuIo<'_, 'a>, Option<&MmqWeights<'a>>)> {
let matrix = self.matrix.as_ref().ok_or(Error::ContextPoisoned)?;
let reference = self.reference.as_mut().ok_or(Error::ContextPoisoned)?;
let output = self.output.as_mut().ok_or(Error::ContextPoisoned)?;
Ok((GuIo { matrix, reference, output }, self.dual.as_ref()))
}
}
fn release_gu(inputs: Vec<DeviceBuffer<'_>>, owners: GuOwners<'_>) -> Result<()> {
let mut cleanup = Ok(());
for input in inputs {
cleanup = combine(cleanup, input.release());
}
for buffer in owners.reference.into_iter().chain(owners.output).chain(owners.matrix) {
cleanup = combine(cleanup, buffer.release());
}
if let Some(owner) = owners.dual {
cleanup = combine(cleanup, owner.close());
}
cleanup
}
pub(super) fn gu_case(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
format: RoutedFormat,
total: usize,
changed: bool,
report: &mut Report,
) -> Result<()> {
let mut owners = GuOwners::default();
let mut inputs = Vec::new();
let operation = gu_run(context, workspace, (&mut owners, &mut inputs), (format, total, changed), report);
combine(operation, release_gu(inputs, owners))
}
fn gu_run<'a>(
context: &'a Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
(owners, inputs): (&mut GuOwners<'a>, &mut Vec<DeviceBuffer<'a>>),
(format, total, changed): (RoutedFormat, usize, bool),
report: &mut Report,
) -> Result<()> {
let raw = weights(format);
owners.matrix = Some(upload(context, &raw)?);
if let Some(kind) = dual_format(format) {
owners.dual = Some(context.mmq_weights(kind, &raw)?);
}
for column in 0..total {
inputs.push(upload(context, &values(2560, column, changed))?);
}
let prepared = inputs.iter().map(|input| context.prepare_routed_gate_up_q8(input)).collect::<Result<Vec<_>>>()?;
let products = gu_products(context, workspace, owners, &prepared, (format, total, changed), report);
let mut cleanup = Ok(());
for owner in prepared {
cleanup = combine(cleanup, owner.close());
}
combine(products, cleanup)
}
fn gu_products<'a>(
context: &'a Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
owners: &mut GuOwners<'a>,
prepared: &[RoutedGateUpQ8<'_>],
(format, total, changed): (RoutedFormat, usize, bool),
report: &mut Report,
) -> Result<()> {
let mut refs: Vec<_> = prepared.iter().collect();
if changed {
refs.reverse();
}
owners.allocate_products(context, total)?;
let (mut io, dual) = owners.split()?;
let allocated = gu_first(context, workspace, &mut io, &refs, (format, total), report)?;
if let Some(dual) = dual {
compare_dual(workspace, dual, &refs, &mut io, report)?;
}
gu_repeat(context, workspace, &mut io, &mut refs, (format, allocated), report)?;
gu_refusals(workspace, &mut io, &refs, format, report)
}
/// The unchanged original raw path completes and the full output is read FIRST; the workspace then agrees.
fn gu_first(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
io: &mut GuIo<'_, '_>,
refs: &[&RoutedGateUpQ8<'_>],
(format, total): (RoutedFormat, usize),
report: &mut Report,
) -> Result<usize> {
context.project_routed_gate_up_columns(io.matrix, refs, io.reference, format)?;
let original = read(io.reference)?;
let allocated = context.allocated_bytes();
workspace.table.as_mut().ok_or(Error::ContextPoisoned)?.copy_from(&[0xa5; TABLE_BYTES])?;
workspace.project_gate_up_columns(io.matrix, refs, io.output, format)?;
compare(&original, &read(io.output)?, report);
check_table_pointers(workspace, refs, total, report)?;
Ok(allocated)
}
/// Only the active pointer prefix of the table was written, and it points at the column scratches.
fn check_table_pointers(
workspace: &RoutedGroupWorkspace<'_>,
refs: &[&RoutedGateUpQ8<'_>],
total: usize,
report: &mut Report,
) -> Result<()> {
let table = read(workspace.table.as_ref().ok_or(Error::ContextPoisoned)?)?;
report.witness_errors += usize::from(table[total * 8..].iter().any(|b| *b != 0xa5));
for (pointer, owner) in table[..total * 8].as_chunks::<8>().0.iter().zip(refs) {
let scratch = owner.scratch.as_ref().ok_or(Error::ContextPoisoned)?;
report.witness_errors +=
usize::from(u64::from_le_bytes(*pointer) != scratch.handle.ok_or(Error::ContextPoisoned)?.qwen_address());
}
Ok(())
}
fn compare_dual(
workspace: &mut RoutedGroupWorkspace<'_>,
dual: &MmqWeights<'_>,
refs: &[&RoutedGateUpQ8<'_>],
io: &mut GuIo<'_, '_>,
report: &mut Report,
) -> Result<()> {
dual.native_gate_up_columns(refs, io.reference)?;
let original = read(io.reference)?;
dual.native_gate_up_columns_in(workspace, refs, io.output)?;
compare(&original, &read(io.output)?, report);
Ok(())
}
/// The active pointer prefix must be completely overwritten on repeat.
fn gu_repeat(
context: &Context<'_>,
workspace: &mut RoutedGroupWorkspace<'_>,
io: &mut GuIo<'_, '_>,
refs: &mut [&RoutedGateUpQ8<'_>],
(format, allocated): (RoutedFormat, usize),
report: &mut Report,
) -> Result<()> {
refs.reverse();
context.project_routed_gate_up_columns(io.matrix, refs, io.reference, format)?;
let original = read(io.reference)?;
workspace.project_gate_up_columns(io.matrix, refs, io.output, format)?;
compare(&original, &read(io.output)?, report);
report.resets += 1;
report.witness_errors += usize::from(context.allocated_bytes() != allocated || workspace.is_failed());
Ok(())
}
/// No columns, too many columns and a down format are refused and leave the output untouched.
fn gu_refusals(
workspace: &mut RoutedGroupWorkspace<'_>,
io: &mut GuIo<'_, '_>,
refs: &[&RoutedGateUpQ8<'_>],
format: RoutedFormat,
report: &mut Report,
) -> Result<()> {
let saved = read(io.output)?;
refused(workspace.project_gate_up_columns(io.matrix, &[], io.output, format), report)?;
let excess = vec![refs[0]; 65];
refused(workspace.project_gate_up_columns(io.matrix, &excess, io.output, format), report)?;
refused(workspace.project_gate_up_columns(io.matrix, refs, io.output, RoutedFormat::Q2_0), report)?;
report.witness_errors += usize::from(read(io.output)? != saved || workspace.is_failed());
Ok(())
}