pub fn swa_layers_unscaled_rope(arch: &str) -> Option<&'static str> {
match arch {
"olmo2" => Some("olmo2.cpp:120-134"),
"mellum" => Some("mellum.cpp:128-142"),
"laguna" => Some("laguna.cpp:48,181-193"),
_ => None,
}
}
pub fn full_layers_rotate_half(arch: &str) -> Option<&'static str> {
match arch {
"step35" => Some("step35.cpp:9"),
_ => None,
}
}
pub fn window_required(arch: &str) -> Option<&'static str> {
match arch {
"cohere2" => Some("cohere2.cpp:13"),
"cohere2moe" => Some("cohere2moe.cpp:13"),
"exaone-moe" => Some("exaone-moe.cpp:13"),
_ => None,
}
}
pub fn swa_layers_drop_rope_factors(arch: &str) -> Option<&'static str> {
match arch {
"step35" => Some("step35.cpp:247"),
_ => None,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SwaGeometry {
pub rope_dim_swa: Option<u64>,
pub key_length_swa: Option<u64>,
pub value_length_swa: Option<u64>,
pub rope_dim_full: u64,
pub head_dim: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RotaryWidths {
pub full: Option<usize>,
pub swa: Option<usize>,
}
pub fn rotary_widths(arch: &str, g: SwaGeometry) -> RotaryWidths {
let head_dim = g.head_dim as usize;
let seeded = g.rope_dim_full as usize;
let swa_raw = g.rope_dim_swa.map(|w| w as usize).unwrap_or(seeded);
let full_raw = if full_layers_rotate_half(arch).is_some() {
seeded / 2
} else {
seeded
};
let narrow = |w: usize| (w > 0 && w < head_dim).then_some(w);
let full = narrow(full_raw);
let swa = (narrow(swa_raw) != full).then_some(swa_raw);
RotaryWidths { full, swa }
}
pub fn swa_geometry_refusal(arch: &str, g: SwaGeometry) -> Option<String> {
let (key, declared, full) = [
("attention.key_length_swa", g.key_length_swa, g.head_dim),
("attention.value_length_swa", g.value_length_swa, g.head_dim),
]
.into_iter()
.find_map(|(key, declared, full)| declared.filter(|v| *v != full).map(|v| (key, v, full)))?;
Some(format!(
"`{arch}.{key}` = {declared} while the full-attention layers use {full}: llama.cpp \
gives the sliding layers their own head width (llama-model.cpp:1215-1219) and frink \
carries one for the whole model, so honouring the file would cache half the layers \
at the wrong width"
))
}
pub fn two_widths_with_factors_refusal(arch: &str, widths: RotaryWidths) -> Option<String> {
if widths.swa.is_none() || swa_layers_drop_rope_factors(arch).is_some() {
return None;
}
Some(format!(
"the sliding layers rotate {:?} dims and the full layers {:?} (`n_rot(il)`, \
llama-hparams.cpp:85-91), and the file declares per-band RoPE divisors (a \
`rope_freqs.weight` tensor or a linear / YaRN scaling): one divisor vector cannot \
serve two widths, and no line of `{arch}`'s graph says which layers take it -- \
`step35.cpp:247` does, and is the only generic-path graph that does",
widths.swa, widths.full
))
}
#[cfg(test)]
mod tests {
use super::*;
fn geometry() -> SwaGeometry {
SwaGeometry {
rope_dim_swa: None,
key_length_swa: None,
value_length_swa: None,
rope_dim_full: 64,
head_dim: 128,
}
}
#[test]
fn a_swa_head_width_equal_to_the_full_one_is_served_and_a_different_one_names_the_key() {
assert_eq!(swa_geometry_refusal("laguna", geometry()), None);
let mut same = geometry();
same.rope_dim_swa = Some(128);
same.key_length_swa = Some(128);
same.value_length_swa = Some(128);
assert_eq!(swa_geometry_refusal("laguna", same), None);
let mut kv = geometry();
kv.value_length_swa = Some(256);
let msg = swa_geometry_refusal("gemma4", kv).expect("refused");
assert!(msg.contains("value_length_swa"), "{msg}");
let mut k = geometry();
k.key_length_swa = Some(64);
let msg = swa_geometry_refusal("gemma4", k).expect("refused");
assert!(
msg.contains("`gemma4.attention.key_length_swa` = 64"),
"{msg}"
);
}
#[test]
fn rotary_widths_resolve_as_llama_cpp_resolves_n_rot_full_and_n_rot_swa() {
let mut xs2 = geometry();
xs2.rope_dim_swa = Some(128);
assert_eq!(
rotary_widths("laguna", xs2),
RotaryWidths {
full: Some(64),
swa: Some(128)
}
);
let mut s35 = geometry();
s35.rope_dim_full = 128;
assert_eq!(
rotary_widths("step35", s35),
RotaryWidths {
full: Some(64),
swa: Some(128)
}
);
assert_eq!(
rotary_widths("gemma3", s35),
RotaryWidths {
full: None,
swa: None
}
);
let mut same = geometry();
same.rope_dim_swa = Some(64);
assert_eq!(
rotary_widths("laguna", same),
RotaryWidths {
full: Some(64),
swa: None
}
);
assert_eq!(
rotary_widths("phi3", geometry()),
RotaryWidths {
full: Some(64),
swa: None
}
);
}
#[test]
fn two_widths_with_divisors_is_refused_except_where_the_graph_drops_them() {
let two = RotaryWidths {
full: Some(64),
swa: Some(128),
};
let msg = two_widths_with_factors_refusal("laguna", two).expect("refused");
assert!(
msg.contains("Some(128)") && msg.contains("step35.cpp:247"),
"{msg}"
);
assert_eq!(two_widths_with_factors_refusal("step35", two), None);
assert_eq!(
two_widths_with_factors_refusal(
"laguna",
RotaryWidths {
full: Some(64),
swa: None
}
),
None
);
}
#[test]
fn the_per_arch_tables_cite_lines_for_exactly_their_rows() {
for arch in ["olmo2", "mellum", "laguna"] {
let lines = swa_layers_unscaled_rope(arch).unwrap_or_else(|| panic!("{arch}"));
assert!(lines.contains(".cpp:"), "{arch}: {lines}");
}
for arch in ["gemma3", "gpt-oss", "exaone4", "afmoe", "llama", "step35"] {
assert_eq!(swa_layers_unscaled_rope(arch), None, "{arch}");
}
assert_eq!(full_layers_rotate_half("step35"), Some("step35.cpp:9"));
assert_eq!(
swa_layers_drop_rope_factors("step35"),
Some("step35.cpp:247")
);
for arch in ["laguna", "gemma3", "llama", "mimo2"] {
assert_eq!(full_layers_rotate_half(arch), None, "{arch}");
assert_eq!(swa_layers_drop_rope_factors(arch), None, "{arch}");
}
}
}