[model-gateway] Optimize special token search using Aho-Corasick (#17387)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This commit is contained in:
Praneth Paruchuri
2026-01-25 13:54:15 -08:00
committed by GitHub
co-authored by gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
parent f5ac1ca10b
commit fc7096f80b
3 changed files with 100 additions and 20 deletions
+42 -20
View File
@@ -1,5 +1,6 @@
use std::{collections::HashSet, sync::Arc};
use aho_corasick::AhoCorasick;
use anyhow::Result;
use super::{
@@ -64,6 +65,8 @@ pub struct StopSequenceDecoder {
/// Sequence for incremental decoding (replaces token_buffer + offsets)
sequence: Sequence,
config: StopSequenceConfig,
aho_corasick: Option<AhoCorasick>,
visible_boundary_idx: usize,
/// Buffer for partial matches (the "jail")
jail_buffer: String,
/// Whether we've stopped
@@ -77,9 +80,31 @@ impl StopSequenceDecoder {
config: StopSequenceConfig,
skip_special_tokens: bool,
) -> Self {
let mut patterns: Vec<String> = config
.stop_sequences
.iter()
.filter(|s| !s.is_empty())
.cloned()
.collect();
let visible_boundary_idx = patterns.len();
patterns.extend(
config
.visible_stop_sequences
.iter()
.filter(|s| !s.is_empty())
.cloned(),
);
let aho_corasick = if patterns.is_empty() {
None
} else {
Some(AhoCorasick::new(patterns).expect("Failed to build Aho-Corasick automaton"))
};
StopSequenceDecoder {
sequence: Sequence::new_with_options(tokenizer, skip_special_tokens),
config,
aho_corasick,
visible_boundary_idx,
jail_buffer: String::new(),
stopped: false,
}
@@ -122,28 +147,25 @@ impl StopSequenceDecoder {
self.jail_buffer.push_str(&new_text);
// Check for hidden stop sequences
for stop_seq in &self.config.stop_sequences {
if let Some(pos) = self.jail_buffer.find(stop_seq) {
// Check for stop sequences
if let Some(ac) = &self.aho_corasick {
if let Some(mat) = ac.find(&self.jail_buffer) {
self.stopped = true;
let output = self.jail_buffer[..pos].to_string();
self.jail_buffer.clear();
return Ok(if output.is_empty() {
SequenceDecoderOutput::Stopped
} else {
SequenceDecoderOutput::StoppedWithText(output)
});
}
}
let is_visible = mat.pattern().as_usize() >= self.visible_boundary_idx;
// Check for visible stop sequences
for stop_seq in &self.config.visible_stop_sequences {
if let Some(pos) = self.jail_buffer.find(stop_seq) {
self.stopped = true;
let end_pos = pos + stop_seq.len();
let output = self.jail_buffer[..end_pos].to_string();
self.jail_buffer.clear();
return Ok(SequenceDecoderOutput::StoppedWithText(output));
if is_visible {
let output = self.jail_buffer[..mat.end()].to_string();
self.jail_buffer.clear();
return Ok(SequenceDecoderOutput::StoppedWithText(output));
} else {
let output = self.jail_buffer[..mat.start()].to_string();
self.jail_buffer.clear();
return Ok(if output.is_empty() {
SequenceDecoderOutput::Stopped
} else {
SequenceDecoderOutput::StoppedWithText(output)
});
}
}
}