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).
★☆☆☆☆ 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. DeBERTa separates…
2. Its attention uses position that is…
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.
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. DeBERTa scales scores by…
2. Relative buckets for window k:
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, AutoModelForSequenceClassificationtok = 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. DeBERTa-v3 pre-trains with…
2. Relative position embeddings in DeBERTa are…
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. BERT combines content and position by…
2. Purely relative attention cannot know…
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.
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
P. He, X. Liu, J. Gao, W. Chen (2021). “DeBERTa: decoding-enhanced BERT with disentangled attention.” Proc. ICLR.
P. He, J. Gao, W. Chen (2023). “DeBERTaV3: improving DeBERTa using ELECTRA-style pre-training with gradient-disentangled embedding sharing.” Proc. ICLR.
Relative-only attention reached 0.388 on an absolute task; adding absolute positions before the output reached 1.000.
Practice
Show:
Level 0
Problem 1Level 0
Score terms per head in DeBERTa?
3.
Problem 2Level 0
Relative buckets for k = 3?
6.
Problem 3Level 0
Year DeBERTa appeared at ICLR?
2021.
Level 1
Problem 4Level 1
DeBERTa’s score divisor √(3d) for d = 64?
13.856.
Problem 5Level 1
Relative embedding parameters for k = 256, d = 768?
393216.
Problem 6Level 1
Bucket δ(10, 6) for k = 3?
4 ≥ 3 → 2k − 1 = 5.
Problem 7Level 1
Bucket δ(10, 14) for k = 3?
−4 ≤ −3 → 0.
Level 2
Problem 8Level 2
Distinct buckets used by query 0 over 512 keys with k = 256?
257.
Problem 9Level 2
Absolute-task accuracy with relative positions only in exercise 2?
0.388.
Problem 10Level 2
Absolute-task accuracy with absolute positions at the output?
1.000.
Problem 11Level 2
DeBERTa score multiply–adds for L = 512, d = 64 (three L²d terms)?
50331648.
Problem 12Level 2
Relative-task accuracy with absolute positions added at the output?
0.996.
Problem 13Level 2
Distinct buckets used by query 256 over 512 keys with k = 256?
511.
Level 3
Problem 14Level 3
Show that disentangled scores depend only on i − j when content is identical.
With Hᵢ = h for all i, Qcᵢ = Qc and Kcⱼ = Kc are constant. The score is Qc·Kc + Qc·Kr_δ(i,j) + Kc·Qr_δ(j,i), and both δ(i, j) and δ(j, i) are functions of i − j. So Aᵢⱼ depends only on i − j. ∎
Problem 15Level 3
Explain why relative-only attention scored above chance on the absolute task.
Away from the edges every position looks identical to relative attention, so i mod 4 cannot be inferred. Near the edges, buckets for missing keys are absent: the first token has no keys to its left. This asymmetry reveals how close a token is to the ends, so a few positions can be identified, giving 0.388 instead of 0.25. ∎
Problem 16Level 3
Compare DeBERTa’s position handling with BERT’s and RoPE’s.
BERT adds learned absolute positions to inputs, mixing content and position and limiting length. RoPE encodes relative position by rotating queries and keys, with no parameters, inside one dot product. DeBERTa uses separate relative-position embeddings with dedicated content-to-position and position-to-content terms, plus absolute positions late in the network. It is more expressive but costs about three times the score computation. ∎
Problem 17Level 3
Advise on encoding position for fixed-format maintenance records (field order matters).
Field identity depends on absolute position (or a field marker), so pure relative attention is not enough. Use relative attention for flexibility plus absolute position or explicit field-type embeddings, in the spirit of DeBERTa’s decoder. Test on records with shifted or missing fields to check robustness. ∎
Level 4
Problem 18Level 4
Absolute-task accuracy with no positions?
0.251.
Problem 19Level 4
DeBERTa-1.5B’s SuperGLUE score?
90.3.
Problem 20Level 4
SuperGLUE human baseline?
89.8.
Coding practice
🐍 Code 1 (Level 1): relative bucket
Clip i − j into 2k buckets.
🐍 Code 2 (Level 0): score scale
√(3d).
🐍 Code 3 (Level 0): relative embeddings
2k × d, shared or per layer.
🐍 Code 4 (Level 0): score cost
One or three L²d terms.
🐍 Code 5 (Level 1): distinct buckets
Buckets a query actually uses.
Topic test
A timed test drawn at random from this topic's question bank. Each attempt gets new questions. For a longer test across the whole chapter, see the chapter test, or mix topics in the question bag.
DeBERTa: topic test
10 questions, drawn at random from a bank of 21. Every attempt is different.
⏱ Time limit: 15 minutes. The test submits itself when time runs out.
Levels covered: 0, 1, 2, 3, 4. No hints or solutions until you submit.