SkillAgentSearch skills...

DeBERTa

The implementation of DeBERTa

Install / Use

/learn @microsoft/DeBERTa

README

DeBERTa: Decoding-enhanced BERT with Disentangled Attention

This repository is the official implementation of DeBERTa: Decoding-enhanced BERT with Disentangled Attention and DeBERTa V3: Improving DeBERTa using ELECTRA-Style Pre-Training with Gradient-Disentangled Embedding Sharing

News

03/18/2023

  • DeBERTaV3 paper is accepted by ICLR 2023.
  • The code for DeBERTaV3 pre-training and continous training is added. Please check Language Model for details.

12/8/2021

  • DeBERTa-V3-XSmall is added. With only 22M backbone parameters which is only 1/4 of RoBERTa-Base and XLNet-Base, DeBERTa-V3-XSmall significantly outperforms the later on MNLI and SQuAD v2.0 tasks (i.e. 1.2% on MNLI-m, 1.5% EM score on SQuAD v2.0). This further demonstrates the efficiency of DeBERTaV3 models.

11/16/2021

3/31/2021

  • Masked language model task is added
  • SuperGLUE tasks is added
  • SiFT code is added

2/03/2021

DeBERTa v2 code and the 900M, 1.5B model are here now. This includes the 1.5B model used for our SuperGLUE single-model submission and achieving 89.9, versus human baseline 89.8. You can find more details about this submission in our blog

What's new in v2

  • Vocabulary In v2 we use a new vocabulary of size 128K built from the training data. Instead of GPT2 tokenizer, we use sentencepiece tokenizer.
  • nGiE(nGram Induced Input Encoding) In v2 we use an additional convolution layer aside with the first transformer layer to better learn the local dependency of input tokens. We will add more ablation studies on this feature.
  • Sharing position projection matrix with content projection matrix in attention layer Based on our previous experiment, we found this can save parameters without affecting performance.
  • Apply bucket to encode relative positions In v2 we use log bucket to encode relative positions similar to T5.
  • 900M model & 1.5B model In v2 we scale our model size to 900M and 1.5B which significantly improves the performance of downstream tasks.

12/29/2020

With DeBERTa 1.5B model, we surpass T5 11B model and human performance on SuperGLUE leaderboard. Code and model will be released soon. Please check out our paper for more details.

06/13/2020

We released the pre-trained models, source code, and fine-tuning scripts to reproduce some of the experimental results in the paper. You can follow similar scripts to apply DeBERTa to your own experiments or applications. Pre-training scripts will be released in the next step.

Introduction to DeBERTa

DeBERTa (Decoding-enhanced BERT with disentangled attention) improves the BERT and RoBERTa models using two novel techniques. The first is the disentangled attention mechanism, where each word is represented using two vectors that encode its content and position, respectively, and the attention weights among words are computed using disentangled matrices on their contents and relative positions. Second, an enhanced mask decoder is used to replace the output softmax layer to predict the masked tokens for model pretraining. We show that these two techniques significantly improve the efficiency of model pre-training and performance of downstream tasks.

Pre-trained Models

Our pre-trained models are packaged into zipped files. You can download them from our releases, or download an individual model via the links below:

|Model | Vocabulary(K)|Backbone Parameters(M)| Hidden Size | Layers| Note| |-------------|------|---------|-----|-----|---------| |V2-XXLarge<sup>1</sup>|128|1320|1536| 48|128K new SPM vocab | |V2-XLarge|128|710|1536| 24| 128K new SPM vocab| |XLarge|50|700|1024|48| Same vocab as RoBERTa| |Large|50|350|1024|24|Same vocab as RoBERTa| |Base|50|100|768|12|Same vocab as RoBERTa| |V2-XXLarge-MNLI|128|1320|1536| 48|Fine-turned with MNLI | |V2-XLarge-MNLI|128|710|1536| 24|Fine-turned with MNLI | |XLarge-MNLI|50|700|1024|48|Fine-turned with MNLI| |Large-MNLI|50|350|1024|24|Fine-turned with MNLI| |Base-MNLI|50|86|768|12|Fine-turned with MNLI| |DeBERTa-V3-Large<sup>2</sup>|128|304|1024| 24| 128K new SPM vocab| |DeBERTa-V3-Base<sup>2</sup>|128|86|768| 12| 128K new SPM vocab| |DeBERTa-V3-Small<sup>2</sup>|128|44|768| 6| 128K new SPM vocab| |DeBERTa-V3-XSmall<sup>2</sup>|128|22|384| 12| 128K new SPM vocab| |mDeBERTa-V3-Base<sup>2</sup>|250|86|768| 12| 250K new SPM vocab, multi-lingual model with 102 languages|

