From 442eefb8e766412ad79d27cca23a2798a1a5876c Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 17 Oct 2024 10:28:07 +0700 Subject: [PATCH 1/2] fix: revert check type for json on model update command --- engine/commands/model_upd_cmd.cc | 288 ++++++++++++++++++++++++++++++- engine/commands/model_upd_cmd.h | 14 ++ 2 files changed, 299 insertions(+), 3 deletions(-) diff --git a/engine/commands/model_upd_cmd.cc b/engine/commands/model_upd_cmd.cc index 18edf1b61..4f0ad2df5 100644 --- a/engine/commands/model_upd_cmd.cc +++ b/engine/commands/model_upd_cmd.cc @@ -1,6 +1,6 @@ #include "model_upd_cmd.h" #include "httplib.h" -#include "json/json.h" + #include "server_start_cmd.h" #include "utils/file_manager_utils.h" #include "utils/logging_utils.h" @@ -26,8 +26,7 @@ void ModelUpdCmd::Exec( Json::Value json_data; for (const auto& [key, value] : options) { if (!value.empty()) { - json_data[key] = value; - CLI_LOG("Updated " << key << " to: " << value); + UpdateConfig(json_data, key, value); } } auto data_str = json_data.toStyledString(); @@ -47,4 +46,287 @@ void ModelUpdCmd::Exec( return; } } + +void ModelUpdCmd::UpdateConfig(Json::Value& data, const std::string& key, + const std::string& value) { + static const std::unordered_map< + std::string, + std::function> + updaters = { + {"name", + [](Json::Value &data, const std::string&, const std::string& v) { + data["name"] = v; + }}, + {"model", + [](Json::Value &data, const std::string&, const std::string& v) { + data["model"] = v; + }}, + {"version", + [](Json::Value &data, const std::string&, const std::string& v) { + data["version"] = v; + }}, + {"engine", + [](Json::Value &data, const std::string&, const std::string& v) { + data["engine"] = v; + }}, + {"prompt_template", + [](Json::Value &data, const std::string&, const std::string& v) { + data["prompt_template"] = v; + }}, + {"system_template", + [](Json::Value &data, const std::string&, const std::string& v) { + data["system_template"] = v; + }}, + {"user_template", + [](Json::Value &data, const std::string&, const std::string& v) { + data["user_template"] = v; + }}, + {"ai_template", + [](Json::Value &data, const std::string&, const std::string& v) { + data["ai_template"] = v; + }}, + {"os", + [](Json::Value &data, const std::string&, const std::string& v) { + data["os"] = v; + }}, + {"gpu_arch", + [](Json::Value &data, const std::string&, const std::string& v) { + data["gpu_arch"] = v; + }}, + {"quantization_method", + [](Json::Value &data, const std::string&, const std::string& v) { + data["quantization_method"] = v; + }}, + {"precision", + [](Json::Value &data, const std::string&, const std::string& v) { + data["precision"] = v; + }}, + {"trtllm_version", + [](Json::Value &data, const std::string&, const std::string& v) { + data["trtllm_version"] = v; + }}, + {"object", + [](Json::Value &data, const std::string&, const std::string& v) { + data["object"] = v; + }}, + {"owned_by", + [](Json::Value &data, const std::string&, const std::string& v) { + data["owned_by"] = v; + }}, + {"grammar", + [](Json::Value &data, const std::string&, const std::string& v) { + data["grammar"] = v; + }}, + {"stop", [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateVectorField( + k, v, [&data](const std::vector& stops) { + Json::Value d(Json::arrayValue); + for (auto const& s: stops) { + d.append(s); + } + data["stop"] = d; + }); + }}, + {"files", [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateVectorField( + k, v, [&data](const std::vector& fs) { + Json::Value d(Json::arrayValue); + for (auto const& f: fs) { + d.append(f); + } + data["files"] = d; + }); + }}, + {"top_p", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField( + k, v, [&data](float f) { data["top_p"] = f; }); + }}, + {"temperature", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["temperature"] = f; + }); + }}, + {"frequency_penalty", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["frequency_penalty"] = f; + }); + }}, + {"presence_penalty", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["presence_penalty"] = f; + }); + }}, + {"dynatemp_range", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["dynatemp_range"] = f; + }); + }}, + {"dynatemp_exponent", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["dynatemp_exponent"] = f; + }); + }}, + {"min_p", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField( + k, v, [&data](float f) { data["min_p"] = f; }); + }}, + {"tfs_z", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField( + k, v, [&data](float f) { data["tfs_z"] = f; }); + }}, + {"typ_p", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField( + k, v, [&data](float f) { data["typ_p"] = f; }); + }}, + {"repeat_penalty", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["repeat_penalty"] = f; + }); + }}, + {"mirostat_tau", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["mirostat_tau"] = f; + }); + }}, + {"mirostat_eta", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["mirostat_eta"] = f; + }); + }}, + {"max_tokens", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["max_tokens"] = static_cast(f); + }); + }}, + {"ngl", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["ngl"] = static_cast(f); + }); + }}, + {"ctx_len", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["ctx_len"] = static_cast(f); + }); + }}, + {"tp", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["tp"] = static_cast(f); + }); + }}, + {"seed", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["seed"] = static_cast(f); + }); + }}, + {"top_k", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["top_k"] = static_cast(f); + }); + }}, + {"repeat_last_n", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["repeat_last_n"] = static_cast(f); + }); + }}, + {"n_probs", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["n_probs"] = static_cast(f); + }); + }}, + {"min_keep", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["min_keep"] = static_cast(f); + }); + }}, + {"stream", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateBooleanField( + k, v, [&data](bool b) { data["stream"] = b; }); + }}, + {"text_model", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateBooleanField( + k, v, [&data](bool b) { data["text_model"] = b; }); + }}, + {"mirostat", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateBooleanField( + k, v, [&data](bool b) { data["mirostat"] = b; }); + }}, + {"penalize_nl", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateBooleanField( + k, v, [&data](bool b) { data["penalize_nl"] = b; }); + }}, + {"ignore_eos", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateBooleanField( + k, v, [&data](bool b) { data["ignore_eos"] = b; }); + }}, + {"created", + [this](Json::Value &data, const std::string& k, const std::string& v) { + UpdateNumericField(k, v, [&data](float f) { + data["created"] = static_cast(f); + }); + }}, + }; + + if (auto it = updaters.find(key); it != updaters.end()) { + it->second(data, key, value); + CLI_LOG("Updated " << key << " to: " << value); + } else { + CLI_LOG("Warning: Unknown configuration key '" << key << "' ignored."); + } +} + +void ModelUpdCmd::UpdateVectorField( + const std::string& key, const std::string& value, + std::function&)> setter) { + std::vector tokens; + std::istringstream iss(value); + std::string token; + while (std::getline(iss, token, ',')) { + tokens.push_back(token); + } + setter(tokens); +} + +void ModelUpdCmd::UpdateNumericField(const std::string& key, + const std::string& value, + std::function setter) { + try { + float numericValue = std::stof(value); + setter(numericValue); + } catch (const std::exception& e) { + CLI_LOG("Failed to parse numeric value for " << key << ": " << e.what()); + } +} + +void ModelUpdCmd::UpdateBooleanField(const std::string& key, + const std::string& value, + std::function setter) { + bool boolValue = (value == "true" || value == "1"); + setter(boolValue); +} } // namespace commands \ No newline at end of file diff --git a/engine/commands/model_upd_cmd.h b/engine/commands/model_upd_cmd.h index 6a5e8f42f..f2eaa8675 100644 --- a/engine/commands/model_upd_cmd.h +++ b/engine/commands/model_upd_cmd.h @@ -4,6 +4,8 @@ #include #include #include +#include +#include "json/json.h" namespace commands { class ModelUpdCmd { public: @@ -11,6 +13,18 @@ class ModelUpdCmd { void Exec(const std::string& host, int port, const std::unordered_map& options); + + private: + void UpdateConfig(Json::Value& data, const std::string& key, + const std::string& value); + void UpdateVectorField( + const std::string& key, const std::string& value, + std::function&)> setter); + void UpdateNumericField(const std::string& key, const std::string& value, + std::function setter); + void UpdateBooleanField(const std::string& key, const std::string& value, + std::function setter); + private: std::string model_handle_; }; From 84c49ef3f1ad6481566a4489e1d37ed464231033 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 17 Oct 2024 10:39:11 +0700 Subject: [PATCH 2/2] fix: build --- engine/commands/model_upd_cmd.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/commands/model_upd_cmd.cc b/engine/commands/model_upd_cmd.cc index 4f0ad2df5..0d907357f 100644 --- a/engine/commands/model_upd_cmd.cc +++ b/engine/commands/model_upd_cmd.cc @@ -287,7 +287,7 @@ void ModelUpdCmd::UpdateConfig(Json::Value& data, const std::string& key, {"created", [this](Json::Value &data, const std::string& k, const std::string& v) { UpdateNumericField(k, v, [&data](float f) { - data["created"] = static_cast(f); + data["created"] = static_cast(f); }); }}, };