fix(router): count Halogen's pinned weights as used when loading beside it

MemAvailable counts Halogen's 63 GiB of pinned weights as reclaimable
cache, so with Halogen resident lemond saw 78 GiB free where 15 GiB was.
A 22 GB model then loaded beside it and overcommitted the host. A backend
now reports such memory; the router evicts its holder when the next model
does not fit beside it, and auto-tune counts it as used when it does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Jeremy FowersandClaude Opus 5.5 committed 2026-10-30 10:55:51 -04:00
1 parent 84c23c2862
commit bfccc318c4
6 files changed
+62 -8

No files matched your search

+7 -5
View File
@@ -95,7 +95,8 @@ inline double get_available_memory_gb(DeviceType device_type,
GpuMemoryVendor gpu_vendor = GpuMemoryVendor::Any,
const std::string& gpu_device = "",
bool* unified_gpu_out = nullptr,
bool* memory_known_out = nullptr) {
bool* memory_known_out = nullptr,
double unreported_used_gb = 0.0) {
if (unified_gpu_out) *unified_gpu_out = false;
if (memory_known_out) *memory_known_out = false;
auto si = create_system_info();
@@ -129,7 +130,7 @@ inline double get_available_memory_gb(DeviceType device_type,
if (metrics) physical_total_gb = metrics->get_memory_total_gb();
}
const double ram_used_gb = pool.unified
? get_used_memory_gb(DEVICE_CPU) : -1.0;
? get_used_memory_gb(DEVICE_CPU) + unreported_used_gb : -1.0;
const auto available_gpu_gb = auto_context_available_gpu_gib(
pool.total_gb, gpu_used_gb, pool.unified,
physical_total_gb, ram_used_gb);
@@ -155,7 +156,7 @@ inline double get_available_memory_gb(DeviceType device_type,
}
// CPU / NPU: use system RAM
const double used_gb = get_used_memory_gb(device_type);
const double used_gb = get_used_memory_gb(device_type) + unreported_used_gb;
// Apple Silicon: the full unified-memory pool (total physical RAM) is reported
// as virtual_gb on the Apple Silicon device.
auto apple = si->get_apple_silicon_device();
@@ -350,7 +351,8 @@ inline int64_t compute_auto_context_size(const ModelInfo& model_info,
/// (i.e. ctx_size was already set to an explicit non-negative value).
/// The caller should check the return value and update RecipeOptions accordingly.
inline int64_t resolve_auto_ctx_size(const RecipeOptions& effective_options,
const ModelInfo& model_info) {
const ModelInfo& model_info,
double unreported_used_gb = 0.0) {
json ctx_json = effective_options.get_option("ctx_size");
int64_t ctx_size = ctx_json.is_number() ? ctx_json.get<int64_t>() : -1;
@@ -370,7 +372,7 @@ inline int64_t resolve_auto_ctx_size(const RecipeOptions& effective_options,
bool memory_known = false;
double available_gb = get_available_memory_gb(
model_info.device, gpu_memory_vendor_for_target(backend, device), device,
&ctx_unified_gpu, &memory_known);
&ctx_unified_gpu, &memory_known, unreported_used_gb);
if (available_gb <= 0) {
int64_t fallback = is_embedding ? EMBEDDING_CTX_SIZE : AUTO_CTX_FALLBACK;
@@ -17,10 +17,14 @@ public:
void load(const std::string& model_name, const ModelInfo& model_info,
const RecipeOptions& options, bool do_not_upgrade = false) override;
void unload() override;
double unreported_resident_gb() const override { return pinned_weights_gb_; }
json chat_completion(const json& request) override;
json completion(const json& request) override;
json responses(const json& request) override;
private:
double pinned_weights_gb_ = 0.0;
};
namespace halogen {
+1
View File
@@ -347,6 +347,7 @@ private:
const std::string& model_name) const;
void ensure_residency_capacity(ModelType type, ResidencyClass residency_class,
const std::string& model_name);
double make_room_beside_unreported_residents(const ModelInfo& model_info);
void transition_server_residency_locked(
WrappedServer* server,
ResidencyClass requested_residency_class);
+5
View File
@@ -506,6 +506,11 @@ public:
return descriptor_ ? descriptor_->slot_policy : SlotPolicy::Standard;
}
// Every free-memory reading lemond takes counts reclaimable page cache as
// free, so memory a backend holds there (Halogen's pinned weights) would be
// promised to the next model too. Returns that memory in GiB. Default: 0.
virtual double unreported_resident_gb() const { return 0.0; }
// Dynamic availability check. Returns "" if the backend can run on this
// system, or a user-facing reason why it cannot. Defaults to "available";
// backends with runtime-dependent availability (cloud) override.
@@ -171,9 +171,9 @@ void HalogenServer::load(const std::string& model_name, const ModelInfo& model_i
// host already short of the weights plus that pool it stalls in the kernel
// instead of failing. Failing here lets the router evict the other models
// and retry. The lookup table is paged on demand and needs no room.
double weights_gb = fs::file_size(checkpoint) / BYTES_PER_GIB;
if (!vision.empty()) weights_gb += fs::file_size(vision) / BYTES_PER_GIB;
if (model_info.min_resident_gb > 0.0) {
double weights_gb = fs::file_size(checkpoint) / BYTES_PER_GIB;
if (!vision.empty()) weights_gb += fs::file_size(vision) / BYTES_PER_GIB;
const double needed_gb = weights_gb + model_info.min_resident_gb;
const auto metrics = create_metrics_platform();
const double total_gb = metrics ? metrics->get_memory_total_gb() : 0.0;
@@ -219,10 +219,12 @@ void HalogenServer::load(const std::string& model_name, const ModelInfo& model_i
BackendUtils::get_backend_image(halogen::descriptor.recipe, kBackend)),
command,
(std::max)(kStartupTimeoutSeconds, utils::HttpClient::get_default_timeout()));
pinned_weights_gb_ = weights_gb;
}
void HalogenServer::unload() {
stop_server();
pinned_weights_gb_ = 0.0;
}
json HalogenServer::chat_completion(const json& request) {
+41 -1
View File
@@ -368,6 +368,43 @@ void Router::ensure_residency_capacity(
evict_server(lru);
}
// Returns the unreported memory still resident after making room, for
// auto-tune to count as used.
double Router::make_room_beside_unreported_residents(const ModelInfo& model_info) {
double unreported_gb = 0.0;
for (const auto& server : loaded_servers_) {
if (server->is_backend_alive()) unreported_gb += server->unreported_resident_gb();
}
if (unreported_gb <= 0.0) return 0.0;
auto metrics = create_metrics_platform();
const double total_gb = metrics ? metrics->get_memory_total_gb() : 0.0;
if (total_gb <= 0.0) return unreported_gb;
const double free_gb = total_gb - metrics->get_memory_usage_gb() - unreported_gb;
const auto* desc = backends::descriptor_for(model_info.recipe);
const double working_set_gb =
(desc && desc->streams_model_from_storage && model_info.min_resident_gb > 0.0
? model_info.min_resident_gb
: model_info.size) * 1e9 / BYTES_PER_GIB;
if (working_set_gb <= free_gb) return unreported_gb;
std::vector<WrappedServer*> holders;
for (const auto& server : loaded_servers_) {
if (server->is_backend_alive() && server->unreported_resident_gb() > 0.0 &&
!server->is_pinned()) {
holders.push_back(server.get());
}
}
for (auto* holder : holders) {
LOG(INFO, "Router") << model_info.model_name << " needs about " << working_set_gb
<< " GiB and " << free_gb << " GiB is free beside "
<< holder->get_model_name() << ", evicting it" << std::endl;
unreported_gb -= holder->unreported_resident_gb();
evict_server(holder);
}
return (std::max)(0.0, unreported_gb);
}
void Router::transition_server_residency_locked(
WrappedServer* server,
ResidencyClass requested_residency_class) {
@@ -1041,9 +1078,12 @@ void Router::load_model(const std::string& model_name,
canonical_model_name);
}
const double unreported_gb =
is_unmetered_load ? 0.0 : make_room_beside_unreported_residents(model_info);
// Auto-tune: resolve ctx_size = -1 → computed from memory + arch metadata
// Done AFTER eviction so that freed VRAM/RAM is visible to the memory query.
int64_t auto_ctx = resolve_auto_ctx_size(effective_options, model_info);
int64_t auto_ctx = resolve_auto_ctx_size(effective_options, model_info, unreported_gb);
const bool ctx_size_auto = auto_ctx != -2;
if (auto_ctx > 0) {
LOG(INFO, "Router") << "Auto-tune ctx_size resolved to " << auto_ctx << std::endl;