Instructions to use kerasformers/modernbert_base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- KerasFormers
How to use kerasformers/modernbert_base with KerasFormers:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Keras
How to use kerasformers/modernbert_base with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://kerasformers/modernbert_base") - Notebooks
- Google Colab
- Kaggle
Run ModernBERT with Keras 3: JAX, PyTorch, or TensorFlow
kerasformers/modernbert_base
Paper: Smarter, Better, Faster, Longer: A Modern Bidirectional Encoder (arXiv:2412.13663) · HF Papers
ModernBERT is Answer.AI / LightOn's modernized bidirectional transformer text encoder: rotary position embeddings, attention that alternates between a global (full) layer and local sliding-window layers, GeGLU feed-forwards, and pre-LayerNorm, with an 8192-token context. Byte-level BPE tokenizer; mask token [MASK]. No token-type ids.
For more details on the model, please go to the upstream model card.
Pure-Keras 3 conversion of answerdotai/ModernBERT-base for kerasformers. One implementation runs unmodified on TensorFlow / Torch / JAX.
This is a fill-mask / encoder checkpoint (ModernBertMaskedLM, modernbert_base). Task heads (sequence / token classify, QA, multiple choice) load via hf: fine-tunes.
✨ Quick start (fill-mask)
import os
os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow"
from kerasformers.models.modernbert import ModernBertMaskedLM, ModernBertTokenizer
mlm = ModernBertMaskedLM.from_weights("kerasformers/modernbert_base")
tokenizer = ModernBertTokenizer.from_weights("kerasformers/modernbert_base")
inputs = tokenizer("The capital of France is [MASK].")
logits = mlm(inputs) # (1, L, vocab_size)
mask = int((inputs["input_ids"][0] == tokenizer.mask_token_id).argmax())
print(tokenizer.decode([int(logits[0, mask].argmax())]))
Load any ModernBERT variant the same way with from_weights("kerasformers/<variant>"):
| Variant | Hub | layers | embed_dim |
|---|---|---|---|
modernbert_base |
kerasformers/modernbert_base |
22 | 768 |
modernbert_large |
kerasformers/modernbert_large |
28 | 1024 |
Available classes
Load any of these from this repo with from_weights("kerasformers/modernbert_base") (or on the fly via the hf: prefix). The pretrained backbone is shared; task heads not stored in this checkpoint start randomly initialized, ready for fine-tuning (or load a hf: fine-tune).
| Class | Task |
|---|---|
ModernBertModel |
Encoder backbone |
ModernBertMaskedLM |
Masked language modeling (fill-mask) |
ModernBertSequenceClassify |
Sequence classification |
ModernBertTokenClassify |
Token classification (NER / POS) |
ModernBertQnA |
Extractive question answering |
ModernBertMultipleChoice |
Multiple choice |
from kerasformers.models.modernbert import ModernBertSequenceClassify
model = ModernBertSequenceClassify.from_weights("kerasformers/modernbert_base")
Tips
- Set
KERAS_BACKENDbefore importing Keras / kerasformers. - Prefer
ModernBertTokenizer.from_weights(...)so tokenization matches. - Use
[MASK](not<mask>). - ModernBERT has no token-type ids; the tokenizer emits only
input_ids/attention_mask. - See ModernBERT docs and Loading Weights.
- Community / upstream safetensors still work via the
hf:prefix, e.g.ModernBertMaskedLM.from_weights("hf:answerdotai/ModernBERT-base").
Special Thanks
A huge thank you to the Answer.AI and LightOn authors for creating and releasing ModernBERT.
License: Apache 2.0.
- Downloads last month
- 3
Model tree for kerasformers/modernbert_base
Base model
answerdotai/ModernBERT-base