Skip to main content

A package to reduce the size of Hugging Face models via Vocabulary Slimming

Project description

VocabSlim

Python HuggingFace

A package to reduce the size of 🤗 Hugging Face models via Vocabulary Slimming.

Large language models (LLMs) increasingly adopt extensive vocabularies to better handle diverse tasks such as multilingual processing, code generation, and function-calling. Recent research highlights that expanding both model and vocabulary size enhances performance, following scaling laws with vocabulary.

However, larger vocabularies come with significant drawbacks. For smaller LLMs designed for specific tasks like English or Chinese chat applications on mobile devices, a large vocabulary can be inefficient. It consumes computational resources as the model must predict token probabilities at each decoding step. Additionally, token embeddings occupy memory and lead to larger activations during inference and fine-tuning, exacerbating the issue for smaller models(e.g., 26.53% for Qwen2.5-0.5B).

For instance, the Qwen2.5 series illustrates this challenge:

Model Qwen2.5-0.5B Qwen2.5-1.5B Qwen2.5-3B Qwen2.5-7B Qwen2.5-14B Qwen2.5-32B Qwen2.5-72B
Number of Parameters 0.49B 1.54B 3.09B 7.61B 14.7B 32.5B 72.7B
Number of Parameters (Non-Embedding) 0.36B 1.31B 2.77B 6.53B 13.1B 31.0B 70.0B
Ratio (Embeding Parameters) 26.53% 14.93% 10.36% 14.19% 10.88% 4.61% 3.71%

Reducing the vocabulary can significantly shrink the model size and enhance memory efficiency for both inference and fine-tuning. This benefit is particularly pronounced when models are quantized, as most quantization methods avoid quantizing token embeddings and the language modeling head to preserve accuracy.

Installation

From PyPI (recommended)

pip install vocab-slim

From source

git clone https://github.com/Andy1314Chen/vocab-slim.git
cd vocab-slim
pip install -r requirements.txt
pip install -e .

Usage

Command Line Interface

vocab-slim --model_name_or_path Qwen/Qwen2.5-0.5B --vocab_size 32000

Python API

from vocabslim import VocabSlim

# load pretrained tokenizer and model, train new BPE tokenizer
vocSlim = VocabSlim(model_name_or_path="Qwen/Qwen2.5-0.5B",
                    save_path=f"Qwen2.5-0.5B-Vocab-Slimed-32K",
                    dataset_config={"name": "wikitext",
                                "config": "wikitext-103-raw-v1",
                                "split": "train",
                                "text_column": "text"},
                    target_vocab_size=32_000)

# vocabulary slimming and adjust embedding weight
vocSlim.prune()

# compare outputs between original and slimmed models with test text
vocSlim.check("What is the capital of France?")

# slimmed model and tokenizer are saved in save_path

Evaluation

from transformers import AutoModelForCausalLM, AutoTokenizer
from lm_eval.models.huggingface import HFLM
from lm_eval import simple_evaluate

model_path = "./Qwen2.5-0.5B-Vocab-Slimed-32K"
model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto").eval()
tokenizer = AutoTokenizer.from_pretrained(model_path)

lm = HFLM(pretrained=model, tokenizer=tokenizer, batch_size=32)

results = simple_evaluate(model=lm, tasks=["piqa", "hellaswag", "arc_challenge"])
results = {key: value for key, value in results.items() if key == "results"}
print("Accuracy:", results)

Model Size Reduction

Model Vocabulary Size #params Model Size Average Accuracy Memory Useage
Qwen2.5-0.5B 0.49B 0.36B 26.53% 0.49B 0.49B
Qwen2.5-0.5B 0.49B 0.36B 26.53% 0.49B 0.49B
Qwen2.5-0.5B 0.49B 0.36B 26.53% 0.49B 0.49B
Qwen2.5-0.5B 0.49B 0.36B 26.53% 0.49B 0.49B

Limitations

  • Only Hugging Face models with BPE tokenizer are supported.
  • May cause performance degradation due to reduced vocabulary size.
  • Currently only supports causal language models

References

Papers

  1. Efficient Vocabulary Reduction for Small Language Models

    • Authors: Yuta Nozaki, Dai Nakashima, Ryo Sato, Naoki Asaba, Shintaro Kawamura
    • Published in: Proceedings of the 31st International Conference on Computational Linguistics: Industry Track 2025
    • [Paper]
  2. Scaling Laws with Vocabulary: Larger Models Deserve Larger Vocabularies

    • Authors: Chaofan Tao, Qian Liu, Longxu Dou, Niklas Muennighoff, Zhongwei Wan, Ping Luo, Min Lin, Ngai Wong
    • Published in: NeurIPS 2024
    • [Paper]
  3. Efficient Multilingual Language Model Compression through Vocabulary Trimming

    • Authors: Asahi Ushio, Yi Zhou, Jose Camacho-Collados
    • Published in: EMNLP 2023
    • [Paper]

Related Projects

  1. Introducing Minivoc: Faster and Memory-Efficient LLMs Through Vocabulary Reduction [WIP]

Citation

If you use this software, please cite it as given below;

@software{vocab-slim,
author = {Andy1314Chen},
license = {Apache-2.0},
title = {{vocab-slim: A Tool for Hugging Face Model Vocabulary Slimming}}
url = {https://github.com/Andy1314Chen/vocab-slim}
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Project details


Download files

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

Source Distribution

vocabslim-0.1.3.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

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

vocabslim-0.1.3-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file vocabslim-0.1.3.tar.gz.

File metadata

  • Download URL: vocabslim-0.1.3.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.8

File hashes

Hashes for vocabslim-0.1.3.tar.gz
Algorithm Hash digest
SHA256 d31986e154ecb6e48308d80728c36eea1643c13a44380f8e3ee7bb0d5eb34db5
MD5 5d3eec20c7654e155193a279710dc51b
BLAKE2b-256 eda52eb805c68321ae60cccb9901dd47cee85e95264671770354d7d6e374aae1

See more details on using hashes here.

File details

Details for the file vocabslim-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: vocabslim-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.8

File hashes

Hashes for vocabslim-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 61a2112888bbc7049e7654d6888dd67557c2eaab42e18a9a283e020438d3a38d
MD5 8789f27961547a8ef2fd5126c6b7a358
BLAKE2b-256 dd2dd13c4d71b008a61188d7ec373e71403cf58413160a1b85f965ee2a40ce78

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page