GGUF explained: what's inside the file, and what Q4_K_M means
ggml just hit 1.0. The file format it reads, GGUF, is what llama.cpp, Ollama and LM Studio load: a 24-byte header, typed metadata with the whole tokenizer, a tensor table, and 32-byte-aligned weights. Plus what Q4_K_M means and what it costs.
On this page
- ggml, GGUF and the apps on top
- Opening one file
- Region 1: the 24-byte header
- Region 2: metadata, including the tokenizer
- Region 3: the tensor table
- Region 4: aligned data, and why loading is fast
- Where the format came from
- What Q4_K_M means
- What it costs in quality
- Which one to download
- GGUF vs safetensors vs MLX
- What ggml 1.0 changes
The same AI model, Llama 3.1 8B Instruct, is a 32.13 GB file at full 32-bit precision and a 4.92 GB file as Q4_K_M. The small one is a single .gguf file, and that one file is all llama.cpp, Ollama or LM Studio need to run it: weights, tokenizer, chat template, everything.
The library that reads it, ggml, just hit version 1.0.0 on 13 November. A good moment to open one of these files and go through it region by region: what the bytes are, what Q4_K_M actually means, how size and quality move with each quantization level, and what 1.0 changes (and doesn’t). Everything below comes from the GGUF specification, llama.cpp’s pull requests and docs, and the published metadata of one public file. No benchmarks were run for this post.
ggml, GGUF and the apps on top
ggml is a “tensor library for machine learning” in plain C/C++ with no dependencies, with “2- to 8-bit integer quantization” and “zero memory allocations during runtime” (README). It’s the math under llama.cpp and whisper.cpp. GGUF is its file format: “a file format for storing models for inference with GGML and executors based on GGML” (spec). Hugging Face’s docs list llama.cpp, LM Studio, GPT4All and Ollama as tools that use it (docs); Ollama imports a GGUF with a one-line FROM /path/to/file.gguf Modelfile (Ollama docs).
Opening one file
The example is the Q4_K_M file in bartowski/Meta-Llama-3.1-8B-Instruct-GGUF (over 300,000 downloads in the month before this was written). The header of the published file says the following; Hugging Face’s GGUF viewer on the file page shows the same metadata and tensor list without downloading anything.
The file has four regions, in this order:
| Region | Bytes in this file | What’s in it |
|---|---|---|
| Header | 0 – 23 (24 bytes) | magic GGUF, version, tensor count, metadata count |
| Metadata | up to byte 7,823,572 | 33 typed key–value pairs |
| Tensor table | 17,335 bytes | name, shape, type and offset of each of 292 tensors |
| Tensor data | from byte 7,840,928 | the weights, each tensor on a 32-byte boundary |
Region 1: the 24-byte header
47 47 55 46 | 03 00 00 00 | 24 01 00 00 00 00 00 00 | 21 00 00 00 00 00 00 00
"GGUF" version 3 tensor_count = 292 metadata_kv_count = 33
The spec: the magic “Must be GGUF at the byte level: 0x47 0x47 0x55 0x46”, the version “Must be 3”, then two little-endian uint64 counters. 4 + 4 + 8 + 8 = 24 bytes, always.
Region 2: metadata, including the tokenizer
Each pair is a key string, a type tag and a value. A few of the 33 in this file:
| Key | Value |
|---|---|
general.architecture |
"llama" |
llama.block_count |
32 |
llama.context_length |
131072 |
llama.embedding_length |
4096 |
general.file_type |
15 (MOSTLY_Q4_K_M in the spec) |
tokenizer.ggml.tokens |
array of 128,256 strings |
tokenizer.ggml.merges |
array of 280,147 strings |
tokenizer.chat_template |
a 4.8 kB Jinja template |
quantize.imatrix.chunks_count |
125 |
That’s the difference from a “tensor-only” format, as Hugging Face describes safetensors: “unlike tensor-only file formats like safetensors … GGUF encodes both the tensors and a standardized set of metadata.” A safetensors model ships its config and tokenizer as separate files next to the weights; a GGUF carries them inside. Most of this region is the tokenizer: the tokens, token types and merges run from byte 1,115 to byte 7,818,495.
Region 3: the tensor table
One entry per tensor: name, number of dimensions, shape, type, and an offset into the data region.
| Name | Shape | Type | Offset |
|---|---|---|---|
token_embd.weight |
4096 × 128256 | Q4_K | 256 |
blk.0.ffn_down.weight |
14336 × 4096 | Q6_K | 295,518,464 |
blk.0.attn_q.weight |
4096 × 4096 | Q4_K | 421,560,576 |
blk.0.attn_norm.weight |
4096 | F32 | 295,502,080 |
Region 4: aligned data, and why loading is fast
“The offset of each tensor’s data must be a multiple of ALIGNMENT”, which is 32 unless general.alignment says otherwise. This file doesn’t set it, so the metadata and tensor table end at byte 7,840,907 and the data starts at 7,840,928, the next multiple of 32.
Alignment is what makes mmap work: the program maps the file into memory and the operating system pages weights in as they’re touched, instead of reading and copying gigabytes up front. Everything that isn’t weights here is 7.84 MB of 4,920.74 MB, 0.16 % of the file.
Where the format came from
GGUF is the fourth format in the family. The spec’s “Historical State of Affairs”: GGML (no versioning or alignment), GGMF (the same, with a version), GGJT (“Aligns the tensors to allow for use with mmap”). For all three, “Adding or removing any new hyperparameters is a breaking change.” GGJT came from llama.cpp PR #613 (March 2023), which changed the format “so we can mmap() weights directly into memory without having to read() or copy them”. The author’s comment on the new magic: “my initials also act as a stamp of approval”; the GG in the earlier names is widely read as Georgi Gerganov’s initials, as Wikipedia notes.
GGUF replaced them in llama.cpp PR #2398, merged 21 August 2023 (“This is a breaking change”). The key change: “a key-value structure for the hyperparameters (now referred to as metadata), rather than a list of untyped values”, so new fields no longer break old readers.
The name: the spec never spells it out. The project’s own Python package describes “the GGUF (GGML Universal File) format” (gguf-py). The “GPT-Generated Unified Format” you often see elsewhere isn’t in the spec or the package.
What Q4_K_M means
- Q4: 4-bit quantization. Weights are stored in small blocks, each block with its own scale, so a 4-bit value is one of 16 levels inside that block’s range.
- K: the k-quants from llama.cpp PR #1684 (June 2023).
Q4_Kis “4-bit quantization in super-blocks containing 8 blocks, each block having 32 weights. Scales and mins are quantized with 6 bits. This ends up using 4.5 bpw.” - M: medium.
Q4_K_M“uses GGML_TYPE_Q6_K for half of the attention.wv and feed_forward.w2 tensors, else GGML_TYPE_Q4_K”;Q4_K_Suses Q4_K for all of them.
The tensor table of the file above shows the mix: 193 tensors in Q4_K, 33 in Q6_K, 66 small F32 norm vectors. In block 0, attn_v and ffn_down are Q6_K. llama.cpp’s quantize README lists Q4_K_M for Llama 3.1 8B at 4.8944 bits per weight, which matches the file: 8,030,261,312 parameters × 4.8944 / 8 ≈ 4.91 GB.
| Type | Bits/weight (README) | File size (published) | vs F32 |
|---|---|---|---|
| F32 | 32 | 32.13 GB | 1× |
| F16 | 16.0005 | 16.06 GB | 2× |
| Q8_0 | 8.5008 | 8.54 GB | 3.8× smaller |
| Q6_K | 6.5633 | 6.60 GB | 4.9× smaller |
| Q5_K_M | 5.7036 | 5.73 GB | 5.6× smaller |
| Q4_K_M | 4.8944 | 4.92 GB | 6.5× smaller |
| Q3_K_M | 3.9960 | 4.02 GB | 8.0× smaller |
| Q2_K | 3.1593 | 3.18 GB | 10.1× smaller |
What it costs in quality
The k-quants PR published wikitext perplexity for the original LLaMA 7B (lower is better): F16 5.9066, Q6_K 5.9110, Q5_K_M 5.9208, Q4_K_M 5.9601, Q3_K_M 6.1503, Q2_K 6.7764. As the author put it, “the 6-bit quantized perplexity is within 0.1% or better from the original fp16 model.” Q4_K_M is 0.9 % worse, for a file that was 3.80 GB instead of 13.0 GB in the same table.
Caveats: one metric, one 2023 model, published by the method’s author. The quantize README notes the loss “is usually measured in Perplexity (ppl) and/or Kullback–Leibler Divergence (kld)” and “can be minimized by using a suitable imatrix file”. That’s the importance matrix from PR #4861: run calibration text through the model first, record which weights matter most, and quantize those more carefully. The example file’s metadata records one (quantize.imatrix.chunks_count = 125).
Which one to download
llama.cpp’s quantize README is blunt: “memory and disk requirements are the same”. The file has to fit in RAM or video memory, with room left for the context (the KV cache, which grows with context length). A reasonable reading of the numbers above:
- Start at Q4_K_M: under 1 % perplexity cost in the published test, 6.5× smaller than F32.
- With memory to spare, Q5_K_M or Q6_K get within a quarter and a tenth of a percent.
- Go below 4 bits only when nothing else fits; the published numbers bend sharply there (Q3_K_M +4.1 %, Q2_K +14.7 %).
GGUF vs safetensors vs MLX
- safetensors: “a new simple format for storing tensors safely (as opposed to pickle)”: an 8-byte header length, a JSON header, then raw tensors. A free-form
__metadata__string map, but config and tokenizer usually live in separate files. - MLX: “an array framework for machine learning on Apple silicon”; mlx-lm converts and quantizes Hugging Face models into its own format.
- GGUF: one file with typed metadata, tokenizer and quantized tensors, for ggml-based runtimes.
What ggml 1.0 changes
The release notes: “ggml 1.0.0 prunes the deprecated public API and the legacy DIAG_MASK ops, adds configurable YaRN truncation and FP8 support, and brings broad performance work across backends.” The RPC protocol moves to v9 “to reject incompatible peers”. Releases follow semantic versioning, and llama.cpp tags only when its internal copy matches a released ggml, so “a downstream building from the release tags and linking to a system-wide ggml will be compatible” (discussion #1579).
What it doesn’t change: the file. The notes list no change to the GGUF format, so the fifth byte of every file is still 03.
Which quant do you actually download, and what runs it? Tell me under the video.