Expand description
§GGUF File Parser
A Rust library for parsing and reading GGUF (GGML Universal Format) files.
GGUF files are binary files that contain key-value metadata and tensors, commonly used for storing quantized machine learning models like LLaMA, Phi, etc.
§Features
- Decode GGUF files (v1, v2, v3)
- Access key-value metadata
- Access tensor information
- Support for little-endian and big-endian files
- CLI tool for quick inspection
- Optional memory-mapped file support (enable
mmapfeature)
§Example
use gguf_rs::get_gguf_container;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Open a GGUF file
let mut container = get_gguf_container("model.gguf")?;
let model = container.decode()?;
// Print model info
println!("Version: {}", model.get_version());
println!("Architecture: {}", model.model_family());
println!("Parameters: {}", model.model_parameters());
println!("File type: {}", model.file_type());
println!("Tensors: {}", model.num_tensor());
// List tensors
for tensor in model.tensors() {
println!(" {}: {:?} {:?}", tensor.name, tensor.kind, tensor.shape);
}
Ok(())
}§CLI Usage
Install the CLI tool:
cargo install gguf-rsShow model info:
gguf model.ggufShow tensors:
gguf model.gguf --tensors§Memory-Mapped Files
For large files, enable the mmap feature for more efficient access:
[dependencies]
gguf-rs = { version = "0.1", features = ["mmap"] }ⓘ
use gguf_rs::mmap::MmapGGUF;
let mmap = MmapGGUF::open("large_model.gguf")?;
let model = mmap.model();§Async I/O
For async applications, enable the async feature:
[dependencies]
gguf-rs = { version = "0.1", features = ["async"] }ⓘ
use gguf_rs::async_io::AsyncGGUF;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut container = AsyncGGUF::open("model.gguf").await?;
let model = container.decode().await?;
println!("Architecture: {}", model.model_family());
Ok(())
}Modules§
- writer
- GGUF file writing support GGUF file writing support
Structs§
- GGUF
Container - GGUF file container for reading GGUF binary files.
- GGUF
Model - Decoded GGUF model containing metadata and tensors.
- Tensor
- Tensor in the GGUF file.
- V1
- Version 1 of the GGUF file.
- V2
- Version 2 of the GGUF file.
- V3
- Version 3 of the GGUF file.
Enums§
- Byte
Order - Byte order of the GGUF file.
- GGML
Type - GGML type of a tensor in the GGUF file.
- Metadata
Value Type - Metadata value type in GGUF files.
- Version
- Version of the GGUF file.
Constants§
- FILE_
MAGIC_ GGJT - Magic constant for
ggmlfiles (versioned, ggjt). - FILE_
MAGIC_ GGLA - Magic constant for
gglafiles (LoRA adapter). - FILE_
MAGIC_ GGMF - Magic constant for
ggmlfiles (versioned, ggmf). - FILE_
MAGIC_ GGML - Magic constant for
ggmlfiles (unversioned). - FILE_
MAGIC_ GGUF_ BE - FILE_
MAGIC_ GGUF_ LE - Magic constant for
gguffiles (versioned, gguf) - GGUF_
DEFAULT_ ALIGNMENT - Default tensor-data alignment per GGUF spec.
- GGUF_
MAX_ DIMS - Maximum number of tensor dimensions supported by GGUF, matching
GGML_MAX_DIMS. - GGUF_
MAX_ STRING_ LENGTH - Maximum allowed string length in bytes (1 GiB), matching llama.cpp’s
GGUF_MAX_STRING_LENGTH. - GGUF_
VERSION_ V1 - GGUF_
VERSION_ V2 - GGUF_
VERSION_ V3
Functions§
- get_
gguf_ container - Get a
GGUFContainerfrom a file, truncating tokenizer arrays to length 3. - get_
gguf_ container_ array_ size - Get a
GGUFContainerfrom a file with the provided max array size.