Implementation:Ggml org Llama cpp Model Saver
| Knowledge Sources | |
|---|---|
| Domains | Model_Serialization, GGUF |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Implements the `llama_model_saver` class that serializes a loaded model back to GGUF format on disk.
Description
This file provides the model serialization logic for writing llama.cpp models to GGUF files. It initializes an empty GGUF context and provides overloaded `add_kv` methods for writing typed metadata (uint32, int32, float, bool, string, arrays, string vectors). A template `add_kv` method handles per-layer arrays with an optimization to collapse uniform values into scalars. The `add_kv_from_model` method writes all model metadata including architecture, hyperparameters, vocabulary tokens/scores/types, RoPE parameters, and expert configurations, while `add_tensors_from_model` adds all tensor descriptors and `save` writes the complete GGUF file.
Usage
Use this module when exporting or re-serializing models to GGUF format, such as during quantization, conversion, or model manipulation workflows that need to write modified models back to disk.
Code Reference
Source Location
- Repository: Ggml_org_Llama_cpp
- File: src/llama-model-saver.cpp
- Lines: 1-285
Signature
class llama_model_saver {
llama_model_saver(const struct llama_model & model);
~llama_model_saver();
void add_kv(const enum llm_kv key, const uint32_t value);
void add_kv(const enum llm_kv key, const int32_t value);
void add_kv(const enum llm_kv key, const float value);
void add_kv(const enum llm_kv key, const bool value);
void add_kv(const enum llm_kv key, const char * value);
template <typename Container>
void add_kv(const enum llm_kv key, const Container & value, const bool per_layer);
void add_kv_from_model();
void add_tensors_from_model();
void save(const std::string & path);
};
Import
#include "llama-model-saver.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | const llama_model & | Yes | Reference to the loaded model to serialize |
| key | enum llm_kv | Yes | Metadata key identifier for KV pairs |
| value | various (uint32_t, int32_t, float, bool, string, Container) | Yes | Value to write for the given metadata key |
| per_layer | bool | No | Whether Container values are per-layer (collapses uniform arrays to scalars) |
| path | const std::string & | Yes | Output file path for the GGUF file (in save method) |
Outputs
| Name | Type | Description |
|---|---|---|
| GGUF file | file on disk | Complete serialized model in GGUF format with metadata and tensor data |
Usage Examples
// Create a model saver from a loaded model
llama_model_saver saver(model);
// Write all metadata and tensors
saver.add_kv_from_model();
saver.add_tensors_from_model();
// Save to disk
saver.save("output_model.gguf");