Package: transformer-smaller-training-vocab
transformer-smaller-training-vocabDocsare availablehereMotivationHave 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 likexlm-roberta-largehave 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 togather usage statistics of the vocabularytemporary reduce the vocabulary to include no tokens that won't be used during trainingfit in the tokens back in after the training is finished, so the full version can be saved.LimitationsThis library works fine, if you use anyFastTokenizerHowever if you want to use aslowtokenizer, 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:XLMRobertaTokenizerRobertaTokenizerBertTokenizerIf you want to use a tokenizer that is not on the list, pleasecreate an issuefor it.Quick StartRequirements and InstallationThe project is based on Transformers 4.1.0+, PyTorch 1.8+ and Python 3.8+
Then, in your favorite virtual environment, simply run:pip install transformer-smaller-training-vocabExample UsageTo 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 resultraw_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.ImpactHere is a table to document how much impact this technique has on training:ModelDatasetVocab reductionModel size reductionxlm-roberta-largeCONLL 03 (en)93.13%42.58%xlm-roberta-baseCONLL 03 (en)93.13%64.31%bert-base-casedCONLL 03 (en)43.64%08.97%bert-base-uncasedCONLL 03 (en)47.62%10.19%bert-large-uncasedCONLL 03 (en)47.62%04.44%roberta-baseCONLL 03 (en)58.39%18.08%roberta-largeCONLL 03 (en)58.39%08.45%bert-base-casedcola77.67%15.97%roberta-basecola86.08%26.66%xlm-roberta-basecola97.79%67.52%bert-base-casedmnli10.94%2.25%roberta-basemnli14.78%4.58%xlm-roberta-basemnli88.83%61.34%bert-base-casedmrpc49.93%10.27%roberta-basemrpc64.02%19.83%xlm-roberta-basemrpc94.88%65.52%bert-base-casedqnli8.62%1.77%roberta-baseqnli17.64%5.46%xlm-roberta-baseqnli87.57%60.47%bert-base-casedqqp7.69%1.58%roberta-baseqqp5.91%1.83%xlm-roberta-baseqqp85.40%58.98%bert-base-casedrte34.68%7.13%roberta-baserte50.49%15.64%xlm-roberta-baserte93.10%64.29%bert-base-casedsst262.39%12.83%roberta-basesst268.60%21.25%xlm-roberta-basesst296.25%66.47%bert-base-casedstsb51.35%10.56%roberta-basestsb64.37%19.93%xlm-roberta-basestsb94.88%65.52%bert-base-casedwnli93.66%19.26%roberta-basewnli96.03%29.74%xlm-roberta-basewnli99.25%68.54%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.