char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Johnathan Craig Maudlincommon: add system-level config file (#26118)8e7f22b67

char/aliceai-t5-moe
3.1 KiB88 linesraw
1#pragma once
2
3#include "common.h"
4#include "arg.h"
5
6#include <string>
7#include <vector>
8#include <map>
9#include <set>
10
11//
12// INI preset parser and writer
13//
14
15constexpr const char * COMMON_PRESET_DEFAULT_NAME = "default";
16
17struct common_preset_context;
18
19struct common_preset {
20    std::string name;
21
22    // options are stored as common_arg to string mapping, representing CLI arg and its value
23    std::map<common_arg, std::string> options;
24
25    // convert preset to CLI argument list
26    std::vector<std::string> to_args(const std::string & bin_path = "") const;
27
28    // convert preset to INI format string
29    std::string to_ini() const;
30
31    // TODO: maybe implement to_env() if needed
32
33    // modify preset options where argument is identified by its env variable
34    void set_option(const common_preset_context & ctx, const std::string & env, const std::string & value);
35
36    // unset option by its env variable
37    void unset_option(const std::string & env);
38
39    // get option value by its env variable, return false if not found
40    bool get_option(const std::string & env, std::string & value) const;
41
42    // merge another preset into this one, overwriting existing options
43    void merge(const common_preset & other);
44
45    // apply preset options to common_params
46    // optionally specify handled_keys to only apply a subset of options (identified by their env), if empty, apply all options
47    void apply_to_params(common_params & params, const std::set<std::string> & handled_keys = std::set<std::string>()) const;
48};
49
50// interface for multiple presets in one file
51using common_presets = std::map<std::string, common_preset>;
52
53// context for loading and editing presets
54struct common_preset_context {
55    common_params default_params; // unused for now
56    common_params_context ctx_params;
57    std::map<std::string, common_arg> key_to_opt;
58
59    bool filter_allowed_keys = false;
60    std::set<std::string> allowed_keys;
61
62    // if true, options unknown to the current example are skipped instead of being an error
63    // used for config files shared by all binaries, where each binary only knows a subset of options
64    bool ignore_unknown_keys = false;
65
66    // if only_remote_allowed is true, only accept whitelisted keys
67    common_preset_context(llama_example ex);
68
69    // load presets from INI file
70    common_presets load_from_ini(const std::string & path, common_preset & global) const;
71
72    // generate presets from cached models
73    common_presets load_from_cache() const;
74
75    // generate presets from local models directory
76    // for the directory structure, see "Using multiple models" in server/README.md
77    common_presets load_from_models_dir(const std::string & models_dir) const;
78
79    // generate one preset from CLI arguments
80    common_preset load_from_args(int argc, char ** argv) const;
81
82    // cascade multiple presets if exist on both: base < added
83    // if preset does not exist in base, it will be added without modification
84    common_presets cascade(const common_presets & base, const common_presets & added) const;
85
86    // apply presets over a base preset (same idea as CSS cascading)
87    common_presets cascade(const common_preset & base, const common_presets & presets) const;
88};