pub(super) fn llama_dots_this_against_q8k(kind: &str) -> bool {
matches!(
kind,
"Q2K"
| "Q3K"
| "Q4K"
| "Q5K"
| "Q6K"
| "IQ2XXS"
| "IQ2XS"
| "IQ2S"
| "IQ3XXS"
| "IQ3S"
| "IQ1S"
| "IQ1M"
| "IQ4XS"
)
}
pub(super) fn body_quant(tensors: &[frink_gguf::TensorInfo]) -> Option<String> {
use std::collections::HashMap;
let mut counts: HashMap<String, usize> = HashMap::new();
for t in tensors {
if t.name == "output.weight" || t.name == "token_embd.weight" {
continue;
}
let kind = format!("{:?}", t.dtype);
if kind != "F32" && kind != "F16" && kind != "BF16" {
*counts.entry(kind).or_default() += 1;
}
}
counts.into_iter().max_by_key(|(_, n)| *n).map(|(k, _)| k)
}
pub(super) fn lm_head_quant(tensors: &[frink_gguf::TensorInfo]) -> Option<String> {
tensors
.iter()
.find(|t| t.name == "output.weight")
.or_else(|| tensors.iter().find(|t| t.name == "token_embd.weight"))
.map(|t| format!("{:?}", t.dtype))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct DominantQuant(Option<String>);
impl DominantQuant {
pub(super) fn of(tensors: &[frink_gguf::TensorInfo]) -> Self {
Self::weigh(
lm_head_quant(tensors).as_deref(),
body_quant(tensors).as_deref(),
)
}
pub(super) fn weigh(lm_head: Option<&str>, body: Option<&str>) -> Self {
let picked = if body.is_some_and(llama_dots_this_against_q8k) {
body
} else {
lm_head.or(body)
};
Self(picked.map(str::to_owned))
}
pub(super) fn label(&self) -> Option<&str> {
self.0.as_deref()
}
pub(super) fn q8k_dotted(&self) -> bool {
self.0.as_deref().is_some_and(llama_dots_this_against_q8k)
}
}
#[cfg(test)]
pub(super) mod tests {
use super::*;
pub(in crate::parity) const Q8K_DOTTED: &[&str] = &[
"Q2K", "Q3K", "Q4K", "Q5K", "Q6K", "IQ2XXS", "IQ2XS", "IQ2S", "IQ3XXS", "IQ3S", "IQ1S",
"IQ1M", "IQ4XS",
];
pub(in crate::parity) const Q8_0_DOTTED: &[&str] = &["Q8_0", "Q4_0", "Q5_0", "IQ4NL"];
pub(super) fn tensor(name: &str, dtype: frink_gguf::GgmlType) -> frink_gguf::TensorInfo {
frink_gguf::TensorInfo {
name: name.to_string(),
shape: vec![1],
dtype,
offset: 0,
}
}
#[test]
fn a_q8_0_head_over_a_kquant_body_is_judged_as_the_kquant_it_is() {
for body in Q8K_DOTTED {
let q = DominantQuant::weigh(Some("Q8_0"), Some(body));
assert!(
q.q8k_dotted(),
"a {body} body dots against Q8_K activations whatever the output head is"
);
assert_eq!(q.label(), Some(*body), "the body is what the report names");
}
assert_eq!(
DominantQuant::weigh(Some("Q6K"), Some("Q4K")).label(),
Some("Q4K"),
"with a K-quant on both ends the report names the body"
);
}
#[test]
fn a_kquant_head_over_a_q8_0_body_still_relaxes_rather_than_inventing_a_wrong() {
let q = DominantQuant::weigh(Some("Q6K"), Some("Q8_0"));
assert!(q.q8k_dotted());
assert_eq!(q.label(), Some("Q6K"));
}
#[test]
fn the_body_is_read_off_the_layers_and_the_output_head_does_not_vote_in_it() {
use frink_gguf::GgmlType;
let table = vec![
tensor("token_embd.weight", GgmlType::Q8_0),
tensor("output.weight", GgmlType::Q8_0),
tensor("blk.0.attn_q.weight", GgmlType::Q4K),
tensor("blk.0.attn_norm.weight", GgmlType::F32),
];
assert_eq!(body_quant(&table).as_deref(), Some("Q4K"));
assert_eq!(lm_head_quant(&table).as_deref(), Some("Q8_0"));
let q = DominantQuant::of(&table);
assert_eq!(
q.label(),
Some("Q4K"),
"a Q8_0 head does not hide a Q4_K body"
);
assert!(q.q8k_dotted());
let tied = vec![
tensor("token_embd.weight", GgmlType::Q8_0),
tensor("blk.0.ffn_up.weight", GgmlType::Q4K),
];
assert_eq!(body_quant(&tied).as_deref(), Some("Q4K"));
assert!(DominantQuant::of(&tied).q8k_dotted());
assert_eq!(DominantQuant::of(&[]).label(), None);
assert!(!DominantQuant::of(&[]).q8k_dotted());
}
#[test]
fn the_q8k_predicate_uses_the_dtype_debug_spelling() {
for kind in Q8K_DOTTED {
assert!(
llama_dots_this_against_q8k(kind),
"{kind} declares vec_dot_type = Q8_K in ggml-cpu.c"
);
}
for wrong in ["Q4_K", "Q6_K", "IQ4_XS"] {
assert!(
!llama_dots_this_against_q8k(wrong),
"{wrong} is ggml's spelling, not GgmlType's -- if this now matches, the \
predicate is accepting both and the next reader cannot tell which is real"
);
}
for q8_0_dotted in Q8_0_DOTTED {
assert!(!llama_dots_this_against_q8k(q8_0_dotted));
}
}
#[test]
fn iq4_xs_is_q8k_dotted_and_iq4_nl_is_not() {
assert!(
llama_dots_this_against_q8k("IQ4XS"),
"ggml declares vec_dot_type = Q8_K for IQ4_XS, so its drift is expected"
);
assert!(
!llama_dots_this_against_q8k("IQ4NL"),
"ggml declares vec_dot_type = Q8_0 for IQ4_NL, so a drift there is NOT \
excused by §10 and would be a real finding"
);
}
}