Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 33 additions & 0 deletions crates/tinyinference-local/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<u64> {
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")]
Expand Down
49 changes: 49 additions & 0 deletions crates/tinyinference-local/src/profile_tests.rs
Original file line number Diff line number Diff line change
@@ -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!(
Expand Down