#[test]
fn test_parity008c_popper_score_calculation() {
struct PopperScore {
prediction: String,
predicted: f64,
actual: f64,
score: f64,
}
impl PopperScore {
fn calculate(prediction: String, predicted: f64, actual: f64) -> Self {
let score = 1.0 - ((predicted - actual).abs() / predicted);
Self {
prediction,
predicted,
actual,
score,
}
}
}
let before = PopperScore::calculate(
"GPU 10x faster".to_string(),
10.0, 2.5, );
let after = PopperScore::calculate(
"GPU 10x faster".to_string(),
10.0, 9.5, );
assert!(
before.score < after.score,
"PARITY-008c: Score should improve when actual approaches predicted"
);
let perfect = PopperScore::calculate("Test".to_string(), 5.0, 5.0);
assert!(
(perfect.score - 1.0).abs() < 0.01,
"PARITY-008c: Perfect match should give score ~1.0"
);
}
#[test]
fn test_parity008d_explicit_thresholds() {
struct AcceptanceThreshold {
metric: String,
minimum: f64,
target: f64,
stretch: f64,
}
let throughput_threshold = AcceptanceThreshold {
metric: "tok/s".to_string(),
minimum: 64.0, target: 225.0, stretch: 300.0, };
let latency_threshold = AcceptanceThreshold {
metric: "ms/token".to_string(),
minimum: 50.0, target: 4.4, stretch: 3.3, };
assert!(
throughput_threshold.minimum < throughput_threshold.target,
"PARITY-008d: Minimum should be less than target"
);
assert!(
throughput_threshold.target < throughput_threshold.stretch,
"PARITY-008d: Target should be less than stretch"
);
assert!(
latency_threshold.minimum > latency_threshold.target,
"PARITY-008d: Latency minimum should be higher than target (lower is better)"
);
}
#[test]
fn test_parity008e_benchmark_reproducibility() {
use std::time::Instant;
let config = GGUFConfig {
architecture: "test".to_string(),
constraints: crate::gguf::ArchConstraints::from_architecture("test"),
hidden_dim: 64,
intermediate_dim: 128,
num_layers: 2,
num_heads: 4,
num_kv_heads: 4,
vocab_size: 100,
context_length: 64,
rope_theta: 10000.0,
eps: 1e-5,
rope_type: 0,
explicit_head_dim: None,
query_pre_attn_scalar: None,
bos_token_id: None,
eos_token_id: None,
};
let model = create_test_model_with_config(&config);
let gen_config = QuantizedGenerateConfig {
max_tokens: 10,
temperature: 0.0,
top_k: 1,
stop_tokens: vec![],
trace: false,
..Default::default()
};
let prompt = vec![1u32, 2, 3];
let start1 = Instant::now();
let result1 = model.generate_with_cache(&prompt, &gen_config).expect("test value should be present");
let _time1 = start1.elapsed();
let start2 = Instant::now();
let result2 = model.generate_with_cache(&prompt, &gen_config).expect("test value should be present");
let _time2 = start2.elapsed();
assert_eq!(
result1, result2,
"PARITY-008e: Deterministic sampling should produce identical outputs"
);
}
#[test]
fn test_parity008f_measurement_validation() {
struct Measurement {
value: f64,
unit: String,
min_valid: f64,
max_valid: f64,
}
impl Measurement {
fn is_valid(&self) -> bool {
self.value >= self.min_valid && self.value <= self.max_valid
}
}
let throughput = Measurement {
value: 64.0,
unit: "tok/s".to_string(),
min_valid: 0.1,
max_valid: 10000.0,
};
let latency = Measurement {
value: 15.6,
unit: "ms".to_string(),
min_valid: 0.001,
max_valid: 10000.0,
};
assert!(
throughput.is_valid(),
"PARITY-008f: Throughput should be valid"
);
assert!(latency.is_valid(), "PARITY-008f: Latency should be valid");
let invalid = Measurement {
value: -5.0,
unit: "tok/s".to_string(),
min_valid: 0.0,
max_valid: 10000.0,
};
assert!(
!invalid.is_valid(),
"PARITY-008f: Negative throughput should be invalid"
);
}
#[test]
fn test_parity009a_cv_stopping_criterion() {
#[derive(Debug)]
struct CVStoppingBenchmark {
target_cv: f64,
max_iterations: usize,
min_iterations: usize,
}
impl CVStoppingBenchmark {
fn new() -> Self {
Self {
target_cv: 0.05, max_iterations: 100,
min_iterations: 5,
}
}
fn calculate_cv(values: &[f64]) -> f64 {
if values.len() < 2 {
return 1.0; }
let mean: f64 = values.iter().sum::<f64>() / values.len() as f64;
if mean == 0.0 {
return 0.0;
}
let variance: f64 =
values.iter().map(|v| (v - mean).powi(2)).sum::<f64>() / values.len() as f64;
variance.sqrt() / mean
}
fn should_stop(&self, values: &[f64]) -> (bool, f64) {
if values.len() < self.min_iterations {
return (false, 1.0);
}
if values.len() >= self.max_iterations {
return (true, Self::calculate_cv(values));
}
let cv = Self::calculate_cv(values);
(cv < self.target_cv, cv)
}
fn run<F>(&self, mut benchmark_fn: F) -> (Vec<f64>, usize, f64)
where
F: FnMut() -> f64,
{
let mut values = Vec::new();
loop {
values.push(benchmark_fn());
let (stop, cv) = self.should_stop(&values);
if stop {
let len = values.len();
return (values, len, cv);
}
}
}
}
let runner = CVStoppingBenchmark::new();
let mut counter = 0;
let (_values, iterations, cv) = runner.run(|| {
counter += 1;
100.0 + (counter as f64 * 0.01) });
println!("\nPARITY-009a: CV-based stopping");
println!(" Iterations: {}", iterations);
println!(" Final CV: {:.4}", cv);
println!(" Target CV: {:.4}", runner.target_cv);
assert!(
cv < runner.target_cv,
"QA-031: CV should be below threshold"
);
assert!(
iterations >= runner.min_iterations,
"QA-031: Should run minimum iterations"
);
assert!(
iterations <= runner.max_iterations,
"QA-031: Should not exceed max iterations"
);
}
#[test]
fn test_parity009b_warmup_discard() {
#[derive(Debug)]
struct WarmupBenchmark {
warmup_iterations: usize,
measurement_iterations: usize,
}
impl WarmupBenchmark {
fn new(warmup: usize, measure: usize) -> Self {
Self {
warmup_iterations: warmup,
measurement_iterations: measure,
}
}
fn run<F>(&self, mut benchmark_fn: F) -> (Vec<f64>, Vec<f64>)
where
F: FnMut(usize) -> f64,
{
let mut warmup_values = Vec::with_capacity(self.warmup_iterations);
let mut measurement_values = Vec::with_capacity(self.measurement_iterations);
for i in 0..self.warmup_iterations {
warmup_values.push(benchmark_fn(i));
}
for i in 0..self.measurement_iterations {
measurement_values.push(benchmark_fn(self.warmup_iterations + i));
}
(warmup_values, measurement_values)
}
}
let runner = WarmupBenchmark::new(3, 5);
let (warmup, measurements) = runner.run(|i| {
if i < 3 {
200.0 - (i as f64 * 30.0) } else {
100.0 + (i as f64 * 0.5) }
});
let warmup_mean: f64 = warmup.iter().sum::<f64>() / warmup.len() as f64;
let measure_mean: f64 = measurements.iter().sum::<f64>() / measurements.len() as f64;
println!("\nPARITY-009b: Warmup discard");
println!(
" Warmup iterations: {} (mean: {:.1})",
warmup.len(),
warmup_mean
);
println!(
" Measurement iterations: {} (mean: {:.1})",
measurements.len(),
measure_mean
);
assert_eq!(warmup.len(), 3, "QA-032: Should have 3 warmup iterations");
assert_eq!(
measurements.len(),
5,
"QA-032: Should have 5 measurement iterations"
);
assert!(
warmup_mean > measure_mean,
"QA-032: Warmup should be slower (JIT effect)"
);
}
#[test]
fn test_parity009c_environment_metadata() {
#[derive(Debug, Clone)]
struct EnvironmentMetadata {
os: String,
arch: String,
cpu_model: String,
cpu_cores: usize,
ram_gb: usize,
rust_version: String,
cargo_profile: String,
target_triple: String,
timestamp: String,
git_commit: String,
benchmark_version: String,
}
impl EnvironmentMetadata {
fn capture() -> Self {
Self {
os: std::env::consts::OS.to_string(),
arch: std::env::consts::ARCH.to_string(),
cpu_model: "Unknown".to_string(), cpu_cores: std::thread::available_parallelism()
.map(std::num::NonZero::get)
.unwrap_or(1),
ram_gb: 16, rust_version: env!("CARGO_PKG_RUST_VERSION").to_string(),
cargo_profile: if cfg!(debug_assertions) {
"debug"
} else {
"release"
}
.to_string(),
target_triple: std::env::consts::ARCH.to_string(),
timestamp: "2025-12-13T22:00:00Z".to_string(),
git_commit: "abc123".to_string(),
benchmark_version: "1.0.0".to_string(),
}
}
fn is_reproducible(&self) -> bool {
!self.os.is_empty()
&& !self.arch.is_empty()
&& self.cpu_cores > 0
&& !self.cargo_profile.is_empty()
}
}
let env = EnvironmentMetadata::capture();
println!("\nPARITY-009c: Environment metadata");
println!(" OS: {}", env.os);
println!(" Arch: {}", env.arch);
println!(" CPU cores: {}", env.cpu_cores);
println!(" Profile: {}", env.cargo_profile);
assert!(
env.is_reproducible(),
"QA-033: Environment must be reproducible"
);
assert!(!env.os.is_empty(), "QA-033: OS must be captured");
assert!(!env.arch.is_empty(), "QA-033: Arch must be captured");
assert!(env.cpu_cores > 0, "QA-033: CPU cores must be captured");
}