From: Meshnet <meshnet@invalid>
Subject: [PATCH] llama: report owned-range filtered loading state

Concern: filtered state. Adds the llama_meshnet_range_report value type and
populates it after owned-range tensor registration with the half-open bounds
and mapped/resident byte counts derived from backend buffers, never from
caller parameters. Layer-filtered KV and session-to-sequence mapping remain
later scoped stories; this patch carries only the owned-range state report.
---
diff --git a/include/llama.h b/include/llama.h
index 229946ede..6fd7ad509 100644
--- a/include/llama.h
+++ b/include/llama.h
@@ -292,6 +292,16 @@ extern "C" {
         ggml_backend_buffer_type_t buft;
     };
 
+    // Immutable report for the project-owned dense-Llama owned-range state.
+    // Bounds are half-open [start, end); byte counts are derived from the
+    // registered tensors and backend buffers, never from caller parameters.
+    struct llama_meshnet_range_report {
+        int32_t start_layer;
+        int32_t end_layer;
+        uint64_t mapped_bytes;
+        uint64_t resident_bytes;
+    };
+
     struct llama_model_params {
         // NULL-terminated list of devices to use for offloading (if NULL, all available devices are used)
         ggml_backend_dev_t * devices;
diff --git a/src/llama-model.cpp b/src/llama-model.cpp
index 05b8b9c91..efb290c1f 100644
--- a/src/llama-model.cpp
+++ b/src/llama-model.cpp
@@ -1015,6 +1015,9 @@ struct llama_model::impl {
     bool has_tensor_overrides;
 
     std::vector<float> tensor_split_owned;
+
+    llama_meshnet_range_report meshnet_range_report = {};
+    bool has_meshnet_range_report = false;
 };
 
 llama_model::llama_model(const llama_model_params & params) : params(params), pimpl(std::make_unique<impl>()) {
@@ -1628,13 +1631,33 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) {
     }
 
     // print memory requirements per buffer type
+    uint64_t meshnet_mapped_bytes = 0;
+    uint64_t meshnet_resident_bytes = 0;
     for (auto & [_, bufs] : pimpl->ctxs_bufs) {
         for (auto & buf: bufs) {
+            meshnet_resident_bytes += ggml_backend_buffer_get_size(buf.get());
             LLAMA_LOG_INFO("%s: %12s model buffer size = %8.2f MiB\n",
                 __func__, ggml_backend_buffer_name(buf.get()), ggml_backend_buffer_get_size(buf.get()) / 1024.0 / 1024.0);
         }
     }
 
+    if (meshnet_range_requested) {
+        // With mmap backend buffers the resident mapping exactly describes the
+        // mapped file spans of the owned tensors. On non-mmap backends the
+        // instantiated allocation is the resident measure and no file span is
+        // claimed as mapped.
+        if (ml.use_mmap) {
+            meshnet_mapped_bytes = meshnet_resident_bytes;
+        }
+        pimpl->meshnet_range_report = {
+            meshnet_start,
+            meshnet_end,
+            meshnet_mapped_bytes,
+            meshnet_resident_bytes,
+        };
+        pimpl->has_meshnet_range_report = true;
+    }
+
     if (ml.no_alloc) {
         return true;
     }
@@ -1726,6 +1749,14 @@ uint64_t llama_model::n_elements() const {
     return pimpl->n_elements;
 }
 
+bool llama_model::meshnet_range_report(llama_meshnet_range_report * out) const {
+    if (out == nullptr || !pimpl->has_meshnet_range_report) {
+        return false;
+    }
+    *out = pimpl->meshnet_range_report;
+    return true;
+}
+
 void llama_model::print_info() const {
     const std::string rope_scaling_type = llama_rope_scaling_type_name(hparams.rope_scaling_type_train);
 
diff --git a/src/llama-model.h b/src/llama-model.h
index 45b054ced..5ef7a1515 100644
--- a/src/llama-model.h
+++ b/src/llama-model.h
@@ -652,6 +652,9 @@ struct llama_model {
     // total number of parameters in the model
     uint64_t n_elements() const;
 
+    // Project-owned owned-range state report; false when no range was loaded.
+    bool meshnet_range_report(llama_meshnet_range_report * out) const;
+
     void print_info() const;
 
     ggml_backend_dev_t dev_layer(int il) const;
