#[test]
fn test_fused_swiglu_simd_size_7() {
let mut gate: Vec<f32> = (0..7).map(|i| i as f32 * 0.5).collect();
let up = vec![1.0f32; 7];
fused_swiglu_simd(&mut gate, &up);
assert!((gate[0] - 0.0).abs() < 1e-5);
}
#[test]
fn test_fused_swiglu_simd_size_15() {
let mut gate: Vec<f32> = (0..15).map(|i| (i as f32 - 7.0) * 0.2).collect();
let up = vec![1.0f32; 15];
fused_swiglu_simd(&mut gate, &up);
for g in &gate {
assert!(g.is_finite());
}
}
#[test]
fn test_fused_swiglu_simd_size_64() {
let mut gate: Vec<f32> = (0..64).map(|i| (i as f32 - 32.0) * 0.05).collect();
let up = vec![1.0f32; 64];
fused_swiglu_simd(&mut gate, &up);
assert!(gate.iter().all(|g| g.is_finite()));
}
#[test]
fn test_softmax_simd_size_7() {
let mut x: Vec<f32> = (0..7).map(|i| i as f32).collect();
softmax_simd(&mut x);
let sum: f32 = x.iter().sum();
assert!((sum - 1.0).abs() < 1e-5);
}
#[test]
fn test_softmax_simd_size_15() {
let mut x: Vec<f32> = (0..15).map(|i| i as f32 * 0.1).collect();
softmax_simd(&mut x);
let sum: f32 = x.iter().sum();
assert!((sum - 1.0).abs() < 1e-5);
}
#[test]
fn test_softmax_simd_size_64() {
let mut x: Vec<f32> = (0..64).map(|i| (i as f32 - 32.0) * 0.1).collect();
softmax_simd(&mut x);
let sum: f32 = x.iter().sum();
assert!((sum - 1.0).abs() < 1e-5);
}
#[test]
fn test_softmax_simd_size_100() {
let mut x: Vec<f32> = (0..100).map(|i| i as f32 * 0.01).collect();
softmax_simd(&mut x);
let sum: f32 = x.iter().sum();
assert!((sum - 1.0).abs() < 1e-5);
}
#[test]
fn test_quantize_activations_q8_0_size_1() {
let activations = vec![42.0f32];
let (scales, quants) = quantize_activations_q8_0(&activations);
assert_eq!(scales.len(), 1);
assert_eq!(quants.len(), 32);
assert_eq!(quants[0], 127);
for q in &quants[1..32] {
assert_eq!(*q, 0i8);
}
}
#[test]
fn test_quantize_activations_q8_0_symmetric() {
let activations = vec![-10.0, 0.0, 10.0];
let (_scales, quants) = quantize_activations_q8_0(&activations);
assert_eq!(quants[0], -127);
assert_eq!(quants[1], 0);
assert_eq!(quants[2], 127);
}
#[test]
fn test_quantize_activations_q8_0_near_zero_max() {
let activations = vec![1e-12f32; 10];
let (scales, _quants) = quantize_activations_q8_0(&activations);
assert!((scales[0] - 1.0 / 127.0).abs() < 1e-10);
}
#[test]
fn test_quantize_activations_q8_0_exact_block() {
let activations: Vec<f32> = (0..32).map(|i| i as f32).collect();
let (scales, quants) = quantize_activations_q8_0(&activations);
assert_eq!(scales.len(), 1);
assert_eq!(quants.len(), 32);
assert_eq!(quants[31], 127);
}
#[test]
fn test_quantize_activations_q8_0_multi_block() {
let activations: Vec<f32> = (0..100).map(|i| (i as f32 - 50.0) * 0.5).collect();
let (scales, quants) = quantize_activations_q8_0(&activations);
assert_eq!(scales.len(), 4);
assert_eq!(quants.len(), 128);
}
#[test]
fn test_fused_rmsnorm_q4_0_matmul_input_dim_mismatch() {
let input = vec![1.0f32; 16]; let norm_weight = vec![1.0f32; 32];
let weight_data = vec![0u8; 18];
let result = fused_rmsnorm_q4_0_matmul(&input, &norm_weight, 1e-5, &weight_data, 32, 1);
assert!(result.is_err());
}
#[test]
fn test_fused_rmsnorm_q4_0_matmul_weight_too_small() {
let input = vec![1.0f32; 32];
let norm_weight = vec![1.0f32; 32];
let weight_data = vec![0u8; 10];
let result = fused_rmsnorm_q4_0_matmul(&input, &norm_weight, 1e-5, &weight_data, 32, 1);
assert!(result.is_err());
}
#[test]
fn test_fused_rmsnorm_q4_0_matmul_zero_out_dim() {
let input = vec![1.0f32; 32];
let norm_weight = vec![1.0f32; 32];
let weight_data = vec![0u8; 18];
let result = fused_rmsnorm_q4_0_matmul(&input, &norm_weight, 1e-5, &weight_data, 32, 0);
assert!(result.is_ok());
assert!(result.expect("test value should be present").is_empty());
}
#[test]
fn test_fused_rmsnorm_ffn_up_gate_input_dim_mismatch() {
let input = vec![1.0f32; 16]; let norm_weight = vec![1.0f32; 32];
let up_weight = vec![0u8; 18];
let gate_weight = vec![0u8; 18];
let result =
fused_rmsnorm_ffn_up_gate(&input, &norm_weight, 1e-5, &up_weight, &gate_weight, 32, 1);
assert!(result.is_err());
}
#[test]
fn test_fused_rmsnorm_ffn_up_gate_up_weight_too_small() {
let input = vec![1.0f32; 32];
let norm_weight = vec![1.0f32; 32];
let up_weight = vec![0u8; 10]; let gate_weight = vec![0u8; 18];
let result =
fused_rmsnorm_ffn_up_gate(&input, &norm_weight, 1e-5, &up_weight, &gate_weight, 32, 1);
assert!(result.is_err());
}
#[test]
fn test_fused_rmsnorm_ffn_up_gate_gate_weight_too_small() {
let input = vec![1.0f32; 32];
let norm_weight = vec![1.0f32; 32];
let up_weight = vec![0u8; 18];
let gate_weight = vec![0u8; 10];
let result =
fused_rmsnorm_ffn_up_gate(&input, &norm_weight, 1e-5, &up_weight, &gate_weight, 32, 1);
assert!(result.is_err());
}
#[test]
fn test_fused_rmsnorm_ffn_up_gate_zero_out_dim() {
let input = vec![1.0f32; 32];
let norm_weight = vec![1.0f32; 32];
let up_weight = vec![0u8; 18];
let gate_weight = vec![0u8; 18];
let result =
fused_rmsnorm_ffn_up_gate(&input, &norm_weight, 1e-5, &up_weight, &gate_weight, 32, 0);
assert!(result.is_ok());
let (up, gate) = result.expect("test value should be present");
assert!(up.is_empty());
assert!(gate.is_empty());
}
#[test]
fn test_swiglu_scalar_produces_output() {
let values: Vec<f32> = (0..32).map(|i| (i as f32 - 16.0) * 0.2).collect();
let up = vec![1.0f32; 32];
let mut gate_scalar = values.clone();
fused_swiglu_scalar(&mut gate_scalar, &up);
for v in &gate_scalar {
assert!(v.is_finite(), "SwiGLU output should be finite");
}
let different = gate_scalar
.iter()
.zip(values.iter())
.any(|(a, b)| (a - b).abs() > 1e-10);
assert!(different, "SwiGLU should transform input");
}
#[test]
fn test_softmax_scalar_simd_parity() {
let values: Vec<f32> = (0..32).map(|i| i as f32 * 0.1).collect();
let mut x_scalar = values.clone();
softmax_scalar(&mut x_scalar);
let mut x_simd = values.clone();
softmax_simd(&mut x_simd);
for (s, d) in x_scalar.iter().zip(x_simd.iter()) {
assert!((s - d).abs() < 1e-5, "Mismatch: scalar={}, simd={}", s, d);
}
}
#[test]
fn test_quantize_rmsnorm_scalar_simd_parity() {
let input: Vec<f32> = (0..64).map(|i| (i as f32 - 32.0) * 0.05).collect();
let norm_weight = vec![1.0f32; 64];
let eps = 1e-5;
let (scales_scalar, quants_scalar) = quantize_rmsnorm_q8_0_scalar(&input, &norm_weight, eps);
let (scales_simd, quants_simd) = quantize_rmsnorm_q8_0(&input, &norm_weight, eps);
for (s, d) in scales_scalar.iter().zip(scales_simd.iter()) {
assert!(
(s - d).abs() < 1e-5,
"Scale mismatch: scalar={}, simd={}",
s,
d
);
}
for (s, d) in quants_scalar.iter().zip(quants_simd.iter()) {
assert!(
(*s as i32 - *d as i32).abs() <= 1,
"Quant mismatch: scalar={}, simd={}",
s,
d
);
}
}
#[test]
fn test_quantize_rmsnorm_q8_0_scalar_size_1() {
let input = vec![1.0f32];
let norm_weight = vec![1.0f32];
let eps = 1e-5;
let (scales, quants) = quantize_rmsnorm_q8_0_scalar(&input, &norm_weight, eps);
assert_eq!(scales.len(), 1);
assert_eq!(quants.len(), 32);
assert_eq!(quants[0], 127);
}
#[test]
fn test_quantize_rmsnorm_q8_0_scalar_size_31() {
let input: Vec<f32> = (0..31).map(|i| i as f32 * 0.1).collect();
let norm_weight = vec![1.0f32; 31];
let eps = 1e-5;
let (scales, quants) = quantize_rmsnorm_q8_0_scalar(&input, &norm_weight, eps);
assert_eq!(scales.len(), 1);
assert_eq!(quants.len(), 32);
assert_eq!(quants[31], 0i8);
}
#[test]
fn test_quantize_rmsnorm_q8_0_scalar_size_64() {
let input: Vec<f32> = (0..64).map(|i| (i as f32 - 32.0) * 0.1).collect();
let norm_weight = vec![1.0f32; 64];
let eps = 1e-5;
let (scales, quants) = quantize_rmsnorm_q8_0_scalar(&input, &norm_weight, eps);
assert_eq!(scales.len(), 2);
assert_eq!(quants.len(), 64);
assert!(scales[0] > 0.0);
assert!(scales[1] > 0.0);
}
#[test]
fn test_fused_swiglu_scalar_symmetry() {
let mut gate_pos = vec![1.0, 2.0, 3.0];
let mut gate_neg = vec![-1.0, -2.0, -3.0];
let up = vec![1.0, 1.0, 1.0];
fused_swiglu_scalar(&mut gate_pos, &up);
fused_swiglu_scalar(&mut gate_neg, &up);
for i in 0..3 {
assert!(gate_pos[i] > 0.0);
assert!(gate_neg[i] < 0.0);
}
}
#[test]
fn test_softmax_scalar_two_elements() {
let mut x = vec![0.0, 0.0];
softmax_scalar(&mut x);
assert!((x[0] - 0.5).abs() < 1e-5);
assert!((x[1] - 0.5).abs() < 1e-5);
}
#[test]
fn test_softmax_scalar_diff_10() {
let mut x = vec![0.0, 10.0];
softmax_scalar(&mut x);
assert!(x[0] < 0.001);
assert!(x[1] > 0.999);
}
#[test]
fn test_quantize_activations_q8_0_all_negative() {
let activations = vec![-5.0f32; 16];
let (_scales, quants) = quantize_activations_q8_0(&activations);
for q in &quants[..16] {
assert_eq!(*q, -127);
}
for q in &quants[16..32] {
assert_eq!(*q, 0);
}
}
#[test]
fn test_quantize_activations_q8_0_alternating() {
let activations = vec![1.0, -1.0, 1.0, -1.0];
let (_scales, quants) = quantize_activations_q8_0(&activations);
assert_eq!(quants[0], 127);
assert_eq!(quants[1], -127);
assert_eq!(quants[2], 127);
assert_eq!(quants[3], -127);
}
#[test]
fn test_quantize_rmsnorm_q8_0_scalar_clamping() {
let input = vec![1000.0f32; 32];
let norm_weight = vec![1.0f32; 32];
let eps = 1e-5;
let (scales, quants) = quantize_rmsnorm_q8_0_scalar(&input, &norm_weight, eps);
for q in quants {
assert_eq!(q, 127);
}
assert!(scales[0] > 0.0);
}