hex/src/transcription/gguf_header.rs
Dax b65f9880e0
Labs: smaller model files (K-quant imports, macOS) (#1786)
Adds Labs › Smaller model files (k-quant-models, labs.k_quant_models, off by
default, macOS only). While on, an imported speech model may use Q6_K, Q5_K,
and Q4_K weights beside F32, F16, and Q8_0: Metal has their mul_mat and
get_rows kernels, while the Linux and Windows Vulkan builds embed shaders
only for the catalog's types and would abort, so the lab is not listed there.

The tensor-type decision is passed into the header check
(gguf_header::TensorTypes) instead of read inside it, and the import's
manifest records `k_quants` (serde default false, so older manifests load).
That lets the selection gate a K-quant import exactly as Parakeet Ultra is
gated: its availability folds in the lab, the listener falls back with
FallbackReason::LabsOff and keeps the saved choice, and `hex model use`
refuses it by name.

The header check now also sizes the K-quant tensors from ggml's block sizes
(256 values in 144, 176, and 210 bytes), so a cut-short file is refused and
the GPU-memory estimate counts them, and a found file's compression reads
Q6_K/Q5_K/Q4_K instead of F32.

Off, every OS keeps the F32, F16, and Q8_0 rule and its message byte for
byte.
2026-11-13 10:11:35 -05:00

502 lines
18 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Reads a GGUF file's header (metadata and tensor table) without loading
//! any weights, so an imported speech model is checked before transcribe.cpp
//! ever sees it. Every count and length is bounded: the file comes from the
//! person's disk and may be anything.
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{BufReader, Read, Seek, SeekFrom};
use std::path::Path;
use color_eyre::eyre::{Result, bail, eyre};
const MAGIC: &[u8; 4] = b"GGUF";
const MAX_KEYS: u64 = 1 << 20;
const MAX_TENSORS: u64 = 1 << 20;
const MAX_STRING: u64 = 1 << 24;
const MAX_ARRAY: u64 = 1 << 26;
const MAX_DIMS: u32 = 8;
const DEFAULT_ALIGNMENT: u64 = 32;
/// The ggml tensor types every Hex build runs on Metal and on Vulkan (the
/// Linux and Windows builds embed Vulkan shaders for these alone).
pub(crate) const SUPPORTED_TENSOR_TYPES: [(u32, &str); 3] = [(0, "F32"), (1, "F16"), (8, "Q8_0")];
/// The ggml K-quant types, which Metal's kernels run and the Vulkan builds
/// have no shaders for (Labs › Smaller model files, macOS). Their IDs are
/// `GGML_TYPE_Q4_K`, `GGML_TYPE_Q5_K`, and `GGML_TYPE_Q6_K`.
pub(crate) const K_QUANT_TENSOR_TYPES: [(u32, &str); 3] =
[(12, "Q4_K"), (13, "Q5_K"), (14, "Q6_K")];
/// The tensor types a caller can run, decided by its caller rather than read
/// from settings here: an import is checked against what the machine it runs
/// on supports.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub(crate) struct TensorTypes {
/// Whether Q4_K, Q5_K, and Q6_K run too (Labs › Smaller model files).
pub(crate) k_quants: bool,
}
impl TensorTypes {
/// F32, F16, and Q8_0: every Hex build on Metal and Vulkan.
pub(crate) const STANDARD: Self = Self { k_quants: false };
/// Also the K quants, where the GPU backend has their kernels.
pub(crate) const WITH_K_QUANTS: Self = Self { k_quants: true };
/// Whether a tensor of `kind` can run.
fn allows(self, kind: u32) -> bool {
let listed = |types: &[(u32, &str)]| types.iter().any(|(id, _)| *id == kind);
listed(&SUPPORTED_TENSOR_TYPES) || (self.k_quants && listed(&K_QUANT_TENSOR_TYPES))
}
/// The types as an import's refusal reads them.
pub(crate) const fn names(self) -> &'static str {
if self.k_quants {
"F32, F16, Q8_0, Q4_K, Q5_K, and Q6_K"
} else {
"F32, F16, and Q8_0"
}
}
/// Where those types run, as an import's refusal reads it. The K quants
/// are macOS only (Metal), so that refusal speaks about this Mac rather
/// than backends the person may not be using.
pub(crate) const fn platform(self) -> &'static str {
if self.k_quants {
"on this Mac"
} else {
"on Metal and Vulkan"
}
}
}
/// What an import needs from the header.
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct GgufHeader {
pub(crate) version: u32,
pub(crate) strings: BTreeMap<String, String>,
pub(crate) bools: BTreeMap<String, bool>,
pub(crate) string_arrays: BTreeMap<String, Vec<String>>,
/// Tensor count per ggml type id.
pub(crate) tensor_types: BTreeMap<u32, usize>,
/// Where the tensor data ends: the file must be at least this long.
pub(crate) data_end: u64,
/// The tensor data's size: what loading the weights takes, roughly.
pub(crate) tensor_bytes: u64,
}
impl GgufHeader {
pub(crate) fn string(&self, key: &str) -> Option<&str> {
self.strings
.get(key)
.map(String::as_str)
.filter(|value| !value.trim().is_empty())
}
pub(crate) fn bool(&self, key: &str) -> bool {
self.bools.get(key).copied().unwrap_or(false)
}
/// Tensor types outside `types`, by name, with counts.
pub(crate) fn unsupported_tensor_types(&self, types: TensorTypes) -> Vec<String> {
self.tensor_types
.iter()
.filter(|(kind, _)| !types.allows(**kind))
.map(|(kind, count)| format!("{} ({count})", type_name(*kind)))
.collect()
}
/// Whether any tensor uses a K-quant type, which needs Labs › Smaller
/// model files to run.
pub(crate) fn uses_k_quants(&self) -> bool {
K_QUANT_TENSOR_TYPES
.iter()
.any(|(kind, _)| self.tensor_types.contains_key(kind))
}
}
/// Reads the header of the GGUF at `path`.
pub(crate) fn read(path: &Path) -> Result<GgufHeader> {
let file = File::open(path)?;
let length = file.metadata()?.len();
let mut reader = Reader {
inner: BufReader::new(file),
};
let header = reader.header()?;
if header.data_end > length {
bail!(
"the file is cut short: its tensors need {} bytes, it has {length}",
header.data_end
);
}
Ok(header)
}
struct Reader<R> {
inner: R,
}
impl<R: Read + Seek> Reader<R> {
fn header(&mut self) -> Result<GgufHeader> {
let mut magic = [0_u8; 4];
self.inner
.read_exact(&mut magic)
.map_err(|_| eyre!("it is not a GGUF file"))?;
if &magic != MAGIC {
bail!("it is not a GGUF file");
}
let version = self.u32()?;
if !(2..=3).contains(&version) {
bail!("it is GGUF version {version}; Hex reads versions 2 and 3");
}
let tensors = self.u64()?;
let keys = self.u64()?;
if tensors > MAX_TENSORS || keys > MAX_KEYS {
bail!("its header is too large to be a speech model");
}
let mut header = GgufHeader {
version,
..Default::default()
};
let mut alignment = DEFAULT_ALIGNMENT;
for _ in 0..keys {
let key = self.string()?;
let kind = self.u32()?;
match kind {
8 => {
let value = self.string()?;
header.strings.insert(key, value);
}
7 => {
let value = self.u8()? != 0;
header.bools.insert(key, value);
}
4 if key == "general.alignment" => {
alignment = u64::from(self.u32()?);
}
9 => {
let item = self.u32()?;
let count = self.u64()?;
if count > MAX_ARRAY {
bail!("its metadata {key} is too long");
}
if item == 8 {
let mut values = Vec::new();
for _ in 0..count {
values.push(self.string()?);
}
header.string_arrays.insert(key, values);
} else {
for _ in 0..count {
self.skip_value(item)?;
}
}
}
kind => self.skip_value(kind)?,
}
}
if alignment == 0 || !alignment.is_power_of_two() {
bail!("its general.alignment ({alignment}) is invalid");
}
let mut extent = 0_u64;
for _ in 0..tensors {
let name = self.string()?;
let dimensions = self.u32()?;
if dimensions == 0 || dimensions > MAX_DIMS {
bail!("tensor {name} has {dimensions} dimensions");
}
let mut elements = 1_u64;
for _ in 0..dimensions {
elements = elements
.checked_mul(self.u64()?)
.ok_or_else(|| eyre!("tensor {name} is too large"))?;
}
let kind = self.u32()?;
let offset = self.u64()?;
*header.tensor_types.entry(kind).or_default() += 1;
if let Some(bytes) = tensor_bytes(kind, elements) {
extent = extent.max(
offset
.checked_add(bytes)
.ok_or_else(|| eyre!("tensor {name} is out of range"))?,
);
}
}
let position = self.inner.stream_position()?;
let data_start = position.div_ceil(alignment) * alignment;
header.data_end = data_start + extent;
header.tensor_bytes = extent;
Ok(header)
}
fn skip_value(&mut self, kind: u32) -> Result<()> {
let width = match kind {
0 | 1 | 7 => 1,
2 | 3 => 2,
4..=6 => 4,
10..=12 => 8,
8 => {
let length = self.u64()?;
if length > MAX_STRING {
bail!("its metadata holds a string too long to read");
}
return self.skip(length);
}
9 => {
let item = self.u32()?;
let count = self.u64()?;
if count > MAX_ARRAY {
bail!("its metadata holds an array too long to read");
}
for _ in 0..count {
self.skip_value(item)?;
}
return Ok(());
}
kind => bail!("its metadata has an unknown value type {kind}"),
};
self.skip(width)
}
fn skip(&mut self, bytes: u64) -> Result<()> {
let skip = i64::try_from(bytes).map_err(|_| eyre!("its header is corrupt"))?;
self.inner.seek(SeekFrom::Current(skip))?;
Ok(())
}
fn string(&mut self) -> Result<String> {
let length = self.u64()?;
if length > MAX_STRING {
bail!("its header holds a string too long to read");
}
let mut bytes = vec![0_u8; usize::try_from(length)?];
self.inner
.read_exact(&mut bytes)
.map_err(|_| eyre!("its header is cut short"))?;
String::from_utf8(bytes).map_err(|_| eyre!("its header holds text that isn't UTF-8"))
}
fn u8(&mut self) -> Result<u8> {
Ok(self.array::<1>()?[0])
}
fn u32(&mut self) -> Result<u32> {
Ok(u32::from_le_bytes(self.array()?))
}
fn u64(&mut self) -> Result<u64> {
Ok(u64::from_le_bytes(self.array()?))
}
fn array<const N: usize>(&mut self) -> Result<[u8; N]> {
let mut bytes = [0_u8; N];
self.inner
.read_exact(&mut bytes)
.map_err(|_| eyre!("its header is cut short"))?;
Ok(bytes)
}
}
/// Bytes a tensor of `kind` with `elements` takes, for the types Hex runs.
fn tensor_bytes(kind: u32, elements: u64) -> Option<u64> {
match kind {
0 => elements.checked_mul(4),
1 => elements.checked_mul(2),
// 32 values in 34 bytes: an f16 scale and 32 int8s.
8 => elements.div_ceil(32).checked_mul(34),
// 256 values in one K super-block, sized by ggml-common.h's
// static_asserts: q4_K 144 bytes, q5_K 176, q6_K 210.
12 => elements.div_ceil(256).checked_mul(144),
13 => elements.div_ceil(256).checked_mul(176),
14 => elements.div_ceil(256).checked_mul(210),
_ => None,
}
}
fn type_name(kind: u32) -> String {
const NAMES: [&str; 31] = [
"F32", "F16", "Q4_0", "Q4_1", "Q4_2", "Q4_3", "Q5_0", "Q5_1", "Q8_0", "Q8_1", "Q2_K",
"Q3_K", "Q4_K", "Q5_K", "Q6_K", "Q8_K", "IQ2_XXS", "IQ2_XS", "IQ3_XXS", "IQ1_S", "IQ4_NL",
"IQ3_S", "IQ2_S", "IQ4_XS", "I8", "I16", "I32", "I64", "F64", "IQ1_M", "BF16",
];
NAMES
.get(kind as usize)
.map_or_else(|| format!("type {kind}"), |name| (*name).to_owned())
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
/// A minimal GGUF: string metadata, string-array metadata, bools, and
/// one tensor per `(name, type, elements)`, with the data it needs.
pub(crate) fn gguf(
strings: &[(&str, &str)],
arrays: &[(&str, &[&str])],
bools: &[(&str, bool)],
tensors: &[(&str, u32, u64)],
) -> Vec<u8> {
fn string(out: &mut Vec<u8>, text: &str) {
out.extend((text.len() as u64).to_le_bytes());
out.extend(text.as_bytes());
}
let mut out = Vec::new();
out.extend(MAGIC);
out.extend(3_u32.to_le_bytes());
out.extend((tensors.len() as u64).to_le_bytes());
out.extend(((strings.len() + arrays.len() + bools.len() + 1) as u64).to_le_bytes());
// An unread u64 value the reader has to skip.
string(&mut out, "general.file_type");
out.extend(10_u32.to_le_bytes());
out.extend(7_u64.to_le_bytes());
for (key, value) in strings {
string(&mut out, key);
out.extend(8_u32.to_le_bytes());
string(&mut out, value);
}
for (key, values) in arrays {
string(&mut out, key);
out.extend(9_u32.to_le_bytes());
out.extend(8_u32.to_le_bytes());
out.extend((values.len() as u64).to_le_bytes());
for value in *values {
string(&mut out, value);
}
}
for (key, value) in bools {
string(&mut out, key);
out.extend(7_u32.to_le_bytes());
out.push(u8::from(*value));
}
let mut offset = 0_u64;
for (name, kind, elements) in tensors {
string(&mut out, name);
out.extend(1_u32.to_le_bytes());
out.extend(elements.to_le_bytes());
out.extend(kind.to_le_bytes());
out.extend(offset.to_le_bytes());
offset += tensor_bytes(*kind, *elements).unwrap_or(4).div_ceil(32) * 32;
}
out.resize(out.len().div_ceil(32) * 32 + offset as usize, 0);
out
}
pub(crate) fn read_header(bytes: Vec<u8>) -> GgufHeader {
read_bytes(bytes).unwrap()
}
fn read_bytes(bytes: Vec<u8>) -> Result<GgufHeader> {
let length = bytes.len() as u64;
let header = Reader {
inner: std::io::Cursor::new(bytes),
}
.header()?;
if header.data_end > length {
bail!("cut short");
}
Ok(header)
}
#[test]
fn metadata_and_tensor_types_are_read_without_the_weights() {
let header = read_bytes(gguf(
&[
("general.architecture", "whisper"),
("stt.variant", "whisper-small"),
],
&[("general.languages", &["en", "de"])],
&[("stt.capability.lang_detect", true)],
&[("a", 0, 8), ("b", 1, 8), ("c", 8, 64)],
))
.unwrap();
assert_eq!(header.string("general.architecture"), Some("whisper"));
assert_eq!(header.string_arrays["general.languages"], ["en", "de"]);
assert!(header.bool("stt.capability.lang_detect"));
assert!(!header.bool("stt.capability.streaming"));
assert_eq!(header.tensor_types, [(0, 1), (1, 1), (8, 1)].into());
assert!(
header
.unsupported_tensor_types(TensorTypes::STANDARD)
.is_empty()
);
assert!(!header.uses_k_quants());
}
#[test]
fn other_tensor_types_are_named() {
let header = read_bytes(gguf(&[], &[], &[], &[("a", 12, 256), ("b", 12, 256)])).unwrap();
assert_eq!(
header.unsupported_tensor_types(TensorTypes::STANDARD),
["Q4_K (2)"]
);
}
#[test]
fn k_quants_are_known_only_where_the_caller_runs_them() {
let header = read_bytes(gguf(
&[],
&[],
&[],
&[("a", 12, 256), ("b", 13, 256), ("c", 14, 512), ("d", 8, 64)],
))
.unwrap();
assert!(header.uses_k_quants());
assert_eq!(
header.unsupported_tensor_types(TensorTypes::STANDARD),
["Q4_K (1)", "Q5_K (1)", "Q6_K (1)"]
);
assert!(
header
.unsupported_tensor_types(TensorTypes::WITH_K_QUANTS)
.is_empty()
);
// A type neither list holds stays refused either way.
let other = read_bytes(gguf(&[], &[], &[], &[("a", 6, 256)])).unwrap();
assert_eq!(
other.unsupported_tensor_types(TensorTypes::WITH_K_QUANTS),
["Q5_0 (1)"]
);
assert_eq!(TensorTypes::STANDARD.names(), "F32, F16, and Q8_0");
assert_eq!(
TensorTypes::WITH_K_QUANTS.names(),
"F32, F16, Q8_0, Q4_K, Q5_K, and Q6_K"
);
}
#[test]
fn k_quant_tensors_are_measured_in_their_ggml_block_sizes() {
// ggml-common.h: one super-block of 256 values per 144, 176, and 210
// bytes, and a partly filled block still takes a whole one.
assert_eq!(tensor_bytes(12, 256), Some(144));
assert_eq!(tensor_bytes(13, 256), Some(176));
assert_eq!(tensor_bytes(14, 256), Some(210));
assert_eq!(tensor_bytes(14, 257), Some(420));
assert_eq!(tensor_bytes(6, 256), None);
// A K-quant file's tensor data counts, so a cut-short file is refused.
// (The fixture pads to 32 bytes, so drop a whole block of padding.)
let mut cut = gguf(&[], &[], &[], &[("a", 14, 256)]);
cut.truncate(cut.len() - 32);
assert!(read_bytes(cut).is_err());
}
#[test]
fn junk_and_truncated_files_are_refused() {
let junk = read_bytes(b"PK\x03\x04 not a model".to_vec()).unwrap_err();
assert_eq!(junk.to_string(), "it is not a GGUF file");
let mut cut = gguf(
&[("general.architecture", "parakeet")],
&[],
&[],
&[("a", 0, 1024)],
);
cut.truncate(cut.len() - 100);
assert!(read_bytes(cut).is_err());
let mut header = gguf(&[("general.architecture", "parakeet")], &[], &[], &[]);
header.truncate(30);
assert_eq!(
read_bytes(header).unwrap_err().to_string(),
"its header is cut short"
);
}
}