Skip to main content

GgufFile

Struct GgufFile 

Source
pub struct GgufFile { /* private fields */ }
Expand description

Read-only handle to a memory-mapped GGUF file.

Implementations§

Source§

impl GgufFile

Source

pub fn open(path: impl AsRef<Path>) -> CandleResult<Self>

Open and parse the header of a GGUF file.

Returns immediately after the descriptor table is read — no tensor data is materialised. read_tensor lazy-loads individual tensors.

Source

pub fn content(&self) -> &Content

Raw access to candle’s parsed header — for callers that need the full metadata / tensor_infos maps. Prefer the typed accessors below.

Source

pub fn architecture(&self) -> CandleResult<&str>

Architecture string, e.g. "qwen3", "llama". Read from general.architecture. Errors if the key is missing or non-string.

Source

pub fn metadata(&self, key: &str) -> Option<&Value>

Raw metadata value lookup. Returns None if the key is absent.

Source

pub fn metadata_string(&self, key: &str) -> CandleResult<&str>

Read a string-typed metadata field. Errors if missing or wrong type.

Source

pub fn metadata_u32(&self, key: &str) -> CandleResult<u32>

Read a u32-typed metadata field. Errors if missing or wrong type.

Source

pub fn metadata_u64(&self, key: &str) -> CandleResult<u64>

Read a u64-typed metadata field. Errors if missing or wrong type.

Source

pub fn metadata_f32(&self, key: &str) -> CandleResult<f32>

Read an f32-typed metadata field. Errors if missing or wrong type.

Source

pub fn metadata_bool(&self, key: &str) -> CandleResult<bool>

Read a bool-typed metadata field. Errors if missing or wrong type.

Source

pub fn tensor_count(&self) -> usize

Total number of tensors declared in the header.

Source

pub fn tensor_names(&self) -> impl Iterator<Item = &str>

Iterate over every tensor name in the file. Order is whatever the underlying HashMap yields — do not rely on it being deterministic.

Source

pub fn tensor_info(&self, name: &str) -> Option<&TensorInfo>

Look up a tensor descriptor (shape, dtype, byte offset) without touching the payload. None if the tensor isn’t in the file.

Source

pub fn has_tensor(&self, name: &str) -> bool

Whether a tensor with name is declared in the header.

Source

pub fn read_tensor(&self, name: &str, device: &Device) -> CandleResult<QTensor>

Materialise a tensor as a candle QTensor on the target device.

The returned tensor is still quantized — no dequant happens here. Wrap it in QMatMul::from_qtensor for inference, or call QTensor::dequantize(device) to get a fp32 Tensor.

Beware: candle copies the bytes into an owned Vec<u8> (see TensorInfo::read). For the steady-state weight upload path use Self::tensor_byte_slice instead — it returns a slice directly into the mmap with no allocation.

Source

pub fn mmap_bytes(&self) -> &[u8]

Whole mmap region as a byte slice. Used to wrap the file as a single zero-copy MTLBuffer on Metal — the lifetime of the slice is tied to &self, so the caller is expected to keep an Arc<GgufFile> alive for as long as anything references the mmap.

Source

pub fn tensor_byte_slice(&self, name: &str) -> Option<&[u8]>

Byte slice covering exactly tensor name inside the mmap. The slice points into the file mapping, so reads are demand-paged and there is no heap allocation. Returns None if the tensor isn’t declared.

The byte length is computed from the tensor’s (elem_count, ggml_dtype) using candle’s block_size() / type_size(). For raw quant tensors (Q4K / Q6K / etc.), these bytes are exactly what QTensor::data() would return — but with no copy.

Source

pub fn tensor_byte_range(&self, name: &str) -> Option<(usize, usize)>

(byte_offset_in_mmap, byte_length) for tensor name. Same computation as Self::tensor_byte_slice but returns the indices rather than the slice — useful when the caller already has the mmap base pointer (e.g. when binding a region of a shared buffer at a given offset).

Trait Implementations§

Source§

impl Debug for GgufFile

Source§

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

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,