transformer-smaller-training-vocab
Temporary remove unused tokens during training to save ram and speed.
What it is and what it does
This package addresses a common inefficiency in transformer training: most tokens in a model's vocabulary are never used on a given dataset, yet their embeddings still consume GPU memory and compute resources during gradient updates. The package provides a context manager that temporarily shrinks the vocabulary to only tokens present in your training data, then restores the full model afterward so you can save the complete version.
It works by gathering vocabulary usage statistics, reducing embeddings and their gradients to only active tokens, and fitting them back after training. The impact varies by model and dataset; some configurations show substantial vocabulary reductions with corresponding memory savings, though the gradient computation overhead from the full transformer layers typically dominates the overall training time. Support is best with HuggingFace FastTokenizers; slow tokenizers require custom implementations, with only XLMRobertaTokenizer, RobertaTokenizer, and BertTokenizer currently supported.
Use it for:
- Fine-tuning large multilingual models on domain-specific datasets where most of the pretrained vocabulary is unused.
- Reducing GPU memory during training of BERT or RoBERTa models on small, specialized text corpora with limited token diversity.
- Optimizing embedding layer memory in resource-constrained environments where vocabulary reduction yields measurable savings.
- Training on low-resource languages or specialized domains where the full pretrained vocabulary is largely redundant.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Reduces transformer model vocabulary to only tokens used during training, cutting embedding memory and compute overhead while preserving the full model after training completes.
Yes, if you are fine-tuning a transformer on a dataset with limited vocabulary diversity and have GPU memory constraints. The savings are real but dataset-dependent. Install friction is low and the codebase is stable. The aging maintenance status (last commit 2025-06-15) is a minor concern but not a blocker for a focused, mature utility.
Install
transformer-smaller-training-vocab on PyPI
pip
pip install transformer-smaller-training-vocabuv
uv add transformer-smaller-training-vocabpoetry
poetry add transformer-smaller-training-vocabInstalling transformer-smaller-training-vocab
Before you install
Low friction installation with a pure Python wheel. Maintenance is aging—last commit was 2025-06-15, over a year old—but the repository remains active and unarchived. Depends on transformers and torch, which are standard in the ML ecosystem.
License in practice
MIT license is permissive; you can use, modify, and distribute this package freely in commercial or private projects with minimal restrictions.
Quickstart
pip install transformer-smaller-training-vocab
from transformer_smaller_training_vocab import reduce_train_vocab
with reduce_train_vocab(model=model, tokenizer=tokenizer, texts=texts):
# training loop here
trainer.train()
trainer.save_model() # restores full vocab
Requires transformers 4.1.0+, PyTorch 1.8+, and Python 3.9+. Works best with FastTokenizer; slow tokenizers (XLMRobertaTokenizer, RobertaTokenizer, BertTokenizer) have limited support.
Verify before relying
- Whether custom slow tokenizers beyond the three listed can be added via user contribution or extension.
- Performance impact on gradient computation and training speed across different model architectures and dataset sizes.
- Compatibility with recent transformers versions beyond 4.1.0 and whether the aging codebase has been tested against current releases.
- Typical vocabulary reduction percentages and memory savings on representative datasets and model sizes.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (<4.0,>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — transformers, torch |
| Maintenance | aging — 425 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 228,760/month — #9,145 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: transformer_smaller_training_vocab-0.4.2-py3-none-any.whl
Tags
More Artificial Intelligence packages
LiteLLM provides a unified Python interface to…
permissive · top 100 on PyPI
huggingface-hubClient library and CLI tool for downloading,…
permissive · top 100 on PyPI
langchainLangChain provides a framework for building…
permissive · top 1,000 on PyPI
hf-xethf-xet provides chunk-based deduplication and…
permissive · top 1,000 on PyPI
tokenizersTokenizers converts raw text into token…
permissive · top 1,000 on PyPI
transformersTransformers provides a unified framework for…
permissive · top 1,000 on PyPI
model2vecModel2Vec converts sentence transformers into…
permissive · top 5,000 on PyPI
setfitSetFit fine-tunes Sentence Transformers for…
permissive · top 15,000 on PyPI
sentence-transformersComputes embeddings and reranking scores for…
permissive · top 1,000 on PyPI
peftPEFT implements parameter-efficient fine-tuning…
permissive · top 5,000 on PyPI
transformer-engineTransformer Engine accelerates Transformer…
unclear · top 15,000 on PyPI
trlTRL provides trainer classes for post-training…
permissive · top 5,000 on PyPI
loralibloralib provides PyTorch modules that implement…
permissive · top 15,000 on PyPI
transformer-engine-cu12Accelerates Transformer model training and…
unclear · top 15,000 on PyPI
megatron-coreMegatron Core provides GPU-optimized building…
permissive · top 15,000 on PyPI