From 59e975650e8daaf1223c969063d15b1f68058891 Mon Sep 17 00:00:00 2001 From: Virgile Pourchet <222121099+Virgile-pct@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:08:48 +0100 Subject: [PATCH 1/2] fix(runtime): plan an integrated GPU's context inside the safety margin On unified memory outside Metal the load budget is system RAM the OS and other programs share, yet the context planner got all of it: the safety margin the node advertises as reserved was never kept free, and a 4B model on a Radeon 780M left about half a GiB of RAM after its first request (#3165). The planner now gets the budget less the margin there, never less than the weights, so the KV cache and compute buffers leave the margin free while every model that loaded before still loads. Device memory budgets are unchanged. --- .../src/runtime/local.rs | 15 +++++-- .../src/runtime/local_memory_plan.rs | 43 +++++++++++++++++++ .../src/runtime/local_model_only.rs | 5 ++- .../src/runtime/local_split/test_support.rs | 2 + 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/mesh/crates/mesh-llm-host-runtime/src/runtime/local.rs b/mesh/crates/mesh-llm-host-runtime/src/runtime/local.rs index 9fdea7d4df..96ecd45038 100644 --- a/mesh/crates/mesh-llm-host-runtime/src/runtime/local.rs +++ b/mesh/crates/mesh-llm-host-runtime/src/runtime/local.rs @@ -5,8 +5,8 @@ use super::context_planning::{ use super::live_request_defaults; use super::local_dflash_plan::{plan_with_default_dflash_draft, planned_default_dflash_draft}; use super::local_memory_plan::{ - MemoryPlanMeasurementKey, MemoryPlanStartPath, emit_measured_memory_reconciliation, - emit_memory_plan_resolved, measured_buffers_footprint, + MemoryPlanMeasurementKey, MemoryPlanStartPath, context_planning_budget_bytes, + emit_measured_memory_reconciliation, emit_memory_plan_resolved, measured_buffers_footprint, }; use super::split_planning::format_gb; use crate::api; @@ -313,6 +313,9 @@ pub(super) struct LocalOpenAiModelStartSpec<'a> { pub(super) pinned_gpu: Option<&'a crate::runtime::StartupPinnedGpuTarget>, pub(super) device_override: Option, pub(super) capacity_budget_bytes: u64, + /// The node's GPU shares system RAM with the OS (an integrated GPU), so + /// the context plan leaves the safety margin free. + pub(super) unified_memory: bool, pub(super) cache_type_k_override: Option<&'a str>, pub(super) cache_type_v_override: Option<&'a str>, pub(super) n_batch_override: Option, @@ -801,6 +804,7 @@ pub(super) async fn start_runtime_local_model( pinned_gpu: spec.pinned_gpu, device_override: spec.device_override, capacity_budget_bytes: local_capacity_bytes, + unified_memory: spec.node.is_soc.unwrap_or(false), cache_type_k_override: spec.cache_type_k_override, cache_type_v_override: spec.cache_type_v_override, n_batch_override: spec.n_batch_override, @@ -1045,7 +1049,12 @@ pub(super) async fn start_local_openai_model( parallel_override: spec.parallel_override, model_bytes: local_model_bytes, projector_bytes: planned_projector_bytes(&spec, &model_name), - vram_bytes: my_vram, + vram_bytes: context_planning_budget_bytes( + my_vram, + spec.unified_memory, + skippy::effective_safety_margin_bytes(spec.mesh_config.defaults.as_ref()), + required_bytes, + ), metadata: compact_meta.as_ref(), kv_cache_quant, local_layer_fraction, diff --git a/mesh/crates/mesh-llm-host-runtime/src/runtime/local_memory_plan.rs b/mesh/crates/mesh-llm-host-runtime/src/runtime/local_memory_plan.rs index 82e0a20123..e5ca1f692e 100644 --- a/mesh/crates/mesh-llm-host-runtime/src/runtime/local_memory_plan.rs +++ b/mesh/crates/mesh-llm-host-runtime/src/runtime/local_memory_plan.rs @@ -98,6 +98,28 @@ fn completed_measurement_snapshot( /// Return a completed native buffer measurement only when its model identity, /// capacity pool, and allocation-affecting configuration all match. +/// The budget the context planner may fill. On unified memory outside Metal +/// the budget is system RAM the OS and other programs share, so the +/// configured safety margin is kept out of the KV cache and compute buffers +/// the planner sizes, the reserve the node already advertises (#3165). The +/// weights are admitted against the full budget, so a model that loads keeps +/// loading; when the weights alone pass the reduced budget, the planner gets +/// just what they need and plans its minimum context. Elsewhere the budget +/// is device memory and passes through unchanged. +pub(super) fn context_planning_budget_bytes( + capacity_bytes: u64, + unified_memory: bool, + safety_margin_bytes: u64, + required_bytes: u64, +) -> u64 { + if !unified_memory || cfg!(target_os = "macos") { + return capacity_bytes; + } + capacity_bytes + .saturating_sub(safety_margin_bytes) + .max(required_bytes.min(capacity_bytes)) +} + pub(super) fn measured_buffers_footprint( key: &MemoryPlanMeasurementKey, ) -> Option { @@ -191,6 +213,27 @@ pub(super) fn emit_measured_memory_reconciliation( #[cfg(test)] mod tests { + #[cfg(not(target_os = "macos"))] + #[test] + fn unified_memory_plans_context_inside_the_safety_margin() { + const GIB: u64 = 1024 * 1024 * 1024; + // A Radeon 780M laptop with 6 GiB of budget and the default 2 GiB margin. + let planned = super::context_planning_budget_bytes(6 * GIB, true, 2 * GIB, 3 * GIB); + assert_eq!(planned, 4 * GIB); + // Weights past the reduced budget still load, on their own size. + let planned = super::context_planning_budget_bytes(6 * GIB, true, 2 * GIB, 5 * GIB); + assert_eq!(planned, 5 * GIB); + } + + #[test] + fn device_memory_budget_is_unchanged() { + const GIB: u64 = 1024 * 1024 * 1024; + assert_eq!( + super::context_planning_budget_bytes(12 * GIB, false, 2 * GIB, 5 * GIB), + 12 * GIB + ); + } + use super::{ MEASURED_PLAN_SNAPSHOT, MeasuredPlanSnapshot, MemoryPlanMeasurementKey, completed_measurement_snapshot, measured_buffers_footprint, diff --git a/mesh/crates/mesh-llm-host-runtime/src/runtime/local_model_only.rs b/mesh/crates/mesh-llm-host-runtime/src/runtime/local_model_only.rs index b9f89a5812..b164bbbf8b 100644 --- a/mesh/crates/mesh-llm-host-runtime/src/runtime/local_model_only.rs +++ b/mesh/crates/mesh-llm-host-runtime/src/runtime/local_model_only.rs @@ -262,9 +262,11 @@ async fn run_local_model_only_inner( let _native_log_forwarding = SkippyNativeLogForwardingGuard; let model_name = model.declared_ref.clone(); + let hardware_survey = hardware::survey(); + let unified_memory = hardware_survey.is_soc; let survey_telemetry = survey::SurveyTelemetry::start( &config, - hardware::survey(), + hardware_survey, survey::SurveyTelemetrySource { node_id: "local-model-only".into(), node_role: "worker".into(), @@ -283,6 +285,7 @@ async fn run_local_model_only_inner( pinned_gpu: model.pinned_gpu.as_ref(), device_override: startup_device_override(model.gpu_id.as_deref()), capacity_budget_bytes: local_capacity_bytes, + unified_memory, cache_type_k_override: model.cache_type_k.as_deref(), cache_type_v_override: model.cache_type_v.as_deref(), n_batch_override: model.n_batch, diff --git a/mesh/crates/mesh-llm-host-runtime/src/runtime/local_split/test_support.rs b/mesh/crates/mesh-llm-host-runtime/src/runtime/local_split/test_support.rs index 1dc4687c06..50e0b2013e 100644 --- a/mesh/crates/mesh-llm-host-runtime/src/runtime/local_split/test_support.rs +++ b/mesh/crates/mesh-llm-host-runtime/src/runtime/local_split/test_support.rs @@ -1092,6 +1092,7 @@ max_tokens = 222 pinned_gpu: None, device_override: Some("CPU".to_string()), capacity_budget_bytes: node.vram_bytes(), + unified_memory: false, cache_type_k_override: None, cache_type_v_override: None, n_batch_override: None, @@ -1145,6 +1146,7 @@ max_tokens = 222 pinned_gpu: None, device_override: None, capacity_budget_bytes: node.vram_bytes(), + unified_memory: false, cache_type_k_override: None, cache_type_v_override: None, n_batch_override: None, From 49b313813aae53d78e649b263d76203cfcdd65c3 Mon Sep 17 00:00:00 2001 From: James Dumay Date: Sun, 29 Nov 2026 15:36:13 +1100 Subject: [PATCH 2/2] fix(runtime): choose the context budget from the selected GPU --- .../src/runtime/local.rs | 6 ++- .../src/runtime/local_memory_plan.rs | 51 ++++++++++++++++++- .../src/runtime/local_model_only.rs | 1 + .../src/runtime/local_split/test_support.rs | 1 + .../src/runtime/runtime_registry.rs | 1 + .../src/runtime/serving_surface.rs | 1 + .../src/runtime/startup_handles.rs | 1 + .../src/runtime/startup_models.rs | 2 + .../startup_models/pinned_gpu_preflight.rs | 1 + 9 files changed, 62 insertions(+), 3 deletions(-) diff --git a/mesh/crates/mesh-llm-host-runtime/src/runtime/local.rs b/mesh/crates/mesh-llm-host-runtime/src/runtime/local.rs index 96ecd45038..b51af69cd2 100644 --- a/mesh/crates/mesh-llm-host-runtime/src/runtime/local.rs +++ b/mesh/crates/mesh-llm-host-runtime/src/runtime/local.rs @@ -1051,7 +1051,11 @@ pub(super) async fn start_local_openai_model( projector_bytes: planned_projector_bytes(&spec, &model_name), vram_bytes: context_planning_budget_bytes( my_vram, - spec.unified_memory, + super::local_memory_plan::selected_device_uses_unified_memory( + spec.device_override.as_deref(), + spec.pinned_gpu, + spec.unified_memory, + ), skippy::effective_safety_margin_bytes(spec.mesh_config.defaults.as_ref()), required_bytes, ), diff --git a/mesh/crates/mesh-llm-host-runtime/src/runtime/local_memory_plan.rs b/mesh/crates/mesh-llm-host-runtime/src/runtime/local_memory_plan.rs index e5ca1f692e..0b265a8322 100644 --- a/mesh/crates/mesh-llm-host-runtime/src/runtime/local_memory_plan.rs +++ b/mesh/crates/mesh-llm-host-runtime/src/runtime/local_memory_plan.rs @@ -96,8 +96,19 @@ fn completed_measurement_snapshot( }) } -/// Return a completed native buffer measurement only when its model identity, -/// capacity pool, and allocation-affecting configuration all match. +/// The explicit CPU selection and a pinned GPU both take precedence over the +/// host-wide survey when deciding which memory pool is being budgeted. +pub(super) fn selected_device_uses_unified_memory( + device_override: Option<&str>, + pinned_gpu: Option<&super::StartupPinnedGpuTarget>, + host_unified_memory: bool, +) -> bool { + if device_override.is_some_and(|device| device.eq_ignore_ascii_case("CPU")) { + return false; + } + pinned_gpu.map_or(host_unified_memory, |gpu| gpu.unified_memory) +} + /// The budget the context planner may fill. On unified memory outside Metal /// the budget is system RAM the OS and other programs share, so the /// configured safety margin is kept out of the KV cache and compute buffers @@ -120,6 +131,8 @@ pub(super) fn context_planning_budget_bytes( .max(required_bytes.min(capacity_bytes)) } +/// Return a completed native buffer measurement only when its model identity, +/// capacity pool, and allocation-affecting configuration all match. pub(super) fn measured_buffers_footprint( key: &MemoryPlanMeasurementKey, ) -> Option { @@ -213,6 +226,40 @@ pub(super) fn emit_measured_memory_reconciliation( #[cfg(test)] mod tests { + #[test] + fn memory_pool_follows_the_selected_device_on_a_mixed_host() { + let mut pinned = super::super::StartupPinnedGpuTarget { + unified_memory: true, + index: 1, + stable_id: "integrated".into(), + backend_device: "Vulkan1".into(), + vram_bytes: 8 * 1024 * 1024 * 1024, + reserved_bytes: None, + }; + assert!(super::selected_device_uses_unified_memory( + None, + Some(&pinned), + false + )); + assert!(!super::selected_device_uses_unified_memory( + Some("CPU"), + Some(&pinned), + true + )); + assert!(!super::selected_device_uses_unified_memory( + Some("CPU"), + None, + true + )); + pinned.unified_memory = false; + assert!(!super::selected_device_uses_unified_memory( + None, + Some(&pinned), + true + )); + assert!(super::selected_device_uses_unified_memory(None, None, true)); + } + #[cfg(not(target_os = "macos"))] #[test] fn unified_memory_plans_context_inside_the_safety_margin() { diff --git a/mesh/crates/mesh-llm-host-runtime/src/runtime/local_model_only.rs b/mesh/crates/mesh-llm-host-runtime/src/runtime/local_model_only.rs index b164bbbf8b..8bc91cccb5 100644 --- a/mesh/crates/mesh-llm-host-runtime/src/runtime/local_model_only.rs +++ b/mesh/crates/mesh-llm-host-runtime/src/runtime/local_model_only.rs @@ -728,6 +728,7 @@ mod local_capacity_tests { assert_eq!(local_capacity_bytes(None, None, &hw, true), 31_427_447_193); let pinned = StartupPinnedGpuTarget { + unified_memory: false, index: 0, stable_id: "pci:0000:01:00.0".to_string(), backend_device: "CUDA0".to_string(), diff --git a/mesh/crates/mesh-llm-host-runtime/src/runtime/local_split/test_support.rs b/mesh/crates/mesh-llm-host-runtime/src/runtime/local_split/test_support.rs index 50e0b2013e..9b43d1b528 100644 --- a/mesh/crates/mesh-llm-host-runtime/src/runtime/local_split/test_support.rs +++ b/mesh/crates/mesh-llm-host-runtime/src/runtime/local_split/test_support.rs @@ -113,6 +113,7 @@ fn split_generation_cli_device_override_survives_pinned_stage_selection() { ..Default::default() }; let pinned_gpu = crate::runtime::StartupPinnedGpuTarget { + unified_memory: false, index: 0, stable_id: "pci:0000:65:00.0".to_string(), backend_device: "CUDA0".to_string(), diff --git a/mesh/crates/mesh-llm-host-runtime/src/runtime/runtime_registry.rs b/mesh/crates/mesh-llm-host-runtime/src/runtime/runtime_registry.rs index bb1aab4fad..ea9d220f26 100644 --- a/mesh/crates/mesh-llm-host-runtime/src/runtime/runtime_registry.rs +++ b/mesh/crates/mesh-llm-host-runtime/src/runtime/runtime_registry.rs @@ -454,6 +454,7 @@ mod tests { let laya = metadata("laya"); let llama_named_laya = metadata("llama"); let gpu = StartupPinnedGpuTarget { + unified_memory: false, index: 0, stable_id: "gpu-a".to_string(), backend_device: "CUDA0".to_string(), diff --git a/mesh/crates/mesh-llm-host-runtime/src/runtime/serving_surface.rs b/mesh/crates/mesh-llm-host-runtime/src/runtime/serving_surface.rs index 831af659ca..a2ff875e25 100644 --- a/mesh/crates/mesh-llm-host-runtime/src/runtime/serving_surface.rs +++ b/mesh/crates/mesh-llm-host-runtime/src/runtime/serving_surface.rs @@ -486,6 +486,7 @@ pub(super) fn startup_model_plan_fixture() -> Vec { ctx_size: Some(4096), gpu_id: None, pinned_gpu: Some(StartupPinnedGpuTarget { + unified_memory: false, index: 1, stable_id: "gpu-b".to_string(), backend_device: "CUDA1".to_string(), diff --git a/mesh/crates/mesh-llm-host-runtime/src/runtime/startup_handles.rs b/mesh/crates/mesh-llm-host-runtime/src/runtime/startup_handles.rs index b2434be025..37447410ad 100644 --- a/mesh/crates/mesh-llm-host-runtime/src/runtime/startup_handles.rs +++ b/mesh/crates/mesh-llm-host-runtime/src/runtime/startup_handles.rs @@ -1211,6 +1211,7 @@ mod capacity_fallback_tests { fn pinned_12gb() -> StartupPinnedGpuTarget { StartupPinnedGpuTarget { + unified_memory: false, index: 0, stable_id: "pci:0000:01:00.0".to_string(), backend_device: "CUDA0".to_string(), diff --git a/mesh/crates/mesh-llm-host-runtime/src/runtime/startup_models.rs b/mesh/crates/mesh-llm-host-runtime/src/runtime/startup_models.rs index 91d713edf5..241a3e987a 100644 --- a/mesh/crates/mesh-llm-host-runtime/src/runtime/startup_models.rs +++ b/mesh/crates/mesh-llm-host-runtime/src/runtime/startup_models.rs @@ -59,6 +59,7 @@ pub(super) struct StartupModelSpec { #[derive(Clone, Debug, PartialEq, Eq)] pub(crate) struct StartupPinnedGpuTarget { + pub(crate) unified_memory: bool, pub(crate) index: usize, pub(crate) stable_id: String, pub(crate) backend_device: String, @@ -1484,6 +1485,7 @@ pub(super) fn preflight_pinned_startup_models_with_gpus( }; plan.pinned_gpu = Some(StartupPinnedGpuTarget { + unified_memory: resolved_gpu.unified_memory, index: resolved_gpu.index, stable_id, backend_device, diff --git a/mesh/crates/mesh-llm-host-runtime/src/runtime/tests/startup_models/pinned_gpu_preflight.rs b/mesh/crates/mesh-llm-host-runtime/src/runtime/tests/startup_models/pinned_gpu_preflight.rs index 3ca33c2fdd..e7dacac5a4 100644 --- a/mesh/crates/mesh-llm-host-runtime/src/runtime/tests/startup_models/pinned_gpu_preflight.rs +++ b/mesh/crates/mesh-llm-host-runtime/src/runtime/tests/startup_models/pinned_gpu_preflight.rs @@ -56,6 +56,7 @@ fn pinned_gpu_startup_preflight_uses_config_gpu_id() { assert_eq!( plans[0].pinned_gpu, Some(StartupPinnedGpuTarget { + unified_memory: false, index: 0, stable_id: "pci:0000:65:00.0".into(), backend_device: "CUDA0".into(),