mod common;
use common::{
assert_all_three_paths_match, graph_caches, load_graph_fixture, worst_vs, GRAPH_PROMPT,
};
use frink_models::{NormOp, RopeLayout};
const OLMO: &str = "olmo";
const OLMO_GOLDEN: [f32; 48] = [
-1.6020501,
0.50705075,
-0.15139839,
-0.5893539,
-1.2324784,
1.0751607,
-1.1203105,
-0.5548687,
-1.9752331,
-1.4798931,
0.06502618,
1.3066584,
-0.7089759,
0.21442454,
0.011099964,
-0.020552337,
-0.628126,
0.2954016,
2.3210027,
0.32131535,
-2.238449,
1.7778922,
0.008124579,
-0.41518313,
0.8804916,
-3.3213017,
0.42046535,
-1.2797751,
-2.1666622,
-1.4526423,
1.0017449,
1.2853384,
-1.0446689,
-0.61206055,
2.1103303,
-0.3623781,
0.99392796,
1.5957211,
-0.30395782,
1.9168491,
0.63057876,
1.120366,
-0.034361586,
-0.023620725,
1.2378674,
-0.7475845,
1.0401692,
0.78326005,
];
const OLMO_CLAMPED_GOLDEN: [f32; 48] = [
-1.5948613,
0.5056803,
-0.13761881,
-0.41353196,
-1.2346972,
1.0697594,
-1.1932158,
-0.51069546,
-1.9717276,
-1.4389496,
0.0059762,
1.2528489,
-0.7875987,
0.31666917,
0.12518159,
-0.07026945,
-0.5794481,
0.35173202,
2.3756158,
0.3073057,
-2.2741225,
1.7606305,
-0.06974431,
-0.36501426,
0.8467765,
-3.2045987,
0.42799103,
-1.3141127,
-2.1140578,
-1.5678383,
1.0730993,
1.3464007,
-1.0052842,
-0.66942203,
2.0486064,
-0.34467196,
1.0760533,
1.6434469,
-0.30358222,
1.9849664,
0.6845192,
1.1937413,
-0.018013388,
0.05063148,
1.2606807,
-0.8459838,
1.043538,
0.8363968,
];
#[test]
fn olmo_matches_llama_cpp_on_all_three_paths() {
assert_all_three_paths_match(OLMO, &OLMO_GOLDEN);
}
#[test]
fn every_norm_site_is_the_non_parametric_layer_norm() {
let d = load_graph_fixture(OLMO);
assert_eq!(d.layers.len(), 2, "layer count");
for (il, layer) in d.layers.iter().enumerate() {
assert_eq!(
layer.attn.norm_weight,
NormOp::LayerNormNoParams,
"blk.{il}: the attention branch norms the residual, with no weight"
);
assert_eq!(
layer.moe.norm_weight,
NormOp::LayerNormNoParams,
"blk.{il}: the FFN branch norms the residual, with no weight"
);
assert!(
layer.attn.post_attn_norm.is_none(),
"blk.{il}: olmo.cpp creates no ATTN_POST_NORM"
);
assert!(
layer.attn.post_ffn_norm.is_none(),
"blk.{il}: olmo.cpp creates no FFN_POST_NORM"
);
}
assert_eq!(
d.final_norm,
NormOp::LayerNormNoParams,
"olmo.cpp:15-36 creates no `output_norm` and :128-130 norms with a null weight"
);
}
#[test]
fn substituting_an_all_ones_rmsnorm_at_any_site_diverges_from_llama_cpp() {
for site in ["attn", "ffn", "final"] {
let mut d = load_graph_fixture(OLMO);
let hidden = d.config.hidden_dim;
match site {
"attn" => {
for layer in d.layers.iter_mut() {
layer.attn.norm_weight = NormOp::Rms(vec![1.0; hidden]);
}
}
"ffn" => {
for layer in d.layers.iter_mut() {
layer.moe.norm_weight = NormOp::Rms(vec![1.0; hidden]);
}
}
_ => d.final_norm = NormOp::Rms(vec![1.0; hidden]),
}
let mut kv = graph_caches(&d);
let worst = worst_vs(
&d.forward_batch_last(&GRAPH_PROMPT, 0, &mut kv),
&OLMO_GOLDEN,
);
assert!(
worst > 1e-2,
"an all-ones RMSNorm at the {site} site moved the output by only {worst}; this \
fixture's hidden states must be too close to centred for the mean subtraction \
to matter, and it cannot see the norm function it exists to pin"
);
}
}
#[test]
fn dropping_the_norm_at_any_site_diverges_from_llama_cpp() {
for site in ["attn", "ffn", "final"] {
let mut d = load_graph_fixture(OLMO);
match site {
"attn" => {
for layer in d.layers.iter_mut() {
layer.attn.norm_weight = NormOp::None;
}
}
"ffn" => {
for layer in d.layers.iter_mut() {
layer.moe.norm_weight = NormOp::None;
}
}
_ => d.final_norm = NormOp::None,
}
let mut kv = graph_caches(&d);
let worst = worst_vs(
&d.forward_batch_last(&GRAPH_PROMPT, 0, &mut kv),
&OLMO_GOLDEN,
);
assert!(
worst > 1e-2,
"removing the {site} norm moved the output by only {worst}; the fixture cannot \
tell OLMo-1 from the post-norm-only topology"
);
}
}
#[test]
fn olmo_ties_its_lm_head_and_uses_the_kernels_own_attention_scale() {
let d = load_graph_fixture(OLMO);
assert_eq!(d.config.attention_scale, None);
assert_eq!(d.config.n_heads, 4);
assert_eq!(d.config.n_kv_heads, 2, "the fixture exercises GQA");
assert_eq!(d.config.head_dim, 6);
assert_eq!(
d.config.rms_norm_eps, 1e-5,
"the epsilon comes from `olmo.attention.layer_norm_epsilon`, not the RMS spelling"
);
assert_eq!(d.config.embedding_scale, None);
assert_eq!(d.config.residual_scale, None);
assert_eq!(d.config.logit_multiplier, None);
}
#[test]
fn olmo_ropes_consecutive_pairs_and_the_fixture_can_see_the_other_variant() {
let d = load_graph_fixture(OLMO);
assert_eq!(d.config.rope_layout, RopeLayout::Norm);
let mut d = load_graph_fixture(OLMO);
d.config.rope_layout = RopeLayout::Neox;
let mut kv = graph_caches(&d);
let worst = worst_vs(
&d.forward_batch_last(&GRAPH_PROMPT, 0, &mut kv),
&OLMO_GOLDEN,
);
assert!(
worst > 1e-3,
"rotating the NEOX pairs moved the output by only {worst}; the attention in this \
fixture is too flat to see a positional bug"
);
}
#[test]
fn a_clamped_olmo_file_matches_llama_cpp_on_all_three_paths() {
assert_all_three_paths_match("olmo_clamped", &OLMO_CLAMPED_GOLDEN);
}
#[test]
fn the_clamp_is_read_into_the_config() {
let clamped = load_graph_fixture("olmo_clamped");
assert_eq!(clamped.config.clamp_kqv, Some(8.0));
let plain = load_graph_fixture(OLMO);
assert_eq!(
plain.config.clamp_kqv, None,
"no key: llama.cpp's 0.0 default, no clamp"
);
}
#[test]
fn removing_or_inventing_the_clamp_diverges_from_llama_cpp() {
let mut d = load_graph_fixture("olmo_clamped");
d.config.clamp_kqv = None;
let mut kv = graph_caches(&d);
let worst = worst_vs(
&d.forward_batch_last(&GRAPH_PROMPT, 0, &mut kv),
&OLMO_CLAMPED_GOLDEN,
);
assert!(
worst > 1e-2,
"dropping the clamp moved the output by only {worst}; the clamped fixture's \
projections must be too small for the clamp to bite, and it cannot see the \
feature it exists to pin"
);
let mut d = load_graph_fixture(OLMO);
d.config.clamp_kqv = Some(8.0);
let mut kv = graph_caches(&d);
let worst = worst_vs(
&d.forward_batch_last(&GRAPH_PROMPT, 0, &mut kv),
&OLMO_GOLDEN,
);
assert!(
worst > 1e-2,
"clamping the unclamped file moved the output by only {worst}"
);
}