Skip to main content

GgufReader

Struct GgufReader 

Source
pub struct GgufReader {
    pub version: u32,
    pub tensor_count: u64,
    pub tensors: Vec<GgufTensorMeta>,
    pub data_offset: usize,
    pub metadata: BTreeMap<String, GgufValue>,
    /* private fields */
}
Expand description

Parsed GGUF file for import

Fields§

§version: u32

Format version

§tensor_count: u64

Number of tensors

§tensors: Vec<GgufTensorMeta>

Tensor infos (name, dims, dtype, data_offset)

§data_offset: usize

Offset where tensor data section starts

§metadata: BTreeMap<String, GgufValue>

Metadata key-value pairs (extracted from GGUF)

Implementations§

Source§

impl GgufReader

Source

pub fn from_file<P>(path: P) -> Result<GgufReader, AprenderError>
where P: AsRef<Path>,

Load and parse a GGUF file

Source

pub fn from_file_full<P>(path: P) -> Result<GgufReader, AprenderError>
where P: AsRef<Path>,

Load a GGUF file preserving ALL metadata keys (no architecture whitelist).

Used by the sharded-GGUF merge (super::merge::merge_gguf_shards) so arbitrary <arch>.* config keys (gemma., phi3., deepseek2.*, …) survive into the merged file — otherwise the merged model is unloadable for any architecture outside the parse whitelist.

Source

pub fn from_bytes(data: Vec<u8>) -> Result<GgufReader, AprenderError>

Parse GGUF from bytes (whitelist metadata — back-compat default).

Source

pub fn from_bytes_keep( data: Vec<u8>, keep_all: bool, ) -> Result<GgufReader, AprenderError>

Parse GGUF from bytes. When keep_all is true EVERY metadata key is retained; otherwise only tokenizer. / general. / known-arch keys are parsed (the rest skipped for efficiency).

Source

pub fn vocabulary(&self) -> Option<Vec<String>>

Get vocabulary tokens from metadata

Returns the token strings indexed by token ID. Uses “tokenizer.ggml.tokens” key from GGUF metadata.

Source

pub fn tokenizer_model(&self) -> Option<String>

Get tokenizer model type (e.g., “llama”, “gpt2”)

Source

pub fn bos_token_id(&self) -> Option<u32>

Get BOS (beginning of sequence) token ID

Source

pub fn eos_token_id(&self) -> Option<u32>

Get EOS (end of sequence) token ID

Source

pub fn merges(&self) -> Option<Vec<String>>

Get BPE merge rules from metadata (PMAT-171)

Returns the merge rules as “token1 token2” strings for BPE encoding. Uses “tokenizer.ggml.merges” key from GGUF metadata.

Source

pub fn architecture(&self) -> Option<String>

Get general architecture name (e.g., “llama”, “qwen2”)

Source

pub fn model_name(&self) -> Option<String>

Get model name from metadata

Source

pub fn hidden_size(&self) -> Option<usize>

Get hidden dimension (embedding_length)

Source

pub fn num_layers(&self) -> Option<usize>

Get number of transformer layers (block_count)

Source

pub fn num_heads(&self) -> Option<usize>

Get number of attention heads

Source

pub fn num_kv_heads(&self) -> Option<usize>

Get number of key-value heads (for GQA)

Source

pub fn vocab_size(&self) -> Option<usize>

Get vocabulary size

Source

pub fn intermediate_size(&self) -> Option<usize>

Get FFN intermediate dimension

Source

pub fn context_length(&self) -> Option<usize>

Get maximum context length

Source

pub fn rope_theta(&self) -> Option<f32>

Get RoPE theta (frequency base)

Source

pub fn rms_norm_eps(&self) -> Option<f32>

Get RMS norm epsilon (or standard LayerNorm epsilon for GPT-2)

Source

pub fn token_type(&self) -> Option<Vec<i32>>

Get per-token type array (tokenizer.ggml.token_type) Values: 1=normal, 2=unknown, 3=control/special, 4=user_defined, etc.

Source

pub fn padding_token_id(&self) -> Option<u32>

Get padding token ID (tokenizer.ggml.padding_token_id)

Source

pub fn add_bos_token(&self) -> Option<bool>

Get add_bos_token flag (tokenizer.ggml.add_bos_token)

Source

pub fn chat_template(&self) -> Option<String>

Get chat template (tokenizer.chat_template)

Source

pub fn pre_tokenizer_type(&self) -> Option<String>

GH-277: Get pre-tokenizer type (tokenizer.ggml.pre)

Source§

impl GgufReader

Source

pub fn get_tensor_f32( &self, name: &str, ) -> Result<(Vec<f32>, Vec<usize>), AprenderError>

Extract a tensor as F32 data (dequantizing if needed)

Postcondition: data.len() == shape.iter().product()

Source

pub fn get_all_tensors_f32( &self, ) -> Result<BTreeMap<String, (Vec<f32>, Vec<usize>)>, AprenderError>

Get all tensors as F32

Source

pub fn get_all_tensors_f32_with_progress( &self, progress: impl Fn(usize, usize, &str), ) -> Result<BTreeMap<String, (Vec<f32>, Vec<usize>)>, AprenderError>

Get all tensors as F32 with per-tensor progress callback.

Contract: GH-692 — progress feedback for large GGUF dequantization. Callback receives (current_index, total_count, tensor_name).

Source

pub fn get_tensor_raw( &self, name: &str, ) -> Result<(Vec<u8>, Vec<usize>, u32), AprenderError>

Get raw tensor bytes without dequantization (preserves Q4K/Q6K)

Returns (raw_bytes, shape, ggml_dtype) where dtype is per GGML spec:

  • 0=F32, 1=F16, 2=Q4_0, 3=Q4_1, 8=Q8_0
  • 10=Q2_K, 11=Q3_K, 12=Q4_K, 13=Q5_K, 14=Q6_K
Source

pub fn get_all_tensors_raw( &self, ) -> Result<BTreeMap<String, (Vec<u8>, Vec<usize>, u32)>, AprenderError>

Get all tensors as raw bytes (preserves quantization)

Returns BTreeMap of name -> (raw_bytes, shape, ggml_dtype)

Trait Implementations§

Source§

impl Debug for GgufReader

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,