DeBERTa › Worked Example: Tracing the Attention Computation

Michael BrenndoerferUpdated July 20, 20253 min read
Language AI HandbookAll chapters
Part 24 of 67BERT and Variants
Chapter 176DeBERTa › Worked Example: Tracing the Attention Computation
Reading section 6 of 17 · Worked Example: Tracing the Attention Computation · 3 minSection 6 of 17 · 3 min

Book contents

Section 6 of 17 · 3 min read

DeBERTa › Worked Example: Tracing the Attention ComputationOne section · Sections in this chapter6 / 17 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

To make the mathematics concrete, let's trace a minimal numerical example through the full disentangled attention computation. We will use a two-token sequence with a two-dimensional hidden space and a single attention head, keeping all numbers small enough to follow by hand.

Suppose we have tokens AA and BB with content vectors:

HA=[1,0],HB=[0,1]H_A = [1, 0], \quad H_B = [0, 1]

And suppose our (simplified, square) projection matrices are all the identity: Wqc=Wkc=Wqp=Wkp=IW_q^c = W_k^c = W_q^p = W_k^p = I. For relative positions, suppose:

PA∣B=[0.5,0.5](B is 1 position ahead of A)P_{A|B} = [0.5, 0.5] \quad \text{(B is 1 position ahead of A)} PB∣A=[−0.5,0.5](A is 1 position behind B)P_{B|A} = [-0.5, 0.5] \quad \text{(A is 1 position behind B)}

Step 1: Compute the three attention components for the A-to-B score AABA_{AB}.

Content-to-content:

HAWqcWkc⊤HB⊤=[1,0]⋅I⋅I⋅[0,1]⊤=[1,0]⋅[0,1]⊤=0\begin{aligned} H_A W_q^c W_k^{c\top} H_B^\top &= [1, 0] \cdot I \cdot I \cdot [0, 1]^\top \\ &= [1, 0] \cdot [0, 1]^\top \\ &= 0 \end{aligned}

The two tokens have orthogonal content vectors, so the semantic similarity is zero.

Content-to-position:

HAWqcWkp⊤PA∣B⊤=[1,0]⋅I⋅I⋅[0.5,0.5]⊤=[1,0]⋅[0.5,0.5]⊤=0.5\begin{aligned} H_A W_q^c W_k^{p\top} P_{A|B}^\top &= [1, 0] \cdot I \cdot I \cdot [0.5, 0.5]^\top \\ &= [1, 0] \cdot [0.5, 0.5]^\top \\ &= 0.5 \end{aligned}

Token AA's content (dimension 0 active) aligns with the first component of the position embedding for "1 ahead."

Position-to-content:

PB∣AWqpWkc⊤HB⊤=[−0.5,0.5]⋅I⋅I⋅[0,1]⊤=[−0.5,0.5]⋅[0,1]⊤=0.5\begin{aligned} P_{B|A} W_q^p W_k^{c\top} H_B^\top &= [-0.5, 0.5] \cdot I \cdot I \cdot [0, 1]^\top \\ &= [-0.5, 0.5] \cdot [0, 1]^\top \\ &= 0.5 \end{aligned}

Token BB's relative position from AA's perspective aligns with token BB's content vector.

Step 2: Sum the three components.

AAB=0+0.5+0.5=1.0A_{AB} = 0 + 0.5 + 0.5 = 1.0

The key insight from this example: even though the semantic content of AA and BB is completely orthogonal (zero content-to-content score), the model still produces a non-zero attention score because the positional components contribute. This is exactly the expressive advantage of disentangled attention: the model can attend based on positional relationship even when semantic similarity is low. Standard BERT attention would compute [1,0]⋅[0,1]⊤=0[1,0] \cdot [0,1]^\top = 0 and conclude that AA should not attend to BB at all, even if their relative position is syntactically important.

Step 3: Scale and normalize.

With a single head and d=2d = 2, the scaling factor is 1/2≈0.7071/\sqrt{2} \approx 0.707. The scaled score is 1.0×0.707=0.7071.0 \times 0.707 = 0.707. For a two-token sequence we compute both AABA_{AB} and AAAA_{AA}, apply softmax across both, and use the resulting probabilities to weight the value vectors. The full softmax step would involve computing AAAA_{AA} by the same three-component method, but the key point is clear: the positional components changed what attention weights the model will learn.