underdog_woof_rust / src /validation.rs
Ahsanmandhar's picture
Upload src/validation.rs with huggingface_hub
7556b61 verified
Raw History Blame Contribute Delete
3.42 kB
//! Model output validation for Woof Rust migration.
//! Asserts DIM=512, safe denormalization range, and i16 PCM16 bounds.
use ndarray::Array1;
/// Assert decoder output is valid:
/// - latent.len() == 512
/// - denormalized values stay in [-1, 1] before PCM16 conversion
/// - PCM16 values in range [-32768, 32767]
///
/// Returns true if all checks pass, false otherwise.
/// Prints diagnostic messages for any failures.
pub fn validate_decoder_output(latent: &Array1<f32>, pcm: &[i16]) -> bool {
let mut valid = true;
// Check latent dimension DIM=512
if latent.len() != 512 {
println!("ERROR: Latent dimension is {}Expected DIM=512", latent.len());
valid = false;
} else {
println!("Validation: Latent DIM=512 ✓");
}
// Check denormalized values stay in [-1, 1] before PCM16 conversion
// This is implicitly checked by the decoder's tanh constraint,
// but we verify the PCM16 range as a proxy
let mut out_of_range_count = 0;
for (i, &sample) in pcm.iter().enumerate() {
if sample < -32768 || sample > 32767 {
out_of_range_count += 1;
if i < 5 { // Show first few violations
println!("WARNING: PCM16 sample[{}] out of range: {}", i, sample);
}
}
}
if out_of_range_count > 0 {
println!("WARNING: {} out of {} PCM16 samples out of [-32768, 32767] range",
out_of_range_count, pcm.len());
valid = false;
} else {
println!("Validation: PCM16 range [-32768, 32767] ✓");
}
// Verify latent values are within reasonable range before denormalization
let min_latent = latent.iter().min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)).unwrap_or(&0.0);
let max_latent = latent.iter().max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)).unwrap_or(&0.0);
if *min_latent < -10.0 || *max_latent > 10.0 {
println!("INFO: Latent values range [{}, {}] is outside typical [-1, 1] range", min_latent, max_latent);
} else {
println!("Validation: Latent values range [{}, {}] ✓", min_latent, max_latent);
}
if valid {
println!("Validation passed");
}
valid
}
#[cfg(test)]
mod tests {
use super::*;
use ndarray::Array1;
#[test]
fn test_validate_decoder_output_valid() {
let latent = Array1::from_vec(vec![0.5f32; 512]);
let pcm: Vec<i16> = vec![0i16; 100]; // Valid PCM16 range
assert!(validate_decoder_output(&latent, &pcm));
}
#[test]
fn test_validate_decoder_output_wrong_dim() {
let latent = Array1::from_vec(vec![0.5f32; 256]); // Wrong dimension
let pcm: Vec<i16> = vec![0i16; 100];
assert!(!validate_decoder_output(&latent, &pcm));
}
#[test]
fn test_validate_decoder_output_out_of_range_pcm() {
let latent = Array1::from_vec(vec![0.5f32; 512]);
let pcm: Vec<i16> = vec![32768i16; 100]; // Out of range (max is 32767)
assert!(!validate_decoder_output(&latent, &pcm));
}
#[test]
fn test_validate_decoder_output_empty_pcm() {
let latent = Array1::from_vec(vec![0.5f32; 512]);
let pcm: Vec<i16> = vec![];
assert!(!validate_decoder_output(&latent, &pcm));
}
}