From f01a78593f14646fb9df667a39d7b27c3b142933 Mon Sep 17 00:00:00 2001 From: Jason Nguyen Date: Thu, 19 Nov 2026 11:19:20 -0600 Subject: [PATCH] fix(local): read an MLX model's context window from its config.json MLX servers report no limits and the profile default is 4096 tokens, which a host system prompt alone can exceed, so hosts trim the user's turn out of the request. The model id of an MLX server is the directory the weights were loaded from; read max_position_embeddings (top level or text_config) from its config.json. An explicit window still wins, and anything unreadable falls back to the profile default. --- crates/tinyinference-local/src/profile.rs | 33 +++++++++++++ .../tinyinference-local/src/profile_tests.rs | 49 +++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/crates/tinyinference-local/src/profile.rs b/crates/tinyinference-local/src/profile.rs index 2c851527..bb022116 100644 --- a/crates/tinyinference-local/src/profile.rs +++ b/crates/tinyinference-local/src/profile.rs @@ -28,6 +28,17 @@ pub fn context_window_with_local_fallback( } let kind = local_kind?; let profile = profile_for_kind(kind); + if matches!(kind, LocalProviderKind::Mlx | LocalProviderKind::Omlx) + && let Some(window) = model_dir_context_window(model) + { + tracing::debug!( + model, + provider = kind.as_str(), + context_window = window, + "using context window from the model directory's config.json" + ); + return Some(window); + } if let Some(window) = profile.default_context_window { tracing::debug!( model, @@ -47,6 +58,28 @@ pub fn context_window_with_local_fallback( Some(CONSERVATIVE_LOCAL_CONTEXT_FLOOR) } +/// Context window a local model directory declares in its `config.json`. +/// +/// MLX servers (`mlx_lm`, `mlx_vlm`) report no limits, and their model id is +/// the directory the weights were loaded from. When `model` is such a +/// directory, reads `max_position_embeddings` from the top level or from a +/// nested `text_config` (multimodal checkpoints). Returns `None` for anything +/// that is not a readable directory with a positive value. +/// +/// shortcut: the model's trained maximum, not what the host's memory can hold; +/// a host that knows better sets an explicit override. +pub fn model_dir_context_window(model: &str) -> Option { + let path = std::path::Path::new(model.trim()).join("config.json"); + if !path.is_absolute() { + return None; + } + let config: serde_json::Value = serde_json::from_slice(&std::fs::read(path).ok()?).ok()?; + [&config, &config["text_config"]] + .iter() + .find_map(|section| section["max_position_embeddings"].as_u64()) + .filter(|window| *window > 0) +} + /// Identifies a local provider type. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] diff --git a/crates/tinyinference-local/src/profile_tests.rs b/crates/tinyinference-local/src/profile_tests.rs index 2074a8d5..f35254a0 100644 --- a/crates/tinyinference-local/src/profile_tests.rs +++ b/crates/tinyinference-local/src/profile_tests.rs @@ -1,5 +1,54 @@ use super::*; +fn model_dir(config: &str) -> tempfile::TempDir { + let dir = tempfile::tempdir().unwrap(); + std::fs::write(dir.path().join("config.json"), config).unwrap(); + dir +} + +#[test] +fn mlx_window_comes_from_the_model_directory_config() { + let flat = model_dir(r#"{"max_position_embeddings": 32768}"#); + let nested = model_dir(r#"{"text_config": {"max_position_embeddings": 262144}}"#); + let flat_id = flat.path().to_str().unwrap(); + let nested_id = nested.path().to_str().unwrap(); + assert_eq!( + context_window_with_local_fallback(flat_id, None, Some(LocalProviderKind::Mlx)), + Some(32_768) + ); + assert_eq!( + context_window_with_local_fallback(nested_id, None, Some(LocalProviderKind::Omlx)), + Some(262_144) + ); + // An explicit window still wins, and other runtimes ignore the directory. + assert_eq!( + context_window_with_local_fallback(flat_id, Some(8_000), Some(LocalProviderKind::Mlx)), + Some(8_000) + ); + assert_eq!( + context_window_with_local_fallback(flat_id, None, Some(LocalProviderKind::Ollama)), + Some(8_192) + ); +} + +#[test] +fn mlx_window_falls_back_when_the_model_directory_is_unusable() { + let missing_value = model_dir(r#"{"hidden_size": 4096}"#); + let broken = model_dir("not json"); + for dir in [&missing_value, &broken] { + assert_eq!( + context_window_with_local_fallback( + dir.path().to_str().unwrap(), + None, + Some(LocalProviderKind::Mlx) + ), + Some(4_096) + ); + } + assert_eq!(model_dir_context_window("relative/path"), None); + assert_eq!(model_dir_context_window("/no/such/dir"), None); +} + #[test] fn local_context_fallback_uses_profiles_and_conservative_floor() { assert_eq!(