GGUF-RS
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.
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
- ✅ Zero-copy metadata access
- ✅ Memory-mapped file support (optional
mmapfeature) - ✅ Async I/O support (optional
asyncfeature) - ✅ Write GGUF files
Installation
Add to your Cargo.toml:
[]
= "0.1"
Or install the CLI tool:
Usage
Library
Basic usage:
use get_gguf_container;
Access specific metadata:
use get_gguf_container;
let mut container = get_gguf_container?;
let model = container.decode?;
// Get specific metadata values
let metadata = model.metadata;
if let Some = metadata.get
// Check context length
if let Some = metadata.get
Work with tensors:
use get_gguf_container;
let mut container = get_gguf_container?;
let model = container.decode?;
// List all tensors
for tensor in model.tensors
// Find specific tensor
let embed_tensor = model.tensors
.iter
.find;
if let Some = embed_tensor
Read full tokenizer vocabulary:
use get_gguf_container_array_size;
// Use get_gguf_container_array_size to read full arrays
// (default get_gguf_container truncates arrays to 3 elements)
let mut container = get_gguf_container_array_size?;
let model = container.decode?;
// Now you can access full tokenizer arrays
let metadata = model.metadata;
if let Some = metadata.get
CLI
Show model metadata:
Show tensors:
Supported GGML Types
| Type | Description |
|---|---|
| F32 | 32-bit float |
| F16 | 16-bit float |
| Q4_0 | 4-bit quantization (type 0) |
| Q4_1 | 4-bit quantization (type 1) |
| Q5_0 | 5-bit quantization (type 0) |
| Q5_1 | 5-bit quantization (type 1) |
| Q8_0 | 8-bit quantization (type 0) |
| Q2_K - Q6_K | K-quant types |
| IQ series | I-quant types (IQ1_S, IQ2_XXS, etc.) |
| BF16 | Brain float 16 |
API Documentation
Full API documentation is available at docs.rs/gguf-rs.
Performance
- Zero-copy metadata access: Metadata is parsed once and stored in memory for fast repeated access
- Lazy tensor data: Tensor metadata is parsed, but actual tensor data is not loaded into memory
- Array truncation: By default, arrays in metadata are truncated to 3 elements for performance. Use
get_gguf_container_array_size()withu64::MAXto read full arrays when needed
Memory Usage
The library has minimal memory overhead:
- Metadata storage: O(n_kv + n_tensors) where n_kv = number of key-value pairs, n_tensors = number of tensors
- No tensor data is loaded into memory unless explicitly requested
Memory-Mapped Files
For large GGUF files (multiple GB), use the mmap feature for efficient access:
[]
= { = "0.1", = ["mmap"] }
use MmapGGUF;
Benefits of memory mapping:
- Lazy loading: Only accessed pages are loaded into memory
- OS-managed paging: The operating system handles memory management
- Fast random access: Direct pointer access to file data
Async I/O
For async applications, enable the async feature:
[]
= { = "0.1", = ["async"] }
use AsyncGGUF;
async
Writing GGUF Files
Create and write GGUF files:
use ;
Compatibility
- Rust version: Requires Rust 1.56+ (edition 2021)
- GGUF versions: Supports v1, v2, and v3
- Byte order: Both little-endian and big-endian files
- Platforms: Works on all platforms supported by Rust (Linux, macOS, Windows, BSD, etc.)
Testing
Benchmarks
Run performance benchmarks:
Benchmarks measure:
- File parsing performance
- Metadata access speed
- Tensor iteration overhead
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and clippy
- Submit a pull request
Security
Please report security vulnerabilities to zackshen0526@gmail.com. See SECURITY.md for more information.
GGUF Specification
This library implements the GGUF specification.
License
MIT License - see LICENSE for details.
Credits
- GGUF format by ggml
- Contributors: @AvivAbachi, @jbooth, @Knight-Ops