Model files: the gguf mod
GGUF is the file format of local AI models: llama.cpp, Ollama and LM Studio run them, and Hugging Face has thousands. A file holds the model’s description (its architecture, context length, chat template and tokenizer) and its weights, the tensors, often quantized to 4 or 8 bits a number. mods/gguf.luma reads what a file holds. It reads only the start of a file, so a model of many gigabytes opens at once. Copy it next to your program and import it.
import "gguf"
m: gguf.model = gguf.open(file("SmolLM2-135M-Instruct-Q8_0.gguf"))
print(m.summary())
print(m.meta["llama.context_length"])
print(m.meta["tokenizer.ggml.model"])
print(m.tensors[0])Output:
llama, Smollm2 135M 8k Lc100K Mix1 Ep2, 134.5M parameters, Q8_0, context 8192, 138.1 MB
8192
gpt2
gguf.tensor {name: "token_embd.weight", kind: "Q8_0", code: 8, shape: [576, 49152], offset: 1785952, size: 30081024}| Function or field | What it is |
|---|---|
gguf.open(f) | reads a file’s description and its list of tensors |
m.summary() | one line: architecture, name, parameters, quantization, context length, size |
m.arch, m.name, m.version | general.architecture, general.name, the format’s version (2 or 3) |
m.meta | every key of the description, a {str: json}: numbers, strings, bools and arrays |
m.array(key) | an array of more than 1,000 items, read in full (see below) |
m.tensors | the tensors, a [gguf.tensor]: name, kind (F32, F16, BF16, Q8_0, Q4_K, IQ4_XS, …), shape (the first number changes fastest), offset (where its bytes start in the file) and size (bytes) |
m.params() | the number of parameters: the numbers of all tensors |
m.read(t) | a tensor’s bytes, as they are in the file, a buffer |
m.map(t) | the same bytes, mapped: read from the file only when used |
m.bytes, m.alignment | the file’s size, and how its tensors are aligned |
The tokenizer
A model reads tokens, not characters: numbered pieces of text from its vocabulary. m.tokenizer() turns text into a model’s tokens and back, as llama.cpp does, from the vocabulary in the file:
tk: gguf.tokenizer = m.tokenizer()
ids: [int] = tk.encode("Hello, world!")
print(ids)
print(tk.decode(ids))
print(tk.count("How many tokens is this?"))
print(tk.piece(ids[0]))Output:
[19556, 28, 905, 17]
Hello, world!
6
Hello| Method or field | What it does |
|---|---|
tk.encode(text) | the token ids of a text; special tokens written in it (<|im_start|>) become their ids |
tk.encode(text, false) | the same, with special tokens read as plain text (user-defined tokens still count) |
tk.decode(ids) | the text of token ids; special tokens give their text |
tk.count(text) | how many tokens a text is |
tk.piece(id) | one token’s text |
tk.bos, tk.eos | the ids of its begin- and end-of-text tokens, -1 if it has none; encode adds neither |
Two kinds of tokenizer are read:
- Byte-level BPE (
tokenizer.ggml.modelgpt2): Llama 3, Qwen 2 and 2.5, SmolLM, StarCoder, GPT-2, MPT, OLMo, Command-R, DeepSeek-R1’s Qwen, Falcon 3, StableLM 2 and others. The text is cut into words the way the model’s family does (tokenizer.ggml.pre: the GPT-2, StarCoder, Llama 3 and Qwen 2 families), and each word’s bytes are merged by the model’s merge list. - SentencePiece (
llama): Llama 1 and 2, Mistral 7B, Phi-3 and others: spaces become▁, pieces merge by their scores, and what isn’t a piece goes as its bytes (<0xE2>).
Another family’s file raises gguf.BROKEN: the pre-tokenizer "kimi-k2" isn't supported yet.
The tokens are llama.cpp’s, tested on its own tests (nine vocabularies) and against Hugging Face’s tokenizers on 12,000 random texts in 15 scripts. One difference is llama.cpp’s too: Qwen’s own tokenizer first normalizes text (NFC), so a letter written as a letter and a separate accent (a and ̈) can give other tokens there than here. Making the tokenizer reads the whole vocabulary: 0.6 seconds for 49,000 tokens in luma run, 0.07 in a built program; then 40,000 characters take about 0.3 seconds in luma run, 0.03 built.
Big arrays
A model’s vocabulary is an array of tens of thousands of strings, and its merges and scores as many. In meta, an array of more than 1,000 items is a summary, so that opening stays fast and small: {"array":"string","count":49152}. m.array(key) reads it when you want it:
tokens: json = m.array("tokenizer.ggml.tokens")
print("${len(tokens)} ${tokens[0]}")Output:
49152 <|endoftext|>Details
- Versions 2 and 3 of the format are read (llama.cpp writes 3). Version 1 files (2023) and big-endian ones are refused with a message saying so.
- All value types are read: integers of 1 to 8 bytes, signed or not, 4- and 8-byte floats, bools, strings, and arrays of them, also of arrays. An unsigned 8-byte number past 2^63 comes out negative.
- A tensor of a type this reader doesn’t know has the kind
type Nand size -1; the file still opens. - A damaged file is refused (error
gguf.BROKEN, 100), naming what is wrong. Counts and lengths are checked against the file’s size first, so a broken or hostile file can’t make the program take a lot of memory. A file that isn’t there isgguf.BROKENtoo. tokenizer.chat_templateis a Jinja template, given as text.- Opening a 145 MB model with a vocabulary of 49,152 tokens takes 0.04 seconds in a built program, 0.4 in
luma run.