Skip to main content
Back

Answer.AI's ModernBERT Runs 4x Faster and Handles 16x Longer Texts Than BERT

Answer.AI and LightOn release a drop-in BERT replacement with an 8,192 token context, code-aware training, and up to 4x faster inference.

Pro

  • Answer.AI and LightOn released ModernBERT, a drop-in BERT replacement in base (149M) and large (395M) sizes.
  • Context length jumps to 8,192 tokens, a 16x increase over BERT's 512.
  • Up to 4x faster inference than BERT on mixed-length inputs, twice as fast as DeBERTa.
  • Trained on 2 trillion tokens including web, code, and scientific text, licensed Apache 2.0.
  • First encoder to score above 80 on StackOverflow QA; state-of-the-art on long-context retrieval.
  • Uses RoPE, GeGLU, alternating local/global attention, unpadding, and Flash Attention 2. Paper here.

BERT has quietly powered a huge share of production NLP for years, from retrieval pipelines to spam classifiers to entity extractors. The architecture underneath it has barely changed since 2018, even as decoder-only LLMs sprinted ahead. ModernBERT, a joint release from Answer.AI and LightOn, ports the last six years of transformer engineering back into an encoder that fits on a consumer GPU.

The release ships as two open-weight checkpoints under Apache 2.0: ModernBERT-base at 149M parameters and ModernBERT-large at 395M. Both work as slot-in replacements for any BERT-like model. The base checkpoint has already crossed five million downloads on Hugging Face, which gives a sense of how many pipelines are quietly swapping it in.

Why bother modernizing an encoder

Encoder-only models produce embeddings rather than text, which makes them the workhorses behind retrieval, reranking, classification, and content moderation. ModernBERT processes text up to four times faster than the original BERT and handles inputs up to 8,192 tokens while using less memory. That 8k window is a 16x jump over the 512 tokens that BERT, RoBERTa, and DeBERTa are stuck with, which matters a lot for RAG systems where small chunks lose semantic context.

The team is explicit that this is a Pareto improvement: better accuracy and better speed at once, which had not really happened in the encoder world since RoBERTa. On GLUE, ModernBERT-base is the first base-size model to beat DeBERTaV3 while using less than a fifth of its memory. It is roughly twice as fast on fixed-length inputs and up to four times faster on the mixed-length inputs you actually see in production.

The architecture changes that matter

ModernBERT borrows heavily from the Llama-era transformer recipe. The core swaps:

  • Rotary Positional Embeddings (RoPE) instead of absolute positions, which is what enables the long-context extension.
  • GeGLU activations replacing the old GeLU MLP layers.
  • Alternating local and global attention: every third layer uses full global attention, while the rest use a 128-token sliding window, so complexity does not balloon with sequence length.
  • Unpadding and sequence packing paired with Flash Attention 2, so compute is not wasted on padding tokens.
  • Removal of unnecessary bias terms and an extra normalization layer after embeddings to stabilize training.

Unpadding is the less obvious win. Traditional batched encoders pad every sequence up to the longest one in the batch and then run attention over the dead tokens. ModernBERT concatenates everything into one long stream, masks properly, and only re-pads at the very end. Combined with sequence packing, which greedily bins inputs to fill the max context, this delivers a lot of the real-world speedup.

The team also did hardware-aware sizing. Layer counts and widths were chosen via a constrained grid search targeting common inference GPUs like the RTX 3090/4090, A10, T4, and L4, while keeping embedding dimensions at 768 (base) and 1024 (large) for backwards compatibility with existing BERT pipelines.

Beyond Wikipedia and books

The other big shift is data diversity. Older encoders were mostly trained on Wikipedia and books, and it showed on anything that involved code or technical text. ModernBERT was trained on 2 trillion tokens from web documents, programming code, and scientific articles.

Training happened in three phases: 1.7T tokens at a 1,024 sequence length, then 250B tokens at 8,192 for long-context adaptation, then a 50B-token annealing phase with a different data mix. The team used StableAdamW with a trapezoidal learning rate schedule on 8 H100s, kept masking rate at 30% instead of the classic 15%, and dropped Next-Sentence Prediction entirely.

Two training tricks are worth flagging. Batch-size warmup ramps the batch size up from small during the noisy early phase so weights update more often per token. The large model was initialized by tiling the base model weights across the larger shape, borrowed from Microsoft's Phi work, which stacked nicely with batch-size warmup for faster convergence.

Where it lands on benchmarks

The headline numbers, from the model card:

ModelGLUE (NLU)BEIR (DPR)MLDR long-context (ColBERT)CodeSearchNetStackQA
BERT-base84.738.928.141.259.5
RoBERTa-base86.437.728.244.359.6
DeBERTaV3-base88.120.221.917.518.6
GTE-en-MLM-base85.641.469.344.971.4
ModernBERT-base88.441.680.256.473.6

The long-context retrieval and code scores are the eye-catchers. ModernBERT is the first encoder-only model to score above 80 on the StackOverflow QA dataset. For anyone building code search, code-aware RAG, or an IDE feature that has to embed a repo, there was no real open baseline before this.

Using it in practice

ModernBERT is a masked language model, so out of the box it does fill-mask. For anything useful you fine-tune it exactly like a regular BERT. It ships in transformers from v4.48.0 onward, and the maintainers strongly recommend installing flash-attn for full speed.

from transformers import AutoTokenizer, AutoModelForMaskedLM
model_id = "answerdotai/ModernBERT-base"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForMaskedLM.from_pretrained(model_id, device_map="auto")
text = "The capital of France is [MASK]."
inputs = tokenizer(text, return_tensors="pt")
outputs = model(**inputs)

One small gotcha: unlike classic BERT, ModernBERT does not use token_type_ids, so you can drop that argument in existing fine-tuning code. It pairs well with Sentence Transformers for embeddings and rerankers, and there are already ColBERT-style multi-vector variants trained on top of it.

What it is not good for

A few honest limitations. The training data is English and code only, so multilingual performance will lag, and the community has been asking for Chinese and other language variants since day one. Using the full 8,192-token window is still slower than short-context inference, so do not reach for it if your inputs are tweets. Like any pretrained model, it also inherits biases from its training corpus.

ModernBERT is a base model. It cannot generate text, it cannot chat, and the raw fill-mask outputs are not useful on their own. The value shows up after fine-tuning on classification, retrieval, reranking, or entity extraction.

What actually transfers from decoder-land

The interesting thing about ModernBERT is not that it beats DeBERTaV3 on GLUE. It is that the same engineering that made decoder-only LLMs so much better, namely RoPE, GLU variants, Flash Attention, better data mixing, and longer contexts, transfers cleanly to encoders when someone puts in the work. The authors expect ModernBERT to become the new standard in the many applications where encoder-only models are now deployed, including RAG pipelines and recommendation systems.

For anyone running a classifier, retriever, or embedding model in production, the calculation is straightforward: same or better accuracy, longer context, faster inference, smaller memory footprint, and it drops into existing BERT fine-tuning code. The full technical details are in the arXiv paper, and the training code lives in the Answer.AI GitHub repo along with intermediate checkpoints for anyone who wants to anneal from a stable pre-decay state on their own domain data.