Skip to main content

llama.cpp — Storage File Analysis

GGUF Model File (.gguf)

Location: User-specified path, e.g. ./models/llama-3.gguf Format Definition: ggml/include/gguf.h (lines 1-31) Implementation: ggml/src/gguf.c

File Structure

[4 bytes] Magic: 0x47475546 ("GGUF")
[4 bytes] Version: uint32 (currently 3)
[8 bytes] tensor_count: int64 — number of tensors in file
[8 bytes] kv_count: int64 — number of key-value metadata pairs

--- Key-Value Metadata Section ---
For each KV pair:
[8 bytes] key_length: uint64
[N bytes] key: UTF-8 string (no null terminator)
[4 bytes] value_type: int32 (gguf_type enum)
If value_type == GGUF_TYPE_ARRAY:
[4 bytes] array_elem_type: int32 (gguf_type)
[8 bytes] array_count: uint64
[N bytes] array elements (concatenated binary representation)
Else:
[N bytes] value (binary representation per type)

--- Tensor Descriptor Section ---
For each tensor:
[8 bytes] name_length: uint64
[N bytes] name: UTF-8 string (no null terminator)
[4 bytes] n_dimensions: uint32
[8*n_dim] dimensions: int64 array (shape per dimension)
[4 bytes] data_type: int32 (ggml_type enum — F32, F16, Q4_0, etc.)
[8 bytes] data_offset: uint64 (offset into tensor data blob)

--- Tensor Data Blob (aligned) ---
Padding to alignment boundary (default 32 bytes, configurable via
"general.alignment" KV key)
[N bytes] Raw tensor data (each tensor starts at its aligned offset)

GGUF Value Types (gguf_type)

Type IDNameSize
0UINT81 byte
1INT81 byte
2UINT162 bytes
3INT162 bytes
4UINT324 bytes
5INT324 bytes
6FLOAT324 bytes
7BOOL1 byte (stored as int8)
8STRING8-byte length + chars
9ARRAYtype + count + elements
10UINT648 bytes
11INT648 bytes
12FLOAT648 bytes

Standard Metadata Keys

KeyTypePurpose
general.architectureSTRINGModel architecture identifier (e.g., "llama", "mistral", "gemma")
general.nameSTRINGHuman-readable model name
general.alignmentUINT32Custom alignment for tensor data (default: 32)
general.file_typeUINT32Quantization type identifier
llama.context_lengthUINT32Training context window size
llama.embedding_lengthUINT32Embedding dimension
llama.block_countUINT32Number of transformer layers
llama.attention.head_countUINT32Number of attention heads
llama.attention.head_count_kvUINT32Number of KV heads
llama.attention.layer_norm_rms_epsilonFLOAT32RMS norm epsilon
llama.rope.freq_baseFLOAT32RoPE base frequency
tokenizer.ggml.modelSTRINGTokenizer type ("llama", "gpt2", "t5", etc.)
tokenizer.ggml.tokensARRAY(STRING)Vocabulary tokens
tokenizer.ggml.scoresARRAY(FLOAT32)Token scores
tokenizer.ggml.token_typeARRAY(INT32)Token type attributes

Design Intent

GGUF is a self-describing binary format that bundles model weights with all hyperparameters and tokenizer data needed for inference. Key design choices:

  1. No separate config files — All metadata (architecture, dimensions, tokenizer) is embedded in the same file as weights
  2. Memory-mappable — Tensor data is placed at aligned offsets so the entire file can be mmap()'d and tensors accessed directly without copying. This enables:
    • Lazy loading (only pages touched by inference are read)
    • Multi-process sharing (OS shares physical pages between processes using the same model file)
    • Zero-copy weight access on CPU
  3. Extensible metadata — KV pairs use string keys with typed values, allowing new metadata without format breaking changes
  4. Array support — Tokenizer vocabularies are stored as typed arrays for efficiency
  5. Alignment configurable — Default 32-byte alignment works for most SIMD; general.alignment KV allows customization

Loading Process (src/llama-model.cpp)

  1. gguf_init_from_file() parses header, KV pairs, and tensor descriptors
  2. llama_model::load_arch() reads general.architecture to determine model type
  3. llama_model::load_hparams() reads architecture-specific hyperparameters from KV pairs
  4. llama_model::load_vocab() reads tokenizer data from KV pairs
  5. llama_model::load_tensors() maps tensor data into backend buffers:
    • CPU: Uses llama_mmap for memory-mapped access
    • GPU: Copies tensors to VRAM via ggml_backend_buffer

Model Conversion Scripts

Location: models/ directory (Python scripts)

ScriptPurpose
convert_hf_to_gguf.pyConvert HuggingFace models to GGUF
convert_hf_to_gguf.py --quantizeConvert + quantize in one step
convert_llama_ggml_to_gguf.pyLegacy GGML → GGUF conversion
convert_ggml_to_gguf.pyOld format migration

These scripts read PyTorch safetensors/ckpt files, extract hyperparameters, and write the GGUF binary format using the gguf Python package.

Session State Files

The server can save/load KV cache state via the /slots endpoint:

Format: GGUF-compatible (uses gguf KV pairs to store session metadata + raw KV cache data) Purpose: Allows pausing and resuming long conversations without re-processing the prompt