|
19 | 19 | #define JSON_ASSERT GGML_ASSERT |
20 | 20 | #include <nlohmann/json.hpp> |
21 | 21 |
|
| 22 | +#ifdef LLAMA_YAML_CPP |
| 23 | +#include <yaml-cpp/yaml.h> |
| 24 | +#endif |
| 25 | + |
22 | 26 | #include <algorithm> |
23 | 27 | #include <climits> |
24 | 28 | #include <cstdarg> |
@@ -65,6 +69,177 @@ static void write_file(const std::string & fname, const std::string & content) { |
65 | 69 | file.close(); |
66 | 70 | } |
67 | 71 |
|
| 72 | +#ifdef LLAMA_YAML_CPP |
| 73 | +static bool common_params_load_from_yaml(const std::string & config_file, common_params & params) { |
| 74 | + if (config_file.empty()) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + try { |
| 79 | + YAML::Node config = YAML::LoadFile(config_file); |
| 80 | + |
| 81 | + // Model parameters |
| 82 | + if (config["model"]) { |
| 83 | + params.model.path = config["model"].as<std::string>(); |
| 84 | + } |
| 85 | + if (config["model_url"]) { |
| 86 | + params.model.url = config["model_url"].as<std::string>(); |
| 87 | + } |
| 88 | + if (config["model_alias"]) { |
| 89 | + params.model_alias = config["model_alias"].as<std::string>(); |
| 90 | + } |
| 91 | + if (config["hf_repo"]) { |
| 92 | + params.model.hf_repo = config["hf_repo"].as<std::string>(); |
| 93 | + } |
| 94 | + if (config["hf_file"]) { |
| 95 | + params.model.hf_file = config["hf_file"].as<std::string>(); |
| 96 | + } |
| 97 | + if (config["hf_token"]) { |
| 98 | + params.hf_token = config["hf_token"].as<std::string>(); |
| 99 | + } |
| 100 | + |
| 101 | + // Context and prediction parameters |
| 102 | + if (config["ctx_size"]) { |
| 103 | + params.n_ctx = config["ctx_size"].as<int32_t>(); |
| 104 | + } |
| 105 | + if (config["predict"]) { |
| 106 | + params.n_predict = config["predict"].as<int32_t>(); |
| 107 | + } |
| 108 | + if (config["batch_size"]) { |
| 109 | + params.n_batch = config["batch_size"].as<int32_t>(); |
| 110 | + } |
| 111 | + if (config["ubatch_size"]) { |
| 112 | + params.n_ubatch = config["ubatch_size"].as<int32_t>(); |
| 113 | + } |
| 114 | + if (config["keep"]) { |
| 115 | + params.n_keep = config["keep"].as<int32_t>(); |
| 116 | + } |
| 117 | + if (config["chunks"]) { |
| 118 | + params.n_chunks = config["chunks"].as<int32_t>(); |
| 119 | + } |
| 120 | + if (config["parallel"]) { |
| 121 | + params.n_parallel = config["parallel"].as<int32_t>(); |
| 122 | + } |
| 123 | + if (config["sequences"]) { |
| 124 | + params.n_sequences = config["sequences"].as<int32_t>(); |
| 125 | + } |
| 126 | + |
| 127 | + // CPU parameters |
| 128 | + if (config["threads"]) { |
| 129 | + params.cpuparams.n_threads = config["threads"].as<int>(); |
| 130 | + } |
| 131 | + if (config["threads_batch"]) { |
| 132 | + params.cpuparams_batch.n_threads = config["threads_batch"].as<int>(); |
| 133 | + } |
| 134 | + |
| 135 | + // GPU parameters |
| 136 | + if (config["n_gpu_layers"]) { |
| 137 | + params.n_gpu_layers = config["n_gpu_layers"].as<int32_t>(); |
| 138 | + } |
| 139 | + if (config["main_gpu"]) { |
| 140 | + params.main_gpu = config["main_gpu"].as<int32_t>(); |
| 141 | + } |
| 142 | + |
| 143 | + // Sampling parameters |
| 144 | + if (config["seed"]) { |
| 145 | + params.sampling.seed = config["seed"].as<uint32_t>(); |
| 146 | + } |
| 147 | + if (config["temperature"]) { |
| 148 | + params.sampling.temp = config["temperature"].as<float>(); |
| 149 | + } |
| 150 | + if (config["top_k"]) { |
| 151 | + params.sampling.top_k = config["top_k"].as<int32_t>(); |
| 152 | + } |
| 153 | + if (config["top_p"]) { |
| 154 | + params.sampling.top_p = config["top_p"].as<float>(); |
| 155 | + } |
| 156 | + if (config["min_p"]) { |
| 157 | + params.sampling.min_p = config["min_p"].as<float>(); |
| 158 | + } |
| 159 | + if (config["typical_p"]) { |
| 160 | + params.sampling.typ_p = config["typical_p"].as<float>(); |
| 161 | + } |
| 162 | + if (config["repeat_last_n"]) { |
| 163 | + params.sampling.penalty_last_n = config["repeat_last_n"].as<int32_t>(); |
| 164 | + } |
| 165 | + if (config["repeat_penalty"]) { |
| 166 | + params.sampling.penalty_repeat = config["repeat_penalty"].as<float>(); |
| 167 | + } |
| 168 | + if (config["frequency_penalty"]) { |
| 169 | + params.sampling.penalty_freq = config["frequency_penalty"].as<float>(); |
| 170 | + } |
| 171 | + if (config["presence_penalty"]) { |
| 172 | + params.sampling.penalty_present = config["presence_penalty"].as<float>(); |
| 173 | + } |
| 174 | + if (config["mirostat"]) { |
| 175 | + params.sampling.mirostat = config["mirostat"].as<int32_t>(); |
| 176 | + } |
| 177 | + if (config["mirostat_tau"]) { |
| 178 | + params.sampling.mirostat_tau = config["mirostat_tau"].as<float>(); |
| 179 | + } |
| 180 | + if (config["mirostat_eta"]) { |
| 181 | + params.sampling.mirostat_eta = config["mirostat_eta"].as<float>(); |
| 182 | + } |
| 183 | + |
| 184 | + // Prompt and system parameters |
| 185 | + if (config["prompt"]) { |
| 186 | + params.prompt = config["prompt"].as<std::string>(); |
| 187 | + } |
| 188 | + if (config["system_prompt"]) { |
| 189 | + params.system_prompt = config["system_prompt"].as<std::string>(); |
| 190 | + } |
| 191 | + if (config["prompt_file"]) { |
| 192 | + params.prompt_file = config["prompt_file"].as<std::string>(); |
| 193 | + } |
| 194 | + if (config["prompt_cache"]) { |
| 195 | + params.path_prompt_cache = config["prompt_cache"].as<std::string>(); |
| 196 | + } |
| 197 | + |
| 198 | + // Input/Output parameters |
| 199 | + if (config["input_prefix"]) { |
| 200 | + params.input_prefix = config["input_prefix"].as<std::string>(); |
| 201 | + } |
| 202 | + if (config["input_suffix"]) { |
| 203 | + params.input_suffix = config["input_suffix"].as<std::string>(); |
| 204 | + } |
| 205 | + |
| 206 | + if (config["verbose"]) { |
| 207 | + params.verbosity = config["verbose"].as<int32_t>(); |
| 208 | + } |
| 209 | + |
| 210 | + if (config["conversation"]) { |
| 211 | + bool conv = config["conversation"].as<bool>(); |
| 212 | + params.conversation_mode = conv ? COMMON_CONVERSATION_MODE_ENABLED : COMMON_CONVERSATION_MODE_DISABLED; |
| 213 | + } |
| 214 | + |
| 215 | + if (config["interactive"]) { |
| 216 | + params.interactive = config["interactive"].as<bool>(); |
| 217 | + } |
| 218 | + if (config["interactive_first"]) { |
| 219 | + params.interactive_first = config["interactive_first"].as<bool>(); |
| 220 | + } |
| 221 | + |
| 222 | + if (config["antiprompt"]) { |
| 223 | + if (config["antiprompt"].IsSequence()) { |
| 224 | + for (const auto & item : config["antiprompt"]) { |
| 225 | + params.antiprompt.push_back(item.as<std::string>()); |
| 226 | + } |
| 227 | + } else { |
| 228 | + params.antiprompt.push_back(config["antiprompt"].as<std::string>()); |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + return true; |
| 233 | + } catch (const YAML::Exception & e) { |
| 234 | + fprintf(stderr, "Error parsing YAML config file '%s': %s\n", config_file.c_str(), e.what()); |
| 235 | + return false; |
| 236 | + } catch (const std::exception & e) { |
| 237 | + fprintf(stderr, "Error loading YAML config file '%s': %s\n", config_file.c_str(), e.what()); |
| 238 | + return false; |
| 239 | + } |
| 240 | +} |
| 241 | +#endif |
| 242 | + |
68 | 243 | common_arg & common_arg::set_examples(std::initializer_list<enum llama_example> examples) { |
69 | 244 | this->examples = std::move(examples); |
70 | 245 | return *this; |
@@ -1301,6 +1476,21 @@ common_params_context common_params_parser_init(common_params & params, llama_ex |
1301 | 1476 | params.usage = true; |
1302 | 1477 | } |
1303 | 1478 | )); |
| 1479 | + |
| 1480 | +#ifdef LLAMA_YAML_CPP |
| 1481 | + add_opt(common_arg( |
| 1482 | + {"--config"}, |
| 1483 | + "CONFIG_FILE", |
| 1484 | + "path to YAML configuration file", |
| 1485 | + [](common_params & params, const std::string & value) { |
| 1486 | + params.config_file = value; |
| 1487 | + if (!common_params_load_from_yaml(value, params)) { |
| 1488 | + throw std::invalid_argument("failed to load YAML config file: " + value); |
| 1489 | + } |
| 1490 | + } |
| 1491 | + ).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER})); |
| 1492 | +#endif |
| 1493 | + |
1304 | 1494 | add_opt(common_arg( |
1305 | 1495 | {"--version"}, |
1306 | 1496 | "show version and build info", |
|
0 commit comments