Module Brot.Decoder
Decoding tokens back to text.
Decoders convert token strings back into natural text by reversing encoding-specific transformations (prefix/suffix removal, byte-level decoding, whitespace normalization, etc.).
Decoders operate on token strings, not IDs. Convert IDs to strings via vocabulary first, then apply decode.
A decoder rewrites a token list into another token list, and decode is the concatenation of the result. Most decoders rewrite each token on its own (bpe, wordpiece, metaspace, replace, strip); byte_fallback and ctc also join or drop tokens; byte_level and fuse collapse the whole list into one token. The distinction matters when composing decoders with sequence: a collapsing decoder hides token boundaries from the decoders that follow it.
type tThe type for decoders.
Constructors
bpe ~suffix () is a decoder for BPE-encoded tokens. Every occurrence of suffix, which marks the end of a word, becomes the space that follows it, except in the last token where it is dropped. suffix defaults to "</w>".
byte_level is a collapsing decoder that reads GPT-2 style byte-to-Unicode tokens back as the text their bytes spell.
A token is mapped character by character, and one character outside the byte-level alphabet leaves the whole token to stand for its own bytes. The bytes of every token are then read as one text, so a character spelled across two tokens comes back whole, and every maximal ill-formed byte sequence becomes one U+FFFD — four stray bytes cost four, while a four-byte character cut short costs one.
byte_fallback is a decoder for byte fallback tokens. Each run of hex byte tokens (e.g. "<0x41>") becomes the text those bytes spell; a run that is not valid UTF-8 becomes one U+FFFD per byte. Other tokens pass through unchanged.
wordpiece ~prefix ~cleanup () is a decoder for WordPiece tokens. Strips continuation prefix (default "##") from non-initial subwords and gives the others a leading space. When cleanup is true (default), applies the detokenization cleanup to every token, once its joining space is prepended: the space before ., ?, !, , and the English contractions is taken back, and " do not" is rewritten to " don't". So "hello"; ","; "world" decodes to "hello, world", while "3"; "."; "14" decodes to "3. 14" because the space after the full stop was never the decoder's to remove.
val metaspace :
?replacement:string ->
?prepend_scheme:Pre_tokenizer.prepend_scheme ->
unit ->
tmetaspace ~replacement ~prepend_scheme () converts metaspace markers back to spaces. replacement defaults to "\u{2581}". Unless prepend_scheme is `Never, the marker was prepended to the text rather than standing for a space, so every occurrence of it in the first token is dropped instead of becoming a space — on `First as on `Always, the two prepending to the same first token. prepend_scheme defaults to `Always.
ctc ~pad_token ~word_delimiter_token ~cleanup () is a decoder for CTC (Connectionist Temporal Classification) output. Deduplicates consecutive tokens, then cuts every occurrence of pad_token (default "<pad>") out of each token, wherever in it they fall, and drops the tokens left empty. When cleanup is true (default), applies the same detokenization cleanup as wordpiece to every token and then replaces word_delimiter_token (default "|") with spaces.
sequence decoders chains decoders left-to-right. Each decoder's output token list feeds into the next.
replace ~pattern ~by () replaces every literal occurrence of pattern with by in each token.
strip ~content ~start ~stop () removes up to start leading and stop trailing occurrences of content from each token. content defaults to " ", start and stop to 0. A token left with nothing between the two cuts becomes empty.
HuggingFace reads content as a single character, so to_json on a decoder whose content is longer, or empty, writes a decoder it rejects.
fuse is a collapsing decoder that concatenates all tokens into a single string with no delimiter.
Operations
decode decoder tokens applies decoder to tokens and returns the decoded text.