Skip to main content

zsfm_gguf/
types.rs

1/// GGML tensor data types used in GGUF files.
2/// Values match the ggml_type enum in ggml.h.
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4#[repr(u32)]
5pub enum GGMLType {
6    F32 = 0,
7    F16 = 1,
8    /// Q8_0: blocks of 32 × i8 with a shared f16 scale (34 bytes/block).
9    Q8_0 = 8,
10    BF16 = 30,
11}
12
13impl GGMLType {
14    /// (block_elems, bytes_per_block) for block-quantized types; None for float types.
15    /// Q8_0: 32 × i8 values + 1 × f16 scale = 34 bytes/block.
16    #[allow(dead_code)]
17    pub fn block_shape(self) -> Option<(usize, usize)> {
18        match self {
19            GGMLType::Q8_0 => Some((32, 34)),
20            _ => None,
21        }
22    }
23}
24
25/// GGUF metadata value types (gguf_metadata_value_type).
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27#[repr(u32)]
28#[allow(dead_code)]
29pub enum GGUFValueType {
30    Uint8 = 0,
31    Int8 = 1,
32    Uint16 = 2,
33    Int16 = 3,
34    Uint32 = 4,
35    Int32 = 5,
36    Float32 = 6,
37    Bool = 7,
38    String = 8,
39    Array = 9,
40    Uint64 = 10,
41    Int64 = 11,
42    Float64 = 12,
43}
44
45/// A typed metadata value for a GGUF key-value pair.
46#[derive(Debug, Clone)]
47#[allow(dead_code)]
48pub enum GGUFMetaValue {
49    Uint8(u8),
50    Int8(i8),
51    Uint16(u16),
52    Int16(i16),
53    Uint32(u32),
54    Int32(i32),
55    Float32(f32),
56    Bool(bool),
57    String(String),
58    Uint64(u64),
59    Int64(i64),
60    Float64(f64),
61    ArrayUint32(Vec<u32>),
62    ArrayFloat32(Vec<f32>),
63    ArrayString(Vec<String>),
64}
65
66impl GGUFMetaValue {
67    pub fn value_type(&self) -> GGUFValueType {
68        match self {
69            GGUFMetaValue::Uint8(_) => GGUFValueType::Uint8,
70            GGUFMetaValue::Int8(_) => GGUFValueType::Int8,
71            GGUFMetaValue::Uint16(_) => GGUFValueType::Uint16,
72            GGUFMetaValue::Int16(_) => GGUFValueType::Int16,
73            GGUFMetaValue::Uint32(_) => GGUFValueType::Uint32,
74            GGUFMetaValue::Int32(_) => GGUFValueType::Int32,
75            GGUFMetaValue::Float32(_) => GGUFValueType::Float32,
76            GGUFMetaValue::Bool(_) => GGUFValueType::Bool,
77            GGUFMetaValue::String(_) => GGUFValueType::String,
78            GGUFMetaValue::Uint64(_) => GGUFValueType::Uint64,
79            GGUFMetaValue::Int64(_) => GGUFValueType::Int64,
80            GGUFMetaValue::Float64(_) => GGUFValueType::Float64,
81            GGUFMetaValue::ArrayUint32(_)
82            | GGUFMetaValue::ArrayFloat32(_)
83            | GGUFMetaValue::ArrayString(_) => GGUFValueType::Array,
84        }
85    }
86}