Files
leleka/crates/cuda-driver/src/mmq_numeric_gpu.rs
T
Pavlo RatushnyiandClaude Sonnet 5.5 b686f6ab75 STRA-338: rename the 854 mechanically named modules (impl_*, *_fn, *_<N>) after what they hold; mechanical_names census 854 -> 0
git mv of the files and of the module directories next to them, mod declarations, #[path] attributes and
the paths that name the modules, rewritten by scripts/dev/rename_mechanical.py from a reviewed name table.
Two files lost one to two lines so the 400-line limit holds again (qsa_prompt_index, original_verify_expert_probe).
limits-baseline.json: test fn_lines 0 -> 1 is --allow-increase for crates/deploy builtin_profiles.rs:149 (37 lines,
the speed-vs-strata session's file, not touched here); the prod params census falls 108 -> 1 (w_launch, extern C ABI).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-08 13:43:56 +01:00

192 lines
6.7 KiB
Rust

//! Ignored original-first MMQ proof. References are produced and pinned by ROOT.
use super::*;
use std::collections::BTreeMap;
use std::fs::OpenOptions;
use std::io::Read;
use std::os::unix::fs::{MetadataExt, OpenOptionsExt};
use std::path::{Component, Path, PathBuf};
const SOURCE_SHA: &str = "a0134e0ac281d743089c54b09a3c221e645cc09a0a7a19041822230c3951ed7d";
const UPSTREAM: &str = "3cf03257f219afbe7334045ff7c6a06ac68c627d";
const CASE_FILES: [&str; 7] = [
"weights.bin",
"input.f32",
"reference_q8.bin",
"reference_streamk.f32",
"reference_fixed_j64_streamk.f32",
"reference_tiling.f32",
"metadata.json",
];
const BUDGET: usize = 32 * 1024 * 1024;
struct Pin {
bytes: usize,
sha: String,
}
struct Receipt {
root: PathBuf,
files: BTreeMap<String, Pin>,
}
struct Case {
tokens: u32,
input: Vec<u8>,
q8: Vec<u8>,
adaptive: Vec<u8>,
fixed: Vec<u8>,
metadata: String,
}
/// Words, packed bytes and launches covered so far.
#[derive(Default)]
struct Totals {
words: usize,
packed_bytes: usize,
launches: usize,
}
/// One format's resident weights, the pristine bytes read back from them, and the shared context.
struct Rig<'a, 'c> {
context: &'a Context<'c>,
experiment: &'a MmqExperiment<'c>,
weights: &'a MmqWeights<'c>,
before: &'a [u8],
}
impl Rig<'_, '_> {
fn weights_unchanged(&self) {
let mut after = vec![0; self.before.len()];
self.weights.buffer.as_ref().unwrap().copy_to(&mut after).unwrap();
assert_eq!(after, self.before);
}
fn profile(
&self,
case: &Case,
(input, output): (&DeviceBuffer<'_>, &mut DeviceBuffer<'_>),
(profile, reference): (MmqProfile, &[u8]),
totals: &mut Totals,
) {
output.copy_from(&vec![0xff; reference.len()]).unwrap();
let charge = self.context.allocated_bytes();
let (values, q8) = self
.experiment
.project_observed(MmqRequest { profile, tokens: case.tokens }, self.weights, input, output)
.unwrap();
assert_eq!(&*q8, case.q8.as_slice(), "complete original D4Q8 bytes");
compare(&values, reference, "MMQ exact original profile");
let mut actual = vec![0; reference.len()];
output.copy_to(&mut actual).unwrap();
assert_eq!(&actual, reference);
assert_eq!(self.context.allocated_bytes(), charge, "all projection scratch freed");
let mut source_after = vec![0; case.input.len()];
input.copy_to(&mut source_after).unwrap();
assert_eq!(source_after, case.input);
self.weights_unchanged();
totals.packed_bytes += q8.len();
totals.words += values.len();
totals.launches += 1;
}
fn case(&self, case: &Case, totals: &mut Totals) {
assert_eq!(integer(&case.metadata, "nsm"), self.experiment.sms as usize);
let mut input = self.context.allocate(case.input.len()).unwrap();
input.copy_from(&case.input).unwrap();
let mut output = self.context.allocate(case.adaptive.len()).unwrap();
for (profile, reference) in
[(MmqProfile::OriginalAdaptive, &case.adaptive), (MmqProfile::OriginalStreamK, &case.fixed)]
{
if profile == MmqProfile::OriginalStreamK && case.tokens == 64 {
continue;
}
self.profile(case, (&input, &mut output), (profile, reference), totals);
}
let mut actual = vec![0; case.input.len()];
input.copy_to(&mut actual).unwrap();
assert_eq!(actual, case.input);
self.weights_unchanged();
output.release().unwrap();
input.release().unwrap();
}
}
fn run_format(
context: &Context<'_>,
experiment: &MmqExperiment<'_>,
format: MmqFormat,
(raw, cases): (&[u8], &[Case]),
totals: &mut Totals,
) {
let weights = experiment.weights(format, raw).unwrap();
let wb = weights.buffer.as_ref().unwrap();
let mut before = vec![0; wb.bytes];
wb.copy_to(&mut before).unwrap();
assert_eq!(&before[..raw.len()], raw);
assert!(before[raw.len()..].iter().all(|b| *b == 0));
let rig = Rig { context, experiment, weights: &weights, before: &before };
for case in cases {
rig.case(case, totals);
}
weights.close().unwrap();
assert_eq!(context.allocated_bytes(), 0);
}
/// Each format's raw weights and its three token-count cases.
fn format_inputs(receipt: &Receipt) -> Vec<(MmqFormat, (Vec<u8>, Vec<Case>))> {
[
(18, MmqFormat::Iq3XxsGateUp, 640, 2560, 627200, 0),
(22, MmqFormat::Iq2SGateUp, 640, 2560, 524800, 0),
(20, MmqFormat::Iq4NlDown, 2560, 640, 921600, 216),
(42, MmqFormat::Q2Down, 2560, 640, 460800, 108),
]
.map(|(kind, format, rows, columns, raw, tail)| (format, cases(receipt, (kind, rows, columns, raw, tail))))
.into()
}
#[test]
#[ignore = "ROOT-only original-first pinned reference; actual CUDA, never automatic suite"]
fn original_first_mmq_adaptive_and_distinct_fixed_profiles_exact() {
assert_eq!(std::env::var("LELEKA_STRA169_RUN_MMQ_NUMERIC").as_deref(), Ok("yes"), "explicit GPU opt-in");
let root = PathBuf::from(std::env::var_os("LELEKA_STRA169_MMQ_REFERENCE_DIR").expect("ROOT reference directory"));
let pin = std::env::var("LELEKA_STRA169_MMQ_REFERENCE_MANIFEST_SHA").expect("ROOT reference pin");
let receipt = Receipt::load(root, &pin);
let inputs = format_inputs(&receipt);
let driver = Driver::load().unwrap();
let context = driver.device(0).unwrap().create_context(BUDGET).unwrap();
let experiment = context.mmq_experiment().unwrap();
assert!((1..=256).contains(&experiment.sms), "closed fixture SM budget");
let mut totals = Totals::default();
for (format, (raw, cases)) in &inputs {
run_format(&context, &experiment, *format, (raw.as_slice(), cases.as_slice()), &mut totals);
}
experiment.close().unwrap();
assert_eq!(context.allocated_bytes(), 0);
assert!(!context.is_poisoned());
context.close().unwrap();
let Totals { words, packed_bytes, launches } = totals;
assert_eq!((launches, words, packed_bytes), (20, 665600, 1022976));
println!(
"MMQ original-first exact: 12adaptive+8distinct-fixed profiles, {words}F32words/{packed_bytes}Q8bytes, \
allocated0"
);
}
#[path = "mmq_numeric_gpu/digest.rs"]
mod digest;
#[allow(unused_imports)]
use digest::compare;
#[allow(unused_imports)]
use digest::digest;
#[allow(unused_imports)]
use digest::field;
#[allow(unused_imports)]
use digest::finite;
#[allow(unused_imports)]
use digest::hex;
#[allow(unused_imports)]
use digest::integer;
#[allow(unused_imports)]
use digest::read_regular;
#[path = "mmq_numeric_gpu/format_cases.rs"]
mod format_cases;
#[allow(unused_imports)]
use format_cases::cases;