This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Notebooks · lib/gguf/header

Reading a GGUF file, part 1, the header

GGUF is the file format of ggml and llama.cpp: one file holds a model's metadata (architecture, hyperparameters, tokenizer) and its tensors, each in one of ggml's number formats. This module reads the part before the tensor data. It is a rewrite, from the specification, of the reader in ggml's src/gguf.cpp and of gguf-py's gguf/gguf_reader.py: ggml (github.com/ggml-org/ggml, MIT, The ggml authors), specification docs/gguf.md at commit 3612c0af93f2. The tests compare it with gguf-py 0.19.0 (tools/gguf/check.sh).

The file starts with four fields, little-endian:

offset  0  u32  magic: the bytes "GGUF"
offset  4  u32  version (3 since August 2023)
offset  8  u64  the number of tensors
offset 16  u64  the number of metadata key-value pairs

Then the key-value pairs, one after another with no padding:

key    a string: u64 length, then that many bytes of UTF-8
type   u32: one of the thirteen value types below
value  1, 2, 4 or 8 bytes for a number or a bool; a string as above;
       an ARRAY is u32 element type, u64 count, then the elements

A file is read in windows of WINDOW bytes (a Reader), so the header of a model, whose tokenizer alone is 700 KB for a 32000-token vocabulary, never needs more than one window in memory.

Used with from gguf.header import read_meta (and the other names here).

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

This program declares a record type (Header), and records run only in compiled C for now, so the Run buttons here are off. Open it in the workbench and use its C tab to compile it.

Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.

[1]lines 35–378