Brot vs. HuggingFace Tokenizers -- a practical comparison
This guide explains how Brot relates to Python's HuggingFace Tokenizers, focusing on:
- How core concepts map (tokenizer types, pipeline stages, encoding results)
- Where the APIs feel similar vs. deliberately different
- How to translate common HuggingFace patterns into Brot
If you already use HuggingFace Tokenizers, this should be enough to become productive in Brot quickly.
1. Big-picture differences
| Aspect | HuggingFace Tokenizers (Python) | Brot (OCaml) |
|---|---|---|
| Language | Python bindings over Rust | Native OCaml |
| Core type | tokenizers.Tokenizer |
Brot.t |
| Encoding result | tokenizers.Encoding |
Encoding.t |
| Algorithms | BPE, WordPiece, Unigram, WordLevel |
Brot.bpe, Brot.wordpiece, Brot.unigram, Brot.word_level, Brot.chars |
| Pipeline stages | Mutable properties on Tokenizer object |
Immutable ~normalizer, ~pre, ~post, ~decoder args |
| Mutability | Tokenizer is mutable (set properties after creation) | Tokenizer is immutable after creation |
| HuggingFace compat | Native format | Full tokenizer.json read/write via from_file/save_pretrained |
| Training | Trainer objects passed to tokenizer.train() |
Brot.train_bpe, Brot.train_wordpiece, etc. |
| Padding config | tokenizer.enable_padding() |
~padding arg on encode/encode_batch |
| Truncation config | tokenizer.enable_truncation() |
~truncation arg on encode/encode_batch |
Brot semantics to know (read once):
- Tokenizers are immutable. Pipeline components are set at construction time, not mutated after.
from_filereturns(t, string) result. Handle errors explicitly.- Padding and truncation are per-call parameters, not global tokenizer state.
- Added tokens use a record type (
Brot.added_token) with explicit control over stripping and normalization. Like HuggingFace's added tokens, they are matched atomically in the input onencode. encodereturnsEncoding.t; useencode_idswhen you only need the ID array.- The
train_*functions count the pre-tokens their own pipeline produces, as HuggingFace's trainers do: pass the~normalizerand~preyou intend to encode with, or a whole line of the corpus counts as one word. - Unigram models segment as HuggingFace does.
from_filereads the scores of atokenizer.jsonfrom their text astokenizersdoes, which can land a score one unit in the last place from the double its text names; that unit decides exact ties, such as whether a run of-is cut-,---,---or---,---,-.of_jsontakes the numbers of a tree as given.
2. Loading pretrained tokenizers
2.1 From a tokenizer.json file
HuggingFace
from tokenizers import Tokenizer
tokenizer = Tokenizer.from_file("tokenizer.json")Brot
let tokenizer = Brot.from_file "tokenizer.json" |> Result.get_ok
Both read the same tokenizer.json format. Brot's from_file returns a result instead of raising an exception.
2.2 From vocabulary and merges files
HuggingFace
from tokenizers import Tokenizer
from tokenizers.models import BPE
tokenizer = Tokenizer(BPE.from_file("vocab.json", "merges.txt"))Brot
let tokenizer = Brot.from_model_file ~vocab:"vocab.json" ~merges:"merges.txt" ()
When ~merges is omitted, Brot infers WordPiece instead of BPE.
2.3 Saving
HuggingFace
tokenizer.save("tokenizer.json")Brot
Brot.save_pretrained tokenizer ~path:"./my_tokenizer"
save_pretrained creates path/tokenizer.json in HuggingFace format. Use to_json when you need the JSON value directly.
3. Encoding text
3.1 Basic encoding
HuggingFace
output = tokenizer.encode("Hello world!")
output.ids # [101, 7592, 2088, 999, 102]
output.tokens # ['[CLS]', 'hello', 'world', '!', '[SEP]']
output.offsets # [(0, 0), (0, 5), (6, 11), (11, 12), (0, 0)]
output.type_ids # [0, 0, 0, 0, 0]
output.attention_mask # [1, 1, 1, 1, 1]Brot
let enc = Brot.encode tokenizer "Hello world!" let ids = Encoding.ids enc (* int array *) let toks = Encoding.tokens enc (* string array *) let offs = Encoding.offsets enc (* (int * int) array *) let types = Encoding.type_ids enc (* int array *) let mask = Encoding.attention_mask enc (* int array *)
3.2 IDs only
HuggingFace
ids = tokenizer.encode("Hello world!").idsBrot
let ids = Brot.encode_ids tokenizer "Hello world!"
encode_ids is a shortcut that avoids constructing the full Encoding.t when you only need token IDs.
3.3 Without special tokens
HuggingFace
output = tokenizer.encode("Hello world!", add_special_tokens=False)Brot
let enc = Brot.encode tokenizer ~add_special_tokens:false "Hello world!"
4. Decoding
4.1 Basic decoding
HuggingFace
text = tokenizer.decode([101, 7592, 2088, 999, 102]) text_clean = tokenizer.decode([101, 7592, 2088, 999, 102], skip_special_tokens=True)
Brot
let text = Brot.decode tokenizer [| 101; 7592; 2088; 999; 102 |] let text_clean = Brot.decode tokenizer ~skip_special_tokens:true [| 101; 7592; 2088; 999; 102 |]
Divergence. skip_special_tokens drops a special added token in brot
whenever its ID appears. HuggingFace matches the token by the string it decodes
to, which for a token whose normalized is set is the normalized form, while
the set it looks that string up in holds unnormalized content — so a normalizer
that rewrites the content stops the token from being skipped at all. On LLaMA,
decode([0], skip_special_tokens=True) is "<unk>" there and "" here.
4.2 Batch decoding
HuggingFace
texts = tokenizer.decode_batch([[101, 7592, 102], [101, 2088, 102]])
Brot
let texts = List.map (Brot.decode tokenizer) [ [| 101; 7592; 102 |]; [| 101; 2088; 102 |] ]
4.3 Streaming decoding
HuggingFace
from tokenizers.decoders import DecodeStream
stream = DecodeStream(skip_special_tokens=False)
for id in ids:
chunk = stream.step(tokenizer, id) # None while the text is not final
if chunk is not None:
print(chunk, end="")Brot
let s = Array.fold_left (fun s id -> let text, s = Brot.decode_step s id in print_string text; s) (Brot.decode_stream tokenizer) ids in print_string (Brot.decode_finish s)
A brot stream is a value: decode_step returns the next one. Its texts and
decode_finish always make up decode of the ids, so a run of byte-fallback
tokens is held until the run ends, where a valid prefix of it may already have
come out of DecodeStream. See decode_step in
brot.mli.
5. Batch encoding
HuggingFace
outputs = tokenizer.encode_batch(["Hello world!", "How are you?"])
# outputs is a list of Encoding objects
for enc in outputs:
print(enc.ids)Brot
let encodings = Brot.encode_batch tokenizer [ "Hello world!"; "How are you?" ] let () = List.iter (fun enc -> let ids = Encoding.ids enc in Array.iter (Printf.printf "%d ") ids; print_newline ()) encodings
Both return a list of encoding objects, one per input.
6. Padding and truncation
6.1 Padding
In HuggingFace, padding is global state on the tokenizer. In Brot, it is a per-call parameter.
HuggingFace
tokenizer.enable_padding(
direction="right",
pad_id=0,
pad_token="[PAD]",
length=128, # fixed length
)
output = tokenizer.encode("Hello")
# output.attention_mask shows 0s for padding positionsBrot
let pad = Brot.padding ~pad_id:0 ~pad_token:"[PAD]" (`Fixed 128) let enc = Brot.encode tokenizer ~padding:pad "Hello" (* Encoding.attention_mask enc has 0s for padding positions *)
Padding strategies:
| HuggingFace | Brot |
|---|---|
length=None (pad to longest in batch) |
`Batch_longest |
length=128 (fixed) |
`Fixed 128 |
pad_to_multiple_of=8 |
`To_multiple 8 |
direction="left" |
~direction:Left` |
direction="right" (default) |
~direction:Right` (default) |
6.2 Truncation
HuggingFace
tokenizer.enable_truncation(max_length=512, direction="right")
output = tokenizer.encode("Very long text ...")Brot
let trunc = Brot.truncation 512 let enc = Brot.encode tokenizer ~truncation:trunc "Very long text ..."
Truncation direction defaults to `Right in both libraries.
6.3 Combined padding and truncation
HuggingFace
tokenizer.enable_padding(length=512, pad_token="[PAD]", pad_id=0) tokenizer.enable_truncation(max_length=512) outputs = tokenizer.encode_batch(texts)
Brot
let pad = Brot.padding ~pad_token:"[PAD]" ~pad_id:0 (`Fixed 512) let trunc = Brot.truncation 512 let encodings = Brot.encode_batch tokenizer ~padding:pad ~truncation:trunc texts
The key difference: Brot passes these as arguments, so different calls can use different settings without mutating the tokenizer.
7. Sentence pairs
HuggingFace
# Single pair
output = tokenizer.encode("premise", "hypothesis")
output.type_ids # [0, 0, 0, 0, 1, 1, 1] (with BERT post-processor)
# Batch of pairs
outputs = tokenizer.encode_batch([("premise1", "hyp1"), ("premise2", "hyp2")])Brot
(* Single pair *) let enc = Brot.encode tokenizer ~pair:"hypothesis" "premise" let type_ids = Encoding.type_ids enc (* 0s for first, 1s for second *) (* Batch of pairs *) let encodings = Brot.encode_batch_pairs tokenizer [ ("premise1", "hyp1"); ("premise2", "hyp2") ]
Brot uses the ~pair optional argument on encode for single pairs and a dedicated encode_batch_pairs for batches, instead of overloading the same function with tuples.
8. Special tokens
8.1 Defining special tokens
HuggingFace
from tokenizers import AddedToken
tokenizer.add_special_tokens([
AddedToken("[CLS]", single_word=False, lstrip=False, rstrip=False),
AddedToken("[SEP]", single_word=False, lstrip=False, rstrip=False),
AddedToken("[PAD]", single_word=False, lstrip=False, rstrip=False),
])Brot
let tokenizer = Brot.bpe ~added_tokens:[ Brot.added_token "[CLS]"; Brot.added_token "[SEP]"; Brot.added_token "[PAD]"; ] ~pad_token:"[PAD]" ~bos_token:"[CLS]" ~eos_token:"[SEP]" ()
In HuggingFace, special tokens are added after construction. In Brot they are usually part of construction, since tokenizers are immutable; Brot.add_tokens returns a new tokenizer with more of them, for any model. The added_token function accepts optional ~single_word, ~lstrip, ~rstrip, ~normalized and ~special parameters matching AddedToken. Its defaults line the two HuggingFace registrations up one for one: added_token c is add_special_tokens([c]) and added_token ~special:false c is add_tokens([c]). Both libraries match these tokens atomically in the input, ahead of the normalizer and the pre-tokenizer, and neither puts them in the model's own vocabulary.
8.2 Role tokens
HuggingFace
tokenizer.pad_token # "[PAD]" tokenizer.cls_token # "[CLS]" tokenizer.sep_token # "[SEP]" tokenizer.unk_token # "[UNK]"
Brot
let pad = Brot.pad_token tokenizer (* string option *) let bos = Brot.bos_token tokenizer (* string option *) let eos = Brot.eos_token tokenizer (* string option *) let unk = Brot.unk_token tokenizer (* string option *)
Brot uses bos_token/eos_token instead of cls_token/sep_token since these are model-agnostic roles. They return option instead of raising on missing tokens.
8.3 Special tokens mask
Both libraries provide a mask distinguishing special tokens from content tokens in the encoding:
HuggingFace
output.special_tokens_mask # [1, 0, 0, 0, 1]
Brot
let mask = Encoding.special_tokens_mask enc (* int array: 1 for special, 0 for content *)
8.4 Special tokens in the input as text
HuggingFace
tokenizer.encode_special_tokens = True
output = tokenizer.encode("<|endoftext|> from a user")Brot
let enc = Brot.encode tokenizer ~match_special_tokens:false "<|endoftext|> from a user"
HuggingFace sets this on the tokenizer; brot takes it per call, on every
encode function, so text from a user and text the program writes can be
encoded by the same tokenizer.
9. Pipeline components
Both libraries use the same four-stage pipeline: normalizer, pre-tokenizer, post-processor, decoder. The difference is how they are configured.
9.1 Normalizer
HuggingFace
from tokenizers import normalizers
tokenizer.normalizer = normalizers.Sequence([
normalizers.NFD(),
normalizers.StripAccents(),
normalizers.Lowercase(),
])Brot
let norm = Normalizer.sequence [ Normalizer.nfd; Normalizer.strip_accents; Normalizer.lowercase ] let tokenizer = Brot.bpe ~normalizer:norm ()
Common normalizers:
| HuggingFace | Brot |
|---|---|
normalizers.NFC() |
Normalizer.nfc |
normalizers.NFD() |
Normalizer.nfd |
normalizers.NFKC() |
Normalizer.nfkc |
normalizers.NFKD() |
Normalizer.nfkd |
normalizers.Lowercase() |
Normalizer.lowercase |
normalizers.StripAccents() |
Normalizer.strip_accents |
normalizers.Strip() |
Normalizer.strip () |
normalizers.Replace(pattern, rep) |
Normalizer.replace ~pattern ~replacement |
normalizers.Replace(Regex(pat), rep) |
Normalizer.replace_regex ~pattern ~replacement |
normalizers.Prepend(s) |
Normalizer.prepend s |
normalizers.BertNormalizer() |
Normalizer.bert () |
normalizers.ByteLevel() |
Normalizer.byte_level |
normalizers.Nmt() |
Normalizer.nmt |
normalizers.Precompiled(charsmap) |
Normalizer.precompiled charsmap |
normalizers.Sequence([...]) |
Normalizer.sequence [...] |
precompiled reads text by extended grapheme cluster. Brot cuts clusters with
its own tables of Unicode 17.0.0, the version HuggingFace's unicode-segmentation
implements, rather than with Uucp's, so the two agree on characters whose
cluster properties changed since: U+05C8, unassigned in Unicode 17, is a
cluster of its own after "½" for both.
9.2 Pre-tokenizer
HuggingFace
from tokenizers import pre_tokenizers
tokenizer.pre_tokenizer = pre_tokenizers.Sequence([
pre_tokenizers.WhitespaceSplit(),
pre_tokenizers.Punctuation(),
])Brot
let pre = Pre_tokenizer.sequence [ Pre_tokenizer.whitespace_split; Pre_tokenizer.punctuation () ] let tokenizer = Brot.bpe ~pre ()
Common pre-tokenizers:
| HuggingFace | Brot |
|---|---|
pre_tokenizers.Whitespace() |
Pre_tokenizer.whitespace |
pre_tokenizers.WhitespaceSplit() |
Pre_tokenizer.whitespace_split |
pre_tokenizers.BertPreTokenizer() |
Pre_tokenizer.bert |
pre_tokenizers.ByteLevel() |
Pre_tokenizer.byte_level () |
pre_tokenizers.Punctuation() |
Pre_tokenizer.punctuation () |
pre_tokenizers.Digits() |
Pre_tokenizer.digits () |
pre_tokenizers.Metaspace() |
Pre_tokenizer.metaspace () |
pre_tokenizers.UnicodeScripts() |
Pre_tokenizer.unicode_scripts |
pre_tokenizers.CharDelimiterSplit(c) |
Pre_tokenizer.char_delimiter c |
pre_tokenizers.Split(pattern, ...) |
Pre_tokenizer.split ~pattern () |
pre_tokenizers.Split(Regex(pattern), ...) |
Pre_tokenizer.split_regex ~pattern () |
pre_tokenizers.Sequence([...]) |
Pre_tokenizer.sequence [...] |
unicode_scripts reads the script of a character from the Unicode 17.0.0
Script property, the data split_regex reads \p{Han} from. HuggingFace ships
a Unicode 9.0.0 table that reports every character assigned since as scriptless
and joins it to the preceding piece, so the two split differently around
characters assigned from Unicode 10.0.0 to 17.0.0, such as "₿" (U+20BF).
Characters assigned after 17.0.0 are scriptless to both.
The same skew runs through the character classes. Brot reads them from Uucp,
which carries Unicode 18.0.0 in brot's lock, where HuggingFace's Rust
dependencies ship older tables. Measured when Uucp carried Unicode 17.0.0,
\p{L} and \p{N}, which byte_level splits on, disagreed on about 4,600
and 13 code points respectively, and the punctuation class of bert and
punctuation on 143; Unicode 18.0.0 widens the gap by the characters it
assigns. Nearly all of those are characters assigned since HuggingFace's tables
were generated, which brot classifies as letters, numbers or punctuation and
HuggingFace does not; two go the other way (U+166D and U+111C9 are punctuation
to HuggingFace and not to Unicode 17.0.0).
split_regex reads the pattern of a Split whose pattern is a Regex, which
is how the tokenizer files of Llama 3, Qwen, DeepSeek-V3 and gpt-oss describe
their pre-tokenization, and gives the pieces HuggingFace gives. A lookahead is
accepted where it ends the pattern or one of its alternatives, as in
\s+(?!\S); a negative one looks at a single character. Under (?i:...) a
character matches the characters with the same simple case folding, so 's
matches 'S and 'ſ, but a folding to several characters is not followed:
(?i:ss) does not match "ß". Lookbehind, a lookahead followed by more of the
pattern, anchors, backreferences, atomic groups and possessive quantifiers are
rejected when the file is loaded, with a message naming the construct.
Bracket classes nest and intersect with &&, as in Kimi K2's
[\p{L}&&[^\p{Han}]], and \p{..} names a script as well as a general
category. A script is the Script property of Unicode 17.0.0, where
HuggingFace's regular expression engine (Oniguruma) carries an older version:
characters assigned since, as the CJK Extension J ideographs (U+323B0 to
U+33479) of Unicode 17.0.0, are Han to brot and to no script in HuggingFace,
so [\p{Han}]+ takes them in brot and leaves them out in HuggingFace.
9.3 Post-processor
HuggingFace
from tokenizers import processors
tokenizer.post_processor = processors.BertProcessing(
sep=("[SEP]", 102),
cls=("[CLS]", 101),
)Brot
let post = Post_processor.bert ~sep:("[SEP]", 102) ~cls:("[CLS]", 101) let tokenizer = Brot.bpe ~post ()
Common post-processors:
| HuggingFace | Brot |
|---|---|
processors.BertProcessing(sep, cls) |
Post_processor.bert ~sep ~cls |
processors.RobertaProcessing(sep, cls) |
Post_processor.roberta ~sep ~cls () |
processors.ByteLevel() |
Post_processor.byte_level () |
processors.TemplateProcessing(single, pair, special_tokens) |
Post_processor.template ~single ?pair ~special_tokens () |
processors.Sequence([...]) |
Post_processor.sequence [...] |
9.4 Decoder
HuggingFace
from tokenizers import decoders tokenizer.decoder = decoders.WordPiece(prefix="##")
Brot
let dec = Decoder.wordpiece ~prefix:"##" () let tokenizer = Brot.wordpiece ~decoder:dec ()
Common decoders:
| HuggingFace | Brot |
|---|---|
decoders.BPEDecoder(suffix) |
Decoder.bpe ~suffix () |
decoders.ByteLevel() |
Decoder.byte_level |
decoders.ByteFallback() |
Decoder.byte_fallback |
decoders.WordPiece(prefix) |
Decoder.wordpiece ~prefix () |
decoders.Metaspace() |
Decoder.metaspace () |
decoders.CTC() |
Decoder.ctc () |
decoders.Replace(pattern, by) |
Decoder.replace ~pattern ~by () |
decoders.Strip() |
Decoder.strip () |
decoders.Fuse() |
Decoder.fuse |
decoders.Sequence([...]) |
Decoder.sequence [...] |
9.5 Inspecting the pipeline
HuggingFace
tokenizer.normalizer tokenizer.pre_tokenizer tokenizer.post_processor tokenizer.decoder
Brot
let norm = Brot.normalizer tokenizer (* Normalizer.t option *) let pre = Brot.pre_tokenizer tokenizer (* Pre_tokenizer.t option *) let post = Brot.post_processor tokenizer (* Post_processor.t option *) let dec = Brot.decoder tokenizer (* Decoder.t option *)
Brot returns option for each stage, since any stage can be absent.
10. Training tokenizers
10.1 BPE training
HuggingFace
from tokenizers import Tokenizer
from tokenizers.models import BPE
from tokenizers.pre_tokenizers import Whitespace
from tokenizers.trainers import BpeTrainer
tokenizer = Tokenizer(BPE())
tokenizer.pre_tokenizer = Whitespace()
trainer = BpeTrainer(
vocab_size=30000,
min_frequency=2,
special_tokens=["[UNK]", "[CLS]", "[SEP]", "[PAD]"],
)
tokenizer.train(["corpus.txt"], trainer)Brot
let tokenizer = Brot.train_bpe (`Files [ "corpus.txt" ]) ~vocab_size:30000 ~min_frequency:2 ~pre:(Brot.Pre_tokenizer.whitespace) ~added_tokens:[ Brot.added_token "[UNK]"; Brot.added_token "[CLS]"; Brot.added_token "[SEP]"; Brot.added_token "[PAD]"; ] ~unk_token:"[UNK]" ~pad_token:"[PAD]"
Brot combines the Tokenizer + Trainer pattern into a single function call. Training data is passed as `Files (file paths) or `Seq (string sequence).
10.2 WordPiece training
HuggingFace
from tokenizers.models import WordPiece from tokenizers.pre_tokenizers import Whitespace from tokenizers.trainers import WordPieceTrainer tokenizer = Tokenizer(WordPiece(unk_token="[UNK]")) tokenizer.pre_tokenizer = Whitespace() trainer = WordPieceTrainer(vocab_size=30000, special_tokens=["[UNK]", "[PAD]"]) tokenizer.train(["corpus.txt"], trainer)
Brot
let tokenizer = Brot.train_wordpiece (`Files [ "corpus.txt" ]) ~vocab_size:30000 ~pre:(Brot.Pre_tokenizer.whitespace) ~unk_token:"[UNK]" ~added_tokens:[ Brot.added_token "[UNK]"; Brot.added_token "[PAD]" ] ~pad_token:"[PAD]"
10.3 Unigram training
HuggingFace
from tokenizers.models import Unigram from tokenizers.pre_tokenizers import Whitespace from tokenizers.trainers import UnigramTrainer tokenizer = Tokenizer(Unigram()) tokenizer.pre_tokenizer = Whitespace() trainer = UnigramTrainer(vocab_size=8000, special_tokens=["<unk>", "<pad>"]) tokenizer.train(["corpus.txt"], trainer)
Brot
let tokenizer = Brot.train_unigram (`Files [ "corpus.txt" ]) ~vocab_size:8000 ~pre:(Brot.Pre_tokenizer.whitespace) ~unk_token:"<unk>" ~added_tokens:[ Brot.added_token "<unk>"; Brot.added_token "<pad>" ] ~pad_token:"<pad>"
10.4 Training from in-memory data
HuggingFace
from tokenizers import Tokenizer
from tokenizers.models import BPE
from tokenizers.pre_tokenizers import Whitespace
from tokenizers.trainers import BpeTrainer
tokenizer = Tokenizer(BPE())
tokenizer.pre_tokenizer = Whitespace()
trainer = BpeTrainer(vocab_size=1000)
tokenizer.train_from_iterator(
["Hello world", "How are you?", "Hello again"],
trainer,
)Brot
let texts = [ "Hello world"; "How are you?"; "Hello again" ] let tokenizer = Brot.train_bpe (`Seq (List.to_seq texts)) ~vocab_size:1000 ~pre:(Brot.Pre_tokenizer.whitespace)
10.5 Extending an existing tokenizer
HuggingFace
# Load, then retrain with more data
tokenizer = Tokenizer.from_file("tokenizer.json")
trainer = BpeTrainer(vocab_size=50000)
tokenizer.train(["more_data.txt"], trainer)Brot
let base = Brot.from_file "tokenizer.json" |> Result.get_ok let tokenizer = Brot.train_bpe ~init:base (`Files [ "more_data.txt" ]) ~vocab_size:50000
The ~init parameter on training functions lets you extend an existing tokenizer with additional data.
11. Vocabulary inspection
HuggingFace
tokenizer.get_vocab() # dict: token -> id
tokenizer.get_vocab_size() # int
tokenizer.token_to_id("[CLS]") # int or None
tokenizer.id_to_token(101) # str or NoneBrot
let v = Brot.vocab tokenizer (* (string * int) list *) let size = Brot.vocab_size tokenizer (* int *) let id = Brot.token_to_id tokenizer "[CLS]" (* int option *) let token = Brot.id_to_token tokenizer 101 (* string option *)
vocab returns an association list instead of a dictionary. token_to_id and id_to_token return option instead of nullable values.
12. Quick cheat sheet
| Task | HuggingFace Tokenizers | Brot |
|---|---|---|
| Load from file | Tokenizer.from_file("tokenizer.json") |
Brot.from_file "tokenizer.json" |
| Save to file | tokenizer.save("tokenizer.json") |
Brot.save_pretrained tokenizer ~path:"./out" |
| Encode text | tokenizer.encode("Hello") |
Brot.encode tokenizer "Hello" |
| Encode IDs only | tokenizer.encode("Hello").ids |
Brot.encode_ids tokenizer "Hello" |
| Encode batch | tokenizer.encode_batch(["a", "b"]) |
Brot.encode_batch tokenizer ["a"; "b"] |
| Encode pair | tokenizer.encode("a", "b") |
Brot.encode tokenizer ~pair:"b" "a" |
| Encode pairs batch | tokenizer.encode_batch([("a","b"), ...]) |
Brot.encode_batch_pairs tokenizer [("a","b"); ...] |
| Decode | tokenizer.decode(ids) |
Brot.decode tokenizer ids |
| Decode batch | tokenizer.decode_batch([ids1, ids2]) |
List.map (Brot.decode tokenizer) [ids1; ids2] |
| Decode a stream | DecodeStream().step(tokenizer, id) |
Brot.decode_step (Brot.decode_stream tokenizer) id |
| Get token IDs | output.ids |
Encoding.ids enc |
| Get tokens | output.tokens |
Encoding.tokens enc |
| Get attention mask | output.attention_mask |
Encoding.attention_mask enc |
| Get type IDs | output.type_ids |
Encoding.type_ids enc |
| Get offsets | output.offsets |
Encoding.offsets enc |
| Padding | tokenizer.enable_padding(length=128) |
Brot.encode tokenizer ~padding:(Brot.padding (Fixed 128)) ...` |
| Truncation | tokenizer.enable_truncation(max_length=512) |
Brot.encode tokenizer ~truncation:(Brot.truncation 512) ... |
| Vocab size | tokenizer.get_vocab_size() |
Brot.vocab_size tokenizer |
| Token to ID | tokenizer.token_to_id("[CLS]") |
Brot.token_to_id tokenizer "[CLS]" |
| ID to token | tokenizer.id_to_token(101) |
Brot.id_to_token tokenizer 101 |
| Train BPE | tokenizer.train(files, BpeTrainer(...)) |
Brot.train_bpe (Files files) ~vocab_size:30000` |
| Train WordPiece | tokenizer.train(files, WordPieceTrainer(...)) |
Brot.train_wordpiece (Files files) ~vocab_size:30000` |
| Train Unigram | tokenizer.train(files, UnigramTrainer(...)) |
Brot.train_unigram (Files files) ~vocab_size:8000` |
| Train from iterator | tokenizer.train_from_iterator(iter, trainer) |
Brot.train_bpe (Seq seq) ~vocab_size:1000` |
| Set normalizer | tokenizer.normalizer = normalizers.Lowercase() |
Brot.bpe ~normalizer:Normalizer.lowercase () |
| Set pre-tokenizer | tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel() |
Brot.bpe ~pre:(Pre_tokenizer.byte_level ()) () |
| Set post-processor | tokenizer.post_processor = processors.BertProcessing(...) |
Brot.bpe ~post:(Post_processor.bert ~sep ~cls) () |
| Set decoder | tokenizer.decoder = decoders.WordPiece() |
Brot.bpe ~decoder:(Decoder.wordpiece ()) () |
| Add special tokens | tokenizer.add_special_tokens([AddedToken(...)]) |
Pass ~added_tokens:[Brot.added_token "..."; ...] at construction, or Brot.add_tokens after it (any model) |
this page is tested with mdxedit this page ↗