Registry /
ai-ml / transformer-smaller-training-vocab
Install & Compatibility
Where this runs
tested against v0.4.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
py 3.10
✕ build_error
✓ 85.15s
py 3.11
✕ build_error
✓ 82.58s
py 3.12
✕ build_error
✓ 72.88s
py 3.13
✕ build_error
✓ 65.93s
py 3.9
✕ build_error
✕ timeout
4992MB installed
● package 4992MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
reduce_train_vocab_and_context
✓ from transformer_smaller_training_vocab import reduce_train_vocab_and_context
recreate_vocab
✓ from transformer_smaller_training_vocab import recreate_vocab
This quickstart demonstrates how to use `reduce_train_vocab_and_context` to create a model and tokenizer with a reduced vocabulary based on a provided dataset, and then `recreate_vocab` to restore the original vocabulary. This process is useful for memory-efficient training of transformer models.
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformer_smaller_training_vocab import reduce_train_vocab_and_context, recreate_vocab
# 1. Load a pre-trained model and tokenizer
model_name = "bert-base-uncased" # Using a small, common model for quickstart
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Dummy dataset for demonstration
texts = ["hello world", "this is a test", "another example sentence"]
tokenized_texts = [tokenizer(text, return_tensors="pt") for text in texts]
# 2. Reduce the vocabulary
reduced_tokenizer, reduced_model, added_tokens_during_reduction = reduce_train_vocab_and_context(
model=model,
tokenizer=tokenizer,
tokenized_datasets=[t["input_ids"] for t in tokenized_texts],
model_resize_strategy="embedding_resize",
)
print(f"Original vocab size: {tokenizer.vocab_size}")
print(f"Reduced vocab size: {reduced_tokenizer.vocab_size}")
# You would typically train with reduced_tokenizer and reduced_model here
# 3. Recreate the original vocabulary (after training, if needed)
recreated_model = recreate_vocab(
reduced_model=reduced_model,
reduced_tokenizer=reduced_tokenizer,
orig_tokenizer=tokenizer,
added_tokens_during_reduction=added_tokens_during_reduction,
model_resize_strategy="embedding_resize",
)
print(f"Recreated model vocab size: {recreated_model.config.vocab_size}")
# Verify recreated_model.config.vocab_size == tokenizer.vocab_size
Errors
Common errors & fixes
RuntimeError: The vocab_size of the new embedding is not correct. Please open an issue on github!
This error typically indicates an issue where the `recreate_vocab` function calculates an incorrect vocabulary size, leading to a mismatch when updating the model's embeddings.
fixThis was a known bug fixed in version 0.3.3. Upgrade your library to `transformer-smaller-training-vocab>=0.3.3`.
Package 'transformer-smaller-training-vocab' requires Python >=3.9, <4.0 but the running Python is 3.8.X
You are attempting to install or run a recent version of the library (>=0.4.1) on an unsupported Python 3.8 environment.
fixUpgrade your Python installation to version 3.9 or newer. For example, use a virtual environment with `python3.9 -m venv .venv`.
AttributeError: 'AddedToken' object has no attribute 'content'
This or similar `AttributeError` related to `AddedToken` objects often points to issues with how special tokens are handled by the tokenizer, particularly after modification or reduction.
fixThis class of issues was addressed in versions 0.2.3 and further refined in 0.4.1. Upgrade to `transformer-smaller-training-vocab>=0.4.1` to resolve.
Upgrade
Version history
0.4.2latest on PyPI · released Jun 15, 2025
Audit
Dependencies
transformersrequiredCore library for transformer models and tokenizers.
torchrequiredPyTorch backend for model operations.
numpyrequiredNumerical operations, a common dependency in ML libraries.
datasetsoptionalUsed for loading and processing datasets, made optional in 0.3.0.