A pure Mojo implementation for parsing GGUF (GPT-Generated Unified Format) model files.
This project provides a minimal prototype GGUF parser implemented entirely in Mojo, without external C library dependencies. GGUF is a binary format used to store large language models and their metadata.
- Pure Mojo Implementation: No external C dependencies
- GGUF Header Parsing: Reads magic number, version, and counts
- Binary Data Reading: Little-endian UInt32/UInt64 parsing
- Extensible Design: Foundation for metadata and tensor parsing
- Validation: Built-in format validation and error handling
gguf_parser.mojo- Core parser implementationmain.mojo- Main demonstration programexample.mojo- Usage examplesextern_gguf.mojo- Original C library bindings (for reference)
GGUF files have the following structure:
-
Header (24 bytes)
- Magic number:
0x46554747("GGUF" in little-endian) - Version:
3(current specification) - Tensor count: Number of tensors in the file
- Metadata count: Number of key-value pairs
- Magic number:
-
Metadata (variable length)
- Key-value pairs with type information
- Supports strings, numbers, arrays, etc.
-
Tensor Information (variable length)
- Tensor headers with names, types, dimensions
-
Tensor Data (variable length, aligned)
- Raw tensor weight data
from gguf_parser import GGUFParser
def main():
var parser = GGUFParser()
# Demo parsing with in-memory data
let success = parser.demo_parse()
if success:
print("GGUF header parsed successfully!")# Run main demonstration
mojo main.mojo
# Run usage examples
mojo example.mojo✅ Completed:
- GGUF header parsing (magic, version, counts)
- Binary data reading utilities (UInt32, UInt64)
- Basic validation and error handling
- In-memory demo data parsing
- Extensible parser structure
🚧 Future Extensions:
- File I/O integration
- Complete metadata value parsing
- Tensor information parsing
- Tensor data reading
- Advanced error handling
The GGUFParser struct provides:
demo_parse()- Demonstrates header parsing with sample dataread_uint32()- Reads 32-bit little-endian integersread_uint64()- Reads 64-bit little-endian integersparse_header()- Validates and parses GGUF headers
GGUFHeader- Header information structureDynamicVector[UInt8]- Binary data storage- Little-endian binary format support
A Python validation script confirms the implementation correctness:
python3 /tmp/validate_gguf.pyThis validates:
- GGUF magic number constants
- Binary data layout (little-endian)
- Creates test GGUF files
GGUF Header parsed successfully:
Magic: 1179993927
Version: 3
Tensor count: 0
Metadata KV count: 0
This is a prototype implementation. Future contributions could include:
- File I/O integration for real GGUF files
- Complete metadata parsing with all value types
- Tensor information and data parsing
- Performance optimizations
- Error handling improvements
This project follows the same license as the parent repository.