Implementation:Ggml org Llama cpp Preset Header
| Knowledge Sources | |
|---|---|
| Domains | Configuration |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Declares the preset configuration system for managing named collections of CLI arguments that can be loaded from INI files, cache, or model directories.
Description
The `common_preset` struct holds a name and a map of `common_arg` to string values, with methods for conversion to CLI args (`to_args`), INI format (`to_ini`), option get/set/unset by environment variable name, merging, and applying to `common_params`. The `common_preset_context` manages preset loading from INI files, cached models, model directories, and CLI arguments, with CSS-like cascading priority for layering multiple presets. Supports remote preset whitelisting for security.
Usage
Use this header when implementing multi-model configuration management. Load presets from INI files or model directories, cascade them with priority ordering, and apply the resulting configuration to `common_params` before model initialization.
Code Reference
Source Location
- Repository: Ggml_org_Llama_cpp
- File: common/preset.h
- Lines: 1-83
Signature
struct common_preset {
std::string name;
std::map<common_arg, std::string> options;
std::vector<std::string> to_args(const std::string & bin_path = "") const;
std::string to_ini() const;
void set_option(const common_preset_context & ctx, const std::string & env, const std::string & value);
void unset_option(const std::string & env);
bool get_option(const std::string & env, std::string & value) const;
void merge(const common_preset & other);
void apply_to_params(common_params & params) const;
};
using common_presets = std::map<std::string, common_preset>;
struct common_preset_context {
common_preset_context(llama_example ex, bool only_remote_allowed = false);
common_presets load_from_ini(const std::string & path, common_preset & global) const;
common_presets load_from_cache() const;
common_presets load_from_models_dir(const std::string & models_dir) const;
common_preset load_from_args(int argc, char ** argv) const;
common_presets cascade(const common_presets & base, const common_presets & added) const;
common_presets cascade(const common_preset & base, const common_presets & presets) const;
};
Import
#include "preset.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| ex | llama_example | Yes | The example type used to configure which arguments are valid |
| only_remote_allowed | bool | No | When true, restricts to whitelisted keys for remote preset security |
| path | const std::string & | Yes (INI load) | Path to the INI file containing preset definitions |
| models_dir | const std::string & | Yes (models dir load) | Path to the local models directory |
| argc, argv | int, char ** | Yes (args load) | CLI arguments to parse into a preset |
| env | const std::string & | Yes (get/set) | Environment variable name identifying the option |
| value | const std::string & | Yes (set) | Value to assign to the specified option |
Outputs
| Name | Type | Description |
|---|---|---|
| presets | common_presets | Map of preset names to preset configurations |
| preset | common_preset | Single preset loaded from arguments |
| args | std::vector<std::string> | CLI argument list representation of a preset |
| ini | std::string | INI format string representation of a preset |
| found | bool | Whether get_option found the requested option |
Usage Examples
#include "preset.h"
// Create a preset context for server mode
common_preset_context ctx(LLAMA_EXAMPLE_SERVER);
// Load presets from an INI file
common_preset global;
common_presets presets = ctx.load_from_ini("models.ini", global);
// Load presets from a models directory
common_presets dir_presets = ctx.load_from_models_dir("/path/to/models");
// Cascade presets (directory overrides INI, like CSS)
common_presets final = ctx.cascade(presets, dir_presets);
// Apply a preset to parameters
common_params params;
final["my_model"].apply_to_params(params);