Skip to main content

dynamo_tokenizers/
lib.rs

1// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4pub mod basetenkenizer;
5pub mod cache;
6pub mod fastokens;
7pub mod hf;
8pub mod tiktoken;
9
10// TODO: Add tokenizer benchmarks
11// TODO: Enable README.md as a module doc
12// #[doc = include_str!("../README.md")]
13
14use std::hash::{DefaultHasher, Hash, Hasher};
15use std::sync::Arc;
16use std::{fs::File, io::BufReader, ops::Deref, path::Path};
17
18use anyhow::Context as _;
19pub use anyhow::{Error, Result};
20
21pub use basetenkenizer::BasetenTokenizer;
22pub use cache::{
23    CacheTokenUsage, CacheTokenUsageFn, CachedTokenizer, L1CacheStats, SharedTokenizerCache,
24    SharedTokenizerCacheStats,
25};
26pub use fastokens::FastTokenizer;
27pub use hf::HuggingFaceTokenizer;
28pub use tiktoken::TikTokenTokenizer;
29pub use traits::DecodeResult;
30
31pub type TokenIdType = u32;
32
33/// A rendered prompt segment with an explicit trust boundary for special tokens.
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35pub struct EncodeSegment<'a> {
36    pub text: &'a str,
37    /// Recognize added/control tokens in this trusted renderer output.
38    ///
39    /// Set this to `false` for user, tool, and attribute content so text that
40    /// resembles a control token is encoded as ordinary text.
41    pub allow_special: bool,
42}
43
44impl<'a> EncodeSegment<'a> {
45    pub const fn new(text: &'a str, allow_special: bool) -> Self {
46        Self {
47            text,
48            allow_special,
49        }
50    }
51
52    pub const fn ordinary(text: &'a str) -> Self {
53        Self::new(text, false)
54    }
55
56    pub const fn control(text: &'a str) -> Self {
57        Self::new(text, true)
58    }
59}
60
61/// Represents the type of tokenizer being used
62#[derive(Debug)]
63pub enum TokenizerType {
64    HuggingFace(String),
65    TikToken(String),
66}
67
68/// character offsets in the original text
69pub type Offsets = (usize, usize);
70
71/// Contains the results of tokenizing text: token IDs, string tokens, and their spans
72#[derive(Debug, Clone)]
73pub enum Encoding {
74    /// Hugging Face
75    Hf(Box<tokenizers::tokenizer::Encoding>),
76    /// Sentence Piece
77    Sp(Vec<TokenIdType>),
78}
79
80impl Encoding {
81    pub fn token_ids(&self) -> &[u32] {
82        match self {
83            Encoding::Hf(inner) => inner.get_ids(),
84            Encoding::Sp(inner) => inner,
85        }
86    }
87}
88
89impl Hash for Encoding {
90    fn hash<H: Hasher>(&self, state: &mut H) {
91        self.token_ids().hash(state);
92    }
93}
94
95pub mod traits {
96    use super::*;
97
98    pub trait Encoder: Send + Sync {
99        fn encode(&self, input: &str) -> Result<Encoding>;
100        fn encode_batch(&self, inputs: &[&str]) -> Result<Vec<Encoding>>;
101
102        /// Encode Kimi K3-style renderer segments while preserving trusted
103        /// control-token and untrusted content boundaries.
104        ///
105        /// Each segment controls whether added/control tokens are recognized
106        /// through [`EncodeSegment::allow_special`]. This prevents text in user,
107        /// tool, or attribute content from becoming structural when it happens
108        /// to resemble a control token.
109        ///
110        /// The Baseten backend preserves legacy tiktoken behavior by splitting
111        /// each segment into chunks of at most 400,000 characters and splitting
112        /// whitespace/non-whitespace runs at 25,000 characters. Independent
113        /// chunks are encoded through the Rayon thread pool, then concatenated
114        /// in input order. Tokenizer post-processing is applied once after all
115        /// segment IDs have been joined.
116        ///
117        /// Backends must not implement this by flattening the segments first,
118        /// because that discards the special-token trust boundary.
119        fn encode_segments(&self, _segments: &[EncodeSegment<'_>]) -> Result<Encoding> {
120            Err(Error::msg(
121                "tokenizer backend does not support segmented encoding",
122            ))
123        }
124    }
125
126    /// Result of decoding token IDs to text.
127    ///
128    /// Distinguishes between fully valid UTF-8 output and output that contains
129    /// trailing incomplete multi-byte sequences (represented as U+FFFD).
130    /// This lets callers like `DecodeStream::step()` decide whether to emit or
131    /// buffer without resorting to hardcoded replacement-character string checks.
132    #[derive(Debug, Clone, PartialEq, Eq, strum::EnumIs)]
133    pub enum DecodeResult {
134        /// No trailing incomplete multi-byte sequences (text does not end with U+FFFD).
135        /// Note: the string may still contain *interior* U+FFFD characters from
136        /// mid-stream invalid byte sequences; only trailing status is tracked here.
137        Complete(String),
138        /// The decoded string ends with U+FFFD, indicating incomplete trailing
139        /// multi-byte bytes that may be completed by subsequent tokens.
140        Partial(String),
141    }
142
143    impl DecodeResult {
144        /// Returns a reference to the inner string.
145        pub fn as_str(&self) -> &str {
146            match self {
147                DecodeResult::Complete(s) | DecodeResult::Partial(s) => s,
148            }
149        }
150
151        /// Construct from a decoded string: `Partial` if it ends with U+FFFD, else `Complete`.
152        pub fn from_decoded(text: String) -> Self {
153            if text.ends_with('\u{FFFD}') {
154                DecodeResult::Partial(text)
155            } else {
156                DecodeResult::Complete(text)
157            }
158        }
159    }
160
161    impl From<String> for DecodeResult {
162        fn from(text: String) -> Self {
163            DecodeResult::from_decoded(text)
164        }
165    }
166
167    impl From<DecodeResult> for String {
168        fn from(result: DecodeResult) -> Self {
169            match result {
170                DecodeResult::Complete(s) | DecodeResult::Partial(s) => s,
171            }
172        }
173    }
174
175    /// Implementations must ensure that partial multi-byte sequences produce U+FFFD
176    /// (`\u{FFFD}`) in the output rather than returning `Err`. This is commonly achieved
177    /// via `String::from_utf8_lossy` (tiktoken) or library-internal byte-fallback handling
178    /// (HuggingFace). `DecodeStream::step()` relies on `DecodeResult::Partial` to detect
179    /// incomplete sequences and buffer tokens until the full character arrives.
180    pub trait Decoder: Send + Sync {
181        fn decode(
182            &self,
183            token_ids: &[TokenIdType],
184            skip_special_tokens: bool,
185        ) -> Result<DecodeResult>;
186    }
187
188    pub trait Tokenizer: Encoder + Decoder {
189        /// Validate that this tokenizer can be safely wrapped in the prefix cache.
190        ///
191        /// Implementations must explicitly opt in by returning `Ok(())`, or
192        /// return an error explaining why the tokenizer is incompatible.
193        fn validate_prefix_cache(&self) -> Result<()> {
194            Err(Error::msg("tokenizer does not support prefix caching"))
195        }
196
197        /// Apply construction-time [`TokenizerOptions`].
198        ///
199        /// The default implementation ignores the options — correct for
200        /// tokenizers with no applicable option.
201        fn with_options(self, options: TokenizerOptions) -> Self
202        where
203            Self: Sized,
204        {
205            let _ = options;
206            self
207        }
208        /// Vocabulary cardinality including added tokens, when the backend
209        /// can expose one. `None` for backends without a bounded id space or
210        /// vocabulary introspection.
211        fn vocab_size(&self) -> Option<usize> {
212            None
213        }
214
215        /// Resolve a token string to its vocabulary id, when the backend
216        /// supports lookup. `Ok(None)` when the token is not in the
217        /// vocabulary. `Err` when this backend cannot do id lookup at all —
218        /// kept distinct from a genuine vocabulary miss so callers can tell
219        /// "unsupported" from "looked up, not found".
220        ///
221        /// Defaults to unsupported, matching `encode_segments` and
222        /// `validate_prefix_cache`: only backends that can perform lookup
223        /// need to override it.
224        fn token_to_id(&self, _token: &str) -> Result<Option<TokenIdType>> {
225            Err(Error::msg(
226                "tokenizer backend does not support token lookup",
227            ))
228        }
229
230        /// Ids of added tokens marked special (e.g. BOS/EOS/PAD/control
231        /// tokens), as distinct from ordinary vocabulary tokens. `Ok(vec![])`
232        /// for backends that genuinely have none, or that do not distinguish
233        /// special from ordinary vocabulary ids. `Err` when the backend
234        /// cannot enumerate added tokens at all — an empty `Vec` alone
235        /// cannot carry that distinction.
236        ///
237        /// Defaults to unsupported, matching `token_to_id`: only backends
238        /// that can enumerate special ids need to override it.
239        fn special_token_ids(&self) -> Result<Vec<TokenIdType>> {
240            Err(Error::msg(
241                "tokenizer backend does not support special token enumeration",
242            ))
243        }
244
245        /// Count of special tokens `encode`'s `add_special_tokens: true`
246        /// path would add to a bare encoding (e.g. BOS/EOS), available
247        /// without performing an encode. `Ok(0)` for backends that
248        /// genuinely add none. `Err` when the backend cannot determine this
249        /// at all — a plain `0` alone cannot carry that distinction, and
250        /// this value feeds token-budget accounting where a silent `0`
251        /// would under-count rather than fail loudly.
252        ///
253        /// Defaults to unsupported, matching `token_to_id`: only backends
254        /// that can determine this count need to override it.
255        fn num_special_tokens_added(&self) -> Result<usize> {
256            Err(Error::msg(
257                "tokenizer backend does not support special token accounting",
258            ))
259        }
260        // fn make_unique_clone(&self) -> Box<dyn Tokenizer>;
261    }
262}
263
264pub fn file_json_field<T: serde::de::DeserializeOwned>(
265    json_file_path: &Path,
266    field_name: &str,
267) -> anyhow::Result<T> {
268    let file = File::open(json_file_path)
269        .with_context(|| format!("Failed to open file: {:?}", json_file_path))?;
270    let reader = BufReader::new(file);
271
272    let json_data: serde_json::Value = serde_json::from_reader(reader)
273        .with_context(|| format!("Failed to parse JSON from file: {:?}", json_file_path))?;
274
275    let map = json_data.as_object().ok_or_else(|| {
276        anyhow::anyhow!("JSON root is not an object in file: {:?}", json_file_path)
277    })?;
278
279    let field_value = map.get(field_name).ok_or_else(|| {
280        anyhow::anyhow!(
281            "Field '{}' not found in JSON file: {:?}",
282            field_name,
283            json_file_path
284        )
285    })?;
286
287    serde_json::from_value(field_value.clone()).with_context(|| {
288        format!(
289            "Failed to deserialize field '{}' (value: {:?}) to the expected type from file: {:?}",
290            field_name, field_value, json_file_path
291        )
292    })
293}
294
295pub fn log_json_err(filename: &str, json: &str, err: &serde_json::Error) {
296    const ERROR_PREFIX: &str = ">>     ";
297
298    if !(err.is_syntax() || err.is_data()) {
299        return;
300    }
301
302    let line = err.line().saturating_sub(1);
303    let column = err.column().saturating_sub(1);
304
305    let json_lines: Vec<&str> = json.lines().collect();
306    if json_lines.is_empty() {
307        tracing::error!("JSON parsing error in {filename}: File is empty.");
308        return;
309    }
310
311    let start_index = line.saturating_sub(2);
312    let end_index = line.saturating_add(3).min(json_lines.len());
313
314    let mut context_lines: Vec<String> = (start_index..end_index)
315        .map(|i| {
316            if i == line {
317                format!("{ERROR_PREFIX}{}", json_lines[i])
318            } else {
319                format!("{:06} {}", i + 1, json_lines[i])
320            }
321        })
322        .collect();
323
324    let col_indicator = "_".to_string().repeat(column + ERROR_PREFIX.len()) + "^";
325    let error_in_context_idx = line - start_index;
326    if error_in_context_idx < context_lines.len() {
327        context_lines.insert(error_in_context_idx + 1, col_indicator);
328    }
329
330    tracing::error!(
331        "JSON parsing error in {filename}: Line {}, column {}:\n{}",
332        err.line(),
333        err.column(),
334        context_lines.join("\n")
335    );
336}
337
338impl Encoding {
339    pub fn get_hash(&self) -> u64 {
340        let mut hasher = DefaultHasher::new();
341        self.hash(&mut hasher);
342        hasher.finish()
343    }
344}
345
346/// Construction options for [`Tokenizer::from_file_with_options`] /
347/// [`create_tokenizer_from_file`], applied to concrete tokenizers via
348/// [`traits::Tokenizer::with_options`].
349#[derive(Debug, Clone, Copy, Default)]
350pub struct TokenizerOptions {
351    /// Ask the tokenizer to add its declared special tokens (e.g. BOS/EOS via
352    /// its post-processor) during `encode`, `encode_batch`, and supported
353    /// `encode_segments` calls.
354    /// Defaults to `false`, the historical behavior.
355    ///
356    /// Applicable to Hugging Face and Baseten tokenizers.
357    pub add_special_tokens: bool,
358}
359
360/// Main tokenizer wrapper that provides a unified interface for different tokenizer implementations
361#[derive(Clone)]
362pub struct Tokenizer(Arc<dyn traits::Tokenizer>);
363
364impl Tokenizer {
365    pub fn from_file(file_path: &str) -> Result<Tokenizer> {
366        Ok(Tokenizer(create_tokenizer_from_file(file_path)?))
367    }
368
369    pub fn from_file_with_options(file_path: &str, options: TokenizerOptions) -> Result<Tokenizer> {
370        Ok(Tokenizer(create_tokenizer_from_file_with_options(
371            file_path, options,
372        )?))
373    }
374
375    /// Create a stateful sequence object for decoding token_ids into text
376    pub fn decode_stream(
377        &self,
378        prompt_token_ids: &[TokenIdType],
379        skip_special_tokens: bool,
380    ) -> DecodeStream {
381        DecodeStream::new(self.0.clone(), prompt_token_ids, skip_special_tokens)
382    }
383}
384
385impl Deref for Tokenizer {
386    type Target = Arc<dyn traits::Tokenizer>;
387
388    fn deref(&self) -> &Self::Target {
389        &self.0
390    }
391}
392
393impl From<Arc<dyn traits::Tokenizer>> for Tokenizer {
394    fn from(tokenizer: Arc<dyn traits::Tokenizer>) -> Self {
395        Tokenizer(tokenizer)
396    }
397}
398
399impl<T> From<Arc<T>> for Tokenizer
400where
401    T: traits::Tokenizer + 'static, // 'static is required to ensure T can be safely put into an Arc
402{
403    fn from(tokenizer: Arc<T>) -> Self {
404        Tokenizer(tokenizer)
405    }
406}
407
408/// Create a tokenizer from a file path to a tokenizer file.
409/// The file extension is used to determine the tokenizer type.
410/// Supported file types are:
411/// - json: HuggingFace tokenizer
412/// - model, tiktoken: tiktoken BPE tokenizer (requires `config.json` with a supported
413///   `model_type` in the same directory; currently: kimi, kimi_k2, kimi_k25, kimi_k3)
414pub fn create_tokenizer_from_file(file_path: &str) -> Result<Arc<dyn traits::Tokenizer>> {
415    create_tokenizer_from_file_with_options(file_path, Default::default())
416}
417
418/// Create a tokenizer from a file path to a tokenizer file with additional tokenizer option.
419/// The file extension is used to determine the tokenizer type.
420/// Supported file types are:
421/// - json: HuggingFace tokenizer
422/// - model, tiktoken: tiktoken BPE tokenizer (requires `config.json` with a supported
423///   `model_type` in the same directory; currently: kimi, kimi_k2, kimi_k25, kimi_k3)
424pub fn create_tokenizer_from_file_with_options(
425    file_path: &str,
426    options: TokenizerOptions,
427) -> Result<Arc<dyn traits::Tokenizer>> {
428    use traits::Tokenizer as _;
429
430    let path = Path::new(file_path);
431    let extension = path
432        .extension()
433        .and_then(std::ffi::OsStr::to_str)
434        .ok_or_else(|| Error::msg("Failed to read file extension".to_string()))?;
435
436    match extension {
437        "json" => {
438            let tokenizer = HuggingFaceTokenizer::from_file(file_path)?.with_options(options);
439            Ok(Arc::new(tokenizer))
440        }
441        "model" | "tiktoken" => {
442            let tokenizer = TikTokenTokenizer::from_file_auto(file_path)?.with_options(options);
443            Ok(Arc::new(tokenizer))
444        }
445        _ => Err(Error::msg(format!(
446            "Unsupported tokenizer file type: .{extension}"
447        ))),
448    }
449}
450
451// With incremental detokenization, we need to consider the final context tokens when handling the initial decode tokens.
452// This is the initial offset from the end of the context that we start decoding from.
453// Both Huggingface TGI and vLLM use this same value.
454// See: https://github.com/huggingface/text-generation-inference/blob/24c2bff65924801ddf90fa24fcc72752d4f45538/server/text_generation_server/models/mamba.py#L169
455// and https://github.com/vllm-project/vllm/blob/da2705198fa19030a25d0bea437f7be6547d47d4/vllm/transformers_utils/detokenizer_utils.py#L51
456const INITIAL_INCREMENTAL_DETOKENIZATION_OFFSET: usize = 5;
457
458/// DecodeStream will keep the state necessary to produce individual chunks of
459/// strings given an input stream of token_ids.
460///
461/// This is necessary because decoding in general cannot achieve that since strings
462/// depend on surrounding ids to provide a valid string. Typically stripping extra spaces.
463pub struct DecodeStream {
464    /// The tokenizer used to decode token_ids
465    tokenizer: Arc<dyn traits::Tokenizer>,
466
467    skip_special_tokens: bool,
468    /// A temporary buffer of the necessary token_ids needed
469    /// to produce valid string chunks.
470    /// This typically contains 3 parts:
471    ///  - read
472    ///  - prefix
473    ///  - rest
474    ///
475    /// Read is the bit necessary to surround the prefix
476    /// so decoding the whole ids produces a valid prefix.
477    /// Prefix is the previously produced string, kept around to trim off of
478    /// the next valid chunk
479    all_token_ids: Vec<u32>,
480
481    prefix_offset: usize,
482
483    read_offset: usize,
484
485    /// Whether any generated text has already been returned to the caller.
486    has_emitted: bool,
487}
488
489impl DecodeStream {
490    pub fn new(
491        tokenizer: Arc<dyn traits::Tokenizer>,
492        prompt_token_ids: &[TokenIdType],
493        skip_special_tokens: bool,
494    ) -> Self {
495        // Earlier prompt tokens are never read by incremental decoding. Keep the
496        // same context suffix and rebase its offsets before copying it.
497        let context_start = prompt_token_ids
498            .len()
499            .saturating_sub(INITIAL_INCREMENTAL_DETOKENIZATION_OFFSET);
500        let prompt_token_ids = prompt_token_ids[context_start..].to_vec();
501        let num_input_tokens = prompt_token_ids.len();
502        Self {
503            tokenizer,
504            skip_special_tokens,
505            all_token_ids: prompt_token_ids,
506            prefix_offset: 0,
507            read_offset: num_input_tokens,
508            has_emitted: false,
509        }
510    }
511
512    /// Step appends a token_id to the internal state and tries to produce a text chunk.
513    ///
514    /// Implementation directly copied from Huggingface's TGI:
515    /// https://github.com/huggingface/text-generation-inference/blob/24c2bff65924801ddf90fa24fcc72752d4f45538/server/text_generation_server/models/model.py#L144
516    ///
517    /// Returning `None` means the given id is not enough to produce a chunk.
518    /// This typically happens with `byte_fallback` options where some tokens do not
519    /// represent valid UTF-8, and only follow-up token_ids will help produce
520    /// a valid chunk.
521    ///
522    /// An error is terminal for this stream because the token may already have
523    /// been appended to its internal state.
524    pub fn step(&mut self, id: u32) -> Result<Option<String>> {
525        self.all_token_ids.push(id);
526
527        let prefix_text: String = self
528            .tokenizer
529            .decode(
530                &self.all_token_ids[self.prefix_offset..self.read_offset],
531                self.skip_special_tokens,
532            )?
533            .into();
534
535        let new_result = self.tokenizer.decode(
536            &self.all_token_ids[self.prefix_offset..],
537            self.skip_special_tokens,
538        )?;
539
540        let new_text = new_result.as_str();
541        let is_partial = new_result.is_partial();
542
543        // Once generated text has been returned, decoding must remain append-only.
544        // A complete rewrite cannot be repaired after the caller has seen the old text.
545        if self.has_emitted && !is_partial && !new_text.starts_with(prefix_text.as_str()) {
546            return Err(Error::msg(
547                "incremental decoding rewrote already emitted text",
548            ));
549        }
550
551        if new_text.len() > prefix_text.len() && !is_partial {
552            let requested_split = prefix_text.len();
553            let split = if self.has_emitted {
554                // starts_with() guarantees this is a character boundary and preserves
555                // everything already returned to the caller.
556                requested_split
557            } else {
558                // Rewinding into prompt context is safe because prompt text was not
559                // returned by this stream.
560                new_text.floor_char_boundary(requested_split)
561            };
562
563            let emitted = new_text[split..].to_string();
564
565            self.prefix_offset = self.read_offset;
566            self.read_offset = self.all_token_ids.len();
567            self.has_emitted = true;
568
569            Ok(Some(emitted))
570        } else {
571            Ok(None)
572        }
573    }
574}
575
576#[cfg(test)]
577mod decode_stream_unicode_tests {
578    use super::{DecodeResult, DecodeStream, Encoding, Result, TokenIdType};
579    use std::sync::Arc;
580
581    struct RewritingTokenizer {
582        prefix_text: &'static str,
583        rewritten_text: &'static str,
584        prefix_is_partial: bool,
585    }
586
587    impl super::traits::Encoder for RewritingTokenizer {
588        fn encode(&self, _input: &str) -> Result<Encoding> {
589            Ok(Encoding::Sp(vec![]))
590        }
591
592        fn encode_batch(&self, _inputs: &[&str]) -> Result<Vec<Encoding>> {
593            Ok(vec![])
594        }
595    }
596
597    impl super::traits::Decoder for RewritingTokenizer {
598        fn decode(
599            &self,
600            token_ids: &[TokenIdType],
601            _skip_special_tokens: bool,
602        ) -> Result<DecodeResult> {
603            let result = match token_ids.len() {
604                1 if self.prefix_is_partial => DecodeResult::Partial(self.prefix_text.to_string()),
605                1 => DecodeResult::Complete(self.prefix_text.to_string()),
606                2 => DecodeResult::Complete(self.rewritten_text.to_string()),
607                _ => DecodeResult::Complete(String::new()),
608            };
609            Ok(result)
610        }
611    }
612
613    impl super::traits::Tokenizer for RewritingTokenizer {}
614
615    #[test]
616    fn prompt_suffix_preserves_decode_inputs_and_output() {
617        let tokenizer: Arc<dyn super::traits::Tokenizer> = Arc::new(
618            super::HuggingFaceTokenizer::from_file(concat!(
619                env!("CARGO_MANIFEST_DIR"),
620                "/tests/data/minimal-bpe/tokenizer.json"
621            ))
622            .unwrap(),
623        );
624        for prompt_len in [0usize, 1, 4, 5, 6, 775_168] {
625            let prompt: Vec<_> = (0..prompt_len)
626                .map(|index| 1 + (index % 22) as u32)
627                .collect();
628            for skip_special_tokens in [false, true] {
629                let mut stream = DecodeStream::new(tokenizer.clone(), &prompt, skip_special_tokens);
630                // Reconstruct the previous full-prompt state as a parity oracle.
631                let mut full = DecodeStream {
632                    tokenizer: tokenizer.clone(),
633                    skip_special_tokens,
634                    all_token_ids: prompt.clone(),
635                    prefix_offset: prompt_len
636                        .saturating_sub(super::INITIAL_INCREMENTAL_DETOKENIZATION_OFFSET),
637                    read_offset: prompt_len,
638                    has_emitted: false,
639                };
640                assert!(stream.all_token_ids.capacity() <= 5);
641                for token in [5, 9, 12, 12, 13, 0, 1, 17, 13, 14, 12, 8, 2] {
642                    assert_eq!(
643                        &stream.all_token_ids[stream.prefix_offset..stream.read_offset],
644                        &full.all_token_ids[full.prefix_offset..full.read_offset],
645                    );
646                    assert_eq!(
647                        &stream.all_token_ids[stream.prefix_offset..],
648                        &full.all_token_ids[full.prefix_offset..],
649                    );
650                    let actual = stream.step(token).map_err(|error| error.to_string());
651                    let expected = full.step(token).map_err(|error| error.to_string());
652                    assert_eq!(actual, expected, "prompt length {prompt_len}");
653                    if actual.is_err() {
654                        break;
655                    }
656                }
657            }
658        }
659    }
660
661    #[test]
662    fn allows_boundary_recovery_before_generated_text_is_emitted() {
663        for (prefix_text, rewritten_text, expected) in [
664            ("㺄馉凓鄗\u{FFFD}", "㺄馉凓鄗𫷲", "𫷲"),
665            (
666                "JUnitworkflow Completion intuition\u{FFFD}",
667                "JUnitworkflow Completion intuition𝟙",
668                "𝟙",
669            ),
670        ] {
671            let tokenizer: Arc<dyn super::traits::Tokenizer> = Arc::new(RewritingTokenizer {
672                prefix_text,
673                rewritten_text,
674                prefix_is_partial: true,
675            });
676            let mut stream = DecodeStream::new(tokenizer, &[1], false);
677
678            assert_eq!(stream.step(2).unwrap(), Some(expected.to_string()));
679        }
680    }
681
682    #[test]
683    fn errors_when_incremental_decode_rewrites_emitted_text() {
684        for (prefix_text, rewritten_text) in [
685            ("abcde", "abcdXY"),
686            ("abcde", "abcdX"),
687            ("abcde", "abcd"),
688            ("㺄馉凓鄗abc", "㺄馉凓鄗𫷲"),
689            (
690                "JUnitworkflow Completion intuitionabc",
691                "JUnitworkflow Completion intuition𝟙",
692            ),
693        ] {
694            let tokenizer: Arc<dyn super::traits::Tokenizer> = Arc::new(RewritingTokenizer {
695                prefix_text,
696                rewritten_text,
697                prefix_is_partial: false,
698            });
699            let mut stream = DecodeStream::new(tokenizer, &[], false);
700
701            assert_eq!(stream.step(1).unwrap(), Some(prefix_text.to_string()));
702
703            let error = stream.step(2).unwrap_err();
704            assert!(error.to_string().contains("already emitted text"));
705        }
706    }
707
708    #[test]
709    fn emits_suffix_when_incremental_decode_preserves_emitted_text() {
710        let tokenizer: Arc<dyn super::traits::Tokenizer> = Arc::new(RewritingTokenizer {
711            prefix_text: "abcde",
712            rewritten_text: "abcdeXY",
713            prefix_is_partial: false,
714        });
715        let mut stream = DecodeStream::new(tokenizer, &[], false);
716
717        assert_eq!(stream.step(1).unwrap(), Some("abcde".to_string()));
718        assert_eq!(stream.step(2).unwrap(), Some("XY".to_string()));
719    }
720}
721
722/// Maintains state for an ongoing sequence of tokens and their decoded text
723pub struct Sequence {
724    /// Encodes text -> token_ids
725    tokenizer: Tokenizer,
726
727    /// The current sequence of token ids
728    token_ids: Vec<TokenIdType>,
729
730    /// The position in the current sequence the last decoded token completed
731    prefix_offset: usize,
732
733    /// Current position in the sequence
734    read_offset: usize,
735}
736
737impl std::fmt::Debug for Sequence {
738    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
739        f.debug_struct("Sequence")
740            .field("tokenizer", &"Arc<dyn Tokenizer>")
741            .field(
742                "token_ids",
743                &format_args!("{}", {
744                    let token_ids = self.token_ids();
745                    if token_ids.len() <= 20 {
746                        format!("{:?}", token_ids)
747                    } else {
748                        let first_ten = &token_ids[..10];
749                        let last_ten = &token_ids[token_ids.len() - 10..];
750                        format!("{:?} ... {:?}", first_ten, last_ten)
751                    }
752                }),
753            )
754            .field("prefix_offset", &self.prefix_offset)
755            .field("read_offset", &self.read_offset)
756            .field("token count", &self.token_ids.len())
757            .finish()
758    }
759}
760
761impl Sequence {
762    pub fn new(tokenizer: Tokenizer) -> Self {
763        Self {
764            tokenizer,
765            token_ids: Vec::new(),
766            prefix_offset: 0,
767            read_offset: 0,
768        }
769    }
770
771    pub fn is_empty(&self) -> bool {
772        self.token_ids.is_empty()
773    }
774
775    pub fn len(&self) -> usize {
776        self.token_ids.len()
777    }
778
779    pub fn clear(&mut self) {
780        self.token_ids.clear();
781        self.prefix_offset = 0;
782        self.read_offset = 0;
783    }
784
785    pub fn append_text(&mut self, input: &str) -> Result<()> {
786        // let tokenizer = self.tokenizer.read().map_err(|err| {
787        //     Error::msg(format!("Failed to acquire read lock on tokenizer: {}", err))
788        // })?;
789
790        let encoding = self.tokenizer.encode(input)?;
791        self.token_ids.extend(encoding.token_ids());
792        Ok(())
793    }
794
795    // Based on
796    // https://github.com/huggingface/text-generation-inference/blob/v0.9.4/server/text_generation_server/models/model.py#L62C9-L62C15
797    // under Apache 2.0 license
798    pub fn append_token_id(&mut self, token_id: TokenIdType) -> Result<String> {
799        self.token_ids.push(token_id);
800        // log::trace!("pushed token_id: {}", token_id);
801
802        let prefix_text: String = self
803            .tokenizer
804            .decode(&self.token_ids[self.prefix_offset..self.read_offset], false)?
805            .into();
806
807        let new_result = self
808            .tokenizer
809            .decode(&self.token_ids[self.prefix_offset..], false)?;
810
811        let new_text = new_result.as_str();
812
813        // if the end character of the previous returned sequence is a multi-byte character
814        // then we can not split the text on that byte offset, so we roll back to the byte offset
815        // of the start of that character
816        let mut prefix_text_len = prefix_text.len();
817        while !new_text.is_char_boundary(prefix_text_len) && prefix_text_len > 0 {
818            prefix_text_len -= 1;
819        }
820        let prefix_text_len = prefix_text_len;
821
822        if new_text.len() > prefix_text.len() {
823            if new_result.is_partial() {
824                return Ok("".to_string());
825            } else {
826                // shift and update the state
827                let new_text = new_text[prefix_text_len..]
828                    .to_string()
829                    .replace('\u{FFFD}', "");
830                self.prefix_offset = self.read_offset;
831                self.read_offset = self.token_ids.len();
832                return Ok(new_text);
833            }
834        }
835
836        Ok("".to_string())
837    }
838
839    pub fn tokenizer(&self) -> Tokenizer {
840        self.tokenizer.clone()
841    }
842
843    pub fn token_ids(&self) -> &[TokenIdType] {
844        &self.token_ids
845    }
846
847    pub fn text(&self) -> Result<String> {
848        // let tokenizer = self.tokenizer.read().map_err(|err| {
849        //     Error::msg(format!("Failed to acquire read lock on tokenizer: {}", err))
850        // })?;
851        Ok(self.tokenizer.decode(&self.token_ids, false)?.into())
852    }
853}
854
855/// The output conditions/values of a SequenceDecoder::add_token_id operation.
856/// Result of decoding a token, indicating whether text was produced or a stop condition was met
857pub enum SequenceDecoderOutput {
858    /// The text for the appended token_id
859    Text(String),
860
861    /// A sequence of token_ids has been partially matched a stop sequence, so the text is held
862    /// until either a match or a divergence
863    Held,
864
865    /// Indicates that a stop sequence has been matched and the decoder is stopped.
866    /// Subsequent calls to append_token_id will return an error
867    Stopped,
868
869    /// Indicates that a stop token_id has been matched and the decoder is stopped.
870    /// Subsequent calls to append_token_id will return an error
871    /// The text for the stop token_id is returned
872    StoppedWithText(String),
873}
874
875/// A Sequence for decoding a stream of token ids into text and detecting stop sequences.
876/// A stop sequence is either a matching token_id or a sequence of texts/strings which match.
877/// Matches happen first at the token-level, then at the sequence-level. Hidden takes precedence
878/// over visible. For example, if you put the same token_id in both `stop_token_ids_visible` and
879/// `stop_token_ids_hidden`, the token_id will be treated as hidden.
880#[derive(Debug)]
881pub struct StopSequenceDecoder {
882    // The current sequence of token ids
883    sequence: Sequence,
884
885    // Stop Tokens - the presence of any one of these should trigger a stop
886    // If found, the text for the matched token will be returned
887    stop_token_ids_visible: Vec<TokenIdType>,
888
889    // Stop Tokens - the presence of any one of these should trigger a stop
890    // If found, the text for the matched token will NOT be returned
891    stop_token_ids_hidden: Vec<TokenIdType>,
892
893    // Stop Words - the presence of any one of these should trigger a stop
894    // If found, the text for the matched token will be returned
895    #[allow(dead_code)]
896    stop_sequences_visible: Vec<String>,
897
898    // Stop Words - the presence of any one of these should trigger a stop
899    // If found, the text for the matched token will NOT be returned
900    stop_sequences_hidden: Vec<String>,
901
902    // If the decoder has observed and returned a stop SequenceDecoderOutput,
903    // futhur calls to append_token_id will return an error
904    stopped: bool,
905
906    // text jail - if a partial stop sequence is being observed, we hold/jail the text
907    // until either the stop sequence is matched or the sequence is reset by a divergence
908    state: String,
909}
910
911impl StopSequenceDecoder {
912    /// Builder object for configurating a StopSequenceDecoder
913    pub fn builder(tokenizer: Tokenizer) -> StopSequenceDecoderBuilder {
914        StopSequenceDecoderBuilder::new(tokenizer)
915    }
916
917    /// Add a token_id to the sequence and return the SequenceDecoderOutput
918    pub fn append_token_id(&mut self, token_id: TokenIdType) -> Result<SequenceDecoderOutput> {
919        if self.stopped {
920            return Err(Error::msg("Decoder is stopped"));
921        }
922
923        // update the sequence
924        let text = self.sequence.append_token_id(token_id)?;
925
926        // append the text to the state
927        self.state.push_str(text.as_str());
928
929        let mut stop: bool = false;
930        let mut visible: bool = false;
931
932        if self.stop_token_ids_visible.contains(&token_id) {
933            stop = true;
934            visible = true;
935        }
936
937        if self.stop_token_ids_hidden.contains(&token_id) {
938            stop = true;
939            visible = false;
940        }
941
942        if stop {
943            self.stopped = true;
944            let state = std::mem::take(&mut self.state);
945            if visible {
946                return Ok(SequenceDecoderOutput::StoppedWithText(state));
947            }
948            return Ok(SequenceDecoderOutput::Stopped);
949        }
950
951        // determine if state matches any of the stop sequences
952        for stop_sequence in self.stop_sequences_hidden.iter() {
953            if stop_sequence.starts_with(&self.state) {
954                if stop_sequence == &self.state {
955                    // on matched stop sequence, we do NOT return the jailed stop sequence
956                    self.stopped = true;
957                    return Ok(SequenceDecoderOutput::Stopped);
958                } else {
959                    return Ok(SequenceDecoderOutput::Held);
960                }
961            }
962        }
963
964        let state = std::mem::take(&mut self.state);
965        Ok(SequenceDecoderOutput::Text(state))
966    }
967
968    pub fn is_empty(&self) -> bool {
969        self.sequence.token_ids.is_empty()
970    }
971
972    pub fn len(&self) -> usize {
973        self.sequence.token_ids.len()
974    }
975
976    pub fn is_complete(&self) -> bool {
977        self.stopped
978    }
979
980    pub fn close(&mut self) {
981        self.stopped = true;
982    }
983}
984
985pub struct StopSequenceDecoderBuilder {
986    tokenizer: Tokenizer,
987    stop_token_ids_visible: Vec<TokenIdType>,
988    stop_token_ids_hidden: Vec<TokenIdType>,
989    stop_sequences_visible: Vec<String>,
990    stop_sequences_hidden: Vec<String>,
991}
992
993impl StopSequenceDecoderBuilder {
994    pub fn new(tokenizer: Tokenizer) -> Self {
995        Self {
996            tokenizer,
997            stop_token_ids_visible: Vec::new(),
998            stop_token_ids_hidden: Vec::new(),
999            stop_sequences_visible: Vec::new(),
1000            stop_sequences_hidden: Vec::new(),
1001        }
1002    }
1003
1004    /// Adds a visible stop token id to the StopSequenceDecoder
1005    pub fn add_stop_token_id_visible(mut self, token_id: TokenIdType) -> Self {
1006        self.stop_token_ids_visible.push(token_id);
1007        self
1008    }
1009
1010    /// Adds a list of visible stop token ids to the StopSequenceDecoder
1011    /// Each token_id is added as for an individual match
1012    pub fn add_stop_token_ids_visible(mut self, token_ids: &[TokenIdType]) -> Self {
1013        self.stop_token_ids_visible.extend(token_ids);
1014        self
1015    }
1016
1017    /// Adds a hidden stop token id to the StopSequenceDecoder
1018    pub fn add_stop_token_id_hidden(mut self, token_id: TokenIdType) -> Self {
1019        self.stop_token_ids_hidden.push(token_id);
1020        self
1021    }
1022
1023    /// Adds a list of hidden stop token ids to the StopSequenceDecoder
1024    /// Each token_id is added as for an individual match
1025    pub fn add_stop_token_ids_hidden(mut self, token_ids: &[TokenIdType]) -> Self {
1026        self.stop_token_ids_hidden.extend(token_ids);
1027        self
1028    }
1029
1030    pub fn add_stop_sequence_visible(mut self, text: &str) -> Self {
1031        self.stop_sequences_visible.push(text.to_string());
1032        self
1033    }
1034
1035    pub fn add_stop_sequences_visible(mut self, strings: &[&str]) -> Self {
1036        self.stop_sequences_visible
1037            .extend(strings.iter().map(|text| text.to_string()));
1038        self
1039    }
1040
1041    pub fn add_stop_sequence_hidden(mut self, text: &str) -> Self {
1042        self.stop_sequences_hidden.push(text.to_string());
1043        self
1044    }
1045
1046    pub fn add_stop_sequences_hidden(mut self, strings: &[&str]) -> Self {
1047        self.stop_sequences_hidden
1048            .extend(strings.iter().map(|text| text.to_string()));
1049        self
1050    }
1051
1052    pub fn build(self) -> Result<StopSequenceDecoder> {
1053        Ok(StopSequenceDecoder {
1054            sequence: Sequence::new(self.tokenizer.clone()),
1055            stop_token_ids_visible: self.stop_token_ids_visible,
1056            stop_token_ids_hidden: self.stop_token_ids_hidden,
1057            stop_sequences_visible: self.stop_sequences_visible,
1058            stop_sequences_hidden: self.stop_sequences_hidden,
1059            stopped: false,
1060            state: String::new(),
1061        })
1062    }
1063}