Complete long-context chat support

This commit is contained in:
Georg Bauer
2026-07-24 18:16:34 +02:00
parent 5ab110ed0b
commit 36946e6e5f
8 changed files with 515 additions and 63 deletions

View File

@@ -324,8 +324,10 @@ impl Generator {
settings: &TurnSettings,
cancelled: &AtomicBool,
mut emit: impl FnMut(bool, String),
mut progress: impl FnMut(u32, u32),
) -> Result<(), String> {
self.executor.reset()?;
progress(self.executor.position(), self.executor.context());
let tokens = self.executor.model().render_conversation(
&settings.system_prompt,
messages,
@@ -337,17 +339,21 @@ impl Generator {
let max_context = self.executor.context() as usize;
if tokens.len() >= max_context {
return Err(format!(
"the conversation uses {} tokens; the current Rust attention port supports fewer than {max_context}",
"the conversation uses {} tokens; the configured context holds fewer than {max_context}",
tokens.len()
));
}
let mut rng = Rng::new(settings.seed.unwrap_or(0x4453_3453_4552_5645));
let mut reasoning = settings.reasoning_mode != ReasoningMode::Direct;
for token in tokens {
let prompt_tokens = tokens.len();
for (index, token) in tokens.into_iter().enumerate() {
if cancelled.load(Ordering::Relaxed) {
return Ok(());
}
self.executor.eval(token)?;
if (index + 1).is_multiple_of(16) || index + 1 == prompt_tokens {
progress(self.executor.position(), self.executor.context());
}
}
for _ in 0..settings
.max_generated_tokens
@@ -380,6 +386,7 @@ impl Generator {
emit(reasoning, String::from_utf8_lossy(&bytes).into_owned());
}
self.executor.eval(token)?;
progress(self.executor.position(), self.executor.context());
}
Ok(())
}
@@ -497,7 +504,8 @@ mod sampling_tests {
ReasoningMode::Direct,
);
assert_eq!(tokens.len(), 10);
let mut executor = metal::Executor::open(model, 128, false).unwrap();
let mut executor = metal::Executor::open(model, 32_768, false).unwrap();
assert_eq!(executor.context(), 32_768);
for token in tokens {
executor.eval(token).unwrap();
}