outlines_core/index.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
//! Building an `Index` to efficiently map vocabulary tokens to state transitions.
use bincode::{Decode, Encode};
use regex_automata::dfa::dense::DFA;
use regex_automata::dfa::Automaton;
use regex_automata::util::primitives::StateID as AutomataStateId;
use regex_automata::Anchored;
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
use crate::prelude::*;
use crate::vocabulary::Vocabulary;
use crate::{Error, Result};
/// `Index` efficiently maps vocabulary tokens to state transitions.
#[derive(Clone, Debug, PartialEq, Encode, Decode)]
pub struct Index {
/// The ID of the initial state in the automaton, processing begins from this state.
initial_state: StateId,
/// A collection of states considered as terminal states.
final_states: HashSet<StateId>,
/// A mapping of state transitions, defined by tokens ids and their corresponding state changes.
///
/// ### Example
/// ```ignore
/// transitions = {
/// 1: {10: 2, 15: 3},
/// 2: {20: 4, 25: 3},
/// 3: {30: 4},
/// 4: {40: 4},
/// }
/// +--------------------------------------+
/// | State 1 |
/// | Initial State |
/// +--------------------------------------+
/// | |
/// + |
/// Token ID 10 |
/// +-----------------------+ |
/// | State 2 | |
/// +-----------------------+ |
/// | | |
/// | + +
/// | Token ID 25 Token ID 15
/// | +------------------------+
/// | | State 3 |
/// | +------------------------+
/// | |
/// + +
/// Token ID 20 Token ID 30
/// +--------------------------------------+
/// | State 4 |
/// | Final state |
/// +--------------------------------------+
/// ```
transitions: HashMap<StateId, HashMap<TokenId, StateId>>,
/// The token ID reserved for the "end-of-sequence" token.
eos_token_id: TokenId,
}
/// The `Index` structure is designed to efficiently map tokens from a given vocabulary
/// to state transitions within a finite-state automaton.
///
/// ## Usage:
/// The `Index` is typically constructed by combining a vocabulary and regular expressions.
/// Once built, it can be used to efficiently evaluate token sequences or to validate input data.
///
/// ## Example:
/// ```rust
/// use outlines_core::prelude::*;
///
/// # fn run() -> Result<(), outlines_core::Error> {
/// let regex = "0|[1-9][0-9]*";
/// let vocabulary = Vocabulary::from_pretrained("openai-community/gpt2", None)?;
/// let index = Index::new(regex, &vocabulary)?;
///
/// let initial_state = index.initial_state();
/// println!("Initial state is {}", initial_state);
/// println!("Is initial state a final state? {}", index.is_final_state(&initial_state));
///
/// let allowed_tokens = index.allowed_tokens(&initial_state).expect("Some allowed tokens");
/// println!("Allowed tokens at initial state are {:?}", allowed_tokens);
///
/// let token_id = allowed_tokens.first().expect("First token");
/// println!("Next state for the token_id {} is {:?}", token_id, index.next_state(&initial_state, token_id));
///
/// println!("Final states are {:?}", index.final_states());
/// println!("Index has exactly {} transitions", index.transitions().len());
/// # Ok(())
/// # }
///
/// ```
///
/// ## Performance:
/// - **Complexity**:
/// The `Index` can accommodate large vocabularies and complex regular expressions.
/// However, its size may grow significantly with the complexity of the input.
/// - **Construction Cost**:
/// Building the `Index` involves processing the vocabulary and regular expressions,
/// which may require a considerable amount of time and computational resources.
impl Index {
/// Builds an `Index` from regular expression and vocabulary tokens.
pub fn new(regex: &str, vocabulary: &Vocabulary) -> Result<Self> {
let eos_token_id = vocabulary.eos_token_id();
let dfa = DFA::new(regex).map_err(Box::new)?;
let start_state = match dfa.universal_start_state(Anchored::Yes) {
Some(s) => s,
None => return Err(Error::DfaHasNoStartState),
};
let mut transitions: HashMap<StateId, HashMap<TokenId, StateId>> = HashMap::default();
let mut final_states: HashSet<StateId> = HashSet::default();
let mut seen: HashSet<AutomataStateId> = HashSet::from_iter([start_state]);
let mut next_states: Vec<AutomataStateId> = vec![start_state];
while let Some(current_state) = next_states.pop() {
if dfa.is_match_state(dfa.next_eoi_state(current_state)) {
final_states.insert(current_state.as_u32());
}
'token_loop: for (token, ids) in vocabulary.tokens().iter() {
if ids.contains(&eos_token_id) {
continue;
}
let mut next_state = current_state;
for transition_byte in token {
next_state = dfa.next_state(next_state, *transition_byte);
if dfa.is_dead_state(next_state) || dfa.is_quit_state(next_state) {
continue 'token_loop;
}
}
let is_intermediate_state = !dfa.is_match_state(next_state);
let is_full_match_state = dfa.is_match_state(dfa.next_eoi_state(next_state));
if is_intermediate_state || is_full_match_state {
for token_id in ids {
transitions
.entry(current_state.as_u32())
.or_default()
.insert(*token_id, next_state.as_u32());
}
}
if !seen.contains(&next_state) {
seen.insert(next_state);
next_states.push(next_state);
}
}
}
// Populate `transitions` with mappings from `final_states` to `eos_token_id`
for &final_state in &final_states {
transitions
.entry(final_state)
.or_default()
.insert(eos_token_id, final_state);
}
Ok(Self {
initial_state: start_state.as_u32(),
final_states,
transitions,
eos_token_id,
})
}
/// Returns the ID of the initial state in the automaton.
pub fn initial_state(&self) -> StateId {
self.initial_state
}
/// Returns set of final states.
pub fn final_states(&self) -> &HashSet<StateId> {
&self.final_states
}
/// Returns state transitions map of tokens ids and their corresponding transition states.
pub fn transitions(&self) -> &HashMap<StateId, HashMap<TokenId, StateId>> {
&self.transitions
}
/// Checks if state is in final states set or not.
pub fn is_final_state(&self, state: &StateId) -> bool {
self.final_states.contains(state)
}
/// Lists allowed tokens for a give state ID or `None` if it is not found in `Index`.
pub fn allowed_tokens(&self, state: &StateId) -> Option<Vec<TokenId>> {
self.transitions
.get(state)
.map(|res| res.keys().cloned().collect())
}
/// Returns transition state for a given state and token id or `None` otherwise.
pub fn next_state(&self, state: &StateId, token_id: &TokenId) -> Option<StateId> {
if token_id == &self.eos_token_id {
return None;
}
Some(*self.transitions.get(state)?.get(token_id)?)
}
}
impl std::fmt::Display for Index {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Index object with transitions:")?;
for (state_id, token_ids) in self.transitions.iter() {
writeln!(f, "{:?} -> {:#?}", state_id, token_ids)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn index_from_regex() {
let regex = "0|[1-9][0-9]*";
let eos_token_id = 4;
let mut vocabulary = Vocabulary::new(eos_token_id);
for (token, token_id) in [("blah", 0), ("1a", 1), ("2", 2), ("0", 3)] {
vocabulary
.try_insert(token, token_id as u32)
.expect("Insert failed");
}
let index = Index::new(regex, &vocabulary).expect("Index failed");
let initial_state = index.initial_state();
assert_eq!(initial_state, 40);
assert_eq!(index.final_states(), &HashSet::from_iter([24, 48, 56]));
assert!(!index.is_final_state(&initial_state));
let expected = HashMap::from_iter([
(24, HashMap::from_iter([(3, 24), (4, 24), (2, 24)])),
(48, HashMap::from_iter([(4, 48)])),
(40, HashMap::from_iter([(3, 48), (2, 56)])),
(56, HashMap::from_iter([(3, 24), (4, 56), (2, 24)])),
]);
assert_eq!(index.transitions(), &expected);
let allowed_tokens = index
.allowed_tokens(&initial_state)
.expect("No allowed tokens");
let token_id = allowed_tokens.first().expect("No first tokens");
let state = 48;
assert_eq!(index.next_state(&initial_state, token_id), Some(state));
assert!(index.is_final_state(&state));
assert_eq!(index.next_state(&state, &eos_token_id), None);
assert_eq!(index.next_state(&state, token_id), None);
}
#[test]
fn index_from_regex_initital_in_allowed() {
let regex = "`\\n(\\.\\n)?`\\n";
let mut vocabulary = Vocabulary::new(104);
for (token, token_id) in [("\n", 103), (".", 102), ("`", 101)] {
vocabulary
.try_insert(token, token_id as u32)
.expect("Insert failed");
}
let index = Index::new(regex, &vocabulary).expect("Index failed");
let allowed = index
.allowed_tokens(&index.initial_state())
.expect("No allowed tokens");
assert!(allowed.contains(&101));
}
#[test]
fn index_from_regex_multibyte() {
let regex = "😇| [😈-😍][😇-😎]*";
let mut vocabulary = Vocabulary::new(8);
for (token, token_id) in [(" 😍", 5), ("blah", 0), ("😇", 2), ("😈a", 1), ("😍", 3)]
{
vocabulary
.try_insert(token, token_id as u32)
.expect("Insert failed");
}
for (token, token_id) in [
(vec![32, 240, 159, 152], 7),
(vec![32, 240, 159, 152, 141], 6),
(vec![240, 159, 152, 141], 4),
] {
vocabulary
.try_insert(token, token_id as u32)
.expect("Insert failed");
}
let index = Index::new(regex, &vocabulary).expect("Index failed");
assert_eq!(index.final_states(), &HashSet::from_iter([208, 128]));
let expected = HashMap::from_iter([
(
208,
HashMap::from_iter([(3, 208), (8, 208), (4, 208), (2, 208)]),
),
(
80,
HashMap::from_iter([(2, 128), (7, 192), (5, 208), (6, 208)]),
),
(128, HashMap::from_iter([(8, 128)])),
]);
assert_eq!(index.transitions(), &expected);
}
}