Skip to content

Language models, retrieval, prompting, and language adaptation · Contextual and sentence encoders · LNG015 · Architecture

DeBERTa

Separates content and relative-position representations in attention and changes decoding during masked pretraining. It is useful for text understanding tasks where fine-grained interactions and relative order matter.

Partly runs in CodeSplash Component demo, not a trained model CodeSplash's own run

In CodeSplash

A CodeSplash method covers part of this entry; some options or variants aren't built yet.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Partly runs in CodeSplash
Code example
Component demo, not a trained model
Who ran it
CodeSplash's own run
CodeSplash's run
Verified in Python and R through describe.zero_shot_text. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Separates content and relative-position representations in attention and changes decoding during masked pretraining. It is useful for text understanding tasks where fine-grained interactions and relative order matter.

Question / objective

Can separately modeling token content and relative position improve contextual representation?

Architecture / computational structure

Disentangled attention combines content and relative-position interactions; enhanced mask decoding supports pretraining. Learned projections/relative embeddings and task heads are parameters; position ranges and variant choices are hyperparameters.

Assumptions

The simplified equation describes the design, not an interchangeable implementation of every DeBERTa version.

Formula / computational definition

Aij∝qi⊤kj+qi⊤ki−jrel+(qj−irel)⊤kj.A_{ij}\propto q_i^\top k_j+q_i^\top k^{\mathrm{rel}}_{i-j}+(q^{\mathrm{rel}}_{j-i})^\top k_j.

Attention logits combine content–content and content–relative-position interactions, schematically a_ij=q_i^c·k_j^c+q_i^c·k_delta^r+q_delta^r·k_j^c. Here delta is a relative position; the actual implementation uses projections and indexing conventions.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Relative-position buckets/range, hidden width, heads, depth, decoder choices, and task-head/fine-tuning settings.

Fitting, fine-tuning, and adaptation

Use the exact DeBERTa generation/configuration, then fit a task head, compatible adapters, or full encoder weights. Domain adaptation uses an appropriate masked-token objective. Tune low learning rates and validation stopping to protect pretrained structure and assess long-input truncation effects.

How to use and calibrate

Use task-specific scores, calibration, domain/length slices, and fair comparisons controlling pretraining scale. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.

Example application

Build a strong entailment or classification model sensitive to word order.

Interpretation / pitfalls

Watch out

Later variants change pretraining objectives; they should be documented as variants rather than silently mixed into one experiment. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.

Python and R code examples

Full implementation route: Transformers has version-specific DeBERTa classes. R reticulate can load the same artifacts; a native port must reproduce relative-position indexing and the selected variant's architecture.

Example scope: Simplified DeBERTa content-content/content-position/position-content attention score.

Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.

Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Simplified DeBERTa content-content/content-position/position-content attention score.

Shared setup for this chapter
import math

def softmax(xs):
    m = max(xs)
    es = [math.exp(x-m) for x in xs]
    return [e/sum(es) for e in es]

def dot(a, b):
    return sum(x*y for x, y in zip(a, b))

def matvec(matrix, vector):
    return [dot(row, vector) for row in matrix]

def meanvec(rows):
    return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]

def sigmoid(x):
    return 1/(1+math.exp(-x))

def nll(probabilities):
    return -sum(math.log(p) for p in probabilities)
qc, kc, qr, kr = [0.4,0.2], [0.1,0.6], [0.3,-0.2], [0.5,0.1]
score = dot(qc,kc) + dot(qc,kr) + dot(qr,kc)
print(score)
R

Component demo, not a trained model — Simplified DeBERTa content-content/content-position/position-content attention score.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
qc <- c(0.4,0.2); kc <- c(0.1,0.6); qr <- c(0.3,-0.2); kr <- c(0.5,0.1)
score <- sum(qc*kc) + sum(qc*kr) + sum(qr*kc)
print(score)

Sources

From the CodeSplash reference, entry LNG015.