Skip to main content

🛏️ trimbed

CI codecov Documentation PyPI version Python versions License

Trim a tokenizer's vocabulary, and optionally its model's embedding table, down to the subset you actually need. You can specify "what you need" explicitly, by presets, or by providing a corpus (one or more datasets) as an anchor for what you'd actually like to keep.

The most typical use would be to provide a corpus of the domain and/or language that is relevant for you, and then specifying to, for example, keep only the top 32k tokens from that corpus, or to remove the tail of the corpus by only keeping 99.95% of its tokens. But more options are available!

Read the documentation for the full guides and the API reference.

Installation

pip install trimbed                       # tokenizer trimming only
pip install "trimbed[model]"              # + torch, for trimming embeddings
pip install "trimbed[model,convert]"      # + sentencepiece/protobuf, for spiece-only tokenizers

Or preferably with uv add.

From a git clone the above would be uv sync, uv sync --extra model and uv sync --all-extras.

Quickstart

Installing the package comes with the trimbed command, which is the main entrypoint for users. It has a number of subcommands: trim, count, inspect, compare and presets. trimbed --help lists them.

If you have only cloned the repository, python -m trimbed.cli works too.

# What am I dealing with?
trimbed inspect --model codefuse-ai/F2LLM-v2-160M

# Trim tokenizer + model from a config file
trimbed trim --config my_config.yaml

# Same config but overridden one value and nothing written (dry-run)
trimbed trim --config my_config.yaml --dry-run selection.top_k=30000

# What did somebody else's trim actually leave behind?
trimbed compare intfloat/multilingual-e5-small clips/e5-small-trm-nl

# Or without a config, keeping a preset
# (only all alphanumeric tokens in the vocab are kept),
# and the model is not trimmed
trimbed trim --model google-bert/bert-base-multilingual-cased \
    --keep-preset alphanumeric --output-dir trimmed/bert --no-trim-model

From Python:

from trimbed import TrimConfig, TrimPipeline

config = TrimConfig.from_yaml("my_config.yaml")
report = TrimPipeline(config).run()
print(report.render())

examples/ has a few Python examples: inspecting a tokenizer, trimming from rules alone, trimming over a corpus with the model, registering your own preset, and diffing a checkpoint against a trimmed version of it.

What it does

  • One code path for every family. trimbed operates on the tokenizers backend document (tokenizer.json) rather than on SentencePiece protobufs, so BPE, WordPiece, Unigram and WordLevel are all covered. The vocabulary surgery itself is delegated to skeletoken.
  • Selection with provenance. Structural tokens, must-keep rules and the corpus frequency ranking are unioned, closed over BPE merge dependencies, then capped. Every kept token records why it survived.
  • Prompts and chat templates stay intact. keep_texts and keep_chat_template keep the ids a prompt is made of, so it goes on tokenizing exactly as it does now.
  • The model follows. Embedding table, output head, head bias, tied and untied, encoder-decoders, pad_to_multiple_of alignment rows, and the token ids stored on the config and generation config.
  • It proves its work. Both tokenizers re-encode sampled texts, and both models can be run and compared, before anything is called a success.
  • Somebody else's trim can be audited too. trimbed compare diffs two tokenizers: whether the smaller one really is the larger one renumbered in place, what survived of the structural tokens, the presets and each Unicode script, and what the same text now costs in tokens.
  • Nothing has to come from the Hub. The checkpoint can be a local directory, and the corpus a directory of JSON Lines, a json/csv/parquet loader pointed at your own files, or a dataset written with save_to_disk.

Documentation

Command line The four subcommands and their flags
Configuration Every config field, and how the override layers stack
How selection works What survives a trim and why
Trimming a model Embedding and head surgery, and verification
Output and reports What lands in the output directory
Extending trimbed A new tokenizer family or a new preset
API reference Every public symbol

Contributing

See CONTRIBUTING.md. In short: make style, make test and make build-docs before you push (or just register the pre-commit hooks).

Acknowledegments

We rely heavily on the internals of skeletoken to map out tokenizers to a standardized Pydantic format.

Metadata

Release files for trimbed 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for trimbed 0.0.4
File Size Uploaded Provenance Actions
trimbed-0.0.4.tar.gz 242.3 kB Without provenance Details for trimbed-0.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for trimbed 0.0.4
File Interpreter ABI Platform Actions
trimbed-0.0.4-py3-none-any.whl Python 3 none any Details for trimbed-0.0.4-py3-none-any.whl

Total release size: 328.4 kB

Release files / trimbed-0.0.4.tar.gz

Download URL trimbed-0.0.4.tar.gz
Size 242.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e0eca6248653fa525778f7f809c2caa9e87f4d3488a7a8f66686c6c51543e158
BLAKE2b-256 checksum
How to use checksums
33c60096d96ce50594060ef68fe09f4c718cf0cffa3ed469b034e0a6ad28f66c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / trimbed-0.0.4-py3-none-any.whl

Download URL trimbed-0.0.4-py3-none-any.whl
Size 86.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
458eb4e137dccb94ebc3a14d9a53942f3c699273ace11eb76dd5f815dccf5eed
BLAKE2b-256 checksum
How to use checksums
889503df0f4b24a762deb49443ce42aae63c10325a38669e1203db61971339b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for trimbed 0.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page