Implementation:Ggml org Llama cpp GGUF Example
| Knowledge Sources | |
|---|---|
| Domains | GGUF, Example |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Demonstrates GGUF file format read and write operations using the low-level gguf API.
Description
This example creates a GGUF context, writes various key-value metadata types (uint8, int8, uint16, float32, string, arrays of int16/float32/string, etc.), creates 10 random tensors with random dimensions and float32 data, and writes everything to a file. It then reads the file back, prints all KV pairs and tensor info, and verifies the data round-trips correctly by comparing tensor values.
Usage
Use this example as a reference and test for the GGUF file format API, demonstrating the full lifecycle of creating, writing, reading, and verifying GGUF files with metadata and tensor data.
Code Reference
Source Location
- Repository: Ggml_org_Llama_cpp
- File: examples/gguf/gguf.cpp
- Lines: 1-270
Signature
static bool gguf_ex_write(const std::string & fname);
static bool gguf_ex_read_0(const std::string & fname);
static bool gguf_ex_read_1(const std::string & fname, bool check_data);
int main(int argc, char ** argv);
Import
#include "ggml.h"
#include "gguf.h"
#include <cstdio>
#include <string>
#include <sstream>
#include <vector>
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| fname | std::string | Yes | Output/input filename for the GGUF file (default "test.gguf") |
Outputs
| Name | Type | Description |
|---|---|---|
| return | int | Exit code: 0 on success, 1 on failure |
| test.gguf | file | Written GGUF file containing metadata KV pairs and 10 random tensors |
Usage Examples
# Build and run the GGUF example
cmake --build build --target llama-gguf
# Run with default filename "test.gguf"
./build/bin/llama-gguf