Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 61 additions & 87 deletions engine/config/model_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <sstream>
#include <string>
#include <vector>

#include "utils/format_utils.h"
namespace config {
struct ModelConfig {
std::string name;
Expand Down Expand Up @@ -58,7 +58,7 @@ struct ModelConfig {
int n_probs = 0;
int min_keep = 0;
std::string grammar;

void FromJson(const Json::Value& json) {
// do now allow to update ID and model field because it is unique identifier
// if (json.isMember("id"))
Expand Down Expand Up @@ -236,116 +236,90 @@ struct ModelConfig {
std::string ToString() const {
std::ostringstream oss;

// Color codes
const std::string RESET = "\033[0m";
const std::string BOLD = "\033[1m";
const std::string GREEN = "\033[1;32m";
const std::string YELLOW = "\033[0;33m";
const std::string BLUE = "\033[0;34m";
const std::string MAGENTA = "\033[0;35m";
const std::string GRAY = "\033[1;90m";

// Helper function to print comments
auto print_comment = [&oss, &GRAY, &RESET](const std::string& comment) {
oss << GRAY << "# " << comment << RESET << "\n";
};

// Helper function to print key-value pairs
auto print_kv = [&oss, &GREEN, &RESET](
const std::string& key, const auto& value,
const std::string& color = "\033[0m") {
oss << GREEN << key << ":" << RESET << " " << color << value << RESET
<< "\n";
};

// Helper function to print boolean values
auto print_bool = [&print_kv, &MAGENTA](const std::string& key,
bool value) {
print_kv(key, value ? "true" : "false", MAGENTA);
};

// Helper function to print float values with fixed precision
auto print_float = [&print_kv, &BLUE](const std::string& key, float value) {
if (!std::isnan(value)) {
std::ostringstream float_oss;
float_oss << std::fixed << std::setprecision(9) << value;
print_kv(key, float_oss.str(), BLUE);
}
};

print_comment("BEGIN GENERAL GGUF METADATA");
oss << format_utils::print_comment("BEGIN GENERAL GGUF METADATA");
if (!id.empty())
print_kv("id", id, YELLOW);
oss << format_utils::print_kv("id", id, format_utils::YELLOW);
if (!name.empty())
print_kv("name", name, YELLOW);
oss << format_utils::print_kv("name", name, format_utils::YELLOW);
if (!model.empty())
print_kv("model", model, YELLOW);
oss << format_utils::print_kv("model", model, format_utils::YELLOW);
if (!version.empty())
print_kv("version", version, YELLOW);
oss << format_utils::print_kv("version", version, format_utils::YELLOW);
if (!files.empty()) {
oss << GREEN << "files:" << RESET << "\n";
oss << format_utils::GREEN << "files:" << format_utils::RESET << "\n";
for (const auto& file : files) {
oss << " - " << YELLOW << file << RESET << "\n";
oss << " - " << format_utils::YELLOW << file << format_utils::RESET
<< "\n";
}
}
print_comment("END GENERAL GGUF METADATA");
oss << format_utils::print_comment("END GENERAL GGUF METADATA");

print_comment("BEGIN INFERENCE PARAMETERS");
print_comment("BEGIN REQUIRED");
oss << format_utils::print_comment("BEGIN INFERENCE PARAMETERS");
oss << format_utils::print_comment("BEGIN REQUIRED");
if (!stop.empty()) {
oss << GREEN << "stop:" << RESET << "\n";
oss << format_utils::GREEN << "stop:" << format_utils::RESET << "\n";
for (const auto& s : stop) {
oss << " - " << YELLOW << s << RESET << "\n";
oss << " - " << format_utils::YELLOW << s << format_utils::RESET
<< "\n";
}
}
print_comment("END REQUIRED");
print_comment("BEGIN OPTIONAL");
oss << format_utils::print_comment("END REQUIRED");
oss << format_utils::print_comment("BEGIN OPTIONAL");

print_bool("stream", stream);
print_float("top_p", top_p);
print_float("temperature", temperature);
print_float("frequency_penalty", frequency_penalty);
print_float("presence_penalty", presence_penalty);
oss << format_utils::print_bool("stream", stream);
oss << format_utils::print_float("top_p", top_p);
oss << format_utils::print_float("temperature", temperature);
oss << format_utils::print_float("frequency_penalty", frequency_penalty);
oss << format_utils::print_float("presence_penalty", presence_penalty);
if (max_tokens != std::numeric_limits<int>::quiet_NaN())
print_kv("max_tokens", max_tokens, MAGENTA);
oss << format_utils::print_kv("max_tokens", std::to_string(max_tokens),
format_utils::MAGENTA);
if (seed != -1)
print_kv("seed", seed, MAGENTA);
print_float("dynatemp_range", dynatemp_range);
print_float("dynatemp_exponent", dynatemp_exponent);
print_kv("top_k", top_k, MAGENTA);
print_float("min_p", min_p);
print_kv("tfs_z", tfs_z, MAGENTA);
print_float("typ_p", typ_p);
print_kv("repeat_last_n", repeat_last_n, MAGENTA);
print_float("repeat_penalty", repeat_penalty);
print_bool("mirostat", mirostat);
print_float("mirostat_tau", mirostat_tau);
print_float("mirostat_eta", mirostat_eta);
print_bool("penalize_nl", penalize_nl);
print_bool("ignore_eos", ignore_eos);
print_kv("n_probs", n_probs, MAGENTA);
print_kv("min_keep", min_keep, MAGENTA);
oss << format_utils::print_kv("seed", std::to_string(seed),
format_utils::MAGENTA);
oss << format_utils::print_float("dynatemp_range", dynatemp_range);
oss << format_utils::print_float("dynatemp_exponent", dynatemp_exponent);
oss << format_utils::print_kv("top_k", std::to_string(top_k),
format_utils::MAGENTA);
oss << format_utils::print_float("min_p", min_p);
oss << format_utils::print_float("tfs_z", tfs_z);
oss << format_utils::print_float("typ_p", typ_p);
oss << format_utils::print_kv(
"repeat_last_n", std::to_string(repeat_last_n), format_utils::MAGENTA);
oss << format_utils::print_float("repeat_penalty", repeat_penalty);
oss << format_utils::print_bool("mirostat", mirostat);
oss << format_utils::print_float("mirostat_tau", mirostat_tau);
oss << format_utils::print_float("mirostat_eta", mirostat_eta);
oss << format_utils::print_bool("penalize_nl", penalize_nl);
oss << format_utils::print_bool("ignore_eos", ignore_eos);
oss << format_utils::print_kv("n_probs", std::to_string(n_probs),
format_utils::MAGENTA);
oss << format_utils::print_kv("min_keep", std::to_string(min_keep),
format_utils::MAGENTA);

print_comment("END OPTIONAL");
print_comment("END INFERENCE PARAMETERS");
print_comment("BEGIN MODEL LOAD PARAMETERS");
print_comment("BEGIN REQUIRED");
oss << format_utils::print_comment("END OPTIONAL");
oss << format_utils::print_comment("END INFERENCE PARAMETERS");
oss << format_utils::print_comment("BEGIN MODEL LOAD PARAMETERS");
oss << format_utils::print_comment("BEGIN REQUIRED");

if (!engine.empty())
print_kv("engine", engine, YELLOW);
oss << format_utils::print_kv("engine", engine, format_utils::YELLOW);
if (!prompt_template.empty())
print_kv("prompt_template", prompt_template, YELLOW);
oss << format_utils::print_kv("prompt_template", prompt_template,
format_utils::YELLOW);

print_comment("END REQUIRED");
print_comment("BEGIN OPTIONAL");
oss << format_utils::print_comment("END REQUIRED");
oss << format_utils::print_comment("BEGIN OPTIONAL");

if (ctx_len != std::numeric_limits<int>::quiet_NaN())
print_kv("ctx_len", ctx_len, MAGENTA);
oss << format_utils::print_kv("ctx_len", std::to_string(ctx_len),
format_utils::MAGENTA);
if (ngl != std::numeric_limits<int>::quiet_NaN())
print_kv("ngl", ngl, MAGENTA);
oss << format_utils::print_kv("ngl", std::to_string(ngl),
format_utils::MAGENTA);

print_comment("END OPTIONAL");
print_comment("END MODEL LOAD PARAMETERS");
oss << format_utils::print_comment("END OPTIONAL");
oss << format_utils::print_comment("END MODEL LOAD PARAMETERS");

return oss.str();
}
Expand Down
110 changes: 58 additions & 52 deletions engine/config/yaml_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <string>
using namespace std;

#include "utils/format_utils.h"
#include "yaml_config.h"

namespace config {
// Method to read YAML file
void YamlHandler::Reset() {
Expand Down Expand Up @@ -271,27 +271,20 @@ void YamlHandler::WriteYamlFile(const std::string& file_path) const {
if (!outFile) {
throw std::runtime_error("Failed to open output file.");
}
// Helper function to write a key-value pair with an optional comment
auto writeKeyValue = [&](const std::string& key, const YAML::Node& value,
const std::string& comment = "") {
if (!value)
return;
outFile << key << ": " << value;
if (!comment.empty()) {
outFile << " # " << comment;
}
outFile << "\n";
};

// Write GENERAL GGUF METADATA
outFile << "# BEGIN GENERAL GGUF METADATA\n";
writeKeyValue("id", yaml_node_["id"],
"Model ID unique between models (author / quantization)");
writeKeyValue("model", yaml_node_["model"],
"Model ID which is used for request construct - should be "
"unique between models (author / quantization)");
writeKeyValue("name", yaml_node_["name"], "metadata.general.name");
writeKeyValue("version", yaml_node_["version"], "metadata.version");
outFile << format_utils::writeKeyValue(
"id", yaml_node_["id"],
"Model ID unique between models (author / quantization)");
outFile << format_utils::writeKeyValue(
"model", yaml_node_["model"],
"Model ID which is used for request construct - should be "
"unique between models (author / quantization)");
outFile << format_utils::writeKeyValue("name", yaml_node_["name"],
"metadata.general.name");
if (yaml_node_["version"]) {
outFile << "version: " << yaml_node_["version"].as<std::string>() << "\n";
}
if (yaml_node_["files"] && yaml_node_["files"].size()) {
outFile << "files: # can be universal protocol (models://) "
"OR absolute local file path (file://) OR https remote URL "
Expand All @@ -316,52 +309,65 @@ void YamlHandler::WriteYamlFile(const std::string& file_path) const {
outFile << "# END REQUIRED\n";
outFile << "\n";
outFile << "# BEGIN OPTIONAL\n";
writeKeyValue("stream", yaml_node_["stream"], "Default true?");
writeKeyValue("top_p", yaml_node_["top_p"], "Ranges: 0 to 1");
writeKeyValue("temperature", yaml_node_["temperature"], "Ranges: 0 to 1");
writeKeyValue("frequency_penalty", yaml_node_["frequency_penalty"],
"Ranges: 0 to 1");
writeKeyValue("presence_penalty", yaml_node_["presence_penalty"],
"Ranges: 0 to 1");
writeKeyValue("max_tokens", yaml_node_["max_tokens"],
"Should be default to context length");
writeKeyValue("seed", yaml_node_["seed"]);
writeKeyValue("dynatemp_range", yaml_node_["dynatemp_range"]);
writeKeyValue("dynatemp_exponent", yaml_node_["dynatemp_exponent"]);
writeKeyValue("top_k", yaml_node_["top_k"]);
writeKeyValue("min_p", yaml_node_["min_p"]);
writeKeyValue("tfs_z", yaml_node_["tfs_z"]);
writeKeyValue("typ_p", yaml_node_["typ_p"]);
writeKeyValue("repeat_last_n", yaml_node_["repeat_last_n"]);
writeKeyValue("repeat_penalty", yaml_node_["repeat_penalty"]);
writeKeyValue("mirostat", yaml_node_["mirostat"]);
writeKeyValue("mirostat_tau", yaml_node_["mirostat_tau"]);
writeKeyValue("mirostat_eta", yaml_node_["mirostat_eta"]);
writeKeyValue("penalize_nl", yaml_node_["penalize_nl"]);
writeKeyValue("ignore_eos", yaml_node_["ignore_eos"]);
writeKeyValue("n_probs", yaml_node_["n_probs"]);
writeKeyValue("min_keep", yaml_node_["min_keep"]);
writeKeyValue("grammar", yaml_node_["grammar"]);
outFile << format_utils::writeKeyValue("stream", yaml_node_["stream"],
"Default true?");
outFile << format_utils::writeKeyValue("top_p", yaml_node_["top_p"],
"Ranges: 0 to 1");
outFile << format_utils::writeKeyValue(
"temperature", yaml_node_["temperature"], "Ranges: 0 to 1");
outFile << format_utils::writeKeyValue(
"frequency_penalty", yaml_node_["frequency_penalty"], "Ranges: 0 to 1");
outFile << format_utils::writeKeyValue(
"presence_penalty", yaml_node_["presence_penalty"], "Ranges: 0 to 1");
outFile << format_utils::writeKeyValue(
"max_tokens", yaml_node_["max_tokens"],
"Should be default to context length");
outFile << format_utils::writeKeyValue("seed", yaml_node_["seed"]);
outFile << format_utils::writeKeyValue("dynatemp_range",
yaml_node_["dynatemp_range"]);
outFile << format_utils::writeKeyValue("dynatemp_exponent",
yaml_node_["dynatemp_exponent"]);
outFile << format_utils::writeKeyValue("top_k", yaml_node_["top_k"]);
outFile << format_utils::writeKeyValue("min_p", yaml_node_["min_p"]);
outFile << format_utils::writeKeyValue("tfs_z", yaml_node_["tfs_z"]);
outFile << format_utils::writeKeyValue("typ_p", yaml_node_["typ_p"]);
outFile << format_utils::writeKeyValue("repeat_last_n",
yaml_node_["repeat_last_n"]);
outFile << format_utils::writeKeyValue("repeat_penalty",
yaml_node_["repeat_penalty"]);
outFile << format_utils::writeKeyValue("mirostat", yaml_node_["mirostat"]);
outFile << format_utils::writeKeyValue("mirostat_tau",
yaml_node_["mirostat_tau"]);
outFile << format_utils::writeKeyValue("mirostat_eta",
yaml_node_["mirostat_eta"]);
outFile << format_utils::writeKeyValue("penalize_nl",
yaml_node_["penalize_nl"]);
outFile << format_utils::writeKeyValue("ignore_eos",
yaml_node_["ignore_eos"]);
outFile << format_utils::writeKeyValue("n_probs", yaml_node_["n_probs"]);
outFile << format_utils::writeKeyValue("min_keep", yaml_node_["min_keep"]);
outFile << format_utils::writeKeyValue("grammar", yaml_node_["grammar"]);
outFile << "# END OPTIONAL\n";
outFile << "# END INFERENCE PARAMETERS\n";
outFile << "\n";
// Write MODEL LOAD PARAMETERS
outFile << "# BEGIN MODEL LOAD PARAMETERS\n";
outFile << "# BEGIN REQUIRED\n";
writeKeyValue("engine", yaml_node_["engine"], "engine to run model");
outFile << format_utils::writeKeyValue("engine", yaml_node_["engine"],
"engine to run model");
outFile << "prompt_template:";
outFile << " " << yaml_node_["prompt_template"] << "\n";
outFile << "# END REQUIRED\n";
outFile << "\n";
outFile << "# BEGIN OPTIONAL\n";
writeKeyValue("ctx_len", yaml_node_["ctx_len"],
"llama.context_length | 0 or undefined = loaded from model");
writeKeyValue("ngl", yaml_node_["ngl"], "Undefined = loaded from model");
outFile << format_utils::writeKeyValue(
"ctx_len", yaml_node_["ctx_len"],
"llama.context_length | 0 or undefined = loaded from model");
outFile << format_utils::writeKeyValue("ngl", yaml_node_["ngl"],
"Undefined = loaded from model");
outFile << "# END OPTIONAL\n";
outFile << "# END MODEL LOAD PARAMETERS\n";

// Write new configuration parameters

outFile.close();
} catch (const std::exception& e) {
std::cerr << "Error writing to file: " << e.what() << std::endl;
Expand Down
Loading
Loading