Skip to content

Commit 3ca60da

Browse files
committed
server: report measured weight residency per backend on /props
A caller that must VERIFY where a model actually ran had no way to ask. The two things that look like an answer are both the REQUEST reflected back: - `n_gpu_layers` is what was asked for. - `load_tensors: offloaded N/M layers to GPU` is `min(n_gpu_layers, n_layer + 1)` — the ask, clamped to the layer count. Pass `--n-gpu-layers 999` to a 29-layer model and it prints "offloaded 29/29" because you asked for 999 and there are 29, not because 29 reached a GPU. So an engine that requested the GPU and silently ran on the CPU prints numbers identical to one that succeeded. Measured on a host where exactly that happened: the server served from system RAM with 500 MiB of a 32 GiB card in use, while every number a caller could reach said the GPU had been used. The banner is also wrapped in `if (llama_supports_gpu_offload())`, so on a build without GPU support it never prints at all — a third distinct meaning for the same absence, and none of the three are distinguishable by a caller. /props now carries the ALLOCATION: "model_weight_buffers": [ { "backend": "CPU_Mapped", "size_bytes": 377487360 }, { "backend": "CUDA0", "size_bytes": 13948538880 } ] Sum the non-CPU backends to get accelerator-resident bytes. This is a thin C view over `llama_model::memory_breakdown()`, which already walks `ctxs_bufs`, aggregates per buffer type and handles the `no_alloc` case — not a second walk of the same data. Note for consumers: a healthy fully-offloaded lane still reports CPU bytes (mapped metadata), so the test is "are there any accelerator bytes", never "are there no host bytes". Verified on Windows/CUDA, RTX 5090, Devstral-Small-2507-Q4_K_M: GET /props returns CUDA0 = 12.99 GiB, CPU_Mapped = 0.35 GiB.
1 parent fb668a8 commit 3ca60da

4 files changed

Lines changed: 64 additions & 0 deletions

File tree

‎include/llama.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,22 @@ extern "C" {
653653
// Returns the total size of all the tensors in the model in bytes
654654
LLAMA_API uint64_t llama_model_size(const struct llama_model * model);
655655

656+
// Where the model's weights ACTUALLY LIVE, aggregated per backend, AS
657+
// ALLOCATED. A thin C view over `llama_model::memory_breakdown()`.
658+
//
659+
// Deliberately NOT `n_gpu_layers`, and not the "offloaded N/M layers to GPU"
660+
// log line: both report what was REQUESTED (the log line is
661+
// `min(n_gpu_layers, n_layer + 1)` — the ask, clamped), so an engine that
662+
// asked for the GPU and silently ran on the CPU reports numbers identical to
663+
// one that succeeded. A caller that must VERIFY placement — a scheduler
664+
// deciding whether a host really is a GPU host — needs the allocation.
665+
//
666+
// Sum the bytes whose backend is not the CPU to get accelerator-resident
667+
// weight bytes. Name returns NULL and bytes returns 0 when `i` is out of range.
668+
LLAMA_API size_t llama_model_n_weight_backends (const struct llama_model * model);
669+
LLAMA_API const char * llama_model_weight_backend_name (const struct llama_model * model, size_t i);
670+
LLAMA_API size_t llama_model_weight_backend_bytes(const struct llama_model * model, size_t i);
671+
656672
// Get the default chat template. Returns nullptr if not available
657673
// If name is NULL, returns the default chat template
658674
LLAMA_API const char * llama_model_chat_template(const struct llama_model * model, const char * name);

‎src/llama-model.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,6 +3043,31 @@ uint64_t llama_model_size(const llama_model * model) {
30433043
return model->size();
30443044
}
30453045

3046+
// Weight residency per backend, over the model's OWN memory_breakdown() — the
3047+
// existing primitive that already walks ctxs_bufs and handles the no_alloc case.
3048+
// Not a second walk of the same data.
3049+
static std::vector<std::pair<const char *, size_t>> llama_model_weight_backends(const llama_model * model) {
3050+
std::vector<std::pair<const char *, size_t>> out;
3051+
for (const auto & [buft, bytes] : model->memory_breakdown()) {
3052+
out.emplace_back(ggml_backend_buft_name(buft), bytes);
3053+
}
3054+
return out;
3055+
}
3056+
3057+
size_t llama_model_n_weight_backends(const llama_model * model) {
3058+
return llama_model_weight_backends(model).size();
3059+
}
3060+
3061+
const char * llama_model_weight_backend_name(const llama_model * model, size_t i) {
3062+
const auto v = llama_model_weight_backends(model);
3063+
return i < v.size() ? v[i].first : nullptr;
3064+
}
3065+
3066+
size_t llama_model_weight_backend_bytes(const llama_model * model, size_t i) {
3067+
const auto v = llama_model_weight_backends(model);
3068+
return i < v.size() ? v[i].second : 0;
3069+
}
3070+
30463071
const char * llama_model_chat_template(const llama_model * model, const char * name) {
30473072
const auto key = name ? LLM_KV(model->arch, name)(LLM_KV_TOKENIZER_CHAT_TEMPLATE)
30483073
: LLM_KV(model->arch)(LLM_KV_TOKENIZER_CHAT_TEMPLATE);

‎tools/server/server-context.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4104,6 +4104,23 @@ server_context_meta server_context::get_meta() const {
41044104
/* model_n_params */ llama_model_n_params(impl->model_tgt),
41054105
/* model_size */ llama_model_size(impl->model_tgt),
41064106
/* model_ftype */ ftype_name,
4107+
/* model_weight_buffers */ [&] {
4108+
// MEASURED placement: where the weights were actually allocated.
4109+
// Not n_gpu_layers, not the "offloaded N/M" banner — both of those
4110+
// echo the REQUEST back (the banner is min(n_gpu_layers, n_layer+1)),
4111+
// so a lane that asked for the GPU and fell back to the CPU prints
4112+
// numbers identical to one that succeeded.
4113+
json bufs = json::array();
4114+
const size_t n = llama_model_n_weight_backends(impl->model_tgt);
4115+
for (size_t i = 0; i < n; i++) {
4116+
const char * backend = llama_model_weight_backend_name(impl->model_tgt, i);
4117+
bufs.push_back(json {
4118+
{ "backend", backend ? backend : "" },
4119+
{ "size_bytes", llama_model_weight_backend_bytes(impl->model_tgt, i) },
4120+
});
4121+
}
4122+
return bufs;
4123+
}(),
41074124
};
41084125
}
41094126

@@ -4498,6 +4515,7 @@ static json get_res_props(const server_context_meta & meta, const common_params
44984515
{ "model_alias", meta.model_name },
44994516
{ "model_ftype", meta.model_ftype },
45004517
{ "model_path", meta.model_path },
4518+
{ "model_weight_buffers", meta.model_weight_buffers },
45014519
{ "modalities", json {
45024520
{"vision", meta.has_inp_image},
45034521
{"video", meta.has_inp_video},

‎tools/server/server-context.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ struct server_context_meta {
5252
uint64_t model_n_params;
5353
uint64_t model_size;
5454
std::string model_ftype;
55+
56+
// Measured weight residency: [{backend, size_bytes}, ...] as ALLOCATED.
57+
// Reported on /props so a caller can verify placement over a CHANNEL rather
58+
// than by scraping the engine's console.
59+
json model_weight_buffers;
5560
};
5661

5762
enum server_state {

0 commit comments

Comments
 (0)