STRA-338 size2 cd3: mmq resident GPU proof tests split into phase helpers
This commit is contained in:
@@ -31,11 +31,7 @@ const ROOT_REFERENCE_SHA: &str = "752d9c2c2c77df57455384dedbfb191b524c6c1c5155d7
|
||||
#[test]
|
||||
#[ignore = "ROOT-only new resident memory transport; actual CUDA"]
|
||||
fn original_references_resident_chain_gather_dual_native_and_guards_exact() {
|
||||
assert_eq!(std::env::var("LELEKA_STRA169_RUN_MMQ_NUMERIC").as_deref(), Ok("yes"));
|
||||
let expected = std::env::var("LELEKA_STRA169_MMQ_REFERENCE_MANIFEST_SHA").unwrap();
|
||||
assert_eq!(expected, ROOT_REFERENCE_SHA);
|
||||
let receipt =
|
||||
Receipt::load(PathBuf::from(std::env::var_os("LELEKA_STRA169_MMQ_REFERENCE_DIR").unwrap()), &expected);
|
||||
let receipt = reference_receipt();
|
||||
let driver = Driver::load().unwrap();
|
||||
let c = driver.device(0).unwrap().create_context(BUDGET).unwrap();
|
||||
let mut w = c.mmq_workspace().unwrap();
|
||||
@@ -43,89 +39,20 @@ fn original_references_resident_chain_gather_dual_native_and_guards_exact() {
|
||||
assert!((1..=256).contains(&sms));
|
||||
assert_eq!(w.allocated_bytes(), 2272516 + sms as usize * 32768);
|
||||
let baseline = c.allocated_bytes();
|
||||
let formats = [
|
||||
(18, MmqFormat::Iq3XxsGateUp, 4),
|
||||
(22, MmqFormat::Iq2SGateUp, 64),
|
||||
(20, MmqFormat::Iq4NlDown, 4),
|
||||
(42, MmqFormat::Q2Down, 64),
|
||||
];
|
||||
let mut owners = Vec::new();
|
||||
let mut words = 0;
|
||||
let mut packed = 0;
|
||||
for (kind, format, t) in formats {
|
||||
let raw = file(&receipt, kind, 4, "weights.bin");
|
||||
let data = file(&receipt, kind, t, "input.f32");
|
||||
let owner = c.mmq_weights(format, &raw).unwrap();
|
||||
assert_eq!(
|
||||
owner.allocated_bytes(),
|
||||
raw.len()
|
||||
+ match kind {
|
||||
20 => 216,
|
||||
42 => 108,
|
||||
_ => 0,
|
||||
}
|
||||
);
|
||||
let before = download(owner.native_buffer().unwrap(), owner.allocated_bytes());
|
||||
same(&before[..raw.len()], &raw, "dual raw identity");
|
||||
assert!(before[raw.len()..].iter().all(|v| *v == 0));
|
||||
native_owner(&c, &owner, &raw, &data);
|
||||
let (n, q) = direct(&receipt, &mut w, kind, t, &owner);
|
||||
words += n;
|
||||
packed += q;
|
||||
same(
|
||||
&download(owner.native_buffer().unwrap(), owner.allocated_bytes()),
|
||||
&before,
|
||||
"dual entire allocation immutable",
|
||||
);
|
||||
owners.push(owner);
|
||||
}
|
||||
let (owners, words, packed) = prove_owner_formats(&receipt, &c, &mut w, PROOF_FORMATS);
|
||||
assert_eq!((words, packed), (217600, 311040));
|
||||
let gathered = gather(&receipt, &mut w);
|
||||
assert_eq!(gathered, 269568);
|
||||
let chain_words = chain(&receipt, &mut w, 18, 4, &owners[0], &owners[2])
|
||||
+ chain(&receipt, &mut w, 22, 64, &owners[1], &owners[3]);
|
||||
assert_eq!(chain_words, 304640);
|
||||
// Abandon a completed group without publication, then next genuine invocation.
|
||||
{
|
||||
let input = floats(&file(&receipt, 18, 4, "input.f32"));
|
||||
let mut chunk = w.prepare_gate_up_chunk(&input, 4).unwrap();
|
||||
let _private = chunk.gather_gate_up(&[0, 3]).unwrap();
|
||||
}
|
||||
assert_eq!(direct(&receipt, &mut w, 18, 4, &owners[0]), (2560, 20736));
|
||||
assert_eq!(witness(&mut w, &file(&receipt, 18, 4, "reference_q8.bin")), 21);
|
||||
let charge = c.allocated_bytes();
|
||||
for t in [0, 65, u32::MAX] {
|
||||
assert!(matches!(w.prepare_gate_up_chunk(&[0.0; 2560], t), Err(Error::InvalidProjectionShape)));
|
||||
}
|
||||
let mut invalid = floats(&file(&receipt, 18, 4, "input.f32"));
|
||||
*invalid.last_mut().unwrap() = f32::NAN;
|
||||
assert!(matches!(w.prepare_gate_up_chunk(&invalid, 4), Err(Error::InvalidQ8Activation)));
|
||||
assert_eq!(c.allocated_bytes(), charge);
|
||||
assert!(!c.is_poisoned());
|
||||
let foreign = driver.device(0).unwrap().create_context(BUDGET).unwrap();
|
||||
let foreign_weight = foreign.mmq_weights(MmqFormat::Iq3XxsGateUp, &file(&receipt, 18, 4, "weights.bin")).unwrap();
|
||||
assert!(matches!(w.project(&foreign_weight, CHUNK, GATE, 4), Err(Error::ContextMismatch)));
|
||||
foreign_weight.close().unwrap();
|
||||
assert_eq!(foreign.allocated_bytes(), 0);
|
||||
foreign.close().unwrap();
|
||||
for ((kind, _, _), owner) in formats.into_iter().zip(owners) {
|
||||
let raw = file(&receipt, kind, 4, "weights.bin");
|
||||
let all = download(owner.native_buffer().unwrap(), owner.allocated_bytes());
|
||||
same(&all[..raw.len()], &raw, "all operations weight immutability");
|
||||
assert!(all[raw.len()..].iter().all(|v| *v == 0));
|
||||
owner.close().unwrap();
|
||||
}
|
||||
prove_gather_chain_abandon(&receipt, &mut w, &owners);
|
||||
prove_guards(&receipt, &driver, &c, &mut w);
|
||||
close_owners(&receipt, PROOF_FORMATS, owners);
|
||||
assert_eq!(c.allocated_bytes(), baseline);
|
||||
w.close().unwrap();
|
||||
assert_eq!(c.allocated_bytes(), 0);
|
||||
assert!(!c.is_poisoned());
|
||||
c.close().unwrap();
|
||||
let small = driver.device(0).unwrap().create_context(2272515 + sms as usize * 32768).unwrap();
|
||||
assert!(matches!(small.mmq_workspace(), Err(Error::BudgetExceeded)));
|
||||
assert_eq!(small.allocated_bytes(), 0);
|
||||
small.close().unwrap();
|
||||
prove_small_budget(&driver, sms);
|
||||
println!(
|
||||
"Resident new transport exact:220160direct+304640chainF32+51200packetwords+14080nativewords,331776direct+269568gatherQ8bytes,21finitechecks,allocated0"
|
||||
"Resident new transport exact:220160direct+304640chainF32+51200packetwords+14080nativewords,\
|
||||
331776direct+269568gatherQ8bytes,21finitechecks,allocated0"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -139,201 +66,43 @@ struct LivePacketAlias<'borrow, 'ctx> {
|
||||
#[test]
|
||||
#[ignore = "ROOT-only changed queued finite boundary and bulk Down copy; actual CUDA"]
|
||||
fn queued_finite_chain_and_bulk_down_packets_exact() {
|
||||
assert_eq!(std::env::var("LELEKA_STRA169_RUN_MMQ_NUMERIC").as_deref(), Ok("yes"));
|
||||
let expected = std::env::var("LELEKA_STRA169_MMQ_REFERENCE_MANIFEST_SHA").unwrap();
|
||||
assert_eq!(expected, ROOT_REFERENCE_SHA);
|
||||
let receipt =
|
||||
Receipt::load(PathBuf::from(std::env::var_os("LELEKA_STRA169_MMQ_REFERENCE_DIR").unwrap()), &expected);
|
||||
const T: u32 = 4;
|
||||
const GU_BYTES: usize = 4 * 2560;
|
||||
const DOWN_BYTES: usize = 4 * 10240;
|
||||
const PACKET_BYTES: usize = 102400;
|
||||
let data = file(&receipt, 18, T, "input.f32");
|
||||
let gu_raw = file(&receipt, 18, T, "weights.bin");
|
||||
let down_raw = file(&receipt, 20, T, "weights.bin");
|
||||
let receipt = reference_receipt();
|
||||
let data = file(&receipt, 18, 4, "input.f32");
|
||||
let gu_raw = file(&receipt, 18, 4, "weights.bin");
|
||||
let down_raw = file(&receipt, 20, 4, "weights.bin");
|
||||
finite(&data);
|
||||
let driver = Driver::load().unwrap();
|
||||
let c = driver.device(0).unwrap().create_context(BUDGET).unwrap();
|
||||
let foreign = driver.device(0).unwrap().create_context(PACKET_BYTES).unwrap();
|
||||
let foreign = driver.device(0).unwrap().create_context(102400).unwrap();
|
||||
let gu = c.mmq_weights(MmqFormat::Iq3XxsGateUp, &gu_raw).unwrap();
|
||||
let down = c.mmq_weights(MmqFormat::Iq4NlDown, &down_raw).unwrap();
|
||||
let weight_before = download(gu.native_buffer().unwrap(), gu.allocated_bytes());
|
||||
let down_weight_before = download(down.native_buffer().unwrap(), down.allocated_bytes());
|
||||
let (weight_before, down_weight_before) = (owner_bytes(&gu), owner_bytes(&down));
|
||||
let mut w = c.mmq_workspace().unwrap();
|
||||
let sms = w.experiment.as_ref().unwrap().sms;
|
||||
assert!((1..=256).contains(&sms));
|
||||
let metadata = String::from_utf8(file(&receipt, 18, T, "metadata.json")).unwrap();
|
||||
assert_eq!(integer(&metadata, "nsm"), sms as usize);
|
||||
check_sms(&w, &receipt);
|
||||
let persistent_charge = c.allocated_bytes();
|
||||
|
||||
// Actual qualified nonresident baseline FIRST, with real original refs.
|
||||
let old = c.mmq_experiment().unwrap();
|
||||
let input = upload(&c, &data);
|
||||
let mut gate = c.allocate(GU_BYTES).unwrap();
|
||||
let mut up = c.allocate(GU_BYTES).unwrap();
|
||||
let (gate_values, q8) = old.project_observed(MmqProfile::OriginalAdaptive, &gu, &input, T, &mut gate).unwrap();
|
||||
let (up_values, _) = old.project_observed(MmqProfile::OriginalAdaptive, &gu, &input, T, &mut up).unwrap();
|
||||
compare(&gate_values, &file(&receipt, 18, T, "reference_streamk.f32"), "original GU");
|
||||
same(&q8, &file(&receipt, 18, T, "reference_q8.bin"), "original D4");
|
||||
let mut hidden = c.allocate(GU_BYTES).unwrap();
|
||||
c.moe_swiglu_columns(&gate, &up, &mut hidden, T as usize).unwrap();
|
||||
let hidden_bytes = download(&hidden, GU_BYTES);
|
||||
finite(&hidden_bytes);
|
||||
let mut old_output = c.allocate(DOWN_BYTES).unwrap();
|
||||
let (down_values, down_q8) =
|
||||
old.project_observed(MmqProfile::OriginalAdaptive, &down, &hidden, T, &mut old_output).unwrap();
|
||||
let down_bytes = download(&old_output, DOWN_BYTES);
|
||||
compare(&down_values, &down_bytes, "qualified actual Down");
|
||||
same(&download(&input, data.len()), &data, "baseline source immutable");
|
||||
old_output.release().unwrap();
|
||||
hidden.release().unwrap();
|
||||
up.release().unwrap();
|
||||
gate.release().unwrap();
|
||||
input.release().unwrap();
|
||||
old.close().unwrap();
|
||||
let base = queued_baseline(&receipt, &c, (&gu, &down), &data);
|
||||
assert_eq!(c.allocated_bytes(), persistent_charge);
|
||||
|
||||
let mut a = upload(&c, &vec![0xa5; PACKET_BYTES]);
|
||||
let mut b = upload(&c, &vec![0x5a; PACKET_BYTES]);
|
||||
let mut d = upload(&c, &vec![0x3c; PACKET_BYTES]);
|
||||
let mut short = upload(&c, &[0x7e; 4]);
|
||||
let charge = c.allocated_bytes();
|
||||
let before = [download(&a, PACKET_BYTES), download(&b, PACKET_BYTES), download(&d, PACKET_BYTES)];
|
||||
let mut expected_packets = before.clone();
|
||||
{
|
||||
let mut prepared = w.prepare_gate_up_chunk(&floats(&data), T).unwrap();
|
||||
same(&download(prepared.workspace.buffer(INPUT).unwrap(), data.len()), &data, "queued source immutable");
|
||||
same(&download(prepared.workspace.buffer(CHUNK).unwrap(), q8.len()), &q8, "queued chunk Q8");
|
||||
let group = prepared.gather_gate_up(&[0, 1, 2, 3]).unwrap();
|
||||
same(&download(group.workspace.buffer(GROUP).unwrap(), q8.len()), &q8, "queued identity group Q8+tail");
|
||||
let actual_hidden = group.gate_up(&gu, &gu).unwrap();
|
||||
same(
|
||||
&download(actual_hidden.workspace.buffer(GATE).unwrap(), GU_BYTES),
|
||||
&encoded(&gate_values),
|
||||
"queued finite Gate",
|
||||
);
|
||||
same(
|
||||
&download(actual_hidden.workspace.buffer(UP).unwrap(), GU_BYTES),
|
||||
&encoded(&up_values),
|
||||
"queued finite Up",
|
||||
);
|
||||
same(
|
||||
&download(actual_hidden.workspace.buffer(HIDDEN).unwrap(), GU_BYTES),
|
||||
&hidden_bytes,
|
||||
"queued finite hidden",
|
||||
);
|
||||
let actual = actual_hidden.down(&down).unwrap();
|
||||
same(&download(actual.workspace.buffer(DOWN_Q8).unwrap(), down_q8.len()), &down_q8, "queued Down Q8");
|
||||
same(&download(actual.workspace.buffer(DOWN).unwrap(), DOWN_BYTES), &down_bytes, "queued finite Down");
|
||||
assert_eq!(c.allocated_bytes(), charge);
|
||||
|
||||
// All full packets, including untouched slots, compared after each bulk copy.
|
||||
actual.copy_down_columns_to_slots(&mut [(0, &mut a, 1), (3, &mut b, 8)]).unwrap();
|
||||
for (column, packet, slot) in [(0usize, 0usize, 1usize), (3, 1, 8)] {
|
||||
expected_packets[packet][slot * 10240..(slot + 1) * 10240]
|
||||
.copy_from_slice(&down_bytes[column * 10240..(column + 1) * 10240]);
|
||||
}
|
||||
for (packet, expected) in [&a, &b, &d].iter().zip(&expected_packets) {
|
||||
same(&download(packet, PACKET_BYTES), expected, "two-packet copy+sentinels");
|
||||
}
|
||||
actual.copy_down_columns_to_slots(&mut [(0, &mut a, 9), (1, &mut b, 0), (2, &mut d, 4)]).unwrap();
|
||||
for (column, packet, slot) in [(0usize, 0usize, 9usize), (1, 1, 0), (2, 2, 4)] {
|
||||
expected_packets[packet][slot * 10240..(slot + 1) * 10240]
|
||||
.copy_from_slice(&down_bytes[column * 10240..(column + 1) * 10240]);
|
||||
}
|
||||
for (packet, expected) in [&a, &b, &d].iter().zip(&expected_packets) {
|
||||
same(&download(packet, PACKET_BYTES), expected, "three-packet copy+sentinels");
|
||||
}
|
||||
assert_eq!(c.allocated_bytes(), charge);
|
||||
|
||||
// Late-invalid entries must refuse before the earlier valid entry is copied.
|
||||
for (columns, slots) in [
|
||||
([0, 0], [2, 2]),
|
||||
([1, 0], [2, 2]),
|
||||
([0, T], [2, 2]),
|
||||
([0, u32::MAX], [2, 2]),
|
||||
([0, 1], [2, 10]),
|
||||
([0, 1], [2, u32::MAX]),
|
||||
] {
|
||||
assert!(matches!(
|
||||
actual
|
||||
.copy_down_columns_to_slots(&mut [(columns[0], &mut a, slots[0]), (columns[1], &mut b, slots[1])]),
|
||||
Err(Error::InvalidProjectionShape)
|
||||
));
|
||||
for (packet, expected) in [&a, &b, &d].iter().zip(&expected_packets) {
|
||||
same(&download(packet, PACKET_BYTES), expected, "all-before-copy refusal");
|
||||
}
|
||||
assert_eq!(c.allocated_bytes(), charge);
|
||||
}
|
||||
assert!(matches!(actual.copy_down_columns_to_slots(&mut []), Err(Error::InvalidProjectionShape)));
|
||||
assert!(matches!(
|
||||
actual.copy_down_columns_to_slots(&mut [(0, &mut a, 2), (1, &mut short, 1)]),
|
||||
Err(Error::InvalidProjectionShape)
|
||||
));
|
||||
same(&download(&short, 4), &[0x7e; 4], "short target unchanged");
|
||||
// RAII restoration runs on both the ordinary refusal and unwind path.
|
||||
let handle = a.handle;
|
||||
let mut alias = LivePacketAlias::new(&mut b, handle);
|
||||
let alias_result = actual.copy_down_columns_to_slots(&mut [(0, &mut a, 2), (1, alias.buffer(), 2)]);
|
||||
drop(alias);
|
||||
assert!(matches!(alias_result, Err(Error::OutputAliasesInput)));
|
||||
for (packet, expected) in [&a, &b, &d].iter().zip(&expected_packets) {
|
||||
same(&download(packet, PACKET_BYTES), expected, "alias refusal and restored real owner");
|
||||
}
|
||||
let mut foreign_packet = upload(&foreign, &before[0]);
|
||||
assert!(matches!(
|
||||
actual.copy_down_columns_to_slots(&mut [(0, &mut a, 2), (1, &mut foreign_packet, 2)]),
|
||||
Err(Error::ContextMismatch)
|
||||
));
|
||||
same(&download(&foreign_packet, PACKET_BYTES), &before[0], "foreign destination unchanged");
|
||||
foreign_packet.release().unwrap();
|
||||
assert_eq!(foreign.allocated_bytes(), 0);
|
||||
for (packet, expected) in [&a, &b, &d].iter().zip(&expected_packets) {
|
||||
same(&download(packet, PACKET_BYTES), expected, "foreign/short refusal no partial write");
|
||||
}
|
||||
same(&download(actual.workspace.buffer(DOWN).unwrap(), DOWN_BYTES), &down_bytes, "copy source immutable");
|
||||
assert!(!c.is_poisoned());
|
||||
assert_eq!(c.allocated_bytes(), charge);
|
||||
// Test-only actual status observation, not a forged finite producer: last
|
||||
// output word NaN must be visible after the queued compact witness completes.
|
||||
actual.workspace.buffers[DOWN]
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.copy_from_range(DOWN_BYTES - 4, &f32::NAN.to_le_bytes())
|
||||
.unwrap();
|
||||
assert!(matches!(actual.workspace.finite(DOWN, DOWN_BYTES / 4, false), Err(Error::NonFiniteMmqOutput("Down"))));
|
||||
actual.workspace.buffers[DOWN].as_mut().unwrap().copy_from_range(0, &down_bytes).unwrap();
|
||||
actual.workspace.finite(DOWN, DOWN_BYTES / 4, false).unwrap();
|
||||
assert!(!c.is_poisoned());
|
||||
assert_eq!(c.allocated_bytes(), charge);
|
||||
short.release().unwrap();
|
||||
d.release().unwrap();
|
||||
b.release().unwrap();
|
||||
a.release().unwrap();
|
||||
} // End exclusive actual lease before observing next immutable source/weights.
|
||||
queued_chain(&mut w, &base, &data, (&gu, &down), Packets::new(&c, &foreign));
|
||||
// End exclusive actual lease before observing next immutable source/weights.
|
||||
assert_eq!(c.allocated_bytes(), persistent_charge);
|
||||
same(
|
||||
&download(gu.native_buffer().unwrap(), gu.allocated_bytes()),
|
||||
&weight_before,
|
||||
"GU full weight owner unchanged",
|
||||
);
|
||||
same(
|
||||
&download(down.native_buffer().unwrap(), down.allocated_bytes()),
|
||||
&down_weight_before,
|
||||
"Down full weight owner unchanged",
|
||||
);
|
||||
w.close().unwrap();
|
||||
down.close().unwrap();
|
||||
gu.close().unwrap();
|
||||
assert_eq!(c.allocated_bytes(), 0);
|
||||
assert_eq!(foreign.allocated_bytes(), 0);
|
||||
foreign.close().unwrap();
|
||||
same(&owner_bytes(&gu), &weight_before, "GU full weight owner unchanged");
|
||||
same(&owner_bytes(&down), &down_weight_before, "Down full weight owner unchanged");
|
||||
queued_close(&c, foreign, w, (gu, down));
|
||||
c.close().unwrap();
|
||||
println!(
|
||||
"Queued changed seam exact:7680GUhidden+10240Down+153600packetwords;fullQ8;10bulkguards;lateNaNstatus;allocated0"
|
||||
"Queued changed seam exact:7680GUhidden+10240Down+153600packetwords;fullQ8;10bulkguards;\
|
||||
lateNaNstatus;allocated0"
|
||||
);
|
||||
}
|
||||
|
||||
#[path = "mmq_resident_gpu/owner_proof_fn.rs"]
|
||||
mod owner_proof_fn;
|
||||
#[allow(unused_imports)]
|
||||
use owner_proof_fn::*;
|
||||
#[path = "mmq_resident_gpu/queued_proof_fn.rs"]
|
||||
mod queued_proof_fn;
|
||||
#[allow(unused_imports)]
|
||||
use queued_proof_fn::*;
|
||||
#[path = "mmq_resident_gpu/digest_fn.rs"]
|
||||
mod digest_fn;
|
||||
#[allow(unused_imports)]
|
||||
|
||||
@@ -3,21 +3,26 @@
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
pub(super) fn chain(
|
||||
/// Original-first nonresident values the resident chain must reproduce.
|
||||
struct ChainBaseline {
|
||||
input_bytes: Vec<u8>,
|
||||
gate_values: Box<[f32]>,
|
||||
up_values: Box<[f32]>,
|
||||
hidden_values: Vec<u8>,
|
||||
down_bytes: Vec<u8>,
|
||||
}
|
||||
/// Immutable qualified modules + original native SwiGLU baseline FIRST.
|
||||
fn chain_baseline(
|
||||
r: &Receipt,
|
||||
w: &mut MmqWorkspace<'_>,
|
||||
kind: u32,
|
||||
t: u32,
|
||||
gu: &MmqWeights<'_>,
|
||||
down: &MmqWeights<'_>,
|
||||
) -> usize {
|
||||
let c = w.context;
|
||||
c: &Context<'_>,
|
||||
(kind, t): (u32, u32),
|
||||
(gu, down): (&MmqWeights<'_>, &MmqWeights<'_>),
|
||||
) -> ChainBaseline {
|
||||
let old = c.mmq_experiment().unwrap();
|
||||
let input_bytes = file(r, kind, t, "input.f32");
|
||||
let input = upload(c, &input_bytes);
|
||||
let mut gate = c.allocate(t as usize * 2560).unwrap();
|
||||
let mut up = c.allocate(t as usize * 2560).unwrap();
|
||||
// Immutable qualified modules + original native SwiGLU baseline FIRST.
|
||||
let (gate_values, q8) = old.project_observed(MmqProfile::OriginalAdaptive, gu, &input, t, &mut gate).unwrap();
|
||||
let (up_values, _) = old.project_observed(MmqProfile::OriginalAdaptive, gu, &input, t, &mut up).unwrap();
|
||||
compare(&gate_values, &file(r, kind, t, "reference_streamk.f32"), "baseline GU original");
|
||||
@@ -31,34 +36,46 @@ pub(super) fn chain(
|
||||
let down_bytes = download(&output, t as usize * 10240);
|
||||
finite(&down_bytes);
|
||||
compare(&down_values, &down_bytes, "baseline Down actual");
|
||||
output.release().unwrap();
|
||||
hidden.release().unwrap();
|
||||
up.release().unwrap();
|
||||
gate.release().unwrap();
|
||||
input.release().unwrap();
|
||||
for buffer in [output, hidden, up, gate, input] {
|
||||
buffer.release().unwrap();
|
||||
}
|
||||
old.close().unwrap();
|
||||
ChainBaseline { input_bytes, gate_values, up_values, hidden_values, down_bytes }
|
||||
}
|
||||
pub(super) fn chain(
|
||||
r: &Receipt,
|
||||
w: &mut MmqWorkspace<'_>,
|
||||
kind: u32,
|
||||
t: u32,
|
||||
gu: &MmqWeights<'_>,
|
||||
down: &MmqWeights<'_>,
|
||||
) -> usize {
|
||||
let c = w.context;
|
||||
let base = chain_baseline(r, c, (kind, t), (gu, down));
|
||||
let charge = c.allocated_bytes();
|
||||
let mut prepared = w.prepare_gate_up_chunk(&floats(&input_bytes), t).unwrap();
|
||||
let mut prepared = w.prepare_gate_up_chunk(&floats(&base.input_bytes), t).unwrap();
|
||||
let ids: Vec<_> = (0..t).collect();
|
||||
let group = prepared.gather_gate_up(&ids).unwrap();
|
||||
let actual_hidden = group.gate_up(gu, gu).unwrap();
|
||||
chain_hidden_checks(&actual_hidden, &base, t);
|
||||
let actual = actual_hidden.down(down).unwrap();
|
||||
let down_bytes = &base.down_bytes;
|
||||
same(&download(actual.workspace.buffer(DOWN).unwrap(), down_bytes.len()), down_bytes, "resident actual Down");
|
||||
chain_slot_checks(c, &actual, down_bytes, t);
|
||||
assert_eq!(c.allocated_bytes(), charge);
|
||||
t as usize * (640 * 3 + 2560)
|
||||
}
|
||||
fn chain_hidden_checks(actual_hidden: &MmqHidden<'_, '_>, base: &ChainBaseline, t: u32) {
|
||||
let n = t as usize * 2560;
|
||||
same(&download(actual_hidden.workspace.buffer(GATE).unwrap(), n), &encoded(&base.gate_values), "resident Gate");
|
||||
same(&download(actual_hidden.workspace.buffer(UP).unwrap(), n), &encoded(&base.up_values), "resident Up");
|
||||
same(
|
||||
&download(actual_hidden.workspace.buffer(GATE).unwrap(), t as usize * 2560),
|
||||
&encoded(&gate_values),
|
||||
"resident Gate",
|
||||
);
|
||||
same(
|
||||
&download(actual_hidden.workspace.buffer(UP).unwrap(), t as usize * 2560),
|
||||
&encoded(&up_values),
|
||||
"resident Up",
|
||||
);
|
||||
same(
|
||||
&download(actual_hidden.workspace.buffer(HIDDEN).unwrap(), t as usize * 2560),
|
||||
&hidden_values,
|
||||
&download(actual_hidden.workspace.buffer(HIDDEN).unwrap(), n),
|
||||
&base.hidden_values,
|
||||
"resident original SwiGLU",
|
||||
);
|
||||
let actual = actual_hidden.down(down).unwrap();
|
||||
same(&download(actual.workspace.buffer(DOWN).unwrap(), down_bytes.len()), &down_bytes, "resident actual Down");
|
||||
}
|
||||
fn chain_slot_checks(c: &Context<'_>, actual: &FiniteMmqOutput<'_, '_>, down_bytes: &[u8], t: u32) {
|
||||
let mut packet = upload(c, &vec![0xa5; 102400]);
|
||||
let before = download(&packet, 102400);
|
||||
for (column, slot) in [(0, 0), (t / 2, 4), (t - 1, 9)] {
|
||||
@@ -71,32 +88,42 @@ pub(super) fn chain(
|
||||
expected[d..d + 10240].copy_from_slice(&down_bytes[s..s + 10240]);
|
||||
}
|
||||
same(&download(&packet, 102400), &expected, "full slotpacket + untouched slots");
|
||||
for (column, slot) in [(t, 0), (u32::MAX, 0), (0, 10), (0, u32::MAX)] {
|
||||
assert!(matches!(
|
||||
actual.copy_down_column_to_slot(column, &mut packet, slot),
|
||||
Err(Error::InvalidProjectionShape)
|
||||
));
|
||||
same(&download(&packet, 102400), &expected, "refused slot immutable");
|
||||
chain_slot_refusals(actual, &mut packet, &expected, t);
|
||||
chain_short_packet(c, actual);
|
||||
if t == 4 {
|
||||
chain_foreign_packet(c, actual, &before);
|
||||
}
|
||||
// Source-only exclusive lifetime: no second mutable workspace use while actual exists.
|
||||
packet.release().unwrap();
|
||||
}
|
||||
fn chain_slot_refusals(actual: &FiniteMmqOutput<'_, '_>, packet: &mut DeviceBuffer<'_>, expected: &[u8], t: u32) {
|
||||
for (column, slot) in [(t, 0), (u32::MAX, 0), (0, 10), (0, u32::MAX)] {
|
||||
assert!(matches!(actual.copy_down_column_to_slot(column, packet, slot), Err(Error::InvalidProjectionShape)));
|
||||
same(&download(packet, 102400), expected, "refused slot immutable");
|
||||
}
|
||||
}
|
||||
fn chain_short_packet(c: &Context<'_>, actual: &FiniteMmqOutput<'_, '_>) {
|
||||
let mut short = upload(c, &[0xa5; 4]);
|
||||
assert!(matches!(actual.copy_down_column_to_slot(0, &mut short, 0), Err(Error::InvalidProjectionShape)));
|
||||
same(&download(&short, 4), &[0xa5; 4], "short packet immutable");
|
||||
short.release().unwrap();
|
||||
if t == 4 {
|
||||
let foreign = c.driver.device(0).unwrap().create_context(102400).unwrap();
|
||||
let mut destination = upload(&foreign, &before);
|
||||
assert!(matches!(actual.copy_down_column_to_slot(0, &mut destination, 0), Err(Error::ContextMismatch)));
|
||||
same(&download(&destination, 102400), &before, "foreign slot destination immutable");
|
||||
destination.release().unwrap();
|
||||
assert_eq!(foreign.allocated_bytes(), 0);
|
||||
foreign.close().unwrap();
|
||||
}
|
||||
// Source-only exclusive lifetime: no second mutable workspace use while actual exists.
|
||||
packet.release().unwrap();
|
||||
assert_eq!(c.allocated_bytes(), charge);
|
||||
t as usize * (640 * 3 + 2560)
|
||||
}
|
||||
fn chain_foreign_packet(c: &Context<'_>, actual: &FiniteMmqOutput<'_, '_>, before: &[u8]) {
|
||||
let foreign = c.driver.device(0).unwrap().create_context(102400).unwrap();
|
||||
let mut destination = upload(&foreign, before);
|
||||
assert!(matches!(actual.copy_down_column_to_slot(0, &mut destination, 0), Err(Error::ContextMismatch)));
|
||||
same(&download(&destination, 102400), before, "foreign slot destination immutable");
|
||||
destination.release().unwrap();
|
||||
assert_eq!(foreign.allocated_bytes(), 0);
|
||||
foreign.close().unwrap();
|
||||
}
|
||||
pub(super) fn witness(w: &mut MmqWorkspace<'_>, reference: &[u8]) -> usize {
|
||||
let gate = witness_gate(w);
|
||||
let q8 = witness_q8(w, reference);
|
||||
witness_zero(w);
|
||||
gate + q8
|
||||
}
|
||||
fn witness_gate(w: &mut MmqWorkspace<'_>) -> usize {
|
||||
let mut checked = 0;
|
||||
let mut base = vec![0u8; 163840];
|
||||
for (i, b) in base.as_chunks_mut::<4>().0.iter_mut().enumerate() {
|
||||
@@ -119,50 +146,55 @@ pub(super) fn witness(w: &mut MmqWorkspace<'_>, reference: &[u8]) -> usize {
|
||||
assert!(!w.context.is_poisoned());
|
||||
checked += 1;
|
||||
}
|
||||
checked
|
||||
}
|
||||
fn witness_check_q8(w: &mut MmqWorkspace<'_>, amount: usize, data: &[u8], expected: u32) {
|
||||
w.buffers[GROUP].as_mut().unwrap().copy_from_range(0, data).unwrap();
|
||||
let r = w.finite(GROUP, amount, true);
|
||||
match expected {
|
||||
0 => r.unwrap(),
|
||||
1 | 3 => assert!(matches!(r, Err(Error::InvalidQ8Activation))),
|
||||
2 => assert!(matches!(r, Err(Error::InvalidDriverValue("MMQ dirty Q8 tail")))),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
assert_eq!(download(w.buffer(STATUS).unwrap(), 4), expected.to_le_bytes());
|
||||
assert!(!w.context.is_poisoned());
|
||||
}
|
||||
fn witness_q8(w: &mut MmqWorkspace<'_>, reference: &[u8]) -> usize {
|
||||
let mut checked = 0;
|
||||
let amount = (reference.len() - 9216) / 144;
|
||||
let check = |w: &mut MmqWorkspace<'_>, data: &[u8], expected: u32| {
|
||||
w.buffers[GROUP].as_mut().unwrap().copy_from_range(0, data).unwrap();
|
||||
let r = w.finite(GROUP, amount, true);
|
||||
match expected {
|
||||
0 => r.unwrap(),
|
||||
1 | 3 => assert!(matches!(r, Err(Error::InvalidQ8Activation))),
|
||||
2 => assert!(matches!(r, Err(Error::InvalidDriverValue("MMQ dirty Q8 tail")))),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
assert_eq!(download(w.buffer(STATUS).unwrap(), 4), expected.to_le_bytes());
|
||||
assert!(!w.context.is_poisoned());
|
||||
};
|
||||
check(w, reference, 0);
|
||||
witness_check_q8(w, amount, reference, 0);
|
||||
checked += 1;
|
||||
for scale in 0..4 {
|
||||
for bad in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
|
||||
let mut data = reference.to_vec();
|
||||
let at = (amount - 1) * 144 + scale * 4;
|
||||
data[at..at + 4].copy_from_slice(&bad.to_le_bytes());
|
||||
check(w, &data, 1);
|
||||
witness_check_q8(w, amount, &data, 1);
|
||||
checked += 1;
|
||||
}
|
||||
}
|
||||
for at in [reference.len() - 9216, reference.len() - 1] {
|
||||
let mut data = reference.to_vec();
|
||||
data[at] = 1;
|
||||
check(w, &data, 2);
|
||||
witness_check_q8(w, amount, &data, 2);
|
||||
checked += 1;
|
||||
}
|
||||
let mut data = reference.to_vec();
|
||||
data[(amount - 1) * 144 + 16..(amount - 1) * 144 + 20].copy_from_slice(&f32::NAN.to_le_bytes());
|
||||
check(w, &data, 0);
|
||||
witness_check_q8(w, amount, &data, 0);
|
||||
checked += 1;
|
||||
data[(amount - 1) * 144..(amount - 1) * 144 + 4].copy_from_slice(&f32::NAN.to_le_bytes());
|
||||
*data.last_mut().unwrap() = 1;
|
||||
check(w, &data, 3);
|
||||
checked += 1;
|
||||
witness_check_q8(w, amount, &data, 3);
|
||||
checked + 1
|
||||
}
|
||||
fn witness_zero(w: &mut MmqWorkspace<'_>) {
|
||||
w.buffers[GATE].as_mut().unwrap().copy_from(&vec![0xa5; 163840]).unwrap();
|
||||
w.zero(GATE, 16).unwrap();
|
||||
let zeros = download(w.buffer(GATE).unwrap(), 163840);
|
||||
assert_eq!(&zeros[..16], &[0; 16]);
|
||||
assert!(zeros[16..].iter().all(|v| *v == 0xa5));
|
||||
checked
|
||||
}
|
||||
impl<'borrow, 'ctx> LivePacketAlias<'borrow, 'ctx> {
|
||||
pub(super) fn new(buffer: &'borrow mut DeviceBuffer<'ctx>, handle: Option<crate::ffi::BufferHandle>) -> Self {
|
||||
|
||||
@@ -38,59 +38,20 @@ impl Receipt {
|
||||
let bytes = read_regular(&root.join("ROOT-REFERENCE.tsv"), 64 * 1024);
|
||||
assert_eq!(digest(&bytes), expected, "ROOT manifest pin");
|
||||
let text = std::str::from_utf8(&bytes).unwrap();
|
||||
let mut headers = BTreeMap::new();
|
||||
let mut files = BTreeMap::new();
|
||||
for line in text.lines() {
|
||||
let fields: Vec<_> = line.split('\t').collect();
|
||||
match fields.as_slice() {
|
||||
["file", name, length, sha] => {
|
||||
assert!(Path::new(name).components().all(|c| matches!(c, Component::Normal(_))));
|
||||
hex(sha);
|
||||
let bytes: usize = length.parse().unwrap();
|
||||
assert!(bytes > 0 && bytes <= 64 * 1024 * 1024);
|
||||
assert!(
|
||||
files.insert((*name).to_owned(), Pin { bytes, sha: (*sha).to_owned() }).is_none(),
|
||||
"duplicate file"
|
||||
);
|
||||
}
|
||||
[key, value] => {
|
||||
assert!(headers.insert(*key, *value).is_none(), "duplicate header");
|
||||
}
|
||||
_ => panic!("closed ROOT manifest record"),
|
||||
}
|
||||
}
|
||||
assert_eq!(headers.len(), 6);
|
||||
for (key, value) in [
|
||||
("schema", "stra169-mmq-reference-v1"),
|
||||
("upstream", UPSTREAM),
|
||||
("source_sha", SOURCE_SHA),
|
||||
("nvcc", "12.8.93"),
|
||||
("profile", "sm120-fastmath-adaptive-original-first"),
|
||||
("cases", "12"),
|
||||
] {
|
||||
assert_eq!(headers.get(key).copied(), Some(value), "ROOT provenance {key}");
|
||||
}
|
||||
let mut allowed = vec![
|
||||
"COMPLETE.json".to_owned(),
|
||||
"original_mmq_oracle".to_owned(),
|
||||
"ORACLE-SOURCE-MANIFEST.json".to_owned(),
|
||||
];
|
||||
for kind in [18, 22, 20, 42] {
|
||||
for tokens in [4, 16, 64] {
|
||||
for name in CASE_FILES {
|
||||
allowed.push(format!("type{kind}-t{tokens}/{name}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
allowed.sort();
|
||||
assert_eq!(files.keys().cloned().collect::<Vec<_>>(), allowed, "exact 87 pinned files");
|
||||
let (headers, files) = parse_manifest(text);
|
||||
check_provenance(&headers);
|
||||
check_pinned_file_set(&files);
|
||||
let receipt = Self { root, files };
|
||||
for name in receipt.files.keys() {
|
||||
let _ = receipt.read(name);
|
||||
receipt.check_pinned_contents();
|
||||
receipt
|
||||
}
|
||||
fn check_pinned_contents(&self) {
|
||||
for name in self.files.keys() {
|
||||
let _ = self.read(name);
|
||||
}
|
||||
assert_eq!(digest(&receipt.read("ORACLE-SOURCE-MANIFEST.json")), SOURCE_SHA);
|
||||
assert!(receipt.read("original_mmq_oracle").starts_with(b"\x7fELF"));
|
||||
let complete = String::from_utf8(receipt.read("COMPLETE.json")).unwrap();
|
||||
assert_eq!(digest(&self.read("ORACLE-SOURCE-MANIFEST.json")), SOURCE_SHA);
|
||||
assert!(self.read("original_mmq_oracle").starts_with(b"\x7fELF"));
|
||||
let complete = String::from_utf8(self.read("COMPLETE.json")).unwrap();
|
||||
for (key, value) in [
|
||||
("schema", "1"),
|
||||
("cases", "12"),
|
||||
@@ -103,7 +64,6 @@ impl Receipt {
|
||||
}
|
||||
assert!(complete.contains("\"original_adaptive_streamk_primary\""));
|
||||
assert!(complete.contains("\"fixed_j64_streamk\""));
|
||||
receipt
|
||||
}
|
||||
pub(super) fn read(&self, name: &str) -> Vec<u8> {
|
||||
let pin = self.files.get(name).expect("closed pinned fixture");
|
||||
@@ -113,6 +73,54 @@ impl Receipt {
|
||||
bytes
|
||||
}
|
||||
}
|
||||
fn parse_manifest(text: &str) -> (BTreeMap<&str, &str>, BTreeMap<String, Pin>) {
|
||||
let mut headers = BTreeMap::new();
|
||||
let mut files = BTreeMap::new();
|
||||
for line in text.lines() {
|
||||
let fields: Vec<_> = line.split('\t').collect();
|
||||
match fields.as_slice() {
|
||||
["file", name, length, sha] => pin_file(&mut files, name, length, sha),
|
||||
[key, value] => {
|
||||
assert!(headers.insert(*key, *value).is_none(), "duplicate header");
|
||||
}
|
||||
_ => panic!("closed ROOT manifest record"),
|
||||
}
|
||||
}
|
||||
(headers, files)
|
||||
}
|
||||
fn pin_file(files: &mut BTreeMap<String, Pin>, name: &str, length: &str, sha: &str) {
|
||||
assert!(Path::new(name).components().all(|c| matches!(c, Component::Normal(_))));
|
||||
hex(sha);
|
||||
let bytes: usize = length.parse().unwrap();
|
||||
assert!(bytes > 0 && bytes <= 64 * 1024 * 1024);
|
||||
assert!(files.insert(name.to_owned(), Pin { bytes, sha: sha.to_owned() }).is_none(), "duplicate file");
|
||||
}
|
||||
fn check_provenance(headers: &BTreeMap<&str, &str>) {
|
||||
assert_eq!(headers.len(), 6);
|
||||
for (key, value) in [
|
||||
("schema", "stra169-mmq-reference-v1"),
|
||||
("upstream", UPSTREAM),
|
||||
("source_sha", SOURCE_SHA),
|
||||
("nvcc", "12.8.93"),
|
||||
("profile", "sm120-fastmath-adaptive-original-first"),
|
||||
("cases", "12"),
|
||||
] {
|
||||
assert_eq!(headers.get(key).copied(), Some(value), "ROOT provenance {key}");
|
||||
}
|
||||
}
|
||||
fn check_pinned_file_set(files: &BTreeMap<String, Pin>) {
|
||||
let mut allowed =
|
||||
vec!["COMPLETE.json".to_owned(), "original_mmq_oracle".to_owned(), "ORACLE-SOURCE-MANIFEST.json".to_owned()];
|
||||
for kind in [18, 22, 20, 42] {
|
||||
for tokens in [4, 16, 64] {
|
||||
for name in CASE_FILES {
|
||||
allowed.push(format!("type{kind}-t{tokens}/{name}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
allowed.sort();
|
||||
assert_eq!(files.keys().cloned().collect::<Vec<_>>(), allowed, "exact 87 pinned files");
|
||||
}
|
||||
pub(super) fn field<'a>(json: &'a str, key: &str) -> &'a str {
|
||||
let needle = format!("\"{key}\":");
|
||||
assert_eq!(json.matches(&needle).count(), 1, "unique metadata key");
|
||||
|
||||
@@ -19,24 +19,42 @@ pub(super) fn native_owner(c: &Context<'_>, dual: &MmqWeights<'_>, raw: &[u8], d
|
||||
assert_eq!(c.allocated_bytes(), charge);
|
||||
same(&download(&input, spec.columns * 4), &data[..spec.columns * 4], "native input immutable");
|
||||
if spec.rows == 640 {
|
||||
let prepared = c.prepare_routed_gate_up_q8(&input).unwrap();
|
||||
c.project_routed_gate_up_prepared(&raw_weight, &prepared, &mut old, raw_format(dual.format)).unwrap();
|
||||
dual.native_gate_up_prepared(&prepared, &mut new).unwrap();
|
||||
same(&download(&new, 2560), &download(&old, 2560), "dual prepared");
|
||||
c.project_routed_gate_up_columns(&raw_weight, &[&prepared], &mut old, raw_format(dual.format)).unwrap();
|
||||
dual.native_gate_up_columns(&[&prepared], &mut new).unwrap();
|
||||
same(&download(&new, 2560), &download(&old, 2560), "dual grouped GU");
|
||||
prepared.close().unwrap();
|
||||
native_gate_up_owner(c, dual, &raw_weight, &input, [&mut old, &mut new]);
|
||||
} else {
|
||||
c.project_routed_down_columns(&raw_weight, &input, &mut old, 1, raw_format(dual.format)).unwrap();
|
||||
dual.native_down_columns(&input, 1, &mut new).unwrap();
|
||||
same(&download(&new, 10240), &download(&old, 10240), "dual grouped Down");
|
||||
native_down_owner(c, dual, &raw_weight, &input, [&mut old, &mut new]);
|
||||
}
|
||||
new.release().unwrap();
|
||||
old.release().unwrap();
|
||||
input.release().unwrap();
|
||||
raw_weight.release().unwrap();
|
||||
}
|
||||
fn native_gate_up_owner(
|
||||
c: &Context<'_>,
|
||||
dual: &MmqWeights<'_>,
|
||||
raw_weight: &DeviceBuffer<'_>,
|
||||
input: &DeviceBuffer<'_>,
|
||||
[old, new]: [&mut DeviceBuffer<'_>; 2],
|
||||
) {
|
||||
let prepared = c.prepare_routed_gate_up_q8(input).unwrap();
|
||||
c.project_routed_gate_up_prepared(raw_weight, &prepared, old, raw_format(dual.format)).unwrap();
|
||||
dual.native_gate_up_prepared(&prepared, new).unwrap();
|
||||
same(&download(new, 2560), &download(old, 2560), "dual prepared");
|
||||
c.project_routed_gate_up_columns(raw_weight, &[&prepared], old, raw_format(dual.format)).unwrap();
|
||||
dual.native_gate_up_columns(&[&prepared], new).unwrap();
|
||||
same(&download(new, 2560), &download(old, 2560), "dual grouped GU");
|
||||
prepared.close().unwrap();
|
||||
}
|
||||
fn native_down_owner(
|
||||
c: &Context<'_>,
|
||||
dual: &MmqWeights<'_>,
|
||||
raw_weight: &DeviceBuffer<'_>,
|
||||
input: &DeviceBuffer<'_>,
|
||||
[old, new]: [&mut DeviceBuffer<'_>; 2],
|
||||
) {
|
||||
c.project_routed_down_columns(raw_weight, input, old, 1, raw_format(dual.format)).unwrap();
|
||||
dual.native_down_columns(input, 1, new).unwrap();
|
||||
same(&download(new, 10240), &download(old, 10240), "dual grouped Down");
|
||||
}
|
||||
pub(super) fn direct(
|
||||
r: &Receipt,
|
||||
w: &mut MmqWorkspace<'_>,
|
||||
@@ -67,6 +85,17 @@ pub(super) fn direct(
|
||||
assert_eq!(w.context.allocated_bytes(), charge);
|
||||
(l.output / 4, l.packed)
|
||||
}
|
||||
fn gathered_expected(original: &[u8], ids: &[u32]) -> Vec<u8> {
|
||||
let mut expected = vec![0; 20 * ids.len() * 144 + 9216];
|
||||
for block in 0..20 {
|
||||
for (col, row) in ids.iter().enumerate() {
|
||||
let src = (block * 64 + *row as usize) * 144;
|
||||
let dst = (block * ids.len() + col) * 144;
|
||||
expected[dst..dst + 144].copy_from_slice(&original[src..src + 144]);
|
||||
}
|
||||
}
|
||||
expected
|
||||
}
|
||||
pub(super) fn gather(r: &Receipt, w: &mut MmqWorkspace<'_>) -> usize {
|
||||
let input = floats(&file(r, 18, 64, "input.f32"));
|
||||
let original = file(r, 18, 64, "reference_q8.bin");
|
||||
@@ -83,14 +112,7 @@ pub(super) fn gather(r: &Receipt, w: &mut MmqWorkspace<'_>) -> usize {
|
||||
let mut checked = 0;
|
||||
for ids in [(0..64).collect::<Vec<u32>>(), vec![0, 7, 32, 63], (0..16).map(|i| i * 4).collect()] {
|
||||
let group = source.gather_gate_up(&ids).unwrap();
|
||||
let mut expected = vec![0; 20 * ids.len() * 144 + 9216];
|
||||
for block in 0..20 {
|
||||
for (col, row) in ids.iter().enumerate() {
|
||||
let src = (block * 64 + *row as usize) * 144;
|
||||
let dst = (block * ids.len() + col) * 144;
|
||||
expected[dst..dst + 144].copy_from_slice(&original[src..src + 144]);
|
||||
}
|
||||
}
|
||||
let expected = gathered_expected(&original, &ids);
|
||||
same(&download(group.workspace.buffer(GROUP).unwrap(), expected.len()), &expected, "entire gathered D4/tail");
|
||||
same(
|
||||
&download(group.workspace.buffer(CHUNK).unwrap(), original.len()),
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
//! Phases of the resident chain/gather/dual-native proof, split out of the test entry by STRA-338.
|
||||
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
pub(super) type ProofFormats = [(u32, MmqFormat, u32); 4];
|
||||
|
||||
pub(super) fn reference_receipt() -> Receipt {
|
||||
assert_eq!(std::env::var("LELEKA_STRA169_RUN_MMQ_NUMERIC").as_deref(), Ok("yes"));
|
||||
let expected = std::env::var("LELEKA_STRA169_MMQ_REFERENCE_MANIFEST_SHA").unwrap();
|
||||
assert_eq!(expected, ROOT_REFERENCE_SHA);
|
||||
Receipt::load(PathBuf::from(std::env::var_os("LELEKA_STRA169_MMQ_REFERENCE_DIR").unwrap()), &expected)
|
||||
}
|
||||
pub(super) fn prove_owner_formats<'c>(
|
||||
receipt: &Receipt,
|
||||
c: &'c Context<'c>,
|
||||
w: &mut MmqWorkspace<'c>,
|
||||
formats: ProofFormats,
|
||||
) -> (Vec<MmqWeights<'c>>, usize, usize) {
|
||||
let mut owners = Vec::new();
|
||||
let mut words = 0;
|
||||
let mut packed = 0;
|
||||
for (kind, format, t) in formats {
|
||||
let (owner, n, q) = prove_owner(receipt, c, w, (kind, format, t));
|
||||
words += n;
|
||||
packed += q;
|
||||
owners.push(owner);
|
||||
}
|
||||
(owners, words, packed)
|
||||
}
|
||||
fn owner_padding(kind: u32) -> usize {
|
||||
match kind {
|
||||
20 => 216,
|
||||
42 => 108,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
fn prove_owner<'c>(
|
||||
receipt: &Receipt,
|
||||
c: &'c Context<'c>,
|
||||
w: &mut MmqWorkspace<'c>,
|
||||
(kind, format, t): (u32, MmqFormat, u32),
|
||||
) -> (MmqWeights<'c>, usize, usize) {
|
||||
let raw = file(receipt, kind, 4, "weights.bin");
|
||||
let data = file(receipt, kind, t, "input.f32");
|
||||
let owner = c.mmq_weights(format, &raw).unwrap();
|
||||
assert_eq!(owner.allocated_bytes(), raw.len() + owner_padding(kind));
|
||||
let before = download(owner.native_buffer().unwrap(), owner.allocated_bytes());
|
||||
same(&before[..raw.len()], &raw, "dual raw identity");
|
||||
assert!(before[raw.len()..].iter().all(|v| *v == 0));
|
||||
native_owner(c, &owner, &raw, &data);
|
||||
let (n, q) = direct(receipt, w, kind, t, &owner);
|
||||
same(
|
||||
&download(owner.native_buffer().unwrap(), owner.allocated_bytes()),
|
||||
&before,
|
||||
"dual entire allocation immutable",
|
||||
);
|
||||
(owner, n, q)
|
||||
}
|
||||
pub(super) fn prove_gather_chain_abandon(receipt: &Receipt, w: &mut MmqWorkspace<'_>, owners: &[MmqWeights<'_>]) {
|
||||
let gathered = gather(receipt, w);
|
||||
assert_eq!(gathered, 269568);
|
||||
let chain_words =
|
||||
chain(receipt, w, 18, 4, &owners[0], &owners[2]) + chain(receipt, w, 22, 64, &owners[1], &owners[3]);
|
||||
assert_eq!(chain_words, 304640);
|
||||
// Abandon a completed group without publication, then next genuine invocation.
|
||||
{
|
||||
let input = floats(&file(receipt, 18, 4, "input.f32"));
|
||||
let mut chunk = w.prepare_gate_up_chunk(&input, 4).unwrap();
|
||||
let _private = chunk.gather_gate_up(&[0, 3]).unwrap();
|
||||
}
|
||||
assert_eq!(direct(receipt, w, 18, 4, &owners[0]), (2560, 20736));
|
||||
assert_eq!(witness(w, &file(receipt, 18, 4, "reference_q8.bin")), 21);
|
||||
}
|
||||
pub(super) fn prove_guards<'c>(receipt: &Receipt, driver: &Driver, c: &'c Context<'c>, w: &mut MmqWorkspace<'c>) {
|
||||
let charge = c.allocated_bytes();
|
||||
for t in [0, 65, u32::MAX] {
|
||||
assert!(matches!(w.prepare_gate_up_chunk(&[0.0; 2560], t), Err(Error::InvalidProjectionShape)));
|
||||
}
|
||||
let mut invalid = floats(&file(receipt, 18, 4, "input.f32"));
|
||||
*invalid.last_mut().unwrap() = f32::NAN;
|
||||
assert!(matches!(w.prepare_gate_up_chunk(&invalid, 4), Err(Error::InvalidQ8Activation)));
|
||||
assert_eq!(c.allocated_bytes(), charge);
|
||||
assert!(!c.is_poisoned());
|
||||
prove_foreign_weight(receipt, driver, w);
|
||||
}
|
||||
fn prove_foreign_weight(receipt: &Receipt, driver: &Driver, w: &mut MmqWorkspace<'_>) {
|
||||
let foreign = driver.device(0).unwrap().create_context(BUDGET).unwrap();
|
||||
let foreign_weight = foreign.mmq_weights(MmqFormat::Iq3XxsGateUp, &file(receipt, 18, 4, "weights.bin")).unwrap();
|
||||
assert!(matches!(w.project(&foreign_weight, CHUNK, GATE, 4), Err(Error::ContextMismatch)));
|
||||
foreign_weight.close().unwrap();
|
||||
assert_eq!(foreign.allocated_bytes(), 0);
|
||||
foreign.close().unwrap();
|
||||
}
|
||||
pub(super) fn close_owners(receipt: &Receipt, formats: ProofFormats, owners: Vec<MmqWeights<'_>>) {
|
||||
for ((kind, _, _), owner) in formats.into_iter().zip(owners) {
|
||||
let raw = file(receipt, kind, 4, "weights.bin");
|
||||
let all = download(owner.native_buffer().unwrap(), owner.allocated_bytes());
|
||||
same(&all[..raw.len()], &raw, "all operations weight immutability");
|
||||
assert!(all[raw.len()..].iter().all(|v| *v == 0));
|
||||
owner.close().unwrap();
|
||||
}
|
||||
}
|
||||
pub(super) fn prove_small_budget(driver: &Driver, sms: u32) {
|
||||
let small = driver.device(0).unwrap().create_context(2272515 + sms as usize * 32768).unwrap();
|
||||
assert!(matches!(small.mmq_workspace(), Err(Error::BudgetExceeded)));
|
||||
assert_eq!(small.allocated_bytes(), 0);
|
||||
small.close().unwrap();
|
||||
}
|
||||
pub(super) const PROOF_FORMATS: ProofFormats = [
|
||||
(18, MmqFormat::Iq3XxsGateUp, 4),
|
||||
(22, MmqFormat::Iq2SGateUp, 64),
|
||||
(20, MmqFormat::Iq4NlDown, 4),
|
||||
(42, MmqFormat::Q2Down, 64),
|
||||
];
|
||||
@@ -0,0 +1,231 @@
|
||||
//! Phases of the queued finite chain / bulk Down packet proof, split out of the test entry by STRA-338.
|
||||
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
const QT: u32 = 4;
|
||||
const GU_BYTES: usize = 4 * 2560;
|
||||
const DOWN_BYTES: usize = 4 * 10240;
|
||||
const PACKET_BYTES: usize = 102400;
|
||||
|
||||
/// Original-first nonresident baseline the queued transport must reproduce bit for bit.
|
||||
pub(super) struct Baseline {
|
||||
gate_values: Box<[f32]>,
|
||||
up_values: Box<[f32]>,
|
||||
q8: Box<[u8]>,
|
||||
hidden_bytes: Vec<u8>,
|
||||
down_q8: Box<[u8]>,
|
||||
down_bytes: Vec<u8>,
|
||||
}
|
||||
/// Four real device packets (three full, one short) with the bytes each must hold after every step.
|
||||
pub(super) struct Packets<'c> {
|
||||
c: &'c Context<'c>,
|
||||
foreign: &'c Context<'c>,
|
||||
a: DeviceBuffer<'c>,
|
||||
b: DeviceBuffer<'c>,
|
||||
d: DeviceBuffer<'c>,
|
||||
short: DeviceBuffer<'c>,
|
||||
charge: usize,
|
||||
before: [Vec<u8>; 3],
|
||||
expected: [Vec<u8>; 3],
|
||||
}
|
||||
|
||||
pub(super) fn check_sms(w: &MmqWorkspace<'_>, receipt: &Receipt) {
|
||||
let sms = w.experiment.as_ref().unwrap().sms;
|
||||
assert!((1..=256).contains(&sms));
|
||||
let metadata = String::from_utf8(file(receipt, 18, QT, "metadata.json")).unwrap();
|
||||
assert_eq!(integer(&metadata, "nsm"), sms as usize);
|
||||
}
|
||||
pub(super) fn owner_bytes(owner: &MmqWeights<'_>) -> Vec<u8> {
|
||||
download(owner.native_buffer().unwrap(), owner.allocated_bytes())
|
||||
}
|
||||
pub(super) fn queued_baseline<'c>(
|
||||
receipt: &Receipt,
|
||||
c: &'c Context<'c>,
|
||||
(gu, down): (&MmqWeights<'_>, &MmqWeights<'_>),
|
||||
data: &[u8],
|
||||
) -> Baseline {
|
||||
// Actual qualified nonresident baseline FIRST, with real original refs.
|
||||
let old = c.mmq_experiment().unwrap();
|
||||
let input = upload(c, data);
|
||||
let mut gate = c.allocate(GU_BYTES).unwrap();
|
||||
let mut up = c.allocate(GU_BYTES).unwrap();
|
||||
let (gate_values, q8) = old.project_observed(MmqProfile::OriginalAdaptive, gu, &input, QT, &mut gate).unwrap();
|
||||
let (up_values, _) = old.project_observed(MmqProfile::OriginalAdaptive, gu, &input, QT, &mut up).unwrap();
|
||||
compare(&gate_values, &file(receipt, 18, QT, "reference_streamk.f32"), "original GU");
|
||||
same(&q8, &file(receipt, 18, QT, "reference_q8.bin"), "original D4");
|
||||
let mut hidden = c.allocate(GU_BYTES).unwrap();
|
||||
c.moe_swiglu_columns(&gate, &up, &mut hidden, QT as usize).unwrap();
|
||||
let hidden_bytes = download(&hidden, GU_BYTES);
|
||||
finite(&hidden_bytes);
|
||||
let mut old_output = c.allocate(DOWN_BYTES).unwrap();
|
||||
let (down_values, down_q8) =
|
||||
old.project_observed(MmqProfile::OriginalAdaptive, down, &hidden, QT, &mut old_output).unwrap();
|
||||
let down_bytes = download(&old_output, DOWN_BYTES);
|
||||
compare(&down_values, &down_bytes, "qualified actual Down");
|
||||
same(&download(&input, data.len()), data, "baseline source immutable");
|
||||
for buffer in [old_output, hidden, up, gate, input] {
|
||||
buffer.release().unwrap();
|
||||
}
|
||||
old.close().unwrap();
|
||||
Baseline { gate_values, up_values, q8, hidden_bytes, down_q8, down_bytes }
|
||||
}
|
||||
impl<'c> Packets<'c> {
|
||||
pub(super) fn new(c: &'c Context<'c>, foreign: &'c Context<'c>) -> Self {
|
||||
let a = upload(c, &vec![0xa5; PACKET_BYTES]);
|
||||
let b = upload(c, &vec![0x5a; PACKET_BYTES]);
|
||||
let d = upload(c, &vec![0x3c; PACKET_BYTES]);
|
||||
let short = upload(c, &[0x7e; 4]);
|
||||
let charge = c.allocated_bytes();
|
||||
let before = [download(&a, PACKET_BYTES), download(&b, PACKET_BYTES), download(&d, PACKET_BYTES)];
|
||||
let expected = before.clone();
|
||||
Self { c, foreign, a, b, d, short, charge, before, expected }
|
||||
}
|
||||
fn assert_expected(&self, label: &str) {
|
||||
for (packet, expected) in [&self.a, &self.b, &self.d].iter().zip(&self.expected) {
|
||||
same(&download(packet, PACKET_BYTES), expected, label);
|
||||
}
|
||||
}
|
||||
/// Record the `(column, packet, slot)` copies in the bytes every packet must now hold.
|
||||
fn record(&mut self, down_bytes: &[u8], copies: &[(usize, usize, usize)]) {
|
||||
for &(column, packet, slot) in copies {
|
||||
self.expected[packet][slot * 10240..(slot + 1) * 10240]
|
||||
.copy_from_slice(&down_bytes[column * 10240..(column + 1) * 10240]);
|
||||
}
|
||||
}
|
||||
/// All full packets, including untouched slots, compared after each bulk copy.
|
||||
fn bulk_copies(&mut self, actual: &FiniteMmqOutput<'_, '_>, down_bytes: &[u8]) {
|
||||
actual.copy_down_columns_to_slots(&mut [(0, &mut self.a, 1), (3, &mut self.b, 8)]).unwrap();
|
||||
self.record(down_bytes, &[(0, 0, 1), (3, 1, 8)]);
|
||||
self.assert_expected("two-packet copy+sentinels");
|
||||
actual
|
||||
.copy_down_columns_to_slots(&mut [(0, &mut self.a, 9), (1, &mut self.b, 0), (2, &mut self.d, 4)])
|
||||
.unwrap();
|
||||
self.record(down_bytes, &[(0, 0, 9), (1, 1, 0), (2, 2, 4)]);
|
||||
self.assert_expected("three-packet copy+sentinels");
|
||||
assert_eq!(self.c.allocated_bytes(), self.charge);
|
||||
}
|
||||
/// Late-invalid entries must refuse before the earlier valid entry is copied.
|
||||
fn refusals(&mut self, actual: &FiniteMmqOutput<'_, '_>) {
|
||||
for (columns, slots) in [
|
||||
([0, 0], [2, 2]),
|
||||
([1, 0], [2, 2]),
|
||||
([0, QT], [2, 2]),
|
||||
([0, u32::MAX], [2, 2]),
|
||||
([0, 1], [2, 10]),
|
||||
([0, 1], [2, u32::MAX]),
|
||||
] {
|
||||
let refused = actual.copy_down_columns_to_slots(&mut [
|
||||
(columns[0], &mut self.a, slots[0]),
|
||||
(columns[1], &mut self.b, slots[1]),
|
||||
]);
|
||||
assert!(matches!(refused, Err(Error::InvalidProjectionShape)));
|
||||
self.assert_expected("all-before-copy refusal");
|
||||
assert_eq!(self.c.allocated_bytes(), self.charge);
|
||||
}
|
||||
assert!(matches!(actual.copy_down_columns_to_slots(&mut []), Err(Error::InvalidProjectionShape)));
|
||||
assert!(matches!(
|
||||
actual.copy_down_columns_to_slots(&mut [(0, &mut self.a, 2), (1, &mut self.short, 1)]),
|
||||
Err(Error::InvalidProjectionShape)
|
||||
));
|
||||
same(&download(&self.short, 4), &[0x7e; 4], "short target unchanged");
|
||||
}
|
||||
fn alias_and_foreign(&mut self, actual: &FiniteMmqOutput<'_, '_>) {
|
||||
// RAII restoration runs on both the ordinary refusal and unwind path.
|
||||
let handle = self.a.handle;
|
||||
let mut alias = LivePacketAlias::new(&mut self.b, handle);
|
||||
let alias_result = actual.copy_down_columns_to_slots(&mut [(0, &mut self.a, 2), (1, alias.buffer(), 2)]);
|
||||
drop(alias);
|
||||
assert!(matches!(alias_result, Err(Error::OutputAliasesInput)));
|
||||
self.assert_expected("alias refusal and restored real owner");
|
||||
let mut foreign_packet = upload(self.foreign, &self.before[0]);
|
||||
assert!(matches!(
|
||||
actual.copy_down_columns_to_slots(&mut [(0, &mut self.a, 2), (1, &mut foreign_packet, 2)]),
|
||||
Err(Error::ContextMismatch)
|
||||
));
|
||||
same(&download(&foreign_packet, PACKET_BYTES), &self.before[0], "foreign destination unchanged");
|
||||
foreign_packet.release().unwrap();
|
||||
assert_eq!(self.foreign.allocated_bytes(), 0);
|
||||
self.assert_expected("foreign/short refusal no partial write");
|
||||
}
|
||||
fn late_nan_status(&self, actual: &mut FiniteMmqOutput<'_, '_>, down_bytes: &[u8]) {
|
||||
same(&download(actual.workspace.buffer(DOWN).unwrap(), DOWN_BYTES), down_bytes, "copy source immutable");
|
||||
assert!(!self.c.is_poisoned());
|
||||
assert_eq!(self.c.allocated_bytes(), self.charge);
|
||||
// Test-only actual status observation, not a forged finite producer: last
|
||||
// output word NaN must be visible after the queued compact witness completes.
|
||||
actual.workspace.buffers[DOWN]
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.copy_from_range(DOWN_BYTES - 4, &f32::NAN.to_le_bytes())
|
||||
.unwrap();
|
||||
assert!(matches!(actual.workspace.finite(DOWN, DOWN_BYTES / 4, false), Err(Error::NonFiniteMmqOutput("Down"))));
|
||||
actual.workspace.buffers[DOWN].as_mut().unwrap().copy_from_range(0, down_bytes).unwrap();
|
||||
actual.workspace.finite(DOWN, DOWN_BYTES / 4, false).unwrap();
|
||||
assert!(!self.c.is_poisoned());
|
||||
assert_eq!(self.c.allocated_bytes(), self.charge);
|
||||
}
|
||||
fn release(self) {
|
||||
self.short.release().unwrap();
|
||||
self.d.release().unwrap();
|
||||
self.b.release().unwrap();
|
||||
self.a.release().unwrap();
|
||||
}
|
||||
}
|
||||
pub(super) fn queued_chain(
|
||||
w: &mut MmqWorkspace<'_>,
|
||||
base: &Baseline,
|
||||
data: &[u8],
|
||||
(gu, down): (&MmqWeights<'_>, &MmqWeights<'_>),
|
||||
packets: Packets<'_>,
|
||||
) {
|
||||
let mut prepared = w.prepare_gate_up_chunk(&floats(data), QT).unwrap();
|
||||
same(&download(prepared.workspace.buffer(INPUT).unwrap(), data.len()), data, "queued source immutable");
|
||||
same(&download(prepared.workspace.buffer(CHUNK).unwrap(), base.q8.len()), &base.q8, "queued chunk Q8");
|
||||
let group = prepared.gather_gate_up(&[0, 1, 2, 3]).unwrap();
|
||||
same(&download(group.workspace.buffer(GROUP).unwrap(), base.q8.len()), &base.q8, "queued identity group Q8+tail");
|
||||
queued_hidden(group.gate_up(gu, gu).unwrap(), base, down, packets);
|
||||
}
|
||||
fn queued_hidden(actual_hidden: MmqHidden<'_, '_>, base: &Baseline, down: &MmqWeights<'_>, packets: Packets<'_>) {
|
||||
same(
|
||||
&download(actual_hidden.workspace.buffer(GATE).unwrap(), GU_BYTES),
|
||||
&encoded(&base.gate_values),
|
||||
"queued finite Gate",
|
||||
);
|
||||
same(
|
||||
&download(actual_hidden.workspace.buffer(UP).unwrap(), GU_BYTES),
|
||||
&encoded(&base.up_values),
|
||||
"queued finite Up",
|
||||
);
|
||||
same(
|
||||
&download(actual_hidden.workspace.buffer(HIDDEN).unwrap(), GU_BYTES),
|
||||
&base.hidden_bytes,
|
||||
"queued finite hidden",
|
||||
);
|
||||
let actual = actual_hidden.down(down).unwrap();
|
||||
queued_down(actual, base, packets);
|
||||
}
|
||||
fn queued_down(mut actual: FiniteMmqOutput<'_, '_>, base: &Baseline, mut packets: Packets<'_>) {
|
||||
let q8 = &base.down_q8;
|
||||
same(&download(actual.workspace.buffer(DOWN_Q8).unwrap(), q8.len()), q8, "queued Down Q8");
|
||||
same(&download(actual.workspace.buffer(DOWN).unwrap(), DOWN_BYTES), &base.down_bytes, "queued finite Down");
|
||||
assert_eq!(packets.c.allocated_bytes(), packets.charge);
|
||||
packets.bulk_copies(&actual, &base.down_bytes);
|
||||
packets.refusals(&actual);
|
||||
packets.alias_and_foreign(&actual);
|
||||
packets.late_nan_status(&mut actual, &base.down_bytes);
|
||||
packets.release();
|
||||
}
|
||||
pub(super) fn queued_close(
|
||||
c: &Context<'_>,
|
||||
foreign: Context<'_>,
|
||||
w: MmqWorkspace<'_>,
|
||||
(gu, down): (MmqWeights<'_>, MmqWeights<'_>),
|
||||
) {
|
||||
w.close().unwrap();
|
||||
down.close().unwrap();
|
||||
gu.close().unwrap();
|
||||
assert_eq!(c.allocated_bytes(), 0);
|
||||
assert_eq!(foreign.allocated_bytes(), 0);
|
||||
foreign.close().unwrap();
|
||||
}
|
||||
Reference in New Issue
Block a user