plugins/toy/x64-cpu/tests/goldens.rs 3.0 KiB 89 lines Raw
1 //! The toy's recorded job. The weights are generated here, so the golden is2 //! reproducible without a tracked model file.3 4 use std::fs;5 use std::path::PathBuf;6 use std::sync::atomic::{AtomicU64, Ordering};7 8 use inf3r_goldens::bundle::Golden;9 10 fn bin() -> PathBuf {11     PathBuf::from(env!("CARGO_BIN_EXE_inf3r-toy-x64-cpu"))12 }13 14 fn push_str(out: &mut Vec<u8>, text: &str) {15     out.extend_from_slice(&(text.len() as u64).to_le_bytes());16     out.extend_from_slice(text.as_bytes());17 }18 19 fn weights() -> PathBuf {20     static NEXT: AtomicU64 = AtomicU64::new(1);21     const ALIGN: usize = 32;22     const WIDTH: usize = 8;23     let vocab = ["x", "a"];24     let mut out = Vec::new();25     out.extend_from_slice(b"GGUF");26     out.extend_from_slice(&3u32.to_le_bytes());27     out.extend_from_slice(&1u64.to_le_bytes());28     out.extend_from_slice(&2u64.to_le_bytes());29     push_str(&mut out, "toy.vocab");30     out.extend_from_slice(&12u32.to_le_bytes());31     out.extend_from_slice(&11u32.to_le_bytes());32     out.extend_from_slice(&(vocab.len() as u64).to_le_bytes());33     for token in vocab {34         push_str(&mut out, token);35     }36     push_str(&mut out, "toy.stop");37     out.extend_from_slice(&11u32.to_le_bytes());38     push_str(&mut out, "zzz");39     push_str(&mut out, "embed");40     out.extend_from_slice(&2u32.to_le_bytes());41     out.extend_from_slice(&(vocab.len() as u64).to_le_bytes());42     out.extend_from_slice(&(WIDTH as u64).to_le_bytes());43     out.extend_from_slice(&0u32.to_le_bytes());44     out.extend_from_slice(&0u64.to_le_bytes());45     let rem = out.len() % ALIGN;46     if rem != 0 {47         out.resize(out.len() + (ALIGN - rem), 0);48     }49     for token in 0..vocab.len() {50         for position in 0..WIDTH {51             let value = if token == 1 && position == 0 {52                 1.0f3253             } else {54                 0.0f3255             };56             out.extend_from_slice(&value.to_le_bytes());57         }58     }59     let path = std::env::temp_dir().join(format!(60         "inf3r-toy-golden-{}-{}.gguf",61         std::process::id(),62         NEXT.fetch_add(1, Ordering::Relaxed)63     ));64     fs::write(&path, out).expect("write the generated GGUF");65     path66 }67 68 #[test]69 fn every_recorded_toy_golden_matches() {70     let weights = weights();71     let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("goldens");72     let mut files: Vec<PathBuf> = fs::read_dir(&dir)73         .expect("the golden directory")74         .filter_map(Result::ok)75         .map(|entry| entry.path())76         .filter(|path| path.extension().is_some_and(|ext| ext == "json"))77         .collect();78     files.sort();79     assert!(!files.is_empty(), "the toy ships no golden");80     for path in files {81         let golden = Golden::read(&path).unwrap_or_else(|why| panic!("{}: {why}", path.display()));82         let run = golden83             .run(bin(), Some(&weights))84             .unwrap_or_else(|why| panic!("{}: {why}", path.display()));85         run.check(&golden)86             .unwrap_or_else(|why| panic!("{}: {why}\n{run:?}", path.display()));87     }88     let _ = fs::remove_file(&weights);89 }