Temporary remove unused tokens during training to save ram and speed.
Project description
transformer-smaller-training-vocab
Motivation
Have you ever trained a transformer model and noticed that most tokens in the vocab are not used? Logically the token embeddings from those terms won't change, however they still take up memory and compute resources on your GPU. One could assume that the embeddings are just a small part of the model and therefore aren't relevant, however looking at models like xlm-roberta-large have 45.72% of parameters as "word_embeddings". Besides that, the gradient computation is done for the whole embedding weight, leading to gradient updates with very large amounts of 0s, eating a lot of memory, especially with state optimizers such as adam.
To reduce these inconveniences, this package provides a simple and easy to use way to
- gather usage statistics of the vocabulary
- temporary reduce the vocabulary to include no tokens that won't be used during training
- fit in the tokens back in after the training is finished, so the full version can be saved.
Limitations
This library works fine, if you use any FastTokenizer
However if you want to use a slow
tokenizer, it get's more tricky as huggingface-transformers has - per current date - no interface for overwriting the vocabulary in transformers.
So they require a custom implementation, currently the following tokenizers are supported:
- XLMRobertaTokenizer
- RobertaTokenizer
- BertTokenizer
If you want to use a tokenizer that is not on the list, please create an issue for it.
Quick Start
Requirements and Installation
The project is based on Transformers 4.1.0+, PyTorch 1.8+ and Python 3.7+ Then, in your favorite virtual environment, simply run:
pip install transformer-smaller-training-vocab
Example Usage
To use more efficient training, it is enough to do the following changes to an abitary training script:
model = ...
tokenizer = ...
raw_datasets = ...
...
+ with reduce_train_vocab(model=model, tokenizer=tokenizer, texts=get_texts_from_dataset(raw_datasets, key="text")):
def preprocess_function(examples):
result = tokenizer(examples["text"], padding=padding, max_length=max_seq_length, truncation=True)
result["label"] = [(label_to_id[l] if l != -1 else -1) for l in examples["label"]]
return result
raw_datasets = raw_datasets.map(
preprocess_function,
batched=True,
)
trainer = Trainer(
model=model,
train_dataset=raw_datasets["train"],
eval_dataset=raw_datasets["validation"],
tokenizer=tokenizer,
...
)
trainer.train()
+ trainer.save_model() # save model at the end to contain the full vocab again.
Done! The Model will now be trained with only use the necessary parts of the token embeddings.
Impact
Here is a table to document how much impact this technique has on training:
Model | Dataset | Vocab reduction | Model size reduction |
---|---|---|---|
xlm-roberta-large | CONLL 03 (en) | 93.13% | 42.58% |
xlm-roberta-base | CONLL 03 (en) | 93.13% | 64.31% |
bert-base-cased | CONLL 03 (en) | 43.64% | 08.97% |
bert-base-uncased | CONLL 03 (en) | 47.62% | 10.19% |
bert-large-uncased | CONLL 03 (en) | 47.62% | 04.44% |
roberta-base | CONLL 03 (en) | 58.39% | 18.08% |
roberta-large | CONLL 03 (en) | 58.39% | 08.45% |
Notice that while those reduced embeddings imply slightly less computation effort, those gains are neglectable, as the gradient computation for the parameters of transformer layers are dominant.
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
Hashes for transformer_smaller_training_vocab-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a87a2b3a0f345210f8d2a20dfb15d624f43d93e5cf35425faf99e0e6bdd5a709 |
|
MD5 | f32f98d837d7bcaf4d1a7dbfe65dcb7b |
|
BLAKE2b-256 | 2921d992cadb9a1731c91d26dcb4839821c15570cfa37699764a9229838e9ff1 |
Hashes for transformer_smaller_training_vocab-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f05a9d02b5c3ef39fd2881d0b604f8d0228652b67e06c46aac3968f35d60da2 |
|
MD5 | 44169fe467979994e0d798ac4e9d87f2 |
|
BLAKE2b-256 | 6d84446df4f3bce5aa4e950237eee0ea051c5d8625658d3a03af043dccb82724 |