Foundations to Frontier
Tests
E. Deep Learning › E12 Transformer Architectures

DeBERTa

DeBERTa (He, Liu, Gao and Chen, ICLR 2021) improves BERT with disentangled attention. Each token keeps separate content and relative-position vectors, and attention scores add content-to-content, content-to-position and position-to-content terms. Because relative attention alone cannot express absolute position, an enhanced mask decoder injects absolute positions just before the output. DeBERTa was the first single model reported above the human baseline on SuperGLUE, and DeBERTa-v3 added ELECTRA-style training. This page covers disentangled attention, relative buckets, the enhanced mask decoder and costs. In the exercises you implement disentangled scores and verify they depend only on relative distance. You then show the reason for the decoder: relative-only attention solves a relative task (1.000) but not an absolute one (0.388), and adding absolute positions near the output solves both (1.000 and 0.996).

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Keeping “what” and “where” separate

BERT adds a word’s meaning and its position into one vector. DeBERTa keeps them separate, a “what” vector and a “where relative to me” vector, and lets attention consider both: how relevant is this word, and how relevant is its position relative to me? Relative position cannot tell a word whether it is first or tenth, so DeBERTa adds absolute positions back just before making its predictions. The demo shows the classic positional patterns DeBERTa replaces with relative ones.

Transformer lab
Rows are positions 0–47, columns are dimensions; low dimensions oscillate fast, high ones slowly. The line shows each position's similarity to position 0, which falls off with distance.
Why engineers care

DeBERTa-style encoders are among the strongest for classification and extraction, so they are a common choice for high-accuracy text analysis in engineering domains.

📝 Quick check (Level 0)
0/3 answered
  1. 1. DeBERTa separates…
  2. 2. Its attention uses position that is…
  3. 3. Absolute positions are added…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Disentangled attention:
    • each token has a content vector Hᵢ, with Qc = HW_q and Kc = HW_k;
    • relative distances have embeddings P, with Qr = PW_qr and Kr = PW_kr;
    • Aᵢⱼ = Qcᵢ·Kcⱼ + Qcᵢ·Kr_δ(i,j) + Kcⱼ·Qr_δ(j,i), scaled by 1/√(3d) since three terms are summed (√192 = 13.9 for d = 64);
    • the position-to-position term is dropped, since it carries no content information.
  • Relative buckets:
    • δ(i, j) = clip(i − j, −k, k − 1) + k, giving 2k buckets;
    • with k = 3, query 10 sees keys 6, 8, 10, 12 and 14 in buckets 5, 5, 3, 1 and 0;
    • with k = 256 and L = 512, the first token uses 257 distinct buckets and a middle token 511.
  • Parameters: relative embeddings are 2k × d, 393,216 for k = 256 and d = 768, shared across layers.
  • Cost: three L²d-sized score terms instead of one, plus small relative projections.
  • Enhanced mask decoder: absolute position embeddings are added to the hidden states before the last layers that predict masked tokens. Relative scores alone are translation-invariant.
📝 Quick check (Level 1)
0/3 answered
  1. 1. DeBERTa scales scores by…
  2. 2. Relative buckets for window k:
  3. 3. Which term does DeBERTa drop?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

DeBERTa in code

Exercise 1.

  • Relative buckets came out as specified.
  • With identical content at every position, disentangled scores depended only on i − j.
  • With zero position embeddings, they reduced to scaled content attention.

Practice.

  • Hugging Face DebertaV2ForSequenceClassification with microsoft/deberta-v3-base is a strong default encoder for classification.
  • DeBERTa-v3 uses ELECTRA-style replaced-token detection (E12-8) with gradient-disentangled embedding sharing between generator and discriminator.
  • Relative attention handles sequences longer than training better than BERT’s learned absolute positions (E12-3), but memory still grows as L².
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tok = AutoTokenizer.from_pretrained("microsoft/deberta-v3-base")
model = AutoModelForSequenceClassification.from_pretrained("microsoft/deberta-v3-base", num_labels=4)
out = model(**tok(["actuator response sluggish at low temperature"], return_tensors="pt"))
🐍 Exercise 1: disentangled attention
Implement DeBERTa's relative buckets and disentangled attention scores; check they depend only on relative distance.
📝 Quick check (Level 2)
0/3 answered
  1. 1. DeBERTa-v3 pre-trains with…
  2. 2. Relative position embeddings in DeBERTa are…
  3. 3. DeBERTa's attention memory grows as…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why disentangle.

  • In BERT, Hᵢ = content + position, and the dot product (cᵢ + pᵢ)ᵀW(cⱼ + pⱼ) mixes content and position terms through one matrix.
  • DeBERTa gives each interaction its own projections, so the model can learn, for example, that adjectives attend to the noun to their right independently of which words they are.
  • Using relative rather than absolute positions in these terms gives translation-invariant attention, which generalises better.

Why relative attention needs absolute help.

  • A model whose only position signal is relative cannot tell where in the sequence a token is, except from boundary effects.
  • Some predictions need absolute position. In the paper’s example “a new store opened beside the new mall”, “store” and “mall” have similar local contexts but different syntactic roles.
  • DeBERTa adds absolute positions late, in the enhanced mask decoder, so attention stays relative while outputs can use absolute position.
  • Exercise 2 isolates this effect.
📝 Quick check (Level 3)
0/3 answered
  1. 1. BERT combines content and position by…
  2. 2. Purely relative attention cannot know…
  3. 3. The 'new store / new mall' example motivates…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Why add absolute positions back?

History.

  • P. He, X. Liu, J. Gao and W. Chen, “DeBERTa: decoding-enhanced BERT with disentangled attention” (ICLR 2021).
  • DeBERTa-1.5B scored 90.3 on SuperGLUE, above the 89.8 human baseline.
  • DeBERTa-v3 (2023) combined it with ELECTRA-style pre-training.

Exercise 2.

  • Model: one bidirectional layer (4 heads, d = 32), trained for 300 steps. Its positional information is:
    • none;
    • RoPE only (relative, inside attention);
    • RoPE plus learned absolute positions added just before the output (an enhanced-mask-decoder-style head).
  • Tasks:
    • absolute: output i mod 4 at position i;
    • relative: output the token two steps back.
positions absolute task relative task
none 0.251 0.272
relative only (RoPE) 0.388 1.000
relative + absolute at the output 1.000 0.996
  • Relative attention alone solves the relative task but not the absolute one. Its 0.388 against chance at 0.25 comes only from boundary effects near the sequence ends.
  • Adding absolute positions just before the output fixes the absolute task without hurting the relative one. This is the division of labour DeBERTa uses: relative position in attention, absolute position near the output.
  • With no position information at all, neither task is learned.
🐍 Exercise 2 (research): why add absolute positions back?
Train one layer with no positions, relative positions only, or relative plus absolute-at-output, on an absolute and a relative task.

DeBERTa in AI, robotics and aerospace

  • High-accuracy text analysis: DeBERTa-v3 encoders are strong baselines for classifying and extracting from technical documents and reports.
  • Position design: combine relative attention with absolute signals where absolute position matters, such as fields in fixed-format records.
  • Cost: three attention terms raise compute; distilled or small variants suit edge deployment.

References

  1. P. He, X. Liu, J. Gao, W. Chen (2021). “DeBERTa: decoding-enhanced BERT with disentangled attention.” Proc. ICLR.
  2. P. He, J. Gao, W. Chen (2023). “DeBERTaV3: improving DeBERTa using ELECTRA-style pre-training with gradient-disentangled embedding sharing.” Proc. ICLR.