Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions mesh/crates/mesh-llm-host-runtime/src/runtime/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -313,6 +313,9 @@ pub(super) struct LocalOpenAiModelStartSpec<'a> {
pub(super) pinned_gpu: Option<&'a crate::runtime::StartupPinnedGpuTarget>,
pub(super) device_override: Option<String>,
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<u32>,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MeasuredBufferFootprint> {
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading