use std::collections::HashMap;
use std::fs::File;
use std::io::{BufReader, Read, Seek};
use std::path::Path;
use anyhow::{bail, Context};
#[derive(Debug, Clone)]
pub enum Value {
U8(u8),
I8(i8),
U16(u16),
I16(i16),
U32(u32),
I32(i32),
F32(f32),
Bool(bool),
String(String),
Array(Vec<Value>),
LargeArray(u64),
U64(u64),
I64(i64),
F64(f64),
}
fn fixed_size(ty: u32) -> Option<i64> {
match ty {
0 | 1 | 7 => Some(1),
2 | 3 => Some(2),
4..=6 => Some(4),
10..=12 => Some(8),
_ => None,
}
}
pub const MAX_ARRAY_ELEMENTS: usize = 1024;
const MAX_DEPTH: u32 = 8;
const MAX_STRING_BYTES: u64 = 64 * 1024 * 1024;
const MAX_ARRAY_LEN: u64 = 10_000_000;
fn check_depth(depth: u32) -> anyhow::Result<()> {
if depth > MAX_DEPTH {
bail!("GGUF value nesting too deep (> {MAX_DEPTH} levels)");
}
Ok(())
}
impl Value {
pub fn as_str(&self) -> Option<&str> {
match self {
Value::String(s) => Some(s),
_ => None,
}
}
pub fn as_u64(&self) -> Option<u64> {
match *self {
Value::U8(v) => Some(v as u64),
Value::U16(v) => Some(v as u64),
Value::U32(v) => Some(v as u64),
Value::U64(v) => Some(v),
Value::I8(v) if v >= 0 => Some(v as u64),
Value::I16(v) if v >= 0 => Some(v as u64),
Value::I32(v) if v >= 0 => Some(v as u64),
Value::I64(v) if v >= 0 => Some(v as u64),
_ => None,
}
}
pub fn as_bool(&self) -> Option<bool> {
match self {
Value::Bool(b) => Some(*b),
_ => None,
}
}
}
#[derive(Debug, Clone)]
struct TensorInfo {
#[allow(dead_code)]
name: String,
dims: Vec<u64>,
ggml_type: u32,
}
#[derive(Debug, Clone, Default)]
pub struct Info {
pub metadata: HashMap<String, Value>,
pub tensor_count: u64,
pub parameter_count: u64,
pub quantization: Option<String>,
}
impl Info {
pub fn str(&self, key: &str) -> Option<&str> {
self.metadata.get(key).and_then(Value::as_str)
}
pub fn u64(&self, key: &str) -> Option<u64> {
self.metadata.get(key).and_then(Value::as_u64)
}
pub fn architecture(&self) -> Option<&str> {
self.str("general.architecture")
}
pub fn context_length(&self) -> Option<u64> {
let arch = self.architecture()?;
self.u64(&format!("{arch}.context_length"))
}
pub fn embedding_length(&self) -> Option<u64> {
let arch = self.architecture()?;
self.u64(&format!("{arch}.embedding_length"))
}
pub fn is_embedding(&self) -> bool {
self.architecture()
.is_some_and(|arch| self.u64(&format!("{arch}.pooling_type")).is_some())
}
pub fn is_decision(&self) -> bool {
self.architecture().is_some_and(|arch| {
self.str(&format!("{arch}.decision.type"))
.is_some_and(|kind| !kind.is_empty())
})
}
pub fn is_speech(&self) -> bool {
self.architecture() == Some("canary")
}
pub fn block_count(&self) -> Option<u64> {
let arch = self.architecture()?;
self.u64(&format!("{arch}.block_count"))
}
pub fn projector_caps(&self) -> ProjectorCaps {
let flag = |key: &str| self.metadata.get(key).and_then(Value::as_bool) == Some(true);
ProjectorCaps {
vision: flag("clip.has_vision_encoder"),
audio: flag("clip.has_audio_encoder"),
gen_audio: flag("clip.has_gen_audio_encoder"),
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ProjectorCaps {
pub vision: bool,
pub audio: bool,
pub gen_audio: bool,
}
struct Reader<R> {
inner: BufReader<R>,
max_array_elements: u64,
}
impl<R: Read + Seek> Reader<R> {
fn u8(&mut self) -> anyhow::Result<u8> {
let mut b = [0u8; 1];
self.inner.read_exact(&mut b)?;
Ok(b[0])
}
fn i8(&mut self) -> anyhow::Result<i8> {
Ok(self.u8()? as i8)
}
fn bool_(&mut self) -> anyhow::Result<bool> {
Ok(self.u8()? != 0)
}
fn u16(&mut self) -> anyhow::Result<u16> {
let mut b = [0u8; 2];
self.inner.read_exact(&mut b)?;
Ok(u16::from_le_bytes(b))
}
fn i16(&mut self) -> anyhow::Result<i16> {
let mut b = [0u8; 2];
self.inner.read_exact(&mut b)?;
Ok(i16::from_le_bytes(b))
}
fn u32(&mut self) -> anyhow::Result<u32> {
let mut b = [0u8; 4];
self.inner.read_exact(&mut b)?;
Ok(u32::from_le_bytes(b))
}
fn i32(&mut self) -> anyhow::Result<i32> {
let mut b = [0u8; 4];
self.inner.read_exact(&mut b)?;
Ok(i32::from_le_bytes(b))
}
fn f32(&mut self) -> anyhow::Result<f32> {
let mut b = [0u8; 4];
self.inner.read_exact(&mut b)?;
Ok(f32::from_le_bytes(b))
}
fn u64(&mut self) -> anyhow::Result<u64> {
let mut b = [0u8; 8];
self.inner.read_exact(&mut b)?;
Ok(u64::from_le_bytes(b))
}
fn i64(&mut self) -> anyhow::Result<i64> {
let mut b = [0u8; 8];
self.inner.read_exact(&mut b)?;
Ok(i64::from_le_bytes(b))
}
fn f64(&mut self) -> anyhow::Result<f64> {
let mut b = [0u8; 8];
self.inner.read_exact(&mut b)?;
Ok(f64::from_le_bytes(b))
}
fn string(&mut self) -> anyhow::Result<String> {
let len = self.string_len()?;
let mut buf = vec![0u8; len as usize];
self.inner.read_exact(&mut buf)?;
Ok(String::from_utf8_lossy(&buf).into_owned())
}
fn string_len(&mut self) -> anyhow::Result<u64> {
let len = self.u64()?;
if len > MAX_STRING_BYTES {
bail!("implausible GGUF string length: {len}");
}
Ok(len)
}
fn array_header(&mut self) -> anyhow::Result<(u32, u64)> {
let elem_ty = self.u32()?;
let len = self.u64()?;
if len > MAX_ARRAY_LEN {
bail!("implausible GGUF array length: {len}");
}
Ok((elem_ty, len))
}
fn value(&mut self, ty: u32) -> anyhow::Result<Value> {
self.value_at(ty, 0)
}
fn skip_value_at(&mut self, ty: u32, depth: u32) -> anyhow::Result<()> {
check_depth(depth)?;
if let Some(size) = fixed_size(ty) {
self.inner.seek_relative(size)?;
return Ok(());
}
match ty {
8 => {
let len = self.string_len()?;
self.inner.seek_relative(len as i64)?;
}
9 => {
let (elem_ty, len) = self.array_header()?;
self.skip_elements(elem_ty, len, depth + 1)?;
}
other => bail!("unknown GGUF value type: {other}"),
}
Ok(())
}
fn skip_elements(&mut self, ty: u32, len: u64, depth: u32) -> anyhow::Result<()> {
if len > 0 {
check_depth(depth)?;
}
match fixed_size(ty) {
Some(size) => self.inner.seek_relative(size * len as i64)?,
None => {
for _ in 0..len {
self.skip_value_at(ty, depth)?;
}
}
}
Ok(())
}
fn value_at(&mut self, ty: u32, depth: u32) -> anyhow::Result<Value> {
check_depth(depth)?;
Ok(match ty {
0 => Value::U8(self.u8()?),
1 => Value::I8(self.i8()?),
2 => Value::U16(self.u16()?),
3 => Value::I16(self.i16()?),
4 => Value::U32(self.u32()?),
5 => Value::I32(self.i32()?),
6 => Value::F32(self.f32()?),
7 => Value::Bool(self.bool_()?),
8 => Value::String(self.string()?),
9 => {
let (elem_ty, len) = self.array_header()?;
if len > self.max_array_elements {
self.skip_elements(elem_ty, len, depth + 1)?;
return Ok(Value::LargeArray(len));
}
let mut items = Vec::with_capacity(len.min(1024) as usize);
for _ in 0..len {
items.push(self.value_at(elem_ty, depth + 1)?);
}
Value::Array(items)
}
10 => Value::U64(self.u64()?),
11 => Value::I64(self.i64()?),
12 => Value::F64(self.f64()?),
other => bail!("unknown GGUF value type: {other}"),
})
}
}
pub fn read_info(path: &Path) -> anyhow::Result<Info> {
read_info_up_to(path, MAX_ARRAY_ELEMENTS as u64)
}
pub fn read_info_full(path: &Path) -> anyhow::Result<Info> {
read_info_up_to(path, u64::MAX)
}
fn read_info_up_to(path: &Path, max_array_elements: u64) -> anyhow::Result<Info> {
let file = File::open(path).with_context(|| format!("open {}", path.display()))?;
let file_len = file.metadata()?.len();
let mut r = Reader {
inner: BufReader::new(file),
max_array_elements,
};
let mut magic = [0u8; 4];
r.inner.read_exact(&mut magic).context("read GGUF magic")?;
if &magic != b"GGUF" {
bail!("not a GGUF file: {}", path.display());
}
let version = r.u32().context("read GGUF version")?;
if version < 2 {
bail!("unsupported GGUF version {version} (only v2+ is supported)");
}
let tensor_count = r.u64().context("read tensor_count")?;
let metadata_kv_count = r.u64().context("read metadata_kv_count")?;
let mut metadata = HashMap::with_capacity(metadata_kv_count.min(4096) as usize);
for _ in 0..metadata_kv_count {
let key = r.string().context("read metadata key")?;
let value_type = r.u32().context("read metadata value type")?;
let value = r
.value(value_type)
.with_context(|| format!("read metadata value for {key:?}"))?;
metadata.insert(key, value);
}
let mut tensors = Vec::with_capacity(tensor_count.min(65536) as usize);
for _ in 0..tensor_count {
let name = r.string().context("read tensor name")?;
let n_dims = r.u32().context("read tensor n_dims")?;
if n_dims > 8 {
bail!("implausible tensor rank: {n_dims}");
}
let mut dims = Vec::with_capacity(n_dims as usize);
for _ in 0..n_dims {
dims.push(r.u64().context("read tensor dim")?);
}
let ggml_type = r.u32().context("read tensor type")?;
let _offset = r.u64().context("read tensor offset")?;
tensors.push(TensorInfo {
name,
dims,
ggml_type,
});
}
let end = r.inner.stream_position()?;
if end > file_len {
bail!("GGUF header runs past the end of the file ({end} > {file_len} bytes)");
}
let parameter_count = tensors
.iter()
.map(|t| {
t.dims
.iter()
.fold(1u128, |acc, &d| acc.saturating_mul(d as u128))
})
.fold(0u128, |acc, n| acc.saturating_add(n))
.min(u64::MAX as u128) as u64;
let quantization = dominant_quantization(&tensors);
Ok(Info {
metadata,
tensor_count,
parameter_count,
quantization,
})
}
pub fn file_type_name(ftype: u64) -> Option<&'static str> {
Some(match ftype {
0 => "F32",
1 => "F16",
2 => "Q4_0",
3 => "Q4_1",
7 => "Q8_0",
8 => "Q5_0",
9 => "Q5_1",
10 => "Q2_K",
11 => "Q3_K_S",
12 => "Q3_K_M",
13 => "Q3_K_L",
14 => "Q4_K_S",
15 => "Q4_K_M",
16 => "Q5_K_S",
17 => "Q5_K_M",
18 => "Q6_K",
19 => "IQ2_XXS",
20 => "IQ2_XS",
21 => "Q2_K_S",
22 => "IQ3_XS",
23 => "IQ3_XXS",
24 => "IQ1_S",
25 => "IQ4_NL",
26 => "IQ3_S",
27 => "IQ3_M",
28 => "IQ2_S",
29 => "IQ2_M",
30 => "IQ4_XS",
31 => "IQ1_M",
32 => "BF16",
36 => "TQ1_0",
37 => "TQ2_0",
38 => "MXFP4_MOE",
39 => "NVFP4",
40 => "Q1_0",
41 => "Q2_0",
_ => return None,
})
}
fn dominant_quantization(tensors: &[TensorInfo]) -> Option<String> {
let mut totals: HashMap<u32, u128> = HashMap::new();
for t in tensors {
if t.dims.len() < 2 {
continue;
}
let elems = t
.dims
.iter()
.fold(1u128, |acc, &d| acc.saturating_mul(d as u128));
let entry = totals.entry(t.ggml_type).or_insert(0);
*entry = entry.saturating_add(elems);
}
let (ty, _) = totals.into_iter().max_by_key(|(_, n)| *n)?;
Some(ggml_type_name(ty).to_string())
}
pub(crate) fn ggml_type_name(ty: u32) -> &'static str {
match ty {
0 => "F32",
1 => "F16",
2 => "Q4_0",
3 => "Q4_1",
6 => "Q5_0",
7 => "Q5_1",
8 => "Q8_0",
9 => "Q8_1",
10 => "Q2_K",
11 => "Q3_K",
12 => "Q4_K",
13 => "Q5_K",
14 => "Q6_K",
15 => "Q8_K",
16 => "IQ2_XXS",
17 => "IQ2_XS",
18 => "IQ3_XXS",
19 => "IQ1_S",
20 => "IQ4_NL",
21 => "IQ3_S",
22 => "IQ2_S",
23 => "IQ4_XS",
24 => "I8",
25 => "I16",
26 => "I32",
27 => "I64",
28 => "F64",
29 => "IQ1_M",
30 => "BF16",
34 => "TQ1_0",
35 => "TQ2_0",
_ => "unknown",
}
}
#[cfg(test)]
pub(crate) fn write_test_gguf_with(extra_u32: &[(&str, u32)]) -> std::path::PathBuf {
write_test_gguf_with_strings(extra_u32, &[])
}
#[cfg(test)]
pub(crate) fn write_test_gguf_with_strings(
extra_u32: &[(&str, u32)],
extra_str: &[(&str, &str)],
) -> std::path::PathBuf {
use std::io::Write;
fn write_string(buf: &mut Vec<u8>, s: &str) {
buf.extend_from_slice(&(s.len() as u64).to_le_bytes());
buf.extend_from_slice(s.as_bytes());
}
let mut buf = Vec::new();
buf.extend_from_slice(b"GGUF");
buf.extend_from_slice(&3u32.to_le_bytes()); buf.extend_from_slice(&1u64.to_le_bytes()); let kv_count = 2 + extra_u32.len() + extra_str.len();
buf.extend_from_slice(&(kv_count as u64).to_le_bytes());
write_string(&mut buf, "general.architecture");
buf.extend_from_slice(&8u32.to_le_bytes()); write_string(&mut buf, "llama");
write_string(&mut buf, "llama.context_length");
buf.extend_from_slice(&4u32.to_le_bytes()); buf.extend_from_slice(&4096u32.to_le_bytes());
for (key, value) in extra_u32 {
write_string(&mut buf, key);
buf.extend_from_slice(&4u32.to_le_bytes()); buf.extend_from_slice(&value.to_le_bytes());
}
for (key, value) in extra_str {
write_string(&mut buf, key);
buf.extend_from_slice(&8u32.to_le_bytes()); write_string(&mut buf, value);
}
write_string(&mut buf, "blk.0.weight");
buf.extend_from_slice(&2u32.to_le_bytes()); buf.extend_from_slice(&4u64.to_le_bytes());
buf.extend_from_slice(&8u64.to_le_bytes());
buf.extend_from_slice(&12u32.to_le_bytes()); buf.extend_from_slice(&0u64.to_le_bytes());
let path = std::env::temp_dir().join(format!(
"llmman-gguf-test-{}-{}.gguf",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let mut f = File::create(&path).unwrap();
f.write_all(&buf).unwrap();
path
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
fn write_test_gguf() -> std::path::PathBuf {
write_test_gguf_with(&[])
}
fn write_string(buf: &mut Vec<u8>, s: &str) {
buf.extend_from_slice(&(s.len() as u64).to_le_bytes());
buf.extend_from_slice(s.as_bytes());
}
#[test]
fn reads_metadata_and_computes_parameter_count_and_quantization() {
let path = write_test_gguf();
let info = read_info(&path).unwrap();
std::fs::remove_file(&path).ok();
assert_eq!(info.architecture(), Some("llama"));
assert_eq!(info.context_length(), Some(4096));
assert_eq!(info.tensor_count, 1);
assert_eq!(info.parameter_count, 32); assert_eq!(info.quantization, Some("Q4_K".to_string()));
}
#[test]
fn is_decision_needs_a_decision_type_under_the_architecture() {
let info = |strings: &[(&str, &str)]| {
let path = write_test_gguf_with_strings(&[], strings);
let info = read_info(&path).unwrap();
std::fs::remove_file(&path).ok();
info
};
assert!(info(&[("llama.decision.type", "clef")]).is_decision());
assert!(!info(&[]).is_decision());
assert!(!info(&[("llama.decision.type", "")]).is_decision());
assert!(!info(&[("clef.decision.type", "clef")]).is_decision());
}
#[test]
fn is_speech_keys_off_the_canary_architecture() {
let info = |strings: &[(&str, &str)]| {
let path = write_test_gguf_with_strings(&[], strings);
let info = read_info(&path).unwrap();
std::fs::remove_file(&path).ok();
info
};
assert!(info(&[("general.architecture", "canary")]).is_speech());
assert!(!info(&[("general.architecture", "llama")]).is_speech());
assert!(!info(&[]).is_speech());
}
#[test]
fn projector_caps_read_the_three_clip_encoder_flags() {
let info = |flags: &[(&str, bool)]| Info {
metadata: flags
.iter()
.map(|(key, on)| (key.to_string(), Value::Bool(*on)))
.collect(),
..Info::default()
};
assert_eq!(
info(&[
("clip.has_vision_encoder", true),
("clip.has_audio_encoder", true),
("clip.has_gen_audio_encoder", true),
])
.projector_caps(),
ProjectorCaps {
vision: true,
audio: true,
gen_audio: true
}
);
let tts = info(&[
("clip.has_gen_audio_encoder", true),
("clip.has_vision_encoder", false),
])
.projector_caps();
assert_eq!(
tts,
ProjectorCaps {
vision: false,
audio: false,
gen_audio: true
}
);
assert_eq!(info(&[]).projector_caps(), ProjectorCaps::default());
let mut odd = Info::default();
odd.metadata.insert(
"clip.has_vision_encoder".into(),
Value::String("true".into()),
);
assert_eq!(odd.projector_caps(), ProjectorCaps::default());
}
#[test]
fn read_info_does_not_overflow_when_summing_saturated_element_counts() {
let mut buf = Vec::new();
buf.extend_from_slice(b"GGUF");
buf.extend_from_slice(&3u32.to_le_bytes()); buf.extend_from_slice(&2u64.to_le_bytes()); buf.extend_from_slice(&0u64.to_le_bytes());
for name in ["blk.0.weight", "blk.1.weight"] {
write_string(&mut buf, name);
buf.extend_from_slice(&3u32.to_le_bytes()); for _ in 0..3 {
buf.extend_from_slice(&u64::MAX.to_le_bytes());
}
buf.extend_from_slice(&0u32.to_le_bytes()); buf.extend_from_slice(&0u64.to_le_bytes()); }
let path = std::env::temp_dir().join(format!(
"llmman-gguf-saturate-{}-{}.gguf",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let mut f = File::create(&path).unwrap();
f.write_all(&buf).unwrap();
let info = read_info(&path).unwrap();
std::fs::remove_file(&path).ok();
assert_eq!(info.parameter_count, u64::MAX);
assert_eq!(info.quantization, Some("F32".to_string()));
}
#[test]
fn file_type_name_keeps_the_mixed_quant_variant() {
assert_eq!(file_type_name(14), Some("Q4_K_S"));
assert_eq!(file_type_name(15), Some("Q4_K_M"));
assert_eq!(file_type_name(0), Some("F32"));
assert_eq!(file_type_name(32), Some("BF16"));
assert_eq!(file_type_name(33), None);
assert_eq!(file_type_name(1024), None);
}
#[test]
fn read_info_rejects_a_non_gguf_file() {
let path =
std::env::temp_dir().join(format!("llmman-gguf-nonmagic-{}.bin", std::process::id()));
std::fs::write(&path, b"not a gguf file at all").unwrap();
let err = read_info(&path).unwrap_err();
std::fs::remove_file(&path).ok();
assert!(err.to_string().contains("not a GGUF file"));
}
#[test]
fn ggml_type_name_covers_common_quantizations() {
assert_eq!(ggml_type_name(0), "F32");
assert_eq!(ggml_type_name(12), "Q4_K");
assert_eq!(ggml_type_name(14), "Q6_K");
assert_eq!(ggml_type_name(9999), "unknown");
}
fn gguf_bytes(kvs: &[(&str, u32, Vec<u8>)], close: bool) -> Vec<u8> {
let mut buf = Vec::new();
buf.extend_from_slice(b"GGUF");
buf.extend_from_slice(&3u32.to_le_bytes());
buf.extend_from_slice(&0u64.to_le_bytes()); buf.extend_from_slice(&(kvs.len() as u64 + 1 + u64::from(close)).to_le_bytes());
write_string(&mut buf, "general.architecture");
buf.extend_from_slice(&8u32.to_le_bytes());
write_string(&mut buf, "llama");
for (key, ty, value) in kvs {
write_string(&mut buf, key);
buf.extend_from_slice(&ty.to_le_bytes());
buf.extend_from_slice(value);
}
if close {
write_string(&mut buf, "llama.context_length");
buf.extend_from_slice(&4u32.to_le_bytes());
buf.extend_from_slice(&4096u32.to_le_bytes());
}
buf
}
fn write_temp_gguf(buf: &[u8]) -> tempfile::NamedTempFile {
let mut file = tempfile::NamedTempFile::new().unwrap();
file.write_all(buf).unwrap();
file
}
fn gguf_with_kvs(kvs: &[(&str, u32, Vec<u8>)]) -> tempfile::NamedTempFile {
write_temp_gguf(&gguf_bytes(kvs, true))
}
fn array_bytes(elem_ty: u32, len: u64, body: &[u8]) -> Vec<u8> {
let mut v = Vec::new();
v.extend_from_slice(&elem_ty.to_le_bytes());
v.extend_from_slice(&len.to_le_bytes());
v.extend_from_slice(body);
v
}
#[test]
fn read_info_skips_arrays_past_the_ceiling_and_stays_aligned() {
let n = MAX_ARRAY_ELEMENTS as u64 + 1;
let mut strings = Vec::new();
for i in 0..n {
write_string(&mut strings, &format!("token-{i}"));
}
let mut nested = Vec::new();
for _ in 0..n {
nested.extend_from_slice(&array_bytes(0, 2, &[7, 7]));
}
let file = gguf_with_kvs(&[
("tok.tokens", 9, array_bytes(8, n, &strings)),
("tok.types", 9, array_bytes(5, 5000, &vec![0u8; 5000 * 4])),
("tok.nested", 9, array_bytes(9, n, &nested)),
("tok.small", 9, array_bytes(4, 2, &[1, 0, 0, 0, 2, 0, 0, 0])),
(
"tok.exact",
9,
array_bytes(0, MAX_ARRAY_ELEMENTS as u64, &vec![0u8; MAX_ARRAY_ELEMENTS]),
),
("tok.after", 4, 99u32.to_le_bytes().to_vec()),
]);
let info = read_info(file.path()).unwrap();
assert!(matches!(info.metadata["tok.tokens"], Value::LargeArray(l) if l == n));
assert!(matches!(
info.metadata["tok.types"],
Value::LargeArray(5000)
));
assert!(matches!(info.metadata["tok.nested"], Value::LargeArray(l) if l == n));
assert!(
matches!(&info.metadata["tok.exact"], Value::Array(a) if a.len() == MAX_ARRAY_ELEMENTS)
);
match &info.metadata["tok.small"] {
Value::Array(a) => assert_eq!(a.len(), 2),
other => panic!("{other:?}"),
}
assert_eq!(info.u64("tok.after"), Some(99));
assert_eq!(info.context_length(), Some(4096), "the key after the skips");
}
#[test]
fn read_info_still_rejects_a_bad_array_it_would_skip() {
let n = MAX_ARRAY_ELEMENTS as u64 + 1;
let file = gguf_with_kvs(&[("tok.bad", 9, array_bytes(99, n, &[]))]);
let err = read_info(file.path()).unwrap_err();
assert!(
format!("{err:#}").contains("unknown GGUF value type"),
"{err:#}"
);
let mut body = Vec::new();
body.extend_from_slice(&(u64::MAX).to_le_bytes());
let file = gguf_with_kvs(&[("tok.bad", 9, array_bytes(8, n, &body))]);
let err = read_info(file.path()).unwrap_err();
assert!(
format!("{err:#}").contains("implausible GGUF string length"),
"{err:#}"
);
let file = gguf_with_kvs(&[("tok.bad", 9, array_bytes(0, 10_000_001, &[]))]);
let err = read_info(file.path()).unwrap_err();
assert!(
format!("{err:#}").contains("implausible GGUF array length"),
"{err:#}"
);
}
#[test]
fn read_info_full_reads_the_arrays_read_info_skips() {
let n = 16_384u64;
let mut strings = Vec::new();
for i in 0..n {
write_string(&mut strings, &format!("piece-{i}"));
}
let file = gguf_with_kvs(&[("tok.tokens", 9, array_bytes(8, n, &strings))]);
let skipped = read_info(file.path()).unwrap();
let full = read_info_full(file.path()).unwrap();
assert!(matches!(skipped.metadata["tok.tokens"], Value::LargeArray(l) if l == n));
match &full.metadata["tok.tokens"] {
Value::Array(a) => {
assert_eq!(a.len() as u64, n);
assert_eq!(a[16_383].as_str(), Some("piece-16383"));
}
other => panic!("{other:?}"),
}
assert_eq!(full.context_length(), Some(4096), "the key after it");
}
#[test]
fn read_info_rejects_a_header_that_ends_inside_a_skipped_array() {
let n = MAX_ARRAY_ELEMENTS as u64 + 1;
let one_short = |each: Vec<u8>, last: Vec<u8>| {
let mut body = each.repeat(n as usize - 1);
body.extend(last);
body
};
let mut a_string = Vec::new();
write_string(&mut a_string, "a");
let mut a_cut_string = 1000u64.to_le_bytes().to_vec(); a_cut_string.extend_from_slice(b"short");
let a_cut_array = array_bytes(4, 1_000_000, &[]);
let cut = [
("u32s", array_bytes(4, n, &[0u8; 8])),
(
"strings",
array_bytes(8, n, &one_short(a_string, a_cut_string)),
),
(
"arrays",
array_bytes(9, n, &one_short(array_bytes(0, 0, &[]), a_cut_array)),
),
];
for (what, value) in cut {
let file = write_temp_gguf(&gguf_bytes(&[("tok.cut", 9, value)], false));
let err = read_info(file.path());
let err = format!("{:#}", err.unwrap_err());
assert!(err.contains("past the end of the file"), "{what}: {err}");
}
let kv = (
"tok.whole",
9,
array_bytes(4, n, &vec![0u8; n as usize * 4]),
);
let file = write_temp_gguf(&gguf_bytes(&[kv], false));
let info = read_info(file.path()).unwrap();
assert!(matches!(info.metadata["tok.whole"], Value::LargeArray(l) if l == n));
}
#[test]
fn read_info_skips_an_empty_array_at_the_deepest_level() {
let mut chain = array_bytes(4, 0, &[]);
for _ in 1..8 {
chain = array_bytes(9, 1, &chain);
}
let n = MAX_ARRAY_ELEMENTS as u64 + 1;
let body: Vec<u8> = (0..n).flat_map(|_| chain.clone()).collect();
let file = gguf_with_kvs(&[("tok.deep", 9, array_bytes(9, n, &body))]);
let info = read_info(file.path());
let info = info.unwrap();
assert!(matches!(info.metadata["tok.deep"], Value::LargeArray(l) if l == n));
assert_eq!(info.context_length(), Some(4096));
}
#[test]
fn read_info_rejects_metadata_nested_past_the_depth_limit_instead_of_overflowing_the_stack() {
let mut buf = Vec::new();
buf.extend_from_slice(b"GGUF");
buf.extend_from_slice(&3u32.to_le_bytes()); buf.extend_from_slice(&0u64.to_le_bytes()); buf.extend_from_slice(&1u64.to_le_bytes());
write_string(&mut buf, "test.nested");
buf.extend_from_slice(&9u32.to_le_bytes()); for _ in 0..12 {
buf.extend_from_slice(&9u32.to_le_bytes()); buf.extend_from_slice(&1u64.to_le_bytes()); }
buf.extend_from_slice(&0u32.to_le_bytes()); buf.extend_from_slice(&1u64.to_le_bytes()); buf.push(0u8);
let path = std::env::temp_dir().join(format!(
"llmman-gguf-nested-{}-{}.gguf",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let mut f = File::create(&path).unwrap();
f.write_all(&buf).unwrap();
let err = read_info(&path).unwrap_err();
std::fs::remove_file(&path).ok();
assert!(
err.chain()
.any(|c| c.to_string().contains("nesting too deep")),
"got: {err:#}"
);
}
}