pub const CLAMPED_QKV_ARCHITECTURES: &[&str] = &["olmo"];
pub fn clamped_refusal(arch: &str, declared: Option<f32>) -> Option<String> {
if !CLAMPED_QKV_ARCHITECTURES.contains(&arch) {
return None;
}
let v = declared.filter(|v| *v > 0.0)?;
Some(format!(
"`{arch}.attention.clamp_kqv` = {v}. llama-graph.cpp:1611-1652 clamps the Q, K and V \
projections to [-{v}, {v}] inside `build_qkv`, before the reshape, the QK-norm and \
RoPE. ferrox has no clamp on any projection, and adding one to the CPU prefill body \
while missing the decode body or a fused Metal launch is the defect shape that has \
already cost this engine eight model features one at a time, so it stops instead. \
`conversion/olmo.py:23-25` writes this key whenever the HF config carries a \
`clip_qkv`: OLMo-7B-Twin-2T and OLMo-1.7-7B do, the original OLMo-7B does not"
))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_a_positive_clamp_refuses() {
assert!(clamped_refusal("olmo", Some(8.0)).is_some());
assert!(
clamped_refusal("olmo", None).is_none(),
"an absent key is llama.cpp's 0.0 default: no clamp"
);
assert!(
clamped_refusal("olmo", Some(0.0)).is_none(),
"`> 0.0f` is llama.cpp's own test; zero means no clamp"
);
assert!(
clamped_refusal("olmo", Some(-1.0)).is_none(),
"a negative clamp is not a clamp in llama.cpp either"
);
}
#[test]
fn an_architecture_whose_graph_does_not_clamp_is_unaffected() {
for arch in ["llama", "qwen3", "olmo2", "mpt", "dbrx"] {
assert!(
clamped_refusal(arch, Some(8.0)).is_none(),
"{arch} must not be gated by this key here"
);
}
}
#[test]
fn the_refusal_names_the_key_the_value_and_the_line() {
let msg = clamped_refusal("olmo", Some(8.0)).expect("refused");
assert!(msg.contains("olmo.attention.clamp_kqv"), "{msg}");
assert!(msg.contains("llama-graph.cpp:1611-1652"), "{msg}");
assert!(msg.contains('8'), "{msg}");
}
}