mod common;
use common::{
assert_all_three_paths_match, graph_caches, graph_fixture_path, kl_vs_golden,
load_graph_fixture, worst_vs, EXAONE_MOE_GOLDEN, GRAPH_PROMPT,
};
use frink_models::loader::LoadError;
use frink_models::swa_layers::SwaLayers;
use frink_models::{Decoder, ModelConfig};
const EXAONE_MOE_MTP_GOLDEN: [f32; 48] = [
-0.0399573,
-0.26254788,
0.112740405,
-0.45957953,
0.20378731,
0.09908521,
0.0650342,
0.03963881,
0.027113974,
0.04426959,
0.2971161,
0.44927487,
-0.08063339,
-0.31896305,
0.12336944,
-0.26059663,
0.23168293,
-0.07957372,
-0.041728646,
-0.06822635,
-0.012256693,
-0.06147337,
0.058683395,
0.018150419,
0.34285027,
-0.17899069,
0.35574993,
0.005653359,
0.21688741,
0.18249817,
0.015758194,
0.19431275,
-0.5292666,
0.089496456,
-0.27419174,
0.6649489,
-0.22339234,
-0.28366813,
0.23308644,
0.13166983,
-0.035332873,
-0.45804775,
0.030781724,
0.18007353,
0.16690017,
-0.18101907,
-0.04980211,
-0.14753367,
];
const MELLUM_GOLDEN: [f32; 48] = [
0.060756013,
-0.023824722,
0.20276277,
-0.08673769,
-0.17752253,
0.16671993,
-0.049065586,
-0.14727482,
-0.23216516,
0.018855155,
-0.15827064,
-0.13220614,
0.017678097,
0.44898403,
0.18785386,
-0.060248606,
0.18946445,
0.09642051,
0.009634852,
-0.22297539,
-0.1649667,
0.3061224,
-0.31932747,
-0.063845895,
-0.42996672,
-0.00028830767,
0.4683508,
-0.33105162,
0.21087095,
-0.16849121,
-0.39124924,
0.124670506,
0.019993221,
-0.48215437,
-0.43157175,
-0.56464714,
0.34288388,
0.11505209,
0.18613203,
-0.30828017,
0.1446941,
-0.335675,
-0.043562084,
-0.14170834,
0.27802056,
0.03002052,
0.1744021,
-0.067254916,
];
const ROWS: [(&str, &[f32]); 4] = [
("exaone_moe_array", &EXAONE_MOE_GOLDEN),
("exaone_moe_array_flipped", &EXAONE_MOE_GOLDEN),
("exaone_moe_mtp", &EXAONE_MOE_MTP_GOLDEN),
("mellum", &MELLUM_GOLDEN),
];
fn prefill(decoder: &Decoder) -> Vec<f32> {
let mut kv = graph_caches(decoder);
decoder.forward_batch_last(&GRAPH_PROMPT, 0, &mut kv)
}
#[test]
fn exaone_moe_with_the_converter_array_matches_llama_cpp_on_all_three_paths() {
assert_all_three_paths_match("exaone_moe_array", &EXAONE_MOE_GOLDEN);
let d = load_graph_fixture("exaone_moe_array");
assert_eq!(
d.config.swa_layers,
SwaLayers::period(4, false),
"exaone-moe.cpp:6-8 seeds 4 and :7's scalar overload returns false on an array"
);
}
#[test]
fn exaone_moe_with_a_disagreeing_array_is_still_the_seeded_period() {
assert_all_three_paths_match("exaone_moe_array_flipped", &EXAONE_MOE_GOLDEN);
let d = load_graph_fixture("exaone_moe_array_flipped");
assert_eq!(d.config.swa_layers, SwaLayers::period(4, false));
assert_eq!(d.config.layer_sliding_window(0), Some(3));
assert_eq!(d.config.layer_sliding_window(3), None);
}
#[test]
fn mellum_matches_llama_cpp_on_all_three_paths() {
assert_all_three_paths_match("mellum", &MELLUM_GOLDEN);
let d = load_graph_fixture("mellum");
assert_eq!(
d.config.swa_layers,
SwaLayers::PerLayer(vec![true, true, false, true].into())
);
assert_eq!(d.config.sliding_window, Some(3));
let windowed: Vec<bool> = (0..4)
.map(|il| d.config.layer_sliding_window(il).is_some())
.collect();
assert_eq!(
windowed,
[true, true, false, true],
"libllama's load_tensors reports is_swa = 1, 1, 0, 1"
);
}
#[test]
fn mellum_with_the_seeded_period_instead_of_its_array_diverges_from_llama_cpp() {
let path = graph_fixture_path("mellum");
let file = frink_gguf::GgufFile::open(&path).expect("opens");
let mut config = ModelConfig::from_gguf(&file).expect("parses");
config.swa_layers = SwaLayers::period(4, false);
let d = Decoder::from_gguf(&path, config).expect("loads");
assert_eq!(d.config.layer_sliding_window(2), Some(3), "sabotage landed");
assert_eq!(d.config.layer_sliding_window(3), None, "sabotage landed");
let worst = worst_vs(&prefill(&d), &MELLUM_GOLDEN);
assert!(
worst > 1e-2,
"the seeded period must be visible against a 3-token window: {worst}"
);
}
#[test]
fn mellum_windowing_every_layer_diverges_from_llama_cpp() {
let path = graph_fixture_path("mellum");
let file = frink_gguf::GgufFile::open(&path).expect("opens");
let mut config = ModelConfig::from_gguf(&file).expect("parses");
config.swa_layers = SwaLayers::All;
let d = Decoder::from_gguf(&path, config).expect("loads");
let worst = worst_vs(&prefill(&d), &MELLUM_GOLDEN);
assert!(worst > 1e-2, "windowing layer 2 must be visible: {worst}");
}
#[test]
fn exaone_moe_with_an_mtp_block_matches_llama_cpp_on_all_three_paths() {
assert_all_three_paths_match("exaone_moe_mtp", &EXAONE_MOE_MTP_GOLDEN);
let d = load_graph_fixture("exaone_moe_mtp");
assert_eq!(
(d.config.n_layers, d.config.n_mtp_blocks),
(4, 1),
"llama.cpp: n_layer = 4, n_layer_all = 5"
);
assert_eq!(d.layers.len(), 4, "the block is not a layer");
assert_eq!(d.config.layer_sliding_window(3), None);
assert!(!d.config.layer_rotates(3));
}
#[test]
fn counting_the_mtp_block_as_a_layer_cannot_load() {
let path = graph_fixture_path("exaone_moe_mtp");
let file = frink_gguf::GgufFile::open(&path).expect("opens");
let mut config = ModelConfig::from_gguf(&file).expect("parses");
config.n_layers = 5;
config.n_mtp_blocks = 0;
match Decoder::from_gguf(&path, config) {
Err(LoadError::Gguf(frink_gguf::GgufError::TensorNotFound(name))) => {
assert!(
name.starts_with("blk.4.ffn_") && name.contains("_exps"),
"the block has no experts: {name}"
);
}
Err(other) => panic!("the block is not a layer, got {other:?}"),
Ok(_) => panic!("the block is not a layer, yet it loaded as one"),
}
}
#[test]
fn an_undeclared_skip_is_refused_by_the_consumption_gate() {
let path = graph_fixture_path("exaone_moe_mtp");
let file = frink_gguf::GgufFile::open(&path).expect("opens");
let mut config = ModelConfig::from_gguf(&file).expect("parses");
config.n_mtp_blocks = 0;
match Decoder::from_gguf(&path, config) {
Err(LoadError::UnconsumedTensors(n, _)) => assert_eq!(n, 15),
Err(other) => panic!("fifteen unread tensors must refuse, got {other:?}"),
Ok(_) => panic!("fifteen unread tensors must refuse, yet it loaded"),
}
}
#[test]
fn report_kl_against_llama_cpp() {
for (name, want) in ROWS {
let d = load_graph_fixture(name);
let got = prefill(&d);
let kl = kl_vs_golden(&got, want);
let worst = worst_vs(&got, want);
eprintln!("{name}: KL(llama.cpp || frink) = {kl:.3e} nats, max |delta| = {worst:.3e}");
assert!(kl < 1e-8, "{name}: KL {kl}");
}
}