Instructions to use zeromodels/modernbert_base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- KerasFormers
How to use zeromodels/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 zeromodels/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://zeromodels/modernbert_base") - Notebooks
- Google Colab
- Kaggle
File size: 4,142 Bytes
26467e8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | ---
pipeline_tag: fill-mask
license: apache-2.0
base_model: answerdotai/ModernBERT-base
library_name: kerasformers
tags:
- keras
- kerasformers
- modernbert
- fill-mask
- text-encoder
- arxiv:2412.13663
- pytorch
- jax
- tf
---
# Run ModernBERT with Keras 3: JAX, PyTorch, or TensorFlow
[](https://github.com/IMvision12/KerasFormers) [](https://imvision12.github.io/KerasFormers/modernbert/)
# kerasformers/modernbert_base
Paper: [Smarter, Better, Faster, Longer: A Modern Bidirectional Encoder (arXiv:2412.13663)](https://arxiv.org/abs/2412.13663) · [HF Papers](https://huggingface.co/papers/2412.13663)
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](https://huggingface.co/answerdotai/ModernBERT-base).
Pure-**Keras 3** conversion of [`answerdotai/ModernBERT-base`](https://huggingface.co/answerdotai/ModernBERT-base) for [kerasformers](https://github.com/IMvision12/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)
```python
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`](https://huggingface.co/kerasformers/modernbert_base) | 22 | 768 |
| `modernbert_large` | [`kerasformers/modernbert_large`](https://huggingface.co/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 |
```python
from kerasformers.models.modernbert import ModernBertSequenceClassify
model = ModernBertSequenceClassify.from_weights("kerasformers/modernbert_base")
```
## Tips
- Set `KERAS_BACKEND` **before** 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](https://imvision12.github.io/KerasFormers/modernbert/) and [Loading Weights](https://imvision12.github.io/KerasFormers/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.
|