//! End-to-end LFM2-MoE (`lfm2moe` arch) GGUF decode oracle. Ignored by default — //! needs the real ~5 GB Q4_K_M checkpoint on disk. //! //! Reference is `llama.cpp` on the same file. NOTE: LFM2 prepends a BOS //! (`<|startoftext|>` = 124894). We validate on a *sharp* idiom prompt where the //! next token is unambiguous, so the argmax cannot be flipped by the small //! numerical difference between this f32 forward and llama.cpp's Q4_K matmul //! (a flat prompt like "The capital of France is" is a poor oracle for that //! reason — LFM2 there is genuinely torn between " city"/" Paris"/" the"). //! //! Run: `cargo test -p jouleclaw-loader-gguf --release --features rayon \ //! --test real_lfm2_oracle -- --ignored --nocapture` use jouleclaw_loader_gguf::bpe_tokenizer::BpeTokenizer; use jouleclaw_loader_gguf::lfm2moe::Lfm2Moe; use jouleclaw_loader_gguf::read_gguf_file; const GG: &str = "/Volumes/macos_4TB_external_1/neuronexus_ai/model_library/lfm2.5-8b-a1b-gguf/LFM2.5-8B-A1B-Q4_K_M.gguf"; const BOS: u32 = 124894; /// "The quick brown fox jumps over the lazy" (+BOS) per `llama-tokenize`. const FOX: [u32; 9] = [BOS, 597, 4104, 10299, 35886, 56085, 890, 278, 45698]; /// " dog" — llama.cpp's confident greedy next token. const DOG: u32 = 4448; fn argmax(v: &[f32]) -> usize { v.iter() .enumerate() .fold((0usize, f32::NEG_INFINITY), |(bi, bv), (i, &x)| if x > bv { (i, x) } else { (bi, bv) }) .0 } #[test] #[ignore] fn real_lfm2_predicts_dog() { if !std::path::Path::new(GG).exists() { eprintln!("skip: lfm2 GGUF not present"); return; } let m = Lfm2Moe::load(GG).expect("load lfm2 gguf"); eprintln!( "lfm2moe: {} layers, d={}, heads={}/{}, experts={}/{}, dense_lead={}, L={}, vocab={}", m.cfg.n_layers, m.cfg.d_model, m.cfg.n_heads, m.cfg.n_kv, m.cfg.n_used, m.cfg.n_experts, m.cfg.leading_dense, m.cfg.l_cache, m.cfg.vocab ); let got = argmax(&m.forward(&FOX)); eprintln!("argmax = {got} (expected {DOG} = ' dog')"); assert_eq!(got, DOG as usize, "greedy next token diverges from llama.cpp"); } /// Full text-in / text-out through the GGUF-embedded tokenizer (+BOS): the prompt /// tokenizes to the llama.cpp ids and greedy decode reads "... dog". #[test] #[ignore] fn real_lfm2_text_roundtrip() { if !std::path::Path::new(GG).exists() { eprintln!("skip: lfm2 GGUF not present"); return; } let tok = BpeTokenizer::from_gguf(&read_gguf_file(GG).expect("gguf")).expect("tokenizer"); let body = tok.encode("The quick brown fox jumps over the lazy"); assert_eq!(body, FOX[1..].to_vec(), "tokenization diverges from llama.cpp"); let mut ids = vec![BOS]; ids.extend_from_slice(&body); let m = Lfm2Moe::load(GG).expect("load"); let text = tok.decode(&m.generate(&ids, 3)); eprintln!("generated = {text:?}"); assert!(text.starts_with(" dog"), "greedy decode should start ' dog', got {text:?}"); }