A long-context extension of
ModernBERT-base
fine-tuned for 32K token context length using YaRN (Yet another RoPE extensioN) scaling.
Model Description
This model extends the original ModernBERT-base from 8,192 tokens to
32,768 tokens
context length while preserving the base model's capabilities. It was fine-tuned using specialized techniques to maintain long-range retrieval performance:
YaRN RoPE Scaling
: Factor of 4.0x to extend positional embeddings
from transformers import AutoModelForMaskedLM, AutoTokenizer
model = AutoModelForMaskedLM.from_pretrained("llm-semantic-router/modernbert-base-32k")
tokenizer = AutoTokenizer.from_pretrained("llm-semantic-router/modernbert-base-32k")
text = "The capital of France is [MASK]."
inputs = tokenizer(text, return_tensors="pt")
outputs = model(**inputs)
# Get predictions
mask_idx = (inputs.input_ids == tokenizer.mask_token_id).nonzero(as_tuple=True)[1]
logits = outputs.logits[0, mask_idx, :]
top_tokens = logits.topk(5).indices[0]
print([tokenizer.decode(t) for t in top_tokens])
# ['Paris', 'Lyon', 'Nancy', ...]
Long Context Usage
# For sequences longer than 8192 tokens, YaRN scaling is automatically applied
long_text = "..." * 10000# Very long document
inputs = tokenizer(
long_text,
return_tensors="pt",
max_length=32768,
truncation=True
)
outputs = model(**inputs)
Feature Extraction
from transformers import AutoModel
model = AutoModel.from_pretrained("llm-semantic-router/modernbert-base-32k")
inputs = tokenizer(text, return_tensors="pt")
outputs = model(**inputs)
embeddings = outputs.last_hidden_state # [batch, seq_len, 768]# Mean pooling for sentence embedding
attention_mask = inputs["attention_mask"]
masked_embeddings = embeddings * attention_mask.unsqueeze(-1)
sentence_embedding = masked_embeddings.sum(1) / attention_mask.sum(1, keepdim=True)
Limitations
Passkey Retrieval at Long Distances
: Like the base model, this model struggles with needle-in-haystack retrieval beyond ~1K tokens. This is a fundamental limitation of MLM architectures, not the fine-tuning.
Context Utilization
: At very long contexts (16K+), the model may not always benefit from additional context for MLM predictions. This is expected behavior for encoder-only models.
@article{modernbert2024,
title={Smarter, Better, Faster, Longer: A Modern Bidirectional Encoder for Fast, Memory Efficient, and Long Context Finetuning and Inference},
author={Warner, Benjamin and Chaffin, Antoine and Geiger, Benjamin and Werra, Leandro von and Tunstall, Lewis and Bartolo, Max and Thrush, Tristan},
journal={arXiv preprint},
year={2024}
}
modernbert-base-32k huggingface.co is an AI model on huggingface.co that provides modernbert-base-32k's model effect (), which can be used instantly with this llm-semantic-router modernbert-base-32k model. huggingface.co supports a free trial of the modernbert-base-32k model, and also provides paid use of the modernbert-base-32k. Support call modernbert-base-32k model through api, including Node.js, Python, http.
modernbert-base-32k huggingface.co is an online trial and call api platform, which integrates modernbert-base-32k's modeling effects, including api services, and provides a free online trial of modernbert-base-32k, you can try modernbert-base-32k online for free by clicking the link below.
llm-semantic-router modernbert-base-32k online free url in huggingface.co:
modernbert-base-32k is an open source model from GitHub that offers a free installation service, and any user can find modernbert-base-32k on GitHub to install. At the same time, huggingface.co provides the effect of modernbert-base-32k install, users can directly use modernbert-base-32k installed effect in huggingface.co for debugging and trial. It also supports api for free installation.
modernbert-base-32k install url in huggingface.co: