Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 5112e6a

Browse files
Chore/fix float number yaml file (#1383)
* fix/mistral-nemo-chat-template * Chore: Fix float number not rounded when write to yaml file * Fix: unitest fail when write yaml * Fix: CI build and add unitest * chore: move to format utils
1 parent 0e32e37 commit 5112e6a

File tree

6 files changed

+316
-142
lines changed

6 files changed

+316
-142
lines changed

engine/config/model_config.h

Lines changed: 61 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <sstream>
88
#include <string>
99
#include <vector>
10-
10+
#include "utils/format_utils.h"
1111
namespace config {
1212
struct ModelConfig {
1313
std::string name;
@@ -58,7 +58,7 @@ struct ModelConfig {
5858
int n_probs = 0;
5959
int min_keep = 0;
6060
std::string grammar;
61-
61+
6262
void FromJson(const Json::Value& json) {
6363
// do now allow to update ID and model field because it is unique identifier
6464
// if (json.isMember("id"))
@@ -236,116 +236,90 @@ struct ModelConfig {
236236
std::string ToString() const {
237237
std::ostringstream oss;
238238

239-
// Color codes
240-
const std::string RESET = "\033[0m";
241-
const std::string BOLD = "\033[1m";
242-
const std::string GREEN = "\033[1;32m";
243-
const std::string YELLOW = "\033[0;33m";
244-
const std::string BLUE = "\033[0;34m";
245-
const std::string MAGENTA = "\033[0;35m";
246-
const std::string GRAY = "\033[1;90m";
247-
248-
// Helper function to print comments
249-
auto print_comment = [&oss, &GRAY, &RESET](const std::string& comment) {
250-
oss << GRAY << "# " << comment << RESET << "\n";
251-
};
252-
253-
// Helper function to print key-value pairs
254-
auto print_kv = [&oss, &GREEN, &RESET](
255-
const std::string& key, const auto& value,
256-
const std::string& color = "\033[0m") {
257-
oss << GREEN << key << ":" << RESET << " " << color << value << RESET
258-
<< "\n";
259-
};
260-
261-
// Helper function to print boolean values
262-
auto print_bool = [&print_kv, &MAGENTA](const std::string& key,
263-
bool value) {
264-
print_kv(key, value ? "true" : "false", MAGENTA);
265-
};
266-
267-
// Helper function to print float values with fixed precision
268-
auto print_float = [&print_kv, &BLUE](const std::string& key, float value) {
269-
if (!std::isnan(value)) {
270-
std::ostringstream float_oss;
271-
float_oss << std::fixed << std::setprecision(9) << value;
272-
print_kv(key, float_oss.str(), BLUE);
273-
}
274-
};
275-
276-
print_comment("BEGIN GENERAL GGUF METADATA");
239+
oss << format_utils::print_comment("BEGIN GENERAL GGUF METADATA");
277240
if (!id.empty())
278-
print_kv("id", id, YELLOW);
241+
oss << format_utils::print_kv("id", id, format_utils::YELLOW);
279242
if (!name.empty())
280-
print_kv("name", name, YELLOW);
243+
oss << format_utils::print_kv("name", name, format_utils::YELLOW);
281244
if (!model.empty())
282-
print_kv("model", model, YELLOW);
245+
oss << format_utils::print_kv("model", model, format_utils::YELLOW);
283246
if (!version.empty())
284-
print_kv("version", version, YELLOW);
247+
oss << format_utils::print_kv("version", version, format_utils::YELLOW);
285248
if (!files.empty()) {
286-
oss << GREEN << "files:" << RESET << "\n";
249+
oss << format_utils::GREEN << "files:" << format_utils::RESET << "\n";
287250
for (const auto& file : files) {
288-
oss << " - " << YELLOW << file << RESET << "\n";
251+
oss << " - " << format_utils::YELLOW << file << format_utils::RESET
252+
<< "\n";
289253
}
290254
}
291-
print_comment("END GENERAL GGUF METADATA");
255+
oss << format_utils::print_comment("END GENERAL GGUF METADATA");
292256

293-
print_comment("BEGIN INFERENCE PARAMETERS");
294-
print_comment("BEGIN REQUIRED");
257+
oss << format_utils::print_comment("BEGIN INFERENCE PARAMETERS");
258+
oss << format_utils::print_comment("BEGIN REQUIRED");
295259
if (!stop.empty()) {
296-
oss << GREEN << "stop:" << RESET << "\n";
260+
oss << format_utils::GREEN << "stop:" << format_utils::RESET << "\n";
297261
for (const auto& s : stop) {
298-
oss << " - " << YELLOW << s << RESET << "\n";
262+
oss << " - " << format_utils::YELLOW << s << format_utils::RESET
263+
<< "\n";
299264
}
300265
}
301-
print_comment("END REQUIRED");
302-
print_comment("BEGIN OPTIONAL");
266+
oss << format_utils::print_comment("END REQUIRED");
267+
oss << format_utils::print_comment("BEGIN OPTIONAL");
303268

304-
print_bool("stream", stream);
305-
print_float("top_p", top_p);
306-
print_float("temperature", temperature);
307-
print_float("frequency_penalty", frequency_penalty);
308-
print_float("presence_penalty", presence_penalty);
269+
oss << format_utils::print_bool("stream", stream);
270+
oss << format_utils::print_float("top_p", top_p);
271+
oss << format_utils::print_float("temperature", temperature);
272+
oss << format_utils::print_float("frequency_penalty", frequency_penalty);
273+
oss << format_utils::print_float("presence_penalty", presence_penalty);
309274
if (max_tokens != std::numeric_limits<int>::quiet_NaN())
310-
print_kv("max_tokens", max_tokens, MAGENTA);
275+
oss << format_utils::print_kv("max_tokens", std::to_string(max_tokens),
276+
format_utils::MAGENTA);
311277
if (seed != -1)
312-
print_kv("seed", seed, MAGENTA);
313-
print_float("dynatemp_range", dynatemp_range);
314-
print_float("dynatemp_exponent", dynatemp_exponent);
315-
print_kv("top_k", top_k, MAGENTA);
316-
print_float("min_p", min_p);
317-
print_kv("tfs_z", tfs_z, MAGENTA);
318-
print_float("typ_p", typ_p);
319-
print_kv("repeat_last_n", repeat_last_n, MAGENTA);
320-
print_float("repeat_penalty", repeat_penalty);
321-
print_bool("mirostat", mirostat);
322-
print_float("mirostat_tau", mirostat_tau);
323-
print_float("mirostat_eta", mirostat_eta);
324-
print_bool("penalize_nl", penalize_nl);
325-
print_bool("ignore_eos", ignore_eos);
326-
print_kv("n_probs", n_probs, MAGENTA);
327-
print_kv("min_keep", min_keep, MAGENTA);
278+
oss << format_utils::print_kv("seed", std::to_string(seed),
279+
format_utils::MAGENTA);
280+
oss << format_utils::print_float("dynatemp_range", dynatemp_range);
281+
oss << format_utils::print_float("dynatemp_exponent", dynatemp_exponent);
282+
oss << format_utils::print_kv("top_k", std::to_string(top_k),
283+
format_utils::MAGENTA);
284+
oss << format_utils::print_float("min_p", min_p);
285+
oss << format_utils::print_float("tfs_z", tfs_z);
286+
oss << format_utils::print_float("typ_p", typ_p);
287+
oss << format_utils::print_kv(
288+
"repeat_last_n", std::to_string(repeat_last_n), format_utils::MAGENTA);
289+
oss << format_utils::print_float("repeat_penalty", repeat_penalty);
290+
oss << format_utils::print_bool("mirostat", mirostat);
291+
oss << format_utils::print_float("mirostat_tau", mirostat_tau);
292+
oss << format_utils::print_float("mirostat_eta", mirostat_eta);
293+
oss << format_utils::print_bool("penalize_nl", penalize_nl);
294+
oss << format_utils::print_bool("ignore_eos", ignore_eos);
295+
oss << format_utils::print_kv("n_probs", std::to_string(n_probs),
296+
format_utils::MAGENTA);
297+
oss << format_utils::print_kv("min_keep", std::to_string(min_keep),
298+
format_utils::MAGENTA);
328299

329-
print_comment("END OPTIONAL");
330-
print_comment("END INFERENCE PARAMETERS");
331-
print_comment("BEGIN MODEL LOAD PARAMETERS");
332-
print_comment("BEGIN REQUIRED");
300+
oss << format_utils::print_comment("END OPTIONAL");
301+
oss << format_utils::print_comment("END INFERENCE PARAMETERS");
302+
oss << format_utils::print_comment("BEGIN MODEL LOAD PARAMETERS");
303+
oss << format_utils::print_comment("BEGIN REQUIRED");
333304

334305
if (!engine.empty())
335-
print_kv("engine", engine, YELLOW);
306+
oss << format_utils::print_kv("engine", engine, format_utils::YELLOW);
336307
if (!prompt_template.empty())
337-
print_kv("prompt_template", prompt_template, YELLOW);
308+
oss << format_utils::print_kv("prompt_template", prompt_template,
309+
format_utils::YELLOW);
338310

339-
print_comment("END REQUIRED");
340-
print_comment("BEGIN OPTIONAL");
311+
oss << format_utils::print_comment("END REQUIRED");
312+
oss << format_utils::print_comment("BEGIN OPTIONAL");
341313

342314
if (ctx_len != std::numeric_limits<int>::quiet_NaN())
343-
print_kv("ctx_len", ctx_len, MAGENTA);
315+
oss << format_utils::print_kv("ctx_len", std::to_string(ctx_len),
316+
format_utils::MAGENTA);
344317
if (ngl != std::numeric_limits<int>::quiet_NaN())
345-
print_kv("ngl", ngl, MAGENTA);
318+
oss << format_utils::print_kv("ngl", std::to_string(ngl),
319+
format_utils::MAGENTA);
346320

347-
print_comment("END OPTIONAL");
348-
print_comment("END MODEL LOAD PARAMETERS");
321+
oss << format_utils::print_comment("END OPTIONAL");
322+
oss << format_utils::print_comment("END MODEL LOAD PARAMETERS");
349323

350324
return oss.str();
351325
}

engine/config/yaml_config.cc

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include <string>
66
using namespace std;
77

8+
#include "utils/format_utils.h"
89
#include "yaml_config.h"
9-
1010
namespace config {
1111
// Method to read YAML file
1212
void YamlHandler::Reset() {
@@ -271,27 +271,20 @@ void YamlHandler::WriteYamlFile(const std::string& file_path) const {
271271
if (!outFile) {
272272
throw std::runtime_error("Failed to open output file.");
273273
}
274-
// Helper function to write a key-value pair with an optional comment
275-
auto writeKeyValue = [&](const std::string& key, const YAML::Node& value,
276-
const std::string& comment = "") {
277-
if (!value)
278-
return;
279-
outFile << key << ": " << value;
280-
if (!comment.empty()) {
281-
outFile << " # " << comment;
282-
}
283-
outFile << "\n";
284-
};
285-
286274
// Write GENERAL GGUF METADATA
287275
outFile << "# BEGIN GENERAL GGUF METADATA\n";
288-
writeKeyValue("id", yaml_node_["id"],
289-
"Model ID unique between models (author / quantization)");
290-
writeKeyValue("model", yaml_node_["model"],
291-
"Model ID which is used for request construct - should be "
292-
"unique between models (author / quantization)");
293-
writeKeyValue("name", yaml_node_["name"], "metadata.general.name");
294-
writeKeyValue("version", yaml_node_["version"], "metadata.version");
276+
outFile << format_utils::writeKeyValue(
277+
"id", yaml_node_["id"],
278+
"Model ID unique between models (author / quantization)");
279+
outFile << format_utils::writeKeyValue(
280+
"model", yaml_node_["model"],
281+
"Model ID which is used for request construct - should be "
282+
"unique between models (author / quantization)");
283+
outFile << format_utils::writeKeyValue("name", yaml_node_["name"],
284+
"metadata.general.name");
285+
if (yaml_node_["version"]) {
286+
outFile << "version: " << yaml_node_["version"].as<std::string>() << "\n";
287+
}
295288
if (yaml_node_["files"] && yaml_node_["files"].size()) {
296289
outFile << "files: # can be universal protocol (models://) "
297290
"OR absolute local file path (file://) OR https remote URL "
@@ -316,52 +309,65 @@ void YamlHandler::WriteYamlFile(const std::string& file_path) const {
316309
outFile << "# END REQUIRED\n";
317310
outFile << "\n";
318311
outFile << "# BEGIN OPTIONAL\n";
319-
writeKeyValue("stream", yaml_node_["stream"], "Default true?");
320-
writeKeyValue("top_p", yaml_node_["top_p"], "Ranges: 0 to 1");
321-
writeKeyValue("temperature", yaml_node_["temperature"], "Ranges: 0 to 1");
322-
writeKeyValue("frequency_penalty", yaml_node_["frequency_penalty"],
323-
"Ranges: 0 to 1");
324-
writeKeyValue("presence_penalty", yaml_node_["presence_penalty"],
325-
"Ranges: 0 to 1");
326-
writeKeyValue("max_tokens", yaml_node_["max_tokens"],
327-
"Should be default to context length");
328-
writeKeyValue("seed", yaml_node_["seed"]);
329-
writeKeyValue("dynatemp_range", yaml_node_["dynatemp_range"]);
330-
writeKeyValue("dynatemp_exponent", yaml_node_["dynatemp_exponent"]);
331-
writeKeyValue("top_k", yaml_node_["top_k"]);
332-
writeKeyValue("min_p", yaml_node_["min_p"]);
333-
writeKeyValue("tfs_z", yaml_node_["tfs_z"]);
334-
writeKeyValue("typ_p", yaml_node_["typ_p"]);
335-
writeKeyValue("repeat_last_n", yaml_node_["repeat_last_n"]);
336-
writeKeyValue("repeat_penalty", yaml_node_["repeat_penalty"]);
337-
writeKeyValue("mirostat", yaml_node_["mirostat"]);
338-
writeKeyValue("mirostat_tau", yaml_node_["mirostat_tau"]);
339-
writeKeyValue("mirostat_eta", yaml_node_["mirostat_eta"]);
340-
writeKeyValue("penalize_nl", yaml_node_["penalize_nl"]);
341-
writeKeyValue("ignore_eos", yaml_node_["ignore_eos"]);
342-
writeKeyValue("n_probs", yaml_node_["n_probs"]);
343-
writeKeyValue("min_keep", yaml_node_["min_keep"]);
344-
writeKeyValue("grammar", yaml_node_["grammar"]);
312+
outFile << format_utils::writeKeyValue("stream", yaml_node_["stream"],
313+
"Default true?");
314+
outFile << format_utils::writeKeyValue("top_p", yaml_node_["top_p"],
315+
"Ranges: 0 to 1");
316+
outFile << format_utils::writeKeyValue(
317+
"temperature", yaml_node_["temperature"], "Ranges: 0 to 1");
318+
outFile << format_utils::writeKeyValue(
319+
"frequency_penalty", yaml_node_["frequency_penalty"], "Ranges: 0 to 1");
320+
outFile << format_utils::writeKeyValue(
321+
"presence_penalty", yaml_node_["presence_penalty"], "Ranges: 0 to 1");
322+
outFile << format_utils::writeKeyValue(
323+
"max_tokens", yaml_node_["max_tokens"],
324+
"Should be default to context length");
325+
outFile << format_utils::writeKeyValue("seed", yaml_node_["seed"]);
326+
outFile << format_utils::writeKeyValue("dynatemp_range",
327+
yaml_node_["dynatemp_range"]);
328+
outFile << format_utils::writeKeyValue("dynatemp_exponent",
329+
yaml_node_["dynatemp_exponent"]);
330+
outFile << format_utils::writeKeyValue("top_k", yaml_node_["top_k"]);
331+
outFile << format_utils::writeKeyValue("min_p", yaml_node_["min_p"]);
332+
outFile << format_utils::writeKeyValue("tfs_z", yaml_node_["tfs_z"]);
333+
outFile << format_utils::writeKeyValue("typ_p", yaml_node_["typ_p"]);
334+
outFile << format_utils::writeKeyValue("repeat_last_n",
335+
yaml_node_["repeat_last_n"]);
336+
outFile << format_utils::writeKeyValue("repeat_penalty",
337+
yaml_node_["repeat_penalty"]);
338+
outFile << format_utils::writeKeyValue("mirostat", yaml_node_["mirostat"]);
339+
outFile << format_utils::writeKeyValue("mirostat_tau",
340+
yaml_node_["mirostat_tau"]);
341+
outFile << format_utils::writeKeyValue("mirostat_eta",
342+
yaml_node_["mirostat_eta"]);
343+
outFile << format_utils::writeKeyValue("penalize_nl",
344+
yaml_node_["penalize_nl"]);
345+
outFile << format_utils::writeKeyValue("ignore_eos",
346+
yaml_node_["ignore_eos"]);
347+
outFile << format_utils::writeKeyValue("n_probs", yaml_node_["n_probs"]);
348+
outFile << format_utils::writeKeyValue("min_keep", yaml_node_["min_keep"]);
349+
outFile << format_utils::writeKeyValue("grammar", yaml_node_["grammar"]);
345350
outFile << "# END OPTIONAL\n";
346351
outFile << "# END INFERENCE PARAMETERS\n";
347352
outFile << "\n";
348353
// Write MODEL LOAD PARAMETERS
349354
outFile << "# BEGIN MODEL LOAD PARAMETERS\n";
350355
outFile << "# BEGIN REQUIRED\n";
351-
writeKeyValue("engine", yaml_node_["engine"], "engine to run model");
356+
outFile << format_utils::writeKeyValue("engine", yaml_node_["engine"],
357+
"engine to run model");
352358
outFile << "prompt_template:";
353359
outFile << " " << yaml_node_["prompt_template"] << "\n";
354360
outFile << "# END REQUIRED\n";
355361
outFile << "\n";
356362
outFile << "# BEGIN OPTIONAL\n";
357-
writeKeyValue("ctx_len", yaml_node_["ctx_len"],
358-
"llama.context_length | 0 or undefined = loaded from model");
359-
writeKeyValue("ngl", yaml_node_["ngl"], "Undefined = loaded from model");
363+
outFile << format_utils::writeKeyValue(
364+
"ctx_len", yaml_node_["ctx_len"],
365+
"llama.context_length | 0 or undefined = loaded from model");
366+
outFile << format_utils::writeKeyValue("ngl", yaml_node_["ngl"],
367+
"Undefined = loaded from model");
360368
outFile << "# END OPTIONAL\n";
361369
outFile << "# END MODEL LOAD PARAMETERS\n";
362370

363-
// Write new configuration parameters
364-
365371
outFile.close();
366372
} catch (const std::exception& e) {
367373
std::cerr << "Error writing to file: " << e.what() << std::endl;

0 commit comments

Comments
 (0)