pub const INTERLEAVE_STEP_IS_REQUIRED: &[&str] = &["ernie4_5-moe"];
pub const INTERLEAVE_STEP_HONOURED_BY_LOADER: &[(&str, &str)] =
&[("llama4", "src/models/llama4.cpp:6,49-51,64")];
pub fn interleave_step(
arch: &str,
step: Option<u64>,
n_experts: usize,
) -> Result<Option<usize>, String> {
let Some((_, lines)) = INTERLEAVE_STEP_HONOURED_BY_LOADER
.iter()
.find(|(a, _)| *a == arch)
else {
return Ok(None);
};
if n_experts == 0 {
return Err(format!(
"`{arch}.expert_count` is 0: llama.cpp throws `{arch} model cannot have zero \
experts` before creating a tensor ({lines}; measured on \
scripts/make_llama4_fixture.py --dense), so a dense Llama 4 (MobileLLM) has no \
reference graph to match"
));
}
match step {
None => Err(format!(
"`{arch}.interleave_moe_layer_step` is missing; llama.cpp reads it as a REQUIRED \
key ({lines}) and every export writes it (conversion/llama.py:392)"
)),
Some(0) => Err(format!(
"`{arch}.interleave_moe_layer_step` is 0: {lines} then loads EVERY layer dense \
with `expert_count` nonzero, a file no converter writes and no fixture has a \
libllama golden for"
)),
Some(step) => Ok(Some(step as usize)),
}
}
pub const DENSE_LAYER_BY_ROUTER_ABSENCE: &[&str] = &["jamba"];
pub fn dense_by_router_absence(
arch: &str,
file: &impl frink_gguf::TensorSource,
layer: usize,
) -> bool {
DENSE_LAYER_BY_ROUTER_ABSENCE.contains(&arch)
&& file
.find_tensor(&format!("blk.{layer}.ffn_gate_inp.weight"))
.is_none()
}
pub fn interleave_step_refusal(arch: &str, step: Option<u64>) -> Option<String> {
if INTERLEAVE_STEP_HONOURED_BY_LOADER
.iter()
.any(|(a, _)| *a == arch)
{
return None;
}
match step {
None if INTERLEAVE_STEP_IS_REQUIRED.contains(&arch) => Some(format!(
"`{arch}.interleave_moe_layer_step` is missing. llama.cpp reads it as a REQUIRED \
key for this architecture (src/models/ernie4-5.cpp:11) and asserts it is positive \
(ernie4-5-moe.cpp:26), so a file without it is one llama.cpp will not load \
either. Every real ERNIE-4.5 MoE export carries it (conversion/ernie.py:88)"
)),
None | Some(1) => None,
Some(0) => Some(format!(
"`{arch}.interleave_moe_layer_step` is 0. llama.cpp asserts \
`hparams.n_moe_layer_step > 0` (src/models/ernie4-5-moe.cpp:26) before building \
the graph, and a step of 0 would divide by zero in its own layer rule at :64"
)),
Some(step) => Some(format!(
"`{arch}.interleave_moe_layer_step` is {step}, and frink serves only 1. \
src/models/ernie4-5-moe.cpp:64 makes a layer MoE when \
`il >= n_layer_dense_lead && (il + 1) % n_moe_layer_step == 0`, while \
ModelConfig::layer_is_dense implements only the leading-dense prefix -- but \
NEITHER does llama.cpp load such a file. Its tensor loader \
(src/models/ernie4-5.cpp:49) creates the expert tensors as REQUIRED for EVERY \
layer at or past `n_layer_dense_lead`, with no step in the condition, and \
creates the dense `ffn_gate`/`ffn_up`/`ffn_down` for none of them. The loader \
and the graph therefore agree only when the step changes nothing, so a \
checkpoint whose interleave really interleaves cannot be loaded by llama.cpp \
and there is no reference to check frink against. Both published ERNIE-4.5 MoE \
checkpoints carry a step of 1, which frink runs and \
tests/one_match_arm_graphs.rs pins against libllama's own logits"
)),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_step_of_one_and_an_absent_key_are_both_served() {
assert!(interleave_step_refusal("ernie4_5-moe", Some(1)).is_none());
assert!(interleave_step_refusal("llama", None).is_none());
assert!(interleave_step_refusal("qwen3moe", Some(1)).is_none());
}
#[test]
fn a_step_above_one_is_refused_and_names_llama_cpps_own_contradiction() {
let r = interleave_step_refusal("ernie4_5-moe", Some(2)).expect("refused");
assert!(r.contains("ernie4-5-moe.cpp:64"), "{r}");
assert!(r.contains("ernie4-5.cpp:49"), "{r}");
assert!(r.contains("cannot be loaded by llama.cpp"), "{r}");
}
#[test]
fn the_key_is_required_for_ernie_and_optional_everywhere_else() {
assert!(interleave_step_refusal("ernie4_5-moe", None).is_some());
for arch in ["llama", "qwen3moe", "dots1", "ernie4_5"] {
assert!(
interleave_step_refusal(arch, None).is_none(),
"{arch} must not require the key"
);
}
}
#[test]
fn llama4_s_step_is_served_and_its_three_refusals_name_the_lines() {
assert_eq!(interleave_step("llama4", Some(2), 16).unwrap(), Some(2));
assert_eq!(interleave_step("llama4", Some(1), 128).unwrap(), Some(1));
assert!(interleave_step("llama4", None, 16)
.unwrap_err()
.contains("REQUIRED"));
assert!(interleave_step("llama4", Some(0), 16)
.unwrap_err()
.contains("EVERY layer dense"));
assert!(interleave_step("llama4", Some(2), 0)
.unwrap_err()
.contains("cannot have zero"));
assert_eq!(interleave_step("ernie4_5-moe", Some(2), 16).unwrap(), None);
assert!(interleave_step_refusal("llama4", Some(2)).is_none());
}
#[test]
fn a_step_of_zero_is_refused_rather_than_dividing_by_zero() {
let r = interleave_step_refusal("ernie4_5-moe", Some(0)).expect("refused");
assert!(r.contains("divide by zero"), "{r}");
}
}