Skip to content
class

WordPiece

extendsTokenizer
WordPiece

WordPiece tokenizer. Owns a string→id vocab + dense reverse table. Continuation pieces are stored with the ## prefix already baked into the vocab key (BERT convention) — encode/decode are responsible for adding/stripping the prefix at segment boundaries. unk_id_ is cached so the encode hot path never has to hash the UNK string. Matches the Python lucid.utils.tokenizer.WordPieceTokenizer.

Constructors

1
ctor

WordPiece

void WordPiece()

Default-construct an empty model; populate via train or by re-constructing with an explicit vocab from a loaded checkpoint.

Methods

11
fnconst

continuing_prefix

const int & continuing_prefix()
fnconst

decode

int decode(const int & ids)
fnconst

encode

int encode(const int & text)

── Tokenizer overrides ────────────────────────────────────────

fnconst

encode_word_

int encode_word_(const int & word)

Apply greedy longest-match to ONE pre-tokenized word. Returns the matched ids, or a single UNK id if the word cannot be tokenized at any position.

fnconst

id_to_token

int id_to_token(int id)
fn

rebuild_tables_

void rebuild_tables_()

Rebuild reverse table after vocab mutation.

fn

train

void train(const int & corpus, int target_vocab_size)

Train on a list of pre-tokenized words (the Python wrapper does the normalization + word-splitting via BertNormalizer + WhitespacePunctuationSplit first). Builds a vocab of size up to target_vocab_size by greedy frequency merging — close to HF's WordPieceTrainer behaviour without the full log-likelihood objective.

fnconst

unk_token

const int & unk_token()

── WordPiece-specific accessors ───────────────────────────────