GrammarMatcher

Struct GrammarMatcher 

Source
pub struct GrammarMatcher { /* private fields */ }
Expand description

A stateful matcher that matches sampled tokens against a compiled grammar — the core of grammar-guided generation.

One GrammarMatcher tracks one request’s decoding state. Each decode step Metrale Engine calls Self::fill_next_token_bitmask to learn which tokens are legal, samples one, then Self::accept_token. Self::rollback undoes accepted tokens (e.g. for speculative decoding); Self::reset returns to the initial state.

Implementations§

Source§

impl GrammarMatcher

Source

pub fn forced_token(&mut self) -> Option<i32>

The single grammar-forced next token, if the current state admits exactly one legal token.

Returns Some(token_id) when the constrained grammar leaves the caller no choice — the token is fully determined, so the model sampling step for this position can be skipped and token_id emitted directly (then fed back through Self::accept_token). Returns None when the continuation is a genuine choice (two or more legal tokens), the state is dead (zero legal tokens), or the matcher has already terminated.

This does NOT mutate the matcher: it computes the next-token bitmask exactly as Self::fill_next_token_bitmask would and inspects its cardinality. The returned token is the unique set bit of that authoritative mask, so accepting it is equivalent to the normal sample-then-accept path.

The packed-bitmask buffer is reused from the matcher’s scratch (FillScratch::coalesce_bitmask), so a repeated forced_token call — e.g. inside the Self::next_forced_tokens chain walk — costs no per-call heap allocation.

Source

pub fn forced_from_bitmask( &self, bitmask: &mut [i32], index: usize, ) -> Option<i32>

Inspect an already-filled next-token bitmask for the forced-token condition — the zero-extra-work entry point.

A caller that has just run Self::fill_next_token_bitmask for the current decode step already holds the authoritative mask; it can pass that buffer here to learn whether the step is forced without a second partition computation. bitmask / index must be the same buffer and slice index handed to the fill call.

Returns Some(token_id) iff the mask has exactly one bit set (the forced token), None otherwise. Behaves identically to Self::forced_token but reuses the caller’s mask, so the per-token server loop pays the coalescence check for free:

ⓘ
let nontrivial = matcher.fill_next_token_bitmask(&mut buf, 0, false);
if let Some(tok) = matcher.forced_from_bitmask(&buf, 0) {
    // skip the model sample — emit `tok` directly
}
Source

pub fn next_forced_tokens(&mut self, max: usize) -> Vec<i32>

The maximal chain of grammar-forced tokens starting at the current state.

Coalescence’s forced-chain: after emitting a forced token the next state may itself be forced. This walks that chain and returns every forced token in order, so the caller can skip the model sample for the whole run at once. The returned vector is empty when the current state is not forced.

The matcher state is left completely UNCHANGED: each forced token is virtually accepted to peek at the next state, and every such virtual accept is rolled back before returning. The caller is responsible for feeding the returned tokens back through Self::accept_token for real.

The walk stops at the first non-forced state, at a dead state, or once a forced token terminates the matcher (a forced stop token is included as the final element — the matcher would terminate there, so there is nothing further to force).

A max cap bounds the chain length; pass usize::MAX for an unbounded walk. A cap of 0 returns an empty vector.

Source

pub fn accept_forced_chain(&mut self, max: usize) -> Vec<i32>

Detect the forced chain and ACCEPT it in place — the efficient server primitive for Coalescence.

Unlike Self::next_forced_tokens, which peeks and rolls back (leaving the caller to re-accept every token), this advances the matcher as it walks: each forced token is detected and kept. The matcher’s parser is therefore traversed once, not twice — no rollback, no caller re-accept. The returned vector lists the tokens accepted, in order, so the caller can append them to its output and skip the model sample for each.

On return the matcher state has advanced past the whole forced run — exactly as if the caller had sampled and accepted those tokens one by one. The walk stops at the first non-forced state (the next position is a genuine choice and must be sampled), at a dead state, or once a forced token terminates the matcher.

max caps the run length (usize::MAX for unbounded); 0 accepts nothing and returns an empty vector.

Source§

impl GrammarMatcher

Source

pub fn find_completion_to_accept(&mut self, max_bytes: usize) -> Option<Vec<u8>>

A grammar-legal byte sequence that advances the grammar from its CURRENT state to one where the root rule can complete (a stop token is legal), or None if none is found within max_bytes and the node budget. The matcher state is left UNCHANGED.

Returns Some(empty) when the grammar can already stop here.

Soundness: every byte of a returned path is applied via parser.advance, and a path is only returned once parser.is_completed() holds — so the result is always a grammar-legal completion. Closure-first ordering means it is not guaranteed to be the globally shortest close, but it is a valid one and is found without exploring content breadth.

Source

