#[cfg(target_arch = "x86_64")]
#[test]
#[ignore = "Performance test - flaky under system load"]
fn test_imp_148a_simd_vs_scalar_speedup() {
if !is_x86_feature_detected!("avx2") {
println!("IMP-148a: Skipping - AVX2 not available");
return;
}
let num_bytes = 32768;
let bytes: Vec<u8> = (0..num_bytes).map(|i| (i % 256) as u8).collect();
let iterations = 1000;
let start = std::time::Instant::now();
let mut scalar_low = vec![0u8; num_bytes];
let mut scalar_high = vec![0u8; num_bytes];
for _ in 0..iterations {
for (i, &byte) in bytes.iter().enumerate() {
scalar_low[i] = byte & 0x0F;
scalar_high[i] = (byte >> 4) & 0x0F;
}
}
let scalar_time = start.elapsed();
#[target_feature(enable = "avx2")]
unsafe fn simd_extract_batch(bytes: &[u8], low: &mut [u8], high: &mut [u8]) {
use std::arch::x86_64::*;
let low_mask = _mm256_set1_epi8(0x0F);
for chunk_start in (0..bytes.len()).step_by(32) {
if chunk_start + 32 <= bytes.len() {
unsafe {
let bytes_vec =
_mm256_loadu_si256(bytes.as_ptr().add(chunk_start).cast::<__m256i>());
let low_vec = _mm256_and_si256(bytes_vec, low_mask);
let high_shifted = _mm256_srli_epi16(bytes_vec, 4);
let high_vec = _mm256_and_si256(high_shifted, low_mask);
_mm256_storeu_si256(
low.as_mut_ptr().add(chunk_start).cast::<__m256i>(),
low_vec,
);
_mm256_storeu_si256(
high.as_mut_ptr().add(chunk_start).cast::<__m256i>(),
high_vec,
);
}
}
}
}
let mut simd_low = vec![0u8; num_bytes];
let mut simd_high = vec![0u8; num_bytes];
let start = std::time::Instant::now();
for _ in 0..iterations {
unsafe {
simd_extract_batch(&bytes, &mut simd_low, &mut simd_high);
}
}
let simd_time = start.elapsed();
let speedup = scalar_time.as_secs_f64() / simd_time.as_secs_f64();
assert_eq!(
simd_low, scalar_low,
"IMP-148a: SIMD low should match scalar"
);
assert_eq!(
simd_high, scalar_high,
"IMP-148a: SIMD high should match scalar"
);
println!("\nIMP-148a: SIMD vs Scalar Nibble Extraction:");
println!(" Scalar: {:.2}ms", scalar_time.as_secs_f64() * 1000.0);
println!(" SIMD: {:.2}ms", simd_time.as_secs_f64() * 1000.0);
println!(" Speedup: {:.2}x", speedup);
assert!(
speedup > 1.5,
"IMP-148a: SIMD should be at least 1.5x faster, got {:.2}x",
speedup
);
}
#[test]
fn test_imp_148b_p1_throughput_improvement() {
let baseline_tps: f64 = 80.0;
let expected_improvement: f64 = 1.5;
let target_tps: f64 = baseline_tps * expected_improvement;
assert!(
(target_tps - 120.0).abs() < 1.0,
"IMP-148b: P1 target should be ~120 tok/s, got {:.1}",
target_tps
);
let llamacpp_tps: f64 = 256.0;
let gap_before: f64 = llamacpp_tps / baseline_tps;
let gap_after: f64 = llamacpp_tps / target_tps;
println!("\nIMP-148b: P1 Fix Impact Analysis:");
println!(
" Before P1: {:.1} tok/s ({:.1}x gap)",
baseline_tps, gap_before
);
println!(
" After P1: {:.1} tok/s ({:.1}x gap)",
target_tps, gap_after
);
println!(" Gap closed: {:.1}x -> {:.1}x", gap_before, gap_after);
assert!(
gap_after < gap_before,
"IMP-148b: Gap should decrease after P1 fix"
);
assert!(
gap_after < 2.5,
"IMP-148b: Gap after P1 should be < 2.5x, got {:.1}x",
gap_after
);
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_imp_148c_simd_scaling() {
if !is_x86_feature_detected!("avx2") {
println!("IMP-148c: Skipping - AVX2 not available");
return;
}
let sizes = [1024, 4096, 16384, 65536];
let mut speedups = Vec::new();
#[target_feature(enable = "avx2")]
unsafe fn simd_extract_148c(bytes: &[u8], low: &mut [u8], high: &mut [u8]) {
use std::arch::x86_64::*;
let mask = _mm256_set1_epi8(0x0F);
for i in (0..bytes.len()).step_by(32) {
if i + 32 <= bytes.len() {
unsafe {
let v = _mm256_loadu_si256(bytes.as_ptr().add(i).cast::<__m256i>());
let l = _mm256_and_si256(v, mask);
let h = _mm256_and_si256(_mm256_srli_epi16(v, 4), mask);
_mm256_storeu_si256(low.as_mut_ptr().add(i).cast::<__m256i>(), l);
_mm256_storeu_si256(high.as_mut_ptr().add(i).cast::<__m256i>(), h);
}
}
}
}
for &size in &sizes {
let bytes: Vec<u8> = (0..size).map(|i| (i % 256) as u8).collect();
let iterations = 100;
let start = std::time::Instant::now();
let mut low = vec![0u8; size];
let mut high = vec![0u8; size];
for _ in 0..iterations {
for (i, &byte) in bytes.iter().enumerate() {
low[i] = byte & 0x0F;
high[i] = (byte >> 4) & 0x0F;
}
}
let scalar_time = start.elapsed();
let start = std::time::Instant::now();
for _ in 0..iterations {
unsafe {
simd_extract_148c(&bytes, &mut low, &mut high);
}
}
let simd_time = start.elapsed();
let speedup = scalar_time.as_secs_f64() / simd_time.as_secs_f64();
speedups.push((size, speedup));
}
println!("\nIMP-148c: SIMD Scaling Analysis:");
for (size, speedup) in &speedups {
println!(" {} bytes: {:.2}x speedup", size, speedup);
}
let mut warnings = Vec::new();
for (size, speedup) in &speedups {
if *size >= 4096 {
let threshold = if *size >= 65536 { 0.1 } else { 0.3 };
if *speedup < threshold {
warnings.push(format!(
"IMP-148c: SIMD below threshold at {} bytes: {:.2}x (threshold {:.1}x)",
size, speedup, threshold
));
}
}
}
if !warnings.is_empty() {
for w in &warnings {
eprintln!("WARNING: {}", w);
}
let large_count = speedups.iter().filter(|(s, _)| *s >= 4096).count();
assert!(
warnings.len() < large_count,
"IMP-148c: ALL large buffer sizes regressed โ likely a real SIMD issue:\n{}",
warnings.join("\n")
);
}
}
#[test]
#[ignore]
fn test_imp_148d_q4k_dequant_efficiency() {
let num_super_blocks = 4;
let q4k_bytes = num_super_blocks * 144;
let mut q4k_data = vec![0u8; q4k_bytes];
for block in 0..num_super_blocks {
let offset = block * 144;
let d = (block as f32 + 1.0) * 0.1;
q4k_data[offset..offset + 2].copy_from_slice(&d.to_le_bytes()[0..2]);
for i in 12..144 {
q4k_data[offset + i] = ((block + i) % 256) as u8;
}
}
let iterations = 100;
let start = std::time::Instant::now();
for _ in 0..iterations {
let _ = dequantize_q4_k(&q4k_data);
}
let dequant_time = start.elapsed();
let throughput = (q4k_bytes * iterations) as f64 / dequant_time.as_secs_f64() / 1_000_000.0;
println!("\nIMP-148d: Q4_K Dequantization Performance:");
println!(
" Data size: {} bytes ({} super-blocks)",
q4k_bytes, num_super_blocks
);
println!(
" Time for {} iterations: {:.2}ms",
iterations,
dequant_time.as_secs_f64() * 1000.0
);
println!(" Throughput: {:.1} MB/s", throughput);
assert!(
throughput > 10.0,
"IMP-148d: Q4_K dequant should be > 10 MB/s, got {:.1}",
throughput
);
}
#[test]
fn test_imp_149a_simd_dispatch() {
let num_super_blocks = 2;
let q4k_bytes = num_super_blocks * 144;
let mut q4k_data = vec![0u8; q4k_bytes];
for block in 0..num_super_blocks {
let offset = block * 144;
let d: f32 = 0.1;
q4k_data[offset..offset + 2].copy_from_slice(&d.to_le_bytes()[0..2]);
}
let num_values = num_super_blocks * 256;
let activations: Vec<f32> = (0..num_values).map(|i| (i as f32) * 0.001).collect();
let scalar_result = fused_q4k_dot(&q4k_data, &activations);
let simd_result = fused_q4k_dot_simd(&q4k_data, &activations);
match (scalar_result, simd_result) {
(Ok(scalar), Ok(simd)) => {
let diff = (scalar - simd).abs();
let tolerance = 0.01 * scalar.abs().max(1.0);
assert!(
diff < tolerance,
"IMP-149a: SIMD and scalar should match. Scalar={}, SIMD={}, diff={}",
scalar,
simd,
diff
);
println!("\nIMP-149a: SIMD dispatch verified");
println!(" Scalar result: {}", scalar);
println!(" SIMD result: {}", simd);
println!(" Difference: {:.6}", diff);
},
(Err(e1), Err(e2)) => {
println!(
"IMP-149a: Both paths returned error (may be expected): {:?}, {:?}",
e1, e2
);
},
(Ok(_), Err(e)) => panic!("IMP-149a: SIMD failed but scalar succeeded: {:?}", e),
(Err(e), Ok(_)) => panic!("IMP-149a: Scalar failed but SIMD succeeded: {:?}", e),
}
}
#[test]
#[ignore = "Performance test - flaky under system load"]
fn test_imp_149b_fused_vs_separate_performance() {
let num_super_blocks = 16; let q4k_bytes = num_super_blocks * 144;
let mut q4k_data = vec![0u8; q4k_bytes];
for block in 0..num_super_blocks {
let offset = block * 144;
let d: f32 = 0.05 + (block as f32) * 0.001;
q4k_data[offset..offset + 2].copy_from_slice(&d.to_le_bytes()[0..2]);
for i in 12..144 {
q4k_data[offset + i] = ((block * 7 + i * 13) % 256) as u8;
}
}
let num_values = num_super_blocks * 256;
let activations: Vec<f32> = (0..num_values).map(|i| ((i % 100) as f32) * 0.01).collect();
let iterations = 100;
let start = std::time::Instant::now();
for _ in 0..iterations {
let dequant = dequantize_q4_k(&q4k_data).unwrap_or_default();
let _dot: f32 = dequant.iter().zip(&activations).map(|(a, b)| a * b).sum();
}
let separate_time = start.elapsed();
let start = std::time::Instant::now();
for _ in 0..iterations {
let _ = fused_q4k_dot_simd(&q4k_data, &activations);
}
let fused_time = start.elapsed();
let speedup = separate_time.as_secs_f64() / fused_time.as_secs_f64();
println!("\nIMP-149b: Fused vs Separate Performance:");
println!(
" Separate (dequant+dot): {:.2}ms",
separate_time.as_secs_f64() * 1000.0
);
println!(" Fused kernel: {:.2}ms", fused_time.as_secs_f64() * 1000.0);
println!(" Speedup: {:.2}x", speedup);
assert!(
speedup > 0.5, "IMP-149b: Fused kernel should not be >50% slower than separate, got {:.2}x",
speedup
);
}