A long-context extension of
ModernBERT-base fine-tuned for 32K token context length using YaRN (Yet another RoPE extensioN) scaling.
1learning_rate: 1e-5
2lr_scheduler: constant_with_warmup
3warmup_ratio: 0.1
4epochs: 1
5batch_size: 6
6gradient_accumulation: 1
7precision: bf16
8max_length: 32768
9
10# RoPE Scaling
11rope_scaling_type: yarn
12rope_scaling_factor: 4.0
13
14# Retrieval Masking
15mlm_probability: 0.30
16retrieval_probability: 0.10
17min_distance_for_retrieval: 512
18
19# EWC Regularization
20ewc_lambda: 1000.0
21ewc_samples: 200
1from transformers import AutoModelForMaskedLM, AutoTokenizer
2
3model = AutoModelForMaskedLM.from_pretrained("llm-semantic-router/modernbert-base-32k")
4tokenizer = AutoTokenizer.from_pretrained("llm-semantic-router/modernbert-base-32k")
5
6text = "The capital of France is [MASK]."
7inputs = tokenizer(text, return_tensors="pt")
8outputs = model(**inputs)
9
10# Get predictions
11mask_idx = (inputs.input_ids == tokenizer.mask_token_id).nonzero(as_tuple=True)[1]
12logits = outputs.logits[0, mask_idx, :]
13top_tokens = logits.topk(5).indices[0]
14print([tokenizer.decode(t) for t in top_tokens])
15# ['Paris', 'Lyon', 'Nancy', ...]
1# For sequences longer than 8192 tokens, YaRN scaling is automatically applied
2long_text = "..." * 10000 # Very long document
3inputs = tokenizer(
4 long_text,
5 return_tensors="pt",
6 max_length=32768,
7 truncation=True
8)
9outputs = model(**inputs)
1from transformers import AutoModel
2
3model = AutoModel.from_pretrained("llm-semantic-router/modernbert-base-32k")
4
5inputs = tokenizer(text, return_tensors="pt")
6outputs = model(**inputs)
7embeddings = outputs.last_hidden_state # [batch, seq_len, 768]
8
9# Mean pooling for sentence embedding
10attention_mask = inputs["attention_mask"]
11masked_embeddings = embeddings * attention_mask.unsqueeze(-1)
12sentence_embedding = masked_embeddings.sum(1) / attention_mask.sum(1, keepdim=True)
-
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.
-
Memory Requirements: Processing 32K tokens requires significant GPU memory (~16GB+ for inference).
-
Domain: Trained primarily on web text; may not generalize well to specialized domains without additional fine-tuning.
This model uses several techniques to preserve long-range capabilities during fine-tuning:
Extends positional embeddings from 8K to 32K using Yet another RoPE extensioN with:
1@misc{modernbert-32k,
2 title={ModernBERT-base-32K: Long-Context Extension of ModernBERT},
3 author={LLM Semantic Router},
4 year={2026},
5 url={https://huggingface.co/llm-semantic-router/modernbert-base-32k}
6}
1@article{modernbert2024,
2 title={Smarter, Better, Faster, Longer: A Modern Bidirectional Encoder for Fast, Memory Efficient, and Long Context Finetuning and Inference},
3 author={Warner, Benjamin and Chaffin, Antoine and Geiger, Benjamin and Werra, Leandro von and Tunstall, Lewis and Bartolo, Max and Thrush, Tristan},
4 journal={arXiv preprint},
5 year={2024}
6}