DeBERTa › Worked Example: Tracing the Attention Computation
Language AI HandbookAll chapters 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 and with content vectors:
And suppose our (simplified, square) projection matrices are all the identity: . For relative positions, suppose:
Step 1: Compute the three attention components for the A-to-B score .
Content-to-content:
The two tokens have orthogonal content vectors, so the semantic similarity is zero.
Content-to-position:
Token 's content (dimension 0 active) aligns with the first component of the position embedding for "1 ahead."
Position-to-content:
Token 's relative position from 's perspective aligns with token 's content vector.
Step 2: Sum the three components.
The key insight from this example: even though the semantic content of and 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 and conclude that should not attend to at all, even if their relative position is syntactically important.
Step 3: Scale and normalize.
With a single head and , the scaling factor is . The scaled score is . For a two-token sequence we compute both and , apply softmax across both, and use the resulting probabilities to weight the value vectors. The full softmax step would involve computing by the same three-component method, but the key point is clear: the positional components changed what attention weights the model will learn.