Skip to content
Merged
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
5 changes: 5 additions & 0 deletions crates/tokenizer/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ impl TokenizerTrait for MockTokenizer {
self.reverse_vocab.get(&id).cloned()
}

fn eos_token_ids(&self) -> &[u32] {
// `<eos>` in the mock vocab.
&[999]
}

fn as_any(&self) -> &dyn std::any::Any {
self
}
Expand Down
46 changes: 46 additions & 0 deletions crates/tokenizer/src/stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ pub struct StopSequenceDecoder {
jail_max_bytes: usize,
/// Whether we've stopped
stopped: bool,
/// The string stop sequence that triggered the stop, if any. Set only for
/// string-sequence matches; token-level stops leave this `None`.
matched_stop: Option<String>,
/// True when there are no string stop sequences (only token-level stops).
/// In this mode the jail buffer is bypassed entirely for lower overhead.
token_only: bool,
Expand Down Expand Up @@ -139,6 +142,7 @@ impl StopSequenceDecoder {
jail_buffer: String::new(),
jail_max_bytes,
stopped: false,
matched_stop: None,
token_only,
}
}
Expand Down Expand Up @@ -208,6 +212,7 @@ impl StopSequenceDecoder {
let input = Input::new(&self.jail_buffer).span(search_start..self.jail_buffer.len());
if let Some(mat) = ac.find(input) {
self.stopped = true;
self.matched_stop = Some(self.jail_buffer[mat.start()..mat.end()].to_string());
let is_visible = mat.pattern().as_usize() >= self.visible_boundary_idx;

if is_visible {
Expand Down Expand Up @@ -287,11 +292,18 @@ impl StopSequenceDecoder {
self.stopped
}

/// The string stop sequence that triggered the stop, if a string sequence
/// matched. `None` for token-level stops or when no stop has fired.
pub fn matched_stop(&self) -> Option<&str> {
self.matched_stop.as_deref()
}

/// Reset the decoder state
pub fn reset(&mut self) {
self.jail_buffer.clear();
self.sequence.clear();
self.stopped = false;
self.matched_stop = None;
}
}

Expand Down Expand Up @@ -455,6 +467,40 @@ mod tests {
));
}

#[test]
fn test_matched_stop_reports_matched_string() {
let tokenizer = Arc::new(MockTokenizer::new());
let config = StopSequenceConfig::default().with_stop_sequence("test");
let mut decoder = StopSequenceDecoder::new(tokenizer, config, false);

// No match yet.
assert_eq!(decoder.matched_stop(), None);

decoder.process_token(1).unwrap(); // "Hello"
decoder.process_token(2).unwrap(); // "world"
assert_eq!(decoder.matched_stop(), None);

// "test" triggers the string stop; the matched string is captured.
decoder.process_token(3).unwrap(); // "test"
assert_eq!(decoder.matched_stop(), Some("test"));

// Reset clears it.
decoder.reset();
assert_eq!(decoder.matched_stop(), None);
}

#[test]
fn test_matched_stop_none_for_token_stop() {
// A token-id stop is not a string match, so matched_stop stays None.
let tokenizer = Arc::new(MockTokenizer::new());
let config = StopSequenceConfig::default().with_stop_token(999);
let mut decoder = StopSequenceDecoder::new(tokenizer, config, false);

let result = decoder.process_token(999).unwrap();
assert_eq!(result, SequenceDecoderOutput::Stopped);
assert_eq!(decoder.matched_stop(), None);
}

#[test]
fn test_flush_after_partial() {
let tokenizer = Arc::new(MockTokenizer::new());
Expand Down
10 changes: 4 additions & 6 deletions model_gateway/src/routers/grpc/backend_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ impl BackendClient {
}
}

/// True if this backend speaks the vLLM protocol (gRPC-vLLM or ZMQ).
pub fn is_vllm(&self) -> bool {
match self {
Self::Grpc(client) => client.is_vllm(),
Self::Zmq(_) => true,
}
/// True if this is a direct-ZMQ backend (the engine receives token ids only
/// and cannot match string stops itself).
pub fn is_zmq(&self) -> bool {
matches!(self, Self::Zmq(_))
}

/// Local liveness. gRPC has no cheap local flag (it uses a health RPC), so
Expand Down
Loading
Loading