pub struct BatchGrammarMatcher { /* private fields */ }Expand description
A batch version of GrammarMatcher that can fill the next token bitmask for multiple matchers in parallel. It utilizes multiple threads to speed up the computation. It is especially useful when the batch size is large.
Implementations§
Source§impl BatchGrammarMatcher
impl BatchGrammarMatcher
Sourcepub fn new(max_threads: i32) -> Self
pub fn new(max_threads: i32) -> Self
Construct the batch grammar matcher.
§Parameters
max_threads: The maximum number of threads to use for parallel processing. Use -1 for automatic thread count (hardware_concurrency / 2).
Sourcepub fn batch_fill_next_token_bitmask(
&mut self,
matchers: &[GrammarMatcher],
bitmask: &mut DLTensor,
indices: Option<&[i32]>,
debug_print: bool,
)
pub fn batch_fill_next_token_bitmask( &mut self, matchers: &[GrammarMatcher], bitmask: &mut DLTensor, indices: Option<&[i32]>, debug_print: bool, )
Fill the next token bitmask for multiple matchers.
§Parameters
matchers: The list of matchers to fill the bitmask for.bitmask: Must be a 2-dimensional int32 tensor with shape (bitmask_batch_size, bitmask_size). Bitmask_batch_size could be larger than the actual batch size to allow padding. Bitmask_size equals to ceil(vocab_size/32).indices: A list of indices to specify which rows in the bitmask to fill. If None, fill the bitmask [0..matchers.len()).debug_print: Whether to print information about generated bitmask (default: false).
Sourcepub fn batch_accept_token(
matchers: &[GrammarMatcher],
tokens: &[i32],
debug_print: bool,
) -> Box<[bool]>
pub fn batch_accept_token( matchers: &[GrammarMatcher], tokens: &[i32], debug_print: bool, ) -> Box<[bool]>
Accept a batch of tokens for multiple matchers.
§Parameters
matchers: The list of matchers to accept tokens for.tokens: The list of tokens to accept.debug_print: Whether to print information about generated bitmask (default: false).
§Returns
A boxed slice of booleans indicating whether each token was accepted by its corresponding matcher.
Sourcepub fn batch_accept_string(
matchers: &[GrammarMatcher],
strings: &[impl AsRef<str>],
debug_print: bool,
) -> Box<[bool]>
pub fn batch_accept_string( matchers: &[GrammarMatcher], strings: &[impl AsRef<str>], debug_print: bool, ) -> Box<[bool]>
Accept a batch of strings for multiple matchers.
§Parameters
matchers: The list of matchers to accept tokens for.strings: The list of strings to accept.debug_print: Whether to print information about generated bitmask (default: false).
§Returns
A boxed slice of booleans indicating whether each string was accepted by its corresponding matcher.
Auto Trait Implementations§
impl Freeze for BatchGrammarMatcher
impl !RefUnwindSafe for BatchGrammarMatcher
impl !Send for BatchGrammarMatcher
impl !Sync for BatchGrammarMatcher
impl Unpin for BatchGrammarMatcher
impl UnwindSafe for BatchGrammarMatcher
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more