Note

  • 1 This is the model(89.9) that surpassed T5 11B(89.3) and human performance(89.8) on SuperGLUE for the first time. 128K new SPM vocab.
  • 2 These V3 DeBERTa models are deberta models pre-trained with ELECTRA-style objective plus gradient-disentangled embedding sharing which significantly improves the model efficiency.

Try the model

Read our documentation

Requirements

  • Linux system, e.g. Ubuntu 18.04LTS
  • CUDA 10.0
  • pytorch 1.3.0
  • python 3.6
  • bash shell 4.0
  • curl
  • docker (optional)
  • nvidia-docker2 (optional)

There are several ways to try our code,

Use docker

Docker is the recommended way to run the code as we already built every dependency into our docker bagai/deberta and you can follow the docker official site to install docker on your machine.

To run with docker, make sure your system fulfills the requirements in the above list. Here are the steps to try the GLUE experiments: Pull the code, run ./run_docker.sh , and then you can run the bash commands under /DeBERTa/experiments/glue/

Use pip

Pull the code and run pip3 install -r requirements.txt in the root directory of the code, then enter experiments/glue/ folder of the code and try the bash commands under that folder for glue experiments.

Install as a pip package

pip install deberta

Use DeBERTa in existing code


# To apply DeBERTa to your existing code, you need to make two changes to your code,
# 1. change your model to consume DeBERTa as the encoder
from DeBERTa import deberta
import torch
class MyModel(torch.nn.Module):
  def __init__(self):
    super().__init__()
    # Your existing model code
    self.deberta = deberta.DeBERTa(pre_trained='base') # Or 'large' 'base-mnli' 'large-mnli' 'xlarge' 'xlarge-mnli' 'xlarge-v2' 'xxlarge-v2'
    # Your existing model code
    # do inilization as before
    # 
    self.deberta.apply_state() # Apply the pre-trained model of DeBERTa at the end of the constructor
    #
  def forward(self, input_ids):
    # The inputs to DeBERTa forward are
    # `input_ids`: a torch.LongTensor of shape [batch_size, sequence_length] with the word token indices in the vocabulary
    # `token_type_ids`: an optional torch.LongTensor of shape [batch_size, sequence_length] with the token types indices selected in [0, 1]. 
    #    Type 0 corresponds to a `sentence A` and type 1 corresponds to a `sentence B` token (see BERT paper for more details).
    # `attention_mask`: an optional parameter for input mask or attention mask. 
    #   - If it's an input mask, then it will be torch.LongTensor of shape [batch_size, sequence_length] with indices selected in [0, 1]. 
    #      It's a mask to be used if the input sequence length is smaller than the max input sequence length in the current batch. 
    #      It's the mask that we typically use for attention when a batch has varying length sentences.
    #   - If it's an attention mask then if will be torch.LongTensor of shape [batch_size, sequence_length, sequence_length]. 
    #      In this case, it's a mask indicating which tokens in the sequence should be attended by other tokens in the sequence. 
    # `output_all_encoded_layers`: whether to output results of all encoder layers, default, True
    encoding = deberta.bert(input_ids)[-1]

# 2. Change your tokenizer with the tokenizer built-in DeBERta
from DeBERTa import deberta
vocab_path, vocab_type = deberta.load_vocab(pretrained_id='base')
tokenizer = deberta.tokenizers[vocab_type](vocab_path)
# We apply t
View on GitHub
GitHub Stars2.2k
CategoryEducation
Updated1d ago
Forks240

Languages

Python

Security Score

100/100

Audited on Mar 25, 2026

No findings