Load only a tokenizer¶
Tokenize-Only Handles¶
tokenizer() (also LLM.tokenizer()) loads only a tokenizer, with no model weights and no inference pipeline, for workloads such as static embeddings (model2vec/POTION) that tokenize, gather rows from a precomputed table and pool. Loading is async; encode() is synchronous and returns a plain list[int]:
from lmlite import tokenizer
tok = await tokenizer(model="minishlab/potion-multilingual-128M")
# or: tok = await tokenizer(tokenizer_json="files/models/<org>/<name>/tokenizer.json",
# tokenizer_config="files/models/<org>/<name>/tokenizer_config.json")
ids = tok.encode("sushi right next door to a pharmacy") # sync, no await
tok.encode("text", add_special_tokens=True) # default False, unlike Transformers.js
tok.decode(ids), tok.vocab_size
tok.close() # later calls raise; repeated close() is safe
Arguments: pass exactly one of
model=ortokenizer_json=;tokenizer_config=goes only withtokenizer_json=. Errors (bad arguments, missing files, unsupported tokenizer types) raise intokenizer(), not on the firstencode().model=fetchestokenizer.jsonand, when present,tokenizer_config.jsonfrom the hostedfiles/models/snapshot first, then from the Hub, likeLLM.create()(hosted_models_url=works the same way). It never makes the Hub task lookup, so hosted tokenizers work offline./drive/modelsZIPs are not read.tokenizer_json=/tokenizer_config=are HTTP(S) URLs. Relative paths resolve against the JupyterLite site root, sofiles/models/...is the hosted files directory. Paths in the kernel’s virtual filesystem are not supported.Without a config, ids come from
tokenizer.jsonalone, which holds the vocabulary, normalizer, pre-tokenizer and special-token template. The config only selects the Transformers.js tokenizer class (tokenizer_class) and named special tokens such aseos_token, which a few tokenizer classes use when encoding.Encoding never truncates. Non-
strinput raisesTypeError. The empty string gives what the Rusttokenizerslibrary gives for the same spec ([]for the Unigram/Metaspace test fixture).Lifetime: each call loads an independent handle; closing one does not affect others.
Cache and progress: tokenizer files go through the browser’s HTTP cache only. They do not appear in
cached_models(): a tokenizer-only Cache API entry would makeLLM.create()treat the whole model as already cached. There is noprogress_callback, because tokenizer files are small.Kernels: works on Pyodide and xeus-python. Ids cross the bridge as a JSON string, so both kernels return plain
ints. Both have opt-in browser tests. If a kernel cannot callencode()synchronously,tokenizer()raises at load time.Latency: about 0.02 ms (Pyodide) and 0.05 ms (xeus-python) per
encode()of a short sentence, warm, in headless Chromium on Apple Silicon with the 97-token test vocabulary. Measure with your own tokenizer before relying on these numbers.