Disentangled Attention in DeBERTa: Separating Content and Position

· 3 min read · 404 words

Authors

DeBERTa's disentangled attention keeps token content and token position in separate representations. Standard Transformers usually add a position embedding to a token embedding before computing attention. Once added, the two sources of information are mixed.

For query position ii and key position jj, DeBERTa builds its attention score from three interactions:

Aij=qickjccontent to content+qickδ(i,j)rcontent to position+qδ(j,i)rkjcposition to content.A_{ij} = \underbrace{q_i^c \cdot k_j^c}_{\text{content to content}} + \underbrace{q_i^c \cdot k_{\delta(i,j)}^r}_{\text{content to position}} + \underbrace{q_{\delta(j,i)}^r \cdot k_j^c}_{\text{position to content}}.

Here cc means content, rr means relative position, and δ(i,j)\delta(i,j) converts the distance between tokens into a bounded position index. Because the raw score adds three terms, DeBERTa scales it by 1/3d1/\sqrt{3d} before softmax, where dd is the vector dimension in this single-head expression.

Loading diagram…

There is deliberately no position-to-position term in this formulation. The model asks how the content at one token relates to another token's content and relative location.

Why separate them?

The meaning of a word depends on both what surrounds it and where those words occur relative to it. Keeping content and position separate lets the model learn asymmetric relations such as “the subject is three positions before this verb” without baking absolute position directly into every token vector.

DeBERTa adds absolute position information later in its enhanced mask decoder, where it is useful for predicting a masked token. That is separate from the relative-position interactions used inside the encoder.

What is easy to misunderstand

  • Relative offsets must be clipped or bucketed for long sequences.
  • The content-to-position and position-to-content lookups use opposite offset directions; mixing their signs silently changes the mechanism.
  • The extra score terms require more work than ordinary content-only attention.
  • Padding and attention masks must be applied after all score components are combined.

Primary source