pub fn find_completion_token_ids( &mut self, max_bytes: usize, ) -> Option<Vec<i32>>

Like Self::find_completion_to_accept, but returns the close as content token ids, greedily encoded against this matcher’s vocab (sorted_decoded_vocab, which excludes stop/special tokens — so a close never emits a control token). Returns Some(empty) when the grammar can already stop, and None when no bounded close exists or a close byte is not representable as a content token.

Source§

impl GrammarMatcher

Source

pub fn fill_next_token_bitmask( &mut self, bitmask: &mut [i32], index: usize, debug: bool, ) -> Result<bool, FillError>

Fill bitmask with the set of tokens acceptable for the next decode position.

index selects the matcher’s slice when the caller batches several matchers into one tensor; for a single matcher pass 0. debug is accepted for API parity (no effect here).

Returns Ok(true) when the mask is not all-accepting (so it must be applied to the logits), Ok(false) when every token is legal. Returns Err if the matcher has terminated or the buffer is too small. Port of FillNextTokenBitmask.

Source

pub fn find_jump_forward_string(&mut self) -> Vec<u8> ⓘ

Find the jump-forward string: the longest prefix that is forced by the grammar regardless of which token is sampled.

The matcher state is left unchanged. Port of FindJumpForwardString: at each step every latest scanable state must agree on exactly one acceptable byte; the union of acceptable bytes having size 1 is the equivalent condition.

Source

pub fn find_jump_forward_string_lossy(&mut self) -> String

find_jump_forward_string as a UTF-8 String, lossily decoding non-UTF-8 bytes. Convenience for callers that want text.

Source§

impl GrammarMatcher

Source

pub fn new( compiled_grammar: CompiledGrammar, override_stop_tokens: Option<Vec<i32>>, terminate_without_stop_token: bool, max_rollback_tokens: i32, ) -> Self

Construct a matcher from a CompiledGrammar.

  • override_stop_tokens — when Some, replaces the tokenizer’s detected stop tokens; must be non-empty (panics otherwise, matching the C++ XGRAMMAR_CHECK).
  • terminate_without_stop_token — terminate on root-rule completion without needing a stop token.
  • max_rollback_tokens — accepted for API faithfulness; the C++ deprecated and ignores it (rollback is unbounded).

Port of the GrammarMatcher constructor.

Source

pub fn from_compiled_grammar(compiled_grammar: CompiledGrammar) -> Self

Convenience constructor with default options (no stop-token override, requires a stop token, unbounded rollback).

Source

pub fn compiled_grammar(&self) -> &CompiledGrammar

The compiled grammar this matcher runs.

Source

pub fn tokenizer_info(&self) -> &TokenizerInfo

The tokenizer info the grammar was compiled against.

Source

pub fn stop_token_ids(&self) -> &[i32]

The stop token ids that terminate this matcher.

Source

pub fn max_rollback_tokens(&self) -> i32

The maximum number of rollback tokens — always -1 (unbounded), matching the deprecated C++ behavior.

Source

pub fn is_terminated(&self) -> bool

True if the matcher has terminated. When terminate_without_stop_token is set this is “root rule completed”; otherwise it is “a stop token was accepted”.

Port of IsTerminated.

Source

pub fn is_grammar_completed(&self) -> bool

True if the root rule has matched at the current position — a stop token may be emitted now. Exposed for callers that need the raw completion state independently of termination policy.

Source

pub fn accept_token(&mut self, token_id: i32, debug: bool) -> bool

Accept one sampled token and advance the matcher.

Returns false (without mutating state) if the token is not grammar-legal, is out of range, is a special token, or the matcher has already terminated. A stop token terminates the matcher. Port of AcceptToken.

Source

pub fn accept_string(&mut self, s: &str, debug: bool) -> bool

Accept a literal string, advancing the matcher byte by byte. The whole string counts as one rollback step. Used to seed a matcher or in tests. Port of AcceptString.

Source

pub fn accept_bytes(&mut self, bytes: &[u8], debug: bool) -> bool

Accept a literal byte string — the byte-level form of Self::accept_string (token bytes are not always UTF-8).

Source

pub fn rollback(&mut self, n: i32)

Roll back the matcher by n accepted tokens / strings.

Each prior accept_token/accept_string is one unit; a stop token is a zero-byte unit whose rollback un-terminates the matcher. Panics if n exceeds the recorded history. Port of Rollback.

Source

pub fn reset(&mut self)

Reset the matcher to its initial state.

Source

pub fn num_history_steps(&self) -> usize

Number of accepted tokens / strings currently in the rollback history.

Trait Implementations§

Source§

impl Debug for GrammarMatcher

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Conv for T

§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
§

impl<T> FmtForward for T

§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pipe for T
where T: ?Sized,

§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Tap for T

§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
§

impl<T> TryConv for T

§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,