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

Commit 5684fe6

Browse files
authored
chore: return model size after pulled (#1626)
* chore: return model size after pulled * chore: remove double cast checking
1 parent 8961a0d commit 5684fe6

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

docs/static/openapi/cortex.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3434,6 +3434,11 @@
34343434
"description": "To enable mmap, default is true",
34353435
"example": true
34363436
},
3437+
"size": {
3438+
"type": "number",
3439+
"description": "The model file size in bytes",
3440+
"example": 1073741824
3441+
},
34373442
"engine": {
34383443
"type": "string",
34393444
"description": "The engine to use.",

engine/config/model_config.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ struct ModelConfig {
5858
bool ignore_eos = false;
5959
int n_probs = 0;
6060
int min_keep = 0;
61+
uint64_t size = 0;
6162
std::string grammar;
6263

6364
void FromJson(const Json::Value& json) {
@@ -70,6 +71,8 @@ struct ModelConfig {
7071
// model = json["model"].asString();
7172
if (json.isMember("version"))
7273
version = json["version"].asString();
74+
if (json.isMember("size"))
75+
size = json["size"].asUInt64();
7376

7477
if (json.isMember("stop") && json["stop"].isArray()) {
7578
stop.clear();
@@ -176,6 +179,7 @@ struct ModelConfig {
176179
obj["name"] = name;
177180
obj["model"] = model;
178181
obj["version"] = version;
182+
obj["size"] = size;
179183

180184
Json::Value stop_array(Json::arrayValue);
181185
for (const auto& s : stop) {
@@ -269,6 +273,7 @@ struct ModelConfig {
269273
oss << format_utils::print_comment("END REQUIRED");
270274
oss << format_utils::print_comment("BEGIN OPTIONAL");
271275

276+
oss << format_utils::print_float("size", size);
272277
oss << format_utils::print_bool("stream", stream);
273278
oss << format_utils::print_float("top_p", top_p);
274279
oss << format_utils::print_float("temperature", temperature);

engine/config/yaml_config.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ void YamlHandler::ModelConfigFromYaml() {
7575
tmp.model = yaml_node_["model"].as<std::string>();
7676
if (yaml_node_["version"])
7777
tmp.version = yaml_node_["version"].as<std::string>();
78+
if (yaml_node_["size"])
79+
tmp.size = yaml_node_["size"].as<uint64_t>();
7880
if (yaml_node_["engine"])
7981
tmp.engine = yaml_node_["engine"].as<std::string>();
8082
if (yaml_node_["prompt_template"]) {
@@ -266,6 +268,8 @@ void YamlHandler::UpdateModelConfig(ModelConfig new_model_config) {
266268
if (!model_config_.grammar.empty())
267269
yaml_node_["grammar"] = model_config_.grammar;
268270

271+
yaml_node_["size"] = model_config_.size;
272+
269273
yaml_node_["created"] = std::time(nullptr);
270274
} catch (const std::exception& e) {
271275
std::cerr << "Error when update model config : " << e.what() << std::endl;
@@ -318,6 +322,7 @@ void YamlHandler::WriteYamlFile(const std::string& file_path) const {
318322
outFile << "# END REQUIRED\n";
319323
outFile << "\n";
320324
outFile << "# BEGIN OPTIONAL\n";
325+
outFile << format_utils::writeKeyValue("size", yaml_node_["size"]);
321326
outFile << format_utils::writeKeyValue("stream", yaml_node_["stream"],
322327
"Default true?");
323328
outFile << format_utils::writeKeyValue("top_p", yaml_node_["top_p"],

engine/services/model_service.cc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
namespace {
1919
void ParseGguf(const DownloadItem& ggufDownloadItem,
2020
std::optional<std::string> author,
21-
std::optional<std::string> name) {
21+
std::optional<std::string> name,
22+
std::optional<std::uint64_t> size) {
2223
namespace fs = std::filesystem;
2324
namespace fmu = file_manager_utils;
2425
config::GGUFHandler gguf_handler;
@@ -35,6 +36,7 @@ void ParseGguf(const DownloadItem& ggufDownloadItem,
3536
model_config.model = ggufDownloadItem.id;
3637
model_config.name =
3738
name.has_value() ? name.value() : gguf_handler.GetModelConfig().name;
39+
model_config.size = size.value_or(0);
3840
yaml_handler.UpdateModelConfig(model_config);
3941

4042
auto yaml_path{ggufDownloadItem.localPath};
@@ -284,8 +286,13 @@ cpp::result<DownloadTask, std::string> ModelService::HandleDownloadUrlAsync(
284286
}}}};
285287

286288
auto on_finished = [author, temp_name](const DownloadTask& finishedTask) {
289+
// Sum downloadedBytes from all items
290+
uint64_t model_size = 0;
291+
for (const auto& item : finishedTask.items) {
292+
model_size = model_size + item.bytes.value_or(0);
293+
}
287294
auto gguf_download_item = finishedTask.items[0];
288-
ParseGguf(gguf_download_item, author, temp_name);
295+
ParseGguf(gguf_download_item, author, temp_name, model_size);
289296
};
290297

291298
downloadTask.id = unique_model_id;
@@ -349,8 +356,13 @@ cpp::result<std::string, std::string> ModelService::HandleUrl(
349356
}}}};
350357

351358
auto on_finished = [author](const DownloadTask& finishedTask) {
359+
// Sum downloadedBytes from all items
360+
uint64_t model_size = 0;
361+
for (const auto& item : finishedTask.items) {
362+
model_size = model_size + item.bytes.value_or(0);
363+
}
352364
auto gguf_download_item = finishedTask.items[0];
353-
ParseGguf(gguf_download_item, author, std::nullopt);
365+
ParseGguf(gguf_download_item, author, std::nullopt, model_size);
354366
};
355367

356368
auto result = download_service_->AddDownloadTask(downloadTask, on_finished);

0 commit comments

Comments
 (0)