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 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

# 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, and registering your own preset.

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.
  • 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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trimbed-0.0.3.tar.gz (225.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

trimbed-0.0.3-py3-none-any.whl (74.1 kB view details)

Uploaded Python 3

File details

Details for the file trimbed-0.0.3.tar.gz.

File metadata

  • Download URL: trimbed-0.0.3.tar.gz
  • Upload date:
  • Size: 225.2 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for trimbed-0.0.3.tar.gz
Algorithm Hash digest
SHA256 cf820cf55d2d6ed02a818eedbcc6f86a1c564f0235fa7cfbb28b7d68d53a485b
MD5 8bd743e054557f4525d0010d707233e5
BLAKE2b-256 a26826ebd7961e70d53f56df6c81c395b898e8eba0df2f63b25e15f9fc1723f5

See more details on using hashes here.

File details

Details for the file trimbed-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: trimbed-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 74.1 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for trimbed-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8d644f699ee05221aa677b390c67e4d54f1e218550858e1845bdccaf0caf1fb4
MD5 63ad5cbf73ef66580595b964cadec63d
BLAKE2b-256 1831c4368718cd03c6403b5a917eb36c3c8cd6179fff397d10983308d4ac7e86

See more details on using hashes here.

Release history Release notifications | RSS feed

0.0.4

2 files

This release

0.0.3 This release

2 files

0.0.2

2 files

0.0.1

2 files

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