Avoid loading models that larger than free memory

This commit is contained in:
ozanarmagan 2023-10-17 00:26:36 +03:00
parent a44f996a1b
commit 5766b83f24
2 changed files with 17 additions and 2 deletions

View File

@ -173,4 +173,8 @@ public:
// compute
return compute_cpu_stats(cpu_data_prev, cpu_data_now);
}
static uint64_t get_memory_free_bytes() {
return get_memory_total_bytes() - get_memory_used_bytes();
}
};

View File

@ -1,4 +1,5 @@
#include "text_embedder_manager.h"
#include "system_metrics.h"
TextEmbedderManager& TextEmbedderManager::get_instance() {
@ -107,8 +108,18 @@ Option<bool> TextEmbedderManager::validate_and_init_local_model(const nlohmann::
return Option<bool>(true);
}
const std::shared_ptr<TextEmbedder>& embedder = std::make_shared<TextEmbedder>(
get_model_name_without_namespace(model_name));
const auto& model_name_without_namespace = get_model_name_without_namespace(model_name);
const auto& free_memory = SystemMetrics::get_memory_free_bytes();
const auto& model_file_size = std::filesystem::file_size(abs_path);
// return error if model file size is greater than free memory
if(model_file_size > free_memory) {
LOG(ERROR) << "Model file size is greater than free memory: " << model_file_size << " > " << free_memory;
return Option<bool>(400, "Model file size is greater than free memory");
}
const std::shared_ptr<TextEmbedder>& embedder = std::make_shared<TextEmbedder>(model_name_without_namespace);
auto validate_op = embedder->validate();
if(!validate_op.ok()) {