diff --git a/include/llama.h b/include/llama.h index 11c03a6ae0a7..0e59fe0df224 100644 --- a/include/llama.h +++ b/include/llama.h @@ -725,6 +725,13 @@ extern "C" { LLAMA_API const char * llama_model_weight_backend_name (const struct llama_model * model, size_t i); LLAMA_API size_t llama_model_weight_backend_bytes(const struct llama_model * model, size_t i); + // Account for the allocator's actual memory pool, including pinned host buffers. + struct llama_weight_allocation { + size_t host; + size_t device; + }; + LLAMA_API struct llama_weight_allocation llama_model_weight_allocation(const struct llama_model * model); + // Get the default chat template. Returns nullptr if not available // If name is NULL, returns the default chat template LLAMA_API const char * llama_model_chat_template(const struct llama_model * model, const char * name); diff --git a/src/llama-model.cpp b/src/llama-model.cpp index 63fa565e7843..f3fc3060f187 100644 --- a/src/llama-model.cpp +++ b/src/llama-model.cpp @@ -3063,6 +3063,18 @@ size_t llama_model_n_weight_backends(const llama_model * model) { return llama_model_weight_backends(model).size(); } +llama_weight_allocation llama_model_weight_allocation(const llama_model * model) { + llama_weight_allocation allocation = {}; + for (const auto & [buft, bytes] : model->memory_breakdown()) { + if (ggml_backend_buft_is_host(buft)) { + allocation.host += bytes; + } else { + allocation.device += bytes; + } + } + return allocation; +} + const char * llama_model_weight_backend_name(const llama_model * model, size_t i) { const auto v = llama_model_weight_backends(model); return i < v.size() ? v[i].first : nullptr;