Package for efficient inference of Transformer models with D2DMoE
Project description
d2dmoe
d2dmoe is a Python package implementing Dense To Dynamic-k Mixture-of-Experts (D2DMoE), a simple and practical method that reduces the computational costs of Transformer models during the inference by converting standard pretrained model checkpoint into its MoE version.
D2DMoE is based on the MoEfication[1], also implemented in d2dmoe, but brings several improvements to this method.
d2dmoe is built on top of PyTorch and benefits from the user-friendly Hugging Face Transformers API.
Installation
d2dmoe can be installed via PyPI by simply executing the following statement:
pip install d2dmoe
If you'd like to use provided training and inference scripts or play with the examples, you must install the library from source. Clone this repository and install d2dmoe with the following commands:
git clone https://github.com/mpiorczynski/d2dmoe.git
cd d2dmoe
pip install -e .
Usage
To perform sparsity enforcement you need to execute:
# create D2DMoE for the task of sequence classification from the BERT-base checkpoint
from transformers import BertForSequenceClassification
from d2dmoe.train import SparsityEnforecementTrainer
bert = BertForSequenceClassification.from_pretrained(
model_name_or_path="mpiorczynski/relu-bert-base-uncased",
...
)
sparsity_trainer = SparsityEnforecementTrainer(
model=bert,
args=training_args,
output_dir='checkpoints/sparsified_bert_base',
sparsity_enforcement_weight=1e-3,
...
)
sparsity_trainer.train()
sparsity_trainer.save_model()
To convert sparsified model into its MoE version you need to execute:
from d2dmoe.models import MoEBertConfig, D2DMoEBertForSequenceClassification
from d2dmoe.train import D2DMoERoutersTrainer
config = MoEBertConfig.from_pretrained(
'checkpoints/sparsified_bert_base',
num_experts=128,
expert_split=True,
...
)
d2dmoe = D2DMoEBertForSequenceClassification.from_pretrained(
model_name_or_path='checkpoints/sparsified_bert_base',
config=config,
...
)
routers_trainer = D2DMoERoutersTrainer(
model=d2dmoe,
output_dir='checkpoints/d2dmoe_bert_base',
num_train_epochs=10,
learning_rate=0.001,
per_device_train_batch_size=64,
tau_to_eval=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 1.0],
...
)
routers_trainer.train()
routers_trainer.save_model()
For complete examples of usage see examples directory.
Supported models
As D2DMoE relies on the activation sparsity phenomenon exhibited in ReLU-based Transformer models, we have provided pretrained models with the ReLU activation function. We derived checkpoints listed below by replacing the GELU with ReLU activation function and continuing pretraining through several iterations to adapt them to the change of the activation function.
| Model | URL |
|---|---|
| ViT-B | mpiorczynski/relu-vit-base-patch16-224 |
| BERT-base | mpiorczynski/relu-bert-base-uncased |
Dev
You need to have Python 3.10 and pip. To install dependencies you need to execute:
make install-dependencies
To format and check code with linter run:
make prepare-code
Run tests using following:
make test
To build the package and upload it to the Python Package Index (PyPI) run:
make build-release:
References
[1] Zhang, Zhengyan, et al. "Moefication: Transformer feed-forward layers are mixtures of experts." arXiv preprint arXiv:2110.01786 (2021).
[2] Piórczyński, Mikołaj, et al. "Exploiting Transformer Activation Sparsity with Dynamic Inference." arXiv preprint arXiv:2310.04361 (2023).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file d2dmoe-0.0.1.tar.gz.
File metadata
- Download URL: d2dmoe-0.0.1.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6feda6a165fe16b9755448f98b0da1ee54a63268c81a2416a3c5e292e2b04442
|
|
| MD5 |
e97c51c9c45f35aa2497775884f7556d
|
|
| BLAKE2b-256 |
b874bda68f6e48b6671a0d56e68226b2a96e4393e39c1f6ba38a8a2daa43e501
|
File details
Details for the file d2dmoe-0.0.1-py3-none-any.whl.
File metadata
- Download URL: d2dmoe-0.0.1-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2107d1db10a904ddfb553e26e0350fd6e88654f49c6ee5a1f2dd9de3b846062d
|
|
| MD5 |
4b2f7cb8a26ce6213de113dc1df92cf0
|
|
| BLAKE2b-256 |
a8de47fd622cf99d77ca7ae3fc851b876a5aa3cd0dea2f0f5c00dca94e4ffa65
|