#[test]
fn test_qa_050_documentation_support() {
let hardware = HardwareSpec::default();
let mut matrix = BenchmarkMatrix::new("test-model", hardware);
matrix.add_entry(MatrixBenchmarkEntry::from_samples(
RuntimeType::Realizar,
ComputeBackendType::Cpu,
"test",
&[100.0],
&[50.0],
90.0,
));
let markdown = matrix.to_markdown_table();
assert!(
markdown.contains("|"),
"QA-050: Should produce markdown table"
);
assert!(
markdown.contains("Runtime") || markdown.contains("runtime"),
"QA-050: Table should have headers"
);
}
#[test]
fn test_imp800a_gpu_parity_benchmark_config() {
let config = GpuParityBenchmark::new("/path/to/model.gguf")
.with_prompt("Hello world")
.with_max_tokens(64)
.with_ollama_endpoint("http://localhost:11434")
.with_warmup(5)
.with_iterations(20);
assert_eq!(config.model_path, "/path/to/model.gguf");
assert_eq!(config.prompt, "Hello world");
assert_eq!(config.max_tokens, 64);
assert_eq!(config.ollama_endpoint, "http://localhost:11434");
assert_eq!(config.warmup_iterations, 5);
assert_eq!(config.measurement_iterations, 20);
}
#[test]
fn test_imp800a_gpu_parity_benchmark_default() {
let config = GpuParityBenchmark::default();
assert!(config.model_path.is_empty());
assert_eq!(config.prompt, "The quick brown fox");
assert_eq!(config.max_tokens, 32);
assert_eq!(config.warmup_iterations, 3);
assert_eq!(config.measurement_iterations, 10);
assert!((config.target_cv - 0.05).abs() < f64::EPSILON);
}
#[test]
fn test_imp800b_gpu_parity_result_struct() {
let result = GpuParityResult::new(150.0, 240.0, 0.03, "NVIDIA RTX 4090", 8192);
assert!((result.realizar_gpu_tps - 150.0).abs() < f64::EPSILON);
assert!((result.ollama_tps - 240.0).abs() < f64::EPSILON);
assert!((result.gap_ratio - 1.6).abs() < 0.01);
assert!((result.cv - 0.03).abs() < f64::EPSILON);
assert_eq!(result.gpu_device, "NVIDIA RTX 4090");
assert_eq!(result.vram_mb, 8192);
}
#[test]
fn test_imp800b_parity_thresholds() {
let m2_pass = GpuParityResult::new(130.0, 240.0, 0.03, "GPU", 8192);
assert!(m2_pass.achieves_m2_parity()); assert!(!m2_pass.achieves_m4_parity());
let m4_pass = GpuParityResult::new(200.0, 240.0, 0.02, "GPU", 8192);
assert!(m4_pass.achieves_m2_parity()); assert!(m4_pass.achieves_m4_parity());
let fail = GpuParityResult::new(50.0, 240.0, 0.05, "GPU", 8192);
assert!(!fail.achieves_m2_parity()); assert!(!fail.achieves_m4_parity());
}
#[test]
fn test_imp800b_cv_stability() {
let stable = GpuParityResult::new(150.0, 240.0, 0.04, "GPU", 8192);
assert!(stable.measurements_stable());
let unstable = GpuParityResult::new(150.0, 240.0, 0.08, "GPU", 8192);
assert!(!unstable.measurements_stable()); }
#[test]
fn test_imp800c_gap_analysis_struct() {
let analysis = GapAnalysis::new(2.0, 1.8).with_statistics(0.01, 1.5, 2.1);
assert!((analysis.claimed_gap - 2.0).abs() < f64::EPSILON);
assert!((analysis.measured_gap - 1.8).abs() < f64::EPSILON);
assert!((analysis.p_value - 0.01).abs() < f64::EPSILON);
assert!((analysis.ci_95_lower - 1.5).abs() < f64::EPSILON);
assert!((analysis.ci_95_upper - 2.1).abs() < f64::EPSILON);
}
#[test]
fn test_imp800c_claim_verification() {
let within_ci = GapAnalysis::new(2.0, 1.8).with_statistics(0.01, 1.5, 2.1);
assert!(within_ci.claim_verified());
let outside_ci = GapAnalysis::new(2.0, 1.2).with_statistics(0.01, 1.5, 2.1);
assert!(!outside_ci.claim_verified()); }
#[test]
fn test_imp800c_statistical_bounds() {
let analysis = GapAnalysis::new(2.0, 1.8).with_statistics(0.05, 1.6, 2.0);
assert!((analysis.ci_95_lower - 1.6).abs() < f64::EPSILON);
assert!((analysis.ci_95_upper - 2.0).abs() < f64::EPSILON);
assert!((analysis.p_value - 0.05).abs() < f64::EPSILON);
}
#[test]
fn test_imp800c_popper_score() {
let analysis = GapAnalysis::new(2.0, 1.6).with_default_claims(150.0);
assert!((analysis.popper_score - 75.0).abs() < f64::EPSILON);
assert_eq!(analysis.claims.len(), 4);
}
#[test]
fn test_imp800d_falsifiable_claim() {
let claim = FalsifiableClaim::new("TEST-001", "Test claim", 5.0, 25.0).evaluate(30.0);
assert_eq!(claim.id, "TEST-001");
assert_eq!(claim.description, "Test claim");
assert!((claim.expected - 5.0).abs() < f64::EPSILON);
assert!((claim.threshold - 25.0).abs() < f64::EPSILON);
assert!((claim.measured - 30.0).abs() < f64::EPSILON);
assert!(claim.verified);
let failed_claim =
FalsifiableClaim::new("TEST-002", "Failing claim", 5.0, 50.0).evaluate(30.0);
assert!(!failed_claim.verified); }
#[test]
fn test_imp800d_gpu_faster_than_cpu() {
let faster = GpuParityResult::new(30.0, 240.0, 0.03, "GPU", 8192);
assert!(faster.gpu_faster_than_cpu()); assert!((faster.cpu_speedup() - 6.0).abs() < f64::EPSILON);
let slower = GpuParityResult::new(4.0, 240.0, 0.03, "GPU", 8192);
assert!(!slower.gpu_faster_than_cpu()); }
#[test]
fn test_imp900a_optimized_gemm_config_default() {
let config = OptimizedGemmConfig::default();
assert_eq!(config.tile_size, 32);
assert_eq!(config.reg_block, 4);
assert!(!config.use_tensor_cores);
assert_eq!(config.vector_width, 4);
assert_eq!(config.k_unroll, 4);
assert!(config.double_buffer);
}
#[test]
fn test_imp900a_shared_memory_calculation() {
let config = OptimizedGemmConfig::default();
assert_eq!(config.shared_memory_bytes(), 32 * 32 * 4 * 4);
let no_double = OptimizedGemmConfig {
double_buffer: false,
..Default::default()
};
assert_eq!(no_double.shared_memory_bytes(), 32 * 32 * 4 * 2);
}
#[test]
fn test_imp900a_threads_per_block() {
let config = OptimizedGemmConfig::default();
assert_eq!(config.threads_per_block(), 64);
let large = OptimizedGemmConfig::large();
assert_eq!(large.threads_per_block(), 64);
}
#[test]
fn test_imp900a_registers_per_thread() {
let config = OptimizedGemmConfig::default();
assert_eq!(config.registers_per_thread(), 16);
let large = OptimizedGemmConfig::large();
assert_eq!(large.registers_per_thread(), 64);
}
#[test]
fn test_imp900a_gemm_performance_result() {
let result = GemmPerformanceResult::new(1024, 1024, 1024, 1.54);
assert!((result.gflops - 1394.5).abs() < 10.0);
let with_peak = result.with_peak(82000.0);
assert!(with_peak.efficiency < 2.0); }
#[test]
fn test_imp900a_performance_improvement_check() {
let result = GemmPerformanceResult::new(1024, 1024, 1024, 0.70);
let baseline_gflops = 1396.0;
assert!(result.improved_by(baseline_gflops, 2.0));
assert!(!result.improved_by(baseline_gflops, 3.0)); }
#[test]
fn test_imp900a_expected_improvement() {
let benchmark = OptimizedGemmBenchmark::default();
let expected = benchmark.expected_improvement();
assert!((expected - 4.68).abs() < 0.1);
}
#[test]
fn test_imp900b_fused_op_spec() {
let spec = FusedOpSpec {
op_type: FusedOpType::GemmBiasActivation,
input_dims: vec![256, 2560],
output_dims: vec![256, 10240],
activation: Some("gelu".to_string()),
fused_launches: 1,
unfused_launches: 3,
};
assert_eq!(spec.launch_reduction(), 3.0);
assert!(spec.achieves_target_reduction()); }
#[test]
fn test_imp900b_launch_reduction_targets() {
let good = FusedOpSpec {
op_type: FusedOpType::FusedAttention,
input_dims: vec![1, 32, 512, 80],
output_dims: vec![1, 32, 512, 80],
activation: None,
fused_launches: 1,
unfused_launches: 4,
};
assert!(good.achieves_target_reduction());
let marginal = FusedOpSpec {
op_type: FusedOpType::LayerNormLinear,
input_dims: vec![256, 2560],
output_dims: vec![256, 2560],
activation: None,
fused_launches: 1,
unfused_launches: 2,
};
assert!(marginal.achieves_target_reduction());
let poor = FusedOpSpec {
op_type: FusedOpType::FusedFfn,
input_dims: vec![256, 2560],
output_dims: vec![256, 2560],
activation: None,
fused_launches: 2,
unfused_launches: 3,
};
assert!(!poor.achieves_target_reduction());
}
#[test]
fn test_imp900c_flash_attention_phi2_config() {
let config = FlashAttentionConfig::phi2();
assert_eq!(config.head_dim, 80);
assert_eq!(config.num_heads, 32);
assert!(config.causal);
assert!((config.scale - 0.1118).abs() < 0.001);
}
#[test]
fn test_imp900c_memory_comparison() {
let config = FlashAttentionConfig::phi2();
let (naive_512, flash_512) = config.memory_comparison(512);
assert_eq!(naive_512, 512 * 512 * 4); assert_eq!(flash_512, 64 * 64 * 4 * 2);
let (naive_2048, flash_2048) = config.memory_comparison(2048);
assert_eq!(naive_2048, 2048 * 2048 * 4); assert_eq!(flash_2048, 64 * 64 * 4 * 2); }
#[test]
fn test_imp900c_memory_savings() {
let config = FlashAttentionConfig::phi2();
let savings_512 = config.memory_savings(512);
assert!((savings_512 - 32.0).abs() < 1.0);
let savings_2048 = config.memory_savings(2048);
assert!((savings_2048 - 512.0).abs() < 10.0);
}
#[test]
fn test_imp900d_memory_pool_default() {
let config = MemoryPoolConfig::default();
assert_eq!(config.initial_size, 256 * 1024 * 1024); assert_eq!(config.max_size, 2 * 1024 * 1024 * 1024); assert!(config.use_pinned_memory);
assert!(config.async_transfers);
assert_eq!(config.size_classes.len(), 9);
}
#[test]
fn test_imp900d_size_class_lookup() {
let config = MemoryPoolConfig::default();
assert_eq!(config.find_size_class(1024), Some(4096));
assert_eq!(config.find_size_class(500_000), Some(1048576));
assert_eq!(config.find_size_class(200_000_000), Some(268435456));
assert_eq!(config.find_size_class(500_000_000), None);
}
#[test]
fn test_imp900d_bandwidth_improvement() {
let pinned = MemoryPoolConfig::default();
assert!((pinned.expected_bandwidth_improvement() - 2.4).abs() < 0.1);
let unpinned = MemoryPoolConfig {
use_pinned_memory: false,
..Default::default()
};
assert!((unpinned.expected_bandwidth_improvement() - 1.0).abs() < f64::EPSILON);
}
#[test]
fn test_imp900_combined_result_baseline() {
let result = Imp900Result::from_baseline(13.1);
assert!((result.baseline_tps - 13.1).abs() < 0.1);
assert!((result.optimized_tps - 13.1).abs() < 0.1);
assert!((result.gap_ratio - 18.32).abs() < 0.1); assert!(result.milestone.is_none()); }
#[test]
fn test_imp900_individual_optimizations() {
let result = Imp900Result::from_baseline(13.1).with_gemm_improvement(2.5);
assert!((result.optimized_tps - 32.75).abs() < 0.1); assert!((result.gap_ratio - 7.33).abs() < 0.1);
assert!(result.milestone.is_none());